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