78171b7ec34b4342d7ecb574f0823a2338d47e9a
[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(); #Read the first command.
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         return ord(self.data[0]);
72     
73     def monitortest(self):
74         """Self-test several functions through the monitor."""
75         print "Performing self-test.";
76         
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
83         
84         print "Self-test complete.";
85     
86     def spisetup(self):
87         """Moved the FET into the SPI application."""
88         print "Initializing SPI.";
89         self.writecmd(1,0x10,0,self.data); #SPI/SETUP
90         #self.readcmd();
91     def spitrans8(self,byte):
92         """Read and write 8 bits by SPI."""
93         self.data=[byte];
94         self.writecmd(1,0,1,self.data);    #SPI exchange
95         #self.readcmd();
96         
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]);
100
101 client=Client();
102 client.serInit("/dev/ttyUSB0")
103
104 client.monitortest();
105
106 client.spisetup();
107 while 1:
108     print "%02x" % client.spitrans8(5);
109     time.sleep(0.1);