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