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