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, serial, os;
10 from GoodFET import GoodFET;
12 class GoodFETMSP430(GoodFET):
14 MSP430APP=0x11; #Changed by inheritors.
20 """Move the FET into the MSP430 JTAG application."""
21 self.writecmd(self.MSP430APP,0x10,0,None);
25 self.writecmd(self.MSP430APP,0x21,0,self.data);
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);
32 def MSP430deviceid(self):
33 """Get the Device ID."""
34 self.writecmd(self.MSP430APP,0xF1);
36 ord(self.data[0])+(ord(self.data[1])<<8)+
37 (ord(self.data[2])<<16)+(ord(self.data[3])<<24));
39 def peek16(self,adr,memory="vn"):
40 return self.MSP430peek(adr);
41 def peek8(self,address, memory="vn"):
42 adr=self.MSP430peek(adr&~1);
43 if adr&1==0: return adr&0xFF;
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,
50 self.writecmd(self.MSP430APP,0x02,4,self.data);
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,
58 self.writecmd(self.MSP430APP,0x02,6,data);
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);
69 print "Failed to write 0x%04x to 0x$04x" % (val,adr);
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);
87 """Start debugging."""
88 self.writecmd(self.MSP430APP,0x20,0,self.data);
89 self.JTAGID=ord(self.data[0]);
90 #print "Identified as %02x." % self.JTAGID;
91 if(not (self.JTAGID==0x89 or self.JTAGID==0x91)):
92 print "Error, misidentified as %02x.\nCheck wiring, as this should be 0x89 or 0x91." % self.JTAGID;
94 def MSP430haltcpu(self):
96 self.writecmd(self.MSP430APP,0xA0,0,self.data);
97 def MSP430releasecpu(self):
99 self.writecmd(self.MSP430APP,0xA1,0,self.data);
100 def MSP430shiftir8(self,ins):
101 """Shift the 8-bit Instruction Register."""
103 self.writecmd(self.MSP430APP,0x80,1,data);
104 return ord(self.data[0]);
105 def MSP430shiftdr16(self,dat):
106 """Shift the 16-bit Data Register."""
107 data=[dat&0xFF,(dat&0xFF00)>>8];
108 self.writecmd(self.MSP430APP,0x81,2,data);
109 return ord(self.data[0])#+(ord(self.data[1])<<8);
110 def MSP430setinstrfetch(self):
111 """Set the instruction fetch mode."""
112 self.writecmd(self.MSP430APP,0xC1,0,self.data);
114 def MSP430ident(self):
115 """Grab self-identification word from 0x0FF0 as big endian."""
117 if(self.JTAGID==0x89):
118 i=self.MSP430peek(0x0ff0);
119 ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
121 if(self.JTAGID==0x91):
122 i=self.MSP430peek(0x1A04);
123 ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
127 def MSP430identstr(self):
128 """Grab model string."""
129 return self.MSP430devices.get(self.MSP430ident());
132 0xf227: "MSP430F22xx",
133 0xf213: "MSP430F21x1",
134 0xf249: "MSP430F24x",
135 0xf26f: "MSP430F261x",
136 0xf237: "MSP430F23x0",
137 0xf201: "MSP430F201x",
140 0xf16c: "MSP430F161x",
141 0xf149: "MSP430F13x", #or f14x(1)
142 0xf112: "MSP430F11x", #or f11x1
143 0xf143: "MSP430F14x",
144 0xf112: "MSP430F11x", #or F11x1A
145 0xf123: "MSP430F1xx", #or F123x
146 0x1132: "MSP430F1122", #or F1132
147 0x1232: "MSP430F1222", #or F1232
148 0xf169: "MSP430F16x",
151 0xF449: "MSP430F43x", #or F44x
152 0xF427: "MSP430FE42x", #or FW42x, F415, F417
153 0xF439: "MSP430FG43x",
154 0xf46f: "MSP430FG46xx", #or F471xx
157 def MSP430test(self):
158 """Test MSP430 JTAG. Requires that a chip be attached."""
160 if self.MSP430ident()==0xffff:
161 print "ERROR Is anything connected?";
162 print "Testing %s." % self.MSP430identstr();
163 print "Testing RAM from 200 to 210.";
164 for a in range(0x200,0x210):
165 self.MSP430poke(a,0);
166 if(self.MSP430peek(a)!=0):
167 print "Fault at %06x" % a;
168 self.MSP430poke(a,0xffff);
169 if(self.MSP430peek(a)!=0xffff):
170 print "Fault at %06x" % a;
172 print "Testing identity consistency."
173 ident=self.MSP430ident();
174 for a in range(1,20):
175 ident2=self.MSP430ident();
177 print "Identity %04x!=%04x" % (ident,ident2);
179 print "Testing flash erase."
180 self.MSP430masserase();
181 for a in range(0xffe0, 0xffff):
182 if self.MSP430peek(a)!=0xffff:
183 print "%04x unerased, equals %04x" % (
184 a, self.MSP430peek(a));
186 print "Testing flash write."
187 for a in range(0xffe0, 0xffff):
188 self.MSP430pokeflash(a,0xbeef);
189 if self.MSP430peek(a)!=0xbeef:
190 print "%04x unset, equals %04x" % (
191 a, self.MSP430peek(a));
193 print "Tests complete, erasing."
194 self.MSP430masserase();
196 def MSP430masserase(self):
197 """Erase MSP430 flash memory."""
198 self.writecmd(self.MSP430APP,0xE3,0,None);
199 def MSP430infoerase(self):
200 """Erase MSP430 info flash."""
201 self.writecmd(self.MSP430APP,0xE8,0,None);
203 def MSP430setPC(self, pc):
204 """Set the program counter."""
205 self.writecmd(self.MSP430APP,0xC2,2,[pc&0xFF,(pc>>8)&0xFF]);
207 """Reset the MSP430 to run on its own."""
208 self.writecmd(self.MSP430APP,0x21,0,None);
209 def MSP430dumpbsl(self):
210 self.MSP430dumpmem(0xC00,0xfff);
211 def MSP430dumpallmem(self):
212 self.MSP430dumpmem(0x200,0xffff);
213 def MSP430dumpmem(self,begin,end):
216 print "%04x %04x" % (i, self.MSP430peek(i));