Makefile for linking client scripts.
[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             parity = serial.PARITY_NONE
25             )
26         #Drop DTR, which is !RST, low to begin the app.
27         self.serialport.setDTR(0);
28         self.serialport.flushInput()
29         self.serialport.flushOutput()
30         
31         #Read and handle the initial command.
32         time.sleep(1);
33         self.readcmd(); #Read the first command.
34         if(self.verb!=0x7F):
35             print "Verb is wrong.  Incorrect firmware?";
36         
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     
73     def monitortest(self):
74         """Self-test several functions through the monitor."""
75         print "Performing monitor self-test.";
76         
77         if self.peekword(0x0c00)!=0x0c04:
78             print "ERROR Fetched wrong value from 0x0c04.";
79         self.pokebyte(0x0021,0); #Drop LED
80         if self.peekbyte(0x0021)!=0:
81             print "ERROR, P1OUT not cleared.";
82         self.pokebyte(0x0021,1); #Light LED
83         
84         print "Self-test complete.";
85     
86     def spisetup(self):
87         """Moved the FET into the SPI application."""
88         self.writecmd(1,0x10,0,self.data); #SPI/SETUP
89         #self.readcmd();
90     def spitrans8(self,byte):
91         """Read and write 8 bits by SPI."""
92         self.data=[byte];
93         self.writecmd(1,0,1,self.data);    #SPI exchange
94         #self.readcmd();
95         
96         if self.app!=1 or self.verb!=0:
97             print "Error in SPI transaction; app=%02x, verb=%02x" % (self.app, self.verb);
98         return ord(self.data[0]);
99     def MSP430setup(self):
100         """Move the FET into the MSP430 JTAG application."""
101         print "Initializing MSP430.";
102         self.writecmd(0x11,0x10,0,self.data);
103     def MSP430peek(self,adr):
104         """Read the contents of memory at an address."""
105         self.data=[adr&0xff, (adr&0xff00)>>8];
106         self.writecmd(0x11,0x02,2,self.data);
107         return ord(self.data[0])+(ord(self.data[1])<<8);
108     def MSP430poke(self,adr,val):
109         """Read the contents of memory at an address."""
110         self.data=[adr&0xff, (adr&0xff00)>>8, val&0xff, (val&0xff00)>>8];
111         self.writecmd(0x11,0x03,4,self.data);
112         return;# ord(self.data[0])+(ord(self.data[1])<<8);
113     
114     def MSP430start(self):
115         """Start debugging."""
116         self.writecmd(0x11,0x20,0,self.data);
117     def MSP430stop(self):
118         """Stop debugging."""
119         self.writecmd(0x11,0x21,0,self.data);
120     def MSP430haltcpu(self):
121         """Halt the CPU."""
122         self.writecmd(0x11,0xA0,0,self.data);
123     def MSP430releasecpu(self):
124         """Resume the CPU."""
125         self.writecmd(0x11,0xA1,0,self.data);
126
127     def MSP430shiftir8(self,ins):
128         """Shift the 8-bit Instruction Register."""
129         data=[ins];
130         self.writecmd(0x11,0x80,1,data);
131         return ord(self.data[0]);
132     def MSP430shiftdr16(self,dat):
133         """Shift the 16-bit Data Register."""
134         data=[dat&0xFF,(dat&0xFF00)>>8];
135         self.writecmd(0x11,0x81,2,data);
136         return ord(self.data[0])#+(ord(self.data[1])<<8);
137     def MSP430setinstrfetch(self):
138         """Set the instruction fetch mode."""
139         self.writecmd(0x11,0xC1,0,self.data);
140         return self.data[0];
141     def MSP430test(self):
142         """Test MSP430 JTAG.  Requires that a chip be attached."""
143         #self.MSP430setup();
144         #self.MSP430start();
145         self.MSP430haltcpu();
146         
147         ident=self.MSP430peek(0x0ff0);
148         print "Target identifies as %04x." % ident;
149         if ident==0xffff:
150             print "Is anything connected?";
151         print "Testing RAM.";
152         temp=self.MSP430peek(0x0200);
153         self.MSP430poke(0x0200,0xdead);
154         if(self.MSP430peek(0x0200)!=0xdead):
155             print "Poke of 0x0200 did not set to 0xDEAD properly.";
156             return;
157         self.MSP430poke(0x0200,temp); #restore old value.
158         self.MSP430releasecpu();
159         
160     def MSP430dumpbsl(self):
161         self.MSP430dumpmem(0xC00,0xfff);
162     def MSP430dumpallmem(self):
163         self.MSP430dumpmem(0x200,0xffff);
164     def MSP430dumpmem(self,begin,end):
165         i=begin;
166         while i<end:
167             print "%04x %04x" % (i, self.MSP430peek(i));
168             i+=2;
169