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