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