Might fix pagesize issue for some Chipcon devices.
[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     def CChaltcpu(self):
17         """Halt the CPU."""
18         self.writecmd(0x30,0x86,0,self.data);
19     def CCreleasecpu(self):
20         """Resume the CPU."""
21         self.writecmd(0x30,0x87,0,self.data);
22     def CCtest(self):
23         self.CCreleasecpu();
24         self.CChaltcpu();
25         #print "Status: %s" % self.CCstatusstr();
26         
27         #Grab ident three times, should be equal.
28         ident1=self.CCident();
29         ident2=self.CCident();
30         ident3=self.CCident();
31         if(ident1!=ident2 or ident2!=ident3):
32             print "Error, repeated ident attempts unequal."
33             print "%04x, %04x, %04x" % (ident1, ident2, ident3);
34         
35         #Single step, printing PC.
36         print "Tracing execution at startup."
37         for i in range(1,15):
38             pc=self.CCgetPC();
39             byte=self.CCpeekcodebyte(i);
40             #print "PC=%04x, %02x" % (pc, byte);
41             self.CCstep_instr();
42         
43         print "Verifying that debugging a NOP doesn't affect the PC."
44         for i in range(1,15):
45             pc=self.CCgetPC();
46             self.CCdebuginstr([0x00]);
47             if(pc!=self.CCgetPC()):
48                 print "ERROR: PC changed during CCdebuginstr([NOP])!";
49         
50         print "Checking pokes to XRAM."
51         for i in range(0xf000,0xf020):
52             self.CCpokedatabyte(i,0xde);
53             if(self.CCpeekdatabyte(i)!=0xde):
54                 print "Error in XDATA at 0x%04x" % i;
55         
56         #print "Status: %s." % self.CCstatusstr();
57         #Exit debugger
58         self.stop();
59         print "Done.";
60
61     def setup(self):
62         """Move the FET into the CC2430/CC2530 application."""
63         #print "Initializing Chipcon.";
64         self.writecmd(0x30,0x10,0,self.data);
65     def CCrd_config(self):
66         """Read the config register of a Chipcon."""
67         self.writecmd(0x30,0x82,0,self.data);
68         return ord(self.data[0]);
69     def CCwr_config(self,config):
70         """Write the config register of a Chipcon."""
71         self.writecmd(0x30,0x81,1,[config&0xFF]);
72     def CClockchip(self):
73         """Set the flash lock bit in info mem."""
74         self.writecmd(0x30, 0x9A, 0, None);
75     
76
77     CCversions={0x0100:"CC1110",
78                 0x8500:"CC2430",
79                 0x8900:"CC2431",
80                 0x8100:"CC2510",
81                 0x9100:"CC2511",
82                 0xA500:"CC2530", #page 52 of SWRU191
83                 0xB500:"CC2531",
84                 0xFF00:"CCmissing"};
85     CCpagesizes={0x01: 1024, #"CC1110",
86                  0x85: 2048, #"CC2430",
87                  0x89: 2048, #"CC2431",
88                  0x81: 1024, #"CC2510",
89                  0x91: 1024, #"CC2511",
90                  0xA5: 2048, #"CC2530", #page 52 of SWRU191
91                  0xB5: 2048, #"CC2531",
92                  0xFF: 0    } #"CCmissing"};
93
94     def CCidentstr(self):
95         ident=self.CCident();
96         chip=self.CCversions.get(ident&0xFF00);
97         return "%s/r%02x" % (chip, ident&0xFF); 
98     def CCident(self):
99         """Get a chipcon's ID."""
100         self.writecmd(0x30,0x8B,0,None);
101         chip=ord(self.data[0]);
102         rev=ord(self.data[1]);
103         return (chip<<8)+rev;
104     def CCpagesize(self):
105         """Get a chipcon's ID."""
106         self.writecmd(0x30,0x8B,0,None);
107         chip=ord(self.data[0]);
108         size=self.CCpagesizes.get(chip);
109         if(size<10):
110             print "ERROR: Pagesize undefined.";
111             print "chip=%02x" %chip;
112             sys.exit(1);
113             #return 2048;
114         return size;
115     def CCgetPC(self):
116         """Get a chipcon's PC."""
117         self.writecmd(0x30,0x83,0,None);
118         hi=ord(self.data[0]);
119         lo=ord(self.data[1]);
120         return (hi<<8)+lo;
121     def CCcmd(self,phrase):
122         self.writecmd(0x30,0x00,len(phrase),phrase);
123         val=ord(self.data[0]);
124         print "Got %02x" % val;
125         return val;
126     def CCdebuginstr(self,instr):
127         self.writecmd(0x30,0x88,len(instr),instr);
128         return ord(self.data[0]);
129     def CCpeekcodebyte(self,adr):
130         """Read the contents of code memory at an address."""
131         self.data=[adr&0xff, (adr&0xff00)>>8];
132         self.writecmd(0x30,0x90,2,self.data);
133         return ord(self.data[0]);
134     def CCpeekdatabyte(self,adr):
135         """Read the contents of data memory at an address."""
136         self.data=[adr&0xff, (adr&0xff00)>>8];
137         self.writecmd(0x30,0x91, 2, self.data);
138         return ord(self.data[0]);
139     def CCpeekirambyte(self,adr):
140         """Read the contents of IRAM at an address."""
141         self.data=[adr&0xff];
142         self.writecmd(0x30,0x02, 1, self.data);
143         return ord(self.data[0]);
144     def CCpeekiramword(self,adr):
145         """Read the little-endian contents of IRAM at an address."""
146         return self.CCpeekirambyte(adr)+(
147             self.CCpeekirambyte(adr+1)<<8);
148     def CCpokeiramword(self,adr,val):
149         self.CCpokeirambyte(adr,val&0xff);
150         self.CCpokeirambyte(adr+1,(val>>8)&0xff);
151     def CCpokeirambyte(self,adr,val):
152         """Write the contents of IRAM at an address."""
153         self.data=[adr&0xff, val&0xff];
154         self.writecmd(0x30,0x02, 2, self.data);
155         return ord(self.data[0]);
156     
157     def CCpokedatabyte(self,adr,val):
158         """Write a byte to data memory."""
159         self.data=[adr&0xff, (adr&0xff00)>>8, val];
160         self.writecmd(0x30, 0x92, 3, self.data);
161         return ord(self.data[0]);
162     def CCchiperase(self):
163         """Erase all of the target's memory."""
164         self.writecmd(0x30,0x80,0,None);
165     def CCstatus(self):
166         """Check the status."""
167         self.writecmd(0x30,0x84,0,None);
168         return ord(self.data[0])
169     #Same as CC2530
170     CCstatusbits={0x80 : "erase_busy",
171                   0x40 : "pcon_idle",
172                   0x20 : "cpu_halted",
173                   0x10 : "pm0",
174                   0x08 : "halt_status",
175                   0x04 : "locked",
176                   0x02 : "oscstable",
177                   0x01 : "overflow"
178                   };
179     CCconfigbits={0x20 : "soft_power_mode",   #new for CC2530
180                   0x08 : "timers_off",
181                   0x04 : "dma_pause",
182                   0x02 : "timer_suspend",
183                   0x01 : "sel_flash_info_page" #stricken from CC2530
184                   };
185                   
186     def CCstatusstr(self):
187         """Check the status as a string."""
188         status=self.CCstatus();
189         str="";
190         i=1;
191         while i<0x100:
192             if(status&i):
193                 str="%s %s" %(self.CCstatusbits[i],str);
194             i*=2;
195         return str;
196     def start(self):
197         """Start debugging."""
198         self.writecmd(0x30,0x20,0,self.data);
199         ident=self.CCidentstr();
200         print "Target identifies as %s." % ident;
201         #print "Status: %s." % self.CCstatusstr();
202         self.CCreleasecpu();
203         self.CChaltcpu();
204         #print "Status: %s." % self.CCstatusstr();
205         
206     def stop(self):
207         """Stop debugging."""
208         self.writecmd(0x30,0x21,0,self.data);
209     def CCstep_instr(self):
210         """Step one instruction."""
211         self.writecmd(0x30,0x89,0,self.data);
212     def CCeraseflashbuffer(self):
213         """Erase the 2kB flash buffer"""
214         self.writecmd(0x30,0x99);
215     def CCflashpage(self,adr):
216         """Flash 2kB a page of flash from 0xF000 in XDATA"""
217         data=[adr&0xFF,
218               (adr>>8)&0xFF,
219               (adr>>16)&0xFF,
220               (adr>>24)&0xFF];
221         print "Flashing buffer to 0x%06x" % adr;
222         self.writecmd(0x30,0x95,4,data);