Chipcon peek and poke for iram, include SFRs.
[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);
44         
45         return ord(self.data[0])+(ord(self.data[1])<<8);
46     def MSP430peekblock(self,adr):
47         """Grab a few block from an SPI Flash ROM.  Block size is unknown"""
48         data=[adr&0xff, (adr&0xff00)>>8,
49               (adr&0xff0000)>>16,(adr&0xff000000)>>24,
50               0x00,0x04];
51         self.writecmd(self.MSP430APP,0x02,6,data);
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         0xf237: "MSP430F23x0",
120         
121         
122         #MSP430F1xx
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",
132         
133         #MSP430F4xx
134         0xF449: "MSP430F43x", #or F44x
135         0xF427: "MSP430FE42x", #or FW42x, F415, F417
136         0xF439: "MSP430FG43x",
137         0xf46f: "MSP430FG46xx", #or F471xx
138         
139         }
140     def MSP430test(self):
141         """Test MSP430 JTAG.  Requires that a chip be attached."""
142         
143         if self.MSP430ident()==0xffff:
144             print "ERROR Is anything connected?";
145         print "Testing %s." % self.MSP430identstr();
146         print "Testing RAM from 200 to 210.";
147         for a in range(0x200,0x210):
148             self.MSP430poke(a,0);
149             if(self.MSP430peek(a)!=0):
150                 print "Fault at %06x" % a;
151             self.MSP430poke(a,0xffff);
152             if(self.MSP430peek(a)!=0xffff):
153                 print "Fault at %06x" % a;
154                 
155         print "Testing identity consistency."
156         ident=self.MSP430ident();
157         for a in range(1,20):
158             ident2=self.MSP430ident();
159             if ident!=ident2:
160                 print "Identity %04x!=%04x" % (ident,ident2);
161         
162         print "Testing flash erase."
163         self.MSP430masserase();
164         for a in range(0xffe0, 0xffff):
165             if self.MSP430peek(a)!=0xffff:
166                 print "%04x unerased, equals %04x" % (
167                     a, self.MSP430peek(a));
168
169         print "Testing flash write."
170         for a in range(0xffe0, 0xffff):
171             self.MSP430pokeflash(a,0xbeef);
172             if self.MSP430peek(a)!=0xbeef:
173                 print "%04x unset, equals %04x" % (
174                     a, self.MSP430peek(a));
175         
176         print "Tests complete, erasing."
177         self.MSP430masserase();
178         
179     def MSP430masserase(self):
180         """Erase MSP430 flash memory."""
181         self.writecmd(self.MSP430APP,0xE3,0,None);
182     
183     def MSP430dumpbsl(self):
184         self.MSP430dumpmem(0xC00,0xfff);
185     def MSP430dumpallmem(self):
186         self.MSP430dumpmem(0x200,0xffff);
187     def MSP430dumpmem(self,begin,end):
188         i=begin;
189         while i<end:
190             print "%04x %04x" % (i, self.MSP430peek(i));
191             i+=2;