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 def eeprompeek(self, adr):
121 """Read a byte of the target's EEPROM."""
122 self.writecmd(self.AVRAPP,0x81 ,2,
123 [ (adr&0xFF), (adr>>8)]
124 );#little-endian address
125 return ord(self.data[0]);
126 def flashpeek(self, adr):
127 """Read a byte of the target's Flash memory."""
128 self.writecmd(self.AVRAPP,0x02 ,2,
129 [ (adr&0xFF), (adr>>8)]
130 );#little-endian address
131 return ord(self.data[0]);
132 def flashpeekblock(self, adr):
133 """Read a byte of the target's Flash memory."""
134 self.writecmd(self.AVRAPP,0x02 ,4,
135 [ (adr&0xFF), (adr>>8) &0xFF, 0x80, 0x00]
139 def eeprompoke(self, adr, val):
140 """Write a byte of the target's EEPROM."""
141 self.writecmd(self.AVRAPP,0x91 ,3,
142 [ (adr&0xFF), (adr>>8), val]
143 );#little-endian address
144 return ord(self.data[0]);
147 """Return an identifying string."""
148 self.writecmd(self.AVRAPP,0x83,0, None);
149 vendor=self.AVRVendors.get(ord(self.data[0]));
150 deviceid=(ord(self.data[1])<<8)+ord(self.data[2]);
151 device=self.AVRDevices.get(deviceid);
153 #Return hex if device is unknown.
154 #They are similar enough that it needn't be known.
156 device=("0x%04x" % deviceid);
158 return "%s %s" % (vendor,device);