Glitching stuff.
[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, serial, os;
9
10 from GoodFET import GoodFET;
11
12 class GoodFETMSP430(GoodFET):
13     APP=0x11;
14     MSP430APP=0x11;  #Changed by inheritors.
15     CoreID=0;
16     DeviceID=0;
17     JTAGID=0;
18     MSP430ident=0;
19     def setup(self):
20         """Move the FET into the MSP430 JTAG application."""
21         self.writecmd(self.MSP430APP,0x10,0,None);
22         
23     def MSP430stop(self):
24         """Stop debugging."""
25         self.writecmd(self.MSP430APP,0x21,0,self.data);
26     
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);
31         return CoreID;
32     def MSP430deviceid(self):
33         """Get the Core ID."""
34         self.writecmd(self.MSP430APP,0xF1);
35         DeviceID=(
36             ord(self.data[0])+(ord(self.data[1])<<8)+
37             (ord(self.data[2])<<16)+(ord(self.data[3])<<24));
38         return DeviceID;
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;
44         else: return adr>>8;
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,
49                    ];
50         self.writecmd(self.MSP430APP,0x02,4,self.data);
51         
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,
57               0x00,0x04];
58         self.writecmd(self.MSP430APP,0x02,6,data);
59         return self.data;
60     
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);
83     def start(self):
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;
90         self.MSP430haltcpu();
91     def MSP430haltcpu(self):
92         """Halt the CPU."""
93         self.writecmd(self.MSP430APP,0xA0,0,self.data);
94     def MSP430releasecpu(self):
95         """Resume the CPU."""
96         self.writecmd(self.MSP430APP,0xA1,0,self.data);
97     def MSP430shiftir8(self,ins):
98         """Shift the 8-bit Instruction Register."""
99         data=[ins];
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);
110         return self.data[0];
111     def MSP430ident(self):
112         """Grab self-identification word from 0x0FF0 as big endian."""
113         ident=0x00;
114         if(self.JTAGID==0x89):
115             i=self.MSP430peek(0x0ff0);
116             ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
117             
118         if(self.JTAGID==0x91):
119             i=self.MSP430peek(0x1A04);
120             ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
121             #ident=0x0091;
122         
123         return ident;
124     def MSP430identstr(self):
125         """Grab model string."""
126         return self.MSP430devices.get(self.MSP430ident());
127     MSP430devices={
128         #MSP430F2xx
129         0xf227: "MSP430F22xx",
130         0xf213: "MSP430F21x1",
131         0xf249: "MSP430F24x",
132         0xf26f: "MSP430F261x",
133         0xf237: "MSP430F23x0",
134         0xf201: "MSP430F201x",
135         
136         #MSP430F1xx
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",
146         
147         #MSP430F4xx
148         0xF449: "MSP430F43x", #or F44x
149         0xF427: "MSP430FE42x", #or FW42x, F415, F417
150         0xF439: "MSP430FG43x",
151         0xf46f: "MSP430FG46xx", #or F471xx
152         
153         }
154     def MSP430test(self):
155         """Test MSP430 JTAG.  Requires that a chip be attached."""
156         
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;
168                 
169         print "Testing identity consistency."
170         ident=self.MSP430ident();
171         for a in range(1,20):
172             ident2=self.MSP430ident();
173             if ident!=ident2:
174                 print "Identity %04x!=%04x" % (ident,ident2);
175         
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));
182
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));
189         
190         print "Tests complete, erasing."
191         self.MSP430masserase();
192         
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]);
199     def MSP430run(self):
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):
207         i=begin;
208         while i<end:
209             print "%04x %04x" % (i, self.MSP430peek(i));
210             i+=2;