New Chipcon library stuff.
[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 CCtest(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
99     def CCidentstr(self):
100         ident=self.CCident();
101         chip=self.CCversions.get(ident&0xFF00);
102         return "%s/r%02x" % (chip, ident&0xFF); 
103     def CCident(self):
104         """Get a chipcon's ID."""
105         self.writecmd(0x30,0x8B,0,None);
106         chip=ord(self.data[0]);
107         rev=ord(self.data[1]);
108         return (chip<<8)+rev;
109     def CCpagesize(self):
110         """Get a chipcon's ID."""
111         self.writecmd(0x30,0x8B,0,None);
112         chip=ord(self.data[0]);
113         size=self.CCpagesizes.get(chip);
114         if(size<10):
115             print "ERROR: Pagesize undefined.";
116             print "chip=%02x" %chip;
117             sys.exit(1);
118             #return 2048;
119         return size;
120     def CCgetPC(self):
121         """Get a chipcon's PC."""
122         self.writecmd(0x30,0x83,0,None);
123         hi=ord(self.data[0]);
124         lo=ord(self.data[1]);
125         return (hi<<8)+lo;
126     def CCcmd(self,phrase):
127         self.writecmd(0x30,0x00,len(phrase),phrase);
128         val=ord(self.data[0]);
129         print "Got %02x" % val;
130         return val;
131     def CCdebuginstr(self,instr):
132         self.writecmd(0x30,0x88,len(instr),instr);
133         return ord(self.data[0]);
134     def CCpeekcodebyte(self,adr):
135         """Read the contents of code memory at an address."""
136         self.data=[adr&0xff, (adr&0xff00)>>8];
137         self.writecmd(0x30,0x90,2,self.data);
138         return ord(self.data[0]);
139     def CCpeekdatabyte(self,adr):
140         """Read the contents of data memory at an address."""
141         self.data=[adr&0xff, (adr&0xff00)>>8];
142         self.writecmd(0x30,0x91, 2, self.data);
143         return ord(self.data[0]);
144     def CCpeekirambyte(self,adr):
145         """Read the contents of IRAM at an address."""
146         self.data=[adr&0xff];
147         self.writecmd(0x30,0x02, 1, self.data);
148         return ord(self.data[0]);
149     def CCpeekiramword(self,adr):
150         """Read the little-endian contents of IRAM at an address."""
151         return self.CCpeekirambyte(adr)+(
152             self.CCpeekirambyte(adr+1)<<8);
153     def CCpokeiramword(self,adr,val):
154         self.CCpokeirambyte(adr,val&0xff);
155         self.CCpokeirambyte(adr+1,(val>>8)&0xff);
156     def CCpokeirambyte(self,adr,val):
157         """Write the contents of IRAM at an address."""
158         self.data=[adr&0xff, val&0xff];
159         self.writecmd(0x30,0x02, 2, self.data);
160         return ord(self.data[0]);
161     
162     def CCpokedatabyte(self,adr,val):
163         """Write a byte to data memory."""
164         self.data=[adr&0xff, (adr&0xff00)>>8, val];
165         self.writecmd(0x30, 0x92, 3, self.data);
166         return ord(self.data[0]);
167     def CCchiperase(self):
168         """Erase all of the target's memory."""
169         self.writecmd(0x30,0x80,0,None);
170     def erase(self):
171         """Erase all of the target's memory."""
172         self.CCchiperase();
173     
174     def CCstatus(self):
175         """Check the status."""
176         self.writecmd(0x30,0x84,0,None);
177         return ord(self.data[0])
178     #Same as CC2530
179     CCstatusbits={0x80 : "erase_busy",
180                   0x40 : "pcon_idle",
181                   0x20 : "cpu_halted",
182                   0x10 : "pm0",
183                   0x08 : "halt_status",
184                   0x04 : "locked",
185                   0x02 : "oscstable",
186                   0x01 : "overflow"
187                   };
188     CCconfigbits={0x20 : "soft_power_mode",   #new for CC2530
189                   0x08 : "timers_off",
190                   0x04 : "dma_pause",
191                   0x02 : "timer_suspend",
192                   0x01 : "sel_flash_info_page" #stricken from CC2530
193                   };
194                   
195     def CCstatusstr(self):
196         """Check the status as a string."""
197         status=self.CCstatus();
198         str="";
199         i=1;
200         while i<0x100:
201             if(status&i):
202                 str="%s %s" %(self.CCstatusbits[i],str);
203             i*=2;
204         return str;
205     def start(self):
206         """Start debugging."""
207         self.writecmd(0x30,0x20,0,self.data);
208         ident=self.CCidentstr();
209         #print "Target identifies as %s." % ident;
210         #print "Status: %s." % self.CCstatusstr();
211         self.CCreleasecpu();
212         self.CChaltcpu();
213         #print "Status: %s." % self.CCstatusstr();
214         
215     def stop(self):
216         """Stop debugging."""
217         self.writecmd(0x30,0x21,0,self.data);
218     def CCstep_instr(self):
219         """Step one instruction."""
220         self.writecmd(0x30,0x89,0,self.data);
221     def CCeraseflashbuffer(self):
222         """Erase the 2kB flash buffer"""
223         self.writecmd(0x30,0x99);
224     def CCflashpage(self,adr):
225         """Flash 2kB a page of flash from 0xF000 in XDATA"""
226         data=[adr&0xFF,
227               (adr>>8)&0xFF,
228               (adr>>16)&0xFF,
229               (adr>>24)&0xFF];
230         print "Flashing buffer to 0x%06x" % adr;
231         self.writecmd(0x30,0x95,4,data);