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