Bit of rearranging of Chipcon support.
[goodfet] / client / GoodFETCC.py
1 #!/usr/bin/env python
2 # GoodFET Client Library
3
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # This code is being rewritten and refactored.  You've been warned!
7
8 import sys;
9 import binascii;
10
11 from GoodFET import GoodFET;
12 from intelhex import IntelHex;
13
14 class GoodFETCC(GoodFET):
15     """A GoodFET variant for use with Chipcon 8051 Zigbee SoC."""
16     APP=0x30;
17     
18     def CChaltcpu(self):
19         """Halt the CPU."""
20         self.writecmd(0x30,0x86,0,self.data);
21     def CCreleasecpu(self):
22         """Resume the CPU."""
23         self.writecmd(0x30,0x87,0,self.data);
24     def test(self):
25         self.CCreleasecpu();
26         self.CChaltcpu();
27         #print "Status: %s" % self.CCstatusstr();
28         
29         #Grab ident three times, should be equal.
30         ident1=self.CCident();
31         ident2=self.CCident();
32         ident3=self.CCident();
33         if(ident1!=ident2 or ident2!=ident3):
34             print "Error, repeated ident attempts unequal."
35             print "%04x, %04x, %04x" % (ident1, ident2, ident3);
36         
37         #Single step, printing PC.
38         print "Tracing execution at startup."
39         for i in range(1,15):
40             pc=self.CCgetPC();
41             byte=self.CCpeekcodebyte(i);
42             #print "PC=%04x, %02x" % (pc, byte);
43             self.CCstep_instr();
44         
45         print "Verifying that debugging a NOP doesn't affect the PC."
46         for i in range(1,15):
47             pc=self.CCgetPC();
48             self.CCdebuginstr([0x00]);
49             if(pc!=self.CCgetPC()):
50                 print "ERROR: PC changed during CCdebuginstr([NOP])!";
51         
52         print "Checking pokes to XRAM."
53         for i in range(0xf000,0xf020):
54             self.CCpokedatabyte(i,0xde);
55             if(self.CCpeekdatabyte(i)!=0xde):
56                 print "Error in XDATA at 0x%04x" % i;
57         
58         #print "Status: %s." % self.CCstatusstr();
59         #Exit debugger
60         self.stop();
61         print "Done.";
62
63     def setup(self):
64         """Move the FET into the CC2430/CC2530 application."""
65         #print "Initializing Chipcon.";
66         self.writecmd(0x30,0x10,0,self.data);
67     def CCrd_config(self):
68         """Read the config register of a Chipcon."""
69         self.writecmd(0x30,0x82,0,self.data);
70         return ord(self.data[0]);
71     def CCwr_config(self,config):
72         """Write the config register of a Chipcon."""
73         self.writecmd(0x30,0x81,1,[config&0xFF]);
74     def CClockchip(self):
75         """Set the flash lock bit in info mem."""
76         self.writecmd(0x30, 0x9A, 0, None);
77     def lock(self):
78         """Set the flash lock bit in info mem."""
79         self.CClockchip();
80     
81
82     CCversions={0x0100:"CC1110",
83                 0x8500:"CC2430",
84                 0x8900:"CC2431",
85                 0x8100:"CC2510",
86                 0x9100:"CC2511",
87                 0xA500:"CC2530", #page 52 of SWRU191
88                 0xB500:"CC2531",
89                 0xFF00:"CCmissing"};
90     CCpagesizes={0x01: 1024, #"CC1110",
91                  0x85: 2048, #"CC2430",
92                  0x89: 2048, #"CC2431",
93                  0x81: 1024, #"CC2510",
94                  0x91: 1024, #"CC2511",
95                  0xA5: 2048, #"CC2530", #page 52 of SWRU191
96                  0xB5: 2048, #"CC2531",
97                  0xFF: 0    } #"CCmissing"};
98     def infostring(self):
99         return self.CCidentstr();
100     def CCidentstr(self):
101         ident=self.CCident();
102         chip=self.CCversions.get(ident&0xFF00);
103         return "%s/r%02x" % (chip, ident&0xFF); 
104     def CCident(self):
105         """Get a chipcon's ID."""
106         self.writecmd(0x30,0x8B,0,None);
107         chip=ord(self.data[0]);
108         rev=ord(self.data[1]);
109         return (chip<<8)+rev;
110     def CCpagesize(self):
111         """Get a chipcon's ID."""
112         self.writecmd(0x30,0x8B,0,None);
113         chip=ord(self.data[0]);
114         size=self.CCpagesizes.get(chip);
115         if(size<10):
116             print "ERROR: Pagesize undefined.";
117             print "chip=%02x" %chip;
118             sys.exit(1);
119             #return 2048;
120         return size;
121     def CCgetPC(self):
122         """Get a chipcon's PC."""
123         self.writecmd(0x30,0x83,0,None);
124         hi=ord(self.data[0]);
125         lo=ord(self.data[1]);
126         return (hi<<8)+lo;
127     def CCcmd(self,phrase):
128         self.writecmd(0x30,0x00,len(phrase),phrase);
129         val=ord(self.data[0]);
130         print "Got %02x" % val;
131         return val;
132     def CCdebuginstr(self,instr):
133         self.writecmd(0x30,0x88,len(instr),instr);
134         return ord(self.data[0]);
135     def peek8(self,address, memory="code"):
136         if(memory=="code" or memory=="flash" or memory=="vn"):
137             return self.CCpeekcodebyte(address);
138         elif(memory=="data" or memory=="xdata" or memory=="ram"):
139             return self.CCpeekdatabyte(address);
140         elif(memory=="idata" or memory=="iram"):
141             return self.CCpeekirambyte(address);
142         print "%s is an unknown memory." % memory;
143         return 0xdead;
144     def CCpeekcodebyte(self,adr):
145         """Read the contents of code memory at an address."""
146         self.data=[adr&0xff, (adr&0xff00)>>8];
147         self.writecmd(0x30,0x90,2,self.data);
148         return ord(self.data[0]);
149     def CCpeekdatabyte(self,adr):
150         """Read the contents of data memory at an address."""
151         self.data=[adr&0xff, (adr&0xff00)>>8];
152         self.writecmd(0x30,0x91, 2, self.data);
153         return ord(self.data[0]);
154     def CCpeekirambyte(self,adr):
155         """Read the contents of IRAM at an address."""
156         self.data=[adr&0xff];
157         self.writecmd(0x30,0x02, 1, self.data);
158         return ord(self.data[0]);
159     def CCpeekiramword(self,adr):
160         """Read the little-endian contents of IRAM at an address."""
161         return self.CCpeekirambyte(adr)+(
162             self.CCpeekirambyte(adr+1)<<8);
163     def CCpokeiramword(self,adr,val):
164         self.CCpokeirambyte(adr,val&0xff);
165         self.CCpokeirambyte(adr+1,(val>>8)&0xff);
166     def CCpokeirambyte(self,adr,val):
167         """Write the contents of IRAM at an address."""
168         self.data=[adr&0xff, val&0xff];
169         self.writecmd(0x30,0x02, 2, self.data);
170         return ord(self.data[0]);
171     
172     def CCpokedatabyte(self,adr,val):
173         """Write a byte to data memory."""
174         self.data=[adr&0xff, (adr&0xff00)>>8, val];
175         self.writecmd(0x30, 0x92, 3, self.data);
176         return ord(self.data[0]);
177     def CCchiperase(self):
178         """Erase all of the target's memory."""
179         self.writecmd(0x30,0x80,0,None);
180     def erase(self):
181         """Erase all of the target's memory."""
182         self.CCchiperase();
183     
184     def CCstatus(self):
185         """Check the status."""
186         self.writecmd(0x30,0x84,0,None);
187         return ord(self.data[0])
188     #Same as CC2530
189     CCstatusbits={0x80 : "erase_busy",
190                   0x40 : "pcon_idle",
191                   0x20 : "cpu_halted",
192                   0x10 : "pm0",
193                   0x08 : "halt_status",
194                   0x04 : "locked",
195                   0x02 : "oscstable",
196                   0x01 : "overflow"
197                   };
198     CCconfigbits={0x20 : "soft_power_mode",   #new for CC2530
199                   0x08 : "timers_off",
200                   0x04 : "dma_pause",
201                   0x02 : "timer_suspend",
202                   0x01 : "sel_flash_info_page" #stricken from CC2530
203                   };
204                   
205     def status(self):
206         """Check the status as a string."""
207         status=self.CCstatus();
208         str="";
209         i=1;
210         while i<0x100:
211             if(status&i):
212                 str="%s %s" %(self.CCstatusbits[i],str);
213             i*=2;
214         return str;
215     def start(self):
216         """Start debugging."""
217         self.writecmd(0x30,0x20,0,self.data);
218         ident=self.CCidentstr();
219         #print "Target identifies as %s." % ident;
220         #print "Status: %s." % self.status();
221         self.CCreleasecpu();
222         self.CChaltcpu();
223         #print "Status: %s." % self.status();
224         
225     def stop(self):
226         """Stop debugging."""
227         self.writecmd(0x30,0x21,0,self.data);
228     def CCstep_instr(self):
229         """Step one instruction."""
230         self.writecmd(0x30,0x89,0,self.data);
231     def CCeraseflashbuffer(self):
232         """Erase the 2kB flash buffer"""
233         self.writecmd(0x30,0x99);
234     def CCflashpage(self,adr):
235         """Flash 2kB a page of flash from 0xF000 in XDATA"""
236         data=[adr&0xFF,
237               (adr>>8)&0xFF,
238               (adr>>16)&0xFF,
239               (adr>>24)&0xFF];
240         print "Flashing buffer to 0x%06x" % adr;
241         self.writecmd(0x30,0x95,4,data);
242
243     def flash(self,file):
244         """Flash an intel hex file to code memory."""
245         print "Flashing %s" % file;
246         
247         h = IntelHex(file);
248         page = 0x0000;
249         pagelen = self.CCpagesize(); #Varies by chip.
250         
251         #print "page=%04x, pagelen=%04x" % (page,pagelen);
252         
253         bcount = 0;
254         
255         #Wipe the RAM buffer for the next flash page.
256         self.CCeraseflashbuffer();
257         for i in h._buf.keys():
258             while(i>=page+pagelen):
259                 if bcount>0:
260                     self.CCflashpage(page);
261                     #client.CCeraseflashbuffer();
262                     bcount=0;
263                     print "Flashed page at %06x" % page
264                 page+=pagelen;
265                     
266             #Place byte into buffer.
267             self.CCpokedatabyte(0xF000+i-page,
268                                 h[i]);
269             bcount+=1;
270             if(i%0x100==0):
271                 print "Buffering %04x toward %06x" % (i,page);
272         #last page
273         self.CCflashpage(page);
274         print "Flashed final page at %06x" % page;
275