644612c4ca893b75c8ef2236cfe657e098c0ba2f
[goodfet] / client / GoodFETCC.py
1 #!/usr/bin/env python
2 # GoodFET Client Library
3
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # This code is being rewritten and refactored.  You've been warned!
7
8 import sys;
9 import binascii;
10
11 from GoodFET import GoodFET;
12 from intelhex import IntelHex;
13
14 import xml.dom.minidom, time, os;
15
16 class GoodFETCC(GoodFET):
17     """A GoodFET variant for use with Chipcon 8051 Zigbee SoC."""
18     APP=0x30;
19     
20     smartrfpath=None;
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;
29             
30         if self.smartrfpath==None:
31             self.smartrfpath="/opt/smartrf7";
32         
33     haveloadedsymbols=False;
34     def loadsymbols(self):
35         if self.haveloadedsymbols:
36             return;
37         try:
38             self.SRF_loadsymbols();
39             self.haveloadedsymbols=True;
40         except:
41             ident=self.CCident();
42             if ident==0x0000 or ident==0xFFFF:
43                 print "Chip ID is 0x%04x, implying a wiring problem." % ident;
44             else:
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)
51         
52     def CMDrs(self,args=[]):
53         """Chip command to grab the radio state."""
54         #try:
55         self.SRF_radiostate();
56         #except:
57         #    print "Error printing radio state.";
58         #    print "SmartRF not found at %s." % self.smartrfpath;
59     def SRF_bitfieldstr(self,bf):
60         name="unused";
61         start=0;
62         stop=0;
63         access="";
64         reset="0x00";
65         description="";
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);
71
72     def SRF_radiostate(self):
73         ident=self.CCident();
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":
81                     name="unknownreg";
82                     address="0xdead";
83                     description="";
84                     bitfields="";
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":
91                             if g.childNodes:
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();
99
100     def SRF_radiostate_select(self,args=[]):
101         lreg = []
102         ident=self.CCident();
103         chip=self.CCversions.get(ident&0xFF00);
104         dom=self.SRF_chipdom(chip,"register_definition.xml");
105         for reg in args:
106             if reg.lower() == "help":
107                 lreg = "help"
108                 break
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":
115                     name="unknownreg";
116                     address="0xdead";
117                     description="";
118                     bitfields="";
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":
125                             if g.childNodes:
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);
130                     if lreg == "help":
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();
136
137     def RF_setfreq(self,frequency):
138         """Set the frequency in Hz."""
139         #FIXME CC1110 specific
140         #Some frequencies fail, probably and FSCAL thing.
141         
142         hz=frequency;
143         freq=int(hz/396.728515625);
144         
145         freq0=freq&0xFF;
146         freq1=(freq&0xFF00)>>8;
147         freq2=(freq&0xFF0000)>>16;
148         
149         self.pokebysym("FREQ2",freq2);
150         self.pokebysym("FREQ1",freq1);
151         self.pokebysym("FREQ0",freq0);
152         
153         self.pokebysym("TEST1",0x31);
154         self.pokebysym("TEST0",0x09);
155         
156         
157         #self.pokebysym("FSCAL2" ,   0x2A);  #above mid
158         self.pokebysym("FSCAL2" ,   0x0A);  #beneath mid
159         
160         #self.CC_RFST_CAL(); #SCAL
161         #time.sleep(1);
162     
163         
164     def RF_getfreq(self):
165         """Get the frequency in Hz."""
166         #FIXME CC1110 specific
167         
168         #return (2400+self.peek(0x05))*10**6
169         #self.poke(0x05,chan);
170         
171         #freq2=self.CCpeekdatabyte(0xdf09);
172         #freq1=self.CCpeekdatabyte(0xdf0a);
173         #freq0=self.CCpeekdatabyte(0xdf0b);
174         freq=0;
175         try:
176             freq2=self.peekbysym("FREQ2");
177             freq1=self.peekbysym("FREQ1");
178             freq0=self.peekbysym("FREQ0");
179             freq=(freq2<<16)+(freq1<<8)+freq0;
180         except:
181             freq=0;
182             
183         hz=freq*396.728515625;
184         
185         return hz;
186     
187     def RF_getchannel(self):
188         """Get the hex channel."""
189         #FIXME CC1110 specific
190         freq=0;
191         try:
192             freq2=self.peekbysym("FREQ2");
193             freq1=self.peekbysym("FREQ1");
194             freq0=self.peekbysym("FREQ0");
195             freq=(freq2<<16)+(freq1<<8)+freq0;
196         except:
197             freq=0;
198             
199         return freq;
200     
201     
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.
206         
207         if self.lastshellcode!=filename or alwaysreload>0:
208             self.lastshellcode=filename;
209             file=__file__;
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;
214         
215             #Load the shellcode.
216             h=IntelHex(filename);
217             for i in h._buf.keys():
218                 self.CCpokedatabyte(i,h[i]);
219         #Execute it.
220         self.CCdebuginstr([0x02, 0xf0, 0x00]); #ljmp 0xF000
221         self.resume();
222         while wait>0 and (0==self.CCstatus()&0x20):
223             a=1;
224             #print "Waiting for shell code to return.";
225         return;
226     def ishalted(self):
227         return self.CCstatus()&0x20;
228     def shellcode(self,code,wait=1):
229         """Copy a block of code into RAM and execute it."""
230         i=0;
231         ram=0xF000;
232         for byte in code:
233             self.pokebyte(0xF000+i,byte);
234             i=i+1;
235         #print "Code loaded, executing."
236         self.CCdebuginstr([0x02, 0xf0, 0x00]); #ljmp 0xF000
237         self.resume();
238         while wait>0 and (0==self.CCstatus()&0x20):
239             a=1;
240             #time.sleep(0.1);
241             #print "Waiting for shell code to return.";
242         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
246               #one:
247               0xE5, 0xBE,       #mov a,SLEEP
248               0x30, 0xE6, 0xFB, #jnb acc.6, back
249               0x53, 0xc6, 0xB8, #anl CLKCON, #0xB8
250               #two
251               0xE5, 0xC6,       #mov a,CLKCON
252               0x20, 0xE6, 0xFB, #jb acc.6, two
253               0x43, 0xBE, 0x04, #orl SLEEP, #0x04
254               0xA5,             #HALT
255               ];
256         self.shellcode(code);
257         
258         #Slower to load, but produced from C.
259         #self.shellcodefile("crystal.ihx");
260         return;
261     def RF_idle(self):
262         """Move the radio to its idle state."""
263         self.CC_RFST_IDLE();
264         return;
265     
266     #Chipcon RF strobes.  CC1110 specific
267     RFST_IDLE=0x04;
268     RFST_RX=0x02;
269     RFST_TX=0x03;
270     RFST_CAL=0x01;
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):
284         RFST=0xDFE1
285         self.pokebyte(RFST,state); #Return to idle state.
286         return;
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.
290
291         self.pokebysym("FSCTRL1"  , 0x08)   # Frequency synthesizer control.
292         self.pokebysym("FSCTRL0"  , 0x00)   # Frequency synthesizer control.
293         
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.
299         
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.
315         elif band=="none":
316             pass;
317         else:
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
327         
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
335         
336         self.pokebysym("AGCCTRL2" , 0xC7)   # AGC control.
337         self.pokebysym("AGCCTRL1" , 0x00)   # AGC control.
338         self.pokebysym("AGCCTRL0" , 0xB2)   # AGC control.
339         
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.
350         
351         #Sync word hack
352         self.pokebysym("SYNC1",0x83);
353         self.pokebysym("SYNC0",0xFE);
354         return;
355     def config_iclicker(self,band="lf"):
356         #Mike Ossmann figured most of this out, with help from neighbors.
357         
358         self.pokebysym("FSCTRL1"  , 0x06)   # Frequency synthesizer control.
359         self.pokebysym("FSCTRL0"  , 0x00)   # Frequency synthesizer control.
360         
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)
366         
367         if band=="ismeu" or band=="eu":
368             print "The EU band is unknown.";
369         elif band=="ismus" or band=="us":
370             #905.5MHz
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."
376         elif band=="none":
377             pass;
378         else:
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
385         
386         #15/16 sync
387         #self.pokebysym("MDMCFG2"  , 0x01)
388         #16/16 sync
389         self.pokebysym("MDMCFG2"  , 0x02)
390         
391         self.pokebysym("MDMCFG1"  , 0x03)
392         self.pokebysym("MDMCFG0"  , 0x3b)
393         
394         self.pokebysym("CHANNR"   , 0x2e)   # Channel zero.
395         
396         #self.pokebysym("DEVIATN"  , 0x71)  # 118.5
397         self.pokebysym("DEVIATN"  , 0x72)   # 253.9 kHz deviation
398         
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)
404         
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.
411         
412         self.pokebysym("PKTLEN"   , 0x09)   # Packet length.
413         
414         self.pokebysym("SYNC1",0xB0);
415         self.pokebysym("SYNC0",0xB0);
416         self.pokebysym("ADDR", 0xB0);
417         return;
418     def config_ook(self,band="none"):
419         self.pokebysym("FSCTRL1"  , 0x0C) #08   # Frequency synthesizer control.
420         self.pokebysym("FSCTRL0"  , 0x00)   # Frequency synthesizer control.
421         
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.
427         
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.
440         elif band=="none":
441             pass;
442         else:
443             #Got a frequency, not a band.
444             self.RF_setfreq(eval(band));
445         
446         #data rate
447         #~1
448         #self.pokebysym("MDMCFG4"  , 0x85)
449         #self.pokebysym("MDMCFG3"  , 0x83)
450         #0.5
451         #self.pokebysym("MDMCFG4"  , 0xf4)
452         #self.pokebysym("MDMCFG3"  , 0x43)
453         #2.4
454         #self.pokebysym("MDMCFG4"  , 0xf6)
455         #self.pokebysym("MDMCFG3"  , 0x83)
456         
457         #4.8 kbaud
458         #print "Warning: Default to 4.8kbaud.";
459         #self.pokebysym("MDMCFG4"  , 0xf7)
460         #self.pokebysym("MDMCFG3"  , 0x83)
461         #9.6 kbaud
462         #print "Warning: Default to 9.6kbaud.";
463         #
464         
465         self.pokebysym("MDMCFG4"  , 0xf8)
466         self.pokebysym("MDMCFG3"  , 0x83)
467         self.pokebysym("MDMCFG2"  , 0x34)   # OOK, carrier-sense, no-manchester
468         
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
474         
475         self.pokebysym("MDMCFG1"  , 0x00)   # Modem configuration.
476         self.pokebysym("MDMCFG0"  , 0xF8)   # Modem configuration.
477         self.pokebysym("CHANNR"   , 0x00)   # Channel number.
478         
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.
484         
485         #self.pokebysym("AGCCTRL2" , 0xC7)   # AGC control.
486         #self.pokebysym("AGCCTRL1" , 0x00)   # AGC control.
487         #self.pokebysym("AGCCTRL0" , 0xB2)   # AGC control.
488         
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.
499         
500         self.pokebysym("SYNC1",0xD3);
501         self.pokebysym("SYNC0",0x91);
502         
503     def config_simpliciti(self,band="none"):
504         self.pokebysym("FSCTRL1"  , 0x0C) #08   # Frequency synthesizer control.
505         self.pokebysym("FSCTRL0"  , 0x00)   # Frequency synthesizer control.
506         
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.
512         
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.
525         elif band=="none":
526             band="none";
527         else:
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.
537         else:
538             self.pokebysym("CHANNR"   , 0x00)   # Channel number.
539         self.pokebysym("DEVIATN"  , 0x42)   # Modem deviation setting (when FSK modulation is enabled).
540         
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.
546         
547         self.pokebysym("AGCCTRL2" , 0xC7)   # AGC control.
548         self.pokebysym("AGCCTRL1" , 0x00)   # AGC control.
549         self.pokebysym("AGCCTRL0" , 0xB2)   # AGC control.
550         
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.
561         
562         self.pokebysym("SYNC1",0xD3);
563         self.pokebysym("SYNC0",0x91);
564         
565     def RF_carrier(self):
566         """Hold a carrier wave on the present frequency."""
567         
568         self.CC1110_crystal(); #FIXME, '1110 specific.
569         self.RF_idle();
570         
571         
572         RFST=0xDFE1;
573         
574         self.config_simpliciti();
575         
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.
581         
582         #Ramp up the power.
583         #self.pokebysym("PA_TABLE0", 0xFF)   # PA output power setting.
584         
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.
592         
593         self.pokebysym("SYNC1",0xAA);
594         self.pokebysym("SYNC0",0xAA);
595         
596         #while ((MARCSTATE & MARCSTATE_MARC_STATE) != MARC_STATE_TX); 
597         state=0;
598         
599         while((state!=0x13)):
600             self.pokebyte(RFST,0x03); #RFST=RFST_STX
601             time.sleep(0.1);
602             state=self.peekbysym("MARCSTATE")&0x1F;
603             #print "state=%02x" % state;
604         print "Holding a carrier on %f MHz." % (self.RF_getfreq()/10**6);
605         
606         return;
607             
608     def RF_getsmac(self):
609         """Return the source MAC address."""
610         
611         #Register 0A is RX_ADDR_P0, five bytes.
612         mac=self.peekbysym("ADDR");
613         return mac;
614     def RF_setsmac(self,mac):
615         """Set the source MAC address."""
616         self.pokebysym("ADDR",mac);
617         return 0;
618     def RF_gettmac(self):
619         """Return the target MAC address."""
620         return 0;
621     def RF_settmac(self,mac):
622         """Set the target MAC address."""
623         return 0;
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."""
631         
632         self.pokeblock(0xFE00,packet,"data");
633         self.shellcodefile("txpacket.ihx");
634         return;
635     def RF_txrxpacket(self,packet):
636         """Transmit a packet.  Untested."""
637         
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");
642
643     def RF_getrssi(self):
644         """Returns the received signal strenght, with a weird offset."""
645         try:
646             rssireg=self.symbols.get("RSSI");
647             return self.CCpeekdatabyte(rssireg)^0x80;
648         except:
649             if self.verbose>0: print "RSSI reg doesn't exist.";
650         try:
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;
657         except:
658             if self.verbose>0: print "RSSIL/RSSIH regs don't exist.";
659         
660         return 0;
661     
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":
669                     name="unknownreg";
670                     address="0xdead";
671                     description="";
672                     bitfields="";
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":
679                             if g.childNodes:
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");
685     def halt(self):
686         """Halt the CPU."""
687         self.CChaltcpu();
688     def CChaltcpu(self):
689         """Halt the CPU."""
690         self.writecmd(self.APP,0x86,0,self.data);
691     def resume(self):
692         self.CCreleasecpu();
693     def CCreleasecpu(self):
694         """Resume the CPU."""
695         self.writecmd(self.APP,0x87,0,self.data);
696     def test(self):
697         self.CCreleasecpu();
698         self.CChaltcpu();
699         #print "Status: %s" % self.CCstatusstr();
700         
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);
708
709         #Single step, printing PC.
710         print "Tracing execution at startup."
711         for i in range(1,15):
712             pc=self.CCgetPC();
713             byte=self.CCpeekcodebyte(i);
714             #print "PC=%04x, %02x" % (pc, byte);
715             self.CCstep_instr();
716         
717         print "Verifying that debugging a NOP doesn't affect the PC."
718         for i in range(1,15):
719             pc=self.CCgetPC();
720             self.CCdebuginstr([0x00]);
721             if(pc!=self.CCgetPC()):
722                 print "ERROR: PC changed during CCdebuginstr([NOP])!";
723         
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;
729         
730         #print "Status: %s." % self.CCstatusstr();
731         #Exit debugger
732         self.stop();
733         print "Done.";
734
735     def setup(self):
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);
749     def lock(self):
750         """Set the flash lock bit in info mem."""
751         self.CClockchip();
752     
753
754     CCversions={0x0100:"cc1110",
755                 0x1100:"cc1111",
756                 0x8500:"cc2430",
757                 0x8900:"cc2431",
758                 0x8100:"cc2510",
759                 0x9100:"cc2511",
760                 0xA500:"cc2530", #page 57 of SWRU191B
761                 0xB500:"cc2531",
762                 0x9500:"cc2533",
763                 0x8D00:"cc2540",
764                 0xFF00:"CCmissing"};
765     execbuf=None;
766     CCexecbuf= {0x0100:0xF000,
767                 0x1100:0xF000,
768                 0x8500:0xF000,
769                 0x8900:0xF000,
770                 0x8100:0xF000,
771                 0x9100:0xF000,
772                 0xA500:0x0000, #CC2530
773                 0xB500:0x8000,
774                 0x9500:0x8000,
775                 0x8D00:0x8000,
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",
787                  0xFF: None}
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;
796         
797         try:
798             return "%s/r%0.4x/ps0x%0.4x" % (chip, ident, pagesize); 
799         except:
800             return "%04x" % ident;
801     def CCident(self):
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);
812         if(size<10):
813             print "ERROR: Pagesize undefined.";
814             print "chip=%0.4x" %chip;
815             sys.exit(1);
816             #return 2048;
817         return size;
818     def getpc(self):
819         return self.CCgetPC();
820     def CCgetPC(self):
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]);
825         return (hi<<8)+lo;
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;
830         return 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;
848         return 0xdead;
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);
886     def erase(self):
887         """Erase all of the target's memory."""
888         self.CCchiperase();
889         self.start();
890     
891     def CCstatus(self):
892         """Check the status."""
893         self.writecmd(self.APP,0x84,0,None);
894         return ord(self.data[0])
895     #Same as CC2530
896     CCstatusbits={0x80 : "erase_busy",
897                   0x40 : "pcon_idle",
898                   0x20 : "cpu_halted",
899                   0x10 : "pm0",
900                   0x08 : "halt_status",
901                   0x04 : "locked",
902                   0x02 : "oscstable",
903                   0x01 : "overflow"
904                   };
905     CCconfigbits={0x20 : "soft_power_mode",   #new for CC2530
906                   0x08 : "timers_off",
907                   0x04 : "dma_pause",
908                   0x02 : "timer_suspend",
909                   0x01 : "sel_flash_info_page" #stricken from CC2530
910                   };
911                   
912     def status(self):
913         """Check the status as a string."""
914         status=self.CCstatus();
915         str="";
916         i=1;
917         while i<0x100:
918             if(status&i):
919                 str="%s %s" %(self.CCstatusbits[i],str);
920             i*=2;
921         return str;
922     def start(self):
923         """Start debugging."""
924         ident=0x0000;
925         #while ident==0xFFFF or ident==0x0000:
926         self.setup();
927         self.writecmd(self.APP,0x20,0,self.data);
928         identa=self.CCident();
929         self.CCidentstr();
930         
931         ident=self.CCident();
932         #Get SmartRF Studio regs if they exist.
933         self.loadsymbols(); 
934         #print "Status: %s" % self.status();
935     def stop(self):
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"""
946         data=[adr&0xFF,
947               (adr>>8)&0xFF,
948               (adr>>16)&0xFF,
949               (adr>>24)&0xFF];
950         print "Flashing buffer to 0x%06x" % adr;
951         self.writecmd(self.APP,0x95,4,data);
952     
953     def setsecret(self,value):
954         """Set a secret word for later retreival.  Used by glitcher."""
955         page = 0x0000;
956         pagelen = self.CCpagesize(); #Varies by chip.
957         print "page=%04x, pagelen=%04x" % (page,pagelen);
958         
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));
965         self.CCflashpage(0);
966         print "code[0]=%x" % self.CCpeekcodebyte(0);
967     def getsecret(self):
968         """Get a secret word.  Used by glitcher."""
969         secret=self.CCpeekcodebyte(0);
970         #print "Got secret %02x" % secret;
971         return secret;
972     
973     #FIXME: This is CC1110-specific and duplicates functionality of 
974     #       SmartRF7 integration.
975     CCspecfuncregs={
976         'P0':0x80,
977         'SP':0x81,
978         'DPL0':0x82,
979         'DPH0':0x83,
980         'DPL1':0x84,
981         'DPH1':0x85,
982         'U0CSR':0x86,
983         'PCON':0x87,
984         'TCON':0x88,
985         'P0IFG':0x89,
986         'P1IFG':0x8A,
987         'P2IFG':0x8B,
988         'PICTL':0x8C,
989         'P1IEN':0x8D,
990         'P0INP':0x8F,
991         'P1':0x90,
992         'RFIM':0x91,
993         'DPS':0x92,
994         'MPAGE':0x93,
995         'ENDIAN':0x95,
996         'S0CON':0x98,
997         'IEN2':0x9A,
998         'S1CON':0x9B,
999         'T2CT':0x9C,
1000         'T2PR':0x9D,
1001         'T2CTL':0x9E,
1002         'P2':0xA0,
1003         'WORIRQ':0xA1,
1004         'WORCTRL':0xA2,
1005         'WOREVT0':0xA3,
1006         'WOREVT1':0xA4,
1007         'WORTIME0':0xA5,
1008         'WORTIME1':0xA6,
1009         'IEN0':0xA8,
1010         'IP0':0xA9,
1011         'FWT':0xAB,
1012         'FADDRL':0xAC,
1013         'FADDRH':0xAD,
1014         'FCTL':0xAE,
1015         'FWDATA':0xAF,
1016         'ENCDI':0xB1,
1017         'ENCDO':0xB2,
1018         'ENCCS':0xB3,
1019         'ADCCON1':0xB4,
1020         'ADCCON2':0xB5,
1021         'ADCCON3':0xB6,
1022         'IEN1':0xB8,
1023         'IP1':0xB9,
1024         'ADCL':0xBA,
1025         'ADCH':0xBB,
1026         'RNDL':0xBC,
1027         'RNDH':0xBD,
1028         'SLEEP':0xBE,
1029         'IRCON':0xC0,
1030         'U0DBUF':0xC1,
1031         'U0BAUD':0xC2,
1032         'U0UCR':0xC4,
1033         'U0GCR':0xC5,
1034         'CLKCON':0xC6,
1035         'MEMCTR':0xC7,
1036         'WDCTL':0xC9,
1037         'T3CNT':0xCA,
1038         'T3CTL':0xCB,
1039         'T3CCTL0':0xCC,
1040         'T3CC0':0xCD,
1041         'T3CCTL1':0xCE,
1042         'T3CC1':0xCF,
1043         'PSW':0xD0,
1044         'DMAIRQ':0xD1,
1045         'DMA1CFGL':0xD2,
1046         'DMA1CFGH':0xD3,
1047         'DMA0CFGL':0xD4,
1048         'DMA0CFGH':0xD5,
1049         'DMAARM':0xD6,
1050         'DMAREQ':0xD7,
1051         'TIMIF':0xD8,
1052         'RFD':0xD9,
1053         'T1CC0L':0xDA,
1054         'T1CC0H':0xDB,
1055         'T1CC1L':0xDC,
1056         'T1CC1H':0xDD,
1057         'T1CC2L':0xDE,
1058         'T1CC2H':0xDF,
1059         'ACC':0xE0,
1060         'RFST':0xE1,
1061         'T1CNTL':0xE2,
1062         'T1CNTH':0xE3,
1063         'T1CTL':0xE4,
1064         'T1CCTL0':0xE5,
1065         'T1CCTL1':0xE6,
1066         'T1CCTL2':0xE7,
1067         'IRCON2':0xE8,
1068         'RFIF':0xE9,
1069         'T4CNT':0xEA,
1070         'T4CTL':0xEB,
1071         'T4CCTL0':0xEC,
1072         'T4CC0':0xED,
1073         'T4CCTL1':0xEE,
1074         'T4CC1':0xEF,
1075         'B':0xF0,
1076         'PERCFG':0xF1,
1077         'ADCCFG':0xF2,
1078         'P0SEL':0xF3,
1079         'P1SEL':0xF4,
1080         'P2SEL':0xF5,
1081         'P1INP':0xF6,
1082         'P2INP':0xF7,
1083         'U1CSR':0xF8,
1084         'U1DBUF':0xF9,
1085         'U1BAUD':0xFA,
1086         'U1UCR':0xFB,
1087         'U1GCR':0xFC,
1088         'P0DIR':0xFD,
1089         'P1DIR':0xFE,
1090         'P2DIR':0xFF
1091     }
1092     def getSPR(self,args=[]):
1093         """Get special function registers."""
1094         print "Special Function Registers:"
1095         if len(args):
1096             for e in args:
1097                 print "    %-8s : 0x%0.2x"%(e,self.CCpeekcodebyte(self.CCspecfuncregs[e]))
1098         else:
1099             for e in self.CCspecfuncregs.keys():
1100                 print "    %-8s : 0x%0.2x"%(e,self.CCpeekcodebyte(self.CCspecfuncregs[e]))
1101     
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);
1105         h = IntelHex(None);
1106         i=start;
1107         while i<=stop:
1108             h[i]=self.CCpeekcodebyte(i);
1109             if(i%0x100==0):
1110                 print "Dumped %04x."%i;
1111                 h.write_hex_file(file); #buffer to disk.
1112             i+=1;
1113         h.write_hex_file(file);
1114
1115     def flash(self,file):
1116         """Flash an intel hex file to code memory."""
1117         print "Flashing %s" % file;
1118         
1119         h = IntelHex(file);
1120         page = 0x0000;
1121         pagelen = self.CCpagesize(); #Varies by chip.
1122         
1123         #print "page=%04x, pagelen=%04x" % (page,pagelen);
1124         
1125         bcount = 0;
1126         
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):
1131                 if bcount>0:
1132                     self.CCflashpage(page);
1133                     #client.CCeraseflashbuffer();
1134                     bcount=0;
1135                     print "Flashed page at %06x" % page
1136                 page+=pagelen;
1137                     
1138             #Place byte into buffer.
1139             self.CCpokedatabyte(0xF000+i-page,
1140                                 h[i]);
1141             bcount+=1;
1142             if(i%0x100==0):
1143                 print "Buffering %04x toward %06x" % (i,page);
1144         #last page
1145         self.CCflashpage(page);
1146         print "Flashed final page at %06x" % page;
1147