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