readChunk (may replace readMem) is implemented, moving memory into regs then reading...
[goodfet] / client / GoodFETCC.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 being rewritten and refactored.  You've been warned!
7
8 import sys;
9 import binascii;
10
11 from GoodFET import GoodFET;
12 from intelhex import IntelHex;
13
14 import xml.dom.minidom;
15
16 class GoodFETCC(GoodFET):
17     """A GoodFET variant for use with Chipcon 8051 Zigbee SoC."""
18     APP=0x30;
19     
20     
21     
22     
23     smartrfpath="/opt/smartrf7";
24     def loadsymbols(self):
25         try: self.SRF_loadsymbols();
26         except:
27             if self.verbose==1: print "SmartRF load failed.";
28     def SRF_chipdom(self,chip="cc1110", doc="register_definition.xml"):
29         fn="%s/config/xml/%s/%s" % (self.smartrfpath,chip,doc);
30         #print "Opening %s" % fn;
31         return xml.dom.minidom.parse(fn)
32         
33     def CMDrs(self,args=[]):
34         """Chip command to grab the radio state."""
35         self.SRF_radiostate();
36     def SRF_bitfieldstr(self,bf):
37         name="unused";
38         start=0;
39         stop=0;
40         access="";
41         reset="0x00";
42         description="";
43         for e in bf.childNodes:
44             if e.localName=="Name" and e.childNodes: name= e.childNodes[0].nodeValue;
45             elif e.localName=="Start": start=e.childNodes[0].nodeValue;
46             elif e.localName=="Stop": stop=e.childNodes[0].nodeValue;
47         return "   [%s:%s] %30s " % (start,stop,name);
48     def SRF_radiostate(self):
49         ident=self.CCident();
50         chip=self.CCversions.get(ident&0xFF00);
51         dom=self.SRF_chipdom(chip,"register_definition.xml");
52         for e in dom.getElementsByTagName("registerdefinition"):
53             for f in e.childNodes:
54                 if f.localName=="DeviceName":
55                     print "// %s RadioState" % (f.childNodes[0].nodeValue);
56                 elif f.localName=="Register":
57                     name="unknownreg";
58                     address="0xdead";
59                     description="";
60                     bitfields="";
61                     for g in f.childNodes:
62                         if g.localName=="Name":
63                             name=g.childNodes[0].nodeValue;
64                         elif g.localName=="Address":
65                             address=g.childNodes[0].nodeValue;
66                         elif g.localName=="Description":
67                             if g.childNodes:
68                                 description=g.childNodes[0].nodeValue;
69                         elif g.localName=="Bitfield":
70                             bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
71                     #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
72                     print "%-10s=0x%02x; /* %-50s */" % (
73                         name,self.CCpeekdatabyte(eval(address)), description);
74                     if bitfields!="": print bitfields.rstrip();
75     def RF_getrssi(self):
76         """Returns the received signal strenght, from 0 to 1."""
77         rssireg=self.symbols.get("RSSI");
78         return self.CCpeekdatabyte(rssireg);
79     def SRF_loadsymbols(self):
80         ident=self.CCident();
81         chip=self.CCversions.get(ident&0xFF00);
82         dom=self.SRF_chipdom(chip,"register_definition.xml");
83         for e in dom.getElementsByTagName("registerdefinition"):
84             for f in e.childNodes:
85                 if f.localName=="Register":
86                     name="unknownreg";
87                     address="0xdead";
88                     description="";
89                     bitfields="";
90                     for g in f.childNodes:
91                         if g.localName=="Name":
92                             name=g.childNodes[0].nodeValue;
93                         elif g.localName=="Address":
94                             address=g.childNodes[0].nodeValue;
95                         elif g.localName=="Description":
96                             if g.childNodes:
97                                 description=g.childNodes[0].nodeValue;
98                         elif g.localName=="Bitfield":
99                             bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
100                     #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
101                     self.symbols.define(eval(address),name,description,"data");
102     def halt(self):
103         """Halt the CPU."""
104         self.CChaltcpu();
105     def CChaltcpu(self):
106         """Halt the CPU."""
107         self.writecmd(self.APP,0x86,0,self.data);
108     def resume(self):
109         self.CCreleasecpu();
110     def CCreleasecpu(self):
111         """Resume the CPU."""
112         self.writecmd(self.APP,0x87,0,self.data);
113     def test(self):
114         self.CCreleasecpu();
115         self.CChaltcpu();
116         #print "Status: %s" % self.CCstatusstr();
117         
118         #Grab ident three times, should be equal.
119         ident1=self.CCident();
120         ident2=self.CCident();
121         ident3=self.CCident();
122         if(ident1!=ident2 or ident2!=ident3):
123             print "Error, repeated ident attempts unequal."
124             print "%04x, %04x, %04x" % (ident1, ident2, ident3);
125         
126         #Single step, printing PC.
127         print "Tracing execution at startup."
128         for i in range(1,15):
129             pc=self.CCgetPC();
130             byte=self.CCpeekcodebyte(i);
131             #print "PC=%04x, %02x" % (pc, byte);
132             self.CCstep_instr();
133         
134         print "Verifying that debugging a NOP doesn't affect the PC."
135         for i in range(1,15):
136             pc=self.CCgetPC();
137             self.CCdebuginstr([0x00]);
138             if(pc!=self.CCgetPC()):
139                 print "ERROR: PC changed during CCdebuginstr([NOP])!";
140         
141         print "Checking pokes to XRAM."
142         for i in range(0xf000,0xf020):
143             self.CCpokedatabyte(i,0xde);
144             if(self.CCpeekdatabyte(i)!=0xde):
145                 print "Error in XDATA at 0x%04x" % i;
146         
147         #print "Status: %s." % self.CCstatusstr();
148         #Exit debugger
149         self.stop();
150         print "Done.";
151
152     def setup(self):
153         """Move the FET into the CC2430/CC2530 application."""
154         #print "Initializing Chipcon.";
155         self.writecmd(self.APP,0x10,0,self.data);
156     def CCrd_config(self):
157         """Read the config register of a Chipcon."""
158         self.writecmd(self.APP,0x82,0,self.data);
159         return ord(self.data[0]);
160     def CCwr_config(self,config):
161         """Write the config register of a Chipcon."""
162         self.writecmd(self.APP,0x81,1,[config&0xFF]);
163     def CClockchip(self):
164         """Set the flash lock bit in info mem."""
165         self.writecmd(self.APP, 0x9A, 0, None);
166     def lock(self):
167         """Set the flash lock bit in info mem."""
168         self.CClockchip();
169     
170
171     CCversions={0x0100:"cc1110",
172                 0x8500:"cc2430",
173                 0x8900:"cc2431",
174                 0x8100:"cc2510",
175                 0x9100:"cc2511",
176                 0xA500:"cc2530", #page 52 of SWRU191
177                 0xB500:"cc2531",
178                 0xFF00:"CCmissing"};
179     CCpagesizes={0x01: 1024, #"CC1110",
180                  0x85: 2048, #"CC2430",
181                  0x89: 2048, #"CC2431",
182                  0x81: 1024, #"CC2510",
183                  0x91: 1024, #"CC2511",
184                  0xA5: 2048, #"CC2530", #page 52 of SWRU191
185                  0xB5: 2048, #"CC2531",
186                  0xFF: 0    } #"CCmissing"};
187     def infostring(self):
188         return self.CCidentstr();
189     def CCidentstr(self):
190         ident=self.CCident();
191         chip=self.CCversions.get(ident&0xFF00);
192         return "%s/r%02x" % (chip, ident&0xFF); 
193     def CCident(self):
194         """Get a chipcon's ID."""
195         self.writecmd(self.APP,0x8B,0,None);
196         chip=ord(self.data[0]);
197         rev=ord(self.data[1]);
198         return (chip<<8)+rev;
199     def CCpagesize(self):
200         """Get a chipcon's ID."""
201         self.writecmd(self.APP,0x8B,0,None);
202         chip=ord(self.data[0]);
203         size=self.CCpagesizes.get(chip);
204         if(size<10):
205             print "ERROR: Pagesize undefined.";
206             print "chip=%02x" %chip;
207             sys.exit(1);
208             #return 2048;
209         return size;
210     def getpc(self):
211         return self.CCgetPC();
212     def CCgetPC(self):
213         """Get a chipcon's PC."""
214         self.writecmd(self.APP,0x83,0,None);
215         hi=ord(self.data[0]);
216         lo=ord(self.data[1]);
217         return (hi<<8)+lo;
218     def CCcmd(self,phrase):
219         self.writecmd(self.APP,0x00,len(phrase),phrase);
220         val=ord(self.data[0]);
221         print "Got %02x" % val;
222         return val;
223     def CCdebuginstr(self,instr):
224         self.writecmd(self.APP,0x88,len(instr),instr);
225         return ord(self.data[0]);
226     def peek8(self,address, memory="code"):
227         if(memory=="code" or memory=="flash" or memory=="vn"):
228             return self.CCpeekcodebyte(address);
229         elif(memory=="data" or memory=="xdata" or memory=="ram"):
230             return self.CCpeekdatabyte(address);
231         elif(memory=="idata" or memory=="iram"):
232             return self.CCpeekirambyte(address);
233         print "%s is an unknown memory." % memory;
234         return 0xdead;
235     def CCpeekcodebyte(self,adr):
236         """Read the contents of code memory at an address."""
237         self.data=[adr&0xff, (adr&0xff00)>>8];
238         self.writecmd(self.APP,0x90,2,self.data);
239         return ord(self.data[0]);
240     def CCpeekdatabyte(self,adr):
241         """Read the contents of data memory at an address."""
242         self.data=[adr&0xff, (adr&0xff00)>>8];
243         self.writecmd(self.APP,0x91, 2, self.data);
244         return ord(self.data[0]);
245     def CCpeekirambyte(self,adr):
246         """Read the contents of IRAM at an address."""
247         self.data=[adr&0xff];
248         self.writecmd(self.APP,0x02, 1, self.data);
249         return ord(self.data[0]);
250     def CCpeekiramword(self,adr):
251         """Read the little-endian contents of IRAM at an address."""
252         return self.CCpeekirambyte(adr)+(
253             self.CCpeekirambyte(adr+1)<<8);
254     def CCpokeiramword(self,adr,val):
255         self.CCpokeirambyte(adr,val&0xff);
256         self.CCpokeirambyte(adr+1,(val>>8)&0xff);
257     def CCpokeirambyte(self,adr,val):
258         """Write the contents of IRAM at an address."""
259         self.data=[adr&0xff, val&0xff];
260         self.writecmd(self.APP,0x02, 2, self.data);
261         return ord(self.data[0]);
262     
263     def CCpokedatabyte(self,adr,val):
264         """Write a byte to data memory."""
265         self.data=[adr&0xff, (adr&0xff00)>>8, val];
266         self.writecmd(self.APP, 0x92, 3, self.data);
267         return ord(self.data[0]);
268     def CCchiperase(self):
269         """Erase all of the target's memory."""
270         self.writecmd(self.APP,0x80,0,None);
271     def erase(self):
272         """Erase all of the target's memory."""
273         self.CCchiperase();
274         self.start();
275     
276     def CCstatus(self):
277         """Check the status."""
278         self.writecmd(self.APP,0x84,0,None);
279         return ord(self.data[0])
280     #Same as CC2530
281     CCstatusbits={0x80 : "erase_busy",
282                   0x40 : "pcon_idle",
283                   0x20 : "cpu_halted",
284                   0x10 : "pm0",
285                   0x08 : "halt_status",
286                   0x04 : "locked",
287                   0x02 : "oscstable",
288                   0x01 : "overflow"
289                   };
290     CCconfigbits={0x20 : "soft_power_mode",   #new for CC2530
291                   0x08 : "timers_off",
292                   0x04 : "dma_pause",
293                   0x02 : "timer_suspend",
294                   0x01 : "sel_flash_info_page" #stricken from CC2530
295                   };
296                   
297     def status(self):
298         """Check the status as a string."""
299         status=self.CCstatus();
300         str="";
301         i=1;
302         while i<0x100:
303             if(status&i):
304                 str="%s %s" %(self.CCstatusbits[i],str);
305             i*=2;
306         return str;
307     def start(self):
308         """Start debugging."""
309         self.setup();
310         self.writecmd(self.APP,0x20,0,self.data);
311         ident=self.CCidentstr();
312         #print "Target identifies as %s." % ident;
313         #print "Status: %s." % self.status();
314         self.CCreleasecpu();
315         self.CChaltcpu();
316         #Get SmartRF Studio regs if they exist.
317         self.loadsymbols(); 
318
319         #print "Status: %s." % self.status();
320         
321     def stop(self):
322         """Stop debugging."""
323         self.writecmd(self.APP,0x21,0,self.data);
324     def CCstep_instr(self):
325         """Step one instruction."""
326         self.writecmd(self.APP,0x89,0,self.data);
327     def CCeraseflashbuffer(self):
328         """Erase the 2kB flash buffer"""
329         self.writecmd(self.APP,0x99);
330     def CCflashpage(self,adr):
331         """Flash 2kB a page of flash from 0xF000 in XDATA"""
332         data=[adr&0xFF,
333               (adr>>8)&0xFF,
334               (adr>>16)&0xFF,
335               (adr>>24)&0xFF];
336         print "Flashing buffer to 0x%06x" % adr;
337         self.writecmd(self.APP,0x95,4,data);
338     
339     def setsecret(self,value):
340         """Set a secret word for later retreival.  Used by glitcher."""
341         page = 0x0000;
342         pagelen = self.CCpagesize(); #Varies by chip.
343         print "page=%04x, pagelen=%04x" % (page,pagelen);
344         
345         self.CCeraseflashbuffer();
346         print "Setting secret to %x" % value;
347         self.CCpokedatabyte(0xF000,value);
348         self.CCpokedatabyte(0xF800,value);
349         print "Setting secret to %x==%x" % (value,
350                                             self.CCpeekdatabyte(0xf000));
351         self.CCflashpage(0);
352         print "code[0]=%x" % self.CCpeekcodebyte(0);
353     def getsecret(self):
354         """Get a secret word.  Used by glitcher."""
355         secret=self.CCpeekcodebyte(0);
356         #print "Got secret %02x" % secret;
357         return secret;
358     
359     def dump(self,file,start=0,stop=0xffff):
360         """Dump an intel hex file from code memory."""
361         print "Dumping code from %04x to %04x as %s." % (start,stop,file);
362         h = IntelHex(None);
363         i=start;
364         while i<=stop:
365             h[i]=self.CCpeekcodebyte(i);
366             if(i%0x100==0):
367                 print "Dumped %04x."%i;
368                 h.write_hex_file(file); #buffer to disk.
369             i+=1;
370         h.write_hex_file(file);
371
372     def flash(self,file):
373         """Flash an intel hex file to code memory."""
374         print "Flashing %s" % file;
375         
376         h = IntelHex(file);
377         page = 0x0000;
378         pagelen = self.CCpagesize(); #Varies by chip.
379         
380         #print "page=%04x, pagelen=%04x" % (page,pagelen);
381         
382         bcount = 0;
383         
384         #Wipe the RAM buffer for the next flash page.
385         self.CCeraseflashbuffer();
386         for i in h._buf.keys():
387             while(i>=page+pagelen):
388                 if bcount>0:
389                     self.CCflashpage(page);
390                     #client.CCeraseflashbuffer();
391                     bcount=0;
392                     print "Flashed page at %06x" % page
393                 page+=pagelen;
394                     
395             #Place byte into buffer.
396             self.CCpokedatabyte(0xF000+i-page,
397                                 h[i]);
398             bcount+=1;
399             if(i%0x100==0):
400                 print "Buffering %04x toward %06x" % (i,page);
401         #last page
402         self.CCflashpage(page);
403         print "Flashed final page at %06x" % page;
404