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