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 from avr910.asm and other sources.
20 #More devices at http://avr.fenceline.de/device_data.html
58 """Move the FET into the AVR application."""
59 self.writecmd(self.AVRAPP,0x10,0,self.data); #SPI/SETUP
62 """Exchange data by AVR.
63 Input should probably be 4 bytes."""
65 self.writecmd(self.AVRAPP,0x00,len(data),data);
69 """Start the connection."""
70 self.writecmd(self.AVRAPP,0x20,0,None);
72 """Forcibly start a connection."""
74 for i in range(0x880,0xfff):
75 #self.glitchVoltages(0x880, i);
78 print "At %04x, Lockbits: %02x" % (i,bits);
79 if(bits==0xFF): return;
81 """Erase the target chip."""
82 self.writecmd(self.AVRAPP,0xF0,0,None);
84 """Read the target's lockbits."""
85 self.writecmd(self.AVRAPP,0x82,0,None);
86 return ord(self.data[0]);
87 def setlockbits(self,bits=0x00):
88 """Read the target's lockbits."""
89 self.writecmd(self.AVRAPP,0x92,1,[bits]);
90 return self.lockbits();
92 def eeprompeek(self, adr):
93 """Read a byte of the target's EEPROM."""
94 self.writecmd(self.AVRAPP,0x81 ,2,
95 [ (adr&0xFF), (adr>>8)]
96 );#little-endian address
97 return ord(self.data[0]);
98 def flashpeek(self, adr):
99 """Read a byte of the target's EEPROM."""
100 self.writecmd(self.AVRAPP,0x02 ,2,
101 [ (adr&0xFF), (adr>>8)]
102 );#little-endian address
103 return ord(self.data[0]);
104 def flashpeekblock(self, adr):
105 """Read a byte of the target's EEPROM."""
106 self.writecmd(self.AVRAPP,0x02 ,4,
107 [ (adr&0xFF), (adr>>8) &0xFF, 0x80, 0x00]
111 def eeprompoke(self, adr, val):
112 """Write a byte of the target's EEPROM."""
113 self.writecmd(self.AVRAPP,0x91 ,3,
114 [ (adr&0xFF), (adr>>8), val]
115 );#little-endian address
116 return ord(self.data[0]);
119 """Return an identifying string."""
120 self.writecmd(self.AVRAPP,0x83,0, None);
121 vendor=self.AVRVendors.get(ord(self.data[0]));
122 deviceid=(ord(self.data[1])<<8)+ord(self.data[2]);
123 device=self.AVRDevices.get(deviceid);
125 #Return hex if device is unknown.
126 #They are similar enough that it needn't be known.
128 device=("0x%04x" % deviceid);
130 return "%s %s" % (vendor,device);