rmspeers on GoodFETCCSPI fixing RF_autocrc and adding support to act like a TinyOS...
[goodfet] / client / GoodFETMSP430.py
1 #!/usr/bin/env python
2 # GoodFET Client Library
3
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # Presently being rewritten.
7
8 import sys, time, string, cStringIO, struct, glob, serial, os;
9
10 from GoodFET import GoodFET;
11
12 class GoodFETMSP430(GoodFET):
13     #Set APP to be MSP430APP or MSP430X2APP, the latter being preferred.
14     
15     #0x16 for class, 0x17 for SBW, 0x11 by default
16     APP=0x11;
17     MSP430APP=0x11;    
18     
19     CoreID=0;
20     DeviceID=0;
21     JTAGID=0;
22     MSP430ident=0;
23     def setup(self):
24         """Move the FET into the MSP430 JTAG application."""
25         self.writecmd(self.MSP430APP,0x10,0,None);
26         
27     def MSP430stop(self):
28         """Stop debugging."""
29         self.writecmd(self.MSP430APP,0x21,0,self.data);
30     
31     def MSP430coreid(self):
32         """Get the Core ID. (MSP430X2 only?)"""
33         self.writecmd(self.MSP430APP,0xF0);
34         CoreID=ord(self.data[0])+(ord(self.data[1])<<8);
35         return CoreID;
36     def MSP430deviceid(self):
37         """Get the Device ID. (MSP430X2 only?)"""
38         self.writecmd(self.MSP430APP,0xF1);
39         DeviceID=(
40             ord(self.data[0])+(ord(self.data[1])<<8)+
41             (ord(self.data[2])<<16)+(ord(self.data[3])<<24));
42         return DeviceID;
43     def peek16(self,adr,memory="vn"):
44         return self.MSP430peek(adr);
45     def peek8(self,adr, memory="vn"):
46         adr=self.MSP430peek(adr&~1);
47         if adr&1==0: return adr&0xFF;
48         else: return adr>>8;
49     def MSP430peek(self,adr):
50         """Read a word at an address."""
51         self.data=[adr&0xff, (adr&0xff00)>>8,
52                    (adr&0xff0000)>>16,(adr&0xff000000)>>24,
53                    ];
54         self.writecmd(self.MSP430APP,0x02,4,self.data);
55         #print "Got %i bytes peeking 0x%04x." % (len(self.data),adr);
56         return ord(self.data[0])+(ord(self.data[1])<<8);
57     def MSP430peekblock(self,adr):
58         """Grab a few block from an SPI Flash ROM.  Block size is unknown"""
59         data=[adr&0xff, (adr&0xff00)>>8,
60               (adr&0xff0000)>>16,(adr&0xff000000)>>24,
61               0x00,0x04];
62         self.writecmd(self.MSP430APP,0x02,6,data);
63         return self.data;
64     
65     def MSP430poke(self,adr,val):
66         """Write the contents of memory at an address."""
67         self.data=[adr&0xff, (adr&0xff00)>>8,
68                    (adr&0xff0000)>>16,(adr&0xff000000)>>24,
69                    val&0xff, (val&0xff00)>>8];
70         self.writecmd(self.MSP430APP,0x03,6,self.data);
71         written=ord(self.data[0])+(ord(self.data[1])<<8);
72         if(written!=val):
73             print "Failed to write 0x%04x to 0x%04x" % (val,adr);
74         return written;
75     def MSP430pokeflash(self,adr,val):
76         """Write the contents of flash memory at an address."""
77         self.data=[adr&0xff, (adr&0xff00)>>8,
78                    (adr&0xff0000)>>16,(adr&0xff000000)>>24,
79                    val&0xff, (val&0xff00)>>8];
80         self.writecmd(self.MSP430APP,0xE1,6,self.data);
81         return ord(self.data[0])+(ord(self.data[1])<<8);
82     def MSP430pokeflashblock(self,adr,data):
83         """Write many words to flash memory at an address."""
84         self.data=[adr&0xff, (adr&0xff00)>>8,
85                    (adr&0xff0000)>>16,(adr&0xff000000)>>24]+data;
86         #print "Writing %i bytes to %x" % (len(data),adr);
87         #print "%2x %2x %2x %2x ..." % (data[0], data[1], data[2], data[3]);
88         self.writecmd(self.MSP430APP,0xE1,len(self.data),self.data);
89         return ord(self.data[0])+(ord(self.data[1])<<8);
90     def start(self):
91         """Start debugging."""
92         self.writecmd(self.MSP430APP,0x20,0,self.data);
93         self.JTAGID=ord(self.data[0]);
94         if(not (self.JTAGID==0x89 or self.JTAGID==0x91)):
95             #Try once more
96             self.writecmd(self.MSP430APP,0x20,0,self.data);
97             self.JTAGID=ord(self.data[0]);
98         
99         #print "Identified as %02x." % self.JTAGID;
100         if(not (self.JTAGID==0x89 or self.JTAGID==0x91)):
101             print "Error, misidentified as %02x.\nCheck wiring, as this should be 0x89 or 0x91." % self.JTAGID;
102         self.MSP430haltcpu();
103     def MSP430haltcpu(self):
104         """Halt the CPU."""
105         self.writecmd(self.MSP430APP,0xA0,0,self.data);
106     def MSP430releasecpu(self):
107         """Resume the CPU."""
108         self.writecmd(self.MSP430APP,0xA1,0,self.data);
109     def MSP430shiftir8(self,ins):
110         """Shift the 8-bit Instruction Register."""
111         data=[ins];
112         self.writecmd(self.MSP430APP,0x80,1,data);
113         return ord(self.data[0]);
114     def MSP430shiftdr16(self,dat):
115         """Shift the 16-bit Data Register."""
116         data=[dat&0xFF,(dat&0xFF00)>>8];
117         self.writecmd(self.MSP430APP,0x81,2,data);
118         return ord(self.data[0])#+(ord(self.data[1])<<8);
119     def MSP430setinstrfetch(self):
120         """Set the instruction fetch mode."""
121         self.writecmd(self.MSP430APP,0xC1,0,self.data);
122         return self.data[0];
123     def MSP430ident(self):
124         """Grab self-identification word from 0x0FF0 as big endian."""
125         ident=0x00;
126         if(self.JTAGID==0x89):
127             i=self.MSP430peek(0x0ff0);
128             ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
129             
130         if(self.JTAGID==0x91):
131             i=self.MSP430peek(0x1A04);
132             ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
133             #ident=0x0091;
134         
135         return ident;
136     def MSP430identstr(self):
137         """Grab model string."""
138         return self.MSP430devices.get(self.MSP430ident());
139     MSP430devices={
140         #MSP430F2xx
141         0xf227: "MSP430F22xx",
142         0xf213: "MSP430F21x1",
143         0xf249: "MSP430F24x",
144         0xf26f: "MSP430F261x",
145         0xf237: "MSP430F23x0",
146         0xf201: "MSP430F201x",
147         
148         #MSP430F1xx
149         0xf16c: "MSP430F161x",
150         0xf149: "MSP430F13x",  #or f14x(1)
151         0xf112: "MSP430F11x",  #or f11x1
152         0xf143: "MSP430F14x",
153         0xf112: "MSP430F11x",  #or F11x1A
154         0xf123: "MSP430F1xx",  #or F123x
155         0x1132: "MSP430F1122", #or F1132
156         0x1232: "MSP430F1222", #or F1232
157         0xf169: "MSP430F16x",
158         
159         #MSP430F4xx
160         0xF449: "MSP430F43x", #or F44x
161         0xF427: "MSP430FE42x", #or FW42x, F415, F417
162         0xF439: "MSP430FG43x",
163         0xf46f: "MSP430FG46xx", #or F471xx
164         
165         }
166     def MSP430test(self):
167         """Test MSP430 JTAG.  Requires that a chip be attached."""
168         
169         if self.MSP430ident()==0xffff:
170             print "ERROR Is anything connected?";
171         print "Testing %s." % self.MSP430identstr();
172         print "Testing RAM from 200 to 210.";
173         for a in range(0x200,0x210):
174             self.MSP430poke(a,0);
175             if(self.MSP430peek(a)!=0):
176                 print "Fault at %06x" % a;
177             self.MSP430poke(a,0xffff);
178             if(self.MSP430peek(a)!=0xffff):
179                 print "Fault at %06x" % a;
180                 
181         print "Testing identity consistency."
182         ident=self.MSP430ident();
183         for a in range(1,20):
184             ident2=self.MSP430ident();
185             if ident!=ident2:
186                 print "Identity %04x!=%04x" % (ident,ident2);
187         
188         print "Testing flash erase."
189         self.MSP430masserase();
190         for a in range(0xffe0, 0xffff):
191             if self.MSP430peek(a)!=0xffff:
192                 print "%04x unerased, equals %04x" % (
193                     a, self.MSP430peek(a));
194
195         print "Testing flash write."
196         for a in range(0xffe0, 0xffff):
197             self.MSP430pokeflash(a,0xbeef);
198             if self.MSP430peek(a)!=0xbeef:
199                 print "%04x unset, equals %04x" % (
200                     a, self.MSP430peek(a));
201         
202         print "Tests complete, erasing."
203         self.MSP430masserase();
204         
205     def MSP430masserase(self):
206         """Erase MSP430 flash memory."""
207         self.writecmd(self.MSP430APP,0xE3,0,None);
208     def MSP430infoerase(self):
209         """Erase MSP430 info flash."""
210         self.writecmd(self.MSP430APP,0xE8,0,None);
211
212     def MSP430setPC(self, pc):
213         """Set the program counter."""
214         self.writecmd(self.MSP430APP,0xC2,2,[pc&0xFF,(pc>>8)&0xFF]);
215     def MSP430setreg(self,reg,val):
216         """Set a register."""
217         self.writecmd(self.MSP430APP,0xD2,3,[reg,val&0xFF,(val>>8)&0xFF]);
218     def MSP430getreg(self,reg):
219         """Get a register."""
220         self.writecmd(self.MSP430APP,0xD3,1,[reg]);
221         return ord(self.data[0])+(ord(self.data[1])<<8);
222
223     def MSP430run(self):
224         """Reset the MSP430 to run on its own."""
225         self.writecmd(self.MSP430APP,0x21,0,None);
226     def MSP430dumpbsl(self):
227         self.MSP430dumpmem(0xC00,0xfff);
228     def MSP430dumpallmem(self):
229         self.MSP430dumpmem(0x200,0xffff);
230     def MSP430dumpmem(self,begin,end):
231         i=begin;
232         while i<end:
233             print "%04x %04x" % (i, self.MSP430peek(i));
234             i+=2;