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 GoodFETSPIFlash(GoodFETSPI):
29 JEDECmanufacturers={0xFF: "MISSING",
35 JEDECdevices={0xFFFFFF: "MISSING",
40 0xC22014: "MX25L8005",
41 0xC22013: "MX25L4005",
44 JEDECsizes={0x14: 0x100000,
51 """Grab an SPI Flash ROM's JEDEC bytes."""
53 data=self.SPItrans(data);
54 #print "Manufacturer: %02x\nType: %02x\nCapacity: %02x" % (ord(data[1]),ord(data[2]),ord(data[3]));
55 self.JEDECmanufacturer=ord(data[1]);
56 self.JEDECtype=ord(data[2]);
57 self.JEDECcapacity=ord(data[3]);
58 self.JEDECsize=self.JEDECsizes.get(self.JEDECcapacity);
59 if self.JEDECsize==None:
61 self.JEDECdevice=(ord(data[1])<<16)+(ord(data[2])<<8)+ord(data[3]);
63 def SPIpeek(self,adr):
64 """Grab a byte from an SPI Flash ROM."""
71 return ord(self.data[4]);
72 def SPIpeekblock(self,adr,blocks=1):
73 """Grab a few block from an SPI Flash ROM. Block size is unknown"""
74 data=[(adr&0xFF0000)>>16,
79 self.writecmd(0x01,0x02,4,data,blocks);
82 def SPIpokebyte(self,adr,val):
83 self.SPIpokebytes(adr,[val]);
84 def SPIpokebytes(self,adr,data):
85 #self.SPIwriteenable();
86 adranddata=[(adr&0xFF0000)>>16,
90 self.writecmd(0x01,0x03,
91 len(adranddata),adranddata);
93 def SPIchiperase(self):
94 """Mass erase an SPI Flash ROM."""
95 self.writecmd(0x01,0x81,0,[]);
96 def SPIwriteenable(self):
97 """SPI Flash Write Enable"""
101 def SPIjedecmanstr(self):
102 """Grab the JEDEC manufacturer string. Call after SPIjedec()."""
103 man=self.JEDECmanufacturers.get(self.JEDECmanufacturer)
108 def SPIjedecstr(self):
109 """Grab the JEDEC manufacturer string. Call after SPIjedec()."""
110 man=self.JEDECmanufacturers.get(self.JEDECmanufacturer);
113 device=self.JEDECdevices.get(self.JEDECdevice);
116 return "%s %s" % (man,device);