Cleaned up LED stuff on 2xx msp430_init_dco().
[goodfet] / client / goodfet.spiflash
1 #!/usr/bin/env python
2
3 #GoodFET SPI Flash Client
4 #by Travis Goodspeed
5
6 #N.B.,
7 #Might be Winbond W25x80-specific.
8
9 import sys;
10 import binascii;
11 import array;
12
13 from GoodFETSPI import GoodFETSPIFlash;
14 from intelhex import IntelHex;
15
16 if(len(sys.argv)==1):
17     print "Usage: %s verb [objects]\n" % sys.argv[0];
18     print "%s info" % sys.argv[0];
19     print "%s dump $foo.rom [0x$start 0x$stop]" % sys.argv[0];
20     print "%s erase" % sys.argv[0];
21     print "%s flash $foo.rom [0x$start 0x$stop]" % sys.argv[0];
22     print "%s verify $foo.rom [0x$start 0x$stop]" % sys.argv[0];
23     print "%s peek 0x$start [0x$stop]" % sys.argv[0];
24     print "%s poke 0x$adr 0x$val" % sys.argv[0];
25     sys.exit();
26
27 #Initialize FET and set baud rate
28 client=GoodFETSPIFlash();
29 client.serInit()
30
31
32 client.SPIsetup();
33
34 #Dummy read.
35 #Might read as all ones if chip has a startup delay.
36 client.SPIjedec();
37
38 if(sys.argv[1]=="test"):
39     result="";
40     dropped=0;
41     for i in range(40):
42         data=client.SPIjedec();
43         if ord(data[1])==0xFF:
44             result+="-";
45             dropped=dropped+1;
46         else:
47             result+="+";
48     print "Connection Test: (- is bad)\n%s" % result;
49     print "%i misreads" % dropped;
50     if(dropped==40):
51         print "No successful reads.  Is the chip wired correctly?";
52     elif(dropped>0):
53         print "Some success, some failures.  Is a wire loose?";
54     else:
55         print "All reads succeeded.  Wiring is probably good.";
56     print "Erasing.";
57     client.SPIchiperase();
58     print "Testing erase.";
59     data=client.SPIpeekblock(0);
60     for i in data:
61         if ord(i)!=0xFF:
62             print "ERROR not properly erased!";
63     data=range(0,10);
64     client.SPIpokebytes(0,data);
65     print "Testing flash write.";
66     for i in data:
67         if(client.SPIpeek(i)!=i):
68             print "%06x not properly poked to %02x" % (i,i);
69     print "Test complete.";
70 if(sys.argv[1]=="info"):
71     data=client.SPIjedec();
72     print "Ident as %s\nManufacturer: %02x %s\nType: %02x\nCapacity: %02x (%i bytes)" % (
73         client.SPIjedecstr(),
74         ord(data[1]),client.SPIjedecmanstr(),
75         ord(data[2]),
76         ord(data[3]),
77         client.JEDECsize);
78
79 if(sys.argv[1]=="dump"):
80     f = sys.argv[2];
81     start=0x0000;
82     stop=client.JEDECsize;
83     if(len(sys.argv)>3):
84         start=int(sys.argv[3],16);
85     if(len(sys.argv)>4):
86         stop=int(sys.argv[4],16);
87     
88     print "Dumping code from %06x to %06x as %s." % (start,stop,f);
89     file = open(f, mode='wb')
90     
91     i=start;
92     while i<=stop:
93         data=client.SPIpeekblock(i);
94         #if(i%0x1000==0):
95         print "Dumped %06x."%i;
96         for j in data:
97             if i<stop: file.write(j);
98             i+=1;
99     file.close()
100
101 if(sys.argv[1]=="verify"):
102     f = sys.argv[2];
103     start=0x0000;
104     stop=client.JEDECsize;
105     if(len(sys.argv)>3):
106         start=int(sys.argv[3],16);
107     if(len(sys.argv)>4):
108         stop=int(sys.argv[4],16);
109     
110     print "Verifying code from %06x to %06x as %s." % (start,stop,f);
111     file = open(f, mode='rb')
112     
113     i=start;
114     bytes=0;
115     while i<=stop:
116         data=client.SPIpeekblock(i);
117         print "Verified %06x." % i;
118         for j in data:
119             if i<stop:
120                 #bits|=ord(file.read(1))^ord(j);
121                 a=ord(file.read(1));
122                 b=ord(j);
123                 if a!=b:
124                     print "%06x: %02x/%02x" % (i,a,b);
125                     bytes+=1;
126             i+=1;
127         if bytes!=0:
128             print "%i bytes don't match." % bytes
129
130     file.close()
131
132 if(sys.argv[1]=="flash"):
133     f = sys.argv[2];
134     start=0x0000;
135     stop=client.JEDECsize;
136     
137     if(len(sys.argv)>3):
138         start=int(sys.argv[3],16);
139     if(len(sys.argv)>4):
140         stop=int(sys.argv[4],16);
141     
142     print "Flashing code from %06x to %06x with %s." % (start,stop,f);
143     file = open(f, mode='rb')
144
145     i=start;
146     chars=list(file.read());
147     
148     #N.B., chunksize must be an even fraction of 0x100.
149     #Increasing above 0x200 doesn't help, 0x100 might be good enough.
150     chunksize=0x100;
151     #chunksize=0x1000;
152     
153     while i<=stop:
154         bytes=range(0,chunksize);
155         for j in range(0,chunksize):
156             bytes[j]=ord(chars[i+j]);
157         client.SPIpokebytes(i,bytes);
158         
159         i+=chunksize;
160         if(i%0x1000==0):
161             print "Flashed %06x."%i;
162     
163     
164     file.close()
165
166
167 if(sys.argv[1]=="erase"):
168   client.SPIchiperase();
169
170 if(sys.argv[1]=="peek"):
171     start=0x0000;
172     if(len(sys.argv)>2):
173         start=int(sys.argv[2],16);
174     stop=start;
175     if(len(sys.argv)>3):
176         stop=int(sys.argv[3],16);
177     print "Peeking from %06x to %06x." % (start,stop);
178     while start<=stop:
179         print "%06x: %02x" % (start,client.SPIpeek(start));
180         start=start+1;
181
182 if(sys.argv[1]=="poke"):
183     start=0x0000;
184     val=0x00;
185     if(len(sys.argv)>2):
186         start=int(sys.argv[2],16);
187     if(len(sys.argv)>3):
188         val=int(sys.argv[3],16);
189     print "Poking %06x to become %02x." % (start,val);
190     
191     while client.SPIpeek(start)!=val:
192         client.SPIpokebyte(start,val);
193         print "Poked to %02x" % client.SPIpeek(start);
194