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 #print "TX %02x %02x" % (app,verb);
64 #little endian 16-bit length
65 self.serialport.write(chr(count&0xFF));
66 self.serialport.write(chr(count>>8));
68 #print "count=%02x, len(data)=%04x" % (count,len(data));
71 for i in range(0,count):
72 #print "Converting %02x at %i" % (data[i],i)
75 self.serialport.write(outstr);
86 """Read a reply from the GoodFET."""
89 self.app=ord(self.serialport.read(1));
90 #print "APP=%2x" % self.app;
91 self.verb=ord(self.serialport.read(1));
92 #print "VERB=%02x" % self.verb;
94 ord(self.serialport.read(1))
95 +(ord(self.serialport.read(1))<<8)
98 #Debugging string; print, but wait.
99 if self.app==0xFF and self.verb==0xFF:
100 print "DEBUG %s" % self.serialport.read(self.count);
102 self.data=self.serialport.read(self.count);
106 def silent(self,s=0):
107 """Transmissions halted when 1."""
109 print "besilent is %i" % self.besilent;
110 self.writecmd(0,0xB0,1,[s]);
113 """Write a byte to P5OUT."""
114 self.writecmd(0,0xA1,1,[byte]);
116 """Write a byte to P5DIR."""
117 self.writecmd(0,0xA0,1,[byte]);
118 def peekbyte(self,address):
119 """Read a byte of memory from the monitor."""
120 self.data=[address&0xff,address>>8];
121 self.writecmd(0,0x02,2,self.data);
123 return ord(self.data[0]);
124 def peekword(self,address):
125 """Read a word of memory from the monitor."""
126 return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
127 def pokebyte(self,address,value):
128 """Set a byte of memory by the monitor."""
129 self.data=[address&0xff,address>>8,value];
130 self.writecmd(0,0x03,3,self.data);
131 return ord(self.data[0]);
132 def dumpmem(self,begin,end):
135 print "%04x %04x" % (i, self.peekword(i));
137 def monitor_ram_pattern(self):
138 """Overwrite all of RAM with 0xBEEF."""
139 self.writecmd(0,0x90,0,self.data);
141 def monitor_ram_depth(self):
142 """Determine how many bytes of RAM are unused by looking for 0xBEEF.."""
143 self.writecmd(0,0x91,0,self.data);
144 return ord(self.data[0])+(ord(self.data[1])<<8);
153 def setBaud(self,baud):
154 """Change the baud rate. TODO fix this."""
155 rates=self.baudrates;
157 print "Changing FET baud."
158 self.serialport.write(chr(0x00));
159 self.serialport.write(chr(0x80));
160 self.serialport.write(chr(1));
161 self.serialport.write(chr(baud));
163 print "Changed host baud."
164 self.serialport.setBaudrate(rates[baud]);
166 self.serialport.flushInput()
167 self.serialport.flushOutput()
169 print "Baud is now %i." % rates[baud];
172 return ord(self.serialport.read(1));
174 for r in self.baudrates:
175 print "\nTrying %i" % r;
176 self.serialport.setBaudrate(r);
178 self.serialport.flushInput()
179 self.serialport.flushOutput()
181 for i in range(1,10):
184 print "Read %02x %02x %02x %02x" % (
185 self.readbyte(),self.readbyte(),self.readbyte(),self.readbyte());
186 def monitortest(self):
187 """Self-test several functions through the monitor."""
188 print "Performing monitor self-test.";
190 if self.peekword(0x0c00)!=0x0c04 and self.peekword(0x0c00)!=0x0c06:
191 print "ERROR Fetched wrong value from 0x0c04.";
192 self.pokebyte(0x0021,0); #Drop LED
193 if self.peekbyte(0x0021)!=0:
194 print "ERROR, P1OUT not cleared.";
195 self.pokebyte(0x0021,1); #Light LED
197 print "Self-test complete.";
202 """Move the FET into the I2C application."""
203 self.writecmd(0x02,0x10,0,self.data); #SPI/SETUP
205 """Start an I2C transaction."""
206 self.writecmd(0x02,0x20,0,self.data); #SPI/SETUP
208 """Stop an I2C transaction."""
209 self.writecmd(0x02,0x21,0,self.data); #SPI/SETUP
210 def I2Cread(self,len=1):
211 """Read len bytes by I2C."""
212 self.writecmd(0x02,0x00,1,[len]); #SPI/SETUP
214 def I2Cwrite(self,bytes):
215 """Write bytes by I2C."""
216 self.writecmd(0x02,0x01,len(bytes),bytes); #SPI/SETUP
217 return ord(self.data[0]);