e2f28e69fb8f7baa0fe9b575d061d555871e5955
[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         
51         #print "Status: %s." % self.CCstatusstr();
52         #Exit debugger
53         self.CCstop();
54         print "Done.";
55
56     def CCsetup(self):
57         """Move the FET into the CC2430/CC2530 application."""
58         #print "Initializing Chipcon.";
59         self.writecmd(0x30,0x10,0,self.data);
60     def CCrd_config(self):
61         """Read the config register of a Chipcon."""
62         self.writecmd(0x30,0x82,0,self.data);
63         return ord(self.data[0]);
64     def CCwr_config(self,config):
65         """Write the config register of a Chipcon."""
66         self.writecmd(0x30,0x81,1,[config&0xFF]);
67     def CClockchip(self):
68         """Set the flash lock bit in info mem."""
69         self.writecmd(0x30, 0x9A, 0, None);
70     
71
72     CCversions={0x0100:"CC1110",
73                 0x8500:"CC2430",
74                 0x8900:"CC2431",
75                 0x8100:"CC2510",
76                 0x9100:"CC2511",
77                 0xA500:"CC2530", #page 52 of SWRU191
78                 0xB500:"CC2531",
79                 0xFF00:"CCmissing"};
80     def CCidentstr(self):
81         ident=self.CCident();
82         chip=self.CCversions.get(ident&0xFF00);
83         return "%s/r%02x" % (chip, ident&0xFF); 
84     def CCident(self):
85         """Get a chipcon's ID."""
86         self.writecmd(0x30,0x8B,0,None);
87         chip=ord(self.data[0]);
88         rev=ord(self.data[1]);
89         return (chip<<8)+rev;
90     def CCgetPC(self):
91         """Get a chipcon's PC."""
92         self.writecmd(0x30,0x83,0,None);
93         hi=ord(self.data[0]);
94         lo=ord(self.data[1]);
95         return (hi<<8)+lo;
96     def CCdebuginstr(self,instr):
97         self.writecmd(0x30,0x88,len(instr),instr);
98         return ord(self.data[0]);
99     def CCpeekcodebyte(self,adr):
100         """Read the contents of code memory at an address."""
101         self.data=[adr&0xff, (adr&0xff00)>>8];
102         self.writecmd(0x30,0x90,2,self.data);
103         return ord(self.data[0]);
104     def CCpeekdatabyte(self,adr):
105         """Read the contents of data memory at an address."""
106         self.data=[adr&0xff, (adr&0xff00)>>8];
107         self.writecmd(0x30,0x91, 2, self.data);
108         return ord(self.data[0]);
109     def CCpeekirambyte(self,adr):
110         """Read the contents of IRAM at an address."""
111         self.data=[adr&0xff];
112         self.writecmd(0x30,0x02, 1, self.data);
113         return ord(self.data[0]);
114     def CCpokeirambyte(self,adr,val):
115         """Write the contents of IRAM at an address."""
116         self.data=[adr&0xff, val&0xff];
117         self.writecmd(0x30,0x02, 2, self.data);
118         return ord(self.data[0]);
119     
120     def CCpokedatabyte(self,adr,val):
121         """Write a byte to data memory."""
122         self.data=[adr&0xff, (adr&0xff00)>>8, val];
123         self.writecmd(0x30, 0x92, 3, self.data);
124         return ord(self.data[0]);
125     def CCchiperase(self):
126         """Erase all of the target's memory."""
127         self.writecmd(0x30,0x80,0,None);
128     def CCstatus(self):
129         """Check the status."""
130         self.writecmd(0x30,0x84,0,None);
131         return ord(self.data[0])
132     #Same as CC2530
133     CCstatusbits={0x80 : "erase_busy",
134                   0x40 : "pcon_idle",
135                   0x20 : "cpu_halted",
136                   0x10 : "pm0",
137                   0x08 : "halt_status",
138                   0x04 : "locked",
139                   0x02 : "oscstable",
140                   0x01 : "overflow"
141                   };
142     CCconfigbits={0x20 : "soft_power_mode",   #new for CC2530
143                   0x08 : "timers_off",
144                   0x04 : "dma_pause",
145                   0x02 : "timer_suspend",
146                   0x01 : "sel_flash_info_page" #stricken from CC2530
147                   };
148                   
149     def CCstatusstr(self):
150         """Check the status as a string."""
151         status=self.CCstatus();
152         str="";
153         i=1;
154         while i<0x100:
155             if(status&i):
156                 str="%s %s" %(self.CCstatusbits[i],str);
157             i*=2;
158         return str;
159     def CCstart(self):
160         """Start debugging."""
161         self.writecmd(0x30,0x20,0,self.data);
162         ident=self.CCidentstr();
163         print "Target identifies as %s." % ident;
164         #print "Status: %s." % self.CCstatusstr();
165         self.CCreleasecpu();
166         self.CChaltcpu();
167         #print "Status: %s." % self.CCstatusstr();
168         
169     def CCstop(self):
170         """Stop debugging."""
171         self.writecmd(0x30,0x21,0,self.data);
172     def CCstep_instr(self):
173         """Step one instruction."""
174         self.writecmd(0x30,0x89,0,self.data);
175     def CCeraseflashbuffer(self):
176         """Erase the 2kB flash buffer"""
177         self.writecmd(0x30,0x99);
178     def CCflashpage(self,adr):
179         """Flash 2kB a page of flash from 0xF000 in XDATA"""
180         data=[adr&0xFF,
181               (adr>>8)&0xFF,
182               (adr>>16)&0xFF,
183               (adr>>24)&0xFF];
184         print "Flashing buffer to 0x%06x" % adr;
185         self.writecmd(0x30,0x95,4,data);