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