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 GoodFETSPI(GoodFET):
14 """Move the FET into the SPI application."""
15 self.writecmd(0x01,0x10,0,self.data); #SPI/SETUP
17 def SPItrans8(self,byte):
18 """Read and write 8 bits by SPI."""
19 data=self.SPItrans([byte]);
22 def SPItrans(self,data):
23 """Exchange data by SPI."""
25 self.writecmd(0x01,0x00,len(data),data);
28 class GoodFETSPI25C(GoodFETSPI):
37 def peek8(self,adr,memory="vn"):
38 """Read a byte from the given address."""
39 data=self.SPItrans([self.READ,(adr>>8)&0xFF,adr&0xFF,0x00]);
43 class GoodFETSPIFlash(GoodFETSPI):
44 JEDECmanufacturers={0xFF: "MISSING",
52 JEDECdevices={0xFFFFFF: "MISSING",
58 0xC22017: "MX25L6405D",
59 0xC22016: "MX25L3205D",
60 0xC22015: "MX25L1605D",
61 0xC22014: "MX25L8005",
62 0xC22013: "MX25L4005",
64 0x1f4501: "AT24DF081",
67 JEDECsizes={0x17: 0x800000,
79 """Grab an SPI Flash ROM's JEDEC bytes."""
81 data=self.SPItrans(data);
83 self.JEDECmanufacturer=ord(data[1]);
84 self.JEDECtype=ord(data[2]);
85 self.JEDECcapacity=ord(data[3]);
86 self.JEDECsize=self.JEDECsizes.get(self.JEDECcapacity);
87 if self.JEDECsize==None:
89 jedec=(ord(data[1])<<16)+(ord(data[2])<<8)+ord(data[3]);
91 self.JEDECsize=1024**2;
92 self.JEDECdevice=jedec;
94 def SPIpeek(self,adr):
95 """Grab a byte from an SPI Flash ROM."""
102 return ord(self.data[4]);
103 def SPIpeekblock(self,adr):
104 """Grab a few block from an SPI Flash ROM. Block size is unknown"""
105 data=[(adr&0xFF0000)>>16,
109 self.writecmd(0x01,0x02,3,data);
112 def SPIpokebyte(self,adr,val):
113 self.SPIpokebytes(adr,[val]);
114 def SPIpokebytes(self,adr,data):
115 #Used to be 24 bits, BE, not 32 bits, LE.
116 adranddata=[adr&0xFF,
121 #print "%06x: poking %i bytes" % (adr,len(data));
122 self.writecmd(0x01,0x03,
123 len(adranddata),adranddata);
125 def SPIchiperase(self):
126 """Mass erase an SPI Flash ROM."""
127 self.writecmd(0x01,0x81);
128 def SPIwriteenable(self):
129 """SPI Flash Write Enable"""
133 def SPIjedecmanstr(self):
134 """Grab the JEDEC manufacturer string. Call after SPIjedec()."""
135 man=self.JEDECmanufacturers.get(self.JEDECmanufacturer)
140 def SPIjedecstr(self):
141 """Grab the JEDEC manufacturer string. Call after SPIjedec()."""
142 man=self.JEDECmanufacturers.get(self.JEDECmanufacturer);
145 device=self.JEDECdevices.get(self.JEDECdevice);
148 return "%s %s" % (man,device);