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):
13 MSP430APP=0x11; #Changed by inheritors.
18 def MSP430setup(self):
19 """Move the FET into the MSP430 JTAG application."""
20 self.writecmd(self.MSP430APP,0x10,0,None);
24 self.writecmd(self.MSP430APP,0x21,0,self.data);
26 def MSP430coreid(self):
27 """Get the Core ID."""
28 self.writecmd(self.MSP430APP,0xF0);
29 CoreID=ord(self.data[0])+(ord(self.data[1])<<8);
31 def MSP430deviceid(self):
32 """Get the Core ID."""
33 self.writecmd(self.MSP430APP,0xF1);
35 ord(self.data[0])+(ord(self.data[1])<<8)+
36 (ord(self.data[2])<<16)+(ord(self.data[3])<<24));
38 def MSP430peek(self,adr):
39 """Read a word at an address."""
40 self.data=[adr&0xff, (adr&0xff00)>>8,
41 (adr&0xff0000)>>16,(adr&0xff000000)>>24,
43 self.writecmd(self.MSP430APP,0x02,4,self.data,1);
44 return ord(self.data[0])+(ord(self.data[1])<<8);
45 def MSP430peekblock(self,adr,blocks=1):
46 """Grab a few block from an SPI Flash ROM. Block size is unknown"""
47 data=[adr&0xff, (adr&0xff00)>>8,
48 (adr&0xff0000)>>16,(adr&0xff000000)>>24,
51 self.writecmd(self.MSP430APP,0x02,5,data,blocks);
54 def MSP430poke(self,adr,val):
55 """Write the contents of memory at an address."""
56 self.data=[adr&0xff, (adr&0xff00)>>8,
57 (adr&0xff0000)>>16,(adr&0xff000000)>>24,
58 val&0xff, (val&0xff00)>>8];
59 self.writecmd(self.MSP430APP,0x03,6,self.data);
60 return ord(self.data[0])+(ord(self.data[1])<<8);
61 def MSP430pokeflash(self,adr,val):
62 """Write the contents of flash 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,0xE1,6,self.data);
67 return ord(self.data[0])+(ord(self.data[1])<<8);
69 def MSP430start(self):
70 """Start debugging."""
71 self.writecmd(self.MSP430APP,0x20,0,self.data);
72 self.JTAGID=ord(self.data[0]);
73 #print "Identified as %02x." % self.JTAGID;
74 if(not (self.JTAGID==0x89 or self.JTAGID==0x91)):
75 print "Error, misidentified as %02x." % self.JTAGID;
77 def MSP430haltcpu(self):
79 self.writecmd(self.MSP430APP,0xA0,0,self.data);
80 def MSP430releasecpu(self):
82 self.writecmd(self.MSP430APP,0xA1,0,self.data);
83 def MSP430shiftir8(self,ins):
84 """Shift the 8-bit Instruction Register."""
86 self.writecmd(self.MSP430APP,0x80,1,data);
87 return ord(self.data[0]);
88 def MSP430shiftdr16(self,dat):
89 """Shift the 16-bit Data Register."""
90 data=[dat&0xFF,(dat&0xFF00)>>8];
91 self.writecmd(self.MSP430APP,0x81,2,data);
92 return ord(self.data[0])#+(ord(self.data[1])<<8);
93 def MSP430setinstrfetch(self):
94 """Set the instruction fetch mode."""
95 self.writecmd(self.MSP430APP,0xC1,0,self.data);
97 def MSP430ident(self):
98 """Grab self-identification word from 0x0FF0 as big endian."""
100 if(self.JTAGID==0x89):
101 i=self.MSP430peek(0x0ff0);
102 ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
104 if(self.JTAGID==0x91):
105 i=self.MSP430peek(0x1A04);
106 ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
110 def MSP430identstr(self):
111 """Grab model string."""
112 return self.MSP430devices.get(self.MSP430ident());
115 0xf227: "MSP430F22xx",
116 0xf213: "MSP430F21x1",
117 0xf249: "MSP430F24x",
118 0xf26f: "MSP430F261x",
119 0xf237: "MSP430F23x0",
123 0xf16c: "MSP430F161x",
124 0xf149: "MSP430F13x", #or f14x(1)
125 0xf112: "MSP430F11x", #or f11x1
126 0xf143: "MSP430F14x",
127 0xf112: "MSP430F11x", #or F11x1A
128 0xf123: "MSP430F1xx", #or F123x
129 0x1132: "MSP430F1122", #or F1132
130 0x1232: "MSP430F1222", #or F1232
131 0xf169: "MSP430F16x",
134 0xF449: "MSP430F43x", #or F44x
135 0xF427: "MSP430FE42x", #or FW42x, F415, F417
136 0xF439: "MSP430FG43x",
137 0xf46f: "MSP430FG46xx", #or F471xx
140 def MSP430test(self):
141 """Test MSP430 JTAG. Requires that a chip be attached."""
142 if self.MSP430ident()==0xffff:
143 print "Is anything connected?";
144 print "Testing RAM from 200 to 210.";
145 for a in range(0x200,0x210):
146 self.MSP430poke(a,0);
147 if(self.MSP430peek(a)!=0):
148 print "Fault at %06x" % a;
149 self.MSP430poke(a,0xffff);
150 if(self.MSP430peek(a)!=0xffff):
151 print "Fault at %06x" % a;
153 print "Testing identity consistency."
154 ident=self.MSP430ident();
155 for a in range(1,20):
156 ident2=self.MSP430ident();
158 print "Identity %04x!=%04x" % (ident,ident2);
160 print "Testing flash erase."
161 self.MSP430masserase();
162 for a in range(0xffe0, 0xffff):
163 if self.MSP430peek(a)!=0xffff:
164 print "%04x unerased, equals %04x" % (
165 a, self.MSP430peek(a));
167 print "Testing flash write."
168 for a in range(0xffe0, 0xffff):
169 self.MSP430pokeflash(a,0xbeef);
170 if self.MSP430peek(a)!=0xbeef:
171 print "%04x unset, equals %04x" % (
172 a, self.MSP430peek(a));
174 print "Tests complete, erasing."
175 self.MSP430masserase();
177 def MSP430masserase(self):
178 """Erase MSP430 flash memory."""
179 self.writecmd(self.MSP430APP,0xE3,0,None);
181 def MSP430dumpbsl(self):
182 self.MSP430dumpmem(0xC00,0xfff);
183 def MSP430dumpallmem(self):
184 self.MSP430dumpmem(0x200,0xffff);
185 def MSP430dumpmem(self,begin,end):
188 print "%04x %04x" % (i, self.MSP430peek(i));