fc8e99befeaa9b9a9bfd1cbcf59a5d709d481297
[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 peek(self,adr,memory="vn"):
46         return self.MSP430peek(adr);
47     def peek8(self,adr, memory="vn"):
48         adr=self.MSP430peek(adr&~1);
49         if adr&1==0: return adr&0xFF;
50         else: return adr>>8;
51
52     def MSP430peek(self,adr):
53         """Read a word at an address."""
54         self.data=[adr&0xff, (adr&0xff00)>>8,
55                    (adr&0xff0000)>>16,(adr&0xff000000)>>24,
56                    ];
57         self.writecmd(self.MSP430APP,0x02,4,self.data);
58         #print "Got %i bytes peeking 0x%04x." % (len(self.data),adr);
59         return ord(self.data[0])+(ord(self.data[1])<<8);
60     def MSP430peekblock(self,adr):
61         """Grab a few block from an SPI Flash ROM.  Block size is unknown"""
62         data=[adr&0xff, (adr&0xff00)>>8,
63               (adr&0xff0000)>>16,(adr&0xff000000)>>24,
64               0x00,0x04];
65         self.writecmd(self.MSP430APP,0x02,6,data);
66         return self.data;
67     
68     def MSP430poke(self,adr,val):
69         """Write the contents of memory at an address."""
70         self.data=[adr&0xff, (adr&0xff00)>>8,
71                    (adr&0xff0000)>>16,(adr&0xff000000)>>24,
72                    val&0xff, (val&0xff00)>>8];
73         self.writecmd(self.MSP430APP,0x03,6,self.data);
74         written=ord(self.data[0])+(ord(self.data[1])<<8);
75         if(written!=val):
76             print "Failed to write 0x%04x to 0x%04x" % (val,adr);
77         return written;
78     def MSP430pokeflash(self,adr,val):
79         """Write the contents of flash memory at an address."""
80         self.data=[adr&0xff, (adr&0xff00)>>8,
81                    (adr&0xff0000)>>16,(adr&0xff000000)>>24,
82                    val&0xff, (val&0xff00)>>8];
83         self.writecmd(self.MSP430APP,0xE1,6,self.data);
84         return ord(self.data[0])+(ord(self.data[1])<<8);
85     def setsecret(self,value):
86         """Set a secret word for later retreival.  Used by glitcher."""
87         self.MSP430pokeflash(0xFFFE,value);
88     def getsecret(self):
89         """Get a secret word.  Used by glitcher."""
90         return self.peek(0xfffe);
91     def MSP430pokeflashblock(self,adr,data):
92         """Write many words to flash memory at an address."""
93         self.data=[adr&0xff, (adr&0xff00)>>8,
94                    (adr&0xff0000)>>16,(adr&0xff000000)>>24]+data;
95         #print "Writing %i bytes to %x" % (len(data),adr);
96         #print "%2x %2x %2x %2x ..." % (data[0], data[1], data[2], data[3]);
97         self.writecmd(self.MSP430APP,0xE1,len(self.data),self.data);
98         return ord(self.data[0])+(ord(self.data[1])<<8);
99     def start(self):
100         """Start debugging."""
101         self.writecmd(self.MSP430APP,0x20,0,self.data);
102         self.JTAGID=ord(self.data[0]);
103         if(not (self.JTAGID==0x89 or self.JTAGID==0x91)):
104             #Try once more
105             self.writecmd(self.MSP430APP,0x20,0,self.data);
106             self.JTAGID=ord(self.data[0]);
107         
108         #print "Identified as %02x." % self.JTAGID;
109         if(not (self.JTAGID==0x89 or self.JTAGID==0x91)):
110             print "Error, misidentified as %02x.\nCheck wiring, as this should be 0x89 or 0x91." % self.JTAGID;
111         self.MSP430haltcpu();
112     def MSP430haltcpu(self):
113         """Halt the CPU."""
114         self.writecmd(self.MSP430APP,0xA0,0,self.data);
115     def MSP430releasecpu(self):
116         """Resume the CPU."""
117         self.writecmd(self.MSP430APP,0xA1,0,self.data);
118     def MSP430shiftir8(self,ins):
119         """Shift the 8-bit Instruction Register."""
120         data=[ins];
121         self.writecmd(self.MSP430APP,0x80,1,data);
122         return ord(self.data[0]);
123     def MSP430shiftdr16(self,dat):
124         """Shift the 16-bit Data Register."""
125         data=[dat&0xFF,(dat&0xFF00)>>8];
126         self.writecmd(self.MSP430APP,0x81,2,data);
127         return ord(self.data[0])#+(ord(self.data[1])<<8);
128     def MSP430setinstrfetch(self):
129         """Set the instruction fetch mode."""
130         self.writecmd(self.MSP430APP,0xC1,0,self.data);
131         return self.data[0];
132     def MSP430ident(self):
133         """Grab self-identification word from 0x0FF0 as big endian."""
134         ident=0x00;
135         if(self.JTAGID==0x89):
136             i=self.MSP430peek(0x0ff0);
137             ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
138             
139         if(self.JTAGID==0x91):
140             i=self.MSP430peek(0x1A04);
141             ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
142             #ident=0x0091;
143         
144         return ident;
145     def MSP430identstr(self):
146         """Grab model string."""
147         return self.MSP430devices.get(self.MSP430ident());
148     MSP430devices={
149         #MSP430F2xx
150         0xf227: "MSP430F22xx",
151         0xf213: "MSP430F21x1",
152         0xf249: "MSP430F24x",
153         0xf26f: "MSP430F261x",
154         0xf237: "MSP430F23x0",
155         0xf201: "MSP430F201x",
156         
157         #MSP430F1xx
158         0xf16c: "MSP430F161x",
159         0xf149: "MSP430F13x",  #or f14x(1)
160         0xf112: "MSP430F11x",  #or f11x1
161         0xf143: "MSP430F14x",
162         0xf112: "MSP430F11x",  #or F11x1A
163         0xf123: "MSP430F1xx",  #or F123x
164         0x1132: "MSP430F1122", #or F1132
165         0x1232: "MSP430F1222", #or F1232
166         0xf169: "MSP430F16x",
167         
168         #MSP430F4xx
169         0xF449: "MSP430F43x", #or F44x
170         0xF427: "MSP430FE42x", #or FW42x, F415, F417
171         0xF439: "MSP430FG43x",
172         0xf46f: "MSP430FG46xx", #or F471xx
173         
174         }
175     def MSP430test(self):
176         """Test MSP430 JTAG.  Requires that a chip be attached."""
177         
178         if self.MSP430ident()==0xffff:
179             print "ERROR Is anything connected?";
180         print "Testing %s." % self.MSP430identstr();
181         print "Testing RAM from 200 to 210.";
182         for a in range(0x200,0x210):
183             self.MSP430poke(a,0);
184             if(self.MSP430peek(a)!=0):
185                 print "Fault at %06x" % a;
186             self.MSP430poke(a,0xffff);
187             if(self.MSP430peek(a)!=0xffff):
188                 print "Fault at %06x" % a;
189                 
190         print "Testing identity consistency."
191         ident=self.MSP430ident();
192         for a in range(1,20):
193             ident2=self.MSP430ident();
194             if ident!=ident2:
195                 print "Identity %04x!=%04x" % (ident,ident2);
196         
197         print "Testing flash erase."
198         self.MSP430masserase();
199         for a in range(0xffe0, 0xffff):
200             if self.MSP430peek(a)!=0xffff:
201                 print "%04x unerased, equals %04x" % (
202                     a, self.MSP430peek(a));
203
204         print "Testing flash write."
205         for a in range(0xffe0, 0xffff):
206             self.MSP430pokeflash(a,0xbeef);
207             if self.MSP430peek(a)!=0xbeef:
208                 print "%04x unset, equals %04x" % (
209                     a, self.MSP430peek(a));
210         
211         print "Tests complete, erasing."
212         self.MSP430masserase();
213     def erase(self):
214         self.MSP430masserase();
215     def MSP430masserase(self):
216         """Erase MSP430 flash memory."""
217         self.writecmd(self.MSP430APP,0xE3,0,None);
218     def MSP430infoerase(self):
219         """Erase MSP430 info flash."""
220         self.writecmd(self.MSP430APP,0xE8,0,None);
221
222     def MSP430setPC(self, pc):
223         """Set the program counter."""
224         self.writecmd(self.MSP430APP,0xC2,2,[pc&0xFF,(pc>>8)&0xFF]);
225     def MSP430setreg(self,reg,val):
226         """Set a register."""
227         self.writecmd(self.MSP430APP,0xD2,3,[reg,val&0xFF,(val>>8)&0xFF]);
228     def MSP430getreg(self,reg):
229         """Get a register."""
230         self.writecmd(self.MSP430APP,0xD3,1,[reg]);
231         return ord(self.data[0])+(ord(self.data[1])<<8);
232
233     def MSP430run(self):
234         """Reset the MSP430 to run on its own."""
235         self.writecmd(self.MSP430APP,0x21,0,None);
236     def MSP430dumpbsl(self):
237         self.MSP430dumpmem(0xC00,0xfff);
238     def MSP430dumpallmem(self):
239         self.MSP430dumpmem(0x200,0xffff);
240     def MSP430dumpmem(self,begin,end):
241         i=begin;
242         while i<end:
243             print "%04x %04x" % (i, self.MSP430peek(i));
244             i+=2;