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();
79 def RF_setfreq(self,frequency):
80 """Set the frequency in Hz."""
81 #FIXME CC1110 specific
84 freq=int(hz/396.728515625);
87 freq1=(freq&0xFF00)>>8;
88 freq2=(freq&0xFF0000)>>16;
90 self.pokebysim("FREQ2",freq2);
91 self.pokebysim("FREQ1",freq1);
92 self.pokebysim("FREQ0",freq0);
96 """Get the frequency in Hz."""
97 #FIXME CC1110 specific
99 #return (2400+self.peek(0x05))*10**6
100 #self.poke(0x05,chan);
102 #freq2=self.CCpeekdatabyte(0xdf09);
103 #freq1=self.CCpeekdatabyte(0xdf0a);
104 #freq0=self.CCpeekdatabyte(0xdf0b);
107 freq2=self.peekbysym("FREQ2");
108 freq1=self.peekbysym("FREQ1");
109 freq0=self.peekbysym("FREQ0");
110 freq=(freq2<<16)+(freq1<<8)+freq0;
114 hz=freq*396.728515625;
118 def RF_carrier(self):
119 """Hold a carrier wave on the present frequency."""
120 print "ERROR, this ain't working yet."
122 def RF_getrssi(self):
123 """Returns the received signal strenght, with a weird offset."""
125 rssireg=self.symbols.get("RSSI");
126 return self.CCpeekdatabyte(rssireg);
128 if self.verbose>0: print "RSSI reg doesn't exist.";
130 #RSSI doesn't exist on 2.4GHz devices. Maybe RSSIL and RSSIH?
131 rssilreg=self.symbols.get("RSSIL");
132 rssil=self.CCpeekdatabyte(rssilreg);
133 rssihreg=self.symbols.get("RSSIL");
134 rssih=self.CCpeekdatabyte(rssihreg);
135 return (rssih<<8)|rssil;
137 if self.verbose>0: print "RSSIL/RSSIH regs don't exist.";
142 def SRF_loadsymbols(self):
143 ident=self.CCident();
144 chip=self.CCversions.get(ident&0xFF00);
145 dom=self.SRF_chipdom(chip,"register_definition.xml");
146 for e in dom.getElementsByTagName("registerdefinition"):
147 for f in e.childNodes:
148 if f.localName=="Register":
153 for g in f.childNodes:
154 if g.localName=="Name":
155 name=g.childNodes[0].nodeValue;
156 elif g.localName=="Address":
157 address=g.childNodes[0].nodeValue;
158 elif g.localName=="Description":
160 description=g.childNodes[0].nodeValue;
161 elif g.localName=="Bitfield":
162 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
163 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
164 self.symbols.define(eval(address),name,description,"data");
170 self.writecmd(self.APP,0x86,0,self.data);
173 def CCreleasecpu(self):
174 """Resume the CPU."""
175 self.writecmd(self.APP,0x87,0,self.data);
179 #print "Status: %s" % self.CCstatusstr();
181 #Grab ident three times, should be equal.
182 ident1=self.CCident();
183 ident2=self.CCident();
184 ident3=self.CCident();
185 if(ident1!=ident2 or ident2!=ident3):
186 print "Error, repeated ident attempts unequal."
187 print "%04x, %04x, %04x" % (ident1, ident2, ident3);
189 #Single step, printing PC.
190 print "Tracing execution at startup."
191 for i in range(1,15):
193 byte=self.CCpeekcodebyte(i);
194 #print "PC=%04x, %02x" % (pc, byte);
197 print "Verifying that debugging a NOP doesn't affect the PC."
198 for i in range(1,15):
200 self.CCdebuginstr([0x00]);
201 if(pc!=self.CCgetPC()):
202 print "ERROR: PC changed during CCdebuginstr([NOP])!";
204 print "Checking pokes to XRAM."
205 for i in range(0xf000,0xf020):
206 self.CCpokedatabyte(i,0xde);
207 if(self.CCpeekdatabyte(i)!=0xde):
208 print "Error in XDATA at 0x%04x" % i;
210 #print "Status: %s." % self.CCstatusstr();
216 """Move the FET into the CC2430/CC2530 application."""
217 #print "Initializing Chipcon.";
218 self.writecmd(self.APP,0x10,0,self.data);
219 def CCrd_config(self):
220 """Read the config register of a Chipcon."""
221 self.writecmd(self.APP,0x82,0,self.data);
222 return ord(self.data[0]);
223 def CCwr_config(self,config):
224 """Write the config register of a Chipcon."""
225 self.writecmd(self.APP,0x81,1,[config&0xFF]);
226 def CClockchip(self):
227 """Set the flash lock bit in info mem."""
228 self.writecmd(self.APP, 0x9A, 0, None);
230 """Set the flash lock bit in info mem."""
234 CCversions={0x0100:"cc1110",
240 0xA500:"cc2530", #page 52 of SWRU191
243 CCpagesizes={0x01: 1024, #"CC1110",
244 0x11: 1024, #"CC1111",
245 0x85: 2048, #"CC2430",
246 0x89: 2048, #"CC2431",
247 0x81: 1024, #"CC2510",
248 0x91: 1024, #"CC2511",
249 0xA5: 2048, #"CC2530", #page 52 of SWRU191
250 0xB5: 2048, #"CC2531",
251 0xFF: 0 } #"CCmissing"};
252 def infostring(self):
253 return self.CCidentstr();
254 def CCidentstr(self):
255 ident=self.CCident();
256 chip=self.CCversions.get(ident&0xFF00);
257 pagesize=self.CCpagesizes.get(ident>0xFF);
259 return "%s/r%0.4x/ps0x%0.4x" % (chip, ident, pagesize);
261 return "%04x" % ident;
263 """Get a chipcon's ID."""
264 self.writecmd(self.APP,0x8B,0,None);
265 chip=ord(self.data[0]);
266 rev=ord(self.data[1]);
267 return (chip<<8)+rev;
268 def CCpagesize(self):
269 """Get a chipcon's ID."""
270 self.writecmd(self.APP,0x8B,0,None);
271 chip=ord(self.data[0]);
272 size=self.CCpagesizes.get(chip);
274 print "ERROR: Pagesize undefined.";
275 print "chip=%0.4x" %chip;
280 return self.CCgetPC();
282 """Get a chipcon's PC."""
283 self.writecmd(self.APP,0x83,0,None);
284 hi=ord(self.data[0]);
285 lo=ord(self.data[1]);
287 def CCcmd(self,phrase):
288 self.writecmd(self.APP,0x00,len(phrase),phrase);
289 val=ord(self.data[0]);
290 print "Got %02x" % val;
292 def CCdebuginstr(self,instr):
293 self.writecmd(self.APP,0x88,len(instr),instr);
294 return ord(self.data[0]);
295 def peekblock(self,adr,length,memory="vn"):
296 """Return a block of data."""
297 data=[adr&0xff, (adr&0xff00)>>8,
298 length&0xFF,(length&0xFF00)>>8];
299 self.writecmd(self.APP,0x91,4,data);
300 return [ord(x) for x in self.data]
301 def peek8(self,address, memory="code"):
302 if(memory=="code" or memory=="flash" or memory=="vn"):
303 return self.CCpeekcodebyte(address);
304 elif(memory=="data" or memory=="xdata" or memory=="ram"):
305 return self.CCpeekdatabyte(address);
306 elif(memory=="idata" or memory=="iram"):
307 return self.CCpeekirambyte(address);
308 print "%s is an unknown memory." % memory;
310 def CCpeekcodebyte(self,adr):
311 """Read the contents of code memory at an address."""
312 self.data=[adr&0xff, (adr&0xff00)>>8];
313 self.writecmd(self.APP,0x90,2,self.data);
314 return ord(self.data[0]);
315 def CCpeekdatabyte(self,adr):
316 """Read the contents of data memory at an address."""
317 self.data=[adr&0xff, (adr&0xff00)>>8];
318 self.writecmd(self.APP,0x91, 2, self.data);
319 return ord(self.data[0]);
320 def CCpeekirambyte(self,adr):
321 """Read the contents of IRAM at an address."""
322 self.data=[adr&0xff];
323 self.writecmd(self.APP,0x02, 1, self.data);
324 return ord(self.data[0]);
325 def CCpeekiramword(self,adr):
326 """Read the little-endian contents of IRAM at an address."""
327 return self.CCpeekirambyte(adr)+(
328 self.CCpeekirambyte(adr+1)<<8);
329 def CCpokeiramword(self,adr,val):
330 self.CCpokeirambyte(adr,val&0xff);
331 self.CCpokeirambyte(adr+1,(val>>8)&0xff);
332 def CCpokeirambyte(self,adr,val):
333 """Write the contents of IRAM at an address."""
334 self.data=[adr&0xff, val&0xff];
335 self.writecmd(self.APP,0x02, 2, self.data);
336 return ord(self.data[0]);
338 def CCpokedatabyte(self,adr,val):
339 """Write a byte to data memory."""
340 self.data=[adr&0xff, (adr&0xff00)>>8, val];
341 self.writecmd(self.APP, 0x92, 3, self.data);
342 return ord(self.data[0]);
343 def CCchiperase(self):
344 """Erase all of the target's memory."""
345 self.writecmd(self.APP,0x80,0,None);
347 """Erase all of the target's memory."""
352 """Check the status."""
353 self.writecmd(self.APP,0x84,0,None);
354 return ord(self.data[0])
356 CCstatusbits={0x80 : "erase_busy",
360 0x08 : "halt_status",
365 CCconfigbits={0x20 : "soft_power_mode", #new for CC2530
368 0x02 : "timer_suspend",
369 0x01 : "sel_flash_info_page" #stricken from CC2530
373 """Check the status as a string."""
374 status=self.CCstatus();
379 str="%s %s" %(self.CCstatusbits[i],str);
383 """Start debugging."""
385 self.writecmd(self.APP,0x20,0,self.data);
386 ident=self.CCidentstr();
387 #print "Target identifies as %s." % ident;
388 #print "Status: %s." % self.status();
391 #Get SmartRF Studio regs if they exist.
395 """Stop debugging."""
396 self.writecmd(self.APP,0x21,0,self.data);
397 def CCstep_instr(self):
398 """Step one instruction."""
399 self.writecmd(self.APP,0x89,0,self.data);
400 def CCeraseflashbuffer(self):
401 """Erase the 2kB flash buffer"""
402 self.writecmd(self.APP,0x99);
403 def CCflashpage(self,adr):
404 """Flash 2kB a page of flash from 0xF000 in XDATA"""
409 print "Flashing buffer to 0x%06x" % adr;
410 self.writecmd(self.APP,0x95,4,data);
412 def setsecret(self,value):
413 """Set a secret word for later retreival. Used by glitcher."""
415 pagelen = self.CCpagesize(); #Varies by chip.
416 print "page=%04x, pagelen=%04x" % (page,pagelen);
418 self.CCeraseflashbuffer();
419 print "Setting secret to %x" % value;
420 self.CCpokedatabyte(0xF000,value);
421 self.CCpokedatabyte(0xF800,value);
422 print "Setting secret to %x==%x" % (value,
423 self.CCpeekdatabyte(0xf000));
425 print "code[0]=%x" % self.CCpeekcodebyte(0);
427 """Get a secret word. Used by glitcher."""
428 secret=self.CCpeekcodebyte(0);
429 #print "Got secret %02x" % secret;
432 def dump(self,file,start=0,stop=0xffff):
433 """Dump an intel hex file from code memory."""
434 print "Dumping code from %04x to %04x as %s." % (start,stop,file);
438 h[i]=self.CCpeekcodebyte(i);
440 print "Dumped %04x."%i;
441 h.write_hex_file(file); #buffer to disk.
443 h.write_hex_file(file);
445 def flash(self,file):
446 """Flash an intel hex file to code memory."""
447 print "Flashing %s" % file;
451 pagelen = self.CCpagesize(); #Varies by chip.
453 #print "page=%04x, pagelen=%04x" % (page,pagelen);
457 #Wipe the RAM buffer for the next flash page.
458 self.CCeraseflashbuffer();
459 for i in h._buf.keys():
460 while(i>=page+pagelen):
462 self.CCflashpage(page);
463 #client.CCeraseflashbuffer();
465 print "Flashed page at %06x" % page
468 #Place byte into buffer.
469 self.CCpokedatabyte(0xF000+i-page,
473 print "Buffering %04x toward %06x" % (i,page);
475 self.CCflashpage(page);
476 print "Flashed final page at %06x" % page;