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.
36 def writecmd(self, app, verb, count, data):
37 self.serialport.write(chr(app));
38 self.serialport.write(chr(verb));
39 self.serialport.write(chr(count));
40 print "count=%02x, len(data)=%04x" % (count,len(data));
42 self.serialport.write(chr(d));
44 self.app=ord(self.serialport.read(1));
45 self.verb=ord(self.serialport.read(1));
46 self.count=ord(self.serialport.read(1));
48 self.data=self.serialport.read(self.count);
49 print "%02x %02x %02x" % (self.app, self.verb, self.count);
50 def handlemonitor(self):
57 #print "Monitor command."
60 print "Unknown application %02x." % self.app
62 def peekbyte(self,address):
63 """Read a byte of memory from the monitor."""
64 self.data=[address&0xff,address>>8];
65 self.writecmd(0,0x02,2,self.data);
67 return ord(self.data[0]);
68 def peekword(self,address):
69 """Read a word of memory from the monitor."""
70 return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
71 def pokebyte(self,address,value):
72 """Set a byte of memory by the monitor."""
73 self.data=[address&0xff,address>>8,value];
74 self.writecmd(0,0x03,3,self.data);
76 print "POKE returned %02x" % ord(self.data[0]);
77 return ord(self.data[0]);
79 def monitortest(self):
80 """Self-test several functions through the monitor."""
81 print "Performing self-test.";
83 if self.peekword(0x0c00)!=0x0c04:
84 print "ERROR Fetched wrong value from 0x0c04.";
85 self.pokebyte(0x0021,0); #Drop LED
86 if self.peekbyte(0x0021)!=0:
87 print "ERROR, P1OUT not cleared.";
89 print "Self-test complete.";
92 client.serInit("/dev/ttyUSB0")