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"""
16 def __init__(self, *args, **kargs):
20 def serInit(self, port=None):
21 """Open the serial port"""
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:
28 glob_list = glob.glob("/dev/tty.usbserial*");
29 if len(glob_list) > 0:
32 glob_list = glob.glob("/dev/ttyUSB*");
33 if len(glob_list) > 0:
36 self.serialport = serial.Serial(
40 parity = serial.PARITY_NONE
43 #This might cause problems, but it makes failure graceful.
44 #self.serialport._timeout = 5;
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()
54 #Read and handle the initial command.
56 self.readcmd(); #Read the first command.
58 print "Verb %02x is wrong. Incorrect firmware?" % self.verb;
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));
69 # count=len(data); #Initial count ignored.
71 #print "TX %02x %02x %04x" % (app,verb,count);
73 #little endian 16-bit length
74 self.serialport.write(chr(count&0xFF));
75 self.serialport.write(chr(count>>8));
77 #print "count=%02x, len(data)=%04x" % (count,len(data));
80 if(isinstance(data,list)):
81 for i in range(0,count):
82 #print "Converting %02x at %i" % (data[i],i)
86 self.serialport.write(outstr);
97 """Read a reply from the GoodFET."""
100 self.app=ord(self.serialport.read(1));
101 #print "APP=%2x" % self.app;
102 self.verb=ord(self.serialport.read(1));
103 #print "VERB=%02x" % self.verb;
105 ord(self.serialport.read(1))
106 +(ord(self.serialport.read(1))<<8)
109 #Debugging string; print, but wait.
110 if self.app==0xFF and self.verb==0xFF:
111 print "# DEBUG %s" % self.serialport.read(self.count);
114 self.data=self.serialport.read(self.count);
117 def glitchApp(self,app):
118 """Glitch into a device by its application."""
119 self.data=[app&0xff];
120 self.writecmd(self.GLITCHAPP,0x80,1,self.data);
121 #return ord(self.data[0]);
122 def glitchVerb(self,app,verb,data):
123 """Glitch during a transaction."""
124 if data==None: data=[];
125 self.data=[app&0xff, verb&0xFF]+data;
126 self.writecmd(self.GLITCHAPP,0x81,len(self.data),self.data);
127 #return ord(self.data[0]);
128 def glitchstart(self):
129 """Glitch into the AVR application."""
130 self.glitchVerb(self.APP,0x20,None);
131 def glitchstarttime(self):
132 """Measure the timer of the START verb."""
133 return self.glitchTime(self.APP,0x20,None);
134 def glitchTime(self,app,verb,data):
135 """Time the execution of a verb."""
136 if data==None: data=[];
137 self.data=[app&0xff, verb&0xFF]+data;
138 self.writecmd(self.GLITCHAPP,0x82,len(self.data),self.data);
139 return ord(self.data[0])+(ord(self.data[1])<<8);
140 def glitchVoltages(self,low=0x0880, high=0x0fff):
141 """Set glitching voltages. (0x0fff is max.)"""
142 self.data=[low&0xff, (low>>8)&0xff,
143 high&0xff, (high>>8)&0xff];
144 self.writecmd(self.GLITCHAPP,0x90,4,self.data);
145 #return ord(self.data[0]);
146 def glitchRate(self,count=0x0800):
147 """Set glitching count period."""
148 self.data=[count&0xff, (count>>8)&0xff];
149 self.writecmd(self.GLITCHAPP,0x91,2,
151 #return ord(self.data[0]);
155 def silent(self,s=0):
156 """Transmissions halted when 1."""
158 print "besilent is %i" % self.besilent;
159 self.writecmd(0,0xB0,1,[s]);
162 """Write a byte to P5OUT."""
163 self.writecmd(0,0xA1,1,[byte]);
165 """Write a byte to P5DIR."""
166 self.writecmd(0,0xA0,1,[byte]);
168 """Call to an address."""
169 self.writecmd(0,0x30,2,
170 [adr&0xFF,(adr>>8)&0xFF]);
171 def execute(self,code):
172 """Execute supplied code."""
173 self.writecmd(0,0x31,2,#len(code),
175 def peekbyte(self,address):
176 """Read a byte of memory from the monitor."""
177 self.data=[address&0xff,address>>8];
178 self.writecmd(0,0x02,2,self.data);
180 return ord(self.data[0]);
181 def peekword(self,address):
182 """Read a word of memory from the monitor."""
183 return self.peekbyte(address)+(self.peekbyte(address+1)<<8);
184 def pokebyte(self,address,value):
185 """Set a byte of memory by the monitor."""
186 self.data=[address&0xff,address>>8,value];
187 self.writecmd(0,0x03,3,self.data);
188 return ord(self.data[0]);
189 def dumpmem(self,begin,end):
192 print "%04x %04x" % (i, self.peekword(i));
194 def monitor_ram_pattern(self):
195 """Overwrite all of RAM with 0xBEEF."""
196 self.writecmd(0,0x90,0,self.data);
198 def monitor_ram_depth(self):
199 """Determine how many bytes of RAM are unused by looking for 0xBEEF.."""
200 self.writecmd(0,0x91,0,self.data);
201 return ord(self.data[0])+(ord(self.data[1])<<8);
210 def setBaud(self,baud):
211 """Change the baud rate. TODO fix this."""
212 rates=self.baudrates;
214 print "Changing FET baud."
215 self.serialport.write(chr(0x00));
216 self.serialport.write(chr(0x80));
217 self.serialport.write(chr(1));
218 self.serialport.write(chr(baud));
220 print "Changed host baud."
221 self.serialport.setBaudrate(rates[baud]);
223 self.serialport.flushInput()
224 self.serialport.flushOutput()
226 print "Baud is now %i." % rates[baud];
229 return ord(self.serialport.read(1));
231 for r in self.baudrates:
232 print "\nTrying %i" % r;
233 self.serialport.setBaudrate(r);
235 self.serialport.flushInput()
236 self.serialport.flushOutput()
238 for i in range(1,10):
241 print "Read %02x %02x %02x %02x" % (
242 self.readbyte(),self.readbyte(),self.readbyte(),self.readbyte());
243 def monitortest(self):
244 """Self-test several functions through the monitor."""
245 print "Performing monitor self-test.";
247 if self.peekword(0x0c00)!=0x0c04 and self.peekword(0x0c00)!=0x0c06:
248 print "ERROR Fetched wrong value from 0x0c04.";
249 self.pokebyte(0x0021,0); #Drop LED
250 if self.peekbyte(0x0021)!=0:
251 print "ERROR, P1OUT not cleared.";
252 self.pokebyte(0x0021,1); #Light LED
254 print "Self-test complete.";
257 # The following functions ought to be implemented in
260 def infostring(self):
261 a=self.peekbyte(0xff0);
262 b=self.peekbyte(0xff1);
263 return "%02x%02x" % (a,b);
265 print "Locking Unsupported.";
267 print "Erasure Unsupported.";
276 def flash(self,file):
277 """Flash an intel hex file to code memory."""
278 print "Flash not implemented.";
279 def peek32(self,address, memory="vn"):
280 return (self.peek16(address,memory)+
281 (self.peek16(address+2,memory)<<16));
282 def peek16(self,address, memory="vn"):
283 return (self.peek8(address,memory)+
284 (self.peek8(address+1,memory)<<8));
285 def peek8(self,address, memory="vn"):