2 # GoodFET Client Library
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
6 # This code is being rewritten and refactored. You've been warned!
11 from GoodFET import GoodFET;
12 from intelhex import IntelHex;
14 import xml.dom.minidom;
16 class GoodFETCC(GoodFET):
17 """A GoodFET variant for use with Chipcon 8051 Zigbee SoC."""
23 smartrfpath="/opt/smartrf7";
24 def loadsymbols(self):
25 try: self.SRF_loadsymbols();
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)
33 def CMDrs(self,args=[]):
34 """Chip command to grab the radio state."""
36 self.SRF_radiostate();
38 print "Error printing radio state.";
39 print "SmartRF not found at %s." % self.smartrfpath;
40 def SRF_bitfieldstr(self,bf):
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):
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":
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":
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();
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):
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":
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":
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");
111 self.writecmd(self.APP,0x86,0,self.data);
114 def CCreleasecpu(self):
115 """Resume the CPU."""
116 self.writecmd(self.APP,0x87,0,self.data);
120 #print "Status: %s" % self.CCstatusstr();
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);
130 #Single step, printing PC.
131 print "Tracing execution at startup."
132 for i in range(1,15):
134 byte=self.CCpeekcodebyte(i);
135 #print "PC=%04x, %02x" % (pc, byte);
138 print "Verifying that debugging a NOP doesn't affect the PC."
139 for i in range(1,15):
141 self.CCdebuginstr([0x00]);
142 if(pc!=self.CCgetPC()):
143 print "ERROR: PC changed during CCdebuginstr([NOP])!";
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;
151 #print "Status: %s." % self.CCstatusstr();
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);
171 """Set the flash lock bit in info mem."""
175 CCversions={0x0100:"cc1110",
181 0xA500:"cc2530", #page 52 of SWRU191
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);
200 return "%s/r%0.4x/ps0x%0.4x" % (chip, ident, pagesize);
202 return "%04x" % ident;
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);
215 print "ERROR: Pagesize undefined.";
216 print "chip=%0.4x" %chip;
221 return self.CCgetPC();
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]);
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;
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;
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]);
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);
288 """Erase all of the target's memory."""
293 """Check the status."""
294 self.writecmd(self.APP,0x84,0,None);
295 return ord(self.data[0])
297 CCstatusbits={0x80 : "erase_busy",
301 0x08 : "halt_status",
306 CCconfigbits={0x20 : "soft_power_mode", #new for CC2530
309 0x02 : "timer_suspend",
310 0x01 : "sel_flash_info_page" #stricken from CC2530
314 """Check the status as a string."""
315 status=self.CCstatus();
320 str="%s %s" %(self.CCstatusbits[i],str);
324 """Start debugging."""
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();
332 #Get SmartRF Studio regs if they exist.
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"""
350 print "Flashing buffer to 0x%06x" % adr;
351 self.writecmd(self.APP,0x95,4,data);
353 def setsecret(self,value):
354 """Set a secret word for later retreival. Used by glitcher."""
356 pagelen = self.CCpagesize(); #Varies by chip.
357 print "page=%04x, pagelen=%04x" % (page,pagelen);
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));
366 print "code[0]=%x" % self.CCpeekcodebyte(0);
368 """Get a secret word. Used by glitcher."""
369 secret=self.CCpeekcodebyte(0);
370 #print "Got secret %02x" % secret;
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);
379 h[i]=self.CCpeekcodebyte(i);
381 print "Dumped %04x."%i;
382 h.write_hex_file(file); #buffer to disk.
384 h.write_hex_file(file);
386 def flash(self,file):
387 """Flash an intel hex file to code memory."""
388 print "Flashing %s" % file;
392 pagelen = self.CCpagesize(); #Varies by chip.
394 #print "page=%04x, pagelen=%04x" % (page,pagelen);
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):
403 self.CCflashpage(page);
404 #client.CCeraseflashbuffer();
406 print "Flashed page at %06x" % page
409 #Place byte into buffer.
410 self.CCpokedatabyte(0xF000+i-page,
414 print "Buffering %04x toward %06x" % (i,page);
416 self.CCflashpage(page);
417 print "Flashed final page at %06x" % page;