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