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, os;
10 from GoodFET import GoodFET;
12 class GoodFETSPI(GoodFET):
15 """Move the FET into the SPI application."""
16 self.writecmd(0x01,0x10,0,self.data); #SPI/SETUP
18 def SPItrans8(self,byte):
19 """Read and write 8 bits by SPI."""
20 data=self.SPItrans([byte]);
23 def SPItrans(self,data):
24 """Exchange data by SPI."""
26 self.writecmd(0x01,0x00,len(data),data);
29 class GoodFETSPI25C(GoodFETSPI):
38 def peek8(self,adr,memory="vn"):
39 """Read a byte from the given address."""
40 data=self.SPItrans([self.READ,(adr>>8)&0xFF,adr&0xFF,0x00]);
44 class GoodFETSPIFlash(GoodFETSPI):
45 JEDECmanufacturers={0xFF: "MISSING",
53 JEDECdevices={0xFFFFFF: "MISSING",
59 0xC22017: "MX25L6405D",
60 0xC22016: "MX25L3205D",
61 0xC22015: "MX25L1605D",
62 0xC22014: "MX25L8005",
63 0xC22013: "MX25L4005",
66 0x1f4501: "AT24DF081",
69 JEDECsizes={0x17: 0x800000,
81 """Grab an SPI Flash ROM's JEDEC bytes. Some chips don't implement
82 this, such as Numonyx M25P* except in the 110nm processed."""
84 data=self.SPItrans(data);
86 self.JEDECmanufacturer=ord(data[1]);
87 if self.JEDECmanufacturer==0xFF:
89 self.JEDECcapacity=0x14;
92 self.JEDECtype=ord(data[2]);
93 self.JEDECcapacity=ord(data[3]);
94 jedec=(ord(data[1])<<16)+(ord(data[2])<<8)+ord(data[3]);
95 self.JEDECsize=self.JEDECsizes.get(self.JEDECcapacity);
96 if self.JEDECsize==None:
100 self.JEDECsize=1024**2;
101 self.JEDECdevice=jedec;
103 def SPIpeek(self,adr):
104 """Grab a byte from an SPI Flash ROM."""
111 return ord(self.data[4]);
112 def SPIpeekblock(self,adr):
113 """Grab a few block from an SPI Flash ROM. Block size is unknown"""
114 data=[(adr&0xFF0000)>>16,
118 self.writecmd(0x01,0x02,3,data);
121 def SPIpokebyte(self,adr,val):
122 self.SPIpokebytes(adr,[val]);
123 def SPIpokebytes(self,adr,data):
124 #Used to be 24 bits, BE, not 32 bits, LE.
125 adranddata=[adr&0xFF,
130 #print "%06x: poking %i bytes" % (adr,len(data));
131 self.writecmd(0x01,0x03,
132 len(adranddata),adranddata);
134 def SPIchiperase(self):
135 """Mass erase an SPI Flash ROM."""
136 self.writecmd(0x01,0x81);
137 def SPIwriteenable(self):
138 """SPI Flash Write Enable"""
142 def SPIjedecmanstr(self):
143 """Grab the JEDEC manufacturer string. Call after SPIjedec()."""
144 man=self.JEDECmanufacturers.get(self.JEDECmanufacturer)
149 def SPIjedecstr(self):
150 """Grab the JEDEC manufacturer string. Call after SPIjedec()."""
151 man=self.JEDECmanufacturers.get(self.JEDECmanufacturer);
154 device=self.JEDECdevices.get(self.JEDECdevice);
157 return "%s %s" % (man,device);