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, os;
16 class GoodFETCC(GoodFET):
17 """A GoodFET variant for use with Chipcon 8051 Zigbee SoC."""
21 def __init__(self,filename=None):
22 """GoodFETCC constructor.
23 Mostly concerned with finding SmartRF7."""
24 if self.smartrfpath==None:
25 self.smartrfpath=os.environ.get("SMARTRF");
26 if self.smartrfpath==None and os.name=='nt':
27 pf=os.environ['PROGRAMFILES'];
28 self.smartrfpath="%s\\\\Texas Instruments\\\\SmartRF Tools\\\\SmartRF Studio 7" % pf;
30 if self.smartrfpath==None:
31 self.smartrfpath="/opt/smartrf7";
33 haveloadedsymbols=False;
34 def loadsymbols(self):
35 if self.haveloadedsymbols:
38 self.SRF_loadsymbols();
39 self.haveloadedsymbols=True;
41 print "SmartRF not found for this chip.\nInstall it with wine and symlink to /opt/smartrf7 .";
42 def SRF_chipdom(self,chip="cc1110", doc="register_definition.xml"):
43 """Loads the chip XML definitions from SmartRF7."""
44 fn="%s/config/xml/%s/%s" % (self.smartrfpath,chip,doc);
45 #print "Opening %s" % fn;
46 return xml.dom.minidom.parse(fn)
48 def CMDrs(self,args=[]):
49 """Chip command to grab the radio state."""
51 self.SRF_radiostate();
53 # print "Error printing radio state.";
54 # print "SmartRF not found at %s." % self.smartrfpath;
55 def SRF_bitfieldstr(self,bf):
62 for e in bf.childNodes:
63 if e.localName=="Name" and e.childNodes: name= e.childNodes[0].nodeValue;
64 elif e.localName=="Start": start=e.childNodes[0].nodeValue;
65 elif e.localName=="Stop": stop=e.childNodes[0].nodeValue;
66 return " [%s:%s] %30s " % (start,stop,name);
68 def SRF_radiostate(self):
70 chip=self.CCversions.get(ident&0xFF00);
71 dom=self.SRF_chipdom(chip,"register_definition.xml");
72 for e in dom.getElementsByTagName("registerdefinition"):
73 for f in e.childNodes:
74 if f.localName=="DeviceName":
75 print "// %s RadioState" % (f.childNodes[0].nodeValue);
76 elif f.localName=="Register":
81 for g in f.childNodes:
82 if g.localName=="Name":
83 name=g.childNodes[0].nodeValue;
84 elif g.localName=="Address":
85 address=g.childNodes[0].nodeValue;
86 elif g.localName=="Description":
88 description=g.childNodes[0].nodeValue;
89 elif g.localName=="Bitfield":
90 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
91 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
92 print "%-10s=0x%02x; /* %-50s */" % (
93 name,self.CCpeekdatabyte(eval(address)), description);
94 if bitfields!="": print bitfields.rstrip();
96 def SRF_radiostate_select(self,args=[]):
99 chip=self.CCversions.get(ident&0xFF00);
100 dom=self.SRF_chipdom(chip,"register_definition.xml");
102 if reg.lower() == "help":
105 lreg.append(reg.lower())
106 for e in dom.getElementsByTagName("registerdefinition"):
107 for f in e.childNodes:
108 if f.localName=="DeviceName":
109 print "// %s RadioState" % (f.childNodes[0].nodeValue);
110 elif f.localName=="Register":
115 for g in f.childNodes:
116 if g.localName=="Name":
117 name=g.childNodes[0].nodeValue;
118 elif g.localName=="Address":
119 address=g.childNodes[0].nodeValue;
120 elif g.localName=="Description":
122 description=g.childNodes[0].nodeValue;
123 elif g.localName=="Bitfield":
124 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
125 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
127 print "%-10s /* %-50s */" % (name, description);
128 elif name.lower() in lreg:
129 print "%-10s=0x%02x; /* %-50s */" % (
130 name,self.CCpeekdatabyte(eval(address)), description);
131 if bitfields!="": print bitfields.rstrip();
133 def RF_setfreq(self,frequency):
134 """Set the frequency in Hz."""
135 #FIXME CC1110 specific
136 #Some frequencies fail, probably and FSCAL thing.
139 freq=int(hz/396.728515625);
142 freq1=(freq&0xFF00)>>8;
143 freq2=(freq&0xFF0000)>>16;
145 self.pokebysym("FREQ2",freq2);
146 self.pokebysym("FREQ1",freq1);
147 self.pokebysym("FREQ0",freq0);
149 self.pokebysym("TEST1",0x31);
150 self.pokebysym("TEST0",0x09);
153 #self.pokebysym("FSCAL2" , 0x2A); #above mid
154 self.pokebysym("FSCAL2" , 0x0A); #beneath mid
156 #self.CC_RFST_CAL(); #SCAL
160 def RF_getfreq(self):
161 """Get the frequency in Hz."""
162 #FIXME CC1110 specific
164 #return (2400+self.peek(0x05))*10**6
165 #self.poke(0x05,chan);
167 #freq2=self.CCpeekdatabyte(0xdf09);
168 #freq1=self.CCpeekdatabyte(0xdf0a);
169 #freq0=self.CCpeekdatabyte(0xdf0b);
172 freq2=self.peekbysym("FREQ2");
173 freq1=self.peekbysym("FREQ1");
174 freq0=self.peekbysym("FREQ0");
175 freq=(freq2<<16)+(freq1<<8)+freq0;
179 hz=freq*396.728515625;
183 def RF_getchannel(self):
184 """Get the hex channel."""
185 #FIXME CC1110 specific
188 freq2=self.peekbysym("FREQ2");
189 freq1=self.peekbysym("FREQ1");
190 freq0=self.peekbysym("FREQ0");
191 freq=(freq2<<16)+(freq1<<8)+freq0;
198 lastshellcode="none";
199 def shellcodefile(self,filename,wait=1, alwaysreload=0):
200 """Run a fragment of shellcode by name."""
201 #FIXME: should identify chip model number, use shellcode for that chip.
203 if self.lastshellcode!=filename or alwaysreload>0:
204 self.lastshellcode=filename;
206 file=file.replace("GoodFETCC.pyc","GoodFETCC.py");
207 #TODO make this generic
208 path=file.replace("GoodFETCC.py","shellcode/chipcon/cc1110/");
209 filename=path+filename;
212 h=IntelHex(filename);
213 for i in h._buf.keys():
214 self.CCpokedatabyte(i,h[i]);
216 self.CCdebuginstr([0x02, 0xf0, 0x00]); #ljmp 0xF000
218 while wait>0 and (0==self.CCstatus()&0x20):
220 #print "Waiting for shell code to return.";
223 return self.CCstatus()&0x20;
224 def shellcode(self,code,wait=1):
225 """Copy a block of code into RAM and execute it."""
229 self.pokebyte(0xF000+i,byte);
231 #print "Code loaded, executing."
232 self.CCdebuginstr([0x02, 0xf0, 0x00]); #ljmp 0xF000
234 while wait>0 and (0==self.CCstatus()&0x20):
237 #print "Waiting for shell code to return.";
239 def CC1110_crystal(self):
240 """Start the main crystal of the CC1110 oscillating, needed for radio use."""
241 code=[0x53, 0xBE, 0xFB, #anl SLEEP, #0xFB
243 0xE5, 0xBE, #mov a,SLEEP
244 0x30, 0xE6, 0xFB, #jnb acc.6, back
245 0x53, 0xc6, 0xB8, #anl CLKCON, #0xB8
247 0xE5, 0xC6, #mov a,CLKCON
248 0x20, 0xE6, 0xFB, #jb acc.6, two
249 0x43, 0xBE, 0x04, #orl SLEEP, #0x04
252 self.shellcode(code);
254 #Slower to load, but produced from C.
255 #self.shellcodefile("crystal.ihx");
258 """Move the radio to its idle state."""
262 #Chipcon RF strobes. CC1110 specific
267 def CC_RFST_IDLE(self):
268 """Switch the radio to idle mode, clearing overflows and errors."""
269 self.CC_RFST(self.RFST_IDLE);
270 def CC_RFST_TX(self):
271 """Switch the radio to TX mode."""
272 self.CC_RFST(self.RFST_TX);
273 def CC_RFST_RX(self):
274 """Switch the radio to RX mode."""
275 self.CC_RFST(self.RFST_RX);
276 def CC_RFST_CAL(self):
277 """Calibrate strobe the radio."""
278 self.CC_RFST(self.RFST_CAL);
279 def CC_RFST(self,state=RFST_IDLE):
281 self.pokebyte(RFST,state); #Return to idle state.
283 def config_dash7(self,band="lf"):
284 #These settings came from the OpenTag project's GIT repo on 18 Dec, 2010.
285 #Waiting for official confirmation of the accuracy.
287 self.pokebysym("FSCTRL1" , 0x08) # Frequency synthesizer control.
288 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
290 #Don't change these while the radio is active.
291 self.pokebysym("FSCAL3" , 0xEA) # Frequency synthesizer calibration.
292 self.pokebysym("FSCAL2" , 0x2A) # Frequency synthesizer calibration.
293 self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
294 self.pokebysym("FSCAL0" , 0x1F) # Frequency synthesizer calibration.
296 if band=="ismeu" or band=="eu":
297 print "There is no official eu band for dash7."
298 self.pokebysym("FREQ2" , 0x21) # Frequency control word, high byte.
299 self.pokebysym("FREQ1" , 0x71) # Frequency control word, middle byte.
300 self.pokebysym("FREQ0" , 0x7a) # Frequency control word, low byte.
301 elif band=="ismus" or band=="us":
302 print "There is no official us band for dash7."
303 self.pokebysym("FREQ2" , 0x22) # Frequency control word, high byte.
304 self.pokebysym("FREQ1" , 0xB1) # Frequency control word, middle byte.
305 self.pokebysym("FREQ0" , 0x3B) # Frequency control word, low byte.
306 elif band=="ismlf" or band=="lf":
307 # 433.9198 MHz, same as Simpliciti.
308 self.pokebysym("FREQ2" , 0x10) # Frequency control word, high byte.
309 self.pokebysym("FREQ1" , 0xB0) # Frequency control word, middle byte.
310 self.pokebysym("FREQ0" , 0x71) # Frequency control word, low byte.
314 #Got a frequency, not a band.
315 self.RF_setfreq(eval(band));
316 self.pokebysym("MDMCFG4" , 0x8B) # 62.5 kbps w/ 200 kHz filter
317 self.pokebysym("MDMCFG3" , 0x3B)
318 self.pokebysym("MDMCFG2" , 0x11)
319 self.pokebysym("MDMCFG1" , 0x02)
320 self.pokebysym("MDMCFG0" , 0x53)
321 self.pokebysym("CHANNR" , 0x00) # Channel zero.
322 self.pokebysym("DEVIATN" , 0x50) # 50 kHz deviation
324 self.pokebysym("FREND1" , 0xB6) # Front end RX configuration.
325 self.pokebysym("FREND0" , 0x10) # Front end RX configuration.
326 self.pokebysym("MCSM2" , 0x1E)
327 self.pokebysym("MCSM1" , 0x3F)
328 self.pokebysym("MCSM0" , 0x30)
329 self.pokebysym("FOCCFG" , 0x1D) # Frequency Offset Compensation Configuration.
330 self.pokebysym("BSCFG" , 0x1E) # 6.25% data error rate
332 self.pokebysym("AGCCTRL2" , 0xC7) # AGC control.
333 self.pokebysym("AGCCTRL1" , 0x00) # AGC control.
334 self.pokebysym("AGCCTRL0" , 0xB2) # AGC control.
336 self.pokebysym("TEST2" , 0x81) # Various test settings.
337 self.pokebysym("TEST1" , 0x35) # Various test settings.
338 self.pokebysym("TEST0" , 0x09) # Various test settings.
339 self.pokebysym("PA_TABLE0", 0xc0) # Max output power.
340 self.pokebysym("PKTCTRL1" , 0x04) # Packet automation control, w/ lqi
341 #self.pokebysym("PKTCTRL1" , 0x00) # Packet automation control. w/o lqi
342 self.pokebysym("PKTCTRL0" , 0x05) # Packet automation control, w/ checksum.
343 #self.pokebysym("PKTCTRL0" , 0x00) # Packet automation control, w/o checksum, fixed length
344 self.pokebysym("ADDR" , 0x01) # Device address.
345 self.pokebysym("PKTLEN" , 0xFF) # Packet length.
348 self.pokebysym("SYNC1",0x83);
349 self.pokebysym("SYNC0",0xFE);
351 def config_iclicker(self,band="lf"):
352 #Mike Ossmann figured most of this out, with help from neighbors.
354 self.pokebysym("FSCTRL1" , 0x06) # Frequency synthesizer control.
355 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
357 #Don't change these while the radio is active.
358 self.pokebysym("FSCAL3" , 0xE9)
359 self.pokebysym("FSCAL2" , 0x2A)
360 self.pokebysym("FSCAL1" , 0x00)
361 self.pokebysym("FSCAL0" , 0x1F)
363 if band=="ismeu" or band=="eu":
364 print "The EU band is unknown.";
365 elif band=="ismus" or band=="us":
367 self.pokebysym("FREQ2" , 0x22) # Frequency control word, high byte.
368 self.pokebysym("FREQ1" , 0xD3) # Frequency control word, middle byte.
369 self.pokebysym("FREQ0" , 0xAC) # Frequency control word, low byte.
370 elif band=="ismlf" or band=="lf":
371 print "There is no LF version of the iclicker."
375 #Got a frequency, not a band.
376 self.RF_setfreq(eval(band));
377 # 812.5kHz bandwidth, 152.34 kbaud
378 self.pokebysym("MDMCFG4" , 0x1C)
379 self.pokebysym("MDMCFG3" , 0x80)
380 # no FEC, 2 byte preamble, 250kHz chan spacing
383 #self.pokebysym("MDMCFG2" , 0x01)
385 self.pokebysym("MDMCFG2" , 0x02)
387 self.pokebysym("MDMCFG1" , 0x03)
388 self.pokebysym("MDMCFG0" , 0x3b)
390 self.pokebysym("CHANNR" , 0x2e) # Channel zero.
392 #self.pokebysym("DEVIATN" , 0x71) # 118.5
393 self.pokebysym("DEVIATN" , 0x72) # 253.9 kHz deviation
395 self.pokebysym("FREND1" , 0x56) # Front end RX configuration.
396 self.pokebysym("FREND0" , 0x10) # Front end RX configuration.
397 self.pokebysym("MCSM2" , 0x07)
398 self.pokebysym("MCSM1" , 0x30) #Auto freq. cal.
399 self.pokebysym("MCSM0" , 0x14)
401 self.pokebysym("TEST2" , 0x88) #
402 self.pokebysym("TEST1" , 0x31) #
403 self.pokebysym("TEST0" , 0x09) # High VCO (Upper band.)
404 self.pokebysym("PA_TABLE0", 0xC0) # Max output power.
405 self.pokebysym("PKTCTRL1" , 0x45) # Preamble qualidy 2*4=6, adr check, status
406 self.pokebysym("PKTCTRL0" , 0x00) # No whitening, CR, fixed len.
408 self.pokebysym("PKTLEN" , 0x09) # Packet length.
410 self.pokebysym("SYNC1",0xB0);
411 self.pokebysym("SYNC0",0xB0);
412 self.pokebysym("ADDR", 0xB0);
414 def config_ook(self,band="none"):
415 self.pokebysym("FSCTRL1" , 0x0C) #08 # Frequency synthesizer control.
416 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
418 #Don't change these while the radio is active.
419 self.pokebysym("FSCAL3" , 0xEA) # Frequency synthesizer calibration.
420 self.pokebysym("FSCAL2" , 0x2A) # Frequency synthesizer calibration.
421 self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
422 self.pokebysym("FSCAL0" , 0x1F) # Frequency synthesizer calibration.
424 if band=="ismeu" or band=="eu":
425 self.pokebysym("FREQ2" , 0x21) # Frequency control word, high byte.
426 self.pokebysym("FREQ1" , 0x71) # Frequency control word, middle byte.
427 self.pokebysym("FREQ0" , 0x7a) # Frequency control word, low byte.
428 elif band=="ismus" or band=="us":
429 self.pokebysym("FREQ2" , 0x22) # Frequency control word, high byte.
430 self.pokebysym("FREQ1" , 0xB1) # Frequency control word, middle byte.
431 self.pokebysym("FREQ0" , 0x3B) # Frequency control word, low byte.
432 elif band=="ismlf" or band=="lf":
433 self.pokebysym("FREQ2" , 0x0C) # Frequency control word, high byte.
434 self.pokebysym("FREQ1" , 0x1D) # Frequency control word, middle byte.
435 self.pokebysym("FREQ0" , 0x89) # Frequency control word, low byte.
439 #Got a frequency, not a band.
440 self.RF_setfreq(eval(band));
444 #self.pokebysym("MDMCFG4" , 0x85)
445 #self.pokebysym("MDMCFG3" , 0x83)
447 #self.pokebysym("MDMCFG4" , 0xf4)
448 #self.pokebysym("MDMCFG3" , 0x43)
450 #self.pokebysym("MDMCFG4" , 0xf6)
451 #self.pokebysym("MDMCFG3" , 0x83)
454 #print "Warning: Default to 4.8kbaud.";
455 #self.pokebysym("MDMCFG4" , 0xf7)
456 #self.pokebysym("MDMCFG3" , 0x83)
458 #print "Warning: Default to 9.6kbaud.";
461 self.pokebysym("MDMCFG4" , 0xf8)
462 self.pokebysym("MDMCFG3" , 0x83)
463 self.pokebysym("MDMCFG2" , 0x34) # OOK, carrier-sense, no-manchester
465 #Kind aright for keeloq
466 print "Warning: Guessing baud rate.";
467 #self.pokebysym("MDMCFG4" , 0xf6)
468 #self.pokebysym("MDMCFG3" , 0x93)
469 #self.pokebysym("MDMCFG2" , 0x3C) # OOK, carrier-sense, manchester
471 self.pokebysym("MDMCFG1" , 0x00) # Modem configuration.
472 self.pokebysym("MDMCFG0" , 0xF8) # Modem configuration.
473 self.pokebysym("CHANNR" , 0x00) # Channel number.
475 self.pokebysym("FREND1" , 0x56) # Front end RX configuration.
476 self.pokebysym("FREND0" , 0x11) # Front end RX configuration.
477 self.pokebysym("MCSM0" , 0x18) # Main Radio Control State Machine configuration.
478 #self.pokebysym("FOCCFG" , 0x1D) # Frequency Offset Compensation Configuration.
479 #self.pokebysym("BSCFG" , 0x1C) # Bit synchronization Configuration.
481 #self.pokebysym("AGCCTRL2" , 0xC7) # AGC control.
482 #self.pokebysym("AGCCTRL1" , 0x00) # AGC control.
483 #self.pokebysym("AGCCTRL0" , 0xB2) # AGC control.
485 self.pokebysym("TEST2" , 0x81) # Various test settings.
486 self.pokebysym("TEST1" , 0x35) # Various test settings.
487 self.pokebysym("TEST0" , 0x0B) # Various test settings.
488 self.pokebysym("PA_TABLE0", 0xc2) # Max output power.
489 self.pokebysym("PKTCTRL1" , 0x04) # Packet automation control, w/ lqi
490 #self.pokebysym("PKTCTRL1" , 0x00) # Packet automation control. w/o lqi
491 #self.pokebysym("PKTCTRL0" , 0x05) # Packet automation control, w/ checksum.
492 self.pokebysym("PKTCTRL0" , 0x00) # Packet automation control, w/o checksum, fixed length
493 self.pokebysym("ADDR" , 0x01) # Device address.
494 self.pokebysym("PKTLEN" , 0xFF) # Packet length.
496 self.pokebysym("SYNC1",0xD3);
497 self.pokebysym("SYNC0",0x91);
499 def config_simpliciti(self,band="none"):
500 self.pokebysym("FSCTRL1" , 0x0C) #08 # Frequency synthesizer control.
501 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
503 #Don't change these while the radio is active.
504 self.pokebysym("FSCAL3" , 0xEA) # Frequency synthesizer calibration.
505 self.pokebysym("FSCAL2" , 0x2A) # Frequency synthesizer calibration.
506 self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
507 self.pokebysym("FSCAL0" , 0x1F) # Frequency synthesizer calibration.
509 if band=="ismeu" or band=="eu":
510 self.pokebysym("FREQ2" , 0x21) # Frequency control word, high byte.
511 self.pokebysym("FREQ1" , 0x71) # Frequency control word, middle byte.
512 self.pokebysym("FREQ0" , 0x7a) # Frequency control word, low byte.
513 elif band=="ismus" or band=="us":
514 self.pokebysym("FREQ2" , 0x22) # Frequency control word, high byte.
515 self.pokebysym("FREQ1" , 0xB1) # Frequency control word, middle byte.
516 self.pokebysym("FREQ0" , 0x3B) # Frequency control word, low byte.
517 elif band=="ismlf" or band=="lf":
518 self.pokebysym("FREQ2" , 0x10) # Frequency control word, high byte.
519 self.pokebysym("FREQ1" , 0xB0) # Frequency control word, middle byte.
520 self.pokebysym("FREQ0" , 0x71) # Frequency control word, low byte.
524 #Got a frequency, not a band.
525 self.RF_setfreq(eval(band));
526 self.pokebysym("MDMCFG4" , 0x7B) # Modem configuration.
527 self.pokebysym("MDMCFG3" , 0x83) # Modem configuration.
528 self.pokebysym("MDMCFG2" , 0x13) # Modem configuration.
529 self.pokebysym("MDMCFG1" , 0x22) # Modem configuration.
530 self.pokebysym("MDMCFG0" , 0xF8) # Modem configuration.
531 if band=="ismus" or band=="us":
532 self.pokebysym("CHANNR" , 20) # Channel number.
534 self.pokebysym("CHANNR" , 0x00) # Channel number.
535 self.pokebysym("DEVIATN" , 0x42) # Modem deviation setting (when FSK modulation is enabled).
537 self.pokebysym("FREND1" , 0xB6) # Front end RX configuration.
538 self.pokebysym("FREND0" , 0x10) # Front end RX configuration.
539 self.pokebysym("MCSM0" , 0x18) # Main Radio Control State Machine configuration.
540 self.pokebysym("FOCCFG" , 0x1D) # Frequency Offset Compensation Configuration.
541 self.pokebysym("BSCFG" , 0x1C) # Bit synchronization Configuration.
543 self.pokebysym("AGCCTRL2" , 0xC7) # AGC control.
544 self.pokebysym("AGCCTRL1" , 0x00) # AGC control.
545 self.pokebysym("AGCCTRL0" , 0xB2) # AGC control.
547 self.pokebysym("TEST2" , 0x81) # Various test settings.
548 self.pokebysym("TEST1" , 0x35) # Various test settings.
549 self.pokebysym("TEST0" , 0x09) # Various test settings.
550 self.pokebysym("PA_TABLE0", 0xc0) # Max output power.
551 self.pokebysym("PKTCTRL1" , 0x04) # Packet automation control, w/ lqi
552 #self.pokebysym("PKTCTRL1" , 0x00) # Packet automation control. w/o lqi
553 self.pokebysym("PKTCTRL0" , 0x05) # Packet automation control, w/ checksum.
554 #self.pokebysym("PKTCTRL0" , 0x00) # Packet automation control, w/o checksum, fixed length
555 self.pokebysym("ADDR" , 0x01) # Device address.
556 self.pokebysym("PKTLEN" , 0xFF) # Packet length.
558 self.pokebysym("SYNC1",0xD3);
559 self.pokebysym("SYNC0",0x91);
561 def RF_carrier(self):
562 """Hold a carrier wave on the present frequency."""
564 self.CC1110_crystal(); #FIXME, '1110 specific.
570 self.config_simpliciti();
572 #Don't change these while the radio is active.
573 #self.pokebysym("FSCAL3" , 0xA9) # Frequency synthesizer calibration.
574 #self.pokebysym("FSCAL2" , 0x0A) # Frequency synthesizer calibration.
575 #self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
576 #self.pokebysym("FSCAL0" , 0x11) # Frequency synthesizer calibration.
579 #self.pokebysym("PA_TABLE0", 0xFF) # PA output power setting.
581 #This is what drops to OOK.
582 #Comment to keep GFSK, might be better at jamming.
583 self.pokebysym("MDMCFG4" , 0x86) # Modem configuration.
584 self.pokebysym("MDMCFG3" , 0x83) # Modem configuration.
585 self.pokebysym("MDMCFG2" , 0x30) # Modem configuration.
586 self.pokebysym("MDMCFG1" , 0x22) # Modem configuration.
587 self.pokebysym("MDMCFG0" , 0xF8) # Modem configuration.
589 self.pokebysym("SYNC1",0xAA);
590 self.pokebysym("SYNC0",0xAA);
592 #while ((MARCSTATE & MARCSTATE_MARC_STATE) != MARC_STATE_TX);
595 while((state!=0x13)):
596 self.pokebyte(RFST,0x03); #RFST=RFST_STX
598 state=self.peekbysym("MARCSTATE")&0x1F;
599 #print "state=%02x" % state;
600 print "Holding a carrier on %f MHz." % (self.RF_getfreq()/10**6);
604 def RF_getsmac(self):
605 """Return the source MAC address."""
607 #Register 0A is RX_ADDR_P0, five bytes.
608 mac=self.peekbysym("ADDR");
610 def RF_setsmac(self,mac):
611 """Set the source MAC address."""
612 self.pokebysym("ADDR",mac);
614 def RF_gettmac(self):
615 """Return the target MAC address."""
617 def RF_settmac(self,mac):
618 """Set the target MAC address."""
620 def RF_rxpacket(self):
621 """Get a packet from the radio. Returns None if none is waiting."""
622 self.shellcodefile("rxpacket.ihx");
623 len=self.peek8(0xFE00,"xdata");
624 return self.peekblock(0xFE00,len+3,"data");
625 def RF_txpacket(self,packet):
626 """Transmit a packet. Untested."""
628 self.pokeblock(0xFE00,packet,"data");
629 self.shellcodefile("txpacket.ihx");
631 def RF_txrxpacket(self,packet):
632 """Transmit a packet. Untested."""
634 self.pokeblock(0xFE00,packet,"data");
635 self.shellcodefile("txrxpacket.ihx");
636 len=self.peek8(0xFE00,"xdata");
637 return self.peekblock(0xFE00,len+3,"data");
639 def RF_getrssi(self):
640 """Returns the received signal strenght, with a weird offset."""
642 rssireg=self.symbols.get("RSSI");
643 return self.CCpeekdatabyte(rssireg)^0x80;
645 if self.verbose>0: print "RSSI reg doesn't exist.";
647 #RSSI doesn't exist on some 2.4GHz devices. Maybe RSSIL and RSSIH?
648 rssilreg=self.symbols.get("RSSIL");
649 rssil=self.CCpeekdatabyte(rssilreg);
650 rssihreg=self.symbols.get("RSSIL");
651 rssih=self.CCpeekdatabyte(rssihreg);
652 return (rssih<<8)|rssil;
654 if self.verbose>0: print "RSSIL/RSSIH regs don't exist.";
658 def SRF_loadsymbols(self):
659 ident=self.CCident();
660 chip=self.CCversions.get(ident&0xFF00);
661 dom=self.SRF_chipdom(chip,"register_definition.xml");
662 for e in dom.getElementsByTagName("registerdefinition"):
663 for f in e.childNodes:
664 if f.localName=="Register":
669 for g in f.childNodes:
670 if g.localName=="Name":
671 name=g.childNodes[0].nodeValue;
672 elif g.localName=="Address":
673 address=g.childNodes[0].nodeValue;
674 elif g.localName=="Description":
676 description=g.childNodes[0].nodeValue;
677 elif g.localName=="Bitfield":
678 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
679 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
680 self.symbols.define(eval(address),name,description,"data");
686 self.writecmd(self.APP,0x86,0,self.data);
689 def CCreleasecpu(self):
690 """Resume the CPU."""
691 self.writecmd(self.APP,0x87,0,self.data);
695 #print "Status: %s" % self.CCstatusstr();
697 #Grab ident three times, should be equal.
698 ident1=self.CCident();
699 ident2=self.CCident();
700 ident3=self.CCident();
701 if(ident1!=ident2 or ident2!=ident3):
702 print "Error, repeated ident attempts unequal."
703 print "%04x, %04x, %04x" % (ident1, ident2, ident3);
705 #Single step, printing PC.
706 print "Tracing execution at startup."
707 for i in range(1,15):
709 byte=self.CCpeekcodebyte(i);
710 #print "PC=%04x, %02x" % (pc, byte);
713 print "Verifying that debugging a NOP doesn't affect the PC."
714 for i in range(1,15):
716 self.CCdebuginstr([0x00]);
717 if(pc!=self.CCgetPC()):
718 print "ERROR: PC changed during CCdebuginstr([NOP])!";
720 print "Checking pokes to XRAM."
721 for i in range(self.execbuf,self.execbuf+0x20):
722 self.CCpokedatabyte(i,0xde);
723 if(self.CCpeekdatabyte(i)!=0xde):
724 print "Error in XDATA at 0x%04x" % i;
726 #print "Status: %s." % self.CCstatusstr();
732 """Move the FET into the Chipcon 8051 application."""
733 #print "Initializing Chipcon.";
734 self.writecmd(self.APP,0x10,0,self.data);
735 def CCrd_config(self):
736 """Read the config register of a Chipcon."""
737 self.writecmd(self.APP,0x82,0,self.data);
738 return ord(self.data[0]);
739 def CCwr_config(self,config):
740 """Write the config register of a Chipcon."""
741 self.writecmd(self.APP,0x81,1,[config&0xFF]);
742 def CClockchip(self):
743 """Set the flash lock bit in info mem."""
744 self.writecmd(self.APP, 0x9A, 0, None);
746 """Set the flash lock bit in info mem."""
750 CCversions={0x0100:"CC1110",
756 0xA500:"CC2530", #page 57 of SWRU191B
762 CCexecbuf= {0x0100:0xF000,
768 0xA500:0x0000, #CC2530
772 0xFF00:None} #missing
773 CCpagesizes={0x01: 1024, #"CC1110",
774 0x11: 1024, #"CC1111",
775 0x85: 2048, #"CC2430",
776 0x89: 2048, #"CC2431",
777 0x81: 1024, #"CC2510",
778 0x91: 1024, #"CC2511",
779 0xA5: 2048, #"CC2530", #page 57 of SWRU191B
780 0xB5: 2048, #"CC2531",
781 0x95: 2048, #"CC2533",
782 0x8D: 2048, #"CC2540",
784 def infostring(self):
785 return self.CCidentstr();
786 def CCidentstr(self):
787 ident=self.CCident();
788 chip=self.CCversions.get(ident&0xFF00);
789 execbuf=self.CCexecbuf.get(ident&0xFF00);
790 pagesize=self.CCpagesizes.get(ident>0xFF);
791 self.execbuf=execbuf;
794 return "%s/r%0.4x/ps0x%0.4x" % (chip, ident, pagesize);
796 return "%04x" % ident;
798 """Get a chipcon's ID."""
799 self.writecmd(self.APP,0x8B,0,None);
800 chip=ord(self.data[0]);
801 rev=ord(self.data[1]);
802 return (chip<<8)+rev;
803 def CCpagesize(self):
804 """Get a chipcon's ID."""
805 self.writecmd(self.APP,0x8B,0,None);
806 chip=ord(self.data[0]);
807 size=self.CCpagesizes.get(chip);
809 print "ERROR: Pagesize undefined.";
810 print "chip=%0.4x" %chip;
815 return self.CCgetPC();
817 """Get a chipcon's PC."""
818 self.writecmd(self.APP,0x83,0,None);
819 hi=ord(self.data[0]);
820 lo=ord(self.data[1]);
822 def CCcmd(self,phrase):
823 self.writecmd(self.APP,0x00,len(phrase),phrase);
824 val=ord(self.data[0]);
825 print "Got %02x" % val;
827 def CCdebuginstr(self,instr):
828 self.writecmd(self.APP,0x88,len(instr),instr);
829 return ord(self.data[0]);
830 #def peekblock(self,adr,length,memory="vn"):
831 # """Return a block of data, broken"""
832 # data=[adr&0xff, (adr&0xff00)>>8,
833 # length&0xFF,(length&0xFF00)>>8];
834 # self.writecmd(self.APP,0x91,4,data);
835 # return [ord(x) for x in self.data]
836 def peek8(self,address, memory="code"):
837 if(memory=="code" or memory=="flash" or memory=="vn"):
838 return self.CCpeekcodebyte(address);
839 elif(memory=="data" or memory=="xdata" or memory=="ram"):
840 return self.CCpeekdatabyte(address);
841 elif(memory=="idata" or memory=="iram"):
842 return self.CCpeekirambyte(address);
843 print "%s is an unknown memory." % memory;
845 def CCpeekcodebyte(self,adr):
846 """Read the contents of code memory at an address."""
847 self.data=[adr&0xff, (adr&0xff00)>>8];
848 self.writecmd(self.APP,0x90,2,self.data);
849 return ord(self.data[0]);
850 def CCpeekdatabyte(self,adr):
851 """Read the contents of data memory at an address."""
852 self.data=[adr&0xff, (adr&0xff00)>>8];
853 self.writecmd(self.APP,0x91, 2, self.data);
854 return ord(self.data[0]);
855 def CCpeekirambyte(self,adr):
856 """Read the contents of IRAM at an address."""
857 self.data=[adr&0xff];
858 self.writecmd(self.APP,0x02, 1, self.data);
859 return ord(self.data[0]);
860 def CCpeekiramword(self,adr):
861 """Read the little-endian contents of IRAM at an address."""
862 return self.CCpeekirambyte(adr)+(
863 self.CCpeekirambyte(adr+1)<<8);
864 def CCpokeiramword(self,adr,val):
865 self.CCpokeirambyte(adr,val&0xff);
866 self.CCpokeirambyte(adr+1,(val>>8)&0xff);
867 def CCpokeirambyte(self,adr,val):
868 """Write the contents of IRAM at an address."""
869 self.data=[adr&0xff, val&0xff];
870 self.writecmd(self.APP,0x02, 2, self.data);
871 return ord(self.data[0]);
872 def pokebyte(self,adr,val,mem="xdata"):
873 self.CCpokedatabyte(adr,val);
874 def CCpokedatabyte(self,adr,val):
875 """Write a byte to data memory."""
876 self.data=[adr&0xff, (adr&0xff00)>>8, val];
877 self.writecmd(self.APP, 0x92, 3, self.data);
878 return ord(self.data[0]);
879 def CCchiperase(self):
880 """Erase all of the target's memory."""
881 self.writecmd(self.APP,0x80,0,None);
883 """Erase all of the target's memory."""
888 """Check the status."""
889 self.writecmd(self.APP,0x84,0,None);
890 return ord(self.data[0])
892 CCstatusbits={0x80 : "erase_busy",
896 0x08 : "halt_status",
901 CCconfigbits={0x20 : "soft_power_mode", #new for CC2530
904 0x02 : "timer_suspend",
905 0x01 : "sel_flash_info_page" #stricken from CC2530
909 """Check the status as a string."""
910 status=self.CCstatus();
915 str="%s %s" %(self.CCstatusbits[i],str);
919 """Start debugging."""
921 #while ident==0xFFFF or ident==0x0000:
923 self.writecmd(self.APP,0x20,0,self.data);
924 identa=self.CCident();
927 ident=self.CCident();
928 #Get SmartRF Studio regs if they exist.
930 #print "Status: %s" % self.status();
932 """Stop debugging."""
933 self.writecmd(self.APP,0x21,0,self.data);
934 def CCstep_instr(self):
935 """Step one instruction."""
936 self.writecmd(self.APP,0x89,0,self.data);
937 def CCeraseflashbuffer(self):
938 """Erase the 2kB flash buffer"""
939 self.writecmd(self.APP,0x99);
940 def CCflashpage(self,adr):
941 """Flash 2kB a page of flash from 0xF000 in XDATA"""
946 print "Flashing buffer to 0x%06x" % adr;
947 self.writecmd(self.APP,0x95,4,data);
949 def setsecret(self,value):
950 """Set a secret word for later retreival. Used by glitcher."""
952 pagelen = self.CCpagesize(); #Varies by chip.
953 print "page=%04x, pagelen=%04x" % (page,pagelen);
955 self.CCeraseflashbuffer();
956 print "Setting secret to %x" % value;
957 self.CCpokedatabyte(0xF000,value);
958 self.CCpokedatabyte(0xF800,value);
959 print "Setting secret to %x==%x" % (value,
960 self.CCpeekdatabyte(0xf000));
962 print "code[0]=%x" % self.CCpeekcodebyte(0);
964 """Get a secret word. Used by glitcher."""
965 secret=self.CCpeekcodebyte(0);
966 #print "Got secret %02x" % secret;
969 #FIXME: This is CC1110-specific and duplicates functionality of
970 # SmartRF7 integration.
1088 def getSPR(self,args=[]):
1089 """Get special function registers."""
1090 print "Special Function Registers:"
1093 print " %-8s : 0x%0.2x"%(e,self.CCpeekcodebyte(self.CCspecfuncregs[e]))
1095 for e in self.CCspecfuncregs.keys():
1096 print " %-8s : 0x%0.2x"%(e,self.CCpeekcodebyte(self.CCspecfuncregs[e]))
1098 def dump(self,file,start=0,stop=0xffff):
1099 """Dump an intel hex file from code memory."""
1100 print "Dumping code from %04x to %04x as %s." % (start,stop,file);
1104 h[i]=self.CCpeekcodebyte(i);
1106 print "Dumped %04x."%i;
1107 h.write_hex_file(file); #buffer to disk.
1109 h.write_hex_file(file);
1111 def flash(self,file):
1112 """Flash an intel hex file to code memory."""
1113 print "Flashing %s" % file;
1117 pagelen = self.CCpagesize(); #Varies by chip.
1119 #print "page=%04x, pagelen=%04x" % (page,pagelen);
1123 #Wipe the RAM buffer for the next flash page.
1124 self.CCeraseflashbuffer();
1125 for i in h._buf.keys():
1126 while(i>=page+pagelen):
1128 self.CCflashpage(page);
1129 #client.CCeraseflashbuffer();
1131 print "Flashed page at %06x" % page
1134 #Place byte into buffer.
1135 self.CCpokedatabyte(0xF000+i-page,
1139 print "Buffering %04x toward %06x" % (i,page);
1141 self.CCflashpage(page);
1142 print "Flashed final page at %06x" % page;