2 # GoodFET SPI and SPIFlash 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, os;
10 from GoodFET import GoodFET;
12 class GoodFETAVR(GoodFET):
15 AVRVendors={0x1E: "Atmel",
19 #List imported from http://avr.fenceline.de/device_data.html
60 0x958a: "ATmega32U2", #TODO add the other U series.
85 #These are missing from the Fenceline DB.
90 """Move the FET into the AVR application."""
91 self.writecmd(self.AVRAPP,0x10,0,self.data); #SPI/SETUP
94 """Exchange data by AVR.
95 Input should probably be 4 bytes."""
97 self.writecmd(self.AVRAPP,0x00,len(data),data);
101 """Start the connection."""
102 self.writecmd(self.AVRAPP,0x20,0,None);
103 def forcestart(self):
104 """Forcibly start a connection."""
106 for i in range(0x880,0xfff):
107 #self.glitchVoltages(0x880, i);
109 bits=self.lockbits();
110 print "At %04x, Lockbits: %02x" % (i,bits);
111 if(bits==0xFF): return;
113 """Erase the target chip."""
114 self.writecmd(self.AVRAPP,0xF0,0,None);
116 """Read the target's lockbits."""
117 self.writecmd(self.AVRAPP,0x82,0,None);
118 return ord(self.data[0]);
119 def setlockbits(self,bits=0x00):
120 """Read the target's lockbits."""
121 self.writecmd(self.AVRAPP,0x92,1,[bits]);
122 return self.lockbits();
124 self.setlockbits(0xFC);
125 def eeprompeek(self, adr):
126 """Read a byte of the target's EEPROM."""
127 self.writecmd(self.AVRAPP,0x81 ,2,
128 [ (adr&0xFF), (adr>>8)]
129 );#little-endian address
130 return ord(self.data[0]);
131 def flashpeek(self, adr):
132 """Read a byte of the target's Flash memory."""
133 self.writecmd(self.AVRAPP,0x02 ,2,
134 [ (adr&0xFF), (adr>>8)]
135 );#little-endian address
136 return ord(self.data[0]);
137 def flashpeekblock(self, adr):
138 """Read a byte of the target's Flash memory."""
139 self.writecmd(self.AVRAPP,0x02 ,4,
140 [ (adr&0xFF), (adr>>8) &0xFF, 0x80, 0x00]
144 def eeprompoke(self, adr, val):
145 """Write a byte of the target's EEPROM."""
146 self.writecmd(self.AVRAPP,0x91 ,3,
147 [ (adr&0xFF), (adr>>8), val]
148 );#little-endian address
149 return ord(self.data[0]);
152 """Return an identifying string."""
153 self.writecmd(self.AVRAPP,0x83,0, None);
154 vendor=self.AVRVendors.get(ord(self.data[0]));
155 deviceid=(ord(self.data[1])<<8)+ord(self.data[2]);
156 device=self.AVRDevices.get(deviceid);
158 #Return hex if device is unknown.
159 #They are similar enough that it needn't be known.
161 device=("0x%04x" % deviceid);
163 return "%s %s" % (vendor,device);