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.";
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]);
217 self.CCdebuginstr([0x02, 0xf0, 0x00]); #ljmp 0xF000
219 while wait>0 and (0==self.CCstatus()&0x20):
222 #print "Waiting for shell code to return.";
225 return self.CCstatus()&0x20;
226 def shellcode(self,code,wait=1):
227 """Copy a block of code into RAM and execute it."""
231 self.pokebyte(0xF000+i,byte);
233 #print "Code loaded, executing."
234 self.CCdebuginstr([0x02, 0xf0, 0x00]); #ljmp 0xF000
236 while wait>0 and (0==self.CCstatus()&0x20):
239 #print "Waiting for shell code to return.";
241 def CC1110_crystal(self):
242 """Start the main crystal of the CC1110 oscillating, needed for radio use."""
243 code=[0x53, 0xBE, 0xFB, #anl SLEEP, #0xFB
245 0xE5, 0xBE, #mov a,SLEEP
246 0x30, 0xE6, 0xFB, #jnb acc.6, back
247 0x53, 0xc6, 0xB8, #anl CLKCON, #0xB8
249 0xE5, 0xC6, #mov a,CLKCON
250 0x20, 0xE6, 0xFB, #jb acc.6, two
251 0x43, 0xBE, 0x04, #orl SLEEP, #0x04
254 self.shellcode(code);
256 #Slower to load, but produced from C.
257 #self.shellcodefile("crystal.ihx");
260 """Move the radio to its idle state."""
264 #Chipcon RF strobes. CC1110 specific
269 def CC_RFST_IDLE(self):
270 """Switch the radio to idle mode, clearing overflows and errors."""
271 self.CC_RFST(self.RFST_IDLE);
272 def CC_RFST_TX(self):
273 """Switch the radio to TX mode."""
274 self.CC_RFST(self.RFST_TX);
275 def CC_RFST_RX(self):
276 """Switch the radio to RX mode."""
277 self.CC_RFST(self.RFST_RX);
278 def CC_RFST_CAL(self):
279 """Calibrate strobe the radio."""
280 self.CC_RFST(self.RFST_CAL);
281 def CC_RFST(self,state=RFST_IDLE):
283 self.pokebyte(RFST,state); #Return to idle state.
285 def config_dash7(self,band="lf"):
286 #These settings came from the OpenTag project's GIT repo on 18 Dec, 2010.
287 #Waiting for official confirmation of the accuracy.
289 self.pokebysym("FSCTRL1" , 0x08) # Frequency synthesizer control.
290 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
292 #Don't change these while the radio is active.
293 self.pokebysym("FSCAL3" , 0xEA) # Frequency synthesizer calibration.
294 self.pokebysym("FSCAL2" , 0x2A) # Frequency synthesizer calibration.
295 self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
296 self.pokebysym("FSCAL0" , 0x1F) # Frequency synthesizer calibration.
298 if band=="ismeu" or band=="eu":
299 print "There is no official eu band for dash7."
300 self.pokebysym("FREQ2" , 0x21) # Frequency control word, high byte.
301 self.pokebysym("FREQ1" , 0x71) # Frequency control word, middle byte.
302 self.pokebysym("FREQ0" , 0x7a) # Frequency control word, low byte.
303 elif band=="ismus" or band=="us":
304 print "There is no official us band for dash7."
305 self.pokebysym("FREQ2" , 0x22) # Frequency control word, high byte.
306 self.pokebysym("FREQ1" , 0xB1) # Frequency control word, middle byte.
307 self.pokebysym("FREQ0" , 0x3B) # Frequency control word, low byte.
308 elif band=="ismlf" or band=="lf":
309 # 433.9198 MHz, same as Simpliciti.
310 self.pokebysym("FREQ2" , 0x10) # Frequency control word, high byte.
311 self.pokebysym("FREQ1" , 0xB0) # Frequency control word, middle byte.
312 self.pokebysym("FREQ0" , 0x71) # Frequency control word, low byte.
316 #Got a frequency, not a band.
317 self.RF_setfreq(eval(band));
318 self.pokebysym("MDMCFG4" , 0x8B) # 62.5 kbps w/ 200 kHz filter
319 self.pokebysym("MDMCFG3" , 0x3B)
320 self.pokebysym("MDMCFG2" , 0x11)
321 self.pokebysym("MDMCFG1" , 0x02)
322 self.pokebysym("MDMCFG0" , 0x53)
323 self.pokebysym("CHANNR" , 0x00) # Channel zero.
324 self.pokebysym("DEVIATN" , 0x50) # 50 kHz deviation
326 self.pokebysym("FREND1" , 0xB6) # Front end RX configuration.
327 self.pokebysym("FREND0" , 0x10) # Front end RX configuration.
328 self.pokebysym("MCSM2" , 0x1E)
329 self.pokebysym("MCSM1" , 0x3F)
330 self.pokebysym("MCSM0" , 0x30)
331 self.pokebysym("FOCCFG" , 0x1D) # Frequency Offset Compensation Configuration.
332 self.pokebysym("BSCFG" , 0x1E) # 6.25% data error rate
334 self.pokebysym("AGCCTRL2" , 0xC7) # AGC control.
335 self.pokebysym("AGCCTRL1" , 0x00) # AGC control.
336 self.pokebysym("AGCCTRL0" , 0xB2) # AGC control.
338 self.pokebysym("TEST2" , 0x81) # Various test settings.
339 self.pokebysym("TEST1" , 0x35) # Various test settings.
340 self.pokebysym("TEST0" , 0x09) # Various test settings.
341 self.pokebysym("PA_TABLE0", 0xc0) # Max output power.
342 self.pokebysym("PKTCTRL1" , 0x04) # Packet automation control, w/ lqi
343 #self.pokebysym("PKTCTRL1" , 0x00) # Packet automation control. w/o lqi
344 self.pokebysym("PKTCTRL0" , 0x05) # Packet automation control, w/ checksum.
345 #self.pokebysym("PKTCTRL0" , 0x00) # Packet automation control, w/o checksum, fixed length
346 self.pokebysym("ADDR" , 0x01) # Device address.
347 self.pokebysym("PKTLEN" , 0xFF) # Packet length.
350 self.pokebysym("SYNC1",0x83);
351 self.pokebysym("SYNC0",0xFE);
353 def config_iclicker(self,band="lf"):
354 #Mike Ossmann figured most of this out, with help from neighbors.
356 self.pokebysym("FSCTRL1" , 0x06) # Frequency synthesizer control.
357 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
359 #Don't change these while the radio is active.
360 self.pokebysym("FSCAL3" , 0xE9)
361 self.pokebysym("FSCAL2" , 0x2A)
362 self.pokebysym("FSCAL1" , 0x00)
363 self.pokebysym("FSCAL0" , 0x1F)
365 if band=="ismeu" or band=="eu":
366 print "The EU band is unknown.";
367 elif band=="ismus" or band=="us":
369 self.pokebysym("FREQ2" , 0x22) # Frequency control word, high byte.
370 self.pokebysym("FREQ1" , 0xD3) # Frequency control word, middle byte.
371 self.pokebysym("FREQ0" , 0xAC) # Frequency control word, low byte.
372 elif band=="ismlf" or band=="lf":
373 print "There is no LF version of the iclicker."
377 #Got a frequency, not a band.
378 self.RF_setfreq(eval(band));
379 # 812.5kHz bandwidth, 152.34 kbaud
380 self.pokebysym("MDMCFG4" , 0x1C)
381 self.pokebysym("MDMCFG3" , 0x80)
382 # no FEC, 2 byte preamble, 250kHz chan spacing
385 #self.pokebysym("MDMCFG2" , 0x01)
387 self.pokebysym("MDMCFG2" , 0x02)
389 self.pokebysym("MDMCFG1" , 0x03)
390 self.pokebysym("MDMCFG0" , 0x3b)
392 self.pokebysym("CHANNR" , 0x2e) # Channel zero.
394 #self.pokebysym("DEVIATN" , 0x71) # 118.5
395 self.pokebysym("DEVIATN" , 0x72) # 253.9 kHz deviation
397 self.pokebysym("FREND1" , 0x56) # Front end RX configuration.
398 self.pokebysym("FREND0" , 0x10) # Front end RX configuration.
399 self.pokebysym("MCSM2" , 0x07)
400 self.pokebysym("MCSM1" , 0x30) #Auto freq. cal.
401 self.pokebysym("MCSM0" , 0x14)
403 self.pokebysym("TEST2" , 0x88) #
404 self.pokebysym("TEST1" , 0x31) #
405 self.pokebysym("TEST0" , 0x09) # High VCO (Upper band.)
406 self.pokebysym("PA_TABLE0", 0xC0) # Max output power.
407 self.pokebysym("PKTCTRL1" , 0x45) # Preamble qualidy 2*4=6, adr check, status
408 self.pokebysym("PKTCTRL0" , 0x00) # No whitening, CR, fixed len.
410 self.pokebysym("PKTLEN" , 0x09) # Packet length.
412 self.pokebysym("SYNC1",0xB0);
413 self.pokebysym("SYNC0",0xB0);
414 self.pokebysym("ADDR", 0xB0);
416 def config_ook(self,band="none"):
417 self.pokebysym("FSCTRL1" , 0x0C) #08 # Frequency synthesizer control.
418 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
420 #Don't change these while the radio is active.
421 self.pokebysym("FSCAL3" , 0xEA) # Frequency synthesizer calibration.
422 self.pokebysym("FSCAL2" , 0x2A) # Frequency synthesizer calibration.
423 self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
424 self.pokebysym("FSCAL0" , 0x1F) # Frequency synthesizer calibration.
426 if band=="ismeu" or band=="eu":
427 self.pokebysym("FREQ2" , 0x21) # Frequency control word, high byte.
428 self.pokebysym("FREQ1" , 0x71) # Frequency control word, middle byte.
429 self.pokebysym("FREQ0" , 0x7a) # Frequency control word, low byte.
430 elif band=="ismus" or band=="us":
431 self.pokebysym("FREQ2" , 0x22) # Frequency control word, high byte.
432 self.pokebysym("FREQ1" , 0xB1) # Frequency control word, middle byte.
433 self.pokebysym("FREQ0" , 0x3B) # Frequency control word, low byte.
434 elif band=="ismlf" or band=="lf":
435 self.pokebysym("FREQ2" , 0x0C) # Frequency control word, high byte.
436 self.pokebysym("FREQ1" , 0x1D) # Frequency control word, middle byte.
437 self.pokebysym("FREQ0" , 0x89) # Frequency control word, low byte.
441 #Got a frequency, not a band.
442 self.RF_setfreq(eval(band));
446 #self.pokebysym("MDMCFG4" , 0x85)
447 #self.pokebysym("MDMCFG3" , 0x83)
449 #self.pokebysym("MDMCFG4" , 0xf4)
450 #self.pokebysym("MDMCFG3" , 0x43)
452 #self.pokebysym("MDMCFG4" , 0xf6)
453 #self.pokebysym("MDMCFG3" , 0x83)
456 #print "Warning: Default to 4.8kbaud.";
457 #self.pokebysym("MDMCFG4" , 0xf7)
458 #self.pokebysym("MDMCFG3" , 0x83)
460 #print "Warning: Default to 9.6kbaud.";
463 self.pokebysym("MDMCFG4" , 0xf8)
464 self.pokebysym("MDMCFG3" , 0x83)
465 self.pokebysym("MDMCFG2" , 0x34) # OOK, carrier-sense, no-manchester
467 #Kind aright for keeloq
468 print "Warning: Guessing baud rate.";
469 #self.pokebysym("MDMCFG4" , 0xf6)
470 #self.pokebysym("MDMCFG3" , 0x93)
471 #self.pokebysym("MDMCFG2" , 0x3C) # OOK, carrier-sense, manchester
473 self.pokebysym("MDMCFG1" , 0x00) # Modem configuration.
474 self.pokebysym("MDMCFG0" , 0xF8) # Modem configuration.
475 self.pokebysym("CHANNR" , 0x00) # Channel number.
477 self.pokebysym("FREND1" , 0x56) # Front end RX configuration.
478 self.pokebysym("FREND0" , 0x11) # Front end RX configuration.
479 self.pokebysym("MCSM0" , 0x18) # Main Radio Control State Machine configuration.
480 #self.pokebysym("FOCCFG" , 0x1D) # Frequency Offset Compensation Configuration.
481 #self.pokebysym("BSCFG" , 0x1C) # Bit synchronization Configuration.
483 #self.pokebysym("AGCCTRL2" , 0xC7) # AGC control.
484 #self.pokebysym("AGCCTRL1" , 0x00) # AGC control.
485 #self.pokebysym("AGCCTRL0" , 0xB2) # AGC control.
487 self.pokebysym("TEST2" , 0x81) # Various test settings.
488 self.pokebysym("TEST1" , 0x35) # Various test settings.
489 self.pokebysym("TEST0" , 0x0B) # Various test settings.
490 self.pokebysym("PA_TABLE0", 0xc2) # Max output power.
491 self.pokebysym("PKTCTRL1" , 0x04) # Packet automation control, w/ lqi
492 #self.pokebysym("PKTCTRL1" , 0x00) # Packet automation control. w/o lqi
493 #self.pokebysym("PKTCTRL0" , 0x05) # Packet automation control, w/ checksum.
494 self.pokebysym("PKTCTRL0" , 0x00) # Packet automation control, w/o checksum, fixed length
495 self.pokebysym("ADDR" , 0x01) # Device address.
496 self.pokebysym("PKTLEN" , 0xFF) # Packet length.
498 self.pokebysym("SYNC1",0xD3);
499 self.pokebysym("SYNC0",0x91);
501 def config_simpliciti(self,band="none"):
502 self.pokebysym("FSCTRL1" , 0x0C) #08 # Frequency synthesizer control.
503 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
505 #Don't change these while the radio is active.
506 self.pokebysym("FSCAL3" , 0xEA) # Frequency synthesizer calibration.
507 self.pokebysym("FSCAL2" , 0x2A) # Frequency synthesizer calibration.
508 self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
509 self.pokebysym("FSCAL0" , 0x1F) # Frequency synthesizer calibration.
511 if band=="ismeu" or band=="eu":
512 self.pokebysym("FREQ2" , 0x21) # Frequency control word, high byte.
513 self.pokebysym("FREQ1" , 0x71) # Frequency control word, middle byte.
514 self.pokebysym("FREQ0" , 0x7a) # Frequency control word, low byte.
515 elif band=="ismus" or band=="us":
516 self.pokebysym("FREQ2" , 0x22) # Frequency control word, high byte.
517 self.pokebysym("FREQ1" , 0xB1) # Frequency control word, middle byte.
518 self.pokebysym("FREQ0" , 0x3B) # Frequency control word, low byte.
519 elif band=="ismlf" or band=="lf":
520 self.pokebysym("FREQ2" , 0x10) # Frequency control word, high byte.
521 self.pokebysym("FREQ1" , 0xB0) # Frequency control word, middle byte.
522 self.pokebysym("FREQ0" , 0x71) # Frequency control word, low byte.
526 #Got a frequency, not a band.
527 self.RF_setfreq(eval(band));
528 self.pokebysym("MDMCFG4" , 0x7B) # Modem configuration.
529 self.pokebysym("MDMCFG3" , 0x83) # Modem configuration.
530 self.pokebysym("MDMCFG2" , 0x13) # Modem configuration.
531 self.pokebysym("MDMCFG1" , 0x22) # Modem configuration.
532 self.pokebysym("MDMCFG0" , 0xF8) # Modem configuration.
533 if band=="ismus" or band=="us":
534 self.pokebysym("CHANNR" , 20) # Channel number.
536 self.pokebysym("CHANNR" , 0x00) # Channel number.
537 self.pokebysym("DEVIATN" , 0x42) # Modem deviation setting (when FSK modulation is enabled).
539 self.pokebysym("FREND1" , 0xB6) # Front end RX configuration.
540 self.pokebysym("FREND0" , 0x10) # Front end RX configuration.
541 self.pokebysym("MCSM0" , 0x18) # Main Radio Control State Machine configuration.
542 self.pokebysym("FOCCFG" , 0x1D) # Frequency Offset Compensation Configuration.
543 self.pokebysym("BSCFG" , 0x1C) # Bit synchronization Configuration.
545 self.pokebysym("AGCCTRL2" , 0xC7) # AGC control.
546 self.pokebysym("AGCCTRL1" , 0x00) # AGC control.
547 self.pokebysym("AGCCTRL0" , 0xB2) # AGC control.
549 self.pokebysym("TEST2" , 0x81) # Various test settings.
550 self.pokebysym("TEST1" , 0x35) # Various test settings.
551 self.pokebysym("TEST0" , 0x09) # Various test settings.
552 self.pokebysym("PA_TABLE0", 0xc0) # Max output power.
553 self.pokebysym("PKTCTRL1" , 0x04) # Packet automation control, w/ lqi
554 #self.pokebysym("PKTCTRL1" , 0x00) # Packet automation control. w/o lqi
555 self.pokebysym("PKTCTRL0" , 0x05) # Packet automation control, w/ checksum.
556 #self.pokebysym("PKTCTRL0" , 0x00) # Packet automation control, w/o checksum, fixed length
557 self.pokebysym("ADDR" , 0x01) # Device address.
558 self.pokebysym("PKTLEN" , 0xFF) # Packet length.
560 self.pokebysym("SYNC1",0xD3);
561 self.pokebysym("SYNC0",0x91);
563 def RF_carrier(self):
564 """Hold a carrier wave on the present frequency."""
566 self.CC1110_crystal(); #FIXME, '1110 specific.
572 self.config_simpliciti();
574 #Don't change these while the radio is active.
575 #self.pokebysym("FSCAL3" , 0xA9) # Frequency synthesizer calibration.
576 #self.pokebysym("FSCAL2" , 0x0A) # Frequency synthesizer calibration.
577 #self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
578 #self.pokebysym("FSCAL0" , 0x11) # Frequency synthesizer calibration.
581 #self.pokebysym("PA_TABLE0", 0xFF) # PA output power setting.
583 #This is what drops to OOK.
584 #Comment to keep GFSK, might be better at jamming.
585 self.pokebysym("MDMCFG4" , 0x86) # Modem configuration.
586 self.pokebysym("MDMCFG3" , 0x83) # Modem configuration.
587 self.pokebysym("MDMCFG2" , 0x30) # Modem configuration.
588 self.pokebysym("MDMCFG1" , 0x22) # Modem configuration.
589 self.pokebysym("MDMCFG0" , 0xF8) # Modem configuration.
591 self.pokebysym("SYNC1",0xAA);
592 self.pokebysym("SYNC0",0xAA);
594 #while ((MARCSTATE & MARCSTATE_MARC_STATE) != MARC_STATE_TX);
597 while((state!=0x13)):
598 self.pokebyte(RFST,0x03); #RFST=RFST_STX
600 state=self.peekbysym("MARCSTATE")&0x1F;
601 #print "state=%02x" % state;
602 print "Holding a carrier on %f MHz." % (self.RF_getfreq()/10**6);
606 def RF_getsmac(self):
607 """Return the source MAC address."""
609 #Register 0A is RX_ADDR_P0, five bytes.
610 mac=self.peekbysym("ADDR");
612 def RF_setsmac(self,mac):
613 """Set the source MAC address."""
614 self.pokebysym("ADDR",mac);
616 def RF_gettmac(self):
617 """Return the target MAC address."""
619 def RF_settmac(self,mac):
620 """Set the target MAC address."""
622 def RF_rxpacket(self):
623 """Get a packet from the radio. Returns None if none is waiting."""
624 self.shellcodefile("rxpacket.ihx");
625 len=self.peek8(0xFE00,"xdata");
626 return self.peekblock(0xFE00,len+3,"data");
627 def RF_txpacket(self,packet):
628 """Transmit a packet. Untested."""
630 self.pokeblock(0xFE00,packet,"data");
631 self.shellcodefile("txpacket.ihx");
633 def RF_txrxpacket(self,packet):
634 """Transmit a packet. Untested."""
636 self.pokeblock(0xFE00,packet,"data");
637 self.shellcodefile("txrxpacket.ihx");
638 len=self.peek8(0xFE00,"xdata");
639 return self.peekblock(0xFE00,len+3,"data");
641 def RF_getrssi(self):
642 """Returns the received signal strenght, with a weird offset."""
644 rssireg=self.symbols.get("RSSI");
645 return self.CCpeekdatabyte(rssireg)^0x80;
647 if self.verbose>0: print "RSSI reg doesn't exist.";
649 #RSSI doesn't exist on some 2.4GHz devices. Maybe RSSIL and RSSIH?
650 rssilreg=self.symbols.get("RSSIL");
651 rssil=self.CCpeekdatabyte(rssilreg);
652 rssihreg=self.symbols.get("RSSIL");
653 rssih=self.CCpeekdatabyte(rssihreg);
654 return (rssih<<8)|rssil;
656 if self.verbose>0: print "RSSIL/RSSIH regs don't exist.";
660 def SRF_loadsymbols(self):
661 ident=self.CCident();
662 chip=self.CCversions.get(ident&0xFF00);
663 dom=self.SRF_chipdom(chip,"register_definition.xml");
664 for e in dom.getElementsByTagName("registerdefinition"):
665 for f in e.childNodes:
666 if f.localName=="Register":
671 for g in f.childNodes:
672 if g.localName=="Name":
673 name=g.childNodes[0].nodeValue;
674 elif g.localName=="Address":
675 address=g.childNodes[0].nodeValue;
676 elif g.localName=="Description":
678 description=g.childNodes[0].nodeValue;
679 elif g.localName=="Bitfield":
680 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
681 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
682 self.symbols.define(eval(address),name,description,"data");
688 self.writecmd(self.APP,0x86,0,self.data);
691 def CCreleasecpu(self):
692 """Resume the CPU."""
693 self.writecmd(self.APP,0x87,0,self.data);
697 #print "Status: %s" % self.CCstatusstr();
699 #Grab ident three times, should be equal.
700 ident1=self.CCident();
701 ident2=self.CCident();
702 ident3=self.CCident();
703 if(ident1!=ident2 or ident2!=ident3):
704 print "Error, repeated ident attempts unequal."
705 print "%04x, %04x, %04x" % (ident1, ident2, ident3);
707 #Single step, printing PC.
708 print "Tracing execution at startup."
709 for i in range(1,15):
711 byte=self.CCpeekcodebyte(i);
712 #print "PC=%04x, %02x" % (pc, byte);
715 print "Verifying that debugging a NOP doesn't affect the PC."
716 for i in range(1,15):
718 self.CCdebuginstr([0x00]);
719 if(pc!=self.CCgetPC()):
720 print "ERROR: PC changed during CCdebuginstr([NOP])!";
722 print "Checking pokes to XRAM."
723 for i in range(self.execbuf,self.execbuf+0x20):
724 self.CCpokedatabyte(i,0xde);
725 if(self.CCpeekdatabyte(i)!=0xde):
726 print "Error in XDATA at 0x%04x" % i;
728 #print "Status: %s." % self.CCstatusstr();
734 """Move the FET into the Chipcon 8051 application."""
735 #print "Initializing Chipcon.";
736 self.writecmd(self.APP,0x10,0,self.data);
737 def CCrd_config(self):
738 """Read the config register of a Chipcon."""
739 self.writecmd(self.APP,0x82,0,self.data);
740 return ord(self.data[0]);
741 def CCwr_config(self,config):
742 """Write the config register of a Chipcon."""
743 self.writecmd(self.APP,0x81,1,[config&0xFF]);
744 def CClockchip(self):
745 """Set the flash lock bit in info mem."""
746 self.writecmd(self.APP, 0x9A, 0, None);
748 """Set the flash lock bit in info mem."""
752 CCversions={0x0100:"CC1110",
758 0xA500:"CC2530", #page 57 of SWRU191B
764 CCexecbuf= {0x0100:0xF000,
770 0xA500:0x0000, #CC2530
774 0xFF00:None} #missing
775 CCpagesizes={0x01: 1024, #"CC1110",
776 0x11: 1024, #"CC1111",
777 0x85: 2048, #"CC2430",
778 0x89: 2048, #"CC2431",
779 0x81: 1024, #"CC2510",
780 0x91: 1024, #"CC2511",
781 0xA5: 2048, #"CC2530", #page 57 of SWRU191B
782 0xB5: 2048, #"CC2531",
783 0x95: 2048, #"CC2533",
784 0x8D: 2048, #"CC2540",
786 def infostring(self):
787 return self.CCidentstr();
788 def CCidentstr(self):
789 ident=self.CCident();
790 chip=self.CCversions.get(ident&0xFF00);
791 execbuf=self.CCexecbuf.get(ident&0xFF00);
792 pagesize=self.CCpagesizes.get(ident>0xFF);
793 self.execbuf=execbuf;
796 return "%s/r%0.4x/ps0x%0.4x" % (chip, ident, pagesize);
798 return "%04x" % ident;
800 """Get a chipcon's ID."""
801 self.writecmd(self.APP,0x8B,0,None);
802 chip=ord(self.data[0]);
803 rev=ord(self.data[1]);
804 return (chip<<8)+rev;
805 def CCpagesize(self):
806 """Get a chipcon's ID."""
807 self.writecmd(self.APP,0x8B,0,None);
808 chip=ord(self.data[0]);
809 size=self.CCpagesizes.get(chip);
811 print "ERROR: Pagesize undefined.";
812 print "chip=%0.4x" %chip;
817 return self.CCgetPC();
819 """Get a chipcon's PC."""
820 self.writecmd(self.APP,0x83,0,None);
821 hi=ord(self.data[0]);
822 lo=ord(self.data[1]);
824 def CCcmd(self,phrase):
825 self.writecmd(self.APP,0x00,len(phrase),phrase);
826 val=ord(self.data[0]);
827 print "Got %02x" % val;
829 def CCdebuginstr(self,instr):
830 self.writecmd(self.APP,0x88,len(instr),instr);
831 return ord(self.data[0]);
832 #def peekblock(self,adr,length,memory="vn"):
833 # """Return a block of data, broken"""
834 # data=[adr&0xff, (adr&0xff00)>>8,
835 # length&0xFF,(length&0xFF00)>>8];
836 # self.writecmd(self.APP,0x91,4,data);
837 # return [ord(x) for x in self.data]
838 def peek8(self,address, memory="code"):
839 if(memory=="code" or memory=="flash" or memory=="vn"):
840 return self.CCpeekcodebyte(address);
841 elif(memory=="data" or memory=="xdata" or memory=="ram"):
842 return self.CCpeekdatabyte(address);
843 elif(memory=="idata" or memory=="iram"):
844 return self.CCpeekirambyte(address);
845 print "%s is an unknown memory." % memory;
847 def CCpeekcodebyte(self,adr):
848 """Read the contents of code memory at an address."""
849 self.data=[adr&0xff, (adr&0xff00)>>8];
850 self.writecmd(self.APP,0x90,2,self.data);
851 return ord(self.data[0]);
852 def CCpeekdatabyte(self,adr):
853 """Read the contents of data memory at an address."""
854 self.data=[adr&0xff, (adr&0xff00)>>8];
855 self.writecmd(self.APP,0x91, 2, self.data);
856 return ord(self.data[0]);
857 def CCpeekirambyte(self,adr):
858 """Read the contents of IRAM at an address."""
859 self.data=[adr&0xff];
860 self.writecmd(self.APP,0x02, 1, self.data);
861 return ord(self.data[0]);
862 def CCpeekiramword(self,adr):
863 """Read the little-endian contents of IRAM at an address."""
864 return self.CCpeekirambyte(adr)+(
865 self.CCpeekirambyte(adr+1)<<8);
866 def CCpokeiramword(self,adr,val):
867 self.CCpokeirambyte(adr,val&0xff);
868 self.CCpokeirambyte(adr+1,(val>>8)&0xff);
869 def CCpokeirambyte(self,adr,val):
870 """Write the contents of IRAM at an address."""
871 self.data=[adr&0xff, val&0xff];
872 self.writecmd(self.APP,0x02, 2, self.data);
873 return ord(self.data[0]);
874 def pokebyte(self,adr,val,mem="xdata"):
875 self.CCpokedatabyte(adr,val);
876 def CCpokedatabyte(self,adr,val):
877 """Write a byte to data memory."""
878 self.data=[adr&0xff, (adr&0xff00)>>8, val];
879 self.writecmd(self.APP, 0x92, 3, self.data);
880 return ord(self.data[0]);
881 def CCchiperase(self):
882 """Erase all of the target's memory."""
883 self.writecmd(self.APP,0x80,0,None);
885 """Erase all of the target's memory."""
890 """Check the status."""
891 self.writecmd(self.APP,0x84,0,None);
892 return ord(self.data[0])
894 CCstatusbits={0x80 : "erase_busy",
898 0x08 : "halt_status",
903 CCconfigbits={0x20 : "soft_power_mode", #new for CC2530
906 0x02 : "timer_suspend",
907 0x01 : "sel_flash_info_page" #stricken from CC2530
911 """Check the status as a string."""
912 status=self.CCstatus();
917 str="%s %s" %(self.CCstatusbits[i],str);
921 """Start debugging."""
923 #while ident==0xFFFF or ident==0x0000:
925 self.writecmd(self.APP,0x20,0,self.data);
926 identa=self.CCident();
929 ident=self.CCident();
930 #Get SmartRF Studio regs if they exist.
932 print "Status: %s" % self.status();
934 """Stop debugging."""
935 self.writecmd(self.APP,0x21,0,self.data);
936 def CCstep_instr(self):
937 """Step one instruction."""
938 self.writecmd(self.APP,0x89,0,self.data);
939 def CCeraseflashbuffer(self):
940 """Erase the 2kB flash buffer"""
941 self.writecmd(self.APP,0x99);
942 def CCflashpage(self,adr):
943 """Flash 2kB a page of flash from 0xF000 in XDATA"""
948 print "Flashing buffer to 0x%06x" % adr;
949 self.writecmd(self.APP,0x95,4,data);
951 def setsecret(self,value):
952 """Set a secret word for later retreival. Used by glitcher."""
954 pagelen = self.CCpagesize(); #Varies by chip.
955 print "page=%04x, pagelen=%04x" % (page,pagelen);
957 self.CCeraseflashbuffer();
958 print "Setting secret to %x" % value;
959 self.CCpokedatabyte(0xF000,value);
960 self.CCpokedatabyte(0xF800,value);
961 print "Setting secret to %x==%x" % (value,
962 self.CCpeekdatabyte(0xf000));
964 print "code[0]=%x" % self.CCpeekcodebyte(0);
966 """Get a secret word. Used by glitcher."""
967 secret=self.CCpeekcodebyte(0);
968 #print "Got secret %02x" % secret;
971 #FIXME: This is CC1110-specific and duplicates functionality of
972 # SmartRF7 integration.
1090 def getSPR(self,args=[]):
1091 """Get special function registers."""
1092 print "Special Function Registers:"
1095 print " %-8s : 0x%0.2x"%(e,self.CCpeekcodebyte(self.CCspecfuncregs[e]))
1097 for e in self.CCspecfuncregs.keys():
1098 print " %-8s : 0x%0.2x"%(e,self.CCpeekcodebyte(self.CCspecfuncregs[e]))
1100 def dump(self,file,start=0,stop=0xffff):
1101 """Dump an intel hex file from code memory."""
1102 print "Dumping code from %04x to %04x as %s." % (start,stop,file);
1106 h[i]=self.CCpeekcodebyte(i);
1108 print "Dumped %04x."%i;
1109 h.write_hex_file(file); #buffer to disk.
1111 h.write_hex_file(file);
1113 def flash(self,file):
1114 """Flash an intel hex file to code memory."""
1115 print "Flashing %s" % file;
1119 pagelen = self.CCpagesize(); #Varies by chip.
1121 #print "page=%04x, pagelen=%04x" % (page,pagelen);
1125 #Wipe the RAM buffer for the next flash page.
1126 self.CCeraseflashbuffer();
1127 for i in h._buf.keys():
1128 while(i>=page+pagelen):
1130 self.CCflashpage(page);
1131 #client.CCeraseflashbuffer();
1133 print "Flashed page at %06x" % page
1136 #Place byte into buffer.
1137 self.CCpokedatabyte(0xF000+i-page,
1141 print "Buffering %04x toward %06x" % (i,page);
1143 self.CCflashpage(page);
1144 print "Flashed final page at %06x" % page;