updates to JTAGARM7 appid and some additional test play
[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 Device 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         written=ord(self.data[0])+(ord(self.data[1])<<8);
68         if(written!=val):
69             print "Failed to write 0x%04x to 0x$04x" % (val,adr);
70         return written;
71     def MSP430pokeflash(self,adr,val):
72         """Write the contents of flash memory at an address."""
73         self.data=[adr&0xff, (adr&0xff00)>>8,
74                    (adr&0xff0000)>>16,(adr&0xff000000)>>24,
75                    val&0xff, (val&0xff00)>>8];
76         self.writecmd(self.MSP430APP,0xE1,6,self.data);
77         return ord(self.data[0])+(ord(self.data[1])<<8);
78     def MSP430pokeflashblock(self,adr,data):
79         """Write many words to flash memory at an address."""
80         self.data=[adr&0xff, (adr&0xff00)>>8,
81                    (adr&0xff0000)>>16,(adr&0xff000000)>>24]+data;
82         #print "Writing %i bytes to %x" % (len(data),adr);
83         #print "%2x %2x %2x %2x ..." % (data[0], data[1], data[2], data[3]);
84         self.writecmd(self.MSP430APP,0xE1,len(self.data),self.data);
85         return ord(self.data[0])+(ord(self.data[1])<<8);
86     def start(self):
87         """Start debugging."""
88         self.writecmd(self.MSP430APP,0x20,0,self.data);
89         self.JTAGID=ord(self.data[0]);
90         #print "Identified as %02x." % self.JTAGID;
91         if(not (self.JTAGID==0x89 or self.JTAGID==0x91)):
92             print "Error, misidentified as %02x.\nCheck wiring, as this should be 0x89 or 0x91." % self.JTAGID;
93         self.MSP430haltcpu();
94     def MSP430haltcpu(self):
95         """Halt the CPU."""
96         self.writecmd(self.MSP430APP,0xA0,0,self.data);
97     def MSP430releasecpu(self):
98         """Resume the CPU."""
99         self.writecmd(self.MSP430APP,0xA1,0,self.data);
100     def MSP430shiftir8(self,ins):
101         """Shift the 8-bit Instruction Register."""
102         data=[ins];
103         self.writecmd(self.MSP430APP,0x80,1,data);
104         return ord(self.data[0]);
105     def MSP430shiftdr16(self,dat):
106         """Shift the 16-bit Data Register."""
107         data=[dat&0xFF,(dat&0xFF00)>>8];
108         self.writecmd(self.MSP430APP,0x81,2,data);
109         return ord(self.data[0])#+(ord(self.data[1])<<8);
110     def MSP430setinstrfetch(self):
111         """Set the instruction fetch mode."""
112         self.writecmd(self.MSP430APP,0xC1,0,self.data);
113         return self.data[0];
114     def MSP430ident(self):
115         """Grab self-identification word from 0x0FF0 as big endian."""
116         ident=0x00;
117         if(self.JTAGID==0x89):
118             i=self.MSP430peek(0x0ff0);
119             ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
120             
121         if(self.JTAGID==0x91):
122             i=self.MSP430peek(0x1A04);
123             ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
124             #ident=0x0091;
125         
126         return ident;
127     def MSP430identstr(self):
128         """Grab model string."""
129         return self.MSP430devices.get(self.MSP430ident());
130     MSP430devices={
131         #MSP430F2xx
132         0xf227: "MSP430F22xx",
133         0xf213: "MSP430F21x1",
134         0xf249: "MSP430F24x",
135         0xf26f: "MSP430F261x",
136         0xf237: "MSP430F23x0",
137         0xf201: "MSP430F201x",
138         
139         #MSP430F1xx
140         0xf16c: "MSP430F161x",
141         0xf149: "MSP430F13x",  #or f14x(1)
142         0xf112: "MSP430F11x",  #or f11x1
143         0xf143: "MSP430F14x",
144         0xf112: "MSP430F11x",  #or F11x1A
145         0xf123: "MSP430F1xx",  #or F123x
146         0x1132: "MSP430F1122", #or F1132
147         0x1232: "MSP430F1222", #or F1232
148         0xf169: "MSP430F16x",
149         
150         #MSP430F4xx
151         0xF449: "MSP430F43x", #or F44x
152         0xF427: "MSP430FE42x", #or FW42x, F415, F417
153         0xF439: "MSP430FG43x",
154         0xf46f: "MSP430FG46xx", #or F471xx
155         
156         }
157     def MSP430test(self):
158         """Test MSP430 JTAG.  Requires that a chip be attached."""
159         
160         if self.MSP430ident()==0xffff:
161             print "ERROR Is anything connected?";
162         print "Testing %s." % self.MSP430identstr();
163         print "Testing RAM from 200 to 210.";
164         for a in range(0x200,0x210):
165             self.MSP430poke(a,0);
166             if(self.MSP430peek(a)!=0):
167                 print "Fault at %06x" % a;
168             self.MSP430poke(a,0xffff);
169             if(self.MSP430peek(a)!=0xffff):
170                 print "Fault at %06x" % a;
171                 
172         print "Testing identity consistency."
173         ident=self.MSP430ident();
174         for a in range(1,20):
175             ident2=self.MSP430ident();
176             if ident!=ident2:
177                 print "Identity %04x!=%04x" % (ident,ident2);
178         
179         print "Testing flash erase."
180         self.MSP430masserase();
181         for a in range(0xffe0, 0xffff):
182             if self.MSP430peek(a)!=0xffff:
183                 print "%04x unerased, equals %04x" % (
184                     a, self.MSP430peek(a));
185
186         print "Testing flash write."
187         for a in range(0xffe0, 0xffff):
188             self.MSP430pokeflash(a,0xbeef);
189             if self.MSP430peek(a)!=0xbeef:
190                 print "%04x unset, equals %04x" % (
191                     a, self.MSP430peek(a));
192         
193         print "Tests complete, erasing."
194         self.MSP430masserase();
195         
196     def MSP430masserase(self):
197         """Erase MSP430 flash memory."""
198         self.writecmd(self.MSP430APP,0xE3,0,None);
199     def MSP430infoerase(self):
200         """Erase MSP430 info flash."""
201         self.writecmd(self.MSP430APP,0xE8,0,None);
202
203     def MSP430setPC(self, pc):
204         """Set the program counter."""
205         self.writecmd(self.MSP430APP,0xC2,2,[pc&0xFF,(pc>>8)&0xFF]);
206     def MSP430run(self):
207         """Reset the MSP430 to run on its own."""
208         self.writecmd(self.MSP430APP,0x21,0,None);
209     def MSP430dumpbsl(self):
210         self.MSP430dumpmem(0xC00,0xfff);
211     def MSP430dumpallmem(self):
212         self.MSP430dumpmem(0x200,0xffff);
213     def MSP430dumpmem(self,begin,end):
214         i=begin;
215         while i<end:
216             print "%04x %04x" % (i, self.MSP430peek(i));
217             i+=2;