2 # GoodFET Client Library
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
6 # This code is ugly as sin, for bootstrapping the firmware only.
7 # Rewrite cleanly as soon as is convenient.
9 import sys, time, string, cStringIO, struct
10 sys.path.append("/usr/lib/tinyos")
15 def __init__(self, *args, **kargs):
20 def serInit(self, port):
21 """Open the serial port"""
22 self.serialport = serial.Serial(
25 parity = serial.PARITY_NONE
27 #Drop DTR, which is !RST, low to begin the app.
28 self.serialport.setDTR(0);
29 self.serialport.flushInput()
30 self.serialport.flushOutput()
32 #Read and handle the initial command.
34 self.readcmd(); #Read the first command.
36 print "Verb is wrong. Incorrect firmware?";
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));
47 self.serialport.write(chr(d));
48 self.readcmd(); #Uncomment this later, to ensure a response.
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));
55 self.data=self.serialport.read(self.count);
56 #print "READ %02x %02x %02x " % (self.app, self.verb, self.count);
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);
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]);
74 def monitortest(self):
75 """Self-test several functions through the monitor."""
76 print "Performing monitor self-test.";
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
85 print "Self-test complete.";
88 """Moved the FET into the SPI application."""
89 print "Initializing SPI.";
90 self.writecmd(1,0x10,0,self.data); #SPI/SETUP
92 def spitrans8(self,byte):
93 """Read and write 8 bits by SPI."""
95 self.writecmd(1,0,1,self.data); #SPI exchange
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);
116 def MSP430start(self):
117 """Start debugging."""
118 self.writecmd(0x11,0x20,0,self.data);
119 def MSP430haltcpu(self):
121 self.writecmd(0x11,0xA0,0,self.data);
122 def MSP430releasecpu(self):
123 """Resume the CPU."""
124 self.writecmd(0x11,0xA1,0,self.data);
126 def MSP430shiftir8(self,ins):
127 """Shift the 8-bit Instruction Register."""
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);
140 def MSP430test(self):
141 """Test MSP430 JTAG. Requires that a chip be attached."""
144 self.MSP430haltcpu();
146 ident=self.MSP430peek(0x0ff0);
147 print "Target identifies as %04x." % ident;
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.";
156 self.MSP430poke(0x0200,temp); #restore old value.
157 self.MSP430releasecpu();
159 def MSP430dumpbsl(self):
162 print "%04x %04x" % (i, self.MSP430peek(i));