cdb83bbc9e8385b23b9a42f6cfdbced477c96d48
[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 the contents of memory at an address."""
40         self.data=[adr&0xff, (adr&0xff00)>>8,
41                    (adr&0xff0000)>>16,(adr&0xff000000)>>24];
42         self.writecmd(self.MSP430APP,0x02,4,self.data);
43         return ord(self.data[0])+(ord(self.data[1])<<8);
44     def MSP430poke(self,adr,val):
45         """Write the contents of memory at an address."""
46         self.data=[adr&0xff, (adr&0xff00)>>8, val&0xff, (val&0xff00)>>8];
47         self.writecmd(self.MSP430APP,0x03,4,self.data);
48         return ord(self.data[0])+(ord(self.data[1])<<8);
49     def MSP430start(self):
50         """Start debugging."""
51         self.writecmd(self.MSP430APP,0x20,0,self.data);
52         self.JTAGID=ord(self.data[0]);
53         #print "Identified as %02x." % id;
54         if(self.JTAGID==0x89 or self.JTAGID==0x91):
55             print "Successfully connected."
56         else:
57             print "Error, misidentified as %02x." % id;
58     
59     def MSP430haltcpu(self):
60         """Halt the CPU."""
61         self.writecmd(self.MSP430APP,0xA0,0,self.data);
62     def MSP430releasecpu(self):
63         """Resume the CPU."""
64         self.writecmd(self.MSP430APP,0xA1,0,self.data);
65     def MSP430shiftir8(self,ins):
66         """Shift the 8-bit Instruction Register."""
67         data=[ins];
68         self.writecmd(self.MSP430APP,0x80,1,data);
69         return ord(self.data[0]);
70     def MSP430shiftdr16(self,dat):
71         """Shift the 16-bit Data Register."""
72         data=[dat&0xFF,(dat&0xFF00)>>8];
73         self.writecmd(self.MSP430APP,0x81,2,data);
74         return ord(self.data[0])#+(ord(self.data[1])<<8);
75     def MSP430setinstrfetch(self):
76         """Set the instruction fetch mode."""
77         self.writecmd(self.MSP430APP,0xC1,0,self.data);
78         return self.data[0];
79     def MSP430ident(self):
80         """Grab self-identification word from 0x0FF0 as big endian."""
81         if(self.JTAGID==0x89):
82             i=self.MSP430peek(0x0ff0);
83             ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
84         if(self.JTAGID==0x91):
85             i=self.MSP430peek(0x1A04);
86             ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
87         return ident;
88     def MSP430test(self):
89         """Test MSP430 JTAG.  Requires that a chip be attached."""
90         if self.MSP430ident()==0xffff:
91             print "Is anything connected?";
92         print "Testing RAM.";
93         temp=self.MSP430peek(0x0200);
94         self.MSP430poke(0x0200,0xdead);
95         if(self.MSP430peek(0x0200)!=0xdead):
96             print "Poke of 0x0200 did not set to 0xDEAD properly.";
97             return;
98         self.MSP430poke(0x0200,temp); #restore old value.
99     def MSP430flashtest(self):
100         self.MSP430masserase();
101         i=0x2500;
102         while(i<0xFFFF):
103             if(self.MSP430peek(i)!=0xFFFF):
104                 print "ERROR: Unerased flash at %04x."%i;
105             self.MSP430writeflash(i,0xDEAD);
106             i+=2;
107     def MSP430masserase(self):
108         """Erase MSP430 flash memory."""
109         self.writecmd(self.MSP430APP,0xE3,0,None);
110     def MSP430writeflash(self,adr,val):
111         """Write a word of flash memory."""
112         if(self.MSP430peek(adr)!=0xFFFF):
113             print "FLASH ERROR: %04x not clear." % adr;
114         data=[adr&0xFF,(adr&0xFF00)>>8,val&0xFF,(val&0xFF00)>>8];
115         self.writecmd(self.MSP430APP,0xE1,4,data);
116         rval=ord(self.data[0])+(ord(self.data[1])<<8);
117         if(val!=rval):
118             print "FLASH WRITE ERROR AT %04x.  Found %04x, wrote %04x." % (adr,rval,val);
119         
120     def MSP430dumpbsl(self):
121         self.MSP430dumpmem(0xC00,0xfff);
122     def MSP430dumpallmem(self):
123         self.MSP430dumpmem(0x200,0xffff);
124     def MSP430dumpmem(self,begin,end):
125         i=begin;
126         while i<end:
127             print "%04x %04x" % (i, self.MSP430peek(i));
128             i+=2;