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