MSP430 test cases, stable poking of RAM.
[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     MSP430APP=0x11;  #Changed by inheritors.
14     CoreID=0;
15     DeviceID=0;
16     JTAGID=0;
17     MSP430ident=0;
18     def MSP430setup(self):
19         """Move the FET into the MSP430 JTAG application."""
20         self.writecmd(self.MSP430APP,0x10,0,None);
21         
22     def MSP430stop(self):
23         """Stop debugging."""
24         self.writecmd(self.MSP430APP,0x21,0,self.data);
25     
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);
30         return CoreID;
31     def MSP430deviceid(self):
32         """Get the Core ID."""
33         self.writecmd(self.MSP430APP,0xF1);
34         DeviceID=(
35             ord(self.data[0])+(ord(self.data[1])<<8)+
36             (ord(self.data[2])<<16)+(ord(self.data[3])<<24));
37         return DeviceID;
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,
42                    ]; 
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,
49               blocks];
50         
51         self.writecmd(self.MSP430APP,0x02,5,data,blocks);
52         return self.data;
53     
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);
68     
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;
76         
77     def MSP430haltcpu(self):
78         """Halt the CPU."""
79         self.writecmd(self.MSP430APP,0xA0,0,self.data);
80     def MSP430releasecpu(self):
81         """Resume the CPU."""
82         self.writecmd(self.MSP430APP,0xA1,0,self.data);
83     def MSP430shiftir8(self,ins):
84         """Shift the 8-bit Instruction Register."""
85         data=[ins];
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);
96         return self.data[0];
97     def MSP430ident(self):
98         """Grab self-identification word from 0x0FF0 as big endian."""
99         ident=0x00;
100         if(self.JTAGID==0x89):
101             i=self.MSP430peek(0x0ff0);
102             ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
103             
104         if(self.JTAGID==0x91):
105             i=self.MSP430peek(0x1A04);
106             ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
107             #ident=0x0091;
108         
109         return ident;
110     def MSP430identstr(self):
111         """Grab model string."""
112         return self.MSP430devices.get(self.MSP430ident());
113     MSP430devices={
114         #MSP430F2xx
115         0xf227: "MSP430F22xx",
116         0xf213: "MSP430F21x1",
117         0xf249: "MSP430F24x",
118         0xf26f: "MSP430F261x",
119         
120         #MSP430F1xx
121         0xf16c: "MSP430F161x",
122         0xf149: "MSP430F13x", #or f14x(1)
123         0xf112: "MSP430F11x", #or f11x1
124         0xf143: "MSP430F14x",
125         0xf112: "MSP430F11x", #or F11x1A
126         0xf123: "MSP430F1xx", #or F123x
127         0x1132: "MSP430F1122", #or F1132
128         0x1232: "MSP430F1222", #or F1232
129         0xf169: "MSP430F16x",
130         
131         #MSP430F4xx
132         0xF449: "MSP430F43x", #or F44x
133         0xF427: "MSP430FE42x", #or FW42x, F415, F417
134         0xF439: "MSP430FG43x",
135         0xf46f: "MSP430FG46xx", #or F471xx
136         
137         }
138     def MSP430test(self):
139         """Test MSP430 JTAG.  Requires that a chip be attached."""
140         if self.MSP430ident()==0xffff:
141             print "Is anything connected?";
142         print "Testing RAM from 200 to 210.";
143         for a in range(0x200,0x210):
144             self.MSP430poke(a,0);
145             if(self.MSP430peek(a)!=0):
146                 print "Fault at %06x" % a;
147             self.MSP430poke(a,0xffff);
148             if(self.MSP430peek(a)!=0xffff):
149                 print "Fault at %06x" % a;
150                 
151         print "Testing identity consistency."
152         ident=self.MSP430ident();
153         for a in range(1,20):
154             ident2=self.MSP430ident();
155             if ident!=ident2:
156                 print "Identity %04x!=%04x" % (ident,ident2);
157         
158         print "Testing flash erase."
159         self.MSP430masserase();
160         for a in range(0xffe0, 0xffff):
161             if self.MSP430peek(a)!=0xffff:
162                 print "%04x unerased, equals %04x" % (
163                     a, self.MSP430peek(a));
164
165         print "Testing flash write."
166         for a in range(0xffe0, 0xffff):
167             self.MSP430pokeflash(a,0xbeef);
168             if self.MSP430peek(a)!=0xbeef:
169                 print "%04x unset, equals %04x" % (
170                     a, self.MSP430peek(a));
171         
172         print "Tests complete, erasing."
173         self.MSP430masserase();
174         
175     def MSP430flashtest(self):
176         self.MSP430masserase();
177         i=0x2500;
178         while(i<0xFFFF):
179             if(self.MSP430peek(i)!=0xFFFF):
180                 print "ERROR: Unerased flash at %04x."%i;
181             self.MSP430writeflash(i,0xDEAD);
182             i+=2;
183     def MSP430masserase(self):
184         """Erase MSP430 flash memory."""
185         self.writecmd(self.MSP430APP,0xE3,0,None);
186     
187     def MSP430dumpbsl(self):
188         self.MSP430dumpmem(0xC00,0xfff);
189     def MSP430dumpallmem(self):
190         self.MSP430dumpmem(0x200,0xffff);
191     def MSP430dumpmem(self,begin,end):
192         i=begin;
193         while i<end:
194             print "%04x %04x" % (i, self.MSP430peek(i));
195             i+=2;