2 # GoodFET Client Library
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
6 # Presently being rewritten.
8 import sys, time, string, cStringIO, struct, glob, serial, os;
10 from GoodFET import GoodFET;
12 class GoodFETMSP430(GoodFET):
13 MSP430APP=0x11; #Changed by inheritors.
18 def MSP430setup(self):
19 """Move the FET into the MSP430 JTAG application."""
20 self.writecmd(self.MSP430APP,0x10,0,None);
24 self.writecmd(self.MSP430APP,0x21,0,self.data);
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);
31 def MSP430deviceid(self):
32 """Get the Core ID."""
33 self.writecmd(self.MSP430APP,0xF1);
35 ord(self.data[0])+(ord(self.data[1])<<8)+
36 (ord(self.data[2])<<16)+(ord(self.data[3])<<24));
38 def MSP430peek(self,adr):
39 """Read the contents of memory at an address."""
40 self.data=[adr&0xff, (adr&0xff00)>>8];
41 self.writecmd(self.MSP430APP,0x02,2,self.data);
42 return ord(self.data[0])+(ord(self.data[1])<<8);
43 def MSP430poke(self,adr,val):
44 """Read the contents of memory at an address."""
45 self.data=[adr&0xff, (adr&0xff00)>>8, val&0xff, (val&0xff00)>>8];
46 self.writecmd(self.MSP430APP,0x03,4,self.data);
47 return;# ord(self.data[0])+(ord(self.data[1])<<8);
48 def MSP430start(self):
49 """Start debugging."""
50 self.writecmd(self.MSP430APP,0x20,0,self.data);
51 self.JTAGID=ord(self.data[0]);
52 #print "Identified as %02x." % id;
53 if(self.JTAGID==0x89 or self.JTAGID==0x91):
54 print "Successfully connected."
56 print "Error, misidentified as %02x." % id;
58 def MSP430haltcpu(self):
60 self.writecmd(self.MSP430APP,0xA0,0,self.data);
61 def MSP430releasecpu(self):
63 self.writecmd(self.MSP430APP,0xA1,0,self.data);
64 def MSP430shiftir8(self,ins):
65 """Shift the 8-bit Instruction Register."""
67 self.writecmd(self.MSP430APP,0x80,1,data);
68 return ord(self.data[0]);
69 def MSP430shiftdr16(self,dat):
70 """Shift the 16-bit Data Register."""
71 data=[dat&0xFF,(dat&0xFF00)>>8];
72 self.writecmd(self.MSP430APP,0x81,2,data);
73 return ord(self.data[0])#+(ord(self.data[1])<<8);
74 def MSP430setinstrfetch(self):
75 """Set the instruction fetch mode."""
76 self.writecmd(self.MSP430APP,0xC1,0,self.data);
78 def MSP430ident(self):
79 """Grab self-identification word from 0x0FF0 as big endian."""
80 if(self.JTAGID==0x89):
81 i=self.MSP430peek(0x0ff0);
82 ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
83 if(self.JTAGID==0x91):
84 i=self.MSP430peek(0x1A04);
85 ident=((i&0xFF00)>>8)+((i&0xFF)<<8)
88 """Test MSP430 JTAG. Requires that a chip be attached."""
89 if self.MSP430ident()==0xffff:
90 print "Is anything connected?";
92 temp=self.MSP430peek(0x0200);
93 self.MSP430poke(0x0200,0xdead);
94 if(self.MSP430peek(0x0200)!=0xdead):
95 print "Poke of 0x0200 did not set to 0xDEAD properly.";
97 self.MSP430poke(0x0200,temp); #restore old value.
98 def MSP430flashtest(self):
99 self.MSP430masserase();
102 if(self.MSP430peek(i)!=0xFFFF):
103 print "ERROR: Unerased flash at %04x."%i;
104 self.MSP430writeflash(i,0xDEAD);
106 def MSP430masserase(self):
107 """Erase MSP430 flash memory."""
108 self.writecmd(self.MSP430APP,0xE3,0,None);
109 def MSP430writeflash(self,adr,val):
110 """Write a word of flash memory."""
111 if(self.MSP430peek(adr)!=0xFFFF):
112 print "FLASH ERROR: %04x not clear." % adr;
113 data=[adr&0xFF,(adr&0xFF00)>>8,val&0xFF,(val&0xFF00)>>8];
114 self.writecmd(self.MSP430APP,0xE1,4,data);
115 rval=ord(self.data[0])+(ord(self.data[1])<<8);
117 print "FLASH WRITE ERROR AT %04x. Found %04x, wrote %04x." % (adr,rval,val);
119 def MSP430dumpbsl(self):
120 self.MSP430dumpmem(0xC00,0xfff);
121 def MSP430dumpallmem(self):
122 self.MSP430dumpmem(0x200,0xffff);
123 def MSP430dumpmem(self,begin,end):
126 print "%04x %04x" % (i, self.MSP430peek(i));