goodfet.spiflash can erase, poke byte.
[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                         0xC2: "MXIC"};
120
121     JEDECdevices={0xFFFFFF: "MISSING",
122                   0xEF3014: "W25X80L",
123                   0xEF3013: "W25X40L",
124                   0xEF3012: "W25X20L",
125                   0xEF3011: "W25X10L",
126                   0xC22014: "MX25L8005",
127                   0xC22013: "MX25L4005"
128                   };
129     def SPIjedec(self):
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]);
138         return data;
139     def SPIpeek(self,adr):
140         """Grab a byte from an SPI Flash ROM."""
141         data=[0x03,
142               (adr&0xFF0000)>>16,
143               (adr&0xFF00)>>8,
144               adr&0xFF,
145               0];
146         self.SPItrans(data);
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,
151               (adr&0xFF00)>>8,
152               adr&0xFF];
153         
154         self.writecmd(0x01,0x02,3,data);
155         return self.data;
156     def SPIpokebyte(self,adr,val):
157         self.SPIwriteenable();
158         data=[0x02,
159               (adr&0xFF0000)>>16,
160               (adr&0xFF00)>>8,
161               adr&0xFF,
162               val];
163         self.SPItrans(data);
164     def SPIchiperase(self):
165         """Mass erase an SPI Flash ROM."""
166         self.SPIwriteenable();
167         #Chip Erase
168         data=[0xC7];
169         self.SPItrans(data);
170     def SPIwriteenable(self):
171         """SPI Flash Write Enable"""
172         data=[0x06];
173         self.SPItrans(data);
174         
175     def SPIjedecmanstr(self):
176         """Grab the JEDEC manufacturer string.  Call after SPIjedec()."""
177         man=self.JEDECmanufacturers.get(self.JEDECmanufacturer)
178         if man==0:
179             man="UNKNOWN";
180         return man;
181     
182     def SPIjedecstr(self):
183         """Grab the JEDEC manufacturer string.  Call after SPIjedec()."""
184         man=self.JEDECmanufacturers.get(self.JEDECmanufacturer);
185         if man==0:
186             man="UNKNOWN";
187         device=self.JEDECdevices.get(self.JEDECdevice);
188         if device==0:
189             device="???"
190         return "%s %s" % (man,device);
191     def MSP430setup(self):
192         """Move the FET into the MSP430 JTAG application."""
193         print "Initializing MSP430.";
194         self.writecmd(0x11,0x10,0,self.data);
195
196     
197     
198     def CCsetup(self):
199         """Move the FET into the CC2430/CC2530 application."""
200         print "Initializing Chipcon.";
201         self.writecmd(0x30,0x10,0,self.data);
202     def CCrd_config(self):
203         """Read the config register of a Chipcon."""
204         self.writecmd(0x30,0x82,0,self.data);
205         return ord(self.data[0]);
206     def CCwr_config(self,config):
207         """Write the config register of a Chipcon."""
208         self.writecmd(0x30,0x81,1,[config&0xFF]);
209     
210     CCversions={0x0100:"CC1110",
211                 0x8500:"CC2430",
212                 0x8900:"CC2431",
213                 0x8100:"CC2510",
214                 0x9100:"CC2511",
215                 0xFF00:"CCmissing"};
216     def CCidentstr(self):
217         ident=self.CCident();
218         chip=self.CCversions[ident&0xFF00];
219         return "%s/r%02x" % (chip, ident&0xFF); 
220     def CCident(self):
221         """Get a chipcon's ID."""
222         self.writecmd(0x30,0x8B,0,None);
223         chip=ord(self.data[0]);
224         rev=ord(self.data[1]);
225         return (chip<<8)+rev;
226     def CCgetPC(self):
227         """Get a chipcon's PC."""
228         self.writecmd(0x30,0x83,0,None);
229         hi=ord(self.data[0]);
230         lo=ord(self.data[1]);
231         return (hi<<8)+lo;
232     def CCdebuginstr(self,instr):
233         self.writecmd(0x30,0x88,len(instr),instr);
234         return ord(self.data[0]);
235     def MSP430peek(self,adr):
236         """Read the contents of memory at an address."""
237         self.data=[adr&0xff, (adr&0xff00)>>8];
238         self.writecmd(0x11,0x02,2,self.data);
239         return ord(self.data[0])+(ord(self.data[1])<<8);
240     def CCpeekcodebyte(self,adr):
241         """Read the contents of code memory at an address."""
242         self.data=[adr&0xff, (adr&0xff00)>>8];
243         self.writecmd(0x30,0x90,2,self.data);
244         return ord(self.data[0]);
245     def CCpeekdatabyte(self,adr):
246         """Read the contents of data memory at an address."""
247         self.data=[adr&0xff, (adr&0xff00)>>8];
248         self.writecmd(0x30,0x91, 2, self.data);
249         return ord(self.data[0]);
250     def CCpokedatabyte(self,adr,val):
251         """Write a byte to data memory."""
252         self.data=[adr&0xff, (adr&0xff00)>>8, val];
253         self.writecmd(0x30, 0x92, 3, self.data);
254         return ord(self.data[0]);
255     def CCchiperase(self):
256         """Erase all of the target's memory."""
257         self.writecmd(0x30,0x80,0,None);
258     def CCstatus(self):
259         """Check the status."""
260         self.writecmd(0x30,0x84,0,None);
261         return ord(self.data[0])
262     CCstatusbits={0x80 : "erased",
263                   0x40 : "pcon_idle",
264                   0x20 : "halted",
265                   0x10 : "pm0",
266                   0x08 : "halted",
267                   0x04 : "locked",
268                   0x02 : "oscstable",
269                   0x01 : "overflow"};
270     def CCstatusstr(self):
271         """Check the status as a string."""
272         status=self.CCstatus();
273         str="";
274         i=1;
275         while i<0x100:
276             if(status&i):
277                 str="%s %s" %(self.CCstatusbits[i],str);
278             i*=2;
279         return str;
280     def MSP430poke(self,adr,val):
281         """Read the contents of memory at an address."""
282         self.data=[adr&0xff, (adr&0xff00)>>8, val&0xff, (val&0xff00)>>8];
283         self.writecmd(0x11,0x03,4,self.data);
284         return;# ord(self.data[0])+(ord(self.data[1])<<8);
285     def MSP430start(self):
286         """Start debugging."""
287         self.writecmd(0x11,0x20,0,self.data);
288         ident=self.MSP430ident();
289         print "Target identifies as %04x." % ident;
290     
291     def CCstart(self):
292         """Start debugging."""
293         self.writecmd(0x30,0x20,0,self.data);
294         ident=self.CCidentstr();
295         print "Target identifies as %s." % ident;
296         print "Status: %s." % self.CCstatusstr();
297         self.CCreleasecpu();
298         self.CChaltcpu();
299         print "Status: %s." % self.CCstatusstr();
300     def CCstop(self):
301         """Stop debugging."""
302         self.writecmd(0x30,0x21,0,self.data);
303     def CCstep_instr(self):
304         """Step one instruction."""
305         self.writecmd(0x30,0x89,0,self.data);
306     def MSP430stop(self):
307         """Stop debugging."""
308         self.writecmd(0x11,0x21,0,self.data);
309     def MSP430haltcpu(self):
310         """Halt the CPU."""
311         self.writecmd(0x11,0xA0,0,self.data);
312     def MSP430releasecpu(self):
313         """Resume the CPU."""
314         self.writecmd(0x11,0xA1,0,self.data);
315     def CChaltcpu(self):
316         """Halt the CPU."""
317         self.writecmd(0x30,0x86,0,self.data);
318     def CCreleasecpu(self):
319         """Resume the CPU."""
320         self.writecmd(0x30,0x87,0,self.data);
321     def MSP430shiftir8(self,ins):
322         """Shift the 8-bit Instruction Register."""
323         data=[ins];
324         self.writecmd(0x11,0x80,1,data);
325         return ord(self.data[0]);
326     def MSP430shiftdr16(self,dat):
327         """Shift the 16-bit Data Register."""
328         data=[dat&0xFF,(dat&0xFF00)>>8];
329         self.writecmd(0x11,0x81,2,data);
330         return ord(self.data[0])#+(ord(self.data[1])<<8);
331     def MSP430setinstrfetch(self):
332         """Set the instruction fetch mode."""
333         self.writecmd(0x11,0xC1,0,self.data);
334         return self.data[0];
335     def MSP430ident(self):
336         """Grab self-identification word from 0x0FF0 as big endian."""
337         i=self.MSP430peek(0x0ff0);
338         return ((i&0xFF00)>>8)+((i&0xFF)<<8)
339     def MSP430test(self):
340         """Test MSP430 JTAG.  Requires that a chip be attached."""
341         if self.MSP430ident()==0xffff:
342             print "Is anything connected?";
343         print "Testing RAM.";
344         temp=self.MSP430peek(0x0200);
345         self.MSP430poke(0x0200,0xdead);
346         if(self.MSP430peek(0x0200)!=0xdead):
347             print "Poke of 0x0200 did not set to 0xDEAD properly.";
348             return;
349         self.MSP430poke(0x0200,temp); #restore old value.
350     def MSP430flashtest(self):
351         self.MSP430masserase();
352         i=0x2500;
353         while(i<0xFFFF):
354             if(self.MSP430peek(i)!=0xFFFF):
355                 print "ERROR: Unerased flash at %04x."%i;
356             self.MSP430writeflash(i,0xDEAD);
357             i+=2;
358     def MSP430masserase(self):
359         """Erase MSP430 flash memory."""
360         self.writecmd(0x11,0xE3,0,None);
361     def MSP430writeflash(self,adr,val):
362         """Write a word of flash memory."""
363         if(self.MSP430peek(adr)!=0xFFFF):
364             print "FLASH ERROR: %04x not clear." % adr;
365         data=[adr&0xFF,(adr&0xFF00)>>8,val&0xFF,(val&0xFF00)>>8];
366         self.writecmd(0x11,0xE1,4,data);
367         rval=ord(self.data[0])+(ord(self.data[1])<<8);
368         if(val!=rval):
369             print "FLASH WRITE ERROR AT %04x.  Found %04x, wrote %04x." % (adr,rval,val);
370             
371     def MSP430dumpbsl(self):
372         self.MSP430dumpmem(0xC00,0xfff);
373     def MSP430dumpallmem(self):
374         self.MSP430dumpmem(0x200,0xffff);
375     def MSP430dumpmem(self,begin,end):
376         i=begin;
377         while i<end:
378             print "%04x %04x" % (i, self.MSP430peek(i));
379             i+=2;
380     def CCtest(self):
381         self.CCreleasecpu();
382         self.CChaltcpu();
383         print "Status: %s" % self.CCstatusstr();
384         
385         #Grab ident three times, should be equal.
386         ident1=self.CCident();
387         ident2=self.CCident();
388         ident3=self.CCident();
389         if(ident1!=ident2 or ident2!=ident3):
390             print "Error, repeated ident attempts unequal."
391             print "%04x, %04x, %04x" % (ident1, ident2, ident3);
392         
393         #Single step, printing PC.
394         #print "Tracing execution at startup."
395         for i in range(1,15):
396             pc=self.CCgetPC();
397             byte=self.CCpeekcodebyte(i);
398             print "PC=%04x, %02x" % (pc, byte);
399             self.CCstep_instr();
400         
401         #print "Verifying that debugging a NOP doesn't affect the PC."
402         for i in range(1,15):
403             pc=self.CCgetPC();
404             self.CCdebuginstr([0x00]);
405             if(pc!=self.CCgetPC()):
406                 print "ERROR: PC changed during CCdebuginstr([NOP])!";
407         for i in range(0xE500,0xE600):
408             byte=self.CCpeekdatabyte(i);
409             print "data %04x: %02x" % (i,byte);
410             self.CCpokedatabyte(i,i&0xFF);
411             byte=self.CCpeekdatabyte(i);
412             print "data %04x: %02x" % (i,byte);
413         print "Status: %s." % self.CCstatusstr();
414         #Exit debugger
415         self.CCstop();
416         print "Done.";