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
43 #This might cause problems, but it makes failure graceful.
44 #self.serialport._timeout = 5;
46 #Explicitly set RTS and DTR to halt board.
47 self.serialport.setRTS(1);
48 self.serialport.setDTR(1);
49 #Drop DTR, which is !RST, low to begin the app.
50 self.serialport.setDTR(0);
51 self.serialport.flushInput()
52 self.serialport.flushOutput()
54 #Read and handle the initial command.
56 self.readcmd(); #Read the first command.
58 print "Verb %02x is wrong. Incorrect firmware?" % self.verb;
60 def getbuffer(self,size=0x1c00):
61 writecmd(0,0xC2,[size&0xFF,(size>>16)&0xFF]);
62 print "Got %02x%02x buffer size." % (self.data[1],self.data[0]);
63 def writecmd(self, app, verb, count=0, data=[]):
64 """Write a command and some data to the GoodFET."""
65 self.serialport.write(chr(app));
66 self.serialport.write(chr(verb));
69 # count=len(data); #Initial count ignored.
71 #print "TX %02x %02x %04x" % (app,verb,count);
73 #little endian 16-bit length
74 self.serialport.write(chr(count&0xFF));
75 self.serialport.write(chr(count>>8));
77 #print "count=%02x, len(data)=%04x" % (count,len(data));
80 if(isinstance(data,list)):
81 for i in range(0,count):
82 #print "Converting %02x at %i" % (data[i],i)
86 self.serialport.write(outstr);
97 """Read a reply from the GoodFET."""
100 self.app=ord(self.serialport.read(1));
101 #print "APP=%2x" % self.app;
102 self.verb=ord(self.serialport.read(1));
103 #print "VERB=%02x" % self.verb;
105 ord(self.serialport.read(1))
106 +(ord(self.serialport.read(1))<<8)
109 #Debugging string; print, but wait.
110 if self.app==0xFF and self.verb==0xFF:
111 print "# DEBUG %s" % self.serialport.read(self.count);
113 self.data=self.serialport.read(self.count);
116 def glitchApp(self,app):
117 """Glitch into a device by its application."""
118 self.data=[app&0xff];
119 self.writecmd(self.GLITCHAPP,0x80,1,self.data);
120 #return ord(self.data[0]);
121 def glitchVerb(self,app,verb,data):
122 """Glitch during a transaction."""
123 if data==None: data=[];
124 self.data=[app&0xff, verb&0xFF]+data;
125 self.writecmd(self.GLITCHAPP,0x81,len(self.data),self.data);
126 #return ord(self.data[0]);
127 def glitchstart(self):
128 """Glitch into the AVR application."""
129 self.glitchVerb(self.APP,0x20,None);
130 def glitchstarttime(self):
131 """Measure the timer of the START verb."""
132 return self.glitchTime(self.APP,0x20,None);
133 def glitchTime(self,app,verb,data):
134 """Time the execution of a verb."""
135 if data==None: data=[];
136 self.data=[app&0xff, verb&0xFF]+data;
137 self.writecmd(self.GLITCHAPP,0x82,len(self.data),self.data);
138 return ord(self.data[0])+(ord(self.data[1])<<8);
139 def glitchVoltages(self,low=0x0880, high=0x0fff):
140 """Set glitching voltages. (0x0fff is max.)"""
141 self.data=[low&0xff, (low>>8)&0xff,
142 high&0xff, (high>>8)&0xff];
143 self.writecmd(self.GLITCHAPP,0x90,4,self.data);
144 #return ord(self.data[0]);
145 def glitchRate(self,count=0x0800):
146 """Set glitching count period."""
147 self.data=[count&0xff, (count>>8)&0xff];
148 self.writecmd(self.GLITCHAPP,0x91,2,
150 #return ord(self.data[0]);
154 def silent(self,s=0):
155 """Transmissions halted when 1."""
157 print "besilent is %i" % self.besilent;
158 self.writecmd(0,0xB0,1,[s]);
161 """Write a byte to P5OUT."""
162 self.writecmd(0,0xA1,1,[byte]);
164 """Write a byte to P5DIR."""
165 self.writecmd(0,0xA0,1,[byte]);
167 """Call to an address."""
168 self.writecmd(0,0x30,2,
169 [adr&0xFF,(adr>>8)&0xFF]);
170 def execute(self,code):
171 """Execute supplied code."""
172 self.writecmd(0,0x31,2,#len(code),
174 def peekbyte(self,address):
175 """Read a byte of memory from the monitor."""
176 self.data=[address&0xff,address>>8];
177 self.writecmd(0,0x02,2,self.data);
179 return ord(self.data[0]);
180 def peekword(self,address):
181 """Read a word of memory from the monitor."""
182 return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
183 def pokebyte(self,address,value):
184 """Set a byte of memory by the monitor."""
185 self.data=[address&0xff,address>>8,value];
186 self.writecmd(0,0x03,3,self.data);
187 return ord(self.data[0]);
188 def dumpmem(self,begin,end):
191 print "%04x %04x" % (i, self.peekword(i));
193 def monitor_ram_pattern(self):
194 """Overwrite all of RAM with 0xBEEF."""
195 self.writecmd(0,0x90,0,self.data);
197 def monitor_ram_depth(self):
198 """Determine how many bytes of RAM are unused by looking for 0xBEEF.."""
199 self.writecmd(0,0x91,0,self.data);
200 return ord(self.data[0])+(ord(self.data[1])<<8);
209 def setBaud(self,baud):
210 """Change the baud rate. TODO fix this."""
211 rates=self.baudrates;
213 print "Changing FET baud."
214 self.serialport.write(chr(0x00));
215 self.serialport.write(chr(0x80));
216 self.serialport.write(chr(1));
217 self.serialport.write(chr(baud));
219 print "Changed host baud."
220 self.serialport.setBaudrate(rates[baud]);
222 self.serialport.flushInput()
223 self.serialport.flushOutput()
225 print "Baud is now %i." % rates[baud];
228 return ord(self.serialport.read(1));
230 for r in self.baudrates:
231 print "\nTrying %i" % r;
232 self.serialport.setBaudrate(r);
234 self.serialport.flushInput()
235 self.serialport.flushOutput()
237 for i in range(1,10):
240 print "Read %02x %02x %02x %02x" % (
241 self.readbyte(),self.readbyte(),self.readbyte(),self.readbyte());
242 def monitortest(self):
243 """Self-test several functions through the monitor."""
244 print "Performing monitor self-test.";
246 if self.peekword(0x0c00)!=0x0c04 and self.peekword(0x0c00)!=0x0c06:
247 print "ERROR Fetched wrong value from 0x0c04.";
248 self.pokebyte(0x0021,0); #Drop LED
249 if self.peekbyte(0x0021)!=0:
250 print "ERROR, P1OUT not cleared.";
251 self.pokebyte(0x0021,1); #Light LED
253 print "Self-test complete.";
258 """Move the FET into the I2C application."""
259 self.writecmd(0x02,0x10,0,self.data); #SPI/SETUP
261 """Start an I2C transaction."""
262 self.writecmd(0x02,0x20,0,self.data); #SPI/SETUP
264 """Stop an I2C transaction."""
265 self.writecmd(0x02,0x21,0,self.data); #SPI/SETUP
266 def I2Cread(self,len=1):
267 """Read len bytes by I2C."""
268 self.writecmd(0x02,0x00,1,[len]); #SPI/SETUP
270 def I2Cwrite(self,bytes):
271 """Write bytes by I2C."""
272 self.writecmd(0x02,0x01,len(bytes),bytes); #SPI/SETUP
273 return ord(self.data[0]);