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 self.serialport.write(chr(d));
79 """Read a reply from the GoodFET."""
82 self.app=ord(self.serialport.read(1));
83 #print "APP=%2x" % self.app;
84 self.verb=ord(self.serialport.read(1));
85 #print "VERB=%02x" % self.verb;
87 ord(self.serialport.read(1))
88 +(ord(self.serialport.read(1))<<8)
91 #Debugging string; print, but wait.
92 if self.app==0xFF and self.verb==0xFF:
93 print "DEBUG %s" % self.serialport.read(self.count);
95 self.data=self.serialport.read(self.count);
100 """Transmissions halted when 1."""
102 print "besilent is %i" % self.besilent;
103 self.writecmd(0,0xB0,1,[s]);
106 """Write a byte to P5OUT."""
107 self.writecmd(0,0xA1,1,[byte]);
109 """Write a byte to P5DIR."""
110 self.writecmd(0,0xA0,1,[byte]);
111 def peekbyte(self,address):
112 """Read a byte of memory from the monitor."""
113 self.data=[address&0xff,address>>8];
114 self.writecmd(0,0x02,2,self.data);
116 return ord(self.data[0]);
117 def peekword(self,address):
118 """Read a word of memory from the monitor."""
119 return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
120 def pokebyte(self,address,value):
121 """Set a byte of memory by the monitor."""
122 self.data=[address&0xff,address>>8,value];
123 self.writecmd(0,0x03,3,self.data);
124 return ord(self.data[0]);
125 def dumpmem(self,begin,end):
128 print "%04x %04x" % (i, self.peekword(i));
130 def monitor_ram_pattern(self):
131 """Overwrite all of RAM with 0xBEEF."""
132 self.writecmd(0,0x90,0,self.data);
134 def monitor_ram_depth(self):
135 """Determine how many bytes of RAM are unused by looking for 0xBEEF.."""
136 self.writecmd(0,0x91,0,self.data);
137 return ord(self.data[0])+(ord(self.data[1])<<8);
146 def setBaud(self,baud):
147 """Change the baud rate. TODO fix this."""
148 rates=self.baudrates;
150 print "Changing FET baud."
151 self.serialport.write(chr(0x00));
152 self.serialport.write(chr(0x80));
153 self.serialport.write(chr(1));
154 self.serialport.write(chr(baud));
156 print "Changed host baud."
157 self.serialport.setBaudrate(rates[baud]);
159 self.serialport.flushInput()
160 self.serialport.flushOutput()
162 print "Baud is now %i." % rates[baud];
165 return ord(self.serialport.read(1));
167 for r in self.baudrates:
168 print "\nTrying %i" % r;
169 self.serialport.setBaudrate(r);
171 self.serialport.flushInput()
172 self.serialport.flushOutput()
174 for i in range(1,10):
177 print "Read %02x %02x %02x %02x" % (
178 self.readbyte(),self.readbyte(),self.readbyte(),self.readbyte());
179 def monitortest(self):
180 """Self-test several functions through the monitor."""
181 print "Performing monitor self-test.";
183 if self.peekword(0x0c00)!=0x0c04 and self.peekword(0x0c00)!=0x0c06:
184 print "ERROR Fetched wrong value from 0x0c04.";
185 self.pokebyte(0x0021,0); #Drop LED
186 if self.peekbyte(0x0021)!=0:
187 print "ERROR, P1OUT not cleared.";
188 self.pokebyte(0x0021,1); #Light LED
190 print "Self-test complete.";
195 """Move the FET into the I2C application."""
196 self.writecmd(0x02,0x10,0,self.data); #SPI/SETUP
198 """Start an I2C transaction."""
199 self.writecmd(0x02,0x20,0,self.data); #SPI/SETUP
201 """Stop an I2C transaction."""
202 self.writecmd(0x02,0x21,0,self.data); #SPI/SETUP
203 def I2Cread(self,len=1):
204 """Read len bytes by I2C."""
205 self.writecmd(0x02,0x00,1,[len]); #SPI/SETUP
207 def I2Cwrite(self,bytes):
208 """Write bytes by I2C."""
209 self.writecmd(0x02,0x01,len(bytes),bytes); #SPI/SETUP
210 return ord(self.data[0]);
211 class GoodFETCC(GoodFET):
212 """A GoodFET variant for use with Chipcon 8051 Zigbe SoC."""
215 self.writecmd(0x30,0x86,0,self.data);
216 def CCreleasecpu(self):
217 """Resume the CPU."""
218 self.writecmd(0x30,0x87,0,self.data);
222 #print "Status: %s" % self.CCstatusstr();
224 #Grab ident three times, should be equal.
225 ident1=self.CCident();
226 ident2=self.CCident();
227 ident3=self.CCident();
228 if(ident1!=ident2 or ident2!=ident3):
229 print "Error, repeated ident attempts unequal."
230 print "%04x, %04x, %04x" % (ident1, ident2, ident3);
232 #Single step, printing PC.
233 print "Tracing execution at startup."
234 for i in range(1,15):
236 byte=self.CCpeekcodebyte(i);
237 #print "PC=%04x, %02x" % (pc, byte);
240 print "Verifying that debugging a NOP doesn't affect the PC."
241 for i in range(1,15):
243 self.CCdebuginstr([0x00]);
244 if(pc!=self.CCgetPC()):
245 print "ERROR: PC changed during CCdebuginstr([NOP])!";
248 #print "Status: %s." % self.CCstatusstr();
254 """Move the FET into the CC2430/CC2530 application."""
255 #print "Initializing Chipcon.";
256 self.writecmd(0x30,0x10,0,self.data);
257 def CCrd_config(self):
258 """Read the config register of a Chipcon."""
259 self.writecmd(0x30,0x82,0,self.data);
260 return ord(self.data[0]);
261 def CCwr_config(self,config):
262 """Write the config register of a Chipcon."""
263 self.writecmd(0x30,0x81,1,[config&0xFF]);
265 CCversions={0x0100:"CC1110",
271 def CCidentstr(self):
272 ident=self.CCident();
273 chip=self.CCversions.get(ident&0xFF00);
274 return "%s/r%02x" % (chip, ident&0xFF);
276 """Get a chipcon's ID."""
277 self.writecmd(0x30,0x8B,0,None);
278 chip=ord(self.data[0]);
279 rev=ord(self.data[1]);
280 return (chip<<8)+rev;
282 """Get a chipcon's PC."""
283 self.writecmd(0x30,0x83,0,None);
284 hi=ord(self.data[0]);
285 lo=ord(self.data[1]);
287 def CCdebuginstr(self,instr):
288 self.writecmd(0x30,0x88,len(instr),instr);
289 return ord(self.data[0]);
290 def CCpeekcodebyte(self,adr):
291 """Read the contents of code memory at an address."""
292 self.data=[adr&0xff, (adr&0xff00)>>8];
293 self.writecmd(0x30,0x90,2,self.data);
294 return ord(self.data[0]);
295 def CCpeekdatabyte(self,adr):
296 """Read the contents of data memory at an address."""
297 self.data=[adr&0xff, (adr&0xff00)>>8];
298 self.writecmd(0x30,0x91, 2, self.data);
299 return ord(self.data[0]);
300 def CCpokedatabyte(self,adr,val):
301 """Write a byte to data memory."""
302 self.data=[adr&0xff, (adr&0xff00)>>8, val];
303 self.writecmd(0x30, 0x92, 3, self.data);
304 return ord(self.data[0]);
305 def CCchiperase(self):
306 """Erase all of the target's memory."""
307 self.writecmd(0x30,0x80,0,None);
309 """Check the status."""
310 self.writecmd(0x30,0x84,0,None);
311 return ord(self.data[0])
312 CCstatusbits={0x80 : "erased",
320 def CCstatusstr(self):
321 """Check the status as a string."""
322 status=self.CCstatus();
327 str="%s %s" %(self.CCstatusbits[i],str);
331 """Start debugging."""
332 self.writecmd(0x30,0x20,0,self.data);
333 ident=self.CCidentstr();
334 print "Target identifies as %s." % ident;
335 #print "Status: %s." % self.CCstatusstr();
338 #print "Status: %s." % self.CCstatusstr();
341 """Stop debugging."""
342 self.writecmd(0x30,0x21,0,self.data);
343 def CCstep_instr(self):
344 """Step one instruction."""
345 self.writecmd(0x30,0x89,0,self.data);