added fast packet sniffing option -x. doubles RX speed but could easily result in...
[goodfet] / client / GoodFETMSP430.py
1 #!/usr/bin/env python
2 # GoodFET Client Library
3
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # Presently being rewritten.
7
8 import sys, time, string, cStringIO, struct, glob, os;
9
10 from GoodFET import GoodFET;
11
12 class GoodFETMSP430(GoodFET):
13     #Set APP to be MSP430APP or MSP430X2APP, the latter being preferred.
14     
15     #0x16 for class, 0x17 for SBW, 0x11 by default
16     APP=0x11;
17     MSP430APP=0x11;    
18     
19     CoreID=0;
20     DeviceID=0;
21     JTAGID=0;
22     MSP430ident=0;
23     def setup(self):
24         """Move the FET into the MSP430 JTAG application."""
25         self.writecmd(self.MSP430APP,0x10,0,None);
26         
27     def MSP430stop(self):
28         """Stop debugging."""
29         self.writecmd(self.MSP430APP,0x21,0,self.data);
30     
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);
35         return CoreID;
36     def MSP430deviceid(self):
37         """Get the Device ID. (MSP430X2 only?)"""
38         self.writecmd(self.MSP430APP,0xF1);
39         DeviceID=(
40             ord(self.data[0])+(ord(self.data[1])<<8)+
41             (ord(self.data[2])<<16)+(ord(self.data[3])<<24));
42         return DeviceID;
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;
50         else: return adr>>8;
51
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,
56                    ];
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,
64               0x00,0x04];
65         self.writecmd(self.MSP430APP,0x02,6,data);
66         return self.data;
67     
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);
75         if(written!=val):
76             print "Failed to write 0x%04x to 0x%04x" % (val,adr);
77         return written;
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);
88     def getsecret(self):
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);
99     def start(self):
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)):
104             #Try once more
105             self.writecmd(self.MSP430APP,0x20,0,self.data);
106             self.JTAGID=ord(self.data[0]);
107         
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):
113         """Halt the CPU."""
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."""
120         data=[ins];
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);
131         return self.data[0];
132     def MSP430ident(self):
133         """Grab self-identification word from 0x0FF0 as big endian."""
134         ident=0x00;
135         if(self.JTAGID==0x89):
136             i=self.MSP430peek(0x0ff0);
137             ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
138             
139         if(self.JTAGID==0x91):
140             i=self.MSP430peek(0x1A04);
141             ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
142             #ident=0x0091;
143         
144         return ident;
145     def MSP430identstr(self):
146         """Grab model string."""
147         return self.MSP430devices.get(self.MSP430ident());
148     MSP430devices={
149         #MSP430F2xx
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",
158         
159         #MSP430F1xx
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",
169         
170         #MSP430F4xx
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.
176         }
177     def MSP430test(self):
178         """Test MSP430 JTAG.  Requires that a chip be attached."""
179         
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;
191                 
192         print "Testing identity consistency."
193         ident=self.MSP430ident();
194         for a in range(1,20):
195             ident2=self.MSP430ident();
196             if ident!=ident2:
197                 print "Identity %04x!=%04x" % (ident,ident2);
198         
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));
205
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));
212         
213         print "Tests complete, erasing."
214         self.MSP430masserase();
215     def erase(self):
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);
223
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);
234
235     def MSP430run(self):
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):
243         i=begin;
244         while i<end:
245             print "%04x %04x" % (i, self.MSP430peek(i));
246             i+=2;