743ba55056d84abc7834f0ebc9c25cc72643206a
[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 MSP430start(self):
62         """Start debugging."""
63         self.writecmd(self.MSP430APP,0x20,0,self.data);
64         self.JTAGID=ord(self.data[0]);
65         #print "Identified as %02x." % self.JTAGID;
66         if(not (self.JTAGID==0x89 or self.JTAGID==0x91)):
67             print "Error, misidentified as %02x." % id;
68         
69     def MSP430haltcpu(self):
70         """Halt the CPU."""
71         self.writecmd(self.MSP430APP,0xA0,0,self.data);
72     def MSP430releasecpu(self):
73         """Resume the CPU."""
74         self.writecmd(self.MSP430APP,0xA1,0,self.data);
75     def MSP430shiftir8(self,ins):
76         """Shift the 8-bit Instruction Register."""
77         data=[ins];
78         self.writecmd(self.MSP430APP,0x80,1,data);
79         return ord(self.data[0]);
80     def MSP430shiftdr16(self,dat):
81         """Shift the 16-bit Data Register."""
82         data=[dat&0xFF,(dat&0xFF00)>>8];
83         self.writecmd(self.MSP430APP,0x81,2,data);
84         return ord(self.data[0])#+(ord(self.data[1])<<8);
85     def MSP430setinstrfetch(self):
86         """Set the instruction fetch mode."""
87         self.writecmd(self.MSP430APP,0xC1,0,self.data);
88         return self.data[0];
89     def MSP430ident(self):
90         """Grab self-identification word from 0x0FF0 as big endian."""
91         if(self.JTAGID==0x89):
92             i=self.MSP430peek(0x0ff0);
93             ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
94             
95         if(self.JTAGID==0x91):
96             i=self.MSP430peek(0x1A04);
97             ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
98             #ident=0x0091;
99             
100         return ident;
101     def MSP430test(self):
102         """Test MSP430 JTAG.  Requires that a chip be attached."""
103         if self.MSP430ident()==0xffff:
104             print "Is anything connected?";
105         print "Testing RAM from 1c00 to 1d00.";
106         for a in range(0x1c00,0x1d00):
107             self.MSP430poke(a,0);
108             if(self.MSP430peek(a)!=0):
109                 print "Fault at %06x" % a;
110             self.MSP430poke(a,0xffff);
111             if(self.MSP430peek(a)!=0xffff):
112                 print "Fault at %06x" % a;
113         print "RAM Test Complete."
114         for a in range(1,5):
115             print "Identity %04x" % self.MSP430ident();
116             
117     def MSP430flashtest(self):
118         self.MSP430masserase();
119         i=0x2500;
120         while(i<0xFFFF):
121             if(self.MSP430peek(i)!=0xFFFF):
122                 print "ERROR: Unerased flash at %04x."%i;
123             self.MSP430writeflash(i,0xDEAD);
124             i+=2;
125     def MSP430masserase(self):
126         """Erase MSP430 flash memory."""
127         self.writecmd(self.MSP430APP,0xE3,0,None);
128     def MSP430writeflash(self,adr,val):
129         """Write a word of flash memory."""
130         if(self.MSP430peek(adr)!=0xFFFF):
131             print "FLASH ERROR: %04x not clear." % adr;
132         data=[adr&0xFF,(adr&0xFF00)>>8,val&0xFF,(val&0xFF00)>>8];
133         self.writecmd(self.MSP430APP,0xE1,4,data);
134         rval=ord(self.data[0])+(ord(self.data[1])<<8);
135         if(val!=rval):
136             print "FLASH WRITE ERROR AT %04x.  Found %04x, wrote %04x." % (adr,rval,val);
137         
138     def MSP430dumpbsl(self):
139         self.MSP430dumpmem(0xC00,0xfff);
140     def MSP430dumpallmem(self):
141         self.MSP430dumpmem(0x200,0xffff);
142     def MSP430dumpmem(self,begin,end):
143         i=begin;
144         while i<end:
145             print "%04x %04x" % (i, self.MSP430peek(i));
146             i+=2;