2 # GoodFET Client Library
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
6 # This code is being rewritten and refactored. You've been warned!
8 import sys, time, string, cStringIO, struct, glob, serial, os;
12 """GoodFET Client Library"""
13 def __init__(self, *args, **kargs):
17 def serInit(self, port=None):
18 """Open the serial port"""
20 if port is None and os.environ.get("GOODFET")!=None:
21 glob_list = glob.glob(os.environ.get("GOODFET"));
22 if len(glob_list) > 0:
25 glob_list = glob.glob("/dev/tty.usbserial*");
26 if len(glob_list) > 0:
29 glob_list = glob.glob("/dev/ttyUSB*");
30 if len(glob_list) > 0:
33 self.serialport = serial.Serial(
37 parity = serial.PARITY_NONE
39 #Drop DTR, which is !RST, low to begin the app.
40 self.serialport.setDTR(0);
41 self.serialport.flushInput()
42 self.serialport.flushOutput()
44 #Read and handle the initial command.
46 self.readcmd(); #Read the first command.
48 print "Verb %02x is wrong. Incorrect firmware?" % self.verb;
50 def getbuffer(self,size=0x1c00):
51 writecmd(0,0xC2,[size&0xFF,(size>>16)&0xFF]);
52 print "Got %02x%02x buffer size." % (self.data[1],self.data[0]);
53 def writecmd(self, app, verb, count=0, data=[]):
54 """Write a command and some data to the GoodFET."""
55 self.serialport.write(chr(app));
56 self.serialport.write(chr(verb));
59 #print "TX %02x %02x" % (app,verb);
61 #little endian 16-bit length
62 self.serialport.write(chr(count&0xFF));
63 self.serialport.write(chr(count>>8));
65 #print "count=%02x, len(data)=%04x" % (count,len(data));
68 for i in range(0,count):
69 #print "Converting %02x at %i" % (data[i],i)
72 self.serialport.write(outstr);
83 """Read a reply from the GoodFET."""
86 self.app=ord(self.serialport.read(1));
87 #print "APP=%2x" % self.app;
88 self.verb=ord(self.serialport.read(1));
89 #print "VERB=%02x" % self.verb;
91 ord(self.serialport.read(1))
92 +(ord(self.serialport.read(1))<<8)
95 #Debugging string; print, but wait.
96 if self.app==0xFF and self.verb==0xFF:
97 print "DEBUG %s" % self.serialport.read(self.count);
99 self.data=self.serialport.read(self.count);
103 def silent(self,s=0):
104 """Transmissions halted when 1."""
106 print "besilent is %i" % self.besilent;
107 self.writecmd(0,0xB0,1,[s]);
110 """Write a byte to P5OUT."""
111 self.writecmd(0,0xA1,1,[byte]);
113 """Write a byte to P5DIR."""
114 self.writecmd(0,0xA0,1,[byte]);
115 def peekbyte(self,address):
116 """Read a byte of memory from the monitor."""
117 self.data=[address&0xff,address>>8];
118 self.writecmd(0,0x02,2,self.data);
120 return ord(self.data[0]);
121 def peekword(self,address):
122 """Read a word of memory from the monitor."""
123 return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
124 def pokebyte(self,address,value):
125 """Set a byte of memory by the monitor."""
126 self.data=[address&0xff,address>>8,value];
127 self.writecmd(0,0x03,3,self.data);
128 return ord(self.data[0]);
129 def dumpmem(self,begin,end):
132 print "%04x %04x" % (i, self.peekword(i));
134 def monitor_ram_pattern(self):
135 """Overwrite all of RAM with 0xBEEF."""
136 self.writecmd(0,0x90,0,self.data);
138 def monitor_ram_depth(self):
139 """Determine how many bytes of RAM are unused by looking for 0xBEEF.."""
140 self.writecmd(0,0x91,0,self.data);
141 return ord(self.data[0])+(ord(self.data[1])<<8);
150 def setBaud(self,baud):
151 """Change the baud rate. TODO fix this."""
152 rates=self.baudrates;
154 print "Changing FET baud."
155 self.serialport.write(chr(0x00));
156 self.serialport.write(chr(0x80));
157 self.serialport.write(chr(1));
158 self.serialport.write(chr(baud));
160 print "Changed host baud."
161 self.serialport.setBaudrate(rates[baud]);
163 self.serialport.flushInput()
164 self.serialport.flushOutput()
166 print "Baud is now %i." % rates[baud];
169 return ord(self.serialport.read(1));
171 for r in self.baudrates:
172 print "\nTrying %i" % r;
173 self.serialport.setBaudrate(r);
175 self.serialport.flushInput()
176 self.serialport.flushOutput()
178 for i in range(1,10):
181 print "Read %02x %02x %02x %02x" % (
182 self.readbyte(),self.readbyte(),self.readbyte(),self.readbyte());
183 def monitortest(self):
184 """Self-test several functions through the monitor."""
185 print "Performing monitor self-test.";
187 if self.peekword(0x0c00)!=0x0c04 and self.peekword(0x0c00)!=0x0c06:
188 print "ERROR Fetched wrong value from 0x0c04.";
189 self.pokebyte(0x0021,0); #Drop LED
190 if self.peekbyte(0x0021)!=0:
191 print "ERROR, P1OUT not cleared.";
192 self.pokebyte(0x0021,1); #Light LED
194 print "Self-test complete.";
199 """Move the FET into the I2C application."""
200 self.writecmd(0x02,0x10,0,self.data); #SPI/SETUP
202 """Start an I2C transaction."""
203 self.writecmd(0x02,0x20,0,self.data); #SPI/SETUP
205 """Stop an I2C transaction."""
206 self.writecmd(0x02,0x21,0,self.data); #SPI/SETUP
207 def I2Cread(self,len=1):
208 """Read len bytes by I2C."""
209 self.writecmd(0x02,0x00,1,[len]); #SPI/SETUP
211 def I2Cwrite(self,bytes):
212 """Write bytes by I2C."""
213 self.writecmd(0x02,0x01,len(bytes),bytes); #SPI/SETUP
214 return ord(self.data[0]);