2 # GoodFET Client Application
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):
19 def serInit(self, port):
20 """Open the serial port"""
21 self.serialport = serial.Serial(
24 parity = serial.PARITY_NONE
26 #Drop DTR, which is !RST, low to begin the app.
27 self.serialport.setDTR(0);
28 self.serialport.flushInput()
29 self.serialport.flushOutput()
31 #Read and handle the initial command.
33 client.readcmd(); #Read the first command.
35 print "Verb is wrong. Incorrect firmware?";
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));
46 self.serialport.write(chr(d));
47 self.readcmd(); #Uncomment this later, to ensure a response.
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));
54 self.data=self.serialport.read(self.count);
55 #print "%02x %02x %02x" % (self.app, self.verb, self.count);
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);
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]);
73 def monitortest(self):
74 """Self-test several functions through the monitor."""
75 print "Performing self-test.";
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
84 print "Self-test complete.";
87 """Moved the FET into the SPI application."""
88 print "Initializing SPI.";
89 self.writecmd(1,0x10,0,self.data); #SPI/SETUP
91 def spitrans8(self,byte):
92 """Read and write 8 bits by SPI."""
94 self.writecmd(1,0,1,self.data); #SPI exchange
97 if self.app!=1 or self.verb!=0:
98 print "Error in SPI transaction; app=%02x, verb=%02x" % (self.app, self.verb);
99 return ord(self.data[0]);
102 client.serInit("/dev/ttyUSB0")
104 client.monitortest();
108 print "%02x" % client.spitrans8(5);