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 Core 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 return ord(self.data[0])+(ord(self.data[1])<<8);
68 def MSP430pokeflash(self,adr,val):
69 """Write the contents of flash 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,0xE1,6,self.data);
74 return ord(self.data[0])+(ord(self.data[1])<<8);
75 def MSP430pokeflashblock(self,adr,data):
76 """Write many words to flash memory at an address."""
77 self.data=[adr&0xff, (adr&0xff00)>>8,
78 (adr&0xff0000)>>16,(adr&0xff000000)>>24]+data;
79 #print "Writing %i bytes to %x" % (len(data),adr);
80 #print "%2x %2x %2x %2x ..." % (data[0], data[1], data[2], data[3]);
81 self.writecmd(self.MSP430APP,0xE1,len(self.data),self.data);
82 return ord(self.data[0])+(ord(self.data[1])<<8);
84 """Start debugging."""
85 self.writecmd(self.MSP430APP,0x20,0,self.data);
86 self.JTAGID=ord(self.data[0]);
87 #print "Identified as %02x." % self.JTAGID;
88 if(not (self.JTAGID==0x89 or self.JTAGID==0x91)):
89 print "Error, misidentified as %02x." % self.JTAGID;
91 def MSP430haltcpu(self):
93 self.writecmd(self.MSP430APP,0xA0,0,self.data);
94 def MSP430releasecpu(self):
96 self.writecmd(self.MSP430APP,0xA1,0,self.data);
97 def MSP430shiftir8(self,ins):
98 """Shift the 8-bit Instruction Register."""
100 self.writecmd(self.MSP430APP,0x80,1,data);
101 return ord(self.data[0]);
102 def MSP430shiftdr16(self,dat):
103 """Shift the 16-bit Data Register."""
104 data=[dat&0xFF,(dat&0xFF00)>>8];
105 self.writecmd(self.MSP430APP,0x81,2,data);
106 return ord(self.data[0])#+(ord(self.data[1])<<8);
107 def MSP430setinstrfetch(self):
108 """Set the instruction fetch mode."""
109 self.writecmd(self.MSP430APP,0xC1,0,self.data);
111 def MSP430ident(self):
112 """Grab self-identification word from 0x0FF0 as big endian."""
114 if(self.JTAGID==0x89):
115 i=self.MSP430peek(0x0ff0);
116 ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
118 if(self.JTAGID==0x91):
119 i=self.MSP430peek(0x1A04);
120 ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
124 def MSP430identstr(self):
125 """Grab model string."""
126 return self.MSP430devices.get(self.MSP430ident());
129 0xf227: "MSP430F22xx",
130 0xf213: "MSP430F21x1",
131 0xf249: "MSP430F24x",
132 0xf26f: "MSP430F261x",
133 0xf237: "MSP430F23x0",
134 0xf201: "MSP430F201x",
137 0xf16c: "MSP430F161x",
138 0xf149: "MSP430F13x", #or f14x(1)
139 0xf112: "MSP430F11x", #or f11x1
140 0xf143: "MSP430F14x",
141 0xf112: "MSP430F11x", #or F11x1A
142 0xf123: "MSP430F1xx", #or F123x
143 0x1132: "MSP430F1122", #or F1132
144 0x1232: "MSP430F1222", #or F1232
145 0xf169: "MSP430F16x",
148 0xF449: "MSP430F43x", #or F44x
149 0xF427: "MSP430FE42x", #or FW42x, F415, F417
150 0xF439: "MSP430FG43x",
151 0xf46f: "MSP430FG46xx", #or F471xx
154 def MSP430test(self):
155 """Test MSP430 JTAG. Requires that a chip be attached."""
157 if self.MSP430ident()==0xffff:
158 print "ERROR Is anything connected?";
159 print "Testing %s." % self.MSP430identstr();
160 print "Testing RAM from 200 to 210.";
161 for a in range(0x200,0x210):
162 self.MSP430poke(a,0);
163 if(self.MSP430peek(a)!=0):
164 print "Fault at %06x" % a;
165 self.MSP430poke(a,0xffff);
166 if(self.MSP430peek(a)!=0xffff):
167 print "Fault at %06x" % a;
169 print "Testing identity consistency."
170 ident=self.MSP430ident();
171 for a in range(1,20):
172 ident2=self.MSP430ident();
174 print "Identity %04x!=%04x" % (ident,ident2);
176 print "Testing flash erase."
177 self.MSP430masserase();
178 for a in range(0xffe0, 0xffff):
179 if self.MSP430peek(a)!=0xffff:
180 print "%04x unerased, equals %04x" % (
181 a, self.MSP430peek(a));
183 print "Testing flash write."
184 for a in range(0xffe0, 0xffff):
185 self.MSP430pokeflash(a,0xbeef);
186 if self.MSP430peek(a)!=0xbeef:
187 print "%04x unset, equals %04x" % (
188 a, self.MSP430peek(a));
190 print "Tests complete, erasing."
191 self.MSP430masserase();
193 def MSP430masserase(self):
194 """Erase MSP430 flash memory."""
195 self.writecmd(self.MSP430APP,0xE3,0,None);
196 def MSP430setPC(self, pc):
197 """Set the program counter."""
198 self.writecmd(self.MSP430APP,0xC2,2,[pc&0xFF,(pc>>8)&0xFF]);
200 """Reset the MSP430 to run on its own."""
201 self.writecmd(self.MSP430APP,0x21,0,None);
202 def MSP430dumpbsl(self):
203 self.MSP430dumpmem(0xC00,0xfff);
204 def MSP430dumpallmem(self):
205 self.MSP430dumpmem(0x200,0xffff);
206 def MSP430dumpmem(self,begin,end):
209 print "%04x %04x" % (i, self.MSP430peek(i));