MSP430 JTAG works.
[goodfet] / client / GoodFET.py
1 #!/usr/bin/env python
2 # GoodFET Client Library
3
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # This code is ugly as sin, for bootstrapping the firmware only.
7 # Rewrite cleanly as soon as is convenient.
8
9 import sys, time, string, cStringIO, struct
10 sys.path.append("/usr/lib/tinyos")
11 import serial
12
13
14 class GoodFET:
15     def __init__(self, *args, **kargs):
16         print "inited\n";
17         self.data=[0];
18     def timeout(self):
19         print "timout\n";
20     def serInit(self, port):
21         """Open the serial port"""
22         self.serialport = serial.Serial(
23             port,
24             9600,
25             parity = serial.PARITY_NONE
26             )
27         #Drop DTR, which is !RST, low to begin the app.
28         self.serialport.setDTR(0);
29         self.serialport.flushInput()
30         self.serialport.flushOutput()
31         
32         #Read and handle the initial command.
33         time.sleep(1);
34         self.readcmd(); #Read the first command.
35         if(self.verb!=0x7F):
36             print "Verb is wrong.  Incorrect firmware?";
37         
38         
39     def writecmd(self, app, verb, count, data):
40         """Write a command and some data to the GoodFET."""
41         self.serialport.write(chr(app));
42         self.serialport.write(chr(verb));
43         self.serialport.write(chr(count));
44         #print "count=%02x, len(data)=%04x" % (count,len(data));
45         if count!=0:
46             for d in data:
47                 self.serialport.write(chr(d));
48         self.readcmd();  #Uncomment this later, to ensure a response.
49     def readcmd(self):
50         """Read a reply from the GoodFET."""
51         self.app=ord(self.serialport.read(1));
52         self.verb=ord(self.serialport.read(1));
53         self.count=ord(self.serialport.read(1));
54         if self.count>0:
55             self.data=self.serialport.read(self.count);
56         #print "READ %02x %02x %02x " % (self.app, self.verb, self.count);
57         
58     #Monitor stuff
59     def peekbyte(self,address):
60         """Read a byte of memory from the monitor."""
61         self.data=[address&0xff,address>>8];
62         self.writecmd(0,0x02,2,self.data);
63         #self.readcmd();
64         return ord(self.data[0]);
65     def peekword(self,address):
66         """Read a word of memory from the monitor."""
67         return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
68     def pokebyte(self,address,value):
69         """Set a byte of memory by the monitor."""
70         self.data=[address&0xff,address>>8,value];
71         self.writecmd(0,0x03,3,self.data);
72         return ord(self.data[0]);
73     
74     def monitortest(self):
75         """Self-test several functions through the monitor."""
76         print "Performing monitor self-test.";
77         
78         if self.peekword(0x0c00)!=0x0c04:
79             print "ERROR Fetched wrong value from 0x0c04.";
80         self.pokebyte(0x0021,0); #Drop LED
81         if self.peekbyte(0x0021)!=0:
82             print "ERROR, P1OUT not cleared.";
83         self.pokebyte(0x0021,1); #Light LED
84         
85         print "Self-test complete.";
86     
87     def spisetup(self):
88         """Moved the FET into the SPI application."""
89         print "Initializing SPI.";
90         self.writecmd(1,0x10,0,self.data); #SPI/SETUP
91         #self.readcmd();
92     def spitrans8(self,byte):
93         """Read and write 8 bits by SPI."""
94         self.data=[byte];
95         self.writecmd(1,0,1,self.data);    #SPI exchange
96         #self.readcmd();
97         
98         if self.app!=1 or self.verb!=0:
99             print "Error in SPI transaction; app=%02x, verb=%02x" % (self.app, self.verb);
100         return ord(self.data[0]);
101     def MSP430setup(self):
102         """Move the FET into the MSP430 JTAG application."""
103         print "Initializing MSP430.";
104         self.writecmd(0x11,0x10,0,self.data);
105     def MSP430peek(self,adr):
106         """Read the contents of memory at an address."""
107         self.data=[adr&0xff, (adr&0xff00)>>8];
108         self.writecmd(0x11,0x02,2,self.data);
109         return ord(self.data[0])+(ord(self.data[1])<<8);
110     def MSP430poke(self,adr,val):
111         """Read the contents of memory at an address."""
112         self.data=[adr&0xff, (adr&0xff00)>>8, val&0xff, (val&0xff00)>>8];
113         self.writecmd(0x11,0x03,4,self.data);
114         return;# ord(self.data[0])+(ord(self.data[1])<<8);
115     
116     def MSP430start(self):
117         """Start debugging."""
118         self.writecmd(0x11,0x20,0,self.data);
119     def MSP430haltcpu(self):
120         """Halt the CPU."""
121         self.writecmd(0x11,0xA0,0,self.data);
122     def MSP430releasecpu(self):
123         """Resume the CPU."""
124         self.writecmd(0x11,0xA1,0,self.data);
125
126     def MSP430shiftir8(self,ins):
127         """Shift the 8-bit Instruction Register."""
128         data=[ins];
129         self.writecmd(0x11,0x80,1,data);
130         return ord(self.data[0]);
131     def MSP430shiftdr16(self,dat):
132         """Shift the 16-bit Data Register."""
133         data=[dat&0xFF,(dat&0xFF00)>>8];
134         self.writecmd(0x11,0x81,2,data);
135         return ord(self.data[0])#+(ord(self.data[1])<<8);
136     def MSP430setinstrfetch(self):
137         """Set the instruction fetch mode."""
138         self.writecmd(0x11,0xC1,0,self.data);
139         return self.data[0];
140     def MSP430test(self):
141         """Test MSP430 JTAG.  Requires that a chip be attached."""
142         self.MSP430setup();
143         self.MSP430start();
144         self.MSP430haltcpu();
145         
146         ident=self.MSP430peek(0x0ff0);
147         print "Target identifies as %04x." % ident;
148         if ident==0xffff:
149             print "Is anything connected?";
150         print "Testing RAM.";
151         temp=self.MSP430peek(0x0200);
152         self.MSP430poke(0x0200,0xdead);
153         if(self.MSP430peek(0x0200)!=0xdead):
154             print "Poke of 0x0200 did not set to 0xDEAD properly.";
155             exit;
156         self.MSP430poke(0x0200,temp); #restore old value.
157         self.MSP430releasecpu();
158         
159     def MSP430dumpbsl(self):
160         i=0xC00;
161         while i<0x1000:
162             print "%04x %04x" % (i, self.MSP430peek(i));
163             i+=2;
164