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 MSP430start(self):
62 """Start debugging."""
63 self.writecmd(self.MSP430APP,0x20,0,self.data);
64 self.JTAGID=ord(self.data[0]);
65 #print "Identified as %02x." % self.JTAGID;
66 if(not (self.JTAGID==0x89 or self.JTAGID==0x91)):
67 print "Error, misidentified as %02x." % self.JTAGID;
69 def MSP430haltcpu(self):
71 self.writecmd(self.MSP430APP,0xA0,0,self.data);
72 def MSP430releasecpu(self):
74 self.writecmd(self.MSP430APP,0xA1,0,self.data);
75 def MSP430shiftir8(self,ins):
76 """Shift the 8-bit Instruction Register."""
78 self.writecmd(self.MSP430APP,0x80,1,data);
79 return ord(self.data[0]);
80 def MSP430shiftdr16(self,dat):
81 """Shift the 16-bit Data Register."""
82 data=[dat&0xFF,(dat&0xFF00)>>8];
83 self.writecmd(self.MSP430APP,0x81,2,data);
84 return ord(self.data[0])#+(ord(self.data[1])<<8);
85 def MSP430setinstrfetch(self):
86 """Set the instruction fetch mode."""
87 self.writecmd(self.MSP430APP,0xC1,0,self.data);
89 def MSP430ident(self):
90 """Grab self-identification word from 0x0FF0 as big endian."""
92 if(self.JTAGID==0x89):
93 i=self.MSP430peek(0x0ff0);
94 ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
96 if(self.JTAGID==0x91):
97 i=self.MSP430peek(0x1A04);
98 ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
102 def MSP430identstr(self):
103 """Grab model string."""
104 return self.MSP430devices.get(self.MSP430ident());
107 0xf227: "MSP430F22xx",
108 0xf213: "MSP430F21x1",
109 0xf249: "MSP430F24x",
110 0xf26f: "MSP430F261x",
113 0xf16c: "MSP430F161x",
114 0xf149: "MSP430F13x", #or f14x(1)
115 0xf112: "MSP430F11x", #or f11x1
116 0xf143: "MSP430F14x",
117 0xf112: "MSP430F11x", #or F11x1A
118 0xf123: "MSP430F1xx", #or F123x
119 0x1132: "MSP430F1122", #or F1132
120 0x1232: "MSP430F1222", #or F1232
121 0xf169: "MSP430F16x",
124 0xF449: "MSP430F43x", #or F44x
125 0xF427: "MSP430FE42x", #or FW42x, F415, F417
126 0xF439: "MSP430FG43x",
127 0xf46f: "MSP430FG46xx", #or F471xx
130 def MSP430test(self):
131 """Test MSP430 JTAG. Requires that a chip be attached."""
132 if self.MSP430ident()==0xffff:
133 print "Is anything connected?";
134 print "Testing RAM from 1c00 to 1d00.";
135 for a in range(0x1c00,0x1d00):
136 self.MSP430poke(a,0);
137 if(self.MSP430peek(a)!=0):
138 print "Fault at %06x" % a;
139 self.MSP430poke(a,0xffff);
140 if(self.MSP430peek(a)!=0xffff):
141 print "Fault at %06x" % a;
142 print "RAM Test Complete."
144 print "Identity %04x" % self.MSP430ident();
146 def MSP430flashtest(self):
147 self.MSP430masserase();
150 if(self.MSP430peek(i)!=0xFFFF):
151 print "ERROR: Unerased flash at %04x."%i;
152 self.MSP430writeflash(i,0xDEAD);
154 def MSP430masserase(self):
155 """Erase MSP430 flash memory."""
156 self.writecmd(self.MSP430APP,0xE3,0,None);
157 def MSP430writeflash(self,adr,val):
158 """Write a word of flash memory."""
159 if(self.MSP430peek(adr)!=0xFFFF):
160 print "FLASH ERROR: %04x not clear." % adr;
161 data=[adr&0xFF,(adr&0xFF00)>>8,val&0xFF,(val&0xFF00)>>8];
162 self.writecmd(self.MSP430APP,0xE1,4,data);
163 rval=ord(self.data[0])+(ord(self.data[1])<<8);
165 print "FLASH WRITE ERROR AT %04x. Found %04x, wrote %04x." % (adr,rval,val);
167 def MSP430dumpbsl(self):
168 self.MSP430dumpmem(0xC00,0xfff);
169 def MSP430dumpallmem(self):
170 self.MSP430dumpmem(0x200,0xffff);
171 def MSP430dumpmem(self,begin,end):
174 print "%04x %04x" % (i, self.MSP430peek(i));