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