GoodFET.CCstop() is now GoodFET.stop()
[goodfet] / client / GoodFET.py
1 #!/usr/bin/env python
2 # GoodFET Client Library
3
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # This code is being rewritten and refactored.  You've been warned!
7
8 import sys, time, string, cStringIO, struct, glob, serial, os;
9
10
11 class GoodFET:
12     """GoodFET Client Library"""
13     
14     GLITCHAPP=0x71;
15     
16     def __init__(self, *args, **kargs):
17         self.data=[0];
18     def timeout(self):
19         print "timeout\n";
20     def serInit(self, port=None):
21         """Open the serial port"""
22         
23         if port is None and os.environ.get("GOODFET")!=None:
24             glob_list = glob.glob(os.environ.get("GOODFET"));
25             if len(glob_list) > 0:
26                 port = glob_list[0];
27         if port is None:
28             glob_list = glob.glob("/dev/tty.usbserial*");
29             if len(glob_list) > 0:
30                 port = glob_list[0];
31         if port is None:
32             glob_list = glob.glob("/dev/ttyUSB*");
33             if len(glob_list) > 0:
34                 port = glob_list[0];
35         
36         self.serialport = serial.Serial(
37             port,
38             #9600,
39             115200,
40             parity = serial.PARITY_NONE
41             )
42         
43         #Explicitly set RTS
44         self.serialport.setRTS(1);
45         #Drop DTR, which is !RST, low to begin the app.
46         self.serialport.setDTR(0);
47         self.serialport.flushInput()
48         self.serialport.flushOutput()
49         
50         #Read and handle the initial command.
51         #time.sleep(1);
52         self.readcmd(); #Read the first command.
53         if(self.verb!=0x7F):
54             print "Verb %02x is wrong.  Incorrect firmware?" % self.verb;
55         #print "Connected."
56     def getbuffer(self,size=0x1c00):
57         writecmd(0,0xC2,[size&0xFF,(size>>16)&0xFF]);
58         print "Got %02x%02x buffer size." % (self.data[1],self.data[0]);
59     def writecmd(self, app, verb, count=0, data=[]):
60         """Write a command and some data to the GoodFET."""
61         self.serialport.write(chr(app));
62         self.serialport.write(chr(verb));
63         
64         #if data!=None:
65         #    count=len(data); #Initial count ignored.
66         
67         #print "TX %02x %02x" % (app,verb);
68         
69         #little endian 16-bit length
70         self.serialport.write(chr(count&0xFF));
71         self.serialport.write(chr(count>>8));
72         
73         #print "count=%02x, len(data)=%04x" % (count,len(data));
74         
75         if count!=0:
76             for i in range(0,count):
77                 #print "Converting %02x at %i" % (data[i],i)
78                 data[i]=chr(data[i]);
79             outstr=''.join(data);
80             #outstr=data;
81             self.serialport.write(outstr);
82         if not self.besilent:
83             self.readcmd();
84         
85     besilent=0;
86     app=0;
87     verb=0;
88     count=0;
89     data="";
90
91     def readcmd(self):
92         """Read a reply from the GoodFET."""
93         while 1:
94             #print "Reading...";
95             self.app=ord(self.serialport.read(1));
96             #print "APP=%2x" % self.app;
97             self.verb=ord(self.serialport.read(1));
98             #print "VERB=%02x" % self.verb;
99             self.count=(
100                 ord(self.serialport.read(1))
101                 +(ord(self.serialport.read(1))<<8)
102                 );
103             
104             #Debugging string; print, but wait.
105             if self.app==0xFF and self.verb==0xFF:
106                 print "# DEBUG %s" % self.serialport.read(self.count);
107             else:
108                 self.data=self.serialport.read(self.count);
109                 return self.data;
110     #Glitching stuff.
111     def glitchAPP(self,app):
112         """Glitch into a device by its application."""
113         self.data=[app&0xff];
114         self.writecmd(self.GLITCHAPP,0x80,1,self.data);
115         #return ord(self.data[0]);
116     def glitchVERB(self,app,verb, data):
117         """Glitch during a transaction.."""
118         self.data=[app&0xff, verb&0xFF]+data;
119         self.writecmd(self.GLITCHAPP,0x81,len(self.data),self.data);
120         #return ord(self.data[0]);
121     def glitchVoltages(self,low=0x0880, high=0x0fff):
122         """Set glitching voltages. (0x0fff is max.)"""
123         self.data=[low&0xff, (low>>8)&0xff,
124                    high&0xff, (high>>8)&0xff];
125         self.writecmd(self.GLITCHAPP,0x90,4,self.data);
126         #return ord(self.data[0]);
127     def glitchRate(self,count=0x0800):
128         """Set glitching count period."""
129         self.data=[count&0xff, (count>>8)&0xff];
130         self.writecmd(self.GLITCHAPP,0x91,2,
131                       self.data);
132         #return ord(self.data[0]);
133     
134     
135     #Monitor stuff
136     def silent(self,s=0):
137         """Transmissions halted when 1."""
138         self.besilent=s;
139         print "besilent is %i" % self.besilent;
140         self.writecmd(0,0xB0,1,[s]);
141         
142     def out(self,byte):
143         """Write a byte to P5OUT."""
144         self.writecmd(0,0xA1,1,[byte]);
145     def dir(self,byte):
146         """Write a byte to P5DIR."""
147         self.writecmd(0,0xA0,1,[byte]);
148     def peekbyte(self,address):
149         """Read a byte of memory from the monitor."""
150         self.data=[address&0xff,address>>8];
151         self.writecmd(0,0x02,2,self.data);
152         #self.readcmd();
153         return ord(self.data[0]);
154     def peekword(self,address):
155         """Read a word of memory from the monitor."""
156         return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
157     def pokebyte(self,address,value):
158         """Set a byte of memory by the monitor."""
159         self.data=[address&0xff,address>>8,value];
160         self.writecmd(0,0x03,3,self.data);
161         return ord(self.data[0]);
162     def dumpmem(self,begin,end):
163         i=begin;
164         while i<end:
165             print "%04x %04x" % (i, self.peekword(i));
166             i+=2;
167     def monitor_ram_pattern(self):
168         """Overwrite all of RAM with 0xBEEF."""
169         self.writecmd(0,0x90,0,self.data);
170         return;
171     def monitor_ram_depth(self):
172         """Determine how many bytes of RAM are unused by looking for 0xBEEF.."""
173         self.writecmd(0,0x91,0,self.data);
174         return ord(self.data[0])+(ord(self.data[1])<<8);
175     
176     #Baud rates.
177     baudrates=[115200, 
178                9600,
179                19200,
180                38400,
181                57600,
182                115200];
183     def setBaud(self,baud):
184         """Change the baud rate.  TODO fix this."""
185         rates=self.baudrates;
186         self.data=[baud];
187         print "Changing FET baud."
188         self.serialport.write(chr(0x00));
189         self.serialport.write(chr(0x80));
190         self.serialport.write(chr(1));
191         self.serialport.write(chr(baud));
192         
193         print "Changed host baud."
194         self.serialport.setBaudrate(rates[baud]);
195         time.sleep(1);
196         self.serialport.flushInput()
197         self.serialport.flushOutput()
198         
199         print "Baud is now %i." % rates[baud];
200         return;
201     def readbyte(self):
202         return ord(self.serialport.read(1));
203     def findbaud(self):
204         for r in self.baudrates:
205             print "\nTrying %i" % r;
206             self.serialport.setBaudrate(r);
207             #time.sleep(1);
208             self.serialport.flushInput()
209             self.serialport.flushOutput()
210             
211             for i in range(1,10):
212                 self.readbyte();
213             
214             print "Read %02x %02x %02x %02x" % (
215                 self.readbyte(),self.readbyte(),self.readbyte(),self.readbyte());
216     def monitortest(self):
217         """Self-test several functions through the monitor."""
218         print "Performing monitor self-test.";
219         
220         if self.peekword(0x0c00)!=0x0c04 and self.peekword(0x0c00)!=0x0c06:
221             print "ERROR Fetched wrong value from 0x0c04.";
222         self.pokebyte(0x0021,0); #Drop LED
223         if self.peekbyte(0x0021)!=0:
224             print "ERROR, P1OUT not cleared.";
225         self.pokebyte(0x0021,1); #Light LED
226         
227         print "Self-test complete.";
228     
229     
230
231     def I2Csetup(self):
232         """Move the FET into the I2C application."""
233         self.writecmd(0x02,0x10,0,self.data); #SPI/SETUP
234     def I2Cstart(self):
235         """Start an I2C transaction."""
236         self.writecmd(0x02,0x20,0,self.data); #SPI/SETUP
237     def I2Cstop(self):
238         """Stop an I2C transaction."""
239         self.writecmd(0x02,0x21,0,self.data); #SPI/SETUP
240     def I2Cread(self,len=1):
241         """Read len bytes by I2C."""
242         self.writecmd(0x02,0x00,1,[len]); #SPI/SETUP
243         return self.data;
244     def I2Cwrite(self,bytes):
245         """Write bytes by I2C."""
246         self.writecmd(0x02,0x01,len(bytes),bytes); #SPI/SETUP
247         return ord(self.data[0]);