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, time;
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.pokebysym("FREQ2",freq2);
91 self.pokebysym("FREQ1",freq1);
92 self.pokebysym("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 shellcode(self,code,wait=1):
119 """Copy a block of code into RAM and execute it."""
123 self.pokebyte(0xF000+i,byte);
125 #print "Code loaded, executing."
126 self.CCdebuginstr([0x02, 0xf0, 0x00]); #ljmp 0xF000
128 while wait>0 and (0==self.CCstatus()&0x20):
130 #print "Waiting for shell code to return.";
132 def CC1110_crystal(self):
133 """Start the main crystal of the CC1110 oscillating, needed for radio use."""
134 code=[0x53, 0xBE, 0xFB, #anl SLEEP, #0xFB
136 0xE5, 0xBE, #mov a,SLEEP
137 0x30, 0xE6, 0xFB, #jnb acc.6, back
138 0x53, 0xc6, 0xB8, #anl CLKCON, #0xB8
140 0xE5, 0xC6, #mov a,CLKCON
141 0x20, 0xE6, 0xFB, #jb acc.6, two
142 0x43, 0xBE, 0x04, #orl SLEEP, #0x04
145 self.shellcode(code);
149 self.pokebyte(RFST,0x04); #Return to idle state.
151 def RF_carrier(self):
152 """Hold a carrier wave on the present frequency."""
154 self.CC1110_crystal(); #FIXME, '1110 specific.
165 #self.pokebysym("FSCTRL1" , 0x12) # Frequency synthesizer control.
166 #self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
167 self.pokebysym("FSCTRL1" , 0x0a) # Frequency synthesizer control.
168 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
170 #Don't change these while the radio is active.
171 self.pokebysym("FSCAL3" , 0xA9) # Frequency synthesizer calibration.
172 self.pokebysym("FSCAL2" , 0x0A) # Frequency synthesizer calibration.
173 self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
174 self.pokebysym("FSCAL0" , 0x11) # Frequency synthesizer calibration.
176 #Ossmann's settings, not yet sure how they differ.
177 #self.pokebysym("FSCAL3" , 0xEA) # Frequency synthesizer calibration.
178 #self.pokebysym("FSCAL2" , 0x2A) # Frequency synthesizer calibration.
179 #self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
180 #self.pokebysym("FSCAL0" , 0x1F) # Frequency synthesizer calibration.
183 #self.pokebysym("FREQ2" , 0x10) # Frequency control word, high byte.
184 #self.pokebysym("FREQ1" , 0xEC) # Frequency control word, middle byte.
185 #self.pokebysym("FREQ0" , 0x4E) # Frequency control word, low byte.
186 self.pokebysym("MDMCFG4" , 0x86) # Modem configuration.
187 self.pokebysym("MDMCFG3" , 0x83) # Modem configuration.
188 self.pokebysym("MDMCFG2" , 0x30) # Modem configuration.
189 self.pokebysym("MDMCFG1" , 0x22) # Modem configuration.
190 self.pokebysym("MDMCFG0" , 0xF8) # Modem configuration.
191 self.pokebysym("CHANNR" , 0x00) # Channel number.
192 self.pokebysym("DEVIATN" , 0x00) # Modem deviation setting (when FSK modulation is enabled).
193 self.pokebysym("FREND1" , 0x56) # Front end RX configuration.
195 self.pokebysym("FREND0" , 0x10) # Front end RX configuration.
196 self.pokebysym("MCSM0" , 0x14) # Main Radio Control State Machine configuration.
197 self.pokebysym("FOCCFG" , 0x16) # Frequency Offset Compensation Configuration.
198 self.pokebysym("BSCFG" , 0x6C) # Bit synchronization Configuration.
200 self.pokebysym("AGCCTRL2" , 0x03) # AGC control.
201 self.pokebysym("AGCCTRL1" , 0x40) # AGC control.
202 self.pokebysym("AGCCTRL0" , 0x91) # AGC control.
209 self.pokebysym("TEST2" , 0x88) # Various test settings.
210 self.pokebysym("TEST1" , 0x31) # Various test settings.
211 self.pokebysym("TEST0" , 0x09) # Various test settings.
212 self.pokebysym("PA_TABLE0", 0xC0) # PA output power setting.
213 self.pokebysym("PKTCTRL1" , 0x04) # Packet automation control.
214 self.pokebysym("PKTCTRL0" , 0x22) # Packet automation control.
215 self.pokebysym("ADDR" , 0x00) # Device address.
216 self.pokebysym("PKTLEN" , 0xFF) # Packet length.
218 self.pokebysym("SYNC1",0xAA);
219 self.pokebysym("SYNC0",0xAA);
223 #while ((MARCSTATE & MARCSTATE_MARC_STATE) != MARC_STATE_TX);
226 while((state!=0x13)):
227 self.pokebyte(RFST,0x03); #RFST=RFST_STX
229 state=self.peekbysym("MARCSTATE")&0x1F;
230 #print "state=%02x" % state;
231 print "Holding a carrier on %f MHz." % (self.RF_getfreq()/10**6);
233 #Not needed, radio works when CPU is halted.
239 def RF_getrssi(self):
240 """Returns the received signal strenght, with a weird offset."""
242 rssireg=self.symbols.get("RSSI");
243 return self.CCpeekdatabyte(rssireg);
245 if self.verbose>0: print "RSSI reg doesn't exist.";
247 #RSSI doesn't exist on 2.4GHz devices. Maybe RSSIL and RSSIH?
248 rssilreg=self.symbols.get("RSSIL");
249 rssil=self.CCpeekdatabyte(rssilreg);
250 rssihreg=self.symbols.get("RSSIL");
251 rssih=self.CCpeekdatabyte(rssihreg);
252 return (rssih<<8)|rssil;
254 if self.verbose>0: print "RSSIL/RSSIH regs don't exist.";
259 def SRF_loadsymbols(self):
260 ident=self.CCident();
261 chip=self.CCversions.get(ident&0xFF00);
262 dom=self.SRF_chipdom(chip,"register_definition.xml");
263 for e in dom.getElementsByTagName("registerdefinition"):
264 for f in e.childNodes:
265 if f.localName=="Register":
270 for g in f.childNodes:
271 if g.localName=="Name":
272 name=g.childNodes[0].nodeValue;
273 elif g.localName=="Address":
274 address=g.childNodes[0].nodeValue;
275 elif g.localName=="Description":
277 description=g.childNodes[0].nodeValue;
278 elif g.localName=="Bitfield":
279 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
280 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
281 self.symbols.define(eval(address),name,description,"data");
287 self.writecmd(self.APP,0x86,0,self.data);
290 def CCreleasecpu(self):
291 """Resume the CPU."""
292 self.writecmd(self.APP,0x87,0,self.data);
296 #print "Status: %s" % self.CCstatusstr();
298 #Grab ident three times, should be equal.
299 ident1=self.CCident();
300 ident2=self.CCident();
301 ident3=self.CCident();
302 if(ident1!=ident2 or ident2!=ident3):
303 print "Error, repeated ident attempts unequal."
304 print "%04x, %04x, %04x" % (ident1, ident2, ident3);
306 #Single step, printing PC.
307 print "Tracing execution at startup."
308 for i in range(1,15):
310 byte=self.CCpeekcodebyte(i);
311 #print "PC=%04x, %02x" % (pc, byte);
314 print "Verifying that debugging a NOP doesn't affect the PC."
315 for i in range(1,15):
317 self.CCdebuginstr([0x00]);
318 if(pc!=self.CCgetPC()):
319 print "ERROR: PC changed during CCdebuginstr([NOP])!";
321 print "Checking pokes to XRAM."
322 for i in range(0xf000,0xf020):
323 self.CCpokedatabyte(i,0xde);
324 if(self.CCpeekdatabyte(i)!=0xde):
325 print "Error in XDATA at 0x%04x" % i;
327 #print "Status: %s." % self.CCstatusstr();
333 """Move the FET into the CC2430/CC2530 application."""
334 #print "Initializing Chipcon.";
335 self.writecmd(self.APP,0x10,0,self.data);
336 def CCrd_config(self):
337 """Read the config register of a Chipcon."""
338 self.writecmd(self.APP,0x82,0,self.data);
339 return ord(self.data[0]);
340 def CCwr_config(self,config):
341 """Write the config register of a Chipcon."""
342 self.writecmd(self.APP,0x81,1,[config&0xFF]);
343 def CClockchip(self):
344 """Set the flash lock bit in info mem."""
345 self.writecmd(self.APP, 0x9A, 0, None);
347 """Set the flash lock bit in info mem."""
351 CCversions={0x0100:"cc1110",
357 0xA500:"cc2530", #page 52 of SWRU191
360 CCpagesizes={0x01: 1024, #"CC1110",
361 0x11: 1024, #"CC1111",
362 0x85: 2048, #"CC2430",
363 0x89: 2048, #"CC2431",
364 0x81: 1024, #"CC2510",
365 0x91: 1024, #"CC2511",
366 0xA5: 2048, #"CC2530", #page 52 of SWRU191
367 0xB5: 2048, #"CC2531",
368 0xFF: 0 } #"CCmissing"};
369 def infostring(self):
370 return self.CCidentstr();
371 def CCidentstr(self):
372 ident=self.CCident();
373 chip=self.CCversions.get(ident&0xFF00);
374 pagesize=self.CCpagesizes.get(ident>0xFF);
376 return "%s/r%0.4x/ps0x%0.4x" % (chip, ident, pagesize);
378 return "%04x" % ident;
380 """Get a chipcon's ID."""
381 self.writecmd(self.APP,0x8B,0,None);
382 chip=ord(self.data[0]);
383 rev=ord(self.data[1]);
384 return (chip<<8)+rev;
385 def CCpagesize(self):
386 """Get a chipcon's ID."""
387 self.writecmd(self.APP,0x8B,0,None);
388 chip=ord(self.data[0]);
389 size=self.CCpagesizes.get(chip);
391 print "ERROR: Pagesize undefined.";
392 print "chip=%0.4x" %chip;
397 return self.CCgetPC();
399 """Get a chipcon's PC."""
400 self.writecmd(self.APP,0x83,0,None);
401 hi=ord(self.data[0]);
402 lo=ord(self.data[1]);
404 def CCcmd(self,phrase):
405 self.writecmd(self.APP,0x00,len(phrase),phrase);
406 val=ord(self.data[0]);
407 print "Got %02x" % val;
409 def CCdebuginstr(self,instr):
410 self.writecmd(self.APP,0x88,len(instr),instr);
411 return ord(self.data[0]);
412 def peekblock(self,adr,length,memory="vn"):
413 """Return a block of data."""
414 data=[adr&0xff, (adr&0xff00)>>8,
415 length&0xFF,(length&0xFF00)>>8];
416 self.writecmd(self.APP,0x91,4,data);
417 return [ord(x) for x in self.data]
418 def peek8(self,address, memory="code"):
419 if(memory=="code" or memory=="flash" or memory=="vn"):
420 return self.CCpeekcodebyte(address);
421 elif(memory=="data" or memory=="xdata" or memory=="ram"):
422 return self.CCpeekdatabyte(address);
423 elif(memory=="idata" or memory=="iram"):
424 return self.CCpeekirambyte(address);
425 print "%s is an unknown memory." % memory;
427 def CCpeekcodebyte(self,adr):
428 """Read the contents of code memory at an address."""
429 self.data=[adr&0xff, (adr&0xff00)>>8];
430 self.writecmd(self.APP,0x90,2,self.data);
431 return ord(self.data[0]);
432 def CCpeekdatabyte(self,adr):
433 """Read the contents of data memory at an address."""
434 self.data=[adr&0xff, (adr&0xff00)>>8];
435 self.writecmd(self.APP,0x91, 2, self.data);
436 return ord(self.data[0]);
437 def CCpeekirambyte(self,adr):
438 """Read the contents of IRAM at an address."""
439 self.data=[adr&0xff];
440 self.writecmd(self.APP,0x02, 1, self.data);
441 return ord(self.data[0]);
442 def CCpeekiramword(self,adr):
443 """Read the little-endian contents of IRAM at an address."""
444 return self.CCpeekirambyte(adr)+(
445 self.CCpeekirambyte(adr+1)<<8);
446 def CCpokeiramword(self,adr,val):
447 self.CCpokeirambyte(adr,val&0xff);
448 self.CCpokeirambyte(adr+1,(val>>8)&0xff);
449 def CCpokeirambyte(self,adr,val):
450 """Write the contents of IRAM at an address."""
451 self.data=[adr&0xff, val&0xff];
452 self.writecmd(self.APP,0x02, 2, self.data);
453 return ord(self.data[0]);
454 def pokebyte(self,adr,val,mem="data"):
456 print "FIXME: poking of non data bytes not yet supported.";
457 self.CCpokedatabyte(adr,val);
458 def CCpokedatabyte(self,adr,val):
459 """Write a byte to data memory."""
460 self.data=[adr&0xff, (adr&0xff00)>>8, val];
461 self.writecmd(self.APP, 0x92, 3, self.data);
462 return ord(self.data[0]);
463 def CCchiperase(self):
464 """Erase all of the target's memory."""
465 self.writecmd(self.APP,0x80,0,None);
467 """Erase all of the target's memory."""
472 """Check the status."""
473 self.writecmd(self.APP,0x84,0,None);
474 return ord(self.data[0])
476 CCstatusbits={0x80 : "erase_busy",
480 0x08 : "halt_status",
485 CCconfigbits={0x20 : "soft_power_mode", #new for CC2530
488 0x02 : "timer_suspend",
489 0x01 : "sel_flash_info_page" #stricken from CC2530
493 """Check the status as a string."""
494 status=self.CCstatus();
499 str="%s %s" %(self.CCstatusbits[i],str);
503 """Start debugging."""
505 self.writecmd(self.APP,0x20,0,self.data);
506 ident=self.CCidentstr();
507 #print "Target identifies as %s." % ident;
508 #print "Status: %s." % self.status();
511 #Get SmartRF Studio regs if they exist.
515 """Stop debugging."""
516 self.writecmd(self.APP,0x21,0,self.data);
517 def CCstep_instr(self):
518 """Step one instruction."""
519 self.writecmd(self.APP,0x89,0,self.data);
520 def CCeraseflashbuffer(self):
521 """Erase the 2kB flash buffer"""
522 self.writecmd(self.APP,0x99);
523 def CCflashpage(self,adr):
524 """Flash 2kB a page of flash from 0xF000 in XDATA"""
529 print "Flashing buffer to 0x%06x" % adr;
530 self.writecmd(self.APP,0x95,4,data);
532 def setsecret(self,value):
533 """Set a secret word for later retreival. Used by glitcher."""
535 pagelen = self.CCpagesize(); #Varies by chip.
536 print "page=%04x, pagelen=%04x" % (page,pagelen);
538 self.CCeraseflashbuffer();
539 print "Setting secret to %x" % value;
540 self.CCpokedatabyte(0xF000,value);
541 self.CCpokedatabyte(0xF800,value);
542 print "Setting secret to %x==%x" % (value,
543 self.CCpeekdatabyte(0xf000));
545 print "code[0]=%x" % self.CCpeekcodebyte(0);
547 """Get a secret word. Used by glitcher."""
548 secret=self.CCpeekcodebyte(0);
549 #print "Got secret %02x" % secret;
552 def dump(self,file,start=0,stop=0xffff):
553 """Dump an intel hex file from code memory."""
554 print "Dumping code from %04x to %04x as %s." % (start,stop,file);
558 h[i]=self.CCpeekcodebyte(i);
560 print "Dumped %04x."%i;
561 h.write_hex_file(file); #buffer to disk.
563 h.write_hex_file(file);
565 def flash(self,file):
566 """Flash an intel hex file to code memory."""
567 print "Flashing %s" % file;
571 pagelen = self.CCpagesize(); #Varies by chip.
573 #print "page=%04x, pagelen=%04x" % (page,pagelen);
577 #Wipe the RAM buffer for the next flash page.
578 self.CCeraseflashbuffer();
579 for i in h._buf.keys():
580 while(i>=page+pagelen):
582 self.CCflashpage(page);
583 #client.CCeraseflashbuffer();
585 print "Flashed page at %06x" % page
588 #Place byte into buffer.
589 self.CCpokedatabyte(0xF000+i-page,
593 print "Buffering %04x toward %06x" % (i,page);
595 self.CCflashpage(page);
596 print "Flashed final page at %06x" % page;