Ugly Python client.
[goodfet] / client / goodfet
1 #!/usr/bin/env python
2 # GoodFET Client Application
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 Client:
15     def __init__(self, *args, **kargs):
16         print "inited\n";
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         client.readcmd();
33         client.handlecmd();
34         
35         time.sleep(1);
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));
41         for d in data:
42             self.serialport.write(chr(d));
43     def readcmd(self):
44         self.app=ord(self.serialport.read(1));
45         self.verb=ord(self.serialport.read(1));
46         self.count=ord(self.serialport.read(1));
47         if self.count>0:
48             self.data=self.serialport.read(self.count);
49         print "%02x %02x %02x" % (self.app, self.verb, self.count);
50     def handlemonitor(self):
51         if self.verb==0x7E:
52             print "Monitor: NOK";
53         if self.verb==0x7F:
54             print "Monitor: OK";
55     def handlecmd(self):
56         if self.app==0:
57             #print "Monitor command."
58             self.handlemonitor();
59         else:
60             print "Unknown application %02x." % self.app
61     
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);
66         self.readcmd();
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);
75         self.readcmd();
76         print "POKE returned %02x" % ord(self.data[0]);
77         return ord(self.data[0]);
78     
79     def monitortest(self):
80         """Self-test several functions through the monitor."""
81         print "Performing self-test.";
82         
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.";
88         
89         print "Self-test complete.";
90
91 client=Client();
92 client.serInit("/dev/ttyUSB0")
93
94
95
96 client.monitortest();
97