2 # GoodFET Client Library
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
6 # This code is ugly as sin, for bootstrapping the firmware only.
7 # Rewrite cleanly as soon as is convenient.
9 import sys, time, string, cStringIO, struct, glob, serial
13 def __init__(self, *args, **kargs):
17 def serInit(self, port=None):
18 """Open the serial port"""
21 glob_list = glob.glob("/dev/tty.usbserial*");
22 if len(glob_list) > 0:
25 glob_list = glob.glob("/dev/ttyUSB*");
26 if len(glob_list) > 0:
29 self.serialport = serial.Serial(
33 parity = serial.PARITY_NONE
35 #Drop DTR, which is !RST, low to begin the app.
36 self.serialport.setDTR(0);
37 self.serialport.flushInput()
38 self.serialport.flushOutput()
40 #Read and handle the initial command.
42 self.readcmd(); #Read the first command.
44 print "Verb is wrong. Incorrect firmware?";
46 def writecmd(self, app, verb, count, data):
47 """Write a command and some data to the GoodFET."""
48 self.serialport.write(chr(app));
49 self.serialport.write(chr(verb));
50 self.serialport.write(chr(count));
51 #print "count=%02x, len(data)=%04x" % (count,len(data));
54 self.serialport.write(chr(d));
55 self.readcmd(); #Uncomment this later, to ensure a response.
57 """Read a reply from the GoodFET."""
58 self.app=ord(self.serialport.read(1));
59 self.verb=ord(self.serialport.read(1));
60 self.count=ord(self.serialport.read(1));
62 self.data=self.serialport.read(self.count);
63 #print "READ %02x %02x %02x " % (self.app, self.verb, self.count);
66 def peekbyte(self,address):
67 """Read a byte of memory from the monitor."""
68 self.data=[address&0xff,address>>8];
69 self.writecmd(0,0x02,2,self.data);
71 return ord(self.data[0]);
72 def peekword(self,address):
73 """Read a word of memory from the monitor."""
74 return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
75 def pokebyte(self,address,value):
76 """Set a byte of memory by the monitor."""
77 self.data=[address&0xff,address>>8,value];
78 self.writecmd(0,0x03,3,self.data);
79 return ord(self.data[0]);
80 def dumpmem(self,begin,end):
83 print "%04x %04x" % (i, self.peekword(i));
85 def monitor_ram_pattern(self):
86 """Overwrite all of RAM with 0xBEEF."""
87 self.writecmd(0,0x90,0,self.data);
89 def monitor_ram_depth(self):
90 """Determine how many bytes of RAM are unused by looking for 0xBEEF.."""
91 self.writecmd(0,0x91,0,self.data);
92 return ord(self.data[0])+(ord(self.data[1])<<8);
93 def setBaud(self,baud):
94 rates=[9600, 9600, 19200, 38400];
96 print "Changing FET baud."
97 self.serialport.write(chr(0x00));
98 self.serialport.write(chr(0x80));
99 self.serialport.write(chr(1));
100 self.serialport.write(chr(baud));
102 print "Changed host baud."
103 self.serialport.setBaudrate(rates[baud]);
105 self.serialport.flushInput()
106 self.serialport.flushOutput()
108 print "Baud is now %i." % rates[baud];
110 def monitortest(self):
111 """Self-test several functions through the monitor."""
112 print "Performing monitor self-test.";
114 if self.peekword(0x0c00)!=0x0c04:
115 print "ERROR Fetched wrong value from 0x0c04.";
116 self.pokebyte(0x0021,0); #Drop LED
117 if self.peekbyte(0x0021)!=0:
118 print "ERROR, P1OUT not cleared.";
119 self.pokebyte(0x0021,1); #Light LED
121 print "Self-test complete.";
124 """Moved the FET into the SPI application."""
125 self.writecmd(0x01,0x10,0,self.data); #SPI/SETUP
127 def SPItrans8(self,byte):
128 """Read and write 8 bits by SPI."""
129 data=self.SPItrans([byte]);
132 def SPItrans(self,data):
133 """Exchange data by SPI."""
135 self.writecmd(0x01,0x00,len(data),data);
138 JEDECmanufacturers={0xFF: "MISSING",
144 JEDECdevices={0xFFFFFF: "MISSING",
149 0xC22014: "MX25L8005",
150 0xC22013: "MX25L4005",
154 """Grab an SPI Flash ROM's JEDEC bytes."""
155 data=[0x9f, 0, 0, 0];
156 data=self.SPItrans(data);
157 #print "Manufacturer: %02x\nType: %02x\nCapacity: %02x" % (ord(data[1]),ord(data[2]),ord(data[3]));
158 self.JEDECmanufacturer=ord(data[1]);
159 self.JEDECtype=ord(data[2]);
160 self.JEDECcapacity=ord(data[3]);
161 self.JEDECdevice=(ord(data[1])<<16)+(ord(data[2])<<8)+ord(data[3]);
163 def SPIpeek(self,adr):
164 """Grab a byte from an SPI Flash ROM."""
171 return ord(self.data[4]);
172 def SPIpeekblock(self,adr):
173 """Grab a byte from an SPI Flash ROM."""
174 data=[(adr&0xFF0000)>>16,
178 self.writecmd(0x01,0x02,3,data);
181 def SPIpokebyte(self,adr,val):
182 self.SPIpokebytes(adr,[val]);
183 def SPIpokebytes(self,adr,data):
184 #self.SPIwriteenable();
185 adranddata=[(adr&0xFF0000)>>16,
189 self.writecmd(0x01,0x03,
190 len(adranddata),adranddata);
192 def SPIchiperase(self):
193 """Mass erase an SPI Flash ROM."""
194 self.writecmd(0x01,0x81,0,[]);
195 def SPIwriteenable(self):
196 """SPI Flash Write Enable"""
200 def SPIjedecmanstr(self):
201 """Grab the JEDEC manufacturer string. Call after SPIjedec()."""
202 man=self.JEDECmanufacturers.get(self.JEDECmanufacturer)
207 def SPIjedecstr(self):
208 """Grab the JEDEC manufacturer string. Call after SPIjedec()."""
209 man=self.JEDECmanufacturers.get(self.JEDECmanufacturer);
212 device=self.JEDECdevices.get(self.JEDECdevice);
215 return "%s %s" % (man,device);
216 def MSP430setup(self):
217 """Move the FET into the MSP430 JTAG application."""
218 print "Initializing MSP430.";
219 self.writecmd(0x11,0x10,0,self.data);
224 """Move the FET into the CC2430/CC2530 application."""
225 print "Initializing Chipcon.";
226 self.writecmd(0x30,0x10,0,self.data);
227 def CCrd_config(self):
228 """Read the config register of a Chipcon."""
229 self.writecmd(0x30,0x82,0,self.data);
230 return ord(self.data[0]);
231 def CCwr_config(self,config):
232 """Write the config register of a Chipcon."""
233 self.writecmd(0x30,0x81,1,[config&0xFF]);
235 CCversions={0x0100:"CC1110",
241 def CCidentstr(self):
242 ident=self.CCident();
243 chip=self.CCversions.get(ident&0xFF00);
244 return "%s/r%02x" % (chip, ident&0xFF);
246 """Get a chipcon's ID."""
247 self.writecmd(0x30,0x8B,0,None);
248 chip=ord(self.data[0]);
249 rev=ord(self.data[1]);
250 return (chip<<8)+rev;
252 """Get a chipcon's PC."""
253 self.writecmd(0x30,0x83,0,None);
254 hi=ord(self.data[0]);
255 lo=ord(self.data[1]);
257 def CCdebuginstr(self,instr):
258 self.writecmd(0x30,0x88,len(instr),instr);
259 return ord(self.data[0]);
260 def MSP430peek(self,adr):
261 """Read the contents of memory at an address."""
262 self.data=[adr&0xff, (adr&0xff00)>>8];
263 self.writecmd(0x11,0x02,2,self.data);
264 return ord(self.data[0])+(ord(self.data[1])<<8);
265 def CCpeekcodebyte(self,adr):
266 """Read the contents of code memory at an address."""
267 self.data=[adr&0xff, (adr&0xff00)>>8];
268 self.writecmd(0x30,0x90,2,self.data);
269 return ord(self.data[0]);
270 def CCpeekdatabyte(self,adr):
271 """Read the contents of data memory at an address."""
272 self.data=[adr&0xff, (adr&0xff00)>>8];
273 self.writecmd(0x30,0x91, 2, self.data);
274 return ord(self.data[0]);
275 def CCpokedatabyte(self,adr,val):
276 """Write a byte to data memory."""
277 self.data=[adr&0xff, (adr&0xff00)>>8, val];
278 self.writecmd(0x30, 0x92, 3, self.data);
279 return ord(self.data[0]);
280 def CCchiperase(self):
281 """Erase all of the target's memory."""
282 self.writecmd(0x30,0x80,0,None);
284 """Check the status."""
285 self.writecmd(0x30,0x84,0,None);
286 return ord(self.data[0])
287 CCstatusbits={0x80 : "erased",
295 def CCstatusstr(self):
296 """Check the status as a string."""
297 status=self.CCstatus();
302 str="%s %s" %(self.CCstatusbits[i],str);
305 def MSP430poke(self,adr,val):
306 """Read the contents of memory at an address."""
307 self.data=[adr&0xff, (adr&0xff00)>>8, val&0xff, (val&0xff00)>>8];
308 self.writecmd(0x11,0x03,4,self.data);
309 return;# ord(self.data[0])+(ord(self.data[1])<<8);
310 def MSP430start(self):
311 """Start debugging."""
312 self.writecmd(0x11,0x20,0,self.data);
313 ident=self.MSP430ident();
314 print "Target identifies as %04x." % ident;
317 """Start debugging."""
318 self.writecmd(0x30,0x20,0,self.data);
319 ident=self.CCidentstr();
320 print "Target identifies as %s." % ident;
321 #print "Status: %s." % self.CCstatusstr();
324 print "Status: %s." % self.CCstatusstr();
327 """Stop debugging."""
328 self.writecmd(0x30,0x21,0,self.data);
329 def CCstep_instr(self):
330 """Step one instruction."""
331 self.writecmd(0x30,0x89,0,self.data);
332 def MSP430stop(self):
333 """Stop debugging."""
334 self.writecmd(0x11,0x21,0,self.data);
335 def MSP430haltcpu(self):
337 self.writecmd(0x11,0xA0,0,self.data);
338 def MSP430releasecpu(self):
339 """Resume the CPU."""
340 self.writecmd(0x11,0xA1,0,self.data);
343 self.writecmd(0x30,0x86,0,self.data);
344 def CCreleasecpu(self):
345 """Resume the CPU."""
346 self.writecmd(0x30,0x87,0,self.data);
347 def MSP430shiftir8(self,ins):
348 """Shift the 8-bit Instruction Register."""
350 self.writecmd(0x11,0x80,1,data);
351 return ord(self.data[0]);
352 def MSP430shiftdr16(self,dat):
353 """Shift the 16-bit Data Register."""
354 data=[dat&0xFF,(dat&0xFF00)>>8];
355 self.writecmd(0x11,0x81,2,data);
356 return ord(self.data[0])#+(ord(self.data[1])<<8);
357 def MSP430setinstrfetch(self):
358 """Set the instruction fetch mode."""
359 self.writecmd(0x11,0xC1,0,self.data);
361 def MSP430ident(self):
362 """Grab self-identification word from 0x0FF0 as big endian."""
363 i=self.MSP430peek(0x0ff0);
364 return ((i&0xFF00)>>8)+((i&0xFF)<<8)
365 def MSP430test(self):
366 """Test MSP430 JTAG. Requires that a chip be attached."""
367 if self.MSP430ident()==0xffff:
368 print "Is anything connected?";
369 print "Testing RAM.";
370 temp=self.MSP430peek(0x0200);
371 self.MSP430poke(0x0200,0xdead);
372 if(self.MSP430peek(0x0200)!=0xdead):
373 print "Poke of 0x0200 did not set to 0xDEAD properly.";
375 self.MSP430poke(0x0200,temp); #restore old value.
376 def MSP430flashtest(self):
377 self.MSP430masserase();
380 if(self.MSP430peek(i)!=0xFFFF):
381 print "ERROR: Unerased flash at %04x."%i;
382 self.MSP430writeflash(i,0xDEAD);
384 def MSP430masserase(self):
385 """Erase MSP430 flash memory."""
386 self.writecmd(0x11,0xE3,0,None);
387 def MSP430writeflash(self,adr,val):
388 """Write a word of flash memory."""
389 if(self.MSP430peek(adr)!=0xFFFF):
390 print "FLASH ERROR: %04x not clear." % adr;
391 data=[adr&0xFF,(adr&0xFF00)>>8,val&0xFF,(val&0xFF00)>>8];
392 self.writecmd(0x11,0xE1,4,data);
393 rval=ord(self.data[0])+(ord(self.data[1])<<8);
395 print "FLASH WRITE ERROR AT %04x. Found %04x, wrote %04x." % (adr,rval,val);
397 def MSP430dumpbsl(self):
398 self.MSP430dumpmem(0xC00,0xfff);
399 def MSP430dumpallmem(self):
400 self.MSP430dumpmem(0x200,0xffff);
401 def MSP430dumpmem(self,begin,end):
404 print "%04x %04x" % (i, self.MSP430peek(i));
409 print "Status: %s" % self.CCstatusstr();
411 #Grab ident three times, should be equal.
412 ident1=self.CCident();
413 ident2=self.CCident();
414 ident3=self.CCident();
415 if(ident1!=ident2 or ident2!=ident3):
416 print "Error, repeated ident attempts unequal."
417 print "%04x, %04x, %04x" % (ident1, ident2, ident3);
419 #Single step, printing PC.
420 #print "Tracing execution at startup."
421 for i in range(1,15):
423 byte=self.CCpeekcodebyte(i);
424 print "PC=%04x, %02x" % (pc, byte);
427 #print "Verifying that debugging a NOP doesn't affect the PC."
428 for i in range(1,15):
430 self.CCdebuginstr([0x00]);
431 if(pc!=self.CCgetPC()):
432 print "ERROR: PC changed during CCdebuginstr([NOP])!";
433 for i in range(0xE500,0xE600):
434 byte=self.CCpeekdatabyte(i);
435 print "data %04x: %02x" % (i,byte);
436 self.CCpokedatabyte(i,i&0xFF);
437 byte=self.CCpeekdatabyte(i);
438 print "data %04x: %02x" % (i,byte);
439 print "Status: %s." % self.CCstatusstr();