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