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