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