Client now runs at 115200 baud.
[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         self.data=[0];
17     def timeout(self):
18         print "timout\n";
19     def serInit(self, port):
20         """Open the serial port"""
21         self.serialport = serial.Serial(
22             port,
23             #9600,
24             115200,
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     def writecmd(self, app, verb, count, data):
39         """Write a command and some data to the GoodFET."""
40         self.serialport.write(chr(app));
41         self.serialport.write(chr(verb));
42         self.serialport.write(chr(count));
43         #print "count=%02x, len(data)=%04x" % (count,len(data));
44         if count!=0:
45             for d in data:
46                 self.serialport.write(chr(d));
47         self.readcmd();  #Uncomment this later, to ensure a response.
48     def readcmd(self):
49         """Read a reply from the GoodFET."""
50         self.app=ord(self.serialport.read(1));
51         self.verb=ord(self.serialport.read(1));
52         self.count=ord(self.serialport.read(1));
53         if self.count>0:
54             self.data=self.serialport.read(self.count);
55         #print "READ %02x %02x %02x " % (self.app, self.verb, self.count);
56         
57     #Monitor stuff
58     def peekbyte(self,address):
59         """Read a byte of memory from the monitor."""
60         self.data=[address&0xff,address>>8];
61         self.writecmd(0,0x02,2,self.data);
62         #self.readcmd();
63         return ord(self.data[0]);
64     def peekword(self,address):
65         """Read a word of memory from the monitor."""
66         return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
67     def pokebyte(self,address,value):
68         """Set a byte of memory by the monitor."""
69         self.data=[address&0xff,address>>8,value];
70         self.writecmd(0,0x03,3,self.data);
71         return ord(self.data[0]);
72     def setBaud(self,baud):
73         rates=[9600, 9600, 19200, 38400];
74         self.data=[baud];
75         print "Changing FET baud."
76         self.serialport.write(chr(0x00));
77         self.serialport.write(chr(0x80));
78         self.serialport.write(chr(1));
79         self.serialport.write(chr(baud));
80         
81         print "Changed host baud."
82         self.serialport.setBaudrate(rates[baud]);
83         time.sleep(1);
84         self.serialport.flushInput()
85         self.serialport.flushOutput()
86         
87         print "Baud is now %i." % rates[baud];
88         return;
89     def monitortest(self):
90         """Self-test several functions through the monitor."""
91         print "Performing monitor self-test.";
92         
93         if self.peekword(0x0c00)!=0x0c04:
94             print "ERROR Fetched wrong value from 0x0c04.";
95         self.pokebyte(0x0021,0); #Drop LED
96         if self.peekbyte(0x0021)!=0:
97             print "ERROR, P1OUT not cleared.";
98         self.pokebyte(0x0021,1); #Light LED
99         
100         print "Self-test complete.";
101     
102     def spisetup(self):
103         """Moved the FET into the SPI application."""
104         self.writecmd(1,0x10,0,self.data); #SPI/SETUP
105         #self.readcmd();
106     def spitrans8(self,byte):
107         """Read and write 8 bits by SPI."""
108         self.data=[byte];
109         self.writecmd(1,0,1,self.data);    #SPI exchange
110         #self.readcmd();
111         
112         if self.app!=1 or self.verb!=0:
113             print "Error in SPI transaction; app=%02x, verb=%02x" % (self.app, self.verb);
114         return ord(self.data[0]);
115     def MSP430setup(self):
116         """Move the FET into the MSP430 JTAG application."""
117         print "Initializing MSP430.";
118         self.writecmd(0x11,0x10,0,self.data);
119     def MSP430peek(self,adr):
120         """Read the contents of memory at an address."""
121         self.data=[adr&0xff, (adr&0xff00)>>8];
122         self.writecmd(0x11,0x02,2,self.data);
123         return ord(self.data[0])+(ord(self.data[1])<<8);
124     def MSP430poke(self,adr,val):
125         """Read the contents of memory at an address."""
126         self.data=[adr&0xff, (adr&0xff00)>>8, val&0xff, (val&0xff00)>>8];
127         self.writecmd(0x11,0x03,4,self.data);
128         return;# ord(self.data[0])+(ord(self.data[1])<<8);
129     
130     def MSP430start(self):
131         """Start debugging."""
132         self.writecmd(0x11,0x20,0,self.data);
133         ident=self.MSP430ident();
134         print "Target identifies as %04x." % ident;
135         
136     def MSP430stop(self):
137         """Stop debugging."""
138         self.writecmd(0x11,0x21,0,self.data);
139     def MSP430haltcpu(self):
140         """Halt the CPU."""
141         self.writecmd(0x11,0xA0,0,self.data);
142     def MSP430releasecpu(self):
143         """Resume the CPU."""
144         self.writecmd(0x11,0xA1,0,self.data);
145
146     def MSP430shiftir8(self,ins):
147         """Shift the 8-bit Instruction Register."""
148         data=[ins];
149         self.writecmd(0x11,0x80,1,data);
150         return ord(self.data[0]);
151     def MSP430shiftdr16(self,dat):
152         """Shift the 16-bit Data Register."""
153         data=[dat&0xFF,(dat&0xFF00)>>8];
154         self.writecmd(0x11,0x81,2,data);
155         return ord(self.data[0])#+(ord(self.data[1])<<8);
156     def MSP430setinstrfetch(self):
157         """Set the instruction fetch mode."""
158         self.writecmd(0x11,0xC1,0,self.data);
159         return self.data[0];
160     def MSP430ident(self):
161         """Grab self-identification word from 0x0FF0 as big endian."""
162         i=self.MSP430peek(0x0ff0);
163         return ((i&0xFF00)>>8)+((i&0xFF)<<8)
164     def MSP430test(self):
165         """Test MSP430 JTAG.  Requires that a chip be attached."""
166         if self.MSP430ident()==0xffff:
167             print "Is anything connected?";
168         print "Testing RAM.";
169         temp=self.MSP430peek(0x0200);
170         self.MSP430poke(0x0200,0xdead);
171         if(self.MSP430peek(0x0200)!=0xdead):
172             print "Poke of 0x0200 did not set to 0xDEAD properly.";
173             return;
174         self.MSP430poke(0x0200,temp); #restore old value.
175     def MSP430flashtest(self):
176         self.MSP430masserase();
177         i=0x2500;
178         while(i<0xFFFF):
179             if(self.MSP430peek(i)!=0xFFFF):
180                 print "ERROR: Unerased flash at %04x."%i;
181             self.MSP430writeflash(i,0xDEAD);
182             i+=2;
183     def MSP430masserase(self):
184         """Erase MSP430 flash memory."""
185         self.writecmd(0x11,0xE3,0,None);
186     def MSP430writeflash(self,adr,val):
187         """Write a word of flash memory."""
188         if(self.MSP430peek(adr)!=0xFFFF):
189             print "FLASH ERROR: %04x not clear." % adr;
190         data=[adr&0xFF,(adr&0xFF00)>>8,val&0xFF,(val&0xFF00)>>8];
191         self.writecmd(0x11,0xE1,4,data);
192         rval=ord(self.data[0])+(ord(self.data[1])<<8);
193         if(val!=rval):
194             print "FLASH WRITE ERROR AT %04x.  Found %04x, wrote %04x." % (adr,rval,val);
195             
196     def MSP430dumpbsl(self):
197         self.MSP430dumpmem(0xC00,0xfff);
198     def MSP430dumpallmem(self):
199         self.MSP430dumpmem(0x200,0xffff);
200     def MSP430dumpmem(self,begin,end):
201         i=begin;
202         while i<end:
203             print "%04x %04x" % (i, self.MSP430peek(i));
204             i+=2;
205