Adds support for new config variable.
[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             print "SmartRF not found for this chip.";
42     def SRF_chipdom(self,chip="cc1110", doc="register_definition.xml"):
43         """Loads the chip XML definitions from SmartRF7."""
44         fn="%s/config/xml/%s/%s" % (self.smartrfpath,chip,doc);
45         #print "Opening %s" % fn;
46         return xml.dom.minidom.parse(fn)
47         
48     def CMDrs(self,args=[]):
49         """Chip command to grab the radio state."""
50         #try:
51         self.SRF_radiostate();
52         #except:
53         #    print "Error printing radio state.";
54         #    print "SmartRF not found at %s." % self.smartrfpath;
55     def SRF_bitfieldstr(self,bf):
56         name="unused";
57         start=0;
58         stop=0;
59         access="";
60         reset="0x00";
61         description="";
62         for e in bf.childNodes:
63             if e.localName=="Name" and e.childNodes: name= e.childNodes[0].nodeValue;
64             elif e.localName=="Start": start=e.childNodes[0].nodeValue;
65             elif e.localName=="Stop": stop=e.childNodes[0].nodeValue;
66         return "   [%s:%s] %30s " % (start,stop,name);
67
68     def SRF_radiostate(self):
69         ident=self.CCident();
70         chip=self.CCversions.get(ident&0xFF00);
71         dom=self.SRF_chipdom(chip,"register_definition.xml");
72         for e in dom.getElementsByTagName("registerdefinition"):
73             for f in e.childNodes:
74                 if f.localName=="DeviceName":
75                     print "// %s RadioState" % (f.childNodes[0].nodeValue);
76                 elif f.localName=="Register":
77                     name="unknownreg";
78                     address="0xdead";
79                     description="";
80                     bitfields="";
81                     for g in f.childNodes:
82                         if g.localName=="Name":
83                             name=g.childNodes[0].nodeValue;
84                         elif g.localName=="Address":
85                             address=g.childNodes[0].nodeValue;
86                         elif g.localName=="Description":
87                             if g.childNodes:
88                                 description=g.childNodes[0].nodeValue;
89                         elif g.localName=="Bitfield":
90                             bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
91                     #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
92                     print "%-10s=0x%02x; /* %-50s */" % (
93                         name,self.CCpeekdatabyte(eval(address)), description);
94                     if bitfields!="": print bitfields.rstrip();
95
96     def SRF_radiostate_select(self,args=[]):
97         lreg = []
98         ident=self.CCident();
99         chip=self.CCversions.get(ident&0xFF00);
100         dom=self.SRF_chipdom(chip,"register_definition.xml");
101         for reg in args:
102             if reg.lower() == "help":
103                 lreg = "help"
104                 break
105             lreg.append(reg.lower())
106         for e in dom.getElementsByTagName("registerdefinition"):
107             for f in e.childNodes:
108                 if f.localName=="DeviceName":
109                     print "// %s RadioState" % (f.childNodes[0].nodeValue);
110                 elif f.localName=="Register":
111                     name="unknownreg";
112                     address="0xdead";
113                     description="";
114                     bitfields="";
115                     for g in f.childNodes:
116                         if g.localName=="Name":
117                             name=g.childNodes[0].nodeValue;
118                         elif g.localName=="Address":
119                             address=g.childNodes[0].nodeValue;
120                         elif g.localName=="Description":
121                             if g.childNodes:
122                                 description=g.childNodes[0].nodeValue;
123                         elif g.localName=="Bitfield":
124                             bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
125                     #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
126                     if lreg == "help":
127                         print "%-10s /* %-50s */" % (name, description);
128                     elif name.lower() in lreg:
129                         print "%-10s=0x%02x; /* %-50s */" % (
130                             name,self.CCpeekdatabyte(eval(address)), description);
131                         if bitfields!="": print bitfields.rstrip();
132
133     def RF_setfreq(self,frequency):
134         """Set the frequency in Hz."""
135         #FIXME CC1110 specific
136         #Some frequencies fail, probably and FSCAL thing.
137         
138         hz=frequency;
139         freq=int(hz/396.728515625);
140         
141         freq0=freq&0xFF;
142         freq1=(freq&0xFF00)>>8;
143         freq2=(freq&0xFF0000)>>16;
144         
145         self.pokebysym("FREQ2",freq2);
146         self.pokebysym("FREQ1",freq1);
147         self.pokebysym("FREQ0",freq0);
148         
149         self.pokebysym("TEST1",0x31);
150         self.pokebysym("TEST0",0x09);
151         
152         
153         #self.pokebysym("FSCAL2" ,   0x2A);  #above mid
154         self.pokebysym("FSCAL2" ,   0x0A);  #beneath mid
155         
156         #self.CC_RFST_CAL(); #SCAL
157         #time.sleep(1);
158     
159         
160     def RF_getfreq(self):
161         """Get the frequency in Hz."""
162         #FIXME CC1110 specific
163         
164         #return (2400+self.peek(0x05))*10**6
165         #self.poke(0x05,chan);
166         
167         #freq2=self.CCpeekdatabyte(0xdf09);
168         #freq1=self.CCpeekdatabyte(0xdf0a);
169         #freq0=self.CCpeekdatabyte(0xdf0b);
170         freq=0;
171         try:
172             freq2=self.peekbysym("FREQ2");
173             freq1=self.peekbysym("FREQ1");
174             freq0=self.peekbysym("FREQ0");
175             freq=(freq2<<16)+(freq1<<8)+freq0;
176         except:
177             freq=0;
178             
179         hz=freq*396.728515625;
180         
181         return hz;
182     
183     def RF_getchannel(self):
184         """Get the hex channel."""
185         #FIXME CC1110 specific
186         freq=0;
187         try:
188             freq2=self.peekbysym("FREQ2");
189             freq1=self.peekbysym("FREQ1");
190             freq0=self.peekbysym("FREQ0");
191             freq=(freq2<<16)+(freq1<<8)+freq0;
192         except:
193             freq=0;
194             
195         return freq;
196     
197     
198     lastshellcode="none";
199     def shellcodefile(self,filename,wait=1, alwaysreload=0):
200         """Run a fragment of shellcode by name."""
201         #FIXME: should identify chip model number, use shellcode for that chip.
202         
203         if self.lastshellcode!=filename or alwaysreload>0:
204             self.lastshellcode=filename;
205             file=__file__;
206             file=file.replace("GoodFETCC.pyc","GoodFETCC.py");
207             #TODO make this generic
208             path=file.replace("GoodFETCC.py","shellcode/chipcon/cc1110/");
209             filename=path+filename;
210         
211             #Load the shellcode.
212             h=IntelHex(filename);
213             for i in h._buf.keys():
214                 self.CCpokedatabyte(i,h[i]);
215         #Execute it.
216         self.CCdebuginstr([0x02, 0xf0, 0x00]); #ljmp 0xF000
217         self.resume();
218         while wait>0 and (0==self.CCstatus()&0x20):
219             a=1;
220             #print "Waiting for shell code to return.";
221         return;
222     def ishalted(self):
223         return self.CCstatus()&0x20;
224     def shellcode(self,code,wait=1):
225         """Copy a block of code into RAM and execute it."""
226         i=0;
227         ram=0xF000;
228         for byte in code:
229             self.pokebyte(0xF000+i,byte);
230             i=i+1;
231         #print "Code loaded, executing."
232         self.CCdebuginstr([0x02, 0xf0, 0x00]); #ljmp 0xF000
233         self.resume();
234         while wait>0 and (0==self.CCstatus()&0x20):
235             a=1;
236             #time.sleep(0.1);
237             #print "Waiting for shell code to return.";
238         return;
239     def CC1110_crystal(self):
240         """Start the main crystal of the CC1110 oscillating, needed for radio use."""
241         code=[0x53, 0xBE, 0xFB, #anl SLEEP, #0xFB
242               #one:
243               0xE5, 0xBE,       #mov a,SLEEP
244               0x30, 0xE6, 0xFB, #jnb acc.6, back
245               0x53, 0xc6, 0xB8, #anl CLKCON, #0xB8
246               #two
247               0xE5, 0xC6,       #mov a,CLKCON
248               0x20, 0xE6, 0xFB, #jb acc.6, two
249               0x43, 0xBE, 0x04, #orl SLEEP, #0x04
250               0xA5,             #HALT
251               ];
252         self.shellcode(code);
253         
254         #Slower to load, but produced from C.
255         #self.shellcodefile("crystal.ihx");
256         return;
257     def RF_idle(self):
258         """Move the radio to its idle state."""
259         self.CC_RFST_IDLE();
260         return;
261     
262     #Chipcon RF strobes.  CC1110 specific
263     RFST_IDLE=0x04;
264     RFST_RX=0x02;
265     RFST_TX=0x03;
266     RFST_CAL=0x01;
267     def CC_RFST_IDLE(self):
268         """Switch the radio to idle mode, clearing overflows and errors."""
269         self.CC_RFST(self.RFST_IDLE);
270     def CC_RFST_TX(self):
271         """Switch the radio to TX mode."""
272         self.CC_RFST(self.RFST_TX);
273     def CC_RFST_RX(self):
274         """Switch the radio to RX mode."""
275         self.CC_RFST(self.RFST_RX);
276     def CC_RFST_CAL(self):
277         """Calibrate strobe the radio."""
278         self.CC_RFST(self.RFST_CAL);
279     def CC_RFST(self,state=RFST_IDLE):
280         RFST=0xDFE1
281         self.pokebyte(RFST,state); #Return to idle state.
282         return;
283     def config_dash7(self,band="lf"):
284         #These settings came from the OpenTag project's GIT repo on 18 Dec, 2010.
285         #Waiting for official confirmation of the accuracy.
286
287         self.pokebysym("FSCTRL1"  , 0x08)   # Frequency synthesizer control.
288         self.pokebysym("FSCTRL0"  , 0x00)   # Frequency synthesizer control.
289         
290         #Don't change these while the radio is active.
291         self.pokebysym("FSCAL3"   , 0xEA)   # Frequency synthesizer calibration.
292         self.pokebysym("FSCAL2"   , 0x2A)   # Frequency synthesizer calibration.
293         self.pokebysym("FSCAL1"   , 0x00)   # Frequency synthesizer calibration.
294         self.pokebysym("FSCAL0"   , 0x1F)   # Frequency synthesizer calibration.
295         
296         if band=="ismeu" or band=="eu":
297             print "There is no official eu band for dash7."
298             self.pokebysym("FREQ2"    , 0x21)   # Frequency control word, high byte.
299             self.pokebysym("FREQ1"    , 0x71)   # Frequency control word, middle byte.
300             self.pokebysym("FREQ0"    , 0x7a)   # Frequency control word, low byte.
301         elif band=="ismus" or band=="us":
302             print "There is no official us band for dash7."
303             self.pokebysym("FREQ2"    , 0x22)   # Frequency control word, high byte.
304             self.pokebysym("FREQ1"    , 0xB1)   # Frequency control word, middle byte.
305             self.pokebysym("FREQ0"    , 0x3B)   # Frequency control word, low byte.
306         elif band=="ismlf" or band=="lf":
307             # 433.9198 MHz, same as Simpliciti.
308             self.pokebysym("FREQ2"    , 0x10)   # Frequency control word, high byte.
309             self.pokebysym("FREQ1"    , 0xB0)   # Frequency control word, middle byte.
310             self.pokebysym("FREQ0"    , 0x71)   # Frequency control word, low byte.
311         elif band=="none":
312             pass;
313         else:
314             #Got a frequency, not a band.
315             self.RF_setfreq(eval(band));
316         self.pokebysym("MDMCFG4"  , 0x8B)   # 62.5 kbps w/ 200 kHz filter
317         self.pokebysym("MDMCFG3"  , 0x3B)
318         self.pokebysym("MDMCFG2"  , 0x11)
319         self.pokebysym("MDMCFG1"  , 0x02)
320         self.pokebysym("MDMCFG0"  , 0x53)
321         self.pokebysym("CHANNR"   , 0x00)   # Channel zero.
322         self.pokebysym("DEVIATN"  , 0x50)   # 50 kHz deviation
323         
324         self.pokebysym("FREND1"   , 0xB6)   # Front end RX configuration.
325         self.pokebysym("FREND0"   , 0x10)   # Front end RX configuration.
326         self.pokebysym("MCSM2"    , 0x1E)
327         self.pokebysym("MCSM1"    , 0x3F)
328         self.pokebysym("MCSM0"    , 0x30)
329         self.pokebysym("FOCCFG"   , 0x1D)   # Frequency Offset Compensation Configuration.
330         self.pokebysym("BSCFG"    , 0x1E)   # 6.25% data error rate
331         
332         self.pokebysym("AGCCTRL2" , 0xC7)   # AGC control.
333         self.pokebysym("AGCCTRL1" , 0x00)   # AGC control.
334         self.pokebysym("AGCCTRL0" , 0xB2)   # AGC control.
335         
336         self.pokebysym("TEST2"    , 0x81)   # Various test settings.
337         self.pokebysym("TEST1"    , 0x35)   # Various test settings.
338         self.pokebysym("TEST0"    , 0x09)   # Various test settings.
339         self.pokebysym("PA_TABLE0", 0xc0)   # Max output power.
340         self.pokebysym("PKTCTRL1" , 0x04)   # Packet automation control, w/ lqi
341         #self.pokebysym("PKTCTRL1" , 0x00)   # Packet automation control. w/o lqi
342         self.pokebysym("PKTCTRL0" , 0x05)   # Packet automation control, w/ checksum.
343         #self.pokebysym("PKTCTRL0" , 0x00)   # Packet automation control, w/o checksum, fixed length
344         self.pokebysym("ADDR"     , 0x01)   # Device address.
345         self.pokebysym("PKTLEN"   , 0xFF)   # Packet length.
346         
347         #Sync word hack
348         self.pokebysym("SYNC1",0x83);
349         self.pokebysym("SYNC0",0xFE);
350         return;
351     def config_iclicker(self,band="lf"):
352         #Mike Ossmann figured most of this out, with help from neighbors.
353         
354         self.pokebysym("FSCTRL1"  , 0x06)   # Frequency synthesizer control.
355         self.pokebysym("FSCTRL0"  , 0x00)   # Frequency synthesizer control.
356         
357         #Don't change these while the radio is active.
358         self.pokebysym("FSCAL3"   , 0xE9)
359         self.pokebysym("FSCAL2"   , 0x2A)
360         self.pokebysym("FSCAL1"   , 0x00)
361         self.pokebysym("FSCAL0"   , 0x1F)
362         
363         if band=="ismeu" or band=="eu":
364             print "The EU band is unknown.";
365         elif band=="ismus" or band=="us":
366             #905.5MHz
367             self.pokebysym("FREQ2"    , 0x22)   # Frequency control word, high byte.
368             self.pokebysym("FREQ1"    , 0xD3)   # Frequency control word, middle byte.
369             self.pokebysym("FREQ0"    , 0xAC)   # Frequency control word, low byte.
370         elif band=="ismlf" or band=="lf":
371             print "There is no LF version of the iclicker."
372         elif band=="none":
373             pass;
374         else:
375             #Got a frequency, not a band.
376             self.RF_setfreq(eval(band));
377         # 812.5kHz bandwidth, 152.34 kbaud
378         self.pokebysym("MDMCFG4"  , 0x1C)   
379         self.pokebysym("MDMCFG3"  , 0x80)
380         # no FEC, 2 byte preamble, 250kHz chan spacing
381         
382         #15/16 sync
383         #self.pokebysym("MDMCFG2"  , 0x01)
384         #16/16 sync
385         self.pokebysym("MDMCFG2"  , 0x02)
386         
387         self.pokebysym("MDMCFG1"  , 0x03)
388         self.pokebysym("MDMCFG0"  , 0x3b)
389         
390         self.pokebysym("CHANNR"   , 0x2e)   # Channel zero.
391         
392         #self.pokebysym("DEVIATN"  , 0x71)  # 118.5
393         self.pokebysym("DEVIATN"  , 0x72)   # 253.9 kHz deviation
394         
395         self.pokebysym("FREND1"   , 0x56)   # Front end RX configuration.
396         self.pokebysym("FREND0"   , 0x10)   # Front end RX configuration.
397         self.pokebysym("MCSM2"    , 0x07)
398         self.pokebysym("MCSM1"    , 0x30)   #Auto freq. cal.
399         self.pokebysym("MCSM0"    , 0x14)
400         
401         self.pokebysym("TEST2"    , 0x88)   # 
402         self.pokebysym("TEST1"    , 0x31)   # 
403         self.pokebysym("TEST0"    , 0x09)   # High VCO (Upper band.)
404         self.pokebysym("PA_TABLE0", 0xC0)   # Max output power.
405         self.pokebysym("PKTCTRL1" , 0x45)   # Preamble qualidy 2*4=6, adr check, status
406         self.pokebysym("PKTCTRL0" , 0x00)   # No whitening, CR, fixed len.
407         
408         self.pokebysym("PKTLEN"   , 0x09)   # Packet length.
409         
410         self.pokebysym("SYNC1",0xB0);
411         self.pokebysym("SYNC0",0xB0);
412         self.pokebysym("ADDR", 0xB0);
413         return;
414     def config_ook(self,band="none"):
415         self.pokebysym("FSCTRL1"  , 0x0C) #08   # Frequency synthesizer control.
416         self.pokebysym("FSCTRL0"  , 0x00)   # Frequency synthesizer control.
417         
418         #Don't change these while the radio is active.
419         self.pokebysym("FSCAL3"   , 0xEA)   # Frequency synthesizer calibration.
420         self.pokebysym("FSCAL2"   , 0x2A)   # Frequency synthesizer calibration.
421         self.pokebysym("FSCAL1"   , 0x00)   # Frequency synthesizer calibration.
422         self.pokebysym("FSCAL0"   , 0x1F)   # Frequency synthesizer calibration.
423         
424         if band=="ismeu" or band=="eu":
425             self.pokebysym("FREQ2"    , 0x21)   # Frequency control word, high byte.
426             self.pokebysym("FREQ1"    , 0x71)   # Frequency control word, middle byte.
427             self.pokebysym("FREQ0"    , 0x7a)   # Frequency control word, low byte.
428         elif band=="ismus" or band=="us":
429             self.pokebysym("FREQ2"    , 0x22)   # Frequency control word, high byte.
430             self.pokebysym("FREQ1"    , 0xB1)   # Frequency control word, middle byte.
431             self.pokebysym("FREQ0"    , 0x3B)   # Frequency control word, low byte.
432         elif band=="ismlf" or band=="lf":
433             self.pokebysym("FREQ2"    , 0x0C)   # Frequency control word, high byte.
434             self.pokebysym("FREQ1"    , 0x1D)   # Frequency control word, middle byte.
435             self.pokebysym("FREQ0"    , 0x89)   # Frequency control word, low byte.
436         elif band=="none":
437             pass;
438         else:
439             #Got a frequency, not a band.
440             self.RF_setfreq(eval(band));
441         
442         #data rate
443         #~1
444         #self.pokebysym("MDMCFG4"  , 0x85)
445         #self.pokebysym("MDMCFG3"  , 0x83)
446         #0.5
447         #self.pokebysym("MDMCFG4"  , 0xf4)
448         #self.pokebysym("MDMCFG3"  , 0x43)
449         #2.4
450         #self.pokebysym("MDMCFG4"  , 0xf6)
451         #self.pokebysym("MDMCFG3"  , 0x83)
452         
453         #4.8 kbaud
454         #print "Warning: Default to 4.8kbaud.";
455         #self.pokebysym("MDMCFG4"  , 0xf7)
456         #self.pokebysym("MDMCFG3"  , 0x83)
457         #9.6 kbaud
458         #print "Warning: Default to 9.6kbaud.";
459         #
460         
461         self.pokebysym("MDMCFG4"  , 0xf8)
462         self.pokebysym("MDMCFG3"  , 0x83)
463         self.pokebysym("MDMCFG2"  , 0x34)   # OOK, carrier-sense, no-manchester
464         
465         #Kind aright for keeloq
466         print "Warning: Guessing baud rate.";
467         #self.pokebysym("MDMCFG4"  , 0xf6)
468         #self.pokebysym("MDMCFG3"  , 0x93)
469         #self.pokebysym("MDMCFG2"  , 0x3C)   # OOK, carrier-sense, manchester
470         
471         self.pokebysym("MDMCFG1"  , 0x00)   # Modem configuration.
472         self.pokebysym("MDMCFG0"  , 0xF8)   # Modem configuration.
473         self.pokebysym("CHANNR"   , 0x00)   # Channel number.
474         
475         self.pokebysym("FREND1"   , 0x56)   # Front end RX configuration.
476         self.pokebysym("FREND0"   , 0x11)   # Front end RX configuration.
477         self.pokebysym("MCSM0"    , 0x18)   # Main Radio Control State Machine configuration.
478         #self.pokebysym("FOCCFG"   , 0x1D)   # Frequency Offset Compensation Configuration.
479         #self.pokebysym("BSCFG"    , 0x1C)   # Bit synchronization Configuration.
480         
481         #self.pokebysym("AGCCTRL2" , 0xC7)   # AGC control.
482         #self.pokebysym("AGCCTRL1" , 0x00)   # AGC control.
483         #self.pokebysym("AGCCTRL0" , 0xB2)   # AGC control.
484         
485         self.pokebysym("TEST2"    , 0x81)   # Various test settings.
486         self.pokebysym("TEST1"    , 0x35)   # Various test settings.
487         self.pokebysym("TEST0"    , 0x0B)   # Various test settings.
488         self.pokebysym("PA_TABLE0", 0xc2)   # Max output power.
489         self.pokebysym("PKTCTRL1" , 0x04)   # Packet automation control, w/ lqi
490         #self.pokebysym("PKTCTRL1" , 0x00)   # Packet automation control. w/o lqi
491         #self.pokebysym("PKTCTRL0" , 0x05)   # Packet automation control, w/ checksum.
492         self.pokebysym("PKTCTRL0" , 0x00)   # Packet automation control, w/o checksum, fixed length
493         self.pokebysym("ADDR"     , 0x01)   # Device address.
494         self.pokebysym("PKTLEN"   , 0xFF)   # Packet length.
495         
496         self.pokebysym("SYNC1",0xD3);
497         self.pokebysym("SYNC0",0x91);
498         
499     def config_simpliciti(self,band="none"):
500         self.pokebysym("FSCTRL1"  , 0x0C) #08   # Frequency synthesizer control.
501         self.pokebysym("FSCTRL0"  , 0x00)   # Frequency synthesizer control.
502         
503         #Don't change these while the radio is active.
504         self.pokebysym("FSCAL3"   , 0xEA)   # Frequency synthesizer calibration.
505         self.pokebysym("FSCAL2"   , 0x2A)   # Frequency synthesizer calibration.
506         self.pokebysym("FSCAL1"   , 0x00)   # Frequency synthesizer calibration.
507         self.pokebysym("FSCAL0"   , 0x1F)   # Frequency synthesizer calibration.
508         
509         if band=="ismeu" or band=="eu":
510             self.pokebysym("FREQ2"    , 0x21)   # Frequency control word, high byte.
511             self.pokebysym("FREQ1"    , 0x71)   # Frequency control word, middle byte.
512             self.pokebysym("FREQ0"    , 0x7a)   # Frequency control word, low byte.
513         elif band=="ismus" or band=="us":
514             self.pokebysym("FREQ2"    , 0x22)   # Frequency control word, high byte.
515             self.pokebysym("FREQ1"    , 0xB1)   # Frequency control word, middle byte.
516             self.pokebysym("FREQ0"    , 0x3B)   # Frequency control word, low byte.
517         elif band=="ismlf" or band=="lf":
518             self.pokebysym("FREQ2"    , 0x10)   # Frequency control word, high byte.
519             self.pokebysym("FREQ1"    , 0xB0)   # Frequency control word, middle byte.
520             self.pokebysym("FREQ0"    , 0x71)   # Frequency control word, low byte.
521         elif band=="none":
522             band="none";
523         else:
524             #Got a frequency, not a band.
525             self.RF_setfreq(eval(band));
526         self.pokebysym("MDMCFG4"  , 0x7B)   # Modem configuration.
527         self.pokebysym("MDMCFG3"  , 0x83)   # Modem configuration.
528         self.pokebysym("MDMCFG2"  , 0x13)   # Modem configuration.
529         self.pokebysym("MDMCFG1"  , 0x22)   # Modem configuration.
530         self.pokebysym("MDMCFG0"  , 0xF8)   # Modem configuration.
531         if band=="ismus" or band=="us":
532             self.pokebysym("CHANNR"   , 20)   # Channel number.
533         else:
534             self.pokebysym("CHANNR"   , 0x00)   # Channel number.
535         self.pokebysym("DEVIATN"  , 0x42)   # Modem deviation setting (when FSK modulation is enabled).
536         
537         self.pokebysym("FREND1"   , 0xB6)   # Front end RX configuration.
538         self.pokebysym("FREND0"   , 0x10)   # Front end RX configuration.
539         self.pokebysym("MCSM0"    , 0x18)   # Main Radio Control State Machine configuration.
540         self.pokebysym("FOCCFG"   , 0x1D)   # Frequency Offset Compensation Configuration.
541         self.pokebysym("BSCFG"    , 0x1C)   # Bit synchronization Configuration.
542         
543         self.pokebysym("AGCCTRL2" , 0xC7)   # AGC control.
544         self.pokebysym("AGCCTRL1" , 0x00)   # AGC control.
545         self.pokebysym("AGCCTRL0" , 0xB2)   # AGC control.
546         
547         self.pokebysym("TEST2"    , 0x81)   # Various test settings.
548         self.pokebysym("TEST1"    , 0x35)   # Various test settings.
549         self.pokebysym("TEST0"    , 0x09)   # Various test settings.
550         self.pokebysym("PA_TABLE0", 0xc0)   # Max output power.
551         self.pokebysym("PKTCTRL1" , 0x04)   # Packet automation control, w/ lqi
552         #self.pokebysym("PKTCTRL1" , 0x00)   # Packet automation control. w/o lqi
553         self.pokebysym("PKTCTRL0" , 0x05)   # Packet automation control, w/ checksum.
554         #self.pokebysym("PKTCTRL0" , 0x00)   # Packet automation control, w/o checksum, fixed length
555         self.pokebysym("ADDR"     , 0x01)   # Device address.
556         self.pokebysym("PKTLEN"   , 0xFF)   # Packet length.
557         
558         self.pokebysym("SYNC1",0xD3);
559         self.pokebysym("SYNC0",0x91);
560         
561     def RF_carrier(self):
562         """Hold a carrier wave on the present frequency."""
563         
564         self.CC1110_crystal(); #FIXME, '1110 specific.
565         self.RF_idle();
566         
567         
568         RFST=0xDFE1;
569         
570         self.config_simpliciti();
571         
572         #Don't change these while the radio is active.
573         #self.pokebysym("FSCAL3"   , 0xA9)   # Frequency synthesizer calibration.
574         #self.pokebysym("FSCAL2"   , 0x0A)   # Frequency synthesizer calibration.
575         #self.pokebysym("FSCAL1"   , 0x00)   # Frequency synthesizer calibration.
576         #self.pokebysym("FSCAL0"   , 0x11)   # Frequency synthesizer calibration.
577         
578         #Ramp up the power.
579         #self.pokebysym("PA_TABLE0", 0xFF)   # PA output power setting.
580         
581         #This is what drops to OOK.
582         #Comment to keep GFSK, might be better at jamming.
583         self.pokebysym("MDMCFG4"  , 0x86)   # Modem configuration.
584         self.pokebysym("MDMCFG3"  , 0x83)   # Modem configuration.
585         self.pokebysym("MDMCFG2"  , 0x30)   # Modem configuration.
586         self.pokebysym("MDMCFG1"  , 0x22)   # Modem configuration.
587         self.pokebysym("MDMCFG0"  , 0xF8)   # Modem configuration.
588         
589         self.pokebysym("SYNC1",0xAA);
590         self.pokebysym("SYNC0",0xAA);
591         
592         #while ((MARCSTATE & MARCSTATE_MARC_STATE) != MARC_STATE_TX); 
593         state=0;
594         
595         while((state!=0x13)):
596             self.pokebyte(RFST,0x03); #RFST=RFST_STX
597             time.sleep(0.1);
598             state=self.peekbysym("MARCSTATE")&0x1F;
599             #print "state=%02x" % state;
600         print "Holding a carrier on %f MHz." % (self.RF_getfreq()/10**6);
601         
602         return;
603             
604     def RF_getsmac(self):
605         """Return the source MAC address."""
606         
607         #Register 0A is RX_ADDR_P0, five bytes.
608         mac=self.peekbysym("ADDR");
609         return mac;
610     def RF_setsmac(self,mac):
611         """Set the source MAC address."""
612         self.pokebysym("ADDR",mac);
613         return 0;
614     def RF_gettmac(self):
615         """Return the target MAC address."""
616         return 0;
617     def RF_settmac(self,mac):
618         """Set the target MAC address."""
619         return 0;
620     def RF_rxpacket(self):
621         """Get a packet from the radio.  Returns None if none is waiting."""
622         self.shellcodefile("rxpacket.ihx");
623         len=self.peek8(0xFE00,"xdata");
624         return self.peekblock(0xFE00,len+3,"data");
625     def RF_txpacket(self,packet):
626         """Transmit a packet.  Untested."""
627         
628         self.pokeblock(0xFE00,packet,"data");
629         self.shellcodefile("txpacket.ihx");
630         return;
631     def RF_txrxpacket(self,packet):
632         """Transmit a packet.  Untested."""
633         
634         self.pokeblock(0xFE00,packet,"data");
635         self.shellcodefile("txrxpacket.ihx");
636         len=self.peek8(0xFE00,"xdata");
637         return self.peekblock(0xFE00,len+3,"data");
638
639     def RF_getrssi(self):
640         """Returns the received signal strenght, with a weird offset."""
641         try:
642             rssireg=self.symbols.get("RSSI");
643             return self.CCpeekdatabyte(rssireg)^0x80;
644         except:
645             if self.verbose>0: print "RSSI reg doesn't exist.";
646         try:
647             #RSSI doesn't exist on some 2.4GHz devices.  Maybe RSSIL and RSSIH?
648             rssilreg=self.symbols.get("RSSIL");
649             rssil=self.CCpeekdatabyte(rssilreg);
650             rssihreg=self.symbols.get("RSSIL");
651             rssih=self.CCpeekdatabyte(rssihreg);
652             return (rssih<<8)|rssil;
653         except:
654             if self.verbose>0: print "RSSIL/RSSIH regs don't exist.";
655         
656         return 0;
657     
658     def SRF_loadsymbols(self):
659         ident=self.CCident();
660         chip=self.CCversions.get(ident&0xFF00);
661         dom=self.SRF_chipdom(chip,"register_definition.xml");
662         for e in dom.getElementsByTagName("registerdefinition"):
663             for f in e.childNodes:
664                 if f.localName=="Register":
665                     name="unknownreg";
666                     address="0xdead";
667                     description="";
668                     bitfields="";
669                     for g in f.childNodes:
670                         if g.localName=="Name":
671                             name=g.childNodes[0].nodeValue;
672                         elif g.localName=="Address":
673                             address=g.childNodes[0].nodeValue;
674                         elif g.localName=="Description":
675                             if g.childNodes:
676                                 description=g.childNodes[0].nodeValue;
677                         elif g.localName=="Bitfield":
678                             bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
679                     #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
680                     self.symbols.define(eval(address),name,description,"data");
681     def halt(self):
682         """Halt the CPU."""
683         self.CChaltcpu();
684     def CChaltcpu(self):
685         """Halt the CPU."""
686         self.writecmd(self.APP,0x86,0,self.data);
687     def resume(self):
688         self.CCreleasecpu();
689     def CCreleasecpu(self):
690         """Resume the CPU."""
691         self.writecmd(self.APP,0x87,0,self.data);
692     def test(self):
693         self.CCreleasecpu();
694         self.CChaltcpu();
695         #print "Status: %s" % self.CCstatusstr();
696         
697         #Grab ident three times, should be equal.
698         ident1=self.CCident();
699         ident2=self.CCident();
700         ident3=self.CCident();
701         if(ident1!=ident2 or ident2!=ident3):
702             print "Error, repeated ident attempts unequal."
703             print "%04x, %04x, %04x" % (ident1, ident2, ident3);
704         
705         #Single step, printing PC.
706         print "Tracing execution at startup."
707         for i in range(1,15):
708             pc=self.CCgetPC();
709             byte=self.CCpeekcodebyte(i);
710             #print "PC=%04x, %02x" % (pc, byte);
711             self.CCstep_instr();
712         
713         print "Verifying that debugging a NOP doesn't affect the PC."
714         for i in range(1,15):
715             pc=self.CCgetPC();
716             self.CCdebuginstr([0x00]);
717             if(pc!=self.CCgetPC()):
718                 print "ERROR: PC changed during CCdebuginstr([NOP])!";
719         
720         print "Checking pokes to XRAM."
721         for i in range(self.execbuf,self.execbuf+0x20):
722             self.CCpokedatabyte(i,0xde);
723             if(self.CCpeekdatabyte(i)!=0xde):
724                 print "Error in XDATA at 0x%04x" % i;
725         
726         #print "Status: %s." % self.CCstatusstr();
727         #Exit debugger
728         self.stop();
729         print "Done.";
730
731     def setup(self):
732         """Move the FET into the Chipcon 8051 application."""
733         #print "Initializing Chipcon.";
734         self.writecmd(self.APP,0x10,0,self.data);
735     def CCrd_config(self):
736         """Read the config register of a Chipcon."""
737         self.writecmd(self.APP,0x82,0,self.data);
738         return ord(self.data[0]);
739     def CCwr_config(self,config):
740         """Write the config register of a Chipcon."""
741         self.writecmd(self.APP,0x81,1,[config&0xFF]);
742     def CClockchip(self):
743         """Set the flash lock bit in info mem."""
744         self.writecmd(self.APP, 0x9A, 0, None);
745     def lock(self):
746         """Set the flash lock bit in info mem."""
747         self.CClockchip();
748     
749
750     CCversions={0x0100:"CC1110",
751                 0x1100:"CC1111",
752                 0x8500:"CC2430",
753                 0x8900:"CC2431",
754                 0x8100:"CC2510",
755                 0x9100:"CC2511",
756                 0xA500:"CC2530", #page 57 of SWRU191B
757                 0xB500:"CC2531",
758                 0x9500:"CC2533",
759                 0x8D00:"CC2540",
760                 0xFF00:"CCmissing"};
761     execbuf=None;
762     CCexecbuf= {0x0100:0xF000,
763                 0x1100:0xF000,
764                 0x8500:0xF000,
765                 0x8900:0xF000,
766                 0x8100:0xF000,
767                 0x9100:0xF000,
768                 0xA500:0x0000, #CC2530
769                 0xB500:0x8000,
770                 0x9500:0x8000,
771                 0x8D00:0x8000,
772                 0xFF00:None} #missing
773     CCpagesizes={0x01: 1024, #"CC1110",
774                  0x11: 1024, #"CC1111",
775                  0x85: 2048, #"CC2430",
776                  0x89: 2048, #"CC2431",
777                  0x81: 1024, #"CC2510",
778                  0x91: 1024, #"CC2511",
779                  0xA5: 2048, #"CC2530", #page 57 of SWRU191B
780                  0xB5: 2048, #"CC2531",
781                  0x95: 2048, #"CC2533",
782                  0x8D: 2048, #"CC2540",
783                  0xFF: None}
784     def infostring(self):
785         return self.CCidentstr();
786     def CCidentstr(self):
787         ident=self.CCident();
788         chip=self.CCversions.get(ident&0xFF00);
789         execbuf=self.CCexecbuf.get(ident&0xFF00);
790         pagesize=self.CCpagesizes.get(ident>0xFF);
791         self.execbuf=execbuf;
792         
793         try:
794             return "%s/r%0.4x/ps0x%0.4x" % (chip, ident, pagesize); 
795         except:
796             return "%04x" % ident;
797     def CCident(self):
798         """Get a chipcon's ID."""
799         self.writecmd(self.APP,0x8B,0,None);
800         chip=ord(self.data[0]);
801         rev=ord(self.data[1]);
802         return (chip<<8)+rev;
803     def CCpagesize(self):
804         """Get a chipcon's ID."""
805         self.writecmd(self.APP,0x8B,0,None);
806         chip=ord(self.data[0]);
807         size=self.CCpagesizes.get(chip);
808         if(size<10):
809             print "ERROR: Pagesize undefined.";
810             print "chip=%0.4x" %chip;
811             sys.exit(1);
812             #return 2048;
813         return size;
814     def getpc(self):
815         return self.CCgetPC();
816     def CCgetPC(self):
817         """Get a chipcon's PC."""
818         self.writecmd(self.APP,0x83,0,None);
819         hi=ord(self.data[0]);
820         lo=ord(self.data[1]);
821         return (hi<<8)+lo;
822     def CCcmd(self,phrase):
823         self.writecmd(self.APP,0x00,len(phrase),phrase);
824         val=ord(self.data[0]);
825         print "Got %02x" % val;
826         return val;
827     def CCdebuginstr(self,instr):
828         self.writecmd(self.APP,0x88,len(instr),instr);
829         return ord(self.data[0]);
830     #def peekblock(self,adr,length,memory="vn"):
831     #    """Return a block of data, broken"""
832     #    data=[adr&0xff, (adr&0xff00)>>8,
833     #          length&0xFF,(length&0xFF00)>>8];
834     #    self.writecmd(self.APP,0x91,4,data);
835     #    return [ord(x) for x in self.data]
836     def peek8(self,address, memory="code"):
837         if(memory=="code" or memory=="flash" or memory=="vn"):
838             return self.CCpeekcodebyte(address);
839         elif(memory=="data" or memory=="xdata" or memory=="ram"):
840             return self.CCpeekdatabyte(address);
841         elif(memory=="idata" or memory=="iram"):
842             return self.CCpeekirambyte(address);
843         print "%s is an unknown memory." % memory;
844         return 0xdead;
845     def CCpeekcodebyte(self,adr):
846         """Read the contents of code memory at an address."""
847         self.data=[adr&0xff, (adr&0xff00)>>8];
848         self.writecmd(self.APP,0x90,2,self.data);
849         return ord(self.data[0]);
850     def CCpeekdatabyte(self,adr):
851         """Read the contents of data memory at an address."""
852         self.data=[adr&0xff, (adr&0xff00)>>8];
853         self.writecmd(self.APP,0x91, 2, self.data);
854         return ord(self.data[0]);
855     def CCpeekirambyte(self,adr):
856         """Read the contents of IRAM at an address."""
857         self.data=[adr&0xff];
858         self.writecmd(self.APP,0x02, 1, self.data);
859         return ord(self.data[0]);
860     def CCpeekiramword(self,adr):
861         """Read the little-endian contents of IRAM at an address."""
862         return self.CCpeekirambyte(adr)+(
863             self.CCpeekirambyte(adr+1)<<8);
864     def CCpokeiramword(self,adr,val):
865         self.CCpokeirambyte(adr,val&0xff);
866         self.CCpokeirambyte(adr+1,(val>>8)&0xff);
867     def CCpokeirambyte(self,adr,val):
868         """Write the contents of IRAM at an address."""
869         self.data=[adr&0xff, val&0xff];
870         self.writecmd(self.APP,0x02, 2, self.data);
871         return ord(self.data[0]);
872     def pokebyte(self,adr,val,mem="xdata"):
873         self.CCpokedatabyte(adr,val);
874     def CCpokedatabyte(self,adr,val):
875         """Write a byte to data memory."""
876         self.data=[adr&0xff, (adr&0xff00)>>8, val];
877         self.writecmd(self.APP, 0x92, 3, self.data);
878         return ord(self.data[0]);
879     def CCchiperase(self):
880         """Erase all of the target's memory."""
881         self.writecmd(self.APP,0x80,0,None);
882     def erase(self):
883         """Erase all of the target's memory."""
884         self.CCchiperase();
885         self.start();
886     
887     def CCstatus(self):
888         """Check the status."""
889         self.writecmd(self.APP,0x84,0,None);
890         return ord(self.data[0])
891     #Same as CC2530
892     CCstatusbits={0x80 : "erase_busy",
893                   0x40 : "pcon_idle",
894                   0x20 : "cpu_halted",
895                   0x10 : "pm0",
896                   0x08 : "halt_status",
897                   0x04 : "locked",
898                   0x02 : "oscstable",
899                   0x01 : "overflow"
900                   };
901     CCconfigbits={0x20 : "soft_power_mode",   #new for CC2530
902                   0x08 : "timers_off",
903                   0x04 : "dma_pause",
904                   0x02 : "timer_suspend",
905                   0x01 : "sel_flash_info_page" #stricken from CC2530
906                   };
907                   
908     def status(self):
909         """Check the status as a string."""
910         status=self.CCstatus();
911         str="";
912         i=1;
913         while i<0x100:
914             if(status&i):
915                 str="%s %s" %(self.CCstatusbits[i],str);
916             i*=2;
917         return str;
918     def start(self):
919         """Start debugging."""
920         ident=0x0000;
921         #while ident==0xFFFF or ident==0x0000:
922         self.setup();
923         self.writecmd(self.APP,0x20,0,self.data);
924         identa=self.CCident();
925         self.CCidentstr();
926         
927         ident=self.CCident();
928         #Get SmartRF Studio regs if they exist.
929         self.loadsymbols(); 
930         #print "Status: %s" % self.status();
931     def stop(self):
932         """Stop debugging."""
933         self.writecmd(self.APP,0x21,0,self.data);
934     def CCstep_instr(self):
935         """Step one instruction."""
936         self.writecmd(self.APP,0x89,0,self.data);
937     def CCeraseflashbuffer(self):
938         """Erase the 2kB flash buffer"""
939         self.writecmd(self.APP,0x99);
940     def CCflashpage(self,adr):
941         """Flash 2kB a page of flash from 0xF000 in XDATA"""
942         data=[adr&0xFF,
943               (adr>>8)&0xFF,
944               (adr>>16)&0xFF,
945               (adr>>24)&0xFF];
946         print "Flashing buffer to 0x%06x" % adr;
947         self.writecmd(self.APP,0x95,4,data);
948     
949     def setsecret(self,value):
950         """Set a secret word for later retreival.  Used by glitcher."""
951         page = 0x0000;
952         pagelen = self.CCpagesize(); #Varies by chip.
953         print "page=%04x, pagelen=%04x" % (page,pagelen);
954         
955         self.CCeraseflashbuffer();
956         print "Setting secret to %x" % value;
957         self.CCpokedatabyte(0xF000,value);
958         self.CCpokedatabyte(0xF800,value);
959         print "Setting secret to %x==%x" % (value,
960                                             self.CCpeekdatabyte(0xf000));
961         self.CCflashpage(0);
962         print "code[0]=%x" % self.CCpeekcodebyte(0);
963     def getsecret(self):
964         """Get a secret word.  Used by glitcher."""
965         secret=self.CCpeekcodebyte(0);
966         #print "Got secret %02x" % secret;
967         return secret;
968     
969     #FIXME: This is CC1110-specific and duplicates functionality of 
970     #       SmartRF7 integration.
971     CCspecfuncregs={
972         'P0':0x80,
973         'SP':0x81,
974         'DPL0':0x82,
975         'DPH0':0x83,
976         'DPL1':0x84,
977         'DPH1':0x85,
978         'U0CSR':0x86,
979         'PCON':0x87,
980         'TCON':0x88,
981         'P0IFG':0x89,
982         'P1IFG':0x8A,
983         'P2IFG':0x8B,
984         'PICTL':0x8C,
985         'P1IEN':0x8D,
986         'P0INP':0x8F,
987         'P1':0x90,
988         'RFIM':0x91,
989         'DPS':0x92,
990         'MPAGE':0x93,
991         'ENDIAN':0x95,
992         'S0CON':0x98,
993         'IEN2':0x9A,
994         'S1CON':0x9B,
995         'T2CT':0x9C,
996         'T2PR':0x9D,
997         'T2CTL':0x9E,
998         'P2':0xA0,
999         'WORIRQ':0xA1,
1000         'WORCTRL':0xA2,
1001         'WOREVT0':0xA3,
1002         'WOREVT1':0xA4,
1003         'WORTIME0':0xA5,
1004         'WORTIME1':0xA6,
1005         'IEN0':0xA8,
1006         'IP0':0xA9,
1007         'FWT':0xAB,
1008         'FADDRL':0xAC,
1009         'FADDRH':0xAD,
1010         'FCTL':0xAE,
1011         'FWDATA':0xAF,
1012         'ENCDI':0xB1,
1013         'ENCDO':0xB2,
1014         'ENCCS':0xB3,
1015         'ADCCON1':0xB4,
1016         'ADCCON2':0xB5,
1017         'ADCCON3':0xB6,
1018         'IEN1':0xB8,
1019         'IP1':0xB9,
1020         'ADCL':0xBA,
1021         'ADCH':0xBB,
1022         'RNDL':0xBC,
1023         'RNDH':0xBD,
1024         'SLEEP':0xBE,
1025         'IRCON':0xC0,
1026         'U0DBUF':0xC1,
1027         'U0BAUD':0xC2,
1028         'U0UCR':0xC4,
1029         'U0GCR':0xC5,
1030         'CLKCON':0xC6,
1031         'MEMCTR':0xC7,
1032         'WDCTL':0xC9,
1033         'T3CNT':0xCA,
1034         'T3CTL':0xCB,
1035         'T3CCTL0':0xCC,
1036         'T3CC0':0xCD,
1037         'T3CCTL1':0xCE,
1038         'T3CC1':0xCF,
1039         'PSW':0xD0,
1040         'DMAIRQ':0xD1,
1041         'DMA1CFGL':0xD2,
1042         'DMA1CFGH':0xD3,
1043         'DMA0CFGL':0xD4,
1044         'DMA0CFGH':0xD5,
1045         'DMAARM':0xD6,
1046         'DMAREQ':0xD7,
1047         'TIMIF':0xD8,
1048         'RFD':0xD9,
1049         'T1CC0L':0xDA,
1050         'T1CC0H':0xDB,
1051         'T1CC1L':0xDC,
1052         'T1CC1H':0xDD,
1053         'T1CC2L':0xDE,
1054         'T1CC2H':0xDF,
1055         'ACC':0xE0,
1056         'RFST':0xE1,
1057         'T1CNTL':0xE2,
1058         'T1CNTH':0xE3,
1059         'T1CTL':0xE4,
1060         'T1CCTL0':0xE5,
1061         'T1CCTL1':0xE6,
1062         'T1CCTL2':0xE7,
1063         'IRCON2':0xE8,
1064         'RFIF':0xE9,
1065         'T4CNT':0xEA,
1066         'T4CTL':0xEB,
1067         'T4CCTL0':0xEC,
1068         'T4CC0':0xED,
1069         'T4CCTL1':0xEE,
1070         'T4CC1':0xEF,
1071         'B':0xF0,
1072         'PERCFG':0xF1,
1073         'ADCCFG':0xF2,
1074         'P0SEL':0xF3,
1075         'P1SEL':0xF4,
1076         'P2SEL':0xF5,
1077         'P1INP':0xF6,
1078         'P2INP':0xF7,
1079         'U1CSR':0xF8,
1080         'U1DBUF':0xF9,
1081         'U1BAUD':0xFA,
1082         'U1UCR':0xFB,
1083         'U1GCR':0xFC,
1084         'P0DIR':0xFD,
1085         'P1DIR':0xFE,
1086         'P2DIR':0xFF
1087     }
1088     def getSPR(self,args=[]):
1089         """Get special function registers."""
1090         print "Special Function Registers:"
1091         if len(args):
1092             for e in args:
1093                 print "    %-8s : 0x%0.2x"%(e,self.CCpeekcodebyte(self.CCspecfuncregs[e]))
1094         else:
1095             for e in self.CCspecfuncregs.keys():
1096                 print "    %-8s : 0x%0.2x"%(e,self.CCpeekcodebyte(self.CCspecfuncregs[e]))
1097     
1098     def dump(self,file,start=0,stop=0xffff):
1099         """Dump an intel hex file from code memory."""
1100         print "Dumping code from %04x to %04x as %s." % (start,stop,file);
1101         h = IntelHex(None);
1102         i=start;
1103         while i<=stop:
1104             h[i]=self.CCpeekcodebyte(i);
1105             if(i%0x100==0):
1106                 print "Dumped %04x."%i;
1107                 h.write_hex_file(file); #buffer to disk.
1108             i+=1;
1109         h.write_hex_file(file);
1110
1111     def flash(self,file):
1112         """Flash an intel hex file to code memory."""
1113         print "Flashing %s" % file;
1114         
1115         h = IntelHex(file);
1116         page = 0x0000;
1117         pagelen = self.CCpagesize(); #Varies by chip.
1118         
1119         #print "page=%04x, pagelen=%04x" % (page,pagelen);
1120         
1121         bcount = 0;
1122         
1123         #Wipe the RAM buffer for the next flash page.
1124         self.CCeraseflashbuffer();
1125         for i in h._buf.keys():
1126             while(i>=page+pagelen):
1127                 if bcount>0:
1128                     self.CCflashpage(page);
1129                     #client.CCeraseflashbuffer();
1130                     bcount=0;
1131                     print "Flashed page at %06x" % page
1132                 page+=pagelen;
1133                     
1134             #Place byte into buffer.
1135             self.CCpokedatabyte(0xF000+i-page,
1136                                 h[i]);
1137             bcount+=1;
1138             if(i%0x100==0):
1139                 print "Buffering %04x toward %06x" % (i,page);
1140         #last page
1141         self.CCflashpage(page);
1142         print "Flashed final page at %06x" % page;
1143