2 # GoodFET Client Library
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
6 # This code is being rewritten and refactored. You've been warned!
8 import sys, time, string, cStringIO, struct, glob, serial, os;
12 """GoodFET Client Library"""
13 def __init__(self, *args, **kargs):
17 def serInit(self, port=None):
18 """Open the serial port"""
20 if port is None and os.environ.get("GOODFET")!=None:
21 glob_list = glob.glob(os.environ.get("GOODFET"));
22 if len(glob_list) > 0:
25 glob_list = glob.glob("/dev/tty.usbserial*");
26 if len(glob_list) > 0:
29 glob_list = glob.glob("/dev/ttyUSB*");
30 if len(glob_list) > 0:
33 self.serialport = serial.Serial(
37 parity = serial.PARITY_NONE
41 self.serialport.setRTS(1);
42 #Drop DTR, which is !RST, low to begin the app.
43 self.serialport.setDTR(0);
44 self.serialport.flushInput()
45 self.serialport.flushOutput()
47 #Read and handle the initial command.
49 self.readcmd(); #Read the first command.
51 print "Verb %02x is wrong. Incorrect firmware?" % self.verb;
53 def getbuffer(self,size=0x1c00):
54 writecmd(0,0xC2,[size&0xFF,(size>>16)&0xFF]);
55 print "Got %02x%02x buffer size." % (self.data[1],self.data[0]);
56 def writecmd(self, app, verb, count=0, data=[]):
57 """Write a command and some data to the GoodFET."""
58 self.serialport.write(chr(app));
59 self.serialport.write(chr(verb));
62 # count=len(data); #Initial count ignored.
64 #print "TX %02x %02x" % (app,verb);
66 #little endian 16-bit length
67 self.serialport.write(chr(count&0xFF));
68 self.serialport.write(chr(count>>8));
70 #print "count=%02x, len(data)=%04x" % (count,len(data));
73 for i in range(0,count):
74 #print "Converting %02x at %i" % (data[i],i)
78 self.serialport.write(outstr);
89 """Read a reply from the GoodFET."""
92 self.app=ord(self.serialport.read(1));
93 #print "APP=%2x" % self.app;
94 self.verb=ord(self.serialport.read(1));
95 #print "VERB=%02x" % self.verb;
97 ord(self.serialport.read(1))
98 +(ord(self.serialport.read(1))<<8)
101 #Debugging string; print, but wait.
102 if self.app==0xFF and self.verb==0xFF:
103 print "DEBUG %s" % self.serialport.read(self.count);
105 self.data=self.serialport.read(self.count);
109 def silent(self,s=0):
110 """Transmissions halted when 1."""
112 print "besilent is %i" % self.besilent;
113 self.writecmd(0,0xB0,1,[s]);
116 """Write a byte to P5OUT."""
117 self.writecmd(0,0xA1,1,[byte]);
119 """Write a byte to P5DIR."""
120 self.writecmd(0,0xA0,1,[byte]);
121 def peekbyte(self,address):
122 """Read a byte of memory from the monitor."""
123 self.data=[address&0xff,address>>8];
124 self.writecmd(0,0x02,2,self.data);
126 return ord(self.data[0]);
127 def peekword(self,address):
128 """Read a word of memory from the monitor."""
129 return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
130 def pokebyte(self,address,value):
131 """Set a byte of memory by the monitor."""
132 self.data=[address&0xff,address>>8,value];
133 self.writecmd(0,0x03,3,self.data);
134 return ord(self.data[0]);
135 def dumpmem(self,begin,end):
138 print "%04x %04x" % (i, self.peekword(i));
140 def monitor_ram_pattern(self):
141 """Overwrite all of RAM with 0xBEEF."""
142 self.writecmd(0,0x90,0,self.data);
144 def monitor_ram_depth(self):
145 """Determine how many bytes of RAM are unused by looking for 0xBEEF.."""
146 self.writecmd(0,0x91,0,self.data);
147 return ord(self.data[0])+(ord(self.data[1])<<8);
156 def setBaud(self,baud):
157 """Change the baud rate. TODO fix this."""
158 rates=self.baudrates;
160 print "Changing FET baud."
161 self.serialport.write(chr(0x00));
162 self.serialport.write(chr(0x80));
163 self.serialport.write(chr(1));
164 self.serialport.write(chr(baud));
166 print "Changed host baud."
167 self.serialport.setBaudrate(rates[baud]);
169 self.serialport.flushInput()
170 self.serialport.flushOutput()
172 print "Baud is now %i." % rates[baud];
175 return ord(self.serialport.read(1));
177 for r in self.baudrates:
178 print "\nTrying %i" % r;
179 self.serialport.setBaudrate(r);
181 self.serialport.flushInput()
182 self.serialport.flushOutput()
184 for i in range(1,10):
187 print "Read %02x %02x %02x %02x" % (
188 self.readbyte(),self.readbyte(),self.readbyte(),self.readbyte());
189 def monitortest(self):
190 """Self-test several functions through the monitor."""
191 print "Performing monitor self-test.";
193 if self.peekword(0x0c00)!=0x0c04 and self.peekword(0x0c00)!=0x0c06:
194 print "ERROR Fetched wrong value from 0x0c04.";
195 self.pokebyte(0x0021,0); #Drop LED
196 if self.peekbyte(0x0021)!=0:
197 print "ERROR, P1OUT not cleared.";
198 self.pokebyte(0x0021,1); #Light LED
200 print "Self-test complete.";
205 """Move the FET into the I2C application."""
206 self.writecmd(0x02,0x10,0,self.data); #SPI/SETUP
208 """Start an I2C transaction."""
209 self.writecmd(0x02,0x20,0,self.data); #SPI/SETUP
211 """Stop an I2C transaction."""
212 self.writecmd(0x02,0x21,0,self.data); #SPI/SETUP
213 def I2Cread(self,len=1):
214 """Read len bytes by I2C."""
215 self.writecmd(0x02,0x00,1,[len]); #SPI/SETUP
217 def I2Cwrite(self,bytes):
218 """Write bytes by I2C."""
219 self.writecmd(0x02,0x01,len(bytes),bytes); #SPI/SETUP
220 return ord(self.data[0]);