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, serial, 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
86 """Move the FET into the AVR application."""
87 self.writecmd(self.AVRAPP,0x10,0,self.data); #SPI/SETUP
90 """Exchange data by AVR.
91 Input should probably be 4 bytes."""
93 self.writecmd(self.AVRAPP,0x00,len(data),data);
97 """Start the connection."""
98 self.writecmd(self.AVRAPP,0x20,0,None);
100 """Forcibly start a connection."""
102 for i in range(0x880,0xfff):
103 #self.glitchVoltages(0x880, i);
105 bits=self.lockbits();
106 print "At %04x, Lockbits: %02x" % (i,bits);
107 if(bits==0xFF): return;
109 """Erase the target chip."""
110 self.writecmd(self.AVRAPP,0xF0,0,None);
112 """Read the target's lockbits."""
113 self.writecmd(self.AVRAPP,0x82,0,None);
114 return ord(self.data[0]);
115 def setlockbits(self,bits=0x00):
116 """Read the target's lockbits."""
117 self.writecmd(self.AVRAPP,0x92,1,[bits]);
118 return self.lockbits();
120 self.setlockbits(0xFC);
121 def eeprompeek(self, adr):
122 """Read a byte of the target's EEPROM."""
123 self.writecmd(self.AVRAPP,0x81 ,2,
124 [ (adr&0xFF), (adr>>8)]
125 );#little-endian address
126 return ord(self.data[0]);
127 def flashpeek(self, adr):
128 """Read a byte of the target's Flash memory."""
129 self.writecmd(self.AVRAPP,0x02 ,2,
130 [ (adr&0xFF), (adr>>8)]
131 );#little-endian address
132 return ord(self.data[0]);
133 def flashpeekblock(self, adr):
134 """Read a byte of the target's Flash memory."""
135 self.writecmd(self.AVRAPP,0x02 ,4,
136 [ (adr&0xFF), (adr>>8) &0xFF, 0x80, 0x00]
140 def eeprompoke(self, adr, val):
141 """Write a byte of the target's EEPROM."""
142 self.writecmd(self.AVRAPP,0x91 ,3,
143 [ (adr&0xFF), (adr>>8), val]
144 );#little-endian address
145 return ord(self.data[0]);
148 """Return an identifying string."""
149 self.writecmd(self.AVRAPP,0x83,0, None);
150 vendor=self.AVRVendors.get(ord(self.data[0]));
151 deviceid=(ord(self.data[1])<<8)+ord(self.data[2]);
152 device=self.AVRDevices.get(deviceid);
154 #Return hex if device is unknown.
155 #They are similar enough that it needn't be known.
157 device=("0x%04x" % deviceid);
159 return "%s %s" % (vendor,device);