Register names for NRF.
[goodfet] / client / GoodFETSPI.py
1 #!/usr/bin/env python
2 # GoodFET SPI and SPIFlash Client Library
3
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # This code is being rewritten and refactored.  You've been warned!
7
8 import sys, time, string, cStringIO, struct, glob, serial, os;
9
10 from GoodFET import GoodFET;
11
12 class GoodFETSPI(GoodFET):
13     def SPIsetup(self):
14         """Move the FET into the SPI application."""
15         self.writecmd(0x01,0x10,0,self.data); #SPI/SETUP
16         
17     def SPItrans8(self,byte):
18         """Read and write 8 bits by SPI."""
19         data=self.SPItrans([byte]);
20         return ord(data[0]);
21     
22     def SPItrans(self,data):
23         """Exchange data by SPI."""
24         self.data=data;
25         self.writecmd(0x01,0x00,len(data),data);
26         return self.data;
27
28 class GoodFETSPIFlash(GoodFETSPI):
29     JEDECmanufacturers={0xFF: "MISSING",
30                         0xEF: "Winbond",
31                         0xC2: "MXIC",
32                         0x20: "Numonyx/ST",
33                         0x1F: "Atmel",
34                         0x01: "AMD/Spansion"
35                         };
36
37     JEDECdevices={0xFFFFFF: "MISSING",
38                   0xEF3015: "W25X16L",
39                   0xEF3014: "W25X80L",
40                   0xEF3013: "W25X40L",
41                   0xEF3012: "W25X20L",
42                   0xEF3011: "W25X10L",
43                   0xC22017: "MX25L6405D",
44                   0xC22016: "MX25L3205D",
45                   0xC22015: "MX25L1605D",
46                   0xC22014: "MX25L8005",
47                   0xC22013: "MX25L4005",
48                   0x204011: "M45PE10"
49                   };
50     
51     JEDECsizes={0x17: 0x800000,
52                 0x16: 0x400000,
53                 0x15: 0x200000,
54                 0x14: 0x100000,
55                 0x13: 0x080000,
56                 0x12: 0x040000,
57                 0x11: 0x020000
58                 };
59     
60     JEDECsize=0;
61
62     def SPIjedec(self):
63         """Grab an SPI Flash ROM's JEDEC bytes."""
64         data=[0x9f, 0, 0, 0];
65         data=self.SPItrans(data);
66         
67         self.JEDECmanufacturer=ord(data[1]);
68         self.JEDECtype=ord(data[2]);
69         self.JEDECcapacity=ord(data[3]);
70         self.JEDECsize=self.JEDECsizes.get(self.JEDECcapacity);
71         if self.JEDECsize==None:
72             self.JEDECsize=0;
73         self.JEDECdevice=(ord(data[1])<<16)+(ord(data[2])<<8)+ord(data[3]);
74         return data;
75     def SPIpeek(self,adr):
76         """Grab a byte from an SPI Flash ROM."""
77         data=[0x03,
78               (adr&0xFF0000)>>16,
79               (adr&0xFF00)>>8,
80               adr&0xFF,
81               0];
82         self.SPItrans(data);
83         return ord(self.data[4]);
84     def SPIpeekblock(self,adr):
85         """Grab a few block from an SPI Flash ROM.  Block size is unknown"""
86         data=[(adr&0xFF0000)>>16,
87               (adr&0xFF00)>>8,
88               adr&0xFF];
89         
90         self.writecmd(0x01,0x02,3,data);
91         return self.data;
92     
93     def SPIpokebyte(self,adr,val):
94         self.SPIpokebytes(adr,[val]);
95     def SPIpokebytes(self,adr,data):
96         #Used to be 24 bits, BE, not 32 bits, LE.
97         adranddata=[adr&0xFF,
98                     (adr&0xFF00)>>8,
99                     (adr&0xFF0000)>>16,
100                     0, #MSB
101                     ]+data;
102         #print "%06x: poking %i bytes" % (adr,len(data));
103         self.writecmd(0x01,0x03,
104                       len(adranddata),adranddata);
105         
106     def SPIchiperase(self):
107         """Mass erase an SPI Flash ROM."""
108         self.writecmd(0x01,0x81);
109     def SPIwriteenable(self):
110         """SPI Flash Write Enable"""
111         data=[0x06];
112         self.SPItrans(data);
113         
114     def SPIjedecmanstr(self):
115         """Grab the JEDEC manufacturer string.  Call after SPIjedec()."""
116         man=self.JEDECmanufacturers.get(self.JEDECmanufacturer)
117         if man==0:
118             man="UNKNOWN";
119         return man;
120     
121     def SPIjedecstr(self):
122         """Grab the JEDEC manufacturer string.  Call after SPIjedec()."""
123         man=self.JEDECmanufacturers.get(self.JEDECmanufacturer);
124         if man==0:
125             man="UNKNOWN";
126         device=self.JEDECdevices.get(self.JEDECdevice);
127         if device==0:
128             device="???"
129         return "%s %s" % (man,device);
130