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;
42 if ident==0x0000 or ident==0xFFFF:
43 print "Chip ID is 0x%04x, implying a wiring problem." % ident;
45 print "SmartRF not found for chip 0x%04x." % ident;
46 def SRF_chipdom(self,chip="cc1110", doc="register_definition.xml"):
47 """Loads the chip XML definitions from SmartRF7."""
48 fn="%s/config/xml/%s/%s" % (self.smartrfpath,chip,doc);
49 #print "Opening %s" % fn;
50 return xml.dom.minidom.parse(fn)
52 def CMDrs(self,args=[]):
53 """Chip command to grab the radio state."""
55 self.SRF_radiostate();
57 # print "Error printing radio state.";
58 # print "SmartRF not found at %s." % self.smartrfpath;
59 def SRF_bitfieldstr(self,bf):
66 for e in bf.childNodes:
67 if e.localName=="Name" and e.childNodes: name= e.childNodes[0].nodeValue;
68 elif e.localName=="Start": start=e.childNodes[0].nodeValue;
69 elif e.localName=="Stop": stop=e.childNodes[0].nodeValue;
70 return " [%s:%s] %30s " % (start,stop,name);
72 def SRF_radiostate(self):
74 chip=self.CCversions.get(ident&0xFF00);
75 dom=self.SRF_chipdom(chip,"register_definition.xml");
76 for e in dom.getElementsByTagName("registerdefinition"):
77 for f in e.childNodes:
78 if f.localName=="DeviceName":
79 print "// %s RadioState" % (f.childNodes[0].nodeValue);
80 elif f.localName=="Register":
85 for g in f.childNodes:
86 if g.localName=="Name":
87 name=g.childNodes[0].nodeValue;
88 elif g.localName=="Address":
89 address=g.childNodes[0].nodeValue;
90 elif g.localName=="Description":
92 description=g.childNodes[0].nodeValue;
93 elif g.localName=="Bitfield":
94 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
95 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
96 print "%-10s=0x%02x; /* %-50s */" % (
97 name,self.CCpeekdatabyte(eval(address)), description);
98 if bitfields!="": print bitfields.rstrip();
100 def SRF_radiostate_select(self,args=[]):
102 ident=self.CCident();
103 chip=self.CCversions.get(ident&0xFF00);
104 dom=self.SRF_chipdom(chip,"register_definition.xml");
106 if reg.lower() == "help":
109 lreg.append(reg.lower())
110 for e in dom.getElementsByTagName("registerdefinition"):
111 for f in e.childNodes:
112 if f.localName=="DeviceName":
113 print "// %s RadioState" % (f.childNodes[0].nodeValue);
114 elif f.localName=="Register":
119 for g in f.childNodes:
120 if g.localName=="Name":
121 name=g.childNodes[0].nodeValue;
122 elif g.localName=="Address":
123 address=g.childNodes[0].nodeValue;
124 elif g.localName=="Description":
126 description=g.childNodes[0].nodeValue;
127 elif g.localName=="Bitfield":
128 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
129 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
131 print "%-10s /* %-50s */" % (name, description);
132 elif name.lower() in lreg:
133 print "%-10s=0x%02x; /* %-50s */" % (
134 name,self.CCpeekdatabyte(eval(address)), description);
135 if bitfields!="": print bitfields.rstrip();
137 def RF_setfreq(self,frequency):
138 """Set the frequency in Hz."""
139 #FIXME CC1110 specific
140 #Some frequencies fail, probably and FSCAL thing.
143 freq=int(hz/396.728515625);
146 freq1=(freq&0xFF00)>>8;
147 freq2=(freq&0xFF0000)>>16;
149 self.pokebysym("FREQ2",freq2);
150 self.pokebysym("FREQ1",freq1);
151 self.pokebysym("FREQ0",freq0);
153 self.pokebysym("TEST1",0x31);
154 self.pokebysym("TEST0",0x09);
157 #self.pokebysym("FSCAL2" , 0x2A); #above mid
158 self.pokebysym("FSCAL2" , 0x0A); #beneath mid
160 #self.CC_RFST_CAL(); #SCAL
164 def RF_getfreq(self):
165 """Get the frequency in Hz."""
166 #FIXME CC1110 specific
168 #return (2400+self.peek(0x05))*10**6
169 #self.poke(0x05,chan);
171 #freq2=self.CCpeekdatabyte(0xdf09);
172 #freq1=self.CCpeekdatabyte(0xdf0a);
173 #freq0=self.CCpeekdatabyte(0xdf0b);
176 freq2=self.peekbysym("FREQ2");
177 freq1=self.peekbysym("FREQ1");
178 freq0=self.peekbysym("FREQ0");
179 freq=(freq2<<16)+(freq1<<8)+freq0;
183 hz=freq*396.728515625;
187 def RF_getchannel(self):
188 """Get the hex channel."""
189 #FIXME CC1110 specific
192 freq2=self.peekbysym("FREQ2");
193 freq1=self.peekbysym("FREQ1");
194 freq0=self.peekbysym("FREQ0");
195 freq=(freq2<<16)+(freq1<<8)+freq0;
202 lastshellcode="none";
203 def shellcodefile(self,filename,wait=1, alwaysreload=0):
204 """Run a fragment of shellcode by name."""
205 #FIXME: should identify chip model number, use shellcode for that chip.
207 if self.lastshellcode!=filename or alwaysreload>0:
208 self.lastshellcode=filename;
210 file=file.replace("GoodFETCC.pyc","GoodFETCC.py");
211 #TODO make this generic
212 path=file.replace("GoodFETCC.py","shellcode/chipcon/cc1110/");
213 filename=path+filename;
216 h=IntelHex(filename);
217 for i in h._buf.keys():
218 self.CCpokedatabyte(i,h[i]);
220 self.CCdebuginstr([0x02, 0xf0, 0x00]); #ljmp 0xF000
222 while wait>0 and (0==self.CCstatus()&0x20):
224 #print "Waiting for shell code to return.";
227 return self.CCstatus()&0x20;
228 def shellcode(self,code,wait=1):
229 """Copy a block of code into RAM and execute it."""
233 self.pokebyte(0xF000+i,byte);
235 #print "Code loaded, executing."
236 self.CCdebuginstr([0x02, 0xf0, 0x00]); #ljmp 0xF000
238 while wait>0 and (0==self.CCstatus()&0x20):
241 #print "Waiting for shell code to return.";
243 def CC1110_crystal(self):
244 """Start the main crystal of the CC1110 oscillating, needed for radio use."""
245 code=[0x53, 0xBE, 0xFB, #anl SLEEP, #0xFB
247 0xE5, 0xBE, #mov a,SLEEP
248 0x30, 0xE6, 0xFB, #jnb acc.6, back
249 0x53, 0xc6, 0xB8, #anl CLKCON, #0xB8
251 0xE5, 0xC6, #mov a,CLKCON
252 0x20, 0xE6, 0xFB, #jb acc.6, two
253 0x43, 0xBE, 0x04, #orl SLEEP, #0x04
256 self.shellcode(code);
258 #Slower to load, but produced from C.
259 #self.shellcodefile("crystal.ihx");
262 """Move the radio to its idle state."""
266 #Chipcon RF strobes. CC1110 specific
271 def CC_RFST_IDLE(self):
272 """Switch the radio to idle mode, clearing overflows and errors."""
273 self.CC_RFST(self.RFST_IDLE);
274 def CC_RFST_TX(self):
275 """Switch the radio to TX mode."""
276 self.CC_RFST(self.RFST_TX);
277 def CC_RFST_RX(self):
278 """Switch the radio to RX mode."""
279 self.CC_RFST(self.RFST_RX);
280 def CC_RFST_CAL(self):
281 """Calibrate strobe the radio."""
282 self.CC_RFST(self.RFST_CAL);
283 def CC_RFST(self,state=RFST_IDLE):
285 self.pokebyte(RFST,state); #Return to idle state.
287 def config_dash7(self,band="lf"):
288 #These settings came from the OpenTag project's GIT repo on 18 Dec, 2010.
289 #Waiting for official confirmation of the accuracy.
291 self.pokebysym("FSCTRL1" , 0x08) # Frequency synthesizer control.
292 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
294 #Don't change these while the radio is active.
295 self.pokebysym("FSCAL3" , 0xEA) # Frequency synthesizer calibration.
296 self.pokebysym("FSCAL2" , 0x2A) # Frequency synthesizer calibration.
297 self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
298 self.pokebysym("FSCAL0" , 0x1F) # Frequency synthesizer calibration.
300 if band=="ismeu" or band=="eu":
301 print "There is no official eu band for dash7."
302 self.pokebysym("FREQ2" , 0x21) # Frequency control word, high byte.
303 self.pokebysym("FREQ1" , 0x71) # Frequency control word, middle byte.
304 self.pokebysym("FREQ0" , 0x7a) # Frequency control word, low byte.
305 elif band=="ismus" or band=="us":
306 print "There is no official us band for dash7."
307 self.pokebysym("FREQ2" , 0x22) # Frequency control word, high byte.
308 self.pokebysym("FREQ1" , 0xB1) # Frequency control word, middle byte.
309 self.pokebysym("FREQ0" , 0x3B) # Frequency control word, low byte.
310 elif band=="ismlf" or band=="lf":
311 # 433.9198 MHz, same as Simpliciti.
312 self.pokebysym("FREQ2" , 0x10) # Frequency control word, high byte.
313 self.pokebysym("FREQ1" , 0xB0) # Frequency control word, middle byte.
314 self.pokebysym("FREQ0" , 0x71) # Frequency control word, low byte.
318 #Got a frequency, not a band.
319 self.RF_setfreq(eval(band));
320 self.pokebysym("MDMCFG4" , 0x8B) # 62.5 kbps w/ 200 kHz filter
321 self.pokebysym("MDMCFG3" , 0x3B)
322 self.pokebysym("MDMCFG2" , 0x11)
323 self.pokebysym("MDMCFG1" , 0x02)
324 self.pokebysym("MDMCFG0" , 0x53)
325 self.pokebysym("CHANNR" , 0x00) # Channel zero.
326 self.pokebysym("DEVIATN" , 0x50) # 50 kHz deviation
328 self.pokebysym("FREND1" , 0xB6) # Front end RX configuration.
329 self.pokebysym("FREND0" , 0x10) # Front end RX configuration.
330 self.pokebysym("MCSM2" , 0x1E)
331 self.pokebysym("MCSM1" , 0x3F)
332 self.pokebysym("MCSM0" , 0x30)
333 self.pokebysym("FOCCFG" , 0x1D) # Frequency Offset Compensation Configuration.
334 self.pokebysym("BSCFG" , 0x1E) # 6.25% data error rate
336 self.pokebysym("AGCCTRL2" , 0xC7) # AGC control.
337 self.pokebysym("AGCCTRL1" , 0x00) # AGC control.
338 self.pokebysym("AGCCTRL0" , 0xB2) # AGC control.
340 self.pokebysym("TEST2" , 0x81) # Various test settings.
341 self.pokebysym("TEST1" , 0x35) # Various test settings.
342 self.pokebysym("TEST0" , 0x09) # Various test settings.
343 self.pokebysym("PA_TABLE0", 0xc0) # Max output power.
344 self.pokebysym("PKTCTRL1" , 0x04) # Packet automation control, w/ lqi
345 #self.pokebysym("PKTCTRL1" , 0x00) # Packet automation control. w/o lqi
346 self.pokebysym("PKTCTRL0" , 0x05) # Packet automation control, w/ checksum.
347 #self.pokebysym("PKTCTRL0" , 0x00) # Packet automation control, w/o checksum, fixed length
348 self.pokebysym("ADDR" , 0x01) # Device address.
349 self.pokebysym("PKTLEN" , 0xFF) # Packet length.
352 self.pokebysym("SYNC1",0x83);
353 self.pokebysym("SYNC0",0xFE);
355 def config_iclicker(self,band="lf"):
356 #Mike Ossmann figured most of this out, with help from neighbors.
358 self.pokebysym("FSCTRL1" , 0x06) # Frequency synthesizer control.
359 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
361 #Don't change these while the radio is active.
362 self.pokebysym("FSCAL3" , 0xE9)
363 self.pokebysym("FSCAL2" , 0x2A)
364 self.pokebysym("FSCAL1" , 0x00)
365 self.pokebysym("FSCAL0" , 0x1F)
367 if band=="ismeu" or band=="eu":
368 print "The EU band is unknown.";
369 elif band=="ismus" or band=="us":
371 self.pokebysym("FREQ2" , 0x22) # Frequency control word, high byte.
372 self.pokebysym("FREQ1" , 0xD3) # Frequency control word, middle byte.
373 self.pokebysym("FREQ0" , 0xAC) # Frequency control word, low byte.
374 elif band=="ismlf" or band=="lf":
375 print "There is no LF version of the iclicker."
379 #Got a frequency, not a band.
380 self.RF_setfreq(eval(band));
381 # 812.5kHz bandwidth, 152.34 kbaud
382 self.pokebysym("MDMCFG4" , 0x1C)
383 self.pokebysym("MDMCFG3" , 0x80)
384 # no FEC, 2 byte preamble, 250kHz chan spacing
387 #self.pokebysym("MDMCFG2" , 0x01)
389 self.pokebysym("MDMCFG2" , 0x02)
391 self.pokebysym("MDMCFG1" , 0x03)
392 self.pokebysym("MDMCFG0" , 0x3b)
394 self.pokebysym("CHANNR" , 0x2e) # Channel zero.
396 #self.pokebysym("DEVIATN" , 0x71) # 118.5
397 self.pokebysym("DEVIATN" , 0x72) # 253.9 kHz deviation
399 self.pokebysym("FREND1" , 0x56) # Front end RX configuration.
400 self.pokebysym("FREND0" , 0x10) # Front end RX configuration.
401 self.pokebysym("MCSM2" , 0x07)
402 self.pokebysym("MCSM1" , 0x30) #Auto freq. cal.
403 self.pokebysym("MCSM0" , 0x14)
405 self.pokebysym("TEST2" , 0x88) #
406 self.pokebysym("TEST1" , 0x31) #
407 self.pokebysym("TEST0" , 0x09) # High VCO (Upper band.)
408 self.pokebysym("PA_TABLE0", 0xC0) # Max output power.
409 self.pokebysym("PKTCTRL1" , 0x45) # Preamble qualidy 2*4=6, adr check, status
410 self.pokebysym("PKTCTRL0" , 0x00) # No whitening, CR, fixed len.
412 self.pokebysym("PKTLEN" , 0x09) # Packet length.
414 self.pokebysym("SYNC1",0xB0);
415 self.pokebysym("SYNC0",0xB0);
416 self.pokebysym("ADDR", 0xB0);
418 def config_ook(self,band="none"):
419 self.pokebysym("FSCTRL1" , 0x0C) #08 # Frequency synthesizer control.
420 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
422 #Don't change these while the radio is active.
423 self.pokebysym("FSCAL3" , 0xEA) # Frequency synthesizer calibration.
424 self.pokebysym("FSCAL2" , 0x2A) # Frequency synthesizer calibration.
425 self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
426 self.pokebysym("FSCAL0" , 0x1F) # Frequency synthesizer calibration.
428 if band=="ismeu" or band=="eu":
429 self.pokebysym("FREQ2" , 0x21) # Frequency control word, high byte.
430 self.pokebysym("FREQ1" , 0x71) # Frequency control word, middle byte.
431 self.pokebysym("FREQ0" , 0x7a) # Frequency control word, low byte.
432 elif band=="ismus" or band=="us":
433 self.pokebysym("FREQ2" , 0x22) # Frequency control word, high byte.
434 self.pokebysym("FREQ1" , 0xB1) # Frequency control word, middle byte.
435 self.pokebysym("FREQ0" , 0x3B) # Frequency control word, low byte.
436 elif band=="ismlf" or band=="lf":
437 self.pokebysym("FREQ2" , 0x0C) # Frequency control word, high byte.
438 self.pokebysym("FREQ1" , 0x1D) # Frequency control word, middle byte.
439 self.pokebysym("FREQ0" , 0x89) # Frequency control word, low byte.
443 #Got a frequency, not a band.
444 self.RF_setfreq(eval(band));
448 #self.pokebysym("MDMCFG4" , 0x85)
449 #self.pokebysym("MDMCFG3" , 0x83)
451 #self.pokebysym("MDMCFG4" , 0xf4)
452 #self.pokebysym("MDMCFG3" , 0x43)
454 #self.pokebysym("MDMCFG4" , 0xf6)
455 #self.pokebysym("MDMCFG3" , 0x83)
458 #print "Warning: Default to 4.8kbaud.";
459 #self.pokebysym("MDMCFG4" , 0xf7)
460 #self.pokebysym("MDMCFG3" , 0x83)
462 #print "Warning: Default to 9.6kbaud.";
465 self.pokebysym("MDMCFG4" , 0xf8)
466 self.pokebysym("MDMCFG3" , 0x83)
467 self.pokebysym("MDMCFG2" , 0x34) # OOK, carrier-sense, no-manchester
469 #Kind aright for keeloq
470 print "Warning: Guessing baud rate.";
471 #self.pokebysym("MDMCFG4" , 0xf6)
472 #self.pokebysym("MDMCFG3" , 0x93)
473 #self.pokebysym("MDMCFG2" , 0x3C) # OOK, carrier-sense, manchester
475 self.pokebysym("MDMCFG1" , 0x00) # Modem configuration.
476 self.pokebysym("MDMCFG0" , 0xF8) # Modem configuration.
477 self.pokebysym("CHANNR" , 0x00) # Channel number.
479 self.pokebysym("FREND1" , 0x56) # Front end RX configuration.
480 self.pokebysym("FREND0" , 0x11) # Front end RX configuration.
481 self.pokebysym("MCSM0" , 0x18) # Main Radio Control State Machine configuration.
482 #self.pokebysym("FOCCFG" , 0x1D) # Frequency Offset Compensation Configuration.
483 #self.pokebysym("BSCFG" , 0x1C) # Bit synchronization Configuration.
485 #self.pokebysym("AGCCTRL2" , 0xC7) # AGC control.
486 #self.pokebysym("AGCCTRL1" , 0x00) # AGC control.
487 #self.pokebysym("AGCCTRL0" , 0xB2) # AGC control.
489 self.pokebysym("TEST2" , 0x81) # Various test settings.
490 self.pokebysym("TEST1" , 0x35) # Various test settings.
491 self.pokebysym("TEST0" , 0x0B) # Various test settings.
492 self.pokebysym("PA_TABLE0", 0xc2) # Max output power.
493 self.pokebysym("PKTCTRL1" , 0x04) # Packet automation control, w/ lqi
494 #self.pokebysym("PKTCTRL1" , 0x00) # Packet automation control. w/o lqi
495 #self.pokebysym("PKTCTRL0" , 0x05) # Packet automation control, w/ checksum.
496 self.pokebysym("PKTCTRL0" , 0x00) # Packet automation control, w/o checksum, fixed length
497 self.pokebysym("ADDR" , 0x01) # Device address.
498 self.pokebysym("PKTLEN" , 0xFF) # Packet length.
500 self.pokebysym("SYNC1",0xD3);
501 self.pokebysym("SYNC0",0x91);
503 def config_simpliciti(self,band="none"):
504 self.pokebysym("FSCTRL1" , 0x0C) #08 # Frequency synthesizer control.
505 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
507 #Don't change these while the radio is active.
508 self.pokebysym("FSCAL3" , 0xEA) # Frequency synthesizer calibration.
509 self.pokebysym("FSCAL2" , 0x2A) # Frequency synthesizer calibration.
510 self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
511 self.pokebysym("FSCAL0" , 0x1F) # Frequency synthesizer calibration.
513 if band=="ismeu" or band=="eu":
514 self.pokebysym("FREQ2" , 0x21) # Frequency control word, high byte.
515 self.pokebysym("FREQ1" , 0x71) # Frequency control word, middle byte.
516 self.pokebysym("FREQ0" , 0x7a) # Frequency control word, low byte.
517 elif band=="ismus" or band=="us":
518 self.pokebysym("FREQ2" , 0x22) # Frequency control word, high byte.
519 self.pokebysym("FREQ1" , 0xB1) # Frequency control word, middle byte.
520 self.pokebysym("FREQ0" , 0x3B) # Frequency control word, low byte.
521 elif band=="ismlf" or band=="lf":
522 self.pokebysym("FREQ2" , 0x10) # Frequency control word, high byte.
523 self.pokebysym("FREQ1" , 0xB0) # Frequency control word, middle byte.
524 self.pokebysym("FREQ0" , 0x71) # Frequency control word, low byte.
528 #Got a frequency, not a band.
529 self.RF_setfreq(eval(band));
530 self.pokebysym("MDMCFG4" , 0x7B) # Modem configuration.
531 self.pokebysym("MDMCFG3" , 0x83) # Modem configuration.
532 self.pokebysym("MDMCFG2" , 0x13) # Modem configuration.
533 self.pokebysym("MDMCFG1" , 0x22) # Modem configuration.
534 self.pokebysym("MDMCFG0" , 0xF8) # Modem configuration.
535 if band=="ismus" or band=="us":
536 self.pokebysym("CHANNR" , 20) # Channel number.
538 self.pokebysym("CHANNR" , 0x00) # Channel number.
539 self.pokebysym("DEVIATN" , 0x42) # Modem deviation setting (when FSK modulation is enabled).
541 self.pokebysym("FREND1" , 0xB6) # Front end RX configuration.
542 self.pokebysym("FREND0" , 0x10) # Front end RX configuration.
543 self.pokebysym("MCSM0" , 0x18) # Main Radio Control State Machine configuration.
544 self.pokebysym("FOCCFG" , 0x1D) # Frequency Offset Compensation Configuration.
545 self.pokebysym("BSCFG" , 0x1C) # Bit synchronization Configuration.
547 self.pokebysym("AGCCTRL2" , 0xC7) # AGC control.
548 self.pokebysym("AGCCTRL1" , 0x00) # AGC control.
549 self.pokebysym("AGCCTRL0" , 0xB2) # AGC control.
551 self.pokebysym("TEST2" , 0x81) # Various test settings.
552 self.pokebysym("TEST1" , 0x35) # Various test settings.
553 self.pokebysym("TEST0" , 0x09) # Various test settings.
554 self.pokebysym("PA_TABLE0", 0xc0) # Max output power.
555 self.pokebysym("PKTCTRL1" , 0x04) # Packet automation control, w/ lqi
556 #self.pokebysym("PKTCTRL1" , 0x00) # Packet automation control. w/o lqi
557 self.pokebysym("PKTCTRL0" , 0x05) # Packet automation control, w/ checksum.
558 #self.pokebysym("PKTCTRL0" , 0x00) # Packet automation control, w/o checksum, fixed length
559 self.pokebysym("ADDR" , 0x01) # Device address.
560 self.pokebysym("PKTLEN" , 0xFF) # Packet length.
562 self.pokebysym("SYNC1",0xD3);
563 self.pokebysym("SYNC0",0x91);
565 def RF_carrier(self):
566 """Hold a carrier wave on the present frequency."""
568 self.CC1110_crystal(); #FIXME, '1110 specific.
574 self.config_simpliciti();
576 #Don't change these while the radio is active.
577 #self.pokebysym("FSCAL3" , 0xA9) # Frequency synthesizer calibration.
578 #self.pokebysym("FSCAL2" , 0x0A) # Frequency synthesizer calibration.
579 #self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
580 #self.pokebysym("FSCAL0" , 0x11) # Frequency synthesizer calibration.
583 #self.pokebysym("PA_TABLE0", 0xFF) # PA output power setting.
585 #This is what drops to OOK.
586 #Comment to keep GFSK, might be better at jamming.
587 self.pokebysym("MDMCFG4" , 0x86) # Modem configuration.
588 self.pokebysym("MDMCFG3" , 0x83) # Modem configuration.
589 self.pokebysym("MDMCFG2" , 0x30) # Modem configuration.
590 self.pokebysym("MDMCFG1" , 0x22) # Modem configuration.
591 self.pokebysym("MDMCFG0" , 0xF8) # Modem configuration.
593 self.pokebysym("SYNC1",0xAA);
594 self.pokebysym("SYNC0",0xAA);
596 #while ((MARCSTATE & MARCSTATE_MARC_STATE) != MARC_STATE_TX);
599 while((state!=0x13)):
600 self.pokebyte(RFST,0x03); #RFST=RFST_STX
602 state=self.peekbysym("MARCSTATE")&0x1F;
603 #print "state=%02x" % state;
604 print "Holding a carrier on %f MHz." % (self.RF_getfreq()/10**6);
608 def RF_getsmac(self):
609 """Return the source MAC address."""
611 #Register 0A is RX_ADDR_P0, five bytes.
612 mac=self.peekbysym("ADDR");
614 def RF_setsmac(self,mac):
615 """Set the source MAC address."""
616 self.pokebysym("ADDR",mac);
618 def RF_gettmac(self):
619 """Return the target MAC address."""
621 def RF_settmac(self,mac):
622 """Set the target MAC address."""
624 def RF_rxpacket(self):
625 """Get a packet from the radio. Returns None if none is waiting."""
626 self.shellcodefile("rxpacket.ihx");
627 len=self.peek8(0xFE00,"xdata");
628 return self.peekblock(0xFE00,len+3,"data");
629 def RF_txpacket(self,packet):
630 """Transmit a packet. Untested."""
632 self.pokeblock(0xFE00,packet,"data");
633 self.shellcodefile("txpacket.ihx");
635 def RF_txrxpacket(self,packet):
636 """Transmit a packet. Untested."""
638 self.pokeblock(0xFE00,packet,"data");
639 self.shellcodefile("txrxpacket.ihx");
640 len=self.peek8(0xFE00,"xdata");
641 return self.peekblock(0xFE00,len+3,"data");
643 def RF_getrssi(self):
644 """Returns the received signal strenght, with a weird offset."""
646 rssireg=self.symbols.get("RSSI");
647 return self.CCpeekdatabyte(rssireg)^0x80;
649 if self.verbose>0: print "RSSI reg doesn't exist.";
651 #RSSI doesn't exist on some 2.4GHz devices. Maybe RSSIL and RSSIH?
652 rssilreg=self.symbols.get("RSSIL");
653 rssil=self.CCpeekdatabyte(rssilreg);
654 rssihreg=self.symbols.get("RSSIL");
655 rssih=self.CCpeekdatabyte(rssihreg);
656 return (rssih<<8)|rssil;
658 if self.verbose>0: print "RSSIL/RSSIH regs don't exist.";
662 def SRF_loadsymbols(self):
663 ident=self.CCident();
664 chip=self.CCversions.get(ident&0xFF00);
665 dom=self.SRF_chipdom(chip,"register_definition.xml");
666 for e in dom.getElementsByTagName("registerdefinition"):
667 for f in e.childNodes:
668 if f.localName=="Register":
673 for g in f.childNodes:
674 if g.localName=="Name":
675 name=g.childNodes[0].nodeValue;
676 elif g.localName=="Address":
677 address=g.childNodes[0].nodeValue;
678 elif g.localName=="Description":
680 description=g.childNodes[0].nodeValue;
681 elif g.localName=="Bitfield":
682 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
683 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
684 self.symbols.define(eval(address),name,description,"data");
690 self.writecmd(self.APP,0x86,0,self.data);
693 def CCreleasecpu(self):
694 """Resume the CPU."""
695 self.writecmd(self.APP,0x87,0,self.data);
699 #print "Status: %s" % self.CCstatusstr();
701 #Grab ident three times, should be equal.
702 ident1=self.CCident();
703 ident2=self.CCident();
704 ident3=self.CCident();
705 if(ident1!=ident2 or ident2!=ident3):
706 print "Error, repeated ident attempts unequal."
707 print "%04x, %04x, %04x" % (ident1, ident2, ident3);
709 #Single step, printing PC.
710 print "Tracing execution at startup."
711 for i in range(1,15):
713 byte=self.CCpeekcodebyte(i);
714 #print "PC=%04x, %02x" % (pc, byte);
717 print "Verifying that debugging a NOP doesn't affect the PC."
718 for i in range(1,15):
720 self.CCdebuginstr([0x00]);
721 if(pc!=self.CCgetPC()):
722 print "ERROR: PC changed during CCdebuginstr([NOP])!";
724 print "Checking pokes to XRAM."
725 for i in range(self.execbuf,self.execbuf+0x20):
726 self.CCpokedatabyte(i,0xde);
727 if(self.CCpeekdatabyte(i)!=0xde):
728 print "Error in XDATA at 0x%04x" % i;
730 #print "Status: %s." % self.CCstatusstr();
736 """Move the FET into the Chipcon 8051 application."""
737 #print "Initializing Chipcon.";
738 self.writecmd(self.APP,0x10,0,self.data);
739 def CCrd_config(self):
740 """Read the config register of a Chipcon."""
741 self.writecmd(self.APP,0x82,0,self.data);
742 return ord(self.data[0]);
743 def CCwr_config(self,config):
744 """Write the config register of a Chipcon."""
745 self.writecmd(self.APP,0x81,1,[config&0xFF]);
746 def CClockchip(self):
747 """Set the flash lock bit in info mem."""
748 self.writecmd(self.APP, 0x9A, 0, None);
750 """Set the flash lock bit in info mem."""
754 CCversions={0x0100:"cc1110",
760 0xA500:"cc2530", #page 57 of SWRU191B
766 CCexecbuf= {0x0100:0xF000,
772 0xA500:0x0000, #CC2530
776 0xFF00:None} #missing
777 CCpagesizes={0x01: 1024, #"CC1110",
778 0x11: 1024, #"CC1111",
779 0x85: 2048, #"CC2430",
780 0x89: 2048, #"CC2431",
781 0x81: 1024, #"CC2510",
782 0x91: 1024, #"CC2511",
783 0xA5: 2048, #"CC2530", #page 57 of SWRU191B
784 0xB5: 2048, #"CC2531",
785 0x95: 2048, #"CC2533",
786 0x8D: 2048, #"CC2540",
788 def infostring(self):
789 return self.CCidentstr();
790 def CCidentstr(self):
791 ident=self.CCident();
792 chip=self.CCversions.get(ident&0xFF00);
793 execbuf=self.CCexecbuf.get(ident&0xFF00);
794 pagesize=self.CCpagesizes.get(ident>0xFF);
795 self.execbuf=execbuf;
798 return "%s/r%0.4x/ps0x%0.4x" % (chip, ident, pagesize);
800 return "%04x" % ident;
802 """Get a chipcon's ID."""
803 self.writecmd(self.APP,0x8B,0,None);
804 chip=ord(self.data[0]);
805 rev=ord(self.data[1]);
806 return (chip<<8)+rev;
807 def CCpagesize(self):
808 """Get a chipcon's ID."""
809 self.writecmd(self.APP,0x8B,0,None);
810 chip=ord(self.data[0]);
811 size=self.CCpagesizes.get(chip);
813 print "ERROR: Pagesize undefined.";
814 print "chip=%0.4x" %chip;
819 return self.CCgetPC();
821 """Get a chipcon's PC."""
822 self.writecmd(self.APP,0x83,0,None);
823 hi=ord(self.data[0]);
824 lo=ord(self.data[1]);
826 def CCcmd(self,phrase):
827 self.writecmd(self.APP,0x00,len(phrase),phrase);
828 val=ord(self.data[0]);
829 print "Got %02x" % val;
831 def CCdebuginstr(self,instr):
832 self.writecmd(self.APP,0x88,len(instr),instr);
833 return ord(self.data[0]);
834 #def peekblock(self,adr,length,memory="vn"):
835 # """Return a block of data, broken"""
836 # data=[adr&0xff, (adr&0xff00)>>8,
837 # length&0xFF,(length&0xFF00)>>8];
838 # self.writecmd(self.APP,0x91,4,data);
839 # return [ord(x) for x in self.data]
840 def peek8(self,address, memory="code"):
841 if(memory=="code" or memory=="flash" or memory=="vn"):
842 return self.CCpeekcodebyte(address);
843 elif(memory=="data" or memory=="xdata" or memory=="ram"):
844 return self.CCpeekdatabyte(address);
845 elif(memory=="idata" or memory=="iram"):
846 return self.CCpeekirambyte(address);
847 print "%s is an unknown memory." % memory;
849 def CCpeekcodebyte(self,adr):
850 """Read the contents of code memory at an address."""
851 self.data=[adr&0xff, (adr&0xff00)>>8];
852 self.writecmd(self.APP,0x90,2,self.data);
853 return ord(self.data[0]);
854 def CCpeekdatabyte(self,adr):
855 """Read the contents of data memory at an address."""
856 self.data=[adr&0xff, (adr&0xff00)>>8];
857 self.writecmd(self.APP,0x91, 2, self.data);
858 return ord(self.data[0]);
859 def CCpeekirambyte(self,adr):
860 """Read the contents of IRAM at an address."""
861 self.data=[adr&0xff];
862 self.writecmd(self.APP,0x02, 1, self.data);
863 return ord(self.data[0]);
864 def CCpeekiramword(self,adr):
865 """Read the little-endian contents of IRAM at an address."""
866 return self.CCpeekirambyte(adr)+(
867 self.CCpeekirambyte(adr+1)<<8);
868 def CCpokeiramword(self,adr,val):
869 self.CCpokeirambyte(adr,val&0xff);
870 self.CCpokeirambyte(adr+1,(val>>8)&0xff);
871 def CCpokeirambyte(self,adr,val):
872 """Write the contents of IRAM at an address."""
873 self.data=[adr&0xff, val&0xff];
874 self.writecmd(self.APP,0x02, 2, self.data);
875 return ord(self.data[0]);
876 def pokebyte(self,adr,val,mem="xdata"):
877 self.CCpokedatabyte(adr,val);
878 def CCpokedatabyte(self,adr,val):
879 """Write a byte to data memory."""
880 self.data=[adr&0xff, (adr&0xff00)>>8, val];
881 self.writecmd(self.APP, 0x92, 3, self.data);
882 return ord(self.data[0]);
883 def CCchiperase(self):
884 """Erase all of the target's memory."""
885 self.writecmd(self.APP,0x80,0,None);
887 """Erase all of the target's memory."""
892 """Check the status."""
893 self.writecmd(self.APP,0x84,0,None);
894 return ord(self.data[0])
896 CCstatusbits={0x80 : "erase_busy",
900 0x08 : "halt_status",
905 CCconfigbits={0x20 : "soft_power_mode", #new for CC2530
908 0x02 : "timer_suspend",
909 0x01 : "sel_flash_info_page" #stricken from CC2530
913 """Check the status as a string."""
914 status=self.CCstatus();
919 str="%s %s" %(self.CCstatusbits[i],str);
923 """Start debugging."""
925 #while ident==0xFFFF or ident==0x0000:
927 self.writecmd(self.APP,0x20,0,self.data);
928 identa=self.CCident();
931 ident=self.CCident();
932 #Get SmartRF Studio regs if they exist.
934 #print "Status: %s" % self.status();
936 """Stop debugging."""
937 self.writecmd(self.APP,0x21,0,self.data);
938 def CCstep_instr(self):
939 """Step one instruction."""
940 self.writecmd(self.APP,0x89,0,self.data);
941 def CCeraseflashbuffer(self):
942 """Erase the 2kB flash buffer"""
943 self.writecmd(self.APP,0x99);
944 def CCflashpage(self,adr):
945 """Flash 2kB a page of flash from 0xF000 in XDATA"""
950 print "Flashing buffer to 0x%06x" % adr;
951 self.writecmd(self.APP,0x95,4,data);
953 def setsecret(self,value):
954 """Set a secret word for later retreival. Used by glitcher."""
956 pagelen = self.CCpagesize(); #Varies by chip.
957 print "page=%04x, pagelen=%04x" % (page,pagelen);
959 self.CCeraseflashbuffer();
960 print "Setting secret to %x" % value;
961 self.CCpokedatabyte(0xF000,value);
962 self.CCpokedatabyte(0xF800,value);
963 print "Setting secret to %x==%x" % (value,
964 self.CCpeekdatabyte(0xf000));
966 print "code[0]=%x" % self.CCpeekcodebyte(0);
968 """Get a secret word. Used by glitcher."""
969 secret=self.CCpeekcodebyte(0);
970 #print "Got secret %02x" % secret;
973 #FIXME: This is CC1110-specific and duplicates functionality of
974 # SmartRF7 integration.
1092 def getSPR(self,args=[]):
1093 """Get special function registers."""
1094 print "Special Function Registers:"
1097 print " %-8s : 0x%0.2x"%(e,self.CCpeekcodebyte(self.CCspecfuncregs[e]))
1099 for e in self.CCspecfuncregs.keys():
1100 print " %-8s : 0x%0.2x"%(e,self.CCpeekcodebyte(self.CCspecfuncregs[e]))
1102 def dump(self,file,start=0,stop=0xffff):
1103 """Dump an intel hex file from code memory."""
1104 print "Dumping code from %04x to %04x as %s." % (start,stop,file);
1108 h[i]=self.CCpeekcodebyte(i);
1110 print "Dumped %04x."%i;
1111 h.write_hex_file(file); #buffer to disk.
1113 h.write_hex_file(file);
1115 def flash(self,file):
1116 """Flash an intel hex file to code memory."""
1117 print "Flashing %s" % file;
1121 pagelen = self.CCpagesize(); #Varies by chip.
1123 #print "page=%04x, pagelen=%04x" % (page,pagelen);
1127 #Wipe the RAM buffer for the next flash page.
1128 self.CCeraseflashbuffer();
1129 for i in h._buf.keys():
1130 while(i>=page+pagelen):
1132 self.CCflashpage(page);
1133 #client.CCeraseflashbuffer();
1135 print "Flashed page at %06x" % page
1138 #Place byte into buffer.
1139 self.CCpokedatabyte(0xF000+i-page,
1143 print "Buffering %04x toward %06x" % (i,page);
1145 self.CCflashpage(page);
1146 print "Flashed final page at %06x" % page;