2 # GoodFET Client Library
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
6 # This code is being rewritten and refactored. You've been warned!
11 from GoodFET import GoodFET;
12 from intelhex import IntelHex;
14 import xml.dom.minidom;
16 class GoodFETCC(GoodFET):
17 """A GoodFET variant for use with Chipcon 8051 Zigbee SoC."""
19 smartrfpath="/opt/smartrf7";
20 def loadsymbols(self):
21 try: self.SRF_loadsymbols();
22 except: print "SmartRF load failed.";
23 def SRF_chipdom(self,chip="cc1110", doc="register_definition.xml"):
24 fn="%s/config/xml/%s/%s" % (self.smartrfpath,chip,doc);
25 print "Opening %s" % fn;
26 return xml.dom.minidom.parse(fn)
28 def CMDrs(self,args=[]):
29 """Chip command to grab the radio state."""
30 self.SRF_radiostate();
31 def SRF_bitfieldstr(self,bf):
38 for e in bf.childNodes:
39 if e.localName=="Name" and e.childNodes: name= e.childNodes[0].nodeValue;
40 elif e.localName=="Start": start=e.childNodes[0].nodeValue;
41 elif e.localName=="Stop": stop=e.childNodes[0].nodeValue;
42 return " [%s:%s] %30s " % (start,stop,name);
43 def SRF_radiostate(self):
45 chip=self.CCversions.get(ident&0xFF00);
46 dom=self.SRF_chipdom(chip,"register_definition.xml");
47 for e in dom.getElementsByTagName("registerdefinition"):
48 for f in e.childNodes:
49 if f.localName=="DeviceName":
50 print "// %s RadioState" % (f.childNodes[0].nodeValue);
51 elif f.localName=="Register":
56 for g in f.childNodes:
57 if g.localName=="Name":
58 name=g.childNodes[0].nodeValue;
59 elif g.localName=="Address":
60 address=g.childNodes[0].nodeValue;
61 elif g.localName=="Description":
63 description=g.childNodes[0].nodeValue;
64 elif g.localName=="Bitfield":
65 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
66 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
67 print "%-10s=0x%02x; /* %-50s */" % (
68 name,self.CCpeekdatabyte(eval(address)), description);
69 if bitfields!="": print bitfields.rstrip();
70 def SRF_loadsymbols(self):
72 chip=self.CCversions.get(ident&0xFF00);
73 dom=self.SRF_chipdom(chip,"register_definition.xml");
74 for e in dom.getElementsByTagName("registerdefinition"):
75 for f in e.childNodes:
76 if f.localName=="Register":
81 for g in f.childNodes:
82 if g.localName=="Name":
83 name=g.childNodes[0].nodeValue;
84 elif g.localName=="Address":
85 address=g.childNodes[0].nodeValue;
86 elif g.localName=="Description":
88 description=g.childNodes[0].nodeValue;
89 elif g.localName=="Bitfield":
90 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
91 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
92 self.symbols.define(eval(address),name,description,"data");
98 self.writecmd(self.APP,0x86,0,self.data);
101 def CCreleasecpu(self):
102 """Resume the CPU."""
103 self.writecmd(self.APP,0x87,0,self.data);
107 #print "Status: %s" % self.CCstatusstr();
109 #Grab ident three times, should be equal.
110 ident1=self.CCident();
111 ident2=self.CCident();
112 ident3=self.CCident();
113 if(ident1!=ident2 or ident2!=ident3):
114 print "Error, repeated ident attempts unequal."
115 print "%04x, %04x, %04x" % (ident1, ident2, ident3);
117 #Single step, printing PC.
118 print "Tracing execution at startup."
119 for i in range(1,15):
121 byte=self.CCpeekcodebyte(i);
122 #print "PC=%04x, %02x" % (pc, byte);
125 print "Verifying that debugging a NOP doesn't affect the PC."
126 for i in range(1,15):
128 self.CCdebuginstr([0x00]);
129 if(pc!=self.CCgetPC()):
130 print "ERROR: PC changed during CCdebuginstr([NOP])!";
132 print "Checking pokes to XRAM."
133 for i in range(0xf000,0xf020):
134 self.CCpokedatabyte(i,0xde);
135 if(self.CCpeekdatabyte(i)!=0xde):
136 print "Error in XDATA at 0x%04x" % i;
138 #print "Status: %s." % self.CCstatusstr();
144 """Move the FET into the CC2430/CC2530 application."""
145 #print "Initializing Chipcon.";
146 self.writecmd(self.APP,0x10,0,self.data);
147 def CCrd_config(self):
148 """Read the config register of a Chipcon."""
149 self.writecmd(self.APP,0x82,0,self.data);
150 return ord(self.data[0]);
151 def CCwr_config(self,config):
152 """Write the config register of a Chipcon."""
153 self.writecmd(self.APP,0x81,1,[config&0xFF]);
154 def CClockchip(self):
155 """Set the flash lock bit in info mem."""
156 self.writecmd(self.APP, 0x9A, 0, None);
158 """Set the flash lock bit in info mem."""
162 CCversions={0x0100:"cc1110",
167 0xA500:"cc2530", #page 52 of SWRU191
170 CCpagesizes={0x01: 1024, #"CC1110",
171 0x85: 2048, #"CC2430",
172 0x89: 2048, #"CC2431",
173 0x81: 1024, #"CC2510",
174 0x91: 1024, #"CC2511",
175 0xA5: 2048, #"CC2530", #page 52 of SWRU191
176 0xB5: 2048, #"CC2531",
177 0xFF: 0 } #"CCmissing"};
178 def infostring(self):
179 return self.CCidentstr();
180 def CCidentstr(self):
181 ident=self.CCident();
182 chip=self.CCversions.get(ident&0xFF00);
183 return "%s/r%02x" % (chip, ident&0xFF);
185 """Get a chipcon's ID."""
186 self.writecmd(self.APP,0x8B,0,None);
187 chip=ord(self.data[0]);
188 rev=ord(self.data[1]);
189 return (chip<<8)+rev;
190 def CCpagesize(self):
191 """Get a chipcon's ID."""
192 self.writecmd(self.APP,0x8B,0,None);
193 chip=ord(self.data[0]);
194 size=self.CCpagesizes.get(chip);
196 print "ERROR: Pagesize undefined.";
197 print "chip=%02x" %chip;
202 return self.CCgetPC();
204 """Get a chipcon's PC."""
205 self.writecmd(self.APP,0x83,0,None);
206 hi=ord(self.data[0]);
207 lo=ord(self.data[1]);
209 def CCcmd(self,phrase):
210 self.writecmd(self.APP,0x00,len(phrase),phrase);
211 val=ord(self.data[0]);
212 print "Got %02x" % val;
214 def CCdebuginstr(self,instr):
215 self.writecmd(self.APP,0x88,len(instr),instr);
216 return ord(self.data[0]);
217 def peek8(self,address, memory="code"):
218 if(memory=="code" or memory=="flash" or memory=="vn"):
219 return self.CCpeekcodebyte(address);
220 elif(memory=="data" or memory=="xdata" or memory=="ram"):
221 return self.CCpeekdatabyte(address);
222 elif(memory=="idata" or memory=="iram"):
223 return self.CCpeekirambyte(address);
224 print "%s is an unknown memory." % memory;
226 def CCpeekcodebyte(self,adr):
227 """Read the contents of code memory at an address."""
228 self.data=[adr&0xff, (adr&0xff00)>>8];
229 self.writecmd(self.APP,0x90,2,self.data);
230 return ord(self.data[0]);
231 def CCpeekdatabyte(self,adr):
232 """Read the contents of data memory at an address."""
233 self.data=[adr&0xff, (adr&0xff00)>>8];
234 self.writecmd(self.APP,0x91, 2, self.data);
235 return ord(self.data[0]);
236 def CCpeekirambyte(self,adr):
237 """Read the contents of IRAM at an address."""
238 self.data=[adr&0xff];
239 self.writecmd(self.APP,0x02, 1, self.data);
240 return ord(self.data[0]);
241 def CCpeekiramword(self,adr):
242 """Read the little-endian contents of IRAM at an address."""
243 return self.CCpeekirambyte(adr)+(
244 self.CCpeekirambyte(adr+1)<<8);
245 def CCpokeiramword(self,adr,val):
246 self.CCpokeirambyte(adr,val&0xff);
247 self.CCpokeirambyte(adr+1,(val>>8)&0xff);
248 def CCpokeirambyte(self,adr,val):
249 """Write the contents of IRAM at an address."""
250 self.data=[adr&0xff, val&0xff];
251 self.writecmd(self.APP,0x02, 2, self.data);
252 return ord(self.data[0]);
254 def CCpokedatabyte(self,adr,val):
255 """Write a byte to data memory."""
256 self.data=[adr&0xff, (adr&0xff00)>>8, val];
257 self.writecmd(self.APP, 0x92, 3, self.data);
258 return ord(self.data[0]);
259 def CCchiperase(self):
260 """Erase all of the target's memory."""
261 self.writecmd(self.APP,0x80,0,None);
263 """Erase all of the target's memory."""
267 """Check the status."""
268 self.writecmd(self.APP,0x84,0,None);
269 return ord(self.data[0])
271 CCstatusbits={0x80 : "erase_busy",
275 0x08 : "halt_status",
280 CCconfigbits={0x20 : "soft_power_mode", #new for CC2530
283 0x02 : "timer_suspend",
284 0x01 : "sel_flash_info_page" #stricken from CC2530
288 """Check the status as a string."""
289 status=self.CCstatus();
294 str="%s %s" %(self.CCstatusbits[i],str);
298 """Start debugging."""
300 self.writecmd(self.APP,0x20,0,self.data);
301 ident=self.CCidentstr();
302 #print "Target identifies as %s." % ident;
303 #print "Status: %s." % self.status();
306 #print "Status: %s." % self.status();
309 """Stop debugging."""
310 self.writecmd(self.APP,0x21,0,self.data);
311 def CCstep_instr(self):
312 """Step one instruction."""
313 self.writecmd(self.APP,0x89,0,self.data);
314 def CCeraseflashbuffer(self):
315 """Erase the 2kB flash buffer"""
316 self.writecmd(self.APP,0x99);
317 def CCflashpage(self,adr):
318 """Flash 2kB a page of flash from 0xF000 in XDATA"""
323 print "Flashing buffer to 0x%06x" % adr;
324 self.writecmd(self.APP,0x95,4,data);
325 def dump(self,file,start=0,stop=0xffff):
326 """Dump an intel hex file from code memory."""
327 print "Dumping code from %04x to %04x as %s." % (start,stop,file);
331 h[i]=self.CCpeekcodebyte(i);
333 print "Dumped %04x."%i;
334 h.write_hex_file(file); #buffer to disk.
336 h.write_hex_file(file);
338 def flash(self,file):
339 """Flash an intel hex file to code memory."""
340 print "Flashing %s" % file;
344 pagelen = self.CCpagesize(); #Varies by chip.
346 #print "page=%04x, pagelen=%04x" % (page,pagelen);
350 #Wipe the RAM buffer for the next flash page.
351 self.CCeraseflashbuffer();
352 for i in h._buf.keys():
353 while(i>=page+pagelen):
355 self.CCflashpage(page);
356 #client.CCeraseflashbuffer();
358 print "Flashed page at %06x" % page
361 #Place byte into buffer.
362 self.CCpokedatabyte(0xF000+i-page,
366 print "Buffering %04x toward %06x" % (i,page);
368 self.CCflashpage(page);
369 print "Flashed final page at %06x" % page;