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);
71 """Erase the target chip."""
72 self.writecmd(self.AVRAPP,0xF0,0,None);
74 """Read the target's lockbits."""
75 self.writecmd(self.AVRAPP,0x82,0,None);
76 return ord(self.data[0]);
77 def eeprompeek(self, adr):
78 """Read a byte of the target's EEPROM."""
79 self.writecmd(self.AVRAPP,0x81 ,2,
80 [ (adr&0xFF), (adr>>8)]
81 );#little-endian address
82 return ord(self.data[0]);
83 def flashpeek(self, adr):
84 """Read a byte of the target's EEPROM."""
85 self.writecmd(self.AVRAPP,0x02 ,2,
86 [ (adr&0xFF), (adr>>8)]
87 );#little-endian address
88 return ord(self.data[0]);
89 def flashpeekblock(self, adr):
90 """Read a byte of the target's EEPROM."""
91 self.writecmd(self.AVRAPP,0x02 ,4,
92 [ (adr&0xFF), (adr>>8) &0xFF, 0x80, 0x00]
96 def eeprompoke(self, adr, val):
97 """Write a byte of the target's EEPROM."""
98 self.writecmd(self.AVRAPP,0x91 ,3,
99 [ (adr&0xFF), (adr>>8), val]
100 );#little-endian address
101 return ord(self.data[0]);
104 """Return an identifying string."""
105 self.writecmd(self.AVRAPP,0x83,0, None);
106 vendor=self.AVRVendors.get(ord(self.data[0]));
107 deviceid=(ord(self.data[1])<<8)+ord(self.data[2]);
108 device=self.AVRDevices.get(deviceid);
110 #Return hex if device is unknown.
111 #They are similar enough that it needn't be known.
113 device=("0x%04x" % deviceid);
115 return "%s %s" % (vendor,device);