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 SRF_chipdom(self,chip="cc1110", doc="register_definition.xml"):
21 fn="%s/config/xml/%s/%s" % (self.smartrfpath,chip,doc);
22 print "Opening %s" % fn;
23 return xml.dom.minidom.parse(fn)
24 def CMDrs(self,args=[]):
25 """Chip command to grab the radio state."""
26 self.SRF_radiostate();
27 def SRF_radiostate(self):
29 chip=self.CCversions.get(ident&0xFF00);
30 dom=self.SRF_chipdom(chip,"register_definition.xml");
31 for e in dom.getElementsByTagName("registerdefinition"):
32 for f in e.childNodes:
33 if f.localName=="DeviceName":
34 print "// %s RadioState" % (f.childNodes[0].nodeValue);
35 elif f.localName=="Register":
39 for g in f.childNodes:
40 if g.localName=="Name":
41 name=g.childNodes[0].nodeValue;
42 elif g.localName=="Address":
43 address=g.childNodes[0].nodeValue;
44 elif g.localName=="Description":
45 description=g.childNodes[0].nodeValue;
46 #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
47 print "%10s=0x%02x; /* %50s */" % (
48 name,self.CCpeekdatabyte(eval(address)), description);
51 self.writecmd(0x30,0x86,0,self.data);
52 def CCreleasecpu(self):
54 self.writecmd(0x30,0x87,0,self.data);
58 #print "Status: %s" % self.CCstatusstr();
60 #Grab ident three times, should be equal.
61 ident1=self.CCident();
62 ident2=self.CCident();
63 ident3=self.CCident();
64 if(ident1!=ident2 or ident2!=ident3):
65 print "Error, repeated ident attempts unequal."
66 print "%04x, %04x, %04x" % (ident1, ident2, ident3);
68 #Single step, printing PC.
69 print "Tracing execution at startup."
72 byte=self.CCpeekcodebyte(i);
73 #print "PC=%04x, %02x" % (pc, byte);
76 print "Verifying that debugging a NOP doesn't affect the PC."
79 self.CCdebuginstr([0x00]);
80 if(pc!=self.CCgetPC()):
81 print "ERROR: PC changed during CCdebuginstr([NOP])!";
83 print "Checking pokes to XRAM."
84 for i in range(0xf000,0xf020):
85 self.CCpokedatabyte(i,0xde);
86 if(self.CCpeekdatabyte(i)!=0xde):
87 print "Error in XDATA at 0x%04x" % i;
89 #print "Status: %s." % self.CCstatusstr();
95 """Move the FET into the CC2430/CC2530 application."""
96 #print "Initializing Chipcon.";
97 self.writecmd(0x30,0x10,0,self.data);
98 def CCrd_config(self):
99 """Read the config register of a Chipcon."""
100 self.writecmd(0x30,0x82,0,self.data);
101 return ord(self.data[0]);
102 def CCwr_config(self,config):
103 """Write the config register of a Chipcon."""
104 self.writecmd(0x30,0x81,1,[config&0xFF]);
105 def CClockchip(self):
106 """Set the flash lock bit in info mem."""
107 self.writecmd(0x30, 0x9A, 0, None);
109 """Set the flash lock bit in info mem."""
113 CCversions={0x0100:"cc1110",
118 0xA500:"cc2530", #page 52 of SWRU191
121 CCpagesizes={0x01: 1024, #"CC1110",
122 0x85: 2048, #"CC2430",
123 0x89: 2048, #"CC2431",
124 0x81: 1024, #"CC2510",
125 0x91: 1024, #"CC2511",
126 0xA5: 2048, #"CC2530", #page 52 of SWRU191
127 0xB5: 2048, #"CC2531",
128 0xFF: 0 } #"CCmissing"};
129 def infostring(self):
130 return self.CCidentstr();
131 def CCidentstr(self):
132 ident=self.CCident();
133 chip=self.CCversions.get(ident&0xFF00);
134 return "%s/r%02x" % (chip, ident&0xFF);
136 """Get a chipcon's ID."""
137 self.writecmd(0x30,0x8B,0,None);
138 chip=ord(self.data[0]);
139 rev=ord(self.data[1]);
140 return (chip<<8)+rev;
141 def CCpagesize(self):
142 """Get a chipcon's ID."""
143 self.writecmd(0x30,0x8B,0,None);
144 chip=ord(self.data[0]);
145 size=self.CCpagesizes.get(chip);
147 print "ERROR: Pagesize undefined.";
148 print "chip=%02x" %chip;
153 """Get a chipcon's PC."""
154 self.writecmd(0x30,0x83,0,None);
155 hi=ord(self.data[0]);
156 lo=ord(self.data[1]);
158 def CCcmd(self,phrase):
159 self.writecmd(0x30,0x00,len(phrase),phrase);
160 val=ord(self.data[0]);
161 print "Got %02x" % val;
163 def CCdebuginstr(self,instr):
164 self.writecmd(0x30,0x88,len(instr),instr);
165 return ord(self.data[0]);
166 def peek8(self,address, memory="code"):
167 if(memory=="code" or memory=="flash" or memory=="vn"):
168 return self.CCpeekcodebyte(address);
169 elif(memory=="data" or memory=="xdata" or memory=="ram"):
170 return self.CCpeekdatabyte(address);
171 elif(memory=="idata" or memory=="iram"):
172 return self.CCpeekirambyte(address);
173 print "%s is an unknown memory." % memory;
175 def CCpeekcodebyte(self,adr):
176 """Read the contents of code memory at an address."""
177 self.data=[adr&0xff, (adr&0xff00)>>8];
178 self.writecmd(0x30,0x90,2,self.data);
179 return ord(self.data[0]);
180 def CCpeekdatabyte(self,adr):
181 """Read the contents of data memory at an address."""
182 self.data=[adr&0xff, (adr&0xff00)>>8];
183 self.writecmd(0x30,0x91, 2, self.data);
184 return ord(self.data[0]);
185 def CCpeekirambyte(self,adr):
186 """Read the contents of IRAM at an address."""
187 self.data=[adr&0xff];
188 self.writecmd(0x30,0x02, 1, self.data);
189 return ord(self.data[0]);
190 def CCpeekiramword(self,adr):
191 """Read the little-endian contents of IRAM at an address."""
192 return self.CCpeekirambyte(adr)+(
193 self.CCpeekirambyte(adr+1)<<8);
194 def CCpokeiramword(self,adr,val):
195 self.CCpokeirambyte(adr,val&0xff);
196 self.CCpokeirambyte(adr+1,(val>>8)&0xff);
197 def CCpokeirambyte(self,adr,val):
198 """Write the contents of IRAM at an address."""
199 self.data=[adr&0xff, val&0xff];
200 self.writecmd(0x30,0x02, 2, self.data);
201 return ord(self.data[0]);
203 def CCpokedatabyte(self,adr,val):
204 """Write a byte to data memory."""
205 self.data=[adr&0xff, (adr&0xff00)>>8, val];
206 self.writecmd(0x30, 0x92, 3, self.data);
207 return ord(self.data[0]);
208 def CCchiperase(self):
209 """Erase all of the target's memory."""
210 self.writecmd(0x30,0x80,0,None);
212 """Erase all of the target's memory."""
216 """Check the status."""
217 self.writecmd(0x30,0x84,0,None);
218 return ord(self.data[0])
220 CCstatusbits={0x80 : "erase_busy",
224 0x08 : "halt_status",
229 CCconfigbits={0x20 : "soft_power_mode", #new for CC2530
232 0x02 : "timer_suspend",
233 0x01 : "sel_flash_info_page" #stricken from CC2530
237 """Check the status as a string."""
238 status=self.CCstatus();
243 str="%s %s" %(self.CCstatusbits[i],str);
247 """Start debugging."""
248 self.writecmd(0x30,0x20,0,self.data);
249 ident=self.CCidentstr();
250 #print "Target identifies as %s." % ident;
251 #print "Status: %s." % self.status();
254 #print "Status: %s." % self.status();
257 """Stop debugging."""
258 self.writecmd(0x30,0x21,0,self.data);
259 def CCstep_instr(self):
260 """Step one instruction."""
261 self.writecmd(0x30,0x89,0,self.data);
262 def CCeraseflashbuffer(self):
263 """Erase the 2kB flash buffer"""
264 self.writecmd(0x30,0x99);
265 def CCflashpage(self,adr):
266 """Flash 2kB a page of flash from 0xF000 in XDATA"""
271 print "Flashing buffer to 0x%06x" % adr;
272 self.writecmd(0x30,0x95,4,data);
274 def flash(self,file):
275 """Flash an intel hex file to code memory."""
276 print "Flashing %s" % file;
280 pagelen = self.CCpagesize(); #Varies by chip.
282 #print "page=%04x, pagelen=%04x" % (page,pagelen);
286 #Wipe the RAM buffer for the next flash page.
287 self.CCeraseflashbuffer();
288 for i in h._buf.keys():
289 while(i>=page+pagelen):
291 self.CCflashpage(page);
292 #client.CCeraseflashbuffer();
294 print "Flashed page at %06x" % page
297 #Place byte into buffer.
298 self.CCpokedatabyte(0xF000+i-page,
302 print "Buffering %04x toward %06x" % (i,page);
304 self.CCflashpage(page);
305 print "Flashed final page at %06x" % page;