952ae98f10332a2e3e1f00f582c44c1ea011ab1d
[goodfet] / client / GoodFET.py
1 #!/usr/bin/env python
2 # GoodFET Client Library
3
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # This code is ugly as sin, for bootstrapping the firmware only.
7 # Rewrite cleanly as soon as is convenient.
8
9 import sys, time, string, cStringIO, struct
10 #sys.path.append("/usr/lib/tinyos")
11 import serial
12
13
14 class GoodFET:
15     def __init__(self, *args, **kargs):
16         self.data=[0];
17     def timeout(self):
18         print "timout\n";
19     def serInit(self, port):
20         """Open the serial port"""
21         self.serialport = serial.Serial(
22             port,
23             #9600,
24             115200,
25             parity = serial.PARITY_NONE
26             )
27         #Drop DTR, which is !RST, low to begin the app.
28         self.serialport.setDTR(0);
29         self.serialport.flushInput()
30         self.serialport.flushOutput()
31         
32         #Read and handle the initial command.
33         #time.sleep(1);
34         self.readcmd(); #Read the first command.
35         if(self.verb!=0x7F):
36             print "Verb is wrong.  Incorrect firmware?";
37         
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));
44         if count!=0:
45             for d in data:
46                 self.serialport.write(chr(d));
47         self.readcmd();  #Uncomment this later, to ensure a response.
48     def readcmd(self):
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));
53         if self.count>0:
54             self.data=self.serialport.read(self.count);
55         #print "READ %02x %02x %02x " % (self.app, self.verb, self.count);
56         
57     #Monitor stuff
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);
62         #self.readcmd();
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];
74         self.data=[baud];
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));
80         
81         print "Changed host baud."
82         self.serialport.setBaudrate(rates[baud]);
83         time.sleep(1);
84         self.serialport.flushInput()
85         self.serialport.flushOutput()
86         
87         print "Baud is now %i." % rates[baud];
88         return;
89     def monitortest(self):
90         """Self-test several functions through the monitor."""
91         print "Performing monitor self-test.";
92         
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
99         
100         print "Self-test complete.";
101     
102     def SPIsetup(self):
103         """Moved the FET into the SPI application."""
104         self.writecmd(0x01,0x10,0,self.data); #SPI/SETUP
105         #self.readcmd();
106     def SPItrans8(self,byte):
107         """Read and write 8 bits by SPI."""
108         data=self.SPItrans([byte]);
109         return ord(data[0]);
110     
111     def SPItrans(self,data):
112         """Exchange data by SPI."""
113         self.data=data;
114         self.writecmd(0x01,0x00,len(data),data);
115         return self.data;
116     
117     JEDECmanufacturers={0xFF: "MISSING",
118                         0xEF: "Winbond"};
119     JEDECdevices={0xEF3014: "W25X80L",
120                   0xEF3013: "W25X40L",
121                   0xEF3012: "W25X20L",
122                   0xEF3011: "W25X10L"};
123     def SPIjedec(self):
124         """Grab an SPI Flash ROM's JEDEC bytes."""
125         data=[0x9f, 0, 0, 0];
126         data=self.SPItrans(data);
127         #print "Manufacturer: %02x\nType: %02x\nCapacity: %02x" % (ord(data[1]),ord(data[2]),ord(data[3]));
128         self.JEDECmanufacturer=ord(data[1]);
129         self.JEDECtype=ord(data[2]);
130         self.JEDECcapacity=ord(data[3]);
131         self.JEDECdevice=(ord(data[1])<<16)+(ord(data[2])<<8)+ord(data[3]);
132         return data;
133     def SPIpeek(self,adr):
134         """Grab a byte from an SPI Flash ROM."""
135         data=[0x03,
136               (adr&0xFF0000)>>16,
137               (adr&0xFF00)>>8,
138               adr&0xFF,
139               0];
140         self.SPItrans(data);
141         return ord(self.data[4]);
142     def SPIpeekblock(self,adr):
143         """Grab a byte from an SPI Flash ROM."""
144         data=[(adr&0xFF0000)>>16,
145               (adr&0xFF00)>>8,
146               adr&0xFF];
147         
148         self.writecmd(0x01,0x02,3,data);
149         return self.data;
150     
151     def SPIjedecmanstr(self):
152         """Grab the JEDEC manufacturer string.  Call after SPIjedec()."""
153         man=self.JEDECmanufacturers[self.JEDECmanufacturer];
154         if man==0:
155             man="UNKNOWN";
156         return man;
157     
158     def SPIjedecstr(self):
159         """Grab the JEDEC manufacturer string.  Call after SPIjedec()."""
160         man=self.JEDECmanufacturers[self.JEDECmanufacturer];
161         if man==0:
162             man="UNKNOWN";
163         device=self.JEDECdevices[self.JEDECdevice];
164         if device==0:
165             device="???"
166         return "%s %s" % (man,device);
167     def MSP430setup(self):
168         """Move the FET into the MSP430 JTAG application."""
169         print "Initializing MSP430.";
170         self.writecmd(0x11,0x10,0,self.data);
171
172     
173     
174     def CCsetup(self):
175         """Move the FET into the CC2430/CC2530 application."""
176         print "Initializing Chipcon.";
177         self.writecmd(0x30,0x10,0,self.data);
178     def CCrd_config(self):
179         """Read the config register of a Chipcon."""
180         self.writecmd(0x30,0x82,0,self.data);
181         return ord(self.data[0]);
182     def CCwr_config(self,config):
183         """Write the config register of a Chipcon."""
184         self.writecmd(0x30,0x81,1,[config&0xFF]);
185     
186     CCversions={0x0100:"CC1110",
187                 0x8500:"CC2430",
188                 0x8900:"CC2431",
189                 0x8100:"CC2510",
190                 0x9100:"CC2511",
191                 0xFF00:"CCmissing"};
192     def CCidentstr(self):
193         ident=self.CCident();
194         chip=self.CCversions[ident&0xFF00];
195         return "%s/r%02x" % (chip, ident&0xFF); 
196     def CCident(self):
197         """Get a chipcon's ID."""
198         self.writecmd(0x30,0x8B,0,None);
199         chip=ord(self.data[0]);
200         rev=ord(self.data[1]);
201         return (chip<<8)+rev;
202     def CCgetPC(self):
203         """Get a chipcon's PC."""
204         self.writecmd(0x30,0x83,0,None);
205         hi=ord(self.data[0]);
206         lo=ord(self.data[1]);
207         return (hi<<8)+lo;
208     def CCdebuginstr(self,instr):
209         self.writecmd(0x30,0x88,len(instr),instr);
210         return ord(self.data[0]);
211     def MSP430peek(self,adr):
212         """Read the contents of memory at an address."""
213         self.data=[adr&0xff, (adr&0xff00)>>8];
214         self.writecmd(0x11,0x02,2,self.data);
215         return ord(self.data[0])+(ord(self.data[1])<<8);
216     def CCpeekcodebyte(self,adr):
217         """Read the contents of code memory at an address."""
218         self.data=[adr&0xff, (adr&0xff00)>>8];
219         self.writecmd(0x30,0x90,2,self.data);
220         return ord(self.data[0]);
221     def CCpeekdatabyte(self,adr):
222         """Read the contents of data memory at an address."""
223         self.data=[adr&0xff, (adr&0xff00)>>8];
224         self.writecmd(0x30,0x91, 2, self.data);
225         return ord(self.data[0]);
226     def CCpokedatabyte(self,adr,val):
227         """Write a byte to data memory."""
228         self.data=[adr&0xff, (adr&0xff00)>>8, val];
229         self.writecmd(0x30, 0x92, 3, self.data);
230         return ord(self.data[0]);
231     def CCchiperase(self):
232         """Erase all of the target's memory."""
233         self.writecmd(0x30,0x80,0,None);
234     def CCstatus(self):
235         """Check the status."""
236         self.writecmd(0x30,0x84,0,None);
237         return ord(self.data[0])
238     CCstatusbits={0x80 : "erased",
239                   0x40 : "pcon_idle",
240                   0x20 : "halted",
241                   0x10 : "pm0",
242                   0x08 : "halted",
243                   0x04 : "locked",
244                   0x02 : "oscstable",
245                   0x01 : "overflow"};
246     def CCstatusstr(self):
247         """Check the status as a string."""
248         status=self.CCstatus();
249         str="";
250         i=1;
251         while i<0x100:
252             if(status&i):
253                 str="%s %s" %(self.CCstatusbits[i],str);
254             i*=2;
255         return str;
256     def MSP430poke(self,adr,val):
257         """Read the contents of memory at an address."""
258         self.data=[adr&0xff, (adr&0xff00)>>8, val&0xff, (val&0xff00)>>8];
259         self.writecmd(0x11,0x03,4,self.data);
260         return;# ord(self.data[0])+(ord(self.data[1])<<8);
261     def MSP430start(self):
262         """Start debugging."""
263         self.writecmd(0x11,0x20,0,self.data);
264         ident=self.MSP430ident();
265         print "Target identifies as %04x." % ident;
266     
267     def CCstart(self):
268         """Start debugging."""
269         self.writecmd(0x30,0x20,0,self.data);
270         ident=self.CCidentstr();
271         print "Target identifies as %s." % ident;
272         print "Status: %s." % self.CCstatusstr();
273         self.CCreleasecpu();
274         self.CChaltcpu();
275         print "Status: %s." % self.CCstatusstr();
276     def CCstop(self):
277         """Stop debugging."""
278         self.writecmd(0x30,0x21,0,self.data);
279     def CCstep_instr(self):
280         """Step one instruction."""
281         self.writecmd(0x30,0x89,0,self.data);
282     def MSP430stop(self):
283         """Stop debugging."""
284         self.writecmd(0x11,0x21,0,self.data);
285     def MSP430haltcpu(self):
286         """Halt the CPU."""
287         self.writecmd(0x11,0xA0,0,self.data);
288     def MSP430releasecpu(self):
289         """Resume the CPU."""
290         self.writecmd(0x11,0xA1,0,self.data);
291     def CChaltcpu(self):
292         """Halt the CPU."""
293         self.writecmd(0x30,0x86,0,self.data);
294     def CCreleasecpu(self):
295         """Resume the CPU."""
296         self.writecmd(0x30,0x87,0,self.data);
297     def MSP430shiftir8(self,ins):
298         """Shift the 8-bit Instruction Register."""
299         data=[ins];
300         self.writecmd(0x11,0x80,1,data);
301         return ord(self.data[0]);
302     def MSP430shiftdr16(self,dat):
303         """Shift the 16-bit Data Register."""
304         data=[dat&0xFF,(dat&0xFF00)>>8];
305         self.writecmd(0x11,0x81,2,data);
306         return ord(self.data[0])#+(ord(self.data[1])<<8);
307     def MSP430setinstrfetch(self):
308         """Set the instruction fetch mode."""
309         self.writecmd(0x11,0xC1,0,self.data);
310         return self.data[0];
311     def MSP430ident(self):
312         """Grab self-identification word from 0x0FF0 as big endian."""
313         i=self.MSP430peek(0x0ff0);
314         return ((i&0xFF00)>>8)+((i&0xFF)<<8)
315     def MSP430test(self):
316         """Test MSP430 JTAG.  Requires that a chip be attached."""
317         if self.MSP430ident()==0xffff:
318             print "Is anything connected?";
319         print "Testing RAM.";
320         temp=self.MSP430peek(0x0200);
321         self.MSP430poke(0x0200,0xdead);
322         if(self.MSP430peek(0x0200)!=0xdead):
323             print "Poke of 0x0200 did not set to 0xDEAD properly.";
324             return;
325         self.MSP430poke(0x0200,temp); #restore old value.
326     def MSP430flashtest(self):
327         self.MSP430masserase();
328         i=0x2500;
329         while(i<0xFFFF):
330             if(self.MSP430peek(i)!=0xFFFF):
331                 print "ERROR: Unerased flash at %04x."%i;
332             self.MSP430writeflash(i,0xDEAD);
333             i+=2;
334     def MSP430masserase(self):
335         """Erase MSP430 flash memory."""
336         self.writecmd(0x11,0xE3,0,None);
337     def MSP430writeflash(self,adr,val):
338         """Write a word of flash memory."""
339         if(self.MSP430peek(adr)!=0xFFFF):
340             print "FLASH ERROR: %04x not clear." % adr;
341         data=[adr&0xFF,(adr&0xFF00)>>8,val&0xFF,(val&0xFF00)>>8];
342         self.writecmd(0x11,0xE1,4,data);
343         rval=ord(self.data[0])+(ord(self.data[1])<<8);
344         if(val!=rval):
345             print "FLASH WRITE ERROR AT %04x.  Found %04x, wrote %04x." % (adr,rval,val);
346             
347     def MSP430dumpbsl(self):
348         self.MSP430dumpmem(0xC00,0xfff);
349     def MSP430dumpallmem(self):
350         self.MSP430dumpmem(0x200,0xffff);
351     def MSP430dumpmem(self,begin,end):
352         i=begin;
353         while i<end:
354             print "%04x %04x" % (i, self.MSP430peek(i));
355             i+=2;
356     def CCtest(self):
357         self.CCreleasecpu();
358         self.CChaltcpu();
359         print "Status: %s" % self.CCstatusstr();
360         
361         #Grab ident three times, should be equal.
362         ident1=self.CCident();
363         ident2=self.CCident();
364         ident3=self.CCident();
365         if(ident1!=ident2 or ident2!=ident3):
366             print "Error, repeated ident attempts unequal."
367             print "%04x, %04x, %04x" % (ident1, ident2, ident3);
368         
369         #Single step, printing PC.
370         #print "Tracing execution at startup."
371         for i in range(1,15):
372             pc=self.CCgetPC();
373             byte=self.CCpeekcodebyte(i);
374             print "PC=%04x, %02x" % (pc, byte);
375             self.CCstep_instr();
376         
377         #print "Verifying that debugging a NOP doesn't affect the PC."
378         for i in range(1,15):
379             pc=self.CCgetPC();
380             self.CCdebuginstr([0x00]);
381             if(pc!=self.CCgetPC()):
382                 print "ERROR: PC changed during CCdebuginstr([NOP])!";
383         for i in range(0xE500,0xE600):
384             byte=self.CCpeekdatabyte(i);
385             print "data %04x: %02x" % (i,byte);
386             self.CCpokedatabyte(i,i&0xFF);
387             byte=self.CCpeekdatabyte(i);
388             print "data %04x: %02x" % (i,byte);
389         print "Status: %s." % self.CCstatusstr();
390         #Exit debugger
391         self.CCstop();
392         print "Done.";