bb96d3036bc0223d0fb7adbb68dcd39fea75264e
[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
15 class GoodFETCC(GoodFET):
16     """A GoodFET variant for use with Chipcon 8051 Zigbeema SoC."""
17     def CChaltcpu(self):
18         """Halt the CPU."""
19         self.writecmd(0x30,0x86,0,self.data);
20     def CCreleasecpu(self):
21         """Resume the CPU."""
22         self.writecmd(0x30,0x87,0,self.data);
23     def CCtest(self):
24         self.CCreleasecpu();
25         self.CChaltcpu();
26         #print "Status: %s" % self.CCstatusstr();
27         
28         #Grab ident three times, should be equal.
29         ident1=self.CCident();
30         ident2=self.CCident();
31         ident3=self.CCident();
32         if(ident1!=ident2 or ident2!=ident3):
33             print "Error, repeated ident attempts unequal."
34             print "%04x, %04x, %04x" % (ident1, ident2, ident3);
35         
36         #Single step, printing PC.
37         print "Tracing execution at startup."
38         for i in range(1,15):
39             pc=self.CCgetPC();
40             byte=self.CCpeekcodebyte(i);
41             #print "PC=%04x, %02x" % (pc, byte);
42             self.CCstep_instr();
43         
44         print "Verifying that debugging a NOP doesn't affect the PC."
45         for i in range(1,15):
46             pc=self.CCgetPC();
47             self.CCdebuginstr([0x00]);
48             if(pc!=self.CCgetPC()):
49                 print "ERROR: PC changed during CCdebuginstr([NOP])!";
50         
51         
52         #print "Status: %s." % self.CCstatusstr();
53         #Exit debugger
54         self.CCstop();
55         print "Done.";
56
57     def CCsetup(self):
58         """Move the FET into the CC2430/CC2530 application."""
59         #print "Initializing Chipcon.";
60         self.writecmd(0x30,0x10,0,self.data);
61     def CCrd_config(self):
62         """Read the config register of a Chipcon."""
63         self.writecmd(0x30,0x82,0,self.data);
64         return ord(self.data[0]);
65     def CCwr_config(self,config):
66         """Write the config register of a Chipcon."""
67         self.writecmd(0x30,0x81,1,[config&0xFF]);
68     
69     CCversions={0x0100:"CC1110",
70                 0x8500:"CC2430",
71                 0x8900:"CC2431",
72                 0x8100:"CC2510",
73                 0x9100:"CC2511",
74                 0xFF00:"CCmissing"};
75     def CCidentstr(self):
76         ident=self.CCident();
77         chip=self.CCversions.get(ident&0xFF00);
78         return "%s/r%02x" % (chip, ident&0xFF); 
79     def CCident(self):
80         """Get a chipcon's ID."""
81         self.writecmd(0x30,0x8B,0,None);
82         chip=ord(self.data[0]);
83         rev=ord(self.data[1]);
84         return (chip<<8)+rev;
85     def CCgetPC(self):
86         """Get a chipcon's PC."""
87         self.writecmd(0x30,0x83,0,None);
88         hi=ord(self.data[0]);
89         lo=ord(self.data[1]);
90         return (hi<<8)+lo;
91     def CCdebuginstr(self,instr):
92         self.writecmd(0x30,0x88,len(instr),instr);
93         return ord(self.data[0]);
94     def CCpeekcodebyte(self,adr):
95         """Read the contents of code memory at an address."""
96         self.data=[adr&0xff, (adr&0xff00)>>8];
97         self.writecmd(0x30,0x90,2,self.data);
98         return ord(self.data[0]);
99     def CCpeekdatabyte(self,adr):
100         """Read the contents of data memory at an address."""
101         self.data=[adr&0xff, (adr&0xff00)>>8];
102         self.writecmd(0x30,0x91, 2, self.data);
103         return ord(self.data[0]);
104     def CCpokedatabyte(self,adr,val):
105         """Write a byte to data memory."""
106         self.data=[adr&0xff, (adr&0xff00)>>8, val];
107         self.writecmd(0x30, 0x92, 3, self.data);
108         return ord(self.data[0]);
109     def CCchiperase(self):
110         """Erase all of the target's memory."""
111         self.writecmd(0x30,0x80,0,None);
112     def CCstatus(self):
113         """Check the status."""
114         self.writecmd(0x30,0x84,0,None);
115         return ord(self.data[0])
116     CCstatusbits={0x80 : "erased",
117                   0x40 : "pcon_idle",
118                   0x20 : "halted",
119                   0x10 : "pm0",
120                   0x08 : "halted",
121                   0x04 : "locked",
122                   0x02 : "oscstable",
123                   0x01 : "overflow"};
124     def CCstatusstr(self):
125         """Check the status as a string."""
126         status=self.CCstatus();
127         str="";
128         i=1;
129         while i<0x100:
130             if(status&i):
131                 str="%s %s" %(self.CCstatusbits[i],str);
132             i*=2;
133         return str;
134     def CCstart(self):
135         """Start debugging."""
136         self.writecmd(0x30,0x20,0,self.data);
137         ident=self.CCidentstr();
138         print "Target identifies as %s." % ident;
139         #print "Status: %s." % self.CCstatusstr();
140         self.CCreleasecpu();
141         self.CChaltcpu();
142         #print "Status: %s." % self.CCstatusstr();
143         
144     def CCstop(self):
145         """Stop debugging."""
146         self.writecmd(0x30,0x21,0,self.data);
147     def CCstep_instr(self):
148         """Step one instruction."""
149         self.writecmd(0x30,0x89,0,self.data);
150