2 # GoodFET Client Library
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
6 # Presently being rewritten.
8 import sys, time, string, cStringIO, struct, glob, os;
10 from GoodFET import GoodFET;
12 class GoodFETMSP430(GoodFET):
13 #Set APP to be MSP430APP or MSP430X2APP, the latter being preferred.
15 #0x16 for class, 0x17 for SBW, 0x11 by default
24 """Move the FET into the MSP430 JTAG application."""
25 self.writecmd(self.MSP430APP,0x10,0,None);
29 self.writecmd(self.MSP430APP,0x21,0,self.data);
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);
36 def MSP430deviceid(self):
37 """Get the Device ID. (MSP430X2 only?)"""
38 self.writecmd(self.MSP430APP,0xF1);
40 ord(self.data[0])+(ord(self.data[1])<<8)+
41 (ord(self.data[2])<<16)+(ord(self.data[3])<<24));
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;
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,
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,
65 self.writecmd(self.MSP430APP,0x02,6,data);
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);
76 print "Failed to write 0x%04x to 0x%04x" % (val,adr);
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);
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);
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)):
105 self.writecmd(self.MSP430APP,0x20,0,self.data);
106 self.JTAGID=ord(self.data[0]);
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):
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."""
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);
132 def MSP430ident(self):
133 """Grab self-identification word from 0x0FF0 as big endian."""
135 if(self.JTAGID==0x89):
136 i=self.MSP430peek(0x0ff0);
137 ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
139 if(self.JTAGID==0x91):
140 i=self.MSP430peek(0x1A04);
141 ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
145 def MSP430identstr(self):
146 """Grab model string."""
147 return self.MSP430devices.get(self.MSP430ident());
150 0xf227: "MSP430F22xx",
151 0xf213: "MSP430F21x1",
152 0xf249: "MSP430F24x",
153 0xf26f: "MSP430F261x",
154 0xf237: "MSP430F23x0",
155 0xf201: "MSP430F201x",
156 #Are G's and F's distinct?
157 0x2553: "MSP430G2553",
160 0xf16c: "MSP430F161x",
161 0xf149: "MSP430F13x", #or f14x(1)
162 0xf112: "MSP430F11x", #or f11x1
163 0xf143: "MSP430F14x",
164 0xf112: "MSP430F11x", #or F11x1A
165 0xf123: "MSP430F1xx", #or F123x
166 0x1132: "MSP430F1122", #or F1132
167 0x1232: "MSP430F1222", #or F1232
168 0xf169: "MSP430F16x",
171 0xF449: "MSP430F43x", #or F44x
172 0xF427: "MSP430FE42x", #or FW42x, F415, F417
173 0xF439: "MSP430FG43x",
174 0xf46f: "MSP430FG46xx", #or F471xx
175 0xF413: "MSP430F413", #or maybe others.
177 def MSP430test(self):
178 """Test MSP430 JTAG. Requires that a chip be attached."""
180 if self.MSP430ident()==0xffff:
181 print "ERROR Is anything connected?";
182 print "Testing %s." % self.MSP430identstr();
183 print "Testing RAM from 200 to 210.";
184 for a in range(0x200,0x210):
185 self.MSP430poke(a,0);
186 if(self.MSP430peek(a)!=0):
187 print "Fault at %06x" % a;
188 self.MSP430poke(a,0xffff);
189 if(self.MSP430peek(a)!=0xffff):
190 print "Fault at %06x" % a;
192 print "Testing identity consistency."
193 ident=self.MSP430ident();
194 for a in range(1,20):
195 ident2=self.MSP430ident();
197 print "Identity %04x!=%04x" % (ident,ident2);
199 print "Testing flash erase."
200 self.MSP430masserase();
201 for a in range(0xffe0, 0xffff):
202 if self.MSP430peek(a)!=0xffff:
203 print "%04x unerased, equals %04x" % (
204 a, self.MSP430peek(a));
206 print "Testing flash write."
207 for a in range(0xffe0, 0xffff):
208 self.MSP430pokeflash(a,0xbeef);
209 if self.MSP430peek(a)!=0xbeef:
210 print "%04x unset, equals %04x" % (
211 a, self.MSP430peek(a));
213 print "Tests complete, erasing."
214 self.MSP430masserase();
216 self.MSP430masserase();
217 def MSP430masserase(self):
218 """Erase MSP430 flash memory."""
219 self.writecmd(self.MSP430APP,0xE3,0,None);
220 def MSP430infoerase(self):
221 """Erase MSP430 info flash."""
222 self.writecmd(self.MSP430APP,0xE8,0,None);
224 def MSP430setPC(self, pc):
225 """Set the program counter."""
226 self.writecmd(self.MSP430APP,0xC2,2,[pc&0xFF,(pc>>8)&0xFF]);
227 def MSP430setreg(self,reg,val):
228 """Set a register."""
229 self.writecmd(self.MSP430APP,0xD2,3,[reg,val&0xFF,(val>>8)&0xFF]);
230 def MSP430getreg(self,reg):
231 """Get a register."""
232 self.writecmd(self.MSP430APP,0xD3,1,[reg]);
233 return ord(self.data[0])+(ord(self.data[1])<<8);
236 """Reset the MSP430 to run on its own."""
237 self.writecmd(self.MSP430APP,0x21,0,None);
238 def MSP430dumpbsl(self):
239 self.MSP430dumpmem(0xC00,0xfff);
240 def MSP430dumpallmem(self):
241 self.MSP430dumpmem(0x200,0xffff);
242 def MSP430dumpmem(self,begin,end):
245 print "%04x %04x" % (i, self.MSP430peek(i));