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();
23 if self.verbose==1: print "SmartRF load failed.";
24 def SRF_chipdom(self,chip="cc1110", doc="register_definition.xml"):
25 fn="%s/config/xml/%s/%s" % (self.smartrfpath,chip,doc);
26 #print "Opening %s" % fn;
27 return xml.dom.minidom.parse(fn)
29 def CMDrs(self,args=[]):
30 """Chip command to grab the radio state."""
31 self.SRF_radiostate();
32 def SRF_bitfieldstr(self,bf):
39 for e in bf.childNodes:
40 if e.localName=="Name" and e.childNodes: name= e.childNodes[0].nodeValue;
41 elif e.localName=="Start": start=e.childNodes[0].nodeValue;
42 elif e.localName=="Stop": stop=e.childNodes[0].nodeValue;
43 return " [%s:%s] %30s " % (start,stop,name);
44 def SRF_radiostate(self):
46 chip=self.CCversions.get(ident&0xFF00);
47 dom=self.SRF_chipdom(chip,"register_definition.xml");
48 for e in dom.getElementsByTagName("registerdefinition"):
49 for f in e.childNodes:
50 if f.localName=="DeviceName":
51 print "// %s RadioState" % (f.childNodes[0].nodeValue);
52 elif f.localName=="Register":
57 for g in f.childNodes:
58 if g.localName=="Name":
59 name=g.childNodes[0].nodeValue;
60 elif g.localName=="Address":
61 address=g.childNodes[0].nodeValue;
62 elif g.localName=="Description":
64 description=g.childNodes[0].nodeValue;
65 elif g.localName=="Bitfield":
66 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
67 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
68 print "%-10s=0x%02x; /* %-50s */" % (
69 name,self.CCpeekdatabyte(eval(address)), description);
70 if bitfields!="": print bitfields.rstrip();
71 def SRF_loadsymbols(self):
73 chip=self.CCversions.get(ident&0xFF00);
74 dom=self.SRF_chipdom(chip,"register_definition.xml");
75 for e in dom.getElementsByTagName("registerdefinition"):
76 for f in e.childNodes:
77 if f.localName=="Register":
82 for g in f.childNodes:
83 if g.localName=="Name":
84 name=g.childNodes[0].nodeValue;
85 elif g.localName=="Address":
86 address=g.childNodes[0].nodeValue;
87 elif g.localName=="Description":
89 description=g.childNodes[0].nodeValue;
90 elif g.localName=="Bitfield":
91 bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
92 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
93 self.symbols.define(eval(address),name,description,"data");
99 self.writecmd(self.APP,0x86,0,self.data);
102 def CCreleasecpu(self):
103 """Resume the CPU."""
104 self.writecmd(self.APP,0x87,0,self.data);
108 #print "Status: %s" % self.CCstatusstr();
110 #Grab ident three times, should be equal.
111 ident1=self.CCident();
112 ident2=self.CCident();
113 ident3=self.CCident();
114 if(ident1!=ident2 or ident2!=ident3):
115 print "Error, repeated ident attempts unequal."
116 print "%04x, %04x, %04x" % (ident1, ident2, ident3);
118 #Single step, printing PC.
119 print "Tracing execution at startup."
120 for i in range(1,15):
122 byte=self.CCpeekcodebyte(i);
123 #print "PC=%04x, %02x" % (pc, byte);
126 print "Verifying that debugging a NOP doesn't affect the PC."
127 for i in range(1,15):
129 self.CCdebuginstr([0x00]);
130 if(pc!=self.CCgetPC()):
131 print "ERROR: PC changed during CCdebuginstr([NOP])!";
133 print "Checking pokes to XRAM."
134 for i in range(0xf000,0xf020):
135 self.CCpokedatabyte(i,0xde);
136 if(self.CCpeekdatabyte(i)!=0xde):
137 print "Error in XDATA at 0x%04x" % i;
139 #print "Status: %s." % self.CCstatusstr();
145 """Move the FET into the CC2430/CC2530 application."""
146 #print "Initializing Chipcon.";
147 self.writecmd(self.APP,0x10,0,self.data);
148 def CCrd_config(self):
149 """Read the config register of a Chipcon."""
150 self.writecmd(self.APP,0x82,0,self.data);
151 return ord(self.data[0]);
152 def CCwr_config(self,config):
153 """Write the config register of a Chipcon."""
154 self.writecmd(self.APP,0x81,1,[config&0xFF]);
155 def CClockchip(self):
156 """Set the flash lock bit in info mem."""
157 self.writecmd(self.APP, 0x9A, 0, None);
159 """Set the flash lock bit in info mem."""
163 CCversions={0x0100:"cc1110",
168 0xA500:"cc2530", #page 52 of SWRU191
171 CCpagesizes={0x01: 1024, #"CC1110",
172 0x85: 2048, #"CC2430",
173 0x89: 2048, #"CC2431",
174 0x81: 1024, #"CC2510",
175 0x91: 1024, #"CC2511",
176 0xA5: 2048, #"CC2530", #page 52 of SWRU191
177 0xB5: 2048, #"CC2531",
178 0xFF: 0 } #"CCmissing"};
179 def infostring(self):
180 return self.CCidentstr();
181 def CCidentstr(self):
182 ident=self.CCident();
183 chip=self.CCversions.get(ident&0xFF00);
184 return "%s/r%02x" % (chip, ident&0xFF);
186 """Get a chipcon's ID."""
187 self.writecmd(self.APP,0x8B,0,None);
188 chip=ord(self.data[0]);
189 rev=ord(self.data[1]);
190 return (chip<<8)+rev;
191 def CCpagesize(self):
192 """Get a chipcon's ID."""
193 self.writecmd(self.APP,0x8B,0,None);
194 chip=ord(self.data[0]);
195 size=self.CCpagesizes.get(chip);
197 print "ERROR: Pagesize undefined.";
198 print "chip=%02x" %chip;
203 return self.CCgetPC();
205 """Get a chipcon's PC."""
206 self.writecmd(self.APP,0x83,0,None);
207 hi=ord(self.data[0]);
208 lo=ord(self.data[1]);
210 def CCcmd(self,phrase):
211 self.writecmd(self.APP,0x00,len(phrase),phrase);
212 val=ord(self.data[0]);
213 print "Got %02x" % val;
215 def CCdebuginstr(self,instr):
216 self.writecmd(self.APP,0x88,len(instr),instr);
217 return ord(self.data[0]);
218 def peek8(self,address, memory="code"):
219 if(memory=="code" or memory=="flash" or memory=="vn"):
220 return self.CCpeekcodebyte(address);
221 elif(memory=="data" or memory=="xdata" or memory=="ram"):
222 return self.CCpeekdatabyte(address);
223 elif(memory=="idata" or memory=="iram"):
224 return self.CCpeekirambyte(address);
225 print "%s is an unknown memory." % memory;
227 def CCpeekcodebyte(self,adr):
228 """Read the contents of code memory at an address."""
229 self.data=[adr&0xff, (adr&0xff00)>>8];
230 self.writecmd(self.APP,0x90,2,self.data);
231 return ord(self.data[0]);
232 def CCpeekdatabyte(self,adr):
233 """Read the contents of data memory at an address."""
234 self.data=[adr&0xff, (adr&0xff00)>>8];
235 self.writecmd(self.APP,0x91, 2, self.data);
236 return ord(self.data[0]);
237 def CCpeekirambyte(self,adr):
238 """Read the contents of IRAM at an address."""
239 self.data=[adr&0xff];
240 self.writecmd(self.APP,0x02, 1, self.data);
241 return ord(self.data[0]);
242 def CCpeekiramword(self,adr):
243 """Read the little-endian contents of IRAM at an address."""
244 return self.CCpeekirambyte(adr)+(
245 self.CCpeekirambyte(adr+1)<<8);
246 def CCpokeiramword(self,adr,val):
247 self.CCpokeirambyte(adr,val&0xff);
248 self.CCpokeirambyte(adr+1,(val>>8)&0xff);
249 def CCpokeirambyte(self,adr,val):
250 """Write the contents of IRAM at an address."""
251 self.data=[adr&0xff, val&0xff];
252 self.writecmd(self.APP,0x02, 2, self.data);
253 return ord(self.data[0]);
255 def CCpokedatabyte(self,adr,val):
256 """Write a byte to data memory."""
257 self.data=[adr&0xff, (adr&0xff00)>>8, val];
258 self.writecmd(self.APP, 0x92, 3, self.data);
259 return ord(self.data[0]);
260 def CCchiperase(self):
261 """Erase all of the target's memory."""
262 self.writecmd(self.APP,0x80,0,None);
264 """Erase all of the target's memory."""
268 """Check the status."""
269 self.writecmd(self.APP,0x84,0,None);
270 return ord(self.data[0])
272 CCstatusbits={0x80 : "erase_busy",
276 0x08 : "halt_status",
281 CCconfigbits={0x20 : "soft_power_mode", #new for CC2530
284 0x02 : "timer_suspend",
285 0x01 : "sel_flash_info_page" #stricken from CC2530
289 """Check the status as a string."""
290 status=self.CCstatus();
295 str="%s %s" %(self.CCstatusbits[i],str);
299 """Start debugging."""
301 self.writecmd(self.APP,0x20,0,self.data);
302 ident=self.CCidentstr();
303 #print "Target identifies as %s." % ident;
304 #print "Status: %s." % self.status();
307 #Get SmartRF Studio regs if they exist.
310 #print "Status: %s." % self.status();
313 """Stop debugging."""
314 self.writecmd(self.APP,0x21,0,self.data);
315 def CCstep_instr(self):
316 """Step one instruction."""
317 self.writecmd(self.APP,0x89,0,self.data);
318 def CCeraseflashbuffer(self):
319 """Erase the 2kB flash buffer"""
320 self.writecmd(self.APP,0x99);
321 def CCflashpage(self,adr):
322 """Flash 2kB a page of flash from 0xF000 in XDATA"""
327 print "Flashing buffer to 0x%06x" % adr;
328 self.writecmd(self.APP,0x95,4,data);
329 def dump(self,file,start=0,stop=0xffff):
330 """Dump an intel hex file from code memory."""
331 print "Dumping code from %04x to %04x as %s." % (start,stop,file);
335 h[i]=self.CCpeekcodebyte(i);
337 print "Dumped %04x."%i;
338 h.write_hex_file(file); #buffer to disk.
340 h.write_hex_file(file);
342 def flash(self,file):
343 """Flash an intel hex file to code memory."""
344 print "Flashing %s" % file;
348 pagelen = self.CCpagesize(); #Varies by chip.
350 #print "page=%04x, pagelen=%04x" % (page,pagelen);
354 #Wipe the RAM buffer for the next flash page.
355 self.CCeraseflashbuffer();
356 for i in h._buf.keys():
357 while(i>=page+pagelen):
359 self.CCflashpage(page);
360 #client.CCeraseflashbuffer();
362 print "Flashed page at %06x" % page
365 #Place byte into buffer.
366 self.CCpokedatabyte(0xF000+i-page,
370 print "Buffering %04x toward %06x" % (i,page);
372 self.CCflashpage(page);
373 print "Flashed final page at %06x" % page;