Monitor now does CALL and EXEC.
[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         #This might cause problems, but it makes failure graceful.
44         #self.serialport._timeout = 5;
45         
46         #Explicitly set RTS and DTR to halt board.
47         self.serialport.setRTS(1);
48         self.serialport.setDTR(1);
49         #Drop DTR, which is !RST, low to begin the app.
50         self.serialport.setDTR(0);
51         self.serialport.flushInput()
52         self.serialport.flushOutput()
53         
54         #Read and handle the initial command.
55         #time.sleep(1);
56         self.readcmd(); #Read the first command.
57         if(self.verb!=0x7F):
58             print "Verb %02x is wrong.  Incorrect firmware?" % self.verb;
59         #print "Connected."
60     def getbuffer(self,size=0x1c00):
61         writecmd(0,0xC2,[size&0xFF,(size>>16)&0xFF]);
62         print "Got %02x%02x buffer size." % (self.data[1],self.data[0]);
63     def writecmd(self, app, verb, count=0, data=[]):
64         """Write a command and some data to the GoodFET."""
65         self.serialport.write(chr(app));
66         self.serialport.write(chr(verb));
67         
68         #if data!=None:
69         #    count=len(data); #Initial count ignored.
70         
71         #print "TX %02x %02x" % (app,verb);
72         
73         #little endian 16-bit length
74         self.serialport.write(chr(count&0xFF));
75         self.serialport.write(chr(count>>8));
76         
77         #print "count=%02x, len(data)=%04x" % (count,len(data));
78         
79         if count!=0:
80             for i in range(0,count):
81                 #print "Converting %02x at %i" % (data[i],i)
82                 data[i]=chr(data[i]);
83             outstr=''.join(data);
84             #outstr=data;
85             self.serialport.write(outstr);
86         if not self.besilent:
87             self.readcmd();
88         
89     besilent=0;
90     app=0;
91     verb=0;
92     count=0;
93     data="";
94
95     def readcmd(self):
96         """Read a reply from the GoodFET."""
97         while 1:
98             #print "Reading...";
99             self.app=ord(self.serialport.read(1));
100             #print "APP=%2x" % self.app;
101             self.verb=ord(self.serialport.read(1));
102             #print "VERB=%02x" % self.verb;
103             self.count=(
104                 ord(self.serialport.read(1))
105                 +(ord(self.serialport.read(1))<<8)
106                 );
107             
108             #Debugging string; print, but wait.
109             if self.app==0xFF and self.verb==0xFF:
110                 print "# DEBUG %s" % self.serialport.read(self.count);
111             else:
112                 self.data=self.serialport.read(self.count);
113                 return self.data;
114     #Glitching stuff.
115     def glitchAPP(self,app):
116         """Glitch into a device by its application."""
117         self.data=[app&0xff];
118         self.writecmd(self.GLITCHAPP,0x80,1,self.data);
119         #return ord(self.data[0]);
120     def glitchVERB(self,app,verb, data):
121         """Glitch during a transaction.."""
122         self.data=[app&0xff, verb&0xFF]+data;
123         self.writecmd(self.GLITCHAPP,0x81,len(self.data),self.data);
124         #return ord(self.data[0]);
125     def glitchVoltages(self,low=0x0880, high=0x0fff):
126         """Set glitching voltages. (0x0fff is max.)"""
127         self.data=[low&0xff, (low>>8)&0xff,
128                    high&0xff, (high>>8)&0xff];
129         self.writecmd(self.GLITCHAPP,0x90,4,self.data);
130         #return ord(self.data[0]);
131     def glitchRate(self,count=0x0800):
132         """Set glitching count period."""
133         self.data=[count&0xff, (count>>8)&0xff];
134         self.writecmd(self.GLITCHAPP,0x91,2,
135                       self.data);
136         #return ord(self.data[0]);
137     
138     
139     #Monitor stuff
140     def silent(self,s=0):
141         """Transmissions halted when 1."""
142         self.besilent=s;
143         print "besilent is %i" % self.besilent;
144         self.writecmd(0,0xB0,1,[s]);
145         
146     def out(self,byte):
147         """Write a byte to P5OUT."""
148         self.writecmd(0,0xA1,1,[byte]);
149     def dir(self,byte):
150         """Write a byte to P5DIR."""
151         self.writecmd(0,0xA0,1,[byte]);
152     def call(self,adr):
153         """Call to an address."""
154         self.writecmd(0,0x30,2,
155                       [adr&0xFF,(adr>>8)&0xFF]);
156     def peekbyte(self,address):
157         """Read a byte of memory from the monitor."""
158         self.data=[address&0xff,address>>8];
159         self.writecmd(0,0x02,2,self.data);
160         #self.readcmd();
161         return ord(self.data[0]);
162     def peekword(self,address):
163         """Read a word of memory from the monitor."""
164         return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
165     def pokebyte(self,address,value):
166         """Set a byte of memory by the monitor."""
167         self.data=[address&0xff,address>>8,value];
168         self.writecmd(0,0x03,3,self.data);
169         return ord(self.data[0]);
170     def dumpmem(self,begin,end):
171         i=begin;
172         while i<end:
173             print "%04x %04x" % (i, self.peekword(i));
174             i+=2;
175     def monitor_ram_pattern(self):
176         """Overwrite all of RAM with 0xBEEF."""
177         self.writecmd(0,0x90,0,self.data);
178         return;
179     def monitor_ram_depth(self):
180         """Determine how many bytes of RAM are unused by looking for 0xBEEF.."""
181         self.writecmd(0,0x91,0,self.data);
182         return ord(self.data[0])+(ord(self.data[1])<<8);
183     
184     #Baud rates.
185     baudrates=[115200, 
186                9600,
187                19200,
188                38400,
189                57600,
190                115200];
191     def setBaud(self,baud):
192         """Change the baud rate.  TODO fix this."""
193         rates=self.baudrates;
194         self.data=[baud];
195         print "Changing FET baud."
196         self.serialport.write(chr(0x00));
197         self.serialport.write(chr(0x80));
198         self.serialport.write(chr(1));
199         self.serialport.write(chr(baud));
200         
201         print "Changed host baud."
202         self.serialport.setBaudrate(rates[baud]);
203         time.sleep(1);
204         self.serialport.flushInput()
205         self.serialport.flushOutput()
206         
207         print "Baud is now %i." % rates[baud];
208         return;
209     def readbyte(self):
210         return ord(self.serialport.read(1));
211     def findbaud(self):
212         for r in self.baudrates:
213             print "\nTrying %i" % r;
214             self.serialport.setBaudrate(r);
215             #time.sleep(1);
216             self.serialport.flushInput()
217             self.serialport.flushOutput()
218             
219             for i in range(1,10):
220                 self.readbyte();
221             
222             print "Read %02x %02x %02x %02x" % (
223                 self.readbyte(),self.readbyte(),self.readbyte(),self.readbyte());
224     def monitortest(self):
225         """Self-test several functions through the monitor."""
226         print "Performing monitor self-test.";
227         
228         if self.peekword(0x0c00)!=0x0c04 and self.peekword(0x0c00)!=0x0c06:
229             print "ERROR Fetched wrong value from 0x0c04.";
230         self.pokebyte(0x0021,0); #Drop LED
231         if self.peekbyte(0x0021)!=0:
232             print "ERROR, P1OUT not cleared.";
233         self.pokebyte(0x0021,1); #Light LED
234         
235         print "Self-test complete.";
236     
237     
238
239     def I2Csetup(self):
240         """Move the FET into the I2C application."""
241         self.writecmd(0x02,0x10,0,self.data); #SPI/SETUP
242     def I2Cstart(self):
243         """Start an I2C transaction."""
244         self.writecmd(0x02,0x20,0,self.data); #SPI/SETUP
245     def I2Cstop(self):
246         """Stop an I2C transaction."""
247         self.writecmd(0x02,0x21,0,self.data); #SPI/SETUP
248     def I2Cread(self,len=1):
249         """Read len bytes by I2C."""
250         self.writecmd(0x02,0x00,1,[len]); #SPI/SETUP
251         return self.data;
252     def I2Cwrite(self,bytes):
253         """Write bytes by I2C."""
254         self.writecmd(0x02,0x01,len(bytes),bytes); #SPI/SETUP
255         return ord(self.data[0]);