cb36031c0ea24e7d75ad8ce3a53f0be23d4faa31
[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(1,0x10,0,self.data); #SPI/SETUP
105         #self.readcmd();
106     def spitrans8(self,byte):
107         """Read and write 8 bits by SPI."""
108         self.data=[byte];
109         self.writecmd(1,0,1,self.data);    #SPI exchange
110         #self.readcmd();
111         
112         if self.app!=1 or self.verb!=0:
113             print "Error in SPI transaction; app=%02x, verb=%02x" % (self.app, self.verb);
114         return ord(self.data[0]);
115     def MSP430setup(self):
116         """Move the FET into the MSP430 JTAG application."""
117         print "Initializing MSP430.";
118         self.writecmd(0x11,0x10,0,self.data);
119     def CCsetup(self):
120         """Move the FET into the CC2430/CC2530 application."""
121         print "Initializing Chipcon.";
122         self.writecmd(0x30,0x10,0,self.data);
123     def CCrd_config(self):
124         """Read the config register of a Chipcon."""
125         self.writecmd(0x30,0x82,0,self.data);
126         return ord(self.data[0]);
127     def CCwr_config(self,config):
128         """Write the config register of a Chipcon."""
129         self.writecmd(0x30,0x81,1,[config&0xFF]);
130     
131     CCversions={0x0100:"CC1110",
132                 0x8500:"CC2430",
133                 0x8900:"CC2431",
134                 0x8100:"CC2510",
135                 0x9100:"CC2511",
136                 0xFF00:"CCmissing"};
137     def CCidentstr(self):
138         ident=self.CCident();
139         chip=self.CCversions[ident&0xFF00];
140         return "%s/r%02x" % (chip, ident&0xFF); 
141     def CCident(self):
142         """Get a chipcon's ID."""
143         self.writecmd(0x30,0x8B,0,None);
144         chip=ord(self.data[0]);
145         rev=ord(self.data[1]);
146         return (chip<<8)+rev;
147     def CCgetPC(self):
148         """Get a chipcon's PC."""
149         self.writecmd(0x30,0x83,0,None);
150         hi=ord(self.data[0]);
151         lo=ord(self.data[1]);
152         return (hi<<8)+lo;
153     def CCdebuginstr(self,instr):
154         self.writecmd(0x30,0x88,len(instr),instr);
155         return ord(self.data[0]);
156     def MSP430peek(self,adr):
157         """Read the contents of memory at an address."""
158         self.data=[adr&0xff, (adr&0xff00)>>8];
159         self.writecmd(0x11,0x02,2,self.data);
160         return ord(self.data[0])+(ord(self.data[1])<<8);
161     def CCpeekcodebyte(self,adr):
162         """Read the contents of code memory at an address."""
163         self.data=[adr&0xff, (adr&0xff00)>>8];
164         self.writecmd(0x30,0x90,2,self.data);
165         return ord(self.data[0]);
166     def CCpeekdatabyte(self,adr):
167         """Read the contents of data memory at an address."""
168         self.data=[adr&0xff, (adr&0xff00)>>8];
169         self.writecmd(0x30,0x91, 2, self.data);
170         return ord(self.data[0]);
171     def CCpokedatabyte(self,adr,val):
172         """Write a byte to data memory."""
173         self.data=[adr&0xff, (adr&0xff00)>>8, val];
174         self.writecmd(0x30, 0x92, 3, self.data);
175         return ord(self.data[0]);
176     def CCchiperase(self):
177         """Erase all of the target's memory."""
178         self.writecmd(0x30,0x80,0,None);
179     def CCstatus(self):
180         """Check the status."""
181         self.writecmd(0x30,0x84,0,None);
182         return ord(self.data[0])
183     CCstatusbits={0x80 : "erased",
184                   0x40 : "pcon_idle",
185                   0x20 : "halted",
186                   0x10 : "pm0",
187                   0x08 : "halted",
188                   0x04 : "locked",
189                   0x02 : "oscstable",
190                   0x01 : "overflow"};
191     def CCstatusstr(self):
192         """Check the status as a string."""
193         status=self.CCstatus();
194         str="";
195         i=1;
196         while i<0x100:
197             if(status&i):
198                 str="%s %s" %(self.CCstatusbits[i],str);
199             i*=2;
200         return str;
201     def MSP430poke(self,adr,val):
202         """Read the contents of memory at an address."""
203         self.data=[adr&0xff, (adr&0xff00)>>8, val&0xff, (val&0xff00)>>8];
204         self.writecmd(0x11,0x03,4,self.data);
205         return;# ord(self.data[0])+(ord(self.data[1])<<8);
206     def MSP430start(self):
207         """Start debugging."""
208         self.writecmd(0x11,0x20,0,self.data);
209         ident=self.MSP430ident();
210         print "Target identifies as %04x." % ident;
211     
212     def CCstart(self):
213         """Start debugging."""
214         self.writecmd(0x30,0x20,0,self.data);
215         ident=self.CCidentstr();
216         print "Target identifies as %s." % ident;
217         print "Status: %s." % self.CCstatusstr();
218         self.CCreleasecpu();
219         self.CChaltcpu();
220         print "Status: %s." % self.CCstatusstr();
221     def CCstop(self):
222         """Stop debugging."""
223         self.writecmd(0x30,0x21,0,self.data);
224     def CCstep_instr(self):
225         """Step one instruction."""
226         self.writecmd(0x30,0x89,0,self.data);
227     def MSP430stop(self):
228         """Stop debugging."""
229         self.writecmd(0x11,0x21,0,self.data);
230     def MSP430haltcpu(self):
231         """Halt the CPU."""
232         self.writecmd(0x11,0xA0,0,self.data);
233     def MSP430releasecpu(self):
234         """Resume the CPU."""
235         self.writecmd(0x11,0xA1,0,self.data);
236     def CChaltcpu(self):
237         """Halt the CPU."""
238         self.writecmd(0x30,0x86,0,self.data);
239     def CCreleasecpu(self):
240         """Resume the CPU."""
241         self.writecmd(0x30,0x87,0,self.data);
242     def MSP430shiftir8(self,ins):
243         """Shift the 8-bit Instruction Register."""
244         data=[ins];
245         self.writecmd(0x11,0x80,1,data);
246         return ord(self.data[0]);
247     def MSP430shiftdr16(self,dat):
248         """Shift the 16-bit Data Register."""
249         data=[dat&0xFF,(dat&0xFF00)>>8];
250         self.writecmd(0x11,0x81,2,data);
251         return ord(self.data[0])#+(ord(self.data[1])<<8);
252     def MSP430setinstrfetch(self):
253         """Set the instruction fetch mode."""
254         self.writecmd(0x11,0xC1,0,self.data);
255         return self.data[0];
256     def MSP430ident(self):
257         """Grab self-identification word from 0x0FF0 as big endian."""
258         i=self.MSP430peek(0x0ff0);
259         return ((i&0xFF00)>>8)+((i&0xFF)<<8)
260     def MSP430test(self):
261         """Test MSP430 JTAG.  Requires that a chip be attached."""
262         if self.MSP430ident()==0xffff:
263             print "Is anything connected?";
264         print "Testing RAM.";
265         temp=self.MSP430peek(0x0200);
266         self.MSP430poke(0x0200,0xdead);
267         if(self.MSP430peek(0x0200)!=0xdead):
268             print "Poke of 0x0200 did not set to 0xDEAD properly.";
269             return;
270         self.MSP430poke(0x0200,temp); #restore old value.
271     def MSP430flashtest(self):
272         self.MSP430masserase();
273         i=0x2500;
274         while(i<0xFFFF):
275             if(self.MSP430peek(i)!=0xFFFF):
276                 print "ERROR: Unerased flash at %04x."%i;
277             self.MSP430writeflash(i,0xDEAD);
278             i+=2;
279     def MSP430masserase(self):
280         """Erase MSP430 flash memory."""
281         self.writecmd(0x11,0xE3,0,None);
282     def MSP430writeflash(self,adr,val):
283         """Write a word of flash memory."""
284         if(self.MSP430peek(adr)!=0xFFFF):
285             print "FLASH ERROR: %04x not clear." % adr;
286         data=[adr&0xFF,(adr&0xFF00)>>8,val&0xFF,(val&0xFF00)>>8];
287         self.writecmd(0x11,0xE1,4,data);
288         rval=ord(self.data[0])+(ord(self.data[1])<<8);
289         if(val!=rval):
290             print "FLASH WRITE ERROR AT %04x.  Found %04x, wrote %04x." % (adr,rval,val);
291             
292     def MSP430dumpbsl(self):
293         self.MSP430dumpmem(0xC00,0xfff);
294     def MSP430dumpallmem(self):
295         self.MSP430dumpmem(0x200,0xffff);
296     def MSP430dumpmem(self,begin,end):
297         i=begin;
298         while i<end:
299             print "%04x %04x" % (i, self.MSP430peek(i));
300             i+=2;
301     def CCtest(self):
302         self.CCreleasecpu();
303         self.CChaltcpu();
304         print "Status: %s" % self.CCstatusstr();
305         
306         #Grab ident three times, should be equal.
307         ident1=self.CCident();
308         ident2=self.CCident();
309         ident3=self.CCident();
310         if(ident1!=ident2 or ident2!=ident3):
311             print "Error, repeated ident attempts unequal."
312             print "%04x, %04x, %04x" % (ident1, ident2, ident3);
313         
314         #Single step, printing PC.
315         #print "Tracing execution at startup."
316         for i in range(1,15):
317             pc=self.CCgetPC();
318             byte=self.CCpeekcodebyte(i);
319             print "PC=%04x, %02x" % (pc, byte);
320             self.CCstep_instr();
321         
322         #print "Verifying that debugging a NOP doesn't affect the PC."
323         for i in range(1,15):
324             pc=self.CCgetPC();
325             self.CCdebuginstr([0x00]);
326             if(pc!=self.CCgetPC()):
327                 print "ERROR: PC changed during CCdebuginstr([NOP])!";
328         for i in range(0xE500,0xE600):
329             byte=self.CCpeekdatabyte(i);
330             print "data %04x: %02x" % (i,byte);
331             self.CCpokedatabyte(i,i&0xFF);
332             byte=self.CCpeekdatabyte(i);
333             print "data %04x: %02x" % (i,byte);
334         print "Status: %s." % self.CCstatusstr();
335         #Exit debugger
336         self.CCstop();
337         print "Done.";