Support for dumping AVR Flash.
[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     def __init__(self, *args, **kargs):
14         self.data=[0];
15     def timeout(self):
16         print "timeout\n";
17     def serInit(self, port=None):
18         """Open the serial port"""
19         
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:
23                 port = glob_list[0];
24         if port is None:
25             glob_list = glob.glob("/dev/tty.usbserial*");
26             if len(glob_list) > 0:
27                 port = glob_list[0];
28         if port is None:
29             glob_list = glob.glob("/dev/ttyUSB*");
30             if len(glob_list) > 0:
31                 port = glob_list[0];
32         
33         self.serialport = serial.Serial(
34             port,
35             #9600,
36             115200,
37             parity = serial.PARITY_NONE
38             )
39         
40         #Explicitly set RTS
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()
46         
47         #Read and handle the initial command.
48         #time.sleep(1);
49         self.readcmd(); #Read the first command.
50         if(self.verb!=0x7F):
51             print "Verb %02x is wrong.  Incorrect firmware?" % self.verb;
52         #print "Connected."
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));
60         
61         #if data!=None:
62         #    count=len(data); #Initial count ignored.
63         
64         #print "TX %02x %02x" % (app,verb);
65         
66         #little endian 16-bit length
67         self.serialport.write(chr(count&0xFF));
68         self.serialport.write(chr(count>>8));
69         
70         #print "count=%02x, len(data)=%04x" % (count,len(data));
71         
72         if count!=0:
73             for i in range(0,count):
74                 #print "Converting %02x at %i" % (data[i],i)
75                 data[i]=chr(data[i]);
76             outstr=''.join(data);
77             #outstr=data;
78             self.serialport.write(outstr);
79         if not self.besilent:
80             self.readcmd();
81         
82     besilent=0;
83     app=0;
84     verb=0;
85     count=0;
86     data="";
87
88     def readcmd(self):
89         """Read a reply from the GoodFET."""
90         while 1:
91             #print "Reading...";
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;
96             self.count=(
97                 ord(self.serialport.read(1))
98                 +(ord(self.serialport.read(1))<<8)
99                 );
100             
101             #Debugging string; print, but wait.
102             if self.app==0xFF and self.verb==0xFF:
103                 print "DEBUG %s" % self.serialport.read(self.count);
104             else:
105                 self.data=self.serialport.read(self.count);
106                 return self.data;
107     
108     #Monitor stuff
109     def silent(self,s=0):
110         """Transmissions halted when 1."""
111         self.besilent=s;
112         print "besilent is %i" % self.besilent;
113         self.writecmd(0,0xB0,1,[s]);
114         
115     def out(self,byte):
116         """Write a byte to P5OUT."""
117         self.writecmd(0,0xA1,1,[byte]);
118     def dir(self,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);
125         #self.readcmd();
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):
136         i=begin;
137         while i<end:
138             print "%04x %04x" % (i, self.peekword(i));
139             i+=2;
140     def monitor_ram_pattern(self):
141         """Overwrite all of RAM with 0xBEEF."""
142         self.writecmd(0,0x90,0,self.data);
143         return;
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);
148     
149     #Baud rates.
150     baudrates=[115200, 
151                9600,
152                19200,
153                38400,
154                57600,
155                115200];
156     def setBaud(self,baud):
157         """Change the baud rate.  TODO fix this."""
158         rates=self.baudrates;
159         self.data=[baud];
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));
165         
166         print "Changed host baud."
167         self.serialport.setBaudrate(rates[baud]);
168         time.sleep(1);
169         self.serialport.flushInput()
170         self.serialport.flushOutput()
171         
172         print "Baud is now %i." % rates[baud];
173         return;
174     def readbyte(self):
175         return ord(self.serialport.read(1));
176     def findbaud(self):
177         for r in self.baudrates:
178             print "\nTrying %i" % r;
179             self.serialport.setBaudrate(r);
180             #time.sleep(1);
181             self.serialport.flushInput()
182             self.serialport.flushOutput()
183             
184             for i in range(1,10):
185                 self.readbyte();
186             
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.";
192         
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
199         
200         print "Self-test complete.";
201     
202     
203
204     def I2Csetup(self):
205         """Move the FET into the I2C application."""
206         self.writecmd(0x02,0x10,0,self.data); #SPI/SETUP
207     def I2Cstart(self):
208         """Start an I2C transaction."""
209         self.writecmd(0x02,0x20,0,self.data); #SPI/SETUP
210     def I2Cstop(self):
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
216         return self.data;
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]);