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;
10 def getClient(name="GoodFET"):
11 import GoodFET, GoodFETCC, GoodFETAVR, GoodFETSPI, GoodFETMSP430;
12 if(name=="GoodFET" or name=="monitor"): return GoodFET.GoodFET();
13 elif name=="cc" or name=="chipcon": return GoodFETCC.GoodFETCC();
14 elif name=="avr": return GoodFETAVR.GoodFETAVR();
15 elif name=="spi": return GoodFETSPI.GoodFETSPI();
16 elif name=="msp430": return GoodFETSPI.GoodFETMSP430();
18 print "Unsupported target: %s" % name;
23 """GoodFET Client Library"""
27 def __init__(self, *args, **kargs):
32 from GoodFETConsole import GoodFETConsole;
33 return GoodFETConsole(self);
36 def serInit(self, port=None):
37 """Open the serial port"""
39 if port is None and os.environ.get("GOODFET")!=None:
40 glob_list = glob.glob(os.environ.get("GOODFET"));
41 if len(glob_list) > 0:
44 glob_list = glob.glob("/dev/tty.usbserial*");
45 if len(glob_list) > 0:
48 glob_list = glob.glob("/dev/ttyUSB*");
49 if len(glob_list) > 0:
52 self.serialport = serial.Serial(
56 parity = serial.PARITY_NONE
59 #This might cause problems, but it makes failure graceful.
60 #self.serialport._timeout = 5;
62 #Explicitly set RTS and DTR to halt board.
63 self.serialport.setRTS(1);
64 self.serialport.setDTR(1);
65 #Drop DTR, which is !RST, low to begin the app.
66 self.serialport.setDTR(0);
67 self.serialport.flushInput()
68 self.serialport.flushOutput()
70 #Read and handle the initial command.
72 self.readcmd(); #Read the first command.
74 print "Verb %02x is wrong. Incorrect firmware?" % self.verb;
76 def getbuffer(self,size=0x1c00):
77 writecmd(0,0xC2,[size&0xFF,(size>>16)&0xFF]);
78 print "Got %02x%02x buffer size." % (self.data[1],self.data[0]);
79 def writecmd(self, app, verb, count=0, data=[]):
80 """Write a command and some data to the GoodFET."""
81 self.serialport.write(chr(app));
82 self.serialport.write(chr(verb));
85 # count=len(data); #Initial count ignored.
87 #print "TX %02x %02x %04x" % (app,verb,count);
89 #little endian 16-bit length
90 self.serialport.write(chr(count&0xFF));
91 self.serialport.write(chr(count>>8));
93 #print "count=%02x, len(data)=%04x" % (count,len(data));
96 if(isinstance(data,list)):
97 for i in range(0,count):
98 #print "Converting %02x at %i" % (data[i],i)
101 outstr=''.join(data);
102 self.serialport.write(outstr);
103 if not self.besilent:
113 """Read a reply from the GoodFET."""
116 self.app=ord(self.serialport.read(1));
117 #print "APP=%2x" % self.app;
118 self.verb=ord(self.serialport.read(1));
119 #print "VERB=%02x" % self.verb;
121 ord(self.serialport.read(1))
122 +(ord(self.serialport.read(1))<<8)
125 #Debugging string; print, but wait.
126 if self.app==0xFF and self.verb==0xFF:
127 print "# DEBUG %s" % self.serialport.read(self.count);
130 self.data=self.serialport.read(self.count);
133 def glitchApp(self,app):
134 """Glitch into a device by its application."""
135 self.data=[app&0xff];
136 self.writecmd(self.GLITCHAPP,0x80,1,self.data);
137 #return ord(self.data[0]);
138 def glitchVerb(self,app,verb,data):
139 """Glitch during a transaction."""
140 if data==None: data=[];
141 self.data=[app&0xff, verb&0xFF]+data;
142 self.writecmd(self.GLITCHAPP,0x81,len(self.data),self.data);
143 #return ord(self.data[0]);
144 def glitchstart(self):
145 """Glitch into the AVR application."""
146 self.glitchVerb(self.APP,0x20,None);
147 def glitchstarttime(self):
148 """Measure the timer of the START verb."""
149 return self.glitchTime(self.APP,0x20,None);
150 def glitchTime(self,app,verb,data):
151 """Time the execution of a verb."""
152 if data==None: data=[];
153 self.data=[app&0xff, verb&0xFF]+data;
154 self.writecmd(self.GLITCHAPP,0x82,len(self.data),self.data);
155 return ord(self.data[0])+(ord(self.data[1])<<8);
156 def glitchVoltages(self,low=0x0880, high=0x0fff):
157 """Set glitching voltages. (0x0fff is max.)"""
158 self.data=[low&0xff, (low>>8)&0xff,
159 high&0xff, (high>>8)&0xff];
160 self.writecmd(self.GLITCHAPP,0x90,4,self.data);
161 #return ord(self.data[0]);
162 def glitchRate(self,count=0x0800):
163 """Set glitching count period."""
164 self.data=[count&0xff, (count>>8)&0xff];
165 self.writecmd(self.GLITCHAPP,0x91,2,
167 #return ord(self.data[0]);
171 def silent(self,s=0):
172 """Transmissions halted when 1."""
174 print "besilent is %i" % self.besilent;
175 self.writecmd(0,0xB0,1,[s]);
178 """Write a byte to P5OUT."""
179 self.writecmd(0,0xA1,1,[byte]);
181 """Write a byte to P5DIR."""
182 self.writecmd(0,0xA0,1,[byte]);
184 """Call to an address."""
185 self.writecmd(0,0x30,2,
186 [adr&0xFF,(adr>>8)&0xFF]);
187 def execute(self,code):
188 """Execute supplied code."""
189 self.writecmd(0,0x31,2,#len(code),
191 def peekbyte(self,address):
192 """Read a byte of memory from the monitor."""
193 self.data=[address&0xff,address>>8];
194 self.writecmd(0,0x02,2,self.data);
196 return ord(self.data[0]);
197 def peekword(self,address):
198 """Read a word of memory from the monitor."""
199 return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
200 def pokebyte(self,address,value):
201 """Set a byte of memory by the monitor."""
202 self.data=[address&0xff,address>>8,value];
203 self.writecmd(0,0x03,3,self.data);
204 return ord(self.data[0]);
205 def dumpmem(self,begin,end):
208 print "%04x %04x" % (i, self.peekword(i));
210 def monitor_ram_pattern(self):
211 """Overwrite all of RAM with 0xBEEF."""
212 self.writecmd(0,0x90,0,self.data);
214 def monitor_ram_depth(self):
215 """Determine how many bytes of RAM are unused by looking for 0xBEEF.."""
216 self.writecmd(0,0x91,0,self.data);
217 return ord(self.data[0])+(ord(self.data[1])<<8);
226 def setBaud(self,baud):
227 """Change the baud rate. TODO fix this."""
228 rates=self.baudrates;
230 print "Changing FET baud."
231 self.serialport.write(chr(0x00));
232 self.serialport.write(chr(0x80));
233 self.serialport.write(chr(1));
234 self.serialport.write(chr(baud));
236 print "Changed host baud."
237 self.serialport.setBaudrate(rates[baud]);
239 self.serialport.flushInput()
240 self.serialport.flushOutput()
242 print "Baud is now %i." % rates[baud];
245 return ord(self.serialport.read(1));
247 for r in self.baudrates:
248 print "\nTrying %i" % r;
249 self.serialport.setBaudrate(r);
251 self.serialport.flushInput()
252 self.serialport.flushOutput()
254 for i in range(1,10):
257 print "Read %02x %02x %02x %02x" % (
258 self.readbyte(),self.readbyte(),self.readbyte(),self.readbyte());
259 def monitortest(self):
260 """Self-test several functions through the monitor."""
261 print "Performing monitor self-test.";
263 if self.peekword(0x0c00)!=0x0c04 and self.peekword(0x0c00)!=0x0c06:
264 print "ERROR Fetched wrong value from 0x0c04.";
265 self.pokebyte(0x0021,0); #Drop LED
266 if self.peekbyte(0x0021)!=0:
267 print "ERROR, P1OUT not cleared.";
268 self.pokebyte(0x0021,1); #Light LED
270 print "Self-test complete.";
273 # The following functions ought to be implemented in
276 def infostring(self):
277 a=self.peekbyte(0xff0);
278 b=self.peekbyte(0xff1);
279 return "%02x%02x" % (a,b);
281 print "Locking Unsupported.";
283 print "Erasure Unsupported.";
292 def flash(self,file):
293 """Flash an intel hex file to code memory."""
294 print "Flash not implemented.";
295 def peek32(self,address, memory="vn"):
296 return (self.peek16(address,memory)+
297 (self.peek16(address+2,memory)<<16));
298 def peek16(self,address, memory="vn"):
299 return (self.peek8(address,memory)+
300 (self.peek8(address+1,memory)<<8));
301 def peek8(self,address, memory="vn"):