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
10 #sys.path.append("/usr/lib/tinyos")
15 def __init__(self, *args, **kargs):
19 def serInit(self, port):
20 """Open the serial port"""
21 self.serialport = serial.Serial(
25 parity = serial.PARITY_NONE
27 #Drop DTR, which is !RST, low to begin the app.
28 self.serialport.setDTR(0);
29 self.serialport.flushInput()
30 self.serialport.flushOutput()
32 #Read and handle the initial command.
34 self.readcmd(); #Read the first command.
36 print "Verb is wrong. Incorrect firmware?";
38 def writecmd(self, app, verb, count, data):
39 """Write a command and some data to the GoodFET."""
40 self.serialport.write(chr(app));
41 self.serialport.write(chr(verb));
42 self.serialport.write(chr(count));
43 #print "count=%02x, len(data)=%04x" % (count,len(data));
46 self.serialport.write(chr(d));
47 self.readcmd(); #Uncomment this later, to ensure a response.
49 """Read a reply from the GoodFET."""
50 self.app=ord(self.serialport.read(1));
51 self.verb=ord(self.serialport.read(1));
52 self.count=ord(self.serialport.read(1));
54 self.data=self.serialport.read(self.count);
55 #print "READ %02x %02x %02x " % (self.app, self.verb, self.count);
58 def peekbyte(self,address):
59 """Read a byte of memory from the monitor."""
60 self.data=[address&0xff,address>>8];
61 self.writecmd(0,0x02,2,self.data);
63 return ord(self.data[0]);
64 def peekword(self,address):
65 """Read a word of memory from the monitor."""
66 return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
67 def pokebyte(self,address,value):
68 """Set a byte of memory by the monitor."""
69 self.data=[address&0xff,address>>8,value];
70 self.writecmd(0,0x03,3,self.data);
71 return ord(self.data[0]);
72 def setBaud(self,baud):
73 rates=[9600, 9600, 19200, 38400];
75 print "Changing FET baud."
76 self.serialport.write(chr(0x00));
77 self.serialport.write(chr(0x80));
78 self.serialport.write(chr(1));
79 self.serialport.write(chr(baud));
81 print "Changed host baud."
82 self.serialport.setBaudrate(rates[baud]);
84 self.serialport.flushInput()
85 self.serialport.flushOutput()
87 print "Baud is now %i." % rates[baud];
89 def monitortest(self):
90 """Self-test several functions through the monitor."""
91 print "Performing monitor self-test.";
93 if self.peekword(0x0c00)!=0x0c04:
94 print "ERROR Fetched wrong value from 0x0c04.";
95 self.pokebyte(0x0021,0); #Drop LED
96 if self.peekbyte(0x0021)!=0:
97 print "ERROR, P1OUT not cleared.";
98 self.pokebyte(0x0021,1); #Light LED
100 print "Self-test complete.";
103 """Moved the FET into the SPI application."""
104 self.writecmd(0x01,0x10,0,self.data); #SPI/SETUP
106 def SPItrans8(self,byte):
107 """Read and write 8 bits by SPI."""
108 data=self.SPItrans([byte]);
111 def SPItrans(self,data):
112 """Exchange data by SPI."""
114 self.writecmd(0x01,0x00,len(data),data);
117 JEDECmanufacturers={0xFF: "MISSING",
121 JEDECdevices={0xFFFFFF: "MISSING",
126 0xC22014: "MX25L8005",
127 0xC22013: "MX25L4005"
130 """Grab an SPI Flash ROM's JEDEC bytes."""
131 data=[0x9f, 0, 0, 0];
132 data=self.SPItrans(data);
133 #print "Manufacturer: %02x\nType: %02x\nCapacity: %02x" % (ord(data[1]),ord(data[2]),ord(data[3]));
134 self.JEDECmanufacturer=ord(data[1]);
135 self.JEDECtype=ord(data[2]);
136 self.JEDECcapacity=ord(data[3]);
137 self.JEDECdevice=(ord(data[1])<<16)+(ord(data[2])<<8)+ord(data[3]);
139 def SPIpeek(self,adr):
140 """Grab a byte from an SPI Flash ROM."""
147 return ord(self.data[4]);
148 def SPIpeekblock(self,adr):
149 """Grab a byte from an SPI Flash ROM."""
150 data=[(adr&0xFF0000)>>16,
154 self.writecmd(0x01,0x02,3,data);
157 def SPIpokebyte(self,adr,val):
158 self.SPIpokebytes(adr,[val]);
159 def SPIpokebytes(self,adr,data):
160 #self.SPIwriteenable();
161 adranddata=[(adr&0xFF0000)>>16,
165 self.writecmd(0x01,0x03,
166 len(adranddata),adranddata);
168 def SPIchiperase(self):
169 """Mass erase an SPI Flash ROM."""
170 self.writecmd(0x01,0x81,0,[]);
171 def SPIwriteenable(self):
172 """SPI Flash Write Enable"""
176 def SPIjedecmanstr(self):
177 """Grab the JEDEC manufacturer string. Call after SPIjedec()."""
178 man=self.JEDECmanufacturers.get(self.JEDECmanufacturer)
183 def SPIjedecstr(self):
184 """Grab the JEDEC manufacturer string. Call after SPIjedec()."""
185 man=self.JEDECmanufacturers.get(self.JEDECmanufacturer);
188 device=self.JEDECdevices.get(self.JEDECdevice);
191 return "%s %s" % (man,device);
192 def MSP430setup(self):
193 """Move the FET into the MSP430 JTAG application."""
194 print "Initializing MSP430.";
195 self.writecmd(0x11,0x10,0,self.data);
200 """Move the FET into the CC2430/CC2530 application."""
201 print "Initializing Chipcon.";
202 self.writecmd(0x30,0x10,0,self.data);
203 def CCrd_config(self):
204 """Read the config register of a Chipcon."""
205 self.writecmd(0x30,0x82,0,self.data);
206 return ord(self.data[0]);
207 def CCwr_config(self,config):
208 """Write the config register of a Chipcon."""
209 self.writecmd(0x30,0x81,1,[config&0xFF]);
211 CCversions={0x0100:"CC1110",
217 def CCidentstr(self):
218 ident=self.CCident();
219 chip=self.CCversions.get(ident&0xFF00);
220 return "%s/r%02x" % (chip, ident&0xFF);
222 """Get a chipcon's ID."""
223 self.writecmd(0x30,0x8B,0,None);
224 chip=ord(self.data[0]);
225 rev=ord(self.data[1]);
226 return (chip<<8)+rev;
228 """Get a chipcon's PC."""
229 self.writecmd(0x30,0x83,0,None);
230 hi=ord(self.data[0]);
231 lo=ord(self.data[1]);
233 def CCdebuginstr(self,instr):
234 self.writecmd(0x30,0x88,len(instr),instr);
235 return ord(self.data[0]);
236 def MSP430peek(self,adr):
237 """Read the contents of memory at an address."""
238 self.data=[adr&0xff, (adr&0xff00)>>8];
239 self.writecmd(0x11,0x02,2,self.data);
240 return ord(self.data[0])+(ord(self.data[1])<<8);
241 def CCpeekcodebyte(self,adr):
242 """Read the contents of code memory at an address."""
243 self.data=[adr&0xff, (adr&0xff00)>>8];
244 self.writecmd(0x30,0x90,2,self.data);
245 return ord(self.data[0]);
246 def CCpeekdatabyte(self,adr):
247 """Read the contents of data memory at an address."""
248 self.data=[adr&0xff, (adr&0xff00)>>8];
249 self.writecmd(0x30,0x91, 2, self.data);
250 return ord(self.data[0]);
251 def CCpokedatabyte(self,adr,val):
252 """Write a byte to data memory."""
253 self.data=[adr&0xff, (adr&0xff00)>>8, val];
254 self.writecmd(0x30, 0x92, 3, self.data);
255 return ord(self.data[0]);
256 def CCchiperase(self):
257 """Erase all of the target's memory."""
258 self.writecmd(0x30,0x80,0,None);
260 """Check the status."""
261 self.writecmd(0x30,0x84,0,None);
262 return ord(self.data[0])
263 CCstatusbits={0x80 : "erased",
271 def CCstatusstr(self):
272 """Check the status as a string."""
273 status=self.CCstatus();
278 str="%s %s" %(self.CCstatusbits[i],str);
281 def MSP430poke(self,adr,val):
282 """Read the contents of memory at an address."""
283 self.data=[adr&0xff, (adr&0xff00)>>8, val&0xff, (val&0xff00)>>8];
284 self.writecmd(0x11,0x03,4,self.data);
285 return;# ord(self.data[0])+(ord(self.data[1])<<8);
286 def MSP430start(self):
287 """Start debugging."""
288 self.writecmd(0x11,0x20,0,self.data);
289 ident=self.MSP430ident();
290 print "Target identifies as %04x." % ident;
293 """Start debugging."""
294 self.writecmd(0x30,0x20,0,self.data);
295 ident=self.CCidentstr();
296 print "Target identifies as %s." % ident;
297 #print "Status: %s." % self.CCstatusstr();
300 print "Status: %s." % self.CCstatusstr();
303 """Stop debugging."""
304 self.writecmd(0x30,0x21,0,self.data);
305 def CCstep_instr(self):
306 """Step one instruction."""
307 self.writecmd(0x30,0x89,0,self.data);
308 def MSP430stop(self):
309 """Stop debugging."""
310 self.writecmd(0x11,0x21,0,self.data);
311 def MSP430haltcpu(self):
313 self.writecmd(0x11,0xA0,0,self.data);
314 def MSP430releasecpu(self):
315 """Resume the CPU."""
316 self.writecmd(0x11,0xA1,0,self.data);
319 self.writecmd(0x30,0x86,0,self.data);
320 def CCreleasecpu(self):
321 """Resume the CPU."""
322 self.writecmd(0x30,0x87,0,self.data);
323 def MSP430shiftir8(self,ins):
324 """Shift the 8-bit Instruction Register."""
326 self.writecmd(0x11,0x80,1,data);
327 return ord(self.data[0]);
328 def MSP430shiftdr16(self,dat):
329 """Shift the 16-bit Data Register."""
330 data=[dat&0xFF,(dat&0xFF00)>>8];
331 self.writecmd(0x11,0x81,2,data);
332 return ord(self.data[0])#+(ord(self.data[1])<<8);
333 def MSP430setinstrfetch(self):
334 """Set the instruction fetch mode."""
335 self.writecmd(0x11,0xC1,0,self.data);
337 def MSP430ident(self):
338 """Grab self-identification word from 0x0FF0 as big endian."""
339 i=self.MSP430peek(0x0ff0);
340 return ((i&0xFF00)>>8)+((i&0xFF)<<8)
341 def MSP430test(self):
342 """Test MSP430 JTAG. Requires that a chip be attached."""
343 if self.MSP430ident()==0xffff:
344 print "Is anything connected?";
345 print "Testing RAM.";
346 temp=self.MSP430peek(0x0200);
347 self.MSP430poke(0x0200,0xdead);
348 if(self.MSP430peek(0x0200)!=0xdead):
349 print "Poke of 0x0200 did not set to 0xDEAD properly.";
351 self.MSP430poke(0x0200,temp); #restore old value.
352 def MSP430flashtest(self):
353 self.MSP430masserase();
356 if(self.MSP430peek(i)!=0xFFFF):
357 print "ERROR: Unerased flash at %04x."%i;
358 self.MSP430writeflash(i,0xDEAD);
360 def MSP430masserase(self):
361 """Erase MSP430 flash memory."""
362 self.writecmd(0x11,0xE3,0,None);
363 def MSP430writeflash(self,adr,val):
364 """Write a word of flash memory."""
365 if(self.MSP430peek(adr)!=0xFFFF):
366 print "FLASH ERROR: %04x not clear." % adr;
367 data=[adr&0xFF,(adr&0xFF00)>>8,val&0xFF,(val&0xFF00)>>8];
368 self.writecmd(0x11,0xE1,4,data);
369 rval=ord(self.data[0])+(ord(self.data[1])<<8);
371 print "FLASH WRITE ERROR AT %04x. Found %04x, wrote %04x." % (adr,rval,val);
373 def MSP430dumpbsl(self):
374 self.MSP430dumpmem(0xC00,0xfff);
375 def MSP430dumpallmem(self):
376 self.MSP430dumpmem(0x200,0xffff);
377 def MSP430dumpmem(self,begin,end):
380 print "%04x %04x" % (i, self.MSP430peek(i));
385 print "Status: %s" % self.CCstatusstr();
387 #Grab ident three times, should be equal.
388 ident1=self.CCident();
389 ident2=self.CCident();
390 ident3=self.CCident();
391 if(ident1!=ident2 or ident2!=ident3):
392 print "Error, repeated ident attempts unequal."
393 print "%04x, %04x, %04x" % (ident1, ident2, ident3);
395 #Single step, printing PC.
396 #print "Tracing execution at startup."
397 for i in range(1,15):
399 byte=self.CCpeekcodebyte(i);
400 print "PC=%04x, %02x" % (pc, byte);
403 #print "Verifying that debugging a NOP doesn't affect the PC."
404 for i in range(1,15):
406 self.CCdebuginstr([0x00]);
407 if(pc!=self.CCgetPC()):
408 print "ERROR: PC changed during CCdebuginstr([NOP])!";
409 for i in range(0xE500,0xE600):
410 byte=self.CCpeekdatabyte(i);
411 print "data %04x: %02x" % (i,byte);
412 self.CCpokedatabyte(i,i&0xFF);
413 byte=self.CCpeekdatabyte(i);
414 print "data %04x: %02x" % (i,byte);
415 print "Status: %s." % self.CCstatusstr();