7f6e3349a8369981df05c12c2b8e95ec3c65d3c6
[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     CCversions={0x0100:"CC1110",
72                 0x8500:"CC2430",
73                 0x8900:"CC2431",
74                 0x8100:"CC2510",
75                 0x9100:"CC2511",
76                 0xFF00:"CCmissing"};
77     def CCidentstr(self):
78         ident=self.CCident();
79         chip=self.CCversions.get(ident&0xFF00);
80         return "%s/r%02x" % (chip, ident&0xFF); 
81     def CCident(self):
82         """Get a chipcon's ID."""
83         self.writecmd(0x30,0x8B,0,None);
84         chip=ord(self.data[0]);
85         rev=ord(self.data[1]);
86         return (chip<<8)+rev;
87     def CCgetPC(self):
88         """Get a chipcon's PC."""
89         self.writecmd(0x30,0x83,0,None);
90         hi=ord(self.data[0]);
91         lo=ord(self.data[1]);
92         return (hi<<8)+lo;
93     def CCdebuginstr(self,instr):
94         self.writecmd(0x30,0x88,len(instr),instr);
95         return ord(self.data[0]);
96     def CCpeekcodebyte(self,adr):
97         """Read the contents of code memory at an address."""
98         self.data=[adr&0xff, (adr&0xff00)>>8];
99         self.writecmd(0x30,0x90,2,self.data);
100         return ord(self.data[0]);
101     def CCpeekdatabyte(self,adr):
102         """Read the contents of data memory at an address."""
103         self.data=[adr&0xff, (adr&0xff00)>>8];
104         self.writecmd(0x30,0x91, 2, self.data);
105         return ord(self.data[0]);
106     def CCpokedatabyte(self,adr,val):
107         """Write a byte to data memory."""
108         self.data=[adr&0xff, (adr&0xff00)>>8, val];
109         self.writecmd(0x30, 0x92, 3, self.data);
110         return ord(self.data[0]);
111     def CCchiperase(self):
112         """Erase all of the target's memory."""
113         self.writecmd(0x30,0x80,0,None);
114     def CCstatus(self):
115         """Check the status."""
116         self.writecmd(0x30,0x84,0,None);
117         return ord(self.data[0])
118     CCstatusbits={0x80 : "erased",
119                   0x40 : "pcon_idle",
120                   0x20 : "cpu_halted",
121                   0x10 : "pm0",
122                   0x08 : "halt_status",
123                   0x04 : "locked",
124                   0x02 : "oscstable",
125                   0x01 : "overflow"};
126     def CCstatusstr(self):
127         """Check the status as a string."""
128         status=self.CCstatus();
129         str="";
130         i=1;
131         while i<0x100:
132             if(status&i):
133                 str="%s %s" %(self.CCstatusbits[i],str);
134             i*=2;
135         return str;
136     def CCstart(self):
137         """Start debugging."""
138         self.writecmd(0x30,0x20,0,self.data);
139         ident=self.CCidentstr();
140         print "Target identifies as %s." % ident;
141         #print "Status: %s." % self.CCstatusstr();
142         self.CCreleasecpu();
143         self.CChaltcpu();
144         #print "Status: %s." % self.CCstatusstr();
145         
146     def CCstop(self):
147         """Stop debugging."""
148         self.writecmd(0x30,0x21,0,self.data);
149     def CCstep_instr(self):
150         """Step one instruction."""
151         self.writecmd(0x30,0x89,0,self.data);
152     def CCeraseflashbuffer(self):
153         """Erase the 2kB flash buffer"""
154         self.writecmd(0x30,0x99);
155     def CCflashpage(self,adr):
156         """Flash 2kB a page of flash from 0xF000 in XDATA"""
157         data=[adr&0xFF,
158               (adr>>8)&0xFF,
159               (adr>>16)&0xFF,
160               (adr>>24)&0xFF];
161         print "Flashing buffer to 0x%06x" % adr;
162         self.writecmd(0x30,0x95,4,data);