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."""
23 smartrfpath="/opt/smartrf7";
24 def loadsymbols(self):
25 try: self.SRF_loadsymbols();
27 if self.verbose==1: print "SmartRF load failed.";
28 def SRF_chipdom(self,chip="cc1110", doc="register_definition.xml"):
29 fn="%s/config/xml/%s/%s" % (self.smartrfpath,chip,doc);
30 #print "Opening %s" % fn;
31 return xml.dom.minidom.parse(fn)
33 def CMDrs(self,args=[]):
34 """Chip command to grab the radio state."""
35 self.SRF_radiostate();
36 def SRF_bitfieldstr(self,bf):
43 for e in bf.childNodes:
44 if e.localName=="Name" and e.childNodes: name= e.childNodes[0].nodeValue;
45 elif e.localName=="Start": start=e.childNodes[0].nodeValue;
46 elif e.localName=="Stop": stop=e.childNodes[0].nodeValue;
47 return " [%s:%s] %30s " % (start,stop,name);
48 def SRF_radiostate(self):
50 chip=self.CCversions.get(ident&0xFF00);
51 dom=self.SRF_chipdom(chip,"register_definition.xml");
52 for e in dom.getElementsByTagName("registerdefinition"):
53 for f in e.childNodes:
54 if f.localName=="DeviceName":
55 print "// %s RadioState" % (f.childNodes[0].nodeValue);
56 elif f.localName=="Register":
61 for g in f.childNodes:
62 if g.localName=="Name":
63 name=g.childNodes[0].nodeValue;
64 elif g.localName=="Address":
65 address=g.childNodes[0].nodeValue;
66 elif g.localName=="Description":
68 description=g.childNodes[0].nodeValue;
69 elif g.localName=="Bitfield":
70 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
71 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
72 print "%-10s=0x%02x; /* %-50s */" % (
73 name,self.CCpeekdatabyte(eval(address)), description);
74 if bitfields!="": print bitfields.rstrip();
76 """Returns the received signal strenght, from 0 to 1."""
77 rssireg=self.symbols.get("RSSI");
78 return self.CCpeekdatabyte(rssireg);
79 def SRF_loadsymbols(self):
81 chip=self.CCversions.get(ident&0xFF00);
82 dom=self.SRF_chipdom(chip,"register_definition.xml");
83 for e in dom.getElementsByTagName("registerdefinition"):
84 for f in e.childNodes:
85 if f.localName=="Register":
90 for g in f.childNodes:
91 if g.localName=="Name":
92 name=g.childNodes[0].nodeValue;
93 elif g.localName=="Address":
94 address=g.childNodes[0].nodeValue;
95 elif g.localName=="Description":
97 description=g.childNodes[0].nodeValue;
98 elif g.localName=="Bitfield":
99 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
100 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
101 self.symbols.define(eval(address),name,description,"data");
107 self.writecmd(self.APP,0x86,0,self.data);
110 def CCreleasecpu(self):
111 """Resume the CPU."""
112 self.writecmd(self.APP,0x87,0,self.data);
116 #print "Status: %s" % self.CCstatusstr();
118 #Grab ident three times, should be equal.
119 ident1=self.CCident();
120 ident2=self.CCident();
121 ident3=self.CCident();
122 if(ident1!=ident2 or ident2!=ident3):
123 print "Error, repeated ident attempts unequal."
124 print "%04x, %04x, %04x" % (ident1, ident2, ident3);
126 #Single step, printing PC.
127 print "Tracing execution at startup."
128 for i in range(1,15):
130 byte=self.CCpeekcodebyte(i);
131 #print "PC=%04x, %02x" % (pc, byte);
134 print "Verifying that debugging a NOP doesn't affect the PC."
135 for i in range(1,15):
137 self.CCdebuginstr([0x00]);
138 if(pc!=self.CCgetPC()):
139 print "ERROR: PC changed during CCdebuginstr([NOP])!";
141 print "Checking pokes to XRAM."
142 for i in range(0xf000,0xf020):
143 self.CCpokedatabyte(i,0xde);
144 if(self.CCpeekdatabyte(i)!=0xde):
145 print "Error in XDATA at 0x%04x" % i;
147 #print "Status: %s." % self.CCstatusstr();
153 """Move the FET into the CC2430/CC2530 application."""
154 #print "Initializing Chipcon.";
155 self.writecmd(self.APP,0x10,0,self.data);
156 def CCrd_config(self):
157 """Read the config register of a Chipcon."""
158 self.writecmd(self.APP,0x82,0,self.data);
159 return ord(self.data[0]);
160 def CCwr_config(self,config):
161 """Write the config register of a Chipcon."""
162 self.writecmd(self.APP,0x81,1,[config&0xFF]);
163 def CClockchip(self):
164 """Set the flash lock bit in info mem."""
165 self.writecmd(self.APP, 0x9A, 0, None);
167 """Set the flash lock bit in info mem."""
171 CCversions={0x0100:"cc1110",
177 0xA500:"cc2530", #page 52 of SWRU191
180 CCpagesizes={0x01: 1024, #"CC1110",
181 0x11: 1024, #"CC1111",
182 0x85: 2048, #"CC2430",
183 0x89: 2048, #"CC2431",
184 0x81: 1024, #"CC2510",
185 0x91: 1024, #"CC2511",
186 0xA5: 2048, #"CC2530", #page 52 of SWRU191
187 0xB5: 2048, #"CC2531",
188 0xFF: 0 } #"CCmissing"};
189 def infostring(self):
190 return self.CCidentstr();
191 def CCidentstr(self):
192 ident=self.CCident();
193 chip=self.CCversions.get(ident&0xFF00);
194 pagesize=self.CCpagesizes.get(ident>0xFF);
195 return "%s/r%0.4x/ps0x%0.4x" % (chip, ident,pagesize);
197 """Get a chipcon's ID."""
198 self.writecmd(self.APP,0x8B,0,None);
199 chip=ord(self.data[0]);
200 rev=ord(self.data[1]);
201 return (chip<<8)+rev;
202 def CCpagesize(self):
203 """Get a chipcon's ID."""
204 self.writecmd(self.APP,0x8B,0,None);
205 chip=ord(self.data[0]);
206 size=self.CCpagesizes.get(chip);
208 print "ERROR: Pagesize undefined.";
209 print "chip=%0.4x" %chip;
214 return self.CCgetPC();
216 """Get a chipcon's PC."""
217 self.writecmd(self.APP,0x83,0,None);
218 hi=ord(self.data[0]);
219 lo=ord(self.data[1]);
221 def CCcmd(self,phrase):
222 self.writecmd(self.APP,0x00,len(phrase),phrase);
223 val=ord(self.data[0]);
224 print "Got %02x" % val;
226 def CCdebuginstr(self,instr):
227 self.writecmd(self.APP,0x88,len(instr),instr);
228 return ord(self.data[0]);
229 def peek8(self,address, memory="code"):
230 if(memory=="code" or memory=="flash" or memory=="vn"):
231 return self.CCpeekcodebyte(address);
232 elif(memory=="data" or memory=="xdata" or memory=="ram"):
233 return self.CCpeekdatabyte(address);
234 elif(memory=="idata" or memory=="iram"):
235 return self.CCpeekirambyte(address);
236 print "%s is an unknown memory." % memory;
238 def CCpeekcodebyte(self,adr):
239 """Read the contents of code memory at an address."""
240 self.data=[adr&0xff, (adr&0xff00)>>8];
241 self.writecmd(self.APP,0x90,2,self.data);
242 return ord(self.data[0]);
243 def CCpeekdatabyte(self,adr):
244 """Read the contents of data memory at an address."""
245 self.data=[adr&0xff, (adr&0xff00)>>8];
246 self.writecmd(self.APP,0x91, 2, self.data);
247 return ord(self.data[0]);
248 def CCpeekirambyte(self,adr):
249 """Read the contents of IRAM at an address."""
250 self.data=[adr&0xff];
251 self.writecmd(self.APP,0x02, 1, self.data);
252 return ord(self.data[0]);
253 def CCpeekiramword(self,adr):
254 """Read the little-endian contents of IRAM at an address."""
255 return self.CCpeekirambyte(adr)+(
256 self.CCpeekirambyte(adr+1)<<8);
257 def CCpokeiramword(self,adr,val):
258 self.CCpokeirambyte(adr,val&0xff);
259 self.CCpokeirambyte(adr+1,(val>>8)&0xff);
260 def CCpokeirambyte(self,adr,val):
261 """Write the contents of IRAM at an address."""
262 self.data=[adr&0xff, val&0xff];
263 self.writecmd(self.APP,0x02, 2, self.data);
264 return ord(self.data[0]);
266 def CCpokedatabyte(self,adr,val):
267 """Write a byte to data memory."""
268 self.data=[adr&0xff, (adr&0xff00)>>8, val];
269 self.writecmd(self.APP, 0x92, 3, self.data);
270 return ord(self.data[0]);
271 def CCchiperase(self):
272 """Erase all of the target's memory."""
273 self.writecmd(self.APP,0x80,0,None);
275 """Erase all of the target's memory."""
280 """Check the status."""
281 self.writecmd(self.APP,0x84,0,None);
282 return ord(self.data[0])
284 CCstatusbits={0x80 : "erase_busy",
288 0x08 : "halt_status",
293 CCconfigbits={0x20 : "soft_power_mode", #new for CC2530
296 0x02 : "timer_suspend",
297 0x01 : "sel_flash_info_page" #stricken from CC2530
301 """Check the status as a string."""
302 status=self.CCstatus();
307 str="%s %s" %(self.CCstatusbits[i],str);
311 """Start debugging."""
313 self.writecmd(self.APP,0x20,0,self.data);
314 ident=self.CCidentstr();
315 #print "Target identifies as %s." % ident;
316 #print "Status: %s." % self.status();
319 #Get SmartRF Studio regs if they exist.
322 #print "Status: %s." % self.status();
325 """Stop debugging."""
326 self.writecmd(self.APP,0x21,0,self.data);
327 def CCstep_instr(self):
328 """Step one instruction."""
329 self.writecmd(self.APP,0x89,0,self.data);
330 def CCeraseflashbuffer(self):
331 """Erase the 2kB flash buffer"""
332 self.writecmd(self.APP,0x99);
333 def CCflashpage(self,adr):
334 """Flash 2kB a page of flash from 0xF000 in XDATA"""
339 print "Flashing buffer to 0x%06x" % adr;
340 self.writecmd(self.APP,0x95,4,data);
342 def setsecret(self,value):
343 """Set a secret word for later retreival. Used by glitcher."""
345 pagelen = self.CCpagesize(); #Varies by chip.
346 print "page=%04x, pagelen=%04x" % (page,pagelen);
348 self.CCeraseflashbuffer();
349 print "Setting secret to %x" % value;
350 self.CCpokedatabyte(0xF000,value);
351 self.CCpokedatabyte(0xF800,value);
352 print "Setting secret to %x==%x" % (value,
353 self.CCpeekdatabyte(0xf000));
355 print "code[0]=%x" % self.CCpeekcodebyte(0);
357 """Get a secret word. Used by glitcher."""
358 secret=self.CCpeekcodebyte(0);
359 #print "Got secret %02x" % secret;
362 def dump(self,file,start=0,stop=0xffff):
363 """Dump an intel hex file from code memory."""
364 print "Dumping code from %04x to %04x as %s." % (start,stop,file);
368 h[i]=self.CCpeekcodebyte(i);
370 print "Dumped %04x."%i;
371 h.write_hex_file(file); #buffer to disk.
373 h.write_hex_file(file);
375 def flash(self,file):
376 """Flash an intel hex file to code memory."""
377 print "Flashing %s" % file;
381 pagelen = self.CCpagesize(); #Varies by chip.
383 #print "page=%04x, pagelen=%04x" % (page,pagelen);
387 #Wipe the RAM buffer for the next flash page.
388 self.CCeraseflashbuffer();
389 for i in h._buf.keys():
390 while(i>=page+pagelen):
392 self.CCflashpage(page);
393 #client.CCeraseflashbuffer();
395 print "Flashed page at %06x" % page
398 #Place byte into buffer.
399 self.CCpokedatabyte(0xF000+i-page,
403 print "Buffering %04x toward %06x" % (i,page);
405 self.CCflashpage(page);
406 print "Flashed final page at %06x" % page;