74c16559899095cc0f21b74ecc1ecef4b9af674e
[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         time.sleep(1);
33         client.readcmd();
34         if(self.verb!=0x7F):
35             print "Verb is wrong.  Incorrect firmware?";
36         
37         
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));
44         if count!=0:
45             for d in data:
46                 self.serialport.write(chr(d));
47         #self.readcmd();  #Uncomment this later, to ensure a response.
48     def readcmd(self):
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));
53         if self.count>0:
54             self.data=self.serialport.read(self.count);
55         #print "%02x %02x %02x" % (self.app, self.verb, self.count);
56     
57     #Monitor stuff
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);
62         self.readcmd();
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         self.readcmd();
72         #print "POKE returned %02x" % ord(self.data[0]);
73         return ord(self.data[0]);
74     
75     def monitortest(self):
76         """Self-test several functions through the monitor."""
77         print "Performing self-test.";
78         
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
85         
86         print "Self-test complete.";
87     
88     def spisetup(self):
89         """Moved the FET into the SPI application."""
90         print "Initializing SPI.";
91         self.writecmd(1,0x10,0,self.data); #SPI/SETUP
92         self.readcmd();
93     def spitrans8(self,byte):
94         """Read and write 8 bits by SPI."""
95         self.data=[byte];
96         self.writecmd(1,0,1,self.data);    #SPI exchange
97         self.readcmd();
98         
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]);
102
103 client=Client();
104 client.serInit("/dev/ttyUSB0")
105
106 client.monitortest();
107
108 client.spisetup();
109 while 1:
110     print "%02x" % client.spitrans8(5);
111     time.sleep(0.1);