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):
14 AVRVendors={0x1E: "Atmel",
18 #List from avr910.asm and other sources.
19 #More devices at http://avr.fenceline.de/device_data.html
57 """Move the FET into the SPI application."""
58 self.writecmd(self.AVRAPP,0x10,0,self.data); #SPI/SETUP
61 """Exchange data by AVR.
62 Input should probably be 4 bytes."""
64 self.writecmd(self.AVRAPP,0x00,len(data),data);
68 """Start the connection."""
69 self.writecmd(self.AVRAPP,0x20,0,None);
70 def glitchstart(self):
71 """Glitch into the AVR application."""
72 self.glitchVerb(self.AVRAPP,0x20,None);
73 def glitchstarttime(self):
74 """Measure the timer of the START verb."""
75 return self.glitchTime(self.AVRAPP,0x20,None);
77 """Forcibly start a connection."""
79 for i in range(0x880,0xfff):
80 #self.glitchVoltages(0x880, i);
83 print "At %04x, Lockbits: %02x" % (i,bits);
84 if(bits==0xFF): return;
86 """Erase the target chip."""
87 self.writecmd(self.AVRAPP,0xF0,0,None);
89 """Read the target's lockbits."""
90 self.writecmd(self.AVRAPP,0x82,0,None);
91 return ord(self.data[0]);
92 def setlockbits(self,bits=0x00):
93 """Read the target's lockbits."""
94 self.writecmd(self.AVRAPP,0x92,1,[bits]);
95 return self.lockbits();
97 def eeprompeek(self, adr):
98 """Read a byte of the target's EEPROM."""
99 self.writecmd(self.AVRAPP,0x81 ,2,
100 [ (adr&0xFF), (adr>>8)]
101 );#little-endian address
102 return ord(self.data[0]);
103 def flashpeek(self, adr):
104 """Read a byte of the target's EEPROM."""
105 self.writecmd(self.AVRAPP,0x02 ,2,
106 [ (adr&0xFF), (adr>>8)]
107 );#little-endian address
108 return ord(self.data[0]);
109 def flashpeekblock(self, adr):
110 """Read a byte of the target's EEPROM."""
111 self.writecmd(self.AVRAPP,0x02 ,4,
112 [ (adr&0xFF), (adr>>8) &0xFF, 0x80, 0x00]
116 def eeprompoke(self, adr, val):
117 """Write a byte of the target's EEPROM."""
118 self.writecmd(self.AVRAPP,0x91 ,3,
119 [ (adr&0xFF), (adr>>8), val]
120 );#little-endian address
121 return ord(self.data[0]);
124 """Return an identifying string."""
125 self.writecmd(self.AVRAPP,0x83,0, None);
126 vendor=self.AVRVendors.get(ord(self.data[0]));
127 deviceid=(ord(self.data[1])<<8)+ord(self.data[2]);
128 device=self.AVRDevices.get(deviceid);
130 #Return hex if device is unknown.
131 #They are similar enough that it needn't be known.
133 device=("0x%04x" % deviceid);
135 return "%s %s" % (vendor,device);