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.
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);
72 #print "POKE returned %02x" % ord(self.data[0]);
73 return ord(self.data[0]);
75 def monitortest(self):
76 """Self-test several functions through the monitor."""
77 print "Performing self-test.";
79 if self.peekword(0x0c00)!=0x0c04:
80 print "ERROR Fetched wrong value from 0x0c04.";
81 self.pokebyte(0x0021,0); #Drop LED
82 if self.peekbyte(0x0021)!=0:
83 print "ERROR, P1OUT not cleared.";
84 self.pokebyte(0x0021,1); #Light LED
86 print "Self-test complete.";
89 """Moved the FET into the SPI application."""
90 print "Initializing SPI.";
91 self.writecmd(1,0x10,0,self.data); #SPI/SETUP
93 def spitrans8(self,byte):
94 """Read and write 8 bits by SPI."""
96 self.writecmd(1,0,1,self.data); #SPI exchange
99 if self.app!=1 or self.verb!=0:
100 print "Error in SPI transaction; app=%02x, verb=%02x" % (self.app, self.verb);
101 return ord(self.data[0]);
104 client.serInit("/dev/ttyUSB0")
106 client.monitortest();
110 print "%02x" % client.spitrans8(5);