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