Better locating of SmartRF Studio 7.
[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 CC2430/CC2530 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     CCpagesizes={0x01: 1024, #"CC1110",
760                  0x11: 1024, #"CC1111",
761                  0x85: 2048, #"CC2430",
762                  0x89: 2048, #"CC2431",
763                  0x81: 1024, #"CC2510",
764                  0x91: 1024, #"CC2511",
765                  0xA5: 2048, #"CC2530", #page 57 of SWRU191B
766                  0xB5: 2048, #"CC2531",
767                  0x95: 2048, #"CC2533",
768                  0x8D: 2048, #"CC2540",
769                  0xFF: 0    } #"CCmissing"};
770     def infostring(self):
771         return self.CCidentstr();
772     def CCidentstr(self):
773         ident=self.CCident();
774         chip=self.CCversions.get(ident&0xFF00);
775         pagesize=self.CCpagesizes.get(ident>0xFF);
776         try:
777             return "%s/r%0.4x/ps0x%0.4x" % (chip, ident, pagesize); 
778         except:
779             return "%04x" % ident;
780     def CCident(self):
781         """Get a chipcon's ID."""
782         self.writecmd(self.APP,0x8B,0,None);
783         chip=ord(self.data[0]);
784         rev=ord(self.data[1]);
785         return (chip<<8)+rev;
786     def CCpagesize(self):
787         """Get a chipcon's ID."""
788         self.writecmd(self.APP,0x8B,0,None);
789         chip=ord(self.data[0]);
790         size=self.CCpagesizes.get(chip);
791         if(size<10):
792             print "ERROR: Pagesize undefined.";
793             print "chip=%0.4x" %chip;
794             sys.exit(1);
795             #return 2048;
796         return size;
797     def getpc(self):
798         return self.CCgetPC();
799     def CCgetPC(self):
800         """Get a chipcon's PC."""
801         self.writecmd(self.APP,0x83,0,None);
802         hi=ord(self.data[0]);
803         lo=ord(self.data[1]);
804         return (hi<<8)+lo;
805     def CCcmd(self,phrase):
806         self.writecmd(self.APP,0x00,len(phrase),phrase);
807         val=ord(self.data[0]);
808         print "Got %02x" % val;
809         return val;
810     def CCdebuginstr(self,instr):
811         self.writecmd(self.APP,0x88,len(instr),instr);
812         return ord(self.data[0]);
813     #def peekblock(self,adr,length,memory="vn"):
814     #    """Return a block of data, broken"""
815     #    data=[adr&0xff, (adr&0xff00)>>8,
816     #          length&0xFF,(length&0xFF00)>>8];
817     #    self.writecmd(self.APP,0x91,4,data);
818     #    return [ord(x) for x in self.data]
819     def peek8(self,address, memory="code"):
820         if(memory=="code" or memory=="flash" or memory=="vn"):
821             return self.CCpeekcodebyte(address);
822         elif(memory=="data" or memory=="xdata" or memory=="ram"):
823             return self.CCpeekdatabyte(address);
824         elif(memory=="idata" or memory=="iram"):
825             return self.CCpeekirambyte(address);
826         print "%s is an unknown memory." % memory;
827         return 0xdead;
828     def CCpeekcodebyte(self,adr):
829         """Read the contents of code memory at an address."""
830         self.data=[adr&0xff, (adr&0xff00)>>8];
831         self.writecmd(self.APP,0x90,2,self.data);
832         return ord(self.data[0]);
833     def CCpeekdatabyte(self,adr):
834         """Read the contents of data memory at an address."""
835         self.data=[adr&0xff, (adr&0xff00)>>8];
836         self.writecmd(self.APP,0x91, 2, self.data);
837         return ord(self.data[0]);
838     def CCpeekirambyte(self,adr):
839         """Read the contents of IRAM at an address."""
840         self.data=[adr&0xff];
841         self.writecmd(self.APP,0x02, 1, self.data);
842         return ord(self.data[0]);
843     def CCpeekiramword(self,adr):
844         """Read the little-endian contents of IRAM at an address."""
845         return self.CCpeekirambyte(adr)+(
846             self.CCpeekirambyte(adr+1)<<8);
847     def CCpokeiramword(self,adr,val):
848         self.CCpokeirambyte(adr,val&0xff);
849         self.CCpokeirambyte(adr+1,(val>>8)&0xff);
850     def CCpokeirambyte(self,adr,val):
851         """Write the contents of IRAM at an address."""
852         self.data=[adr&0xff, val&0xff];
853         self.writecmd(self.APP,0x02, 2, self.data);
854         return ord(self.data[0]);
855     def pokebyte(self,adr,val,mem="xdata"):
856         self.CCpokedatabyte(adr,val);
857     def CCpokedatabyte(self,adr,val):
858         """Write a byte to data memory."""
859         self.data=[adr&0xff, (adr&0xff00)>>8, val];
860         self.writecmd(self.APP, 0x92, 3, self.data);
861         return ord(self.data[0]);
862     def CCchiperase(self):
863         """Erase all of the target's memory."""
864         self.writecmd(self.APP,0x80,0,None);
865     def erase(self):
866         """Erase all of the target's memory."""
867         self.CCchiperase();
868         self.start();
869     
870     def CCstatus(self):
871         """Check the status."""
872         self.writecmd(self.APP,0x84,0,None);
873         return ord(self.data[0])
874     #Same as CC2530
875     CCstatusbits={0x80 : "erase_busy",
876                   0x40 : "pcon_idle",
877                   0x20 : "cpu_halted",
878                   0x10 : "pm0",
879                   0x08 : "halt_status",
880                   0x04 : "locked",
881                   0x02 : "oscstable",
882                   0x01 : "overflow"
883                   };
884     CCconfigbits={0x20 : "soft_power_mode",   #new for CC2530
885                   0x08 : "timers_off",
886                   0x04 : "dma_pause",
887                   0x02 : "timer_suspend",
888                   0x01 : "sel_flash_info_page" #stricken from CC2530
889                   };
890                   
891     def status(self):
892         """Check the status as a string."""
893         status=self.CCstatus();
894         str="";
895         i=1;
896         while i<0x100:
897             if(status&i):
898                 str="%s %s" %(self.CCstatusbits[i],str);
899             i*=2;
900         return str;
901     def start(self):
902         """Start debugging."""
903         self.setup();
904         self.writecmd(self.APP,0x20,0,self.data);
905         ident=self.CCident();
906         if ident==0xFFFF or ident==0x0000:
907             self.writecmd(self.APP,0x20,0,self.data);
908             ident=self.CCident();
909         
910         
911         #print "Target identifies as %s." % ident;
912         #print "Status: %s." % self.status();
913         self.CCreleasecpu();
914         self.CChaltcpu();
915         #Get SmartRF Studio regs if they exist.
916         self.loadsymbols(); 
917         
918     def stop(self):
919         """Stop debugging."""
920         self.writecmd(self.APP,0x21,0,self.data);
921     def CCstep_instr(self):
922         """Step one instruction."""
923         self.writecmd(self.APP,0x89,0,self.data);
924     def CCeraseflashbuffer(self):
925         """Erase the 2kB flash buffer"""
926         self.writecmd(self.APP,0x99);
927     def CCflashpage(self,adr):
928         """Flash 2kB a page of flash from 0xF000 in XDATA"""
929         data=[adr&0xFF,
930               (adr>>8)&0xFF,
931               (adr>>16)&0xFF,
932               (adr>>24)&0xFF];
933         print "Flashing buffer to 0x%06x" % adr;
934         self.writecmd(self.APP,0x95,4,data);
935     
936     def setsecret(self,value):
937         """Set a secret word for later retreival.  Used by glitcher."""
938         page = 0x0000;
939         pagelen = self.CCpagesize(); #Varies by chip.
940         print "page=%04x, pagelen=%04x" % (page,pagelen);
941         
942         self.CCeraseflashbuffer();
943         print "Setting secret to %x" % value;
944         self.CCpokedatabyte(0xF000,value);
945         self.CCpokedatabyte(0xF800,value);
946         print "Setting secret to %x==%x" % (value,
947                                             self.CCpeekdatabyte(0xf000));
948         self.CCflashpage(0);
949         print "code[0]=%x" % self.CCpeekcodebyte(0);
950     def getsecret(self):
951         """Get a secret word.  Used by glitcher."""
952         secret=self.CCpeekcodebyte(0);
953         #print "Got secret %02x" % secret;
954         return secret;
955     
956     #FIXME: This is CC1110-specific and duplicates functionality of 
957     #       SmartRF7 integration.
958     CCspecfuncregs={
959         'P0':0x80,
960         'SP':0x81,
961         'DPL0':0x82,
962         'DPH0':0x83,
963         'DPL1':0x84,
964         'DPH1':0x85,
965         'U0CSR':0x86,
966         'PCON':0x87,
967         'TCON':0x88,
968         'P0IFG':0x89,
969         'P1IFG':0x8A,
970         'P2IFG':0x8B,
971         'PICTL':0x8C,
972         'P1IEN':0x8D,
973         'P0INP':0x8F,
974         'P1':0x90,
975         'RFIM':0x91,
976         'DPS':0x92,
977         'MPAGE':0x93,
978         'ENDIAN':0x95,
979         'S0CON':0x98,
980         'IEN2':0x9A,
981         'S1CON':0x9B,
982         'T2CT':0x9C,
983         'T2PR':0x9D,
984         'T2CTL':0x9E,
985         'P2':0xA0,
986         'WORIRQ':0xA1,
987         'WORCTRL':0xA2,
988         'WOREVT0':0xA3,
989         'WOREVT1':0xA4,
990         'WORTIME0':0xA5,
991         'WORTIME1':0xA6,
992         'IEN0':0xA8,
993         'IP0':0xA9,
994         'FWT':0xAB,
995         'FADDRL':0xAC,
996         'FADDRH':0xAD,
997         'FCTL':0xAE,
998         'FWDATA':0xAF,
999         'ENCDI':0xB1,
1000         'ENCDO':0xB2,
1001         'ENCCS':0xB3,
1002         'ADCCON1':0xB4,
1003         'ADCCON2':0xB5,
1004         'ADCCON3':0xB6,
1005         'IEN1':0xB8,
1006         'IP1':0xB9,
1007         'ADCL':0xBA,
1008         'ADCH':0xBB,
1009         'RNDL':0xBC,
1010         'RNDH':0xBD,
1011         'SLEEP':0xBE,
1012         'IRCON':0xC0,
1013         'U0DBUF':0xC1,
1014         'U0BAUD':0xC2,
1015         'U0UCR':0xC4,
1016         'U0GCR':0xC5,
1017         'CLKCON':0xC6,
1018         'MEMCTR':0xC7,
1019         'WDCTL':0xC9,
1020         'T3CNT':0xCA,
1021         'T3CTL':0xCB,
1022         'T3CCTL0':0xCC,
1023         'T3CC0':0xCD,
1024         'T3CCTL1':0xCE,
1025         'T3CC1':0xCF,
1026         'PSW':0xD0,
1027         'DMAIRQ':0xD1,
1028         'DMA1CFGL':0xD2,
1029         'DMA1CFGH':0xD3,
1030         'DMA0CFGL':0xD4,
1031         'DMA0CFGH':0xD5,
1032         'DMAARM':0xD6,
1033         'DMAREQ':0xD7,
1034         'TIMIF':0xD8,
1035         'RFD':0xD9,
1036         'T1CC0L':0xDA,
1037         'T1CC0H':0xDB,
1038         'T1CC1L':0xDC,
1039         'T1CC1H':0xDD,
1040         'T1CC2L':0xDE,
1041         'T1CC2H':0xDF,
1042         'ACC':0xE0,
1043         'RFST':0xE1,
1044         'T1CNTL':0xE2,
1045         'T1CNTH':0xE3,
1046         'T1CTL':0xE4,
1047         'T1CCTL0':0xE5,
1048         'T1CCTL1':0xE6,
1049         'T1CCTL2':0xE7,
1050         'IRCON2':0xE8,
1051         'RFIF':0xE9,
1052         'T4CNT':0xEA,
1053         'T4CTL':0xEB,
1054         'T4CCTL0':0xEC,
1055         'T4CC0':0xED,
1056         'T4CCTL1':0xEE,
1057         'T4CC1':0xEF,
1058         'B':0xF0,
1059         'PERCFG':0xF1,
1060         'ADCCFG':0xF2,
1061         'P0SEL':0xF3,
1062         'P1SEL':0xF4,
1063         'P2SEL':0xF5,
1064         'P1INP':0xF6,
1065         'P2INP':0xF7,
1066         'U1CSR':0xF8,
1067         'U1DBUF':0xF9,
1068         'U1BAUD':0xFA,
1069         'U1UCR':0xFB,
1070         'U1GCR':0xFC,
1071         'P0DIR':0xFD,
1072         'P1DIR':0xFE,
1073         'P2DIR':0xFF
1074     }
1075     def getSPR(self,args=[]):
1076         """Get special function registers."""
1077         print "Special Function Registers:"
1078         if len(args):
1079             for e in args:
1080                 print "    %-8s : 0x%0.2x"%(e,self.CCpeekcodebyte(self.CCspecfuncregs[e]))
1081         else:
1082             for e in self.CCspecfuncregs.keys():
1083                 print "    %-8s : 0x%0.2x"%(e,self.CCpeekcodebyte(self.CCspecfuncregs[e]))
1084     
1085     def dump(self,file,start=0,stop=0xffff):
1086         """Dump an intel hex file from code memory."""
1087         print "Dumping code from %04x to %04x as %s." % (start,stop,file);
1088         h = IntelHex(None);
1089         i=start;
1090         while i<=stop:
1091             h[i]=self.CCpeekcodebyte(i);
1092             if(i%0x100==0):
1093                 print "Dumped %04x."%i;
1094                 h.write_hex_file(file); #buffer to disk.
1095             i+=1;
1096         h.write_hex_file(file);
1097
1098     def flash(self,file):
1099         """Flash an intel hex file to code memory."""
1100         print "Flashing %s" % file;
1101         
1102         h = IntelHex(file);
1103         page = 0x0000;
1104         pagelen = self.CCpagesize(); #Varies by chip.
1105         
1106         #print "page=%04x, pagelen=%04x" % (page,pagelen);
1107         
1108         bcount = 0;
1109         
1110         #Wipe the RAM buffer for the next flash page.
1111         self.CCeraseflashbuffer();
1112         for i in h._buf.keys():
1113             while(i>=page+pagelen):
1114                 if bcount>0:
1115                     self.CCflashpage(page);
1116                     #client.CCeraseflashbuffer();
1117                     bcount=0;
1118                     print "Flashed page at %06x" % page
1119                 page+=pagelen;
1120                     
1121             #Place byte into buffer.
1122             self.CCpokedatabyte(0xF000+i-page,
1123                                 h[i]);
1124             bcount+=1;
1125             if(i%0x100==0):
1126                 print "Buffering %04x toward %06x" % (i,page);
1127         #last page
1128         self.CCflashpage(page);
1129         print "Flashed final page at %06x" % page;
1130