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"""
16 def __init__(self, *args, **kargs):
20 def serInit(self, port=None):
21 """Open the serial port"""
23 if port is None and os.environ.get("GOODFET")!=None:
24 glob_list = glob.glob(os.environ.get("GOODFET"));
25 if len(glob_list) > 0:
28 glob_list = glob.glob("/dev/tty.usbserial*");
29 if len(glob_list) > 0:
32 glob_list = glob.glob("/dev/ttyUSB*");
33 if len(glob_list) > 0:
36 self.serialport = serial.Serial(
40 parity = serial.PARITY_NONE
44 self.serialport.setRTS(1);
45 #Drop DTR, which is !RST, low to begin the app.
46 self.serialport.setDTR(0);
47 self.serialport.flushInput()
48 self.serialport.flushOutput()
50 #Read and handle the initial command.
52 self.readcmd(); #Read the first command.
54 print "Verb %02x is wrong. Incorrect firmware?" % self.verb;
56 def getbuffer(self,size=0x1c00):
57 writecmd(0,0xC2,[size&0xFF,(size>>16)&0xFF]);
58 print "Got %02x%02x buffer size." % (self.data[1],self.data[0]);
59 def writecmd(self, app, verb, count=0, data=[]):
60 """Write a command and some data to the GoodFET."""
61 self.serialport.write(chr(app));
62 self.serialport.write(chr(verb));
65 # count=len(data); #Initial count ignored.
67 #print "TX %02x %02x" % (app,verb);
69 #little endian 16-bit length
70 self.serialport.write(chr(count&0xFF));
71 self.serialport.write(chr(count>>8));
73 #print "count=%02x, len(data)=%04x" % (count,len(data));
76 for i in range(0,count):
77 #print "Converting %02x at %i" % (data[i],i)
81 self.serialport.write(outstr);
92 """Read a reply from the GoodFET."""
95 self.app=ord(self.serialport.read(1));
96 #print "APP=%2x" % self.app;
97 self.verb=ord(self.serialport.read(1));
98 #print "VERB=%02x" % self.verb;
100 ord(self.serialport.read(1))
101 +(ord(self.serialport.read(1))<<8)
104 #Debugging string; print, but wait.
105 if self.app==0xFF and self.verb==0xFF:
106 print "DEBUG %s" % self.serialport.read(self.count);
108 self.data=self.serialport.read(self.count);
111 def glitchAPP(self,app):
112 """Glitch into a device by its application."""
113 self.data=[app&0xff];
114 self.writecmd(self.GLITCHAPP,0x80,1,self.data);
115 #return ord(self.data[0]);
116 def glitchVERB(self,app,verb, data):
117 """Glitch during a transaction.."""
118 self.data=[app&0xff, verb&0xFF]+data;
119 self.writecmd(self.GLITCHAPP,0x81,len(self.data),self.data);
120 #return ord(self.data[0]);
121 def glitchVoltages(self,low=0x0880, high=0x0fff):
122 """Set glitching voltages. (0x0fff is max.)"""
123 self.data=[low&0xff, (low>>8)&0xff,
124 high&0xff, (high>>8)&0xff];
125 self.writecmd(self.GLITCHAPP,0x90,4,self.data);
126 #return ord(self.data[0]);
127 def glitchRate(self,count=0x0800):
128 """Set glitching count period."""
129 self.data=[count&0xff, (count>>8)&0xff];
130 self.writecmd(self.GLITCHAPP,0x91,2,
132 #return ord(self.data[0]);
136 def silent(self,s=0):
137 """Transmissions halted when 1."""
139 print "besilent is %i" % self.besilent;
140 self.writecmd(0,0xB0,1,[s]);
143 """Write a byte to P5OUT."""
144 self.writecmd(0,0xA1,1,[byte]);
146 """Write a byte to P5DIR."""
147 self.writecmd(0,0xA0,1,[byte]);
148 def peekbyte(self,address):
149 """Read a byte of memory from the monitor."""
150 self.data=[address&0xff,address>>8];
151 self.writecmd(0,0x02,2,self.data);
153 return ord(self.data[0]);
154 def peekword(self,address):
155 """Read a word of memory from the monitor."""
156 return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
157 def pokebyte(self,address,value):
158 """Set a byte of memory by the monitor."""
159 self.data=[address&0xff,address>>8,value];
160 self.writecmd(0,0x03,3,self.data);
161 return ord(self.data[0]);
162 def dumpmem(self,begin,end):
165 print "%04x %04x" % (i, self.peekword(i));
167 def monitor_ram_pattern(self):
168 """Overwrite all of RAM with 0xBEEF."""
169 self.writecmd(0,0x90,0,self.data);
171 def monitor_ram_depth(self):
172 """Determine how many bytes of RAM are unused by looking for 0xBEEF.."""
173 self.writecmd(0,0x91,0,self.data);
174 return ord(self.data[0])+(ord(self.data[1])<<8);
183 def setBaud(self,baud):
184 """Change the baud rate. TODO fix this."""
185 rates=self.baudrates;
187 print "Changing FET baud."
188 self.serialport.write(chr(0x00));
189 self.serialport.write(chr(0x80));
190 self.serialport.write(chr(1));
191 self.serialport.write(chr(baud));
193 print "Changed host baud."
194 self.serialport.setBaudrate(rates[baud]);
196 self.serialport.flushInput()
197 self.serialport.flushOutput()
199 print "Baud is now %i." % rates[baud];
202 return ord(self.serialport.read(1));
204 for r in self.baudrates:
205 print "\nTrying %i" % r;
206 self.serialport.setBaudrate(r);
208 self.serialport.flushInput()
209 self.serialport.flushOutput()
211 for i in range(1,10):
214 print "Read %02x %02x %02x %02x" % (
215 self.readbyte(),self.readbyte(),self.readbyte(),self.readbyte());
216 def monitortest(self):
217 """Self-test several functions through the monitor."""
218 print "Performing monitor self-test.";
220 if self.peekword(0x0c00)!=0x0c04 and self.peekword(0x0c00)!=0x0c06:
221 print "ERROR Fetched wrong value from 0x0c04.";
222 self.pokebyte(0x0021,0); #Drop LED
223 if self.peekbyte(0x0021)!=0:
224 print "ERROR, P1OUT not cleared.";
225 self.pokebyte(0x0021,1); #Light LED
227 print "Self-test complete.";
232 """Move the FET into the I2C application."""
233 self.writecmd(0x02,0x10,0,self.data); #SPI/SETUP
235 """Start an I2C transaction."""
236 self.writecmd(0x02,0x20,0,self.data); #SPI/SETUP
238 """Stop an I2C transaction."""
239 self.writecmd(0x02,0x21,0,self.data); #SPI/SETUP
240 def I2Cread(self,len=1):
241 """Read len bytes by I2C."""
242 self.writecmd(0x02,0x00,1,[len]); #SPI/SETUP
244 def I2Cwrite(self,bytes):
245 """Write bytes by I2C."""
246 self.writecmd(0x02,0x01,len(bytes),bytes); #SPI/SETUP
247 return ord(self.data[0]);