894db94ea8f867ee1a718ca1d08496aba7340d19
[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 Zigbeema 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="vn"):
136         return self.CCpeekcodebyte(address);
137
138     def CCpeekcodebyte(self,adr):
139         """Read the contents of code memory at an address."""
140         self.data=[adr&0xff, (adr&0xff00)>>8];
141         self.writecmd(0x30,0x90,2,self.data);
142         return ord(self.data[0]);
143     def CCpeekdatabyte(self,adr):
144         """Read the contents of data memory at an address."""
145         self.data=[adr&0xff, (adr&0xff00)>>8];
146         self.writecmd(0x30,0x91, 2, self.data);
147         return ord(self.data[0]);
148     def CCpeekirambyte(self,adr):
149         """Read the contents of IRAM at an address."""
150         self.data=[adr&0xff];
151         self.writecmd(0x30,0x02, 1, self.data);
152         return ord(self.data[0]);
153     def CCpeekiramword(self,adr):
154         """Read the little-endian contents of IRAM at an address."""
155         return self.CCpeekirambyte(adr)+(
156             self.CCpeekirambyte(adr+1)<<8);
157     def CCpokeiramword(self,adr,val):
158         self.CCpokeirambyte(adr,val&0xff);
159         self.CCpokeirambyte(adr+1,(val>>8)&0xff);
160     def CCpokeirambyte(self,adr,val):
161         """Write the contents of IRAM at an address."""
162         self.data=[adr&0xff, val&0xff];
163         self.writecmd(0x30,0x02, 2, self.data);
164         return ord(self.data[0]);
165     
166     def CCpokedatabyte(self,adr,val):
167         """Write a byte to data memory."""
168         self.data=[adr&0xff, (adr&0xff00)>>8, val];
169         self.writecmd(0x30, 0x92, 3, self.data);
170         return ord(self.data[0]);
171     def CCchiperase(self):
172         """Erase all of the target's memory."""
173         self.writecmd(0x30,0x80,0,None);
174     def erase(self):
175         """Erase all of the target's memory."""
176         self.CCchiperase();
177     
178     def CCstatus(self):
179         """Check the status."""
180         self.writecmd(0x30,0x84,0,None);
181         return ord(self.data[0])
182     #Same as CC2530
183     CCstatusbits={0x80 : "erase_busy",
184                   0x40 : "pcon_idle",
185                   0x20 : "cpu_halted",
186                   0x10 : "pm0",
187                   0x08 : "halt_status",
188                   0x04 : "locked",
189                   0x02 : "oscstable",
190                   0x01 : "overflow"
191                   };
192     CCconfigbits={0x20 : "soft_power_mode",   #new for CC2530
193                   0x08 : "timers_off",
194                   0x04 : "dma_pause",
195                   0x02 : "timer_suspend",
196                   0x01 : "sel_flash_info_page" #stricken from CC2530
197                   };
198                   
199     def status(self):
200         """Check the status as a string."""
201         status=self.CCstatus();
202         str="";
203         i=1;
204         while i<0x100:
205             if(status&i):
206                 str="%s %s" %(self.CCstatusbits[i],str);
207             i*=2;
208         return str;
209     def start(self):
210         """Start debugging."""
211         self.writecmd(0x30,0x20,0,self.data);
212         ident=self.CCidentstr();
213         #print "Target identifies as %s." % ident;
214         #print "Status: %s." % self.status();
215         self.CCreleasecpu();
216         self.CChaltcpu();
217         #print "Status: %s." % self.status();
218         
219     def stop(self):
220         """Stop debugging."""
221         self.writecmd(0x30,0x21,0,self.data);
222     def CCstep_instr(self):
223         """Step one instruction."""
224         self.writecmd(0x30,0x89,0,self.data);
225     def CCeraseflashbuffer(self):
226         """Erase the 2kB flash buffer"""
227         self.writecmd(0x30,0x99);
228     def CCflashpage(self,adr):
229         """Flash 2kB a page of flash from 0xF000 in XDATA"""
230         data=[adr&0xFF,
231               (adr>>8)&0xFF,
232               (adr>>16)&0xFF,
233               (adr>>24)&0xFF];
234         print "Flashing buffer to 0x%06x" % adr;
235         self.writecmd(0x30,0x95,4,data);