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";
34 def loadsymbols(self):
35 try: self.SRF_loadsymbols();
37 print "SmartRF not found at %s." % self.smartrfpath;
38 def SRF_chipdom(self,chip="cc1110", doc="register_definition.xml"):
39 """Loads the chip XML definitions from SmartRF7."""
40 fn="%s/config/xml/%s/%s" % (self.smartrfpath,chip,doc);
41 #print "Opening %s" % fn;
42 return xml.dom.minidom.parse(fn)
44 def CMDrs(self,args=[]):
45 """Chip command to grab the radio state."""
47 self.SRF_radiostate();
49 # print "Error printing radio state.";
50 # print "SmartRF not found at %s." % self.smartrfpath;
51 def SRF_bitfieldstr(self,bf):
58 for e in bf.childNodes:
59 if e.localName=="Name" and e.childNodes: name= e.childNodes[0].nodeValue;
60 elif e.localName=="Start": start=e.childNodes[0].nodeValue;
61 elif e.localName=="Stop": stop=e.childNodes[0].nodeValue;
62 return " [%s:%s] %30s " % (start,stop,name);
64 def SRF_radiostate(self):
66 chip=self.CCversions.get(ident&0xFF00);
67 dom=self.SRF_chipdom(chip,"register_definition.xml");
68 for e in dom.getElementsByTagName("registerdefinition"):
69 for f in e.childNodes:
70 if f.localName=="DeviceName":
71 print "// %s RadioState" % (f.childNodes[0].nodeValue);
72 elif f.localName=="Register":
77 for g in f.childNodes:
78 if g.localName=="Name":
79 name=g.childNodes[0].nodeValue;
80 elif g.localName=="Address":
81 address=g.childNodes[0].nodeValue;
82 elif g.localName=="Description":
84 description=g.childNodes[0].nodeValue;
85 elif g.localName=="Bitfield":
86 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
87 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
88 print "%-10s=0x%02x; /* %-50s */" % (
89 name,self.CCpeekdatabyte(eval(address)), description);
90 if bitfields!="": print bitfields.rstrip();
92 def SRF_radiostate_select(self,args=[]):
95 chip=self.CCversions.get(ident&0xFF00);
96 dom=self.SRF_chipdom(chip,"register_definition.xml");
98 if reg.lower() == "help":
101 lreg.append(reg.lower())
102 for e in dom.getElementsByTagName("registerdefinition"):
103 for f in e.childNodes:
104 if f.localName=="DeviceName":
105 print "// %s RadioState" % (f.childNodes[0].nodeValue);
106 elif f.localName=="Register":
111 for g in f.childNodes:
112 if g.localName=="Name":
113 name=g.childNodes[0].nodeValue;
114 elif g.localName=="Address":
115 address=g.childNodes[0].nodeValue;
116 elif g.localName=="Description":
118 description=g.childNodes[0].nodeValue;
119 elif g.localName=="Bitfield":
120 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
121 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
123 print "%-10s /* %-50s */" % (name, description);
124 elif name.lower() in lreg:
125 print "%-10s=0x%02x; /* %-50s */" % (
126 name,self.CCpeekdatabyte(eval(address)), description);
127 if bitfields!="": print bitfields.rstrip();
129 def RF_setfreq(self,frequency):
130 """Set the frequency in Hz."""
131 #FIXME CC1110 specific
132 #Some frequencies fail, probably and FSCAL thing.
135 freq=int(hz/396.728515625);
138 freq1=(freq&0xFF00)>>8;
139 freq2=(freq&0xFF0000)>>16;
141 self.pokebysym("FREQ2",freq2);
142 self.pokebysym("FREQ1",freq1);
143 self.pokebysym("FREQ0",freq0);
145 self.pokebysym("TEST1",0x31);
146 self.pokebysym("TEST0",0x09);
149 #self.pokebysym("FSCAL2" , 0x2A); #above mid
150 self.pokebysym("FSCAL2" , 0x0A); #beneath mid
152 #self.CC_RFST_CAL(); #SCAL
156 def RF_getfreq(self):
157 """Get the frequency in Hz."""
158 #FIXME CC1110 specific
160 #return (2400+self.peek(0x05))*10**6
161 #self.poke(0x05,chan);
163 #freq2=self.CCpeekdatabyte(0xdf09);
164 #freq1=self.CCpeekdatabyte(0xdf0a);
165 #freq0=self.CCpeekdatabyte(0xdf0b);
168 freq2=self.peekbysym("FREQ2");
169 freq1=self.peekbysym("FREQ1");
170 freq0=self.peekbysym("FREQ0");
171 freq=(freq2<<16)+(freq1<<8)+freq0;
175 hz=freq*396.728515625;
179 def RF_getchannel(self):
180 """Get the hex channel."""
181 #FIXME CC1110 specific
184 freq2=self.peekbysym("FREQ2");
185 freq1=self.peekbysym("FREQ1");
186 freq0=self.peekbysym("FREQ0");
187 freq=(freq2<<16)+(freq1<<8)+freq0;
194 lastshellcode="none";
195 def shellcodefile(self,filename,wait=1, alwaysreload=0):
196 """Run a fragment of shellcode by name."""
197 #FIXME: should identify chip model number, use shellcode for that chip.
199 if self.lastshellcode!=filename or alwaysreload>0:
200 self.lastshellcode=filename;
202 file=file.replace("GoodFETCC.pyc","GoodFETCC.py");
203 #TODO make this generic
204 path=file.replace("GoodFETCC.py","shellcode/chipcon/cc1110/");
205 filename=path+filename;
208 h=IntelHex(filename);
209 for i in h._buf.keys():
210 self.CCpokedatabyte(i,h[i]);
213 self.CCdebuginstr([0x02, 0xf0, 0x00]); #ljmp 0xF000
215 while wait>0 and (0==self.CCstatus()&0x20):
218 #print "Waiting for shell code to return.";
221 return self.CCstatus()&0x20;
222 def shellcode(self,code,wait=1):
223 """Copy a block of code into RAM and execute it."""
227 self.pokebyte(0xF000+i,byte);
229 #print "Code loaded, executing."
230 self.CCdebuginstr([0x02, 0xf0, 0x00]); #ljmp 0xF000
232 while wait>0 and (0==self.CCstatus()&0x20):
235 #print "Waiting for shell code to return.";
237 def CC1110_crystal(self):
238 """Start the main crystal of the CC1110 oscillating, needed for radio use."""
239 code=[0x53, 0xBE, 0xFB, #anl SLEEP, #0xFB
241 0xE5, 0xBE, #mov a,SLEEP
242 0x30, 0xE6, 0xFB, #jnb acc.6, back
243 0x53, 0xc6, 0xB8, #anl CLKCON, #0xB8
245 0xE5, 0xC6, #mov a,CLKCON
246 0x20, 0xE6, 0xFB, #jb acc.6, two
247 0x43, 0xBE, 0x04, #orl SLEEP, #0x04
250 self.shellcode(code);
252 #Slower to load, but produced from C.
253 #self.shellcodefile("crystal.ihx");
256 """Move the radio to its idle state."""
260 #Chipcon RF strobes. CC1110 specific
265 def CC_RFST_IDLE(self):
266 """Switch the radio to idle mode, clearing overflows and errors."""
267 self.CC_RFST(self.RFST_IDLE);
268 def CC_RFST_TX(self):
269 """Switch the radio to TX mode."""
270 self.CC_RFST(self.RFST_TX);
271 def CC_RFST_RX(self):
272 """Switch the radio to RX mode."""
273 self.CC_RFST(self.RFST_RX);
274 def CC_RFST_CAL(self):
275 """Calibrate strobe the radio."""
276 self.CC_RFST(self.RFST_CAL);
277 def CC_RFST(self,state=RFST_IDLE):
279 self.pokebyte(RFST,state); #Return to idle state.
281 def config_dash7(self,band="lf"):
282 #These settings came from the OpenTag project's GIT repo on 18 Dec, 2010.
283 #Waiting for official confirmation of the accuracy.
285 self.pokebysym("FSCTRL1" , 0x08) # Frequency synthesizer control.
286 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
288 #Don't change these while the radio is active.
289 self.pokebysym("FSCAL3" , 0xEA) # Frequency synthesizer calibration.
290 self.pokebysym("FSCAL2" , 0x2A) # Frequency synthesizer calibration.
291 self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
292 self.pokebysym("FSCAL0" , 0x1F) # Frequency synthesizer calibration.
294 if band=="ismeu" or band=="eu":
295 print "There is no official eu band for dash7."
296 self.pokebysym("FREQ2" , 0x21) # Frequency control word, high byte.
297 self.pokebysym("FREQ1" , 0x71) # Frequency control word, middle byte.
298 self.pokebysym("FREQ0" , 0x7a) # Frequency control word, low byte.
299 elif band=="ismus" or band=="us":
300 print "There is no official us band for dash7."
301 self.pokebysym("FREQ2" , 0x22) # Frequency control word, high byte.
302 self.pokebysym("FREQ1" , 0xB1) # Frequency control word, middle byte.
303 self.pokebysym("FREQ0" , 0x3B) # Frequency control word, low byte.
304 elif band=="ismlf" or band=="lf":
305 # 433.9198 MHz, same as Simpliciti.
306 self.pokebysym("FREQ2" , 0x10) # Frequency control word, high byte.
307 self.pokebysym("FREQ1" , 0xB0) # Frequency control word, middle byte.
308 self.pokebysym("FREQ0" , 0x71) # Frequency control word, low byte.
312 #Got a frequency, not a band.
313 self.RF_setfreq(eval(band));
314 self.pokebysym("MDMCFG4" , 0x8B) # 62.5 kbps w/ 200 kHz filter
315 self.pokebysym("MDMCFG3" , 0x3B)
316 self.pokebysym("MDMCFG2" , 0x11)
317 self.pokebysym("MDMCFG1" , 0x02)
318 self.pokebysym("MDMCFG0" , 0x53)
319 self.pokebysym("CHANNR" , 0x00) # Channel zero.
320 self.pokebysym("DEVIATN" , 0x50) # 50 kHz deviation
322 self.pokebysym("FREND1" , 0xB6) # Front end RX configuration.
323 self.pokebysym("FREND0" , 0x10) # Front end RX configuration.
324 self.pokebysym("MCSM2" , 0x1E)
325 self.pokebysym("MCSM1" , 0x3F)
326 self.pokebysym("MCSM0" , 0x30)
327 self.pokebysym("FOCCFG" , 0x1D) # Frequency Offset Compensation Configuration.
328 self.pokebysym("BSCFG" , 0x1E) # 6.25% data error rate
330 self.pokebysym("AGCCTRL2" , 0xC7) # AGC control.
331 self.pokebysym("AGCCTRL1" , 0x00) # AGC control.
332 self.pokebysym("AGCCTRL0" , 0xB2) # AGC control.
334 self.pokebysym("TEST2" , 0x81) # Various test settings.
335 self.pokebysym("TEST1" , 0x35) # Various test settings.
336 self.pokebysym("TEST0" , 0x09) # Various test settings.
337 self.pokebysym("PA_TABLE0", 0xc0) # Max output power.
338 self.pokebysym("PKTCTRL1" , 0x04) # Packet automation control, w/ lqi
339 #self.pokebysym("PKTCTRL1" , 0x00) # Packet automation control. w/o lqi
340 self.pokebysym("PKTCTRL0" , 0x05) # Packet automation control, w/ checksum.
341 #self.pokebysym("PKTCTRL0" , 0x00) # Packet automation control, w/o checksum, fixed length
342 self.pokebysym("ADDR" , 0x01) # Device address.
343 self.pokebysym("PKTLEN" , 0xFF) # Packet length.
346 self.pokebysym("SYNC1",0x83);
347 self.pokebysym("SYNC0",0xFE);
349 def config_iclicker(self,band="lf"):
350 #Mike Ossmann figured most of this out, with help from neighbors.
352 self.pokebysym("FSCTRL1" , 0x06) # Frequency synthesizer control.
353 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
355 #Don't change these while the radio is active.
356 self.pokebysym("FSCAL3" , 0xE9)
357 self.pokebysym("FSCAL2" , 0x2A)
358 self.pokebysym("FSCAL1" , 0x00)
359 self.pokebysym("FSCAL0" , 0x1F)
361 if band=="ismeu" or band=="eu":
362 print "The EU band is unknown.";
363 elif band=="ismus" or band=="us":
365 self.pokebysym("FREQ2" , 0x22) # Frequency control word, high byte.
366 self.pokebysym("FREQ1" , 0xD3) # Frequency control word, middle byte.
367 self.pokebysym("FREQ0" , 0xAC) # Frequency control word, low byte.
368 elif band=="ismlf" or band=="lf":
369 print "There is no LF version of the iclicker."
373 #Got a frequency, not a band.
374 self.RF_setfreq(eval(band));
375 # 812.5kHz bandwidth, 152.34 kbaud
376 self.pokebysym("MDMCFG4" , 0x1C)
377 self.pokebysym("MDMCFG3" , 0x80)
378 # no FEC, 2 byte preamble, 250kHz chan spacing
381 #self.pokebysym("MDMCFG2" , 0x01)
383 self.pokebysym("MDMCFG2" , 0x02)
385 self.pokebysym("MDMCFG1" , 0x03)
386 self.pokebysym("MDMCFG0" , 0x3b)
388 self.pokebysym("CHANNR" , 0x2e) # Channel zero.
390 #self.pokebysym("DEVIATN" , 0x71) # 118.5
391 self.pokebysym("DEVIATN" , 0x72) # 253.9 kHz deviation
393 self.pokebysym("FREND1" , 0x56) # Front end RX configuration.
394 self.pokebysym("FREND0" , 0x10) # Front end RX configuration.
395 self.pokebysym("MCSM2" , 0x07)
396 self.pokebysym("MCSM1" , 0x30) #Auto freq. cal.
397 self.pokebysym("MCSM0" , 0x14)
399 self.pokebysym("TEST2" , 0x88) #
400 self.pokebysym("TEST1" , 0x31) #
401 self.pokebysym("TEST0" , 0x09) # High VCO (Upper band.)
402 self.pokebysym("PA_TABLE0", 0xC0) # Max output power.
403 self.pokebysym("PKTCTRL1" , 0x45) # Preamble qualidy 2*4=6, adr check, status
404 self.pokebysym("PKTCTRL0" , 0x00) # No whitening, CR, fixed len.
406 self.pokebysym("PKTLEN" , 0x09) # Packet length.
408 self.pokebysym("SYNC1",0xB0);
409 self.pokebysym("SYNC0",0xB0);
410 self.pokebysym("ADDR", 0xB0);
412 def config_ook(self,band="none"):
413 self.pokebysym("FSCTRL1" , 0x0C) #08 # Frequency synthesizer control.
414 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
416 #Don't change these while the radio is active.
417 self.pokebysym("FSCAL3" , 0xEA) # Frequency synthesizer calibration.
418 self.pokebysym("FSCAL2" , 0x2A) # Frequency synthesizer calibration.
419 self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
420 self.pokebysym("FSCAL0" , 0x1F) # Frequency synthesizer calibration.
422 if band=="ismeu" or band=="eu":
423 self.pokebysym("FREQ2" , 0x21) # Frequency control word, high byte.
424 self.pokebysym("FREQ1" , 0x71) # Frequency control word, middle byte.
425 self.pokebysym("FREQ0" , 0x7a) # Frequency control word, low byte.
426 elif band=="ismus" or band=="us":
427 self.pokebysym("FREQ2" , 0x22) # Frequency control word, high byte.
428 self.pokebysym("FREQ1" , 0xB1) # Frequency control word, middle byte.
429 self.pokebysym("FREQ0" , 0x3B) # Frequency control word, low byte.
430 elif band=="ismlf" or band=="lf":
431 self.pokebysym("FREQ2" , 0x0C) # Frequency control word, high byte.
432 self.pokebysym("FREQ1" , 0x1D) # Frequency control word, middle byte.
433 self.pokebysym("FREQ0" , 0x89) # Frequency control word, low byte.
437 #Got a frequency, not a band.
438 self.RF_setfreq(eval(band));
442 #self.pokebysym("MDMCFG4" , 0x85)
443 #self.pokebysym("MDMCFG3" , 0x83)
445 #self.pokebysym("MDMCFG4" , 0xf4)
446 #self.pokebysym("MDMCFG3" , 0x43)
448 #self.pokebysym("MDMCFG4" , 0xf6)
449 #self.pokebysym("MDMCFG3" , 0x83)
452 #print "Warning: Default to 4.8kbaud.";
453 #self.pokebysym("MDMCFG4" , 0xf7)
454 #self.pokebysym("MDMCFG3" , 0x83)
456 #print "Warning: Default to 9.6kbaud.";
459 self.pokebysym("MDMCFG4" , 0xf8)
460 self.pokebysym("MDMCFG3" , 0x83)
461 self.pokebysym("MDMCFG2" , 0x34) # OOK, carrier-sense, no-manchester
463 #Kind aright for keeloq
464 print "Warning: Guessing baud rate.";
465 #self.pokebysym("MDMCFG4" , 0xf6)
466 #self.pokebysym("MDMCFG3" , 0x93)
467 #self.pokebysym("MDMCFG2" , 0x3C) # OOK, carrier-sense, manchester
469 self.pokebysym("MDMCFG1" , 0x00) # Modem configuration.
470 self.pokebysym("MDMCFG0" , 0xF8) # Modem configuration.
471 self.pokebysym("CHANNR" , 0x00) # Channel number.
473 self.pokebysym("FREND1" , 0x56) # Front end RX configuration.
474 self.pokebysym("FREND0" , 0x11) # Front end RX configuration.
475 self.pokebysym("MCSM0" , 0x18) # Main Radio Control State Machine configuration.
476 #self.pokebysym("FOCCFG" , 0x1D) # Frequency Offset Compensation Configuration.
477 #self.pokebysym("BSCFG" , 0x1C) # Bit synchronization Configuration.
479 #self.pokebysym("AGCCTRL2" , 0xC7) # AGC control.
480 #self.pokebysym("AGCCTRL1" , 0x00) # AGC control.
481 #self.pokebysym("AGCCTRL0" , 0xB2) # AGC control.
483 self.pokebysym("TEST2" , 0x81) # Various test settings.
484 self.pokebysym("TEST1" , 0x35) # Various test settings.
485 self.pokebysym("TEST0" , 0x0B) # Various test settings.
486 self.pokebysym("PA_TABLE0", 0xc2) # Max output power.
487 self.pokebysym("PKTCTRL1" , 0x04) # Packet automation control, w/ lqi
488 #self.pokebysym("PKTCTRL1" , 0x00) # Packet automation control. w/o lqi
489 #self.pokebysym("PKTCTRL0" , 0x05) # Packet automation control, w/ checksum.
490 self.pokebysym("PKTCTRL0" , 0x00) # Packet automation control, w/o checksum, fixed length
491 self.pokebysym("ADDR" , 0x01) # Device address.
492 self.pokebysym("PKTLEN" , 0xFF) # Packet length.
494 self.pokebysym("SYNC1",0xD3);
495 self.pokebysym("SYNC0",0x91);
497 def config_simpliciti(self,band="none"):
498 self.pokebysym("FSCTRL1" , 0x0C) #08 # Frequency synthesizer control.
499 self.pokebysym("FSCTRL0" , 0x00) # Frequency synthesizer control.
501 #Don't change these while the radio is active.
502 self.pokebysym("FSCAL3" , 0xEA) # Frequency synthesizer calibration.
503 self.pokebysym("FSCAL2" , 0x2A) # Frequency synthesizer calibration.
504 self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
505 self.pokebysym("FSCAL0" , 0x1F) # Frequency synthesizer calibration.
507 if band=="ismeu" or band=="eu":
508 self.pokebysym("FREQ2" , 0x21) # Frequency control word, high byte.
509 self.pokebysym("FREQ1" , 0x71) # Frequency control word, middle byte.
510 self.pokebysym("FREQ0" , 0x7a) # Frequency control word, low byte.
511 elif band=="ismus" or band=="us":
512 self.pokebysym("FREQ2" , 0x22) # Frequency control word, high byte.
513 self.pokebysym("FREQ1" , 0xB1) # Frequency control word, middle byte.
514 self.pokebysym("FREQ0" , 0x3B) # Frequency control word, low byte.
515 elif band=="ismlf" or band=="lf":
516 self.pokebysym("FREQ2" , 0x10) # Frequency control word, high byte.
517 self.pokebysym("FREQ1" , 0xB0) # Frequency control word, middle byte.
518 self.pokebysym("FREQ0" , 0x71) # Frequency control word, low byte.
522 #Got a frequency, not a band.
523 self.RF_setfreq(eval(band));
524 self.pokebysym("MDMCFG4" , 0x7B) # Modem configuration.
525 self.pokebysym("MDMCFG3" , 0x83) # Modem configuration.
526 self.pokebysym("MDMCFG2" , 0x13) # Modem configuration.
527 self.pokebysym("MDMCFG1" , 0x22) # Modem configuration.
528 self.pokebysym("MDMCFG0" , 0xF8) # Modem configuration.
529 if band=="ismus" or band=="us":
530 self.pokebysym("CHANNR" , 20) # Channel number.
532 self.pokebysym("CHANNR" , 0x00) # Channel number.
533 self.pokebysym("DEVIATN" , 0x42) # Modem deviation setting (when FSK modulation is enabled).
535 self.pokebysym("FREND1" , 0xB6) # Front end RX configuration.
536 self.pokebysym("FREND0" , 0x10) # Front end RX configuration.
537 self.pokebysym("MCSM0" , 0x18) # Main Radio Control State Machine configuration.
538 self.pokebysym("FOCCFG" , 0x1D) # Frequency Offset Compensation Configuration.
539 self.pokebysym("BSCFG" , 0x1C) # Bit synchronization Configuration.
541 self.pokebysym("AGCCTRL2" , 0xC7) # AGC control.
542 self.pokebysym("AGCCTRL1" , 0x00) # AGC control.
543 self.pokebysym("AGCCTRL0" , 0xB2) # AGC control.
545 self.pokebysym("TEST2" , 0x81) # Various test settings.
546 self.pokebysym("TEST1" , 0x35) # Various test settings.
547 self.pokebysym("TEST0" , 0x09) # Various test settings.
548 self.pokebysym("PA_TABLE0", 0xc0) # Max output power.
549 self.pokebysym("PKTCTRL1" , 0x04) # Packet automation control, w/ lqi
550 #self.pokebysym("PKTCTRL1" , 0x00) # Packet automation control. w/o lqi
551 self.pokebysym("PKTCTRL0" , 0x05) # Packet automation control, w/ checksum.
552 #self.pokebysym("PKTCTRL0" , 0x00) # Packet automation control, w/o checksum, fixed length
553 self.pokebysym("ADDR" , 0x01) # Device address.
554 self.pokebysym("PKTLEN" , 0xFF) # Packet length.
556 self.pokebysym("SYNC1",0xD3);
557 self.pokebysym("SYNC0",0x91);
559 def RF_carrier(self):
560 """Hold a carrier wave on the present frequency."""
562 self.CC1110_crystal(); #FIXME, '1110 specific.
568 self.config_simpliciti();
570 #Don't change these while the radio is active.
571 #self.pokebysym("FSCAL3" , 0xA9) # Frequency synthesizer calibration.
572 #self.pokebysym("FSCAL2" , 0x0A) # Frequency synthesizer calibration.
573 #self.pokebysym("FSCAL1" , 0x00) # Frequency synthesizer calibration.
574 #self.pokebysym("FSCAL0" , 0x11) # Frequency synthesizer calibration.
577 #self.pokebysym("PA_TABLE0", 0xFF) # PA output power setting.
579 #This is what drops to OOK.
580 #Comment to keep GFSK, might be better at jamming.
581 self.pokebysym("MDMCFG4" , 0x86) # Modem configuration.
582 self.pokebysym("MDMCFG3" , 0x83) # Modem configuration.
583 self.pokebysym("MDMCFG2" , 0x30) # Modem configuration.
584 self.pokebysym("MDMCFG1" , 0x22) # Modem configuration.
585 self.pokebysym("MDMCFG0" , 0xF8) # Modem configuration.
587 self.pokebysym("SYNC1",0xAA);
588 self.pokebysym("SYNC0",0xAA);
590 #while ((MARCSTATE & MARCSTATE_MARC_STATE) != MARC_STATE_TX);
593 while((state!=0x13)):
594 self.pokebyte(RFST,0x03); #RFST=RFST_STX
596 state=self.peekbysym("MARCSTATE")&0x1F;
597 #print "state=%02x" % state;
598 print "Holding a carrier on %f MHz." % (self.RF_getfreq()/10**6);
602 def RF_getsmac(self):
603 """Return the source MAC address."""
605 #Register 0A is RX_ADDR_P0, five bytes.
606 mac=self.peekbysym("ADDR");
608 def RF_setsmac(self,mac):
609 """Set the source MAC address."""
610 self.pokebysym("ADDR",mac);
612 def RF_gettmac(self):
613 """Return the target MAC address."""
615 def RF_settmac(self,mac):
616 """Set the target MAC address."""
618 def RF_rxpacket(self):
619 """Get a packet from the radio. Returns None if none is waiting."""
620 self.shellcodefile("rxpacket.ihx");
621 len=self.peek8(0xFE00,"xdata");
622 return self.peekblock(0xFE00,len+3,"data");
623 def RF_txpacket(self,packet):
624 """Transmit a packet. Untested."""
626 self.pokeblock(0xFE00,packet,"data");
627 self.shellcodefile("txpacket.ihx");
629 def RF_txrxpacket(self,packet):
630 """Transmit a packet. Untested."""
632 self.pokeblock(0xFE00,packet,"data");
633 self.shellcodefile("txrxpacket.ihx");
634 len=self.peek8(0xFE00,"xdata");
635 return self.peekblock(0xFE00,len+3,"data");
637 def RF_getrssi(self):
638 """Returns the received signal strenght, with a weird offset."""
640 rssireg=self.symbols.get("RSSI");
641 return self.CCpeekdatabyte(rssireg)^0x80;
643 if self.verbose>0: print "RSSI reg doesn't exist.";
645 #RSSI doesn't exist on some 2.4GHz devices. Maybe RSSIL and RSSIH?
646 rssilreg=self.symbols.get("RSSIL");
647 rssil=self.CCpeekdatabyte(rssilreg);
648 rssihreg=self.symbols.get("RSSIL");
649 rssih=self.CCpeekdatabyte(rssihreg);
650 return (rssih<<8)|rssil;
652 if self.verbose>0: print "RSSIL/RSSIH regs don't exist.";
656 def SRF_loadsymbols(self):
657 ident=self.CCident();
658 chip=self.CCversions.get(ident&0xFF00);
659 dom=self.SRF_chipdom(chip,"register_definition.xml");
660 for e in dom.getElementsByTagName("registerdefinition"):
661 for f in e.childNodes:
662 if f.localName=="Register":
667 for g in f.childNodes:
668 if g.localName=="Name":
669 name=g.childNodes[0].nodeValue;
670 elif g.localName=="Address":
671 address=g.childNodes[0].nodeValue;
672 elif g.localName=="Description":
674 description=g.childNodes[0].nodeValue;
675 elif g.localName=="Bitfield":
676 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
677 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
678 self.symbols.define(eval(address),name,description,"data");
684 self.writecmd(self.APP,0x86,0,self.data);
687 def CCreleasecpu(self):
688 """Resume the CPU."""
689 self.writecmd(self.APP,0x87,0,self.data);
693 #print "Status: %s" % self.CCstatusstr();
695 #Grab ident three times, should be equal.
696 ident1=self.CCident();
697 ident2=self.CCident();
698 ident3=self.CCident();
699 if(ident1!=ident2 or ident2!=ident3):
700 print "Error, repeated ident attempts unequal."
701 print "%04x, %04x, %04x" % (ident1, ident2, ident3);
703 #Single step, printing PC.
704 print "Tracing execution at startup."
705 for i in range(1,15):
707 byte=self.CCpeekcodebyte(i);
708 #print "PC=%04x, %02x" % (pc, byte);
711 print "Verifying that debugging a NOP doesn't affect the PC."
712 for i in range(1,15):
714 self.CCdebuginstr([0x00]);
715 if(pc!=self.CCgetPC()):
716 print "ERROR: PC changed during CCdebuginstr([NOP])!";
718 print "Checking pokes to XRAM."
719 for i in range(0xf000,0xf020):
720 self.CCpokedatabyte(i,0xde);
721 if(self.CCpeekdatabyte(i)!=0xde):
722 print "Error in XDATA at 0x%04x" % i;
724 #print "Status: %s." % self.CCstatusstr();
730 """Move the FET into the Chipcon 8051 application."""
731 #print "Initializing Chipcon.";
732 self.writecmd(self.APP,0x10,0,self.data);
733 def CCrd_config(self):
734 """Read the config register of a Chipcon."""
735 self.writecmd(self.APP,0x82,0,self.data);
736 return ord(self.data[0]);
737 def CCwr_config(self,config):
738 """Write the config register of a Chipcon."""
739 self.writecmd(self.APP,0x81,1,[config&0xFF]);
740 def CClockchip(self):
741 """Set the flash lock bit in info mem."""
742 self.writecmd(self.APP, 0x9A, 0, None);
744 """Set the flash lock bit in info mem."""
748 CCversions={0x0100:"CC1110",
754 0xA500:"CC2530", #page 57 of SWRU191B
759 CCexecbuf= {0x0100:0xF000,
769 0xFF00:0xF000} #missing
770 CCpagesizes={0x01: 1024, #"CC1110",
771 0x11: 1024, #"CC1111",
772 0x85: 2048, #"CC2430",
773 0x89: 2048, #"CC2431",
774 0x81: 1024, #"CC2510",
775 0x91: 1024, #"CC2511",
776 0xA5: 2048, #"CC2530", #page 57 of SWRU191B
777 0xB5: 2048, #"CC2531",
778 0x95: 2048, #"CC2533",
779 0x8D: 2048, #"CC2540",
780 0xFF: 0 } #"CCmissing"};
781 def infostring(self):
782 return self.CCidentstr();
783 def CCidentstr(self):
784 ident=self.CCident();
785 chip=self.CCversions.get(ident&0xFF00);
786 pagesize=self.CCpagesizes.get(ident>0xFF);
788 return "%s/r%0.4x/ps0x%0.4x" % (chip, ident, pagesize);
790 return "%04x" % ident;
792 """Get a chipcon's ID."""
793 self.writecmd(self.APP,0x8B,0,None);
794 chip=ord(self.data[0]);
795 rev=ord(self.data[1]);
796 return (chip<<8)+rev;
797 def CCpagesize(self):
798 """Get a chipcon's ID."""
799 self.writecmd(self.APP,0x8B,0,None);
800 chip=ord(self.data[0]);
801 size=self.CCpagesizes.get(chip);
803 print "ERROR: Pagesize undefined.";
804 print "chip=%0.4x" %chip;
809 return self.CCgetPC();
811 """Get a chipcon's PC."""
812 self.writecmd(self.APP,0x83,0,None);
813 hi=ord(self.data[0]);
814 lo=ord(self.data[1]);
816 def CCcmd(self,phrase):
817 self.writecmd(self.APP,0x00,len(phrase),phrase);
818 val=ord(self.data[0]);
819 print "Got %02x" % val;
821 def CCdebuginstr(self,instr):
822 self.writecmd(self.APP,0x88,len(instr),instr);
823 return ord(self.data[0]);
824 #def peekblock(self,adr,length,memory="vn"):
825 # """Return a block of data, broken"""
826 # data=[adr&0xff, (adr&0xff00)>>8,
827 # length&0xFF,(length&0xFF00)>>8];
828 # self.writecmd(self.APP,0x91,4,data);
829 # return [ord(x) for x in self.data]
830 def peek8(self,address, memory="code"):
831 if(memory=="code" or memory=="flash" or memory=="vn"):
832 return self.CCpeekcodebyte(address);
833 elif(memory=="data" or memory=="xdata" or memory=="ram"):
834 return self.CCpeekdatabyte(address);
835 elif(memory=="idata" or memory=="iram"):
836 return self.CCpeekirambyte(address);
837 print "%s is an unknown memory." % memory;
839 def CCpeekcodebyte(self,adr):
840 """Read the contents of code memory at an address."""
841 self.data=[adr&0xff, (adr&0xff00)>>8];
842 self.writecmd(self.APP,0x90,2,self.data);
843 return ord(self.data[0]);
844 def CCpeekdatabyte(self,adr):
845 """Read the contents of data memory at an address."""
846 self.data=[adr&0xff, (adr&0xff00)>>8];
847 self.writecmd(self.APP,0x91, 2, self.data);
848 return ord(self.data[0]);
849 def CCpeekirambyte(self,adr):
850 """Read the contents of IRAM at an address."""
851 self.data=[adr&0xff];
852 self.writecmd(self.APP,0x02, 1, self.data);
853 return ord(self.data[0]);
854 def CCpeekiramword(self,adr):
855 """Read the little-endian contents of IRAM at an address."""
856 return self.CCpeekirambyte(adr)+(
857 self.CCpeekirambyte(adr+1)<<8);
858 def CCpokeiramword(self,adr,val):
859 self.CCpokeirambyte(adr,val&0xff);
860 self.CCpokeirambyte(adr+1,(val>>8)&0xff);
861 def CCpokeirambyte(self,adr,val):
862 """Write the contents of IRAM at an address."""
863 self.data=[adr&0xff, val&0xff];
864 self.writecmd(self.APP,0x02, 2, self.data);
865 return ord(self.data[0]);
866 def pokebyte(self,adr,val,mem="xdata"):
867 self.CCpokedatabyte(adr,val);
868 def CCpokedatabyte(self,adr,val):
869 """Write a byte to data memory."""
870 self.data=[adr&0xff, (adr&0xff00)>>8, val];
871 self.writecmd(self.APP, 0x92, 3, self.data);
872 return ord(self.data[0]);
873 def CCchiperase(self):
874 """Erase all of the target's memory."""
875 self.writecmd(self.APP,0x80,0,None);
877 """Erase all of the target's memory."""
882 """Check the status."""
883 self.writecmd(self.APP,0x84,0,None);
884 return ord(self.data[0])
886 CCstatusbits={0x80 : "erase_busy",
890 0x08 : "halt_status",
895 CCconfigbits={0x20 : "soft_power_mode", #new for CC2530
898 0x02 : "timer_suspend",
899 0x01 : "sel_flash_info_page" #stricken from CC2530
903 """Check the status as a string."""
904 status=self.CCstatus();
909 str="%s %s" %(self.CCstatusbits[i],str);
913 """Start debugging."""
915 self.writecmd(self.APP,0x20,0,self.data);
916 ident=self.CCident();
917 if ident==0xFFFF or ident==0x0000:
918 self.writecmd(self.APP,0x20,0,self.data);
919 ident=self.CCident();
922 #print "Target identifies as %s." % ident;
923 #print "Status: %s." % self.status();
926 #Get SmartRF Studio regs if they exist.
930 """Stop debugging."""
931 self.writecmd(self.APP,0x21,0,self.data);
932 def CCstep_instr(self):
933 """Step one instruction."""
934 self.writecmd(self.APP,0x89,0,self.data);
935 def CCeraseflashbuffer(self):
936 """Erase the 2kB flash buffer"""
937 self.writecmd(self.APP,0x99);
938 def CCflashpage(self,adr):
939 """Flash 2kB a page of flash from 0xF000 in XDATA"""
944 print "Flashing buffer to 0x%06x" % adr;
945 self.writecmd(self.APP,0x95,4,data);
947 def setsecret(self,value):
948 """Set a secret word for later retreival. Used by glitcher."""
950 pagelen = self.CCpagesize(); #Varies by chip.
951 print "page=%04x, pagelen=%04x" % (page,pagelen);
953 self.CCeraseflashbuffer();
954 print "Setting secret to %x" % value;
955 self.CCpokedatabyte(0xF000,value);
956 self.CCpokedatabyte(0xF800,value);
957 print "Setting secret to %x==%x" % (value,
958 self.CCpeekdatabyte(0xf000));
960 print "code[0]=%x" % self.CCpeekcodebyte(0);
962 """Get a secret word. Used by glitcher."""
963 secret=self.CCpeekcodebyte(0);
964 #print "Got secret %02x" % secret;
967 #FIXME: This is CC1110-specific and duplicates functionality of
968 # SmartRF7 integration.
1086 def getSPR(self,args=[]):
1087 """Get special function registers."""
1088 print "Special Function Registers:"
1091 print " %-8s : 0x%0.2x"%(e,self.CCpeekcodebyte(self.CCspecfuncregs[e]))
1093 for e in self.CCspecfuncregs.keys():
1094 print " %-8s : 0x%0.2x"%(e,self.CCpeekcodebyte(self.CCspecfuncregs[e]))
1096 def dump(self,file,start=0,stop=0xffff):
1097 """Dump an intel hex file from code memory."""
1098 print "Dumping code from %04x to %04x as %s." % (start,stop,file);
1102 h[i]=self.CCpeekcodebyte(i);
1104 print "Dumped %04x."%i;
1105 h.write_hex_file(file); #buffer to disk.
1107 h.write_hex_file(file);
1109 def flash(self,file):
1110 """Flash an intel hex file to code memory."""
1111 print "Flashing %s" % file;
1115 pagelen = self.CCpagesize(); #Varies by chip.
1117 #print "page=%04x, pagelen=%04x" % (page,pagelen);
1121 #Wipe the RAM buffer for the next flash page.
1122 self.CCeraseflashbuffer();
1123 for i in h._buf.keys():
1124 while(i>=page+pagelen):
1126 self.CCflashpage(page);
1127 #client.CCeraseflashbuffer();
1129 print "Flashed page at %06x" % page
1132 #Place byte into buffer.
1133 self.CCpokedatabyte(0xF000+i-page,
1137 print "Buffering %04x toward %06x" % (i,page);
1139 self.CCflashpage(page);
1140 print "Flashed final page at %06x" % page;