Moved the MSP430 client to its own file, preparing for MSP430X2 (5xx) support.
[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     def MSP430setup(self):
15         """Move the FET into the MSP430 JTAG application."""
16         print "Initializing MSP430.";
17         self.writecmd(0x11,0x10,0,self.data);
18     def MSP430stop(self):
19         """Stop debugging."""
20         self.writecmd(self.MSP430APP,0x21,0,self.data);
21
22     def MSP430peek(self,adr):
23         """Read the contents of memory at an address."""
24         self.data=[adr&0xff, (adr&0xff00)>>8];
25         self.writecmd(self.MSP430APP,0x02,2,self.data);
26         return ord(self.data[0])+(ord(self.data[1])<<8);
27     def MSP430poke(self,adr,val):
28         """Read the contents of memory at an address."""
29         self.data=[adr&0xff, (adr&0xff00)>>8, val&0xff, (val&0xff00)>>8];
30         self.writecmd(self.MSP430APP,0x03,4,self.data);
31         return;# ord(self.data[0])+(ord(self.data[1])<<8);
32     def MSP430start(self):
33         """Start debugging."""
34         self.writecmd(self.MSP430APP,0x20,0,self.data);
35         ident=self.MSP430ident();
36         print "Target identifies as %04x." % ident;
37     
38     def MSP430haltcpu(self):
39         """Halt the CPU."""
40         self.writecmd(self.MSP430APP,0xA0,0,self.data);
41     def MSP430releasecpu(self):
42         """Resume the CPU."""
43         self.writecmd(self.MSP430APP,0xA1,0,self.data);
44     def MSP430shiftir8(self,ins):
45         """Shift the 8-bit Instruction Register."""
46         data=[ins];
47         self.writecmd(self.MSP430APP,0x80,1,data);
48         return ord(self.data[0]);
49     def MSP430shiftdr16(self,dat):
50         """Shift the 16-bit Data Register."""
51         data=[dat&0xFF,(dat&0xFF00)>>8];
52         self.writecmd(self.MSP430APP,0x81,2,data);
53         return ord(self.data[0])#+(ord(self.data[1])<<8);
54     def MSP430setinstrfetch(self):
55         """Set the instruction fetch mode."""
56         self.writecmd(self.MSP430APP,0xC1,0,self.data);
57         return self.data[0];
58     def MSP430ident(self):
59         """Grab self-identification word from 0x0FF0 as big endian."""
60         i=self.MSP430peek(0x0ff0);
61         return ((i&0xFF00)>>8)+((i&0xFF)<<8)
62     def MSP430test(self):
63         """Test MSP430 JTAG.  Requires that a chip be attached."""
64         if self.MSP430ident()==0xffff:
65             print "Is anything connected?";
66         print "Testing RAM.";
67         temp=self.MSP430peek(0x0200);
68         self.MSP430poke(0x0200,0xdead);
69         if(self.MSP430peek(0x0200)!=0xdead):
70             print "Poke of 0x0200 did not set to 0xDEAD properly.";
71             return;
72         self.MSP430poke(0x0200,temp); #restore old value.
73     def MSP430flashtest(self):
74         self.MSP430masserase();
75         i=0x2500;
76         while(i<0xFFFF):
77             if(self.MSP430peek(i)!=0xFFFF):
78                 print "ERROR: Unerased flash at %04x."%i;
79             self.MSP430writeflash(i,0xDEAD);
80             i+=2;
81     def MSP430masserase(self):
82         """Erase MSP430 flash memory."""
83         self.writecmd(self.MSP430APP,0xE3,0,None);
84     def MSP430writeflash(self,adr,val):
85         """Write a word of flash memory."""
86         if(self.MSP430peek(adr)!=0xFFFF):
87             print "FLASH ERROR: %04x not clear." % adr;
88         data=[adr&0xFF,(adr&0xFF00)>>8,val&0xFF,(val&0xFF00)>>8];
89         self.writecmd(self.MSP430APP,0xE1,4,data);
90         rval=ord(self.data[0])+(ord(self.data[1])<<8);
91         if(val!=rval):
92             print "FLASH WRITE ERROR AT %04x.  Found %04x, wrote %04x." % (adr,rval,val);
93             
94     def MSP430dumpbsl(self):
95         self.MSP430dumpmem(0xC00,0xfff);
96     def MSP430dumpallmem(self):
97         self.MSP430dumpmem(0x200,0xffff);
98     def MSP430dumpmem(self,begin,end):
99         i=begin;
100         while i<end:
101             print "%04x %04x" % (i, self.MSP430peek(i));
102             i+=2;