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 self.SRF_loadsymbols();
22 def SRF_chipdom(self,chip="cc1110", doc="register_definition.xml"):
23 fn="%s/config/xml/%s/%s" % (self.smartrfpath,chip,doc);
24 print "Opening %s" % fn;
25 return xml.dom.minidom.parse(fn)
26 def CMDrs(self,args=[]):
27 """Chip command to grab the radio state."""
28 self.SRF_radiostate();
29 def SRF_bitfieldstr(self,bf):
36 for e in bf.childNodes:
37 if e.localName=="Name" and e.childNodes: name= e.childNodes[0].nodeValue;
38 elif e.localName=="Start": start=e.childNodes[0].nodeValue;
39 elif e.localName=="Stop": stop=e.childNodes[0].nodeValue;
40 return " [%s:%s] %30s " % (start,stop,name);
41 def SRF_radiostate(self):
43 chip=self.CCversions.get(ident&0xFF00);
44 dom=self.SRF_chipdom(chip,"register_definition.xml");
45 for e in dom.getElementsByTagName("registerdefinition"):
46 for f in e.childNodes:
47 if f.localName=="DeviceName":
48 print "// %s RadioState" % (f.childNodes[0].nodeValue);
49 elif f.localName=="Register":
54 for g in f.childNodes:
55 if g.localName=="Name":
56 name=g.childNodes[0].nodeValue;
57 elif g.localName=="Address":
58 address=g.childNodes[0].nodeValue;
59 elif g.localName=="Description":
61 description=g.childNodes[0].nodeValue;
62 elif g.localName=="Bitfield":
63 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
64 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
65 print "%-10s=0x%02x; /* %-50s */" % (
66 name,self.CCpeekdatabyte(eval(address)), description);
67 if bitfields!="": print bitfields.rstrip();
68 def SRF_loadsymbols(self):
70 chip=self.CCversions.get(ident&0xFF00);
71 dom=self.SRF_chipdom(chip,"register_definition.xml");
72 for e in dom.getElementsByTagName("registerdefinition"):
73 for f in e.childNodes:
74 if f.localName=="Register":
79 for g in f.childNodes:
80 if g.localName=="Name":
81 name=g.childNodes[0].nodeValue;
82 elif g.localName=="Address":
83 address=g.childNodes[0].nodeValue;
84 elif g.localName=="Description":
86 description=g.childNodes[0].nodeValue;
87 elif g.localName=="Bitfield":
88 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
89 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
90 self.symbols.define(eval(address),name,description,"data");
96 self.writecmd(self.APP,0x86,0,self.data);
99 def CCreleasecpu(self):
100 """Resume the CPU."""
101 self.writecmd(self.APP,0x87,0,self.data);
105 #print "Status: %s" % self.CCstatusstr();
107 #Grab ident three times, should be equal.
108 ident1=self.CCident();
109 ident2=self.CCident();
110 ident3=self.CCident();
111 if(ident1!=ident2 or ident2!=ident3):
112 print "Error, repeated ident attempts unequal."
113 print "%04x, %04x, %04x" % (ident1, ident2, ident3);
115 #Single step, printing PC.
116 print "Tracing execution at startup."
117 for i in range(1,15):
119 byte=self.CCpeekcodebyte(i);
120 #print "PC=%04x, %02x" % (pc, byte);
123 print "Verifying that debugging a NOP doesn't affect the PC."
124 for i in range(1,15):
126 self.CCdebuginstr([0x00]);
127 if(pc!=self.CCgetPC()):
128 print "ERROR: PC changed during CCdebuginstr([NOP])!";
130 print "Checking pokes to XRAM."
131 for i in range(0xf000,0xf020):
132 self.CCpokedatabyte(i,0xde);
133 if(self.CCpeekdatabyte(i)!=0xde):
134 print "Error in XDATA at 0x%04x" % i;
136 #print "Status: %s." % self.CCstatusstr();
142 """Move the FET into the CC2430/CC2530 application."""
143 #print "Initializing Chipcon.";
144 self.writecmd(self.APP,0x10,0,self.data);
145 def CCrd_config(self):
146 """Read the config register of a Chipcon."""
147 self.writecmd(self.APP,0x82,0,self.data);
148 return ord(self.data[0]);
149 def CCwr_config(self,config):
150 """Write the config register of a Chipcon."""
151 self.writecmd(self.APP,0x81,1,[config&0xFF]);
152 def CClockchip(self):
153 """Set the flash lock bit in info mem."""
154 self.writecmd(self.APP, 0x9A, 0, None);
156 """Set the flash lock bit in info mem."""
160 CCversions={0x0100:"cc1110",
165 0xA500:"cc2530", #page 52 of SWRU191
168 CCpagesizes={0x01: 1024, #"CC1110",
169 0x85: 2048, #"CC2430",
170 0x89: 2048, #"CC2431",
171 0x81: 1024, #"CC2510",
172 0x91: 1024, #"CC2511",
173 0xA5: 2048, #"CC2530", #page 52 of SWRU191
174 0xB5: 2048, #"CC2531",
175 0xFF: 0 } #"CCmissing"};
176 def infostring(self):
177 return self.CCidentstr();
178 def CCidentstr(self):
179 ident=self.CCident();
180 chip=self.CCversions.get(ident&0xFF00);
181 return "%s/r%02x" % (chip, ident&0xFF);
183 """Get a chipcon's ID."""
184 self.writecmd(self.APP,0x8B,0,None);
185 chip=ord(self.data[0]);
186 rev=ord(self.data[1]);
187 return (chip<<8)+rev;
188 def CCpagesize(self):
189 """Get a chipcon's ID."""
190 self.writecmd(self.APP,0x8B,0,None);
191 chip=ord(self.data[0]);
192 size=self.CCpagesizes.get(chip);
194 print "ERROR: Pagesize undefined.";
195 print "chip=%02x" %chip;
200 return self.CCgetPC();
202 """Get a chipcon's PC."""
203 self.writecmd(self.APP,0x83,0,None);
204 hi=ord(self.data[0]);
205 lo=ord(self.data[1]);
207 def CCcmd(self,phrase):
208 self.writecmd(self.APP,0x00,len(phrase),phrase);
209 val=ord(self.data[0]);
210 print "Got %02x" % val;
212 def CCdebuginstr(self,instr):
213 self.writecmd(self.APP,0x88,len(instr),instr);
214 return ord(self.data[0]);
215 def peek8(self,address, memory="code"):
216 if(memory=="code" or memory=="flash" or memory=="vn"):
217 return self.CCpeekcodebyte(address);
218 elif(memory=="data" or memory=="xdata" or memory=="ram"):
219 return self.CCpeekdatabyte(address);
220 elif(memory=="idata" or memory=="iram"):
221 return self.CCpeekirambyte(address);
222 print "%s is an unknown memory." % memory;
224 def CCpeekcodebyte(self,adr):
225 """Read the contents of code memory at an address."""
226 self.data=[adr&0xff, (adr&0xff00)>>8];
227 self.writecmd(self.APP,0x90,2,self.data);
228 return ord(self.data[0]);
229 def CCpeekdatabyte(self,adr):
230 """Read the contents of data memory at an address."""
231 self.data=[adr&0xff, (adr&0xff00)>>8];
232 self.writecmd(self.APP,0x91, 2, self.data);
233 return ord(self.data[0]);
234 def CCpeekirambyte(self,adr):
235 """Read the contents of IRAM at an address."""
236 self.data=[adr&0xff];
237 self.writecmd(self.APP,0x02, 1, self.data);
238 return ord(self.data[0]);
239 def CCpeekiramword(self,adr):
240 """Read the little-endian contents of IRAM at an address."""
241 return self.CCpeekirambyte(adr)+(
242 self.CCpeekirambyte(adr+1)<<8);
243 def CCpokeiramword(self,adr,val):
244 self.CCpokeirambyte(adr,val&0xff);
245 self.CCpokeirambyte(adr+1,(val>>8)&0xff);
246 def CCpokeirambyte(self,adr,val):
247 """Write the contents of IRAM at an address."""
248 self.data=[adr&0xff, val&0xff];
249 self.writecmd(self.APP,0x02, 2, self.data);
250 return ord(self.data[0]);
252 def CCpokedatabyte(self,adr,val):
253 """Write a byte to data memory."""
254 self.data=[adr&0xff, (adr&0xff00)>>8, val];
255 self.writecmd(self.APP, 0x92, 3, self.data);
256 return ord(self.data[0]);
257 def CCchiperase(self):
258 """Erase all of the target's memory."""
259 self.writecmd(self.APP,0x80,0,None);
261 """Erase all of the target's memory."""
265 """Check the status."""
266 self.writecmd(self.APP,0x84,0,None);
267 return ord(self.data[0])
269 CCstatusbits={0x80 : "erase_busy",
273 0x08 : "halt_status",
278 CCconfigbits={0x20 : "soft_power_mode", #new for CC2530
281 0x02 : "timer_suspend",
282 0x01 : "sel_flash_info_page" #stricken from CC2530
286 """Check the status as a string."""
287 status=self.CCstatus();
292 str="%s %s" %(self.CCstatusbits[i],str);
296 """Start debugging."""
298 self.writecmd(self.APP,0x20,0,self.data);
299 ident=self.CCidentstr();
300 #print "Target identifies as %s." % ident;
301 #print "Status: %s." % self.status();
304 #print "Status: %s." % self.status();
307 """Stop debugging."""
308 self.writecmd(self.APP,0x21,0,self.data);
309 def CCstep_instr(self):
310 """Step one instruction."""
311 self.writecmd(self.APP,0x89,0,self.data);
312 def CCeraseflashbuffer(self):
313 """Erase the 2kB flash buffer"""
314 self.writecmd(self.APP,0x99);
315 def CCflashpage(self,adr):
316 """Flash 2kB a page of flash from 0xF000 in XDATA"""
321 print "Flashing buffer to 0x%06x" % adr;
322 self.writecmd(self.APP,0x95,4,data);
323 def dump(self,file,start=0,stop=0xffff):
324 """Dump an intel hex file from code memory."""
325 print "Dumping code from %04x to %04x as %s." % (start,stop,file);
329 h[i]=self.CCpeekcodebyte(i);
331 print "Dumped %04x."%i;
332 h.write_hex_file(file); #buffer to disk.
334 h.write_hex_file(file);
336 def flash(self,file):
337 """Flash an intel hex file to code memory."""
338 print "Flashing %s" % file;
342 pagelen = self.CCpagesize(); #Varies by chip.
344 #print "page=%04x, pagelen=%04x" % (page,pagelen);
348 #Wipe the RAM buffer for the next flash page.
349 self.CCeraseflashbuffer();
350 for i in h._buf.keys():
351 while(i>=page+pagelen):
353 self.CCflashpage(page);
354 #client.CCeraseflashbuffer();
356 print "Flashed page at %06x" % page
359 #Place byte into buffer.
360 self.CCpokedatabyte(0xF000+i-page,
364 print "Buffering %04x toward %06x" % (i,page);
366 self.CCflashpage(page);
367 print "Flashed final page at %06x" % page;