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;
119 def CC1110_crystal(self):
120 """Start the main crystal of the CC1110 oscillating, needed for radio use."""
122 #//C code for the same.
123 #SLEEP &= ~SLEEP_OSC_PD;
124 #while( !(SLEEP & SLEEP_XOSC_S) );
125 #CLKCON = (CLKCON & ~(CLKCON_CLKSPD | CLKCON_OSC)) | CLKSPD_DIV_1;
126 #while (CLKCON & CLKCON_OSC);
127 #SLEEP |= SLEEP_OSC_PD;
129 #registers and constants.
130 #FIXME cc1110 specific
139 sleep=self.peekbyte(SLEEP);
140 sleep&=~SLEEP_XOSC_S;
141 self.pokebyte(SLEEP,sleep);
142 while(0==(self.peekbyte(SLEEP)&SLEEP_XOSC_S)):
144 clkcon=self.peekbyte(CLKCON);
145 clkcon=(clkcon & ~(CLKCON_CLKSPD | CLKCON_OSC)) | CLKSPD_DIV_1
146 self.pokebyte(CLKCON,clkcon);
148 while(clkcon&CLKCON_OSC):
149 clkcon=self.peekbyte(CLKCON);
150 sleep=self.peekbyte(SLEEP);
152 self.pokebyte(SLEEP,sleep);
157 self.pokebyte(RFST,0x04); #Return to idle state.
159 def RF_carrier(self):
160 """Hold a carrier wave on the present frequency."""
162 self.CC1110_crystal(); #FIXME, '1110 specific.
173 #self.pokebysym("FSCTRL1" , 0x12) # Frequency synthesizer control.
174 #self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
175 self.pokebysym("FSCTRL1" , 0x0a) # Frequency synthesizer control.
176 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
178 #Don't change these while the radio is active.
179 self.pokebysym("FSCAL3" , 0xA9) # Frequency synthesizer calibration.
180 self.pokebysym("FSCAL2" , 0x0A) # Frequency synthesizer calibration.
181 self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
182 self.pokebysym("FSCAL0" , 0x11) # Frequency synthesizer calibration.
184 #Ossmann's settings, not yet sure how they differ.
185 #self.pokebysym("FSCAL3" , 0xEA) # Frequency synthesizer calibration.
186 #self.pokebysym("FSCAL2" , 0x2A) # Frequency synthesizer calibration.
187 #self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
188 #self.pokebysym("FSCAL0" , 0x1F) # Frequency synthesizer calibration.
191 #self.pokebysym("FREQ2" , 0x10) # Frequency control word, high byte.
192 #self.pokebysym("FREQ1" , 0xEC) # Frequency control word, middle byte.
193 #self.pokebysym("FREQ0" , 0x4E) # Frequency control word, low byte.
194 self.pokebysym("MDMCFG4" , 0x86) # Modem configuration.
195 self.pokebysym("MDMCFG3" , 0x83) # Modem configuration.
196 self.pokebysym("MDMCFG2" , 0x30) # Modem configuration.
197 self.pokebysym("MDMCFG1" , 0x22) # Modem configuration.
198 self.pokebysym("MDMCFG0" , 0xF8) # Modem configuration.
199 self.pokebysym("CHANNR" , 0x00) # Channel number.
200 self.pokebysym("DEVIATN" , 0x00) # Modem deviation setting (when FSK modulation is enabled).
201 self.pokebysym("FREND1" , 0x56) # Front end RX configuration.
203 self.pokebysym("FREND0" , 0x10) # Front end RX configuration.
204 self.pokebysym("MCSM0" , 0x14) # Main Radio Control State Machine configuration.
205 self.pokebysym("FOCCFG" , 0x16) # Frequency Offset Compensation Configuration.
206 self.pokebysym("BSCFG" , 0x6C) # Bit synchronization Configuration.
208 self.pokebysym("AGCCTRL2" , 0x03) # AGC control.
209 self.pokebysym("AGCCTRL1" , 0x40) # AGC control.
210 self.pokebysym("AGCCTRL0" , 0x91) # AGC control.
217 self.pokebysym("TEST2" , 0x88) # Various test settings.
218 self.pokebysym("TEST1" , 0x31) # Various test settings.
219 self.pokebysym("TEST0" , 0x09) # Various test settings.
220 self.pokebysym("PA_TABLE0", 0xC0) # PA output power setting.
221 self.pokebysym("PKTCTRL1" , 0x04) # Packet automation control.
222 self.pokebysym("PKTCTRL0" , 0x22) # Packet automation control.
223 self.pokebysym("ADDR" , 0x00) # Device address.
224 self.pokebysym("PKTLEN" , 0xFF) # Packet length.
226 self.pokebysym("SYNC1",0xAA);
227 self.pokebysym("SYNC0",0xAA);
231 #while ((MARCSTATE & MARCSTATE_MARC_STATE) != MARC_STATE_TX);
234 while((state!=0x13)):
235 self.pokebyte(RFST,0x03); #RFST=RFST_STX
237 state=self.peekbysym("MARCSTATE")&0x1F;
238 print "state=%02x" % state;
239 print "Holding a carrier on %f MHz." % (self.RF_getfreq()/10**6);
241 #Not needed, radio works when CPU is halted.
247 def RF_getrssi(self):
248 """Returns the received signal strenght, with a weird offset."""
250 rssireg=self.symbols.get("RSSI");
251 return self.CCpeekdatabyte(rssireg);
253 if self.verbose>0: print "RSSI reg doesn't exist.";
255 #RSSI doesn't exist on 2.4GHz devices. Maybe RSSIL and RSSIH?
256 rssilreg=self.symbols.get("RSSIL");
257 rssil=self.CCpeekdatabyte(rssilreg);
258 rssihreg=self.symbols.get("RSSIL");
259 rssih=self.CCpeekdatabyte(rssihreg);
260 return (rssih<<8)|rssil;
262 if self.verbose>0: print "RSSIL/RSSIH regs don't exist.";
267 def SRF_loadsymbols(self):
268 ident=self.CCident();
269 chip=self.CCversions.get(ident&0xFF00);
270 dom=self.SRF_chipdom(chip,"register_definition.xml");
271 for e in dom.getElementsByTagName("registerdefinition"):
272 for f in e.childNodes:
273 if f.localName=="Register":
278 for g in f.childNodes:
279 if g.localName=="Name":
280 name=g.childNodes[0].nodeValue;
281 elif g.localName=="Address":
282 address=g.childNodes[0].nodeValue;
283 elif g.localName=="Description":
285 description=g.childNodes[0].nodeValue;
286 elif g.localName=="Bitfield":
287 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
288 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
289 self.symbols.define(eval(address),name,description,"data");
295 self.writecmd(self.APP,0x86,0,self.data);
298 def CCreleasecpu(self):
299 """Resume the CPU."""
300 self.writecmd(self.APP,0x87,0,self.data);
304 #print "Status: %s" % self.CCstatusstr();
306 #Grab ident three times, should be equal.
307 ident1=self.CCident();
308 ident2=self.CCident();
309 ident3=self.CCident();
310 if(ident1!=ident2 or ident2!=ident3):
311 print "Error, repeated ident attempts unequal."
312 print "%04x, %04x, %04x" % (ident1, ident2, ident3);
314 #Single step, printing PC.
315 print "Tracing execution at startup."
316 for i in range(1,15):
318 byte=self.CCpeekcodebyte(i);
319 #print "PC=%04x, %02x" % (pc, byte);
322 print "Verifying that debugging a NOP doesn't affect the PC."
323 for i in range(1,15):
325 self.CCdebuginstr([0x00]);
326 if(pc!=self.CCgetPC()):
327 print "ERROR: PC changed during CCdebuginstr([NOP])!";
329 print "Checking pokes to XRAM."
330 for i in range(0xf000,0xf020):
331 self.CCpokedatabyte(i,0xde);
332 if(self.CCpeekdatabyte(i)!=0xde):
333 print "Error in XDATA at 0x%04x" % i;
335 #print "Status: %s." % self.CCstatusstr();
341 """Move the FET into the CC2430/CC2530 application."""
342 #print "Initializing Chipcon.";
343 self.writecmd(self.APP,0x10,0,self.data);
344 def CCrd_config(self):
345 """Read the config register of a Chipcon."""
346 self.writecmd(self.APP,0x82,0,self.data);
347 return ord(self.data[0]);
348 def CCwr_config(self,config):
349 """Write the config register of a Chipcon."""
350 self.writecmd(self.APP,0x81,1,[config&0xFF]);
351 def CClockchip(self):
352 """Set the flash lock bit in info mem."""
353 self.writecmd(self.APP, 0x9A, 0, None);
355 """Set the flash lock bit in info mem."""
359 CCversions={0x0100:"cc1110",
365 0xA500:"cc2530", #page 52 of SWRU191
368 CCpagesizes={0x01: 1024, #"CC1110",
369 0x11: 1024, #"CC1111",
370 0x85: 2048, #"CC2430",
371 0x89: 2048, #"CC2431",
372 0x81: 1024, #"CC2510",
373 0x91: 1024, #"CC2511",
374 0xA5: 2048, #"CC2530", #page 52 of SWRU191
375 0xB5: 2048, #"CC2531",
376 0xFF: 0 } #"CCmissing"};
377 def infostring(self):
378 return self.CCidentstr();
379 def CCidentstr(self):
380 ident=self.CCident();
381 chip=self.CCversions.get(ident&0xFF00);
382 pagesize=self.CCpagesizes.get(ident>0xFF);
384 return "%s/r%0.4x/ps0x%0.4x" % (chip, ident, pagesize);
386 return "%04x" % ident;
388 """Get a chipcon's ID."""
389 self.writecmd(self.APP,0x8B,0,None);
390 chip=ord(self.data[0]);
391 rev=ord(self.data[1]);
392 return (chip<<8)+rev;
393 def CCpagesize(self):
394 """Get a chipcon's ID."""
395 self.writecmd(self.APP,0x8B,0,None);
396 chip=ord(self.data[0]);
397 size=self.CCpagesizes.get(chip);
399 print "ERROR: Pagesize undefined.";
400 print "chip=%0.4x" %chip;
405 return self.CCgetPC();
407 """Get a chipcon's PC."""
408 self.writecmd(self.APP,0x83,0,None);
409 hi=ord(self.data[0]);
410 lo=ord(self.data[1]);
412 def CCcmd(self,phrase):
413 self.writecmd(self.APP,0x00,len(phrase),phrase);
414 val=ord(self.data[0]);
415 print "Got %02x" % val;
417 def CCdebuginstr(self,instr):
418 self.writecmd(self.APP,0x88,len(instr),instr);
419 return ord(self.data[0]);
420 def peekblock(self,adr,length,memory="vn"):
421 """Return a block of data."""
422 data=[adr&0xff, (adr&0xff00)>>8,
423 length&0xFF,(length&0xFF00)>>8];
424 self.writecmd(self.APP,0x91,4,data);
425 return [ord(x) for x in self.data]
426 def peek8(self,address, memory="code"):
427 if(memory=="code" or memory=="flash" or memory=="vn"):
428 return self.CCpeekcodebyte(address);
429 elif(memory=="data" or memory=="xdata" or memory=="ram"):
430 return self.CCpeekdatabyte(address);
431 elif(memory=="idata" or memory=="iram"):
432 return self.CCpeekirambyte(address);
433 print "%s is an unknown memory." % memory;
435 def CCpeekcodebyte(self,adr):
436 """Read the contents of code memory at an address."""
437 self.data=[adr&0xff, (adr&0xff00)>>8];
438 self.writecmd(self.APP,0x90,2,self.data);
439 return ord(self.data[0]);
440 def CCpeekdatabyte(self,adr):
441 """Read the contents of data memory at an address."""
442 self.data=[adr&0xff, (adr&0xff00)>>8];
443 self.writecmd(self.APP,0x91, 2, self.data);
444 return ord(self.data[0]);
445 def CCpeekirambyte(self,adr):
446 """Read the contents of IRAM at an address."""
447 self.data=[adr&0xff];
448 self.writecmd(self.APP,0x02, 1, self.data);
449 return ord(self.data[0]);
450 def CCpeekiramword(self,adr):
451 """Read the little-endian contents of IRAM at an address."""
452 return self.CCpeekirambyte(adr)+(
453 self.CCpeekirambyte(adr+1)<<8);
454 def CCpokeiramword(self,adr,val):
455 self.CCpokeirambyte(adr,val&0xff);
456 self.CCpokeirambyte(adr+1,(val>>8)&0xff);
457 def CCpokeirambyte(self,adr,val):
458 """Write the contents of IRAM at an address."""
459 self.data=[adr&0xff, val&0xff];
460 self.writecmd(self.APP,0x02, 2, self.data);
461 return ord(self.data[0]);
462 def pokebyte(self,adr,val,mem="data"):
464 print "FIXME: poking of non data bytes not yet supported.";
465 self.CCpokedatabyte(adr,val);
466 def CCpokedatabyte(self,adr,val):
467 """Write a byte to data memory."""
468 self.data=[adr&0xff, (adr&0xff00)>>8, val];
469 self.writecmd(self.APP, 0x92, 3, self.data);
470 return ord(self.data[0]);
471 def CCchiperase(self):
472 """Erase all of the target's memory."""
473 self.writecmd(self.APP,0x80,0,None);
475 """Erase all of the target's memory."""
480 """Check the status."""
481 self.writecmd(self.APP,0x84,0,None);
482 return ord(self.data[0])
484 CCstatusbits={0x80 : "erase_busy",
488 0x08 : "halt_status",
493 CCconfigbits={0x20 : "soft_power_mode", #new for CC2530
496 0x02 : "timer_suspend",
497 0x01 : "sel_flash_info_page" #stricken from CC2530
501 """Check the status as a string."""
502 status=self.CCstatus();
507 str="%s %s" %(self.CCstatusbits[i],str);
511 """Start debugging."""
513 self.writecmd(self.APP,0x20,0,self.data);
514 ident=self.CCidentstr();
515 #print "Target identifies as %s." % ident;
516 #print "Status: %s." % self.status();
519 #Get SmartRF Studio regs if they exist.
523 """Stop debugging."""
524 self.writecmd(self.APP,0x21,0,self.data);
525 def CCstep_instr(self):
526 """Step one instruction."""
527 self.writecmd(self.APP,0x89,0,self.data);
528 def CCeraseflashbuffer(self):
529 """Erase the 2kB flash buffer"""
530 self.writecmd(self.APP,0x99);
531 def CCflashpage(self,adr):
532 """Flash 2kB a page of flash from 0xF000 in XDATA"""
537 print "Flashing buffer to 0x%06x" % adr;
538 self.writecmd(self.APP,0x95,4,data);
540 def setsecret(self,value):
541 """Set a secret word for later retreival. Used by glitcher."""
543 pagelen = self.CCpagesize(); #Varies by chip.
544 print "page=%04x, pagelen=%04x" % (page,pagelen);
546 self.CCeraseflashbuffer();
547 print "Setting secret to %x" % value;
548 self.CCpokedatabyte(0xF000,value);
549 self.CCpokedatabyte(0xF800,value);
550 print "Setting secret to %x==%x" % (value,
551 self.CCpeekdatabyte(0xf000));
553 print "code[0]=%x" % self.CCpeekcodebyte(0);
555 """Get a secret word. Used by glitcher."""
556 secret=self.CCpeekcodebyte(0);
557 #print "Got secret %02x" % secret;
560 def dump(self,file,start=0,stop=0xffff):
561 """Dump an intel hex file from code memory."""
562 print "Dumping code from %04x to %04x as %s." % (start,stop,file);
566 h[i]=self.CCpeekcodebyte(i);
568 print "Dumped %04x."%i;
569 h.write_hex_file(file); #buffer to disk.
571 h.write_hex_file(file);
573 def flash(self,file):
574 """Flash an intel hex file to code memory."""
575 print "Flashing %s" % file;
579 pagelen = self.CCpagesize(); #Varies by chip.
581 #print "page=%04x, pagelen=%04x" % (page,pagelen);
585 #Wipe the RAM buffer for the next flash page.
586 self.CCeraseflashbuffer();
587 for i in h._buf.keys():
588 while(i>=page+pagelen):
590 self.CCflashpage(page);
591 #client.CCeraseflashbuffer();
593 print "Flashed page at %06x" % page
596 #Place byte into buffer.
597 self.CCpokedatabyte(0xF000+i-page,
601 print "Buffering %04x toward %06x" % (i,page);
603 self.CCflashpage(page);
604 print "Flashed final page at %06x" % page;