Support for debugging strings, closer to a clean MSP430X2 implementation.
[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                         };
34
35     JEDECdevices={0xFFFFFF: "MISSING",
36                   0xEF3015: "W25X16L",
37                   0xEF3014: "W25X80L",
38                   0xEF3013: "W25X40L",
39                   0xEF3012: "W25X20L",
40                   0xEF3011: "W25X10L",
41                   0xC22017: "MX25L6405D",
42                   0xC22016: "MX25L3205D",
43                   0xC22015: "MX25L1605D",
44                   0xC22014: "MX25L8005",
45                   0xC22013: "MX25L4005",
46                   0x204011: "M45PE10"
47                   };
48     
49     JEDECsizes={0x16: 0x800000,
50                 0x16: 0x400000,
51                 0x15: 0x200000,
52                 0x14: 0x100000,
53                 0x13: 0x080000,
54                 0x12: 0x040000,
55                 0x11: 0x020000}
56     JEDECsize=0;
57
58     def SPIjedec(self):
59         """Grab an SPI Flash ROM's JEDEC bytes."""
60         data=[0x9f, 0, 0, 0];
61         data=self.SPItrans(data);
62         #print "Manufacturer: %02x\nType: %02x\nCapacity: %02x" % (ord(data[1]),ord(data[2]),ord(data[3]));
63         self.JEDECmanufacturer=ord(data[1]);
64         self.JEDECtype=ord(data[2]);
65         self.JEDECcapacity=ord(data[3]);
66         self.JEDECsize=self.JEDECsizes.get(self.JEDECcapacity);
67         if self.JEDECsize==None:
68             self.JEDECsize=0;
69         self.JEDECdevice=(ord(data[1])<<16)+(ord(data[2])<<8)+ord(data[3]);
70         return data;
71     def SPIpeek(self,adr):
72         """Grab a byte from an SPI Flash ROM."""
73         data=[0x03,
74               (adr&0xFF0000)>>16,
75               (adr&0xFF00)>>8,
76               adr&0xFF,
77               0];
78         self.SPItrans(data);
79         return ord(self.data[4]);
80     def SPIpeekblock(self,adr,blocks=1):
81         """Grab a few block from an SPI Flash ROM.  Block size is unknown"""
82         data=[(adr&0xFF0000)>>16,
83               (adr&0xFF00)>>8,
84               adr&0xFF,
85               blocks];
86         
87         self.writecmd(0x01,0x02,4,data,blocks);
88         return self.data;
89     
90     def SPIpokebyte(self,adr,val):
91         self.SPIpokebytes(adr,[val]);
92     def SPIpokebytes(self,adr,data):
93         #self.SPIwriteenable();
94         adranddata=[(adr&0xFF0000)>>16,
95               (adr&0xFF00)>>8,
96               adr&0xFF
97               ]+data;
98         self.writecmd(0x01,0x03,
99                       len(adranddata),adranddata);
100         
101     def SPIchiperase(self):
102         """Mass erase an SPI Flash ROM."""
103         self.writecmd(0x01,0x81,0,[]);
104     def SPIwriteenable(self):
105         """SPI Flash Write Enable"""
106         data=[0x06];
107         self.SPItrans(data);
108         
109     def SPIjedecmanstr(self):
110         """Grab the JEDEC manufacturer string.  Call after SPIjedec()."""
111         man=self.JEDECmanufacturers.get(self.JEDECmanufacturer)
112         if man==0:
113             man="UNKNOWN";
114         return man;
115     
116     def SPIjedecstr(self):
117         """Grab the JEDEC manufacturer string.  Call after SPIjedec()."""
118         man=self.JEDECmanufacturers.get(self.JEDECmanufacturer);
119         if man==0:
120             man="UNKNOWN";
121         device=self.JEDECdevices.get(self.JEDECdevice);
122         if device==0:
123             device="???"
124         return "%s %s" % (man,device);
125