Might fix pagesize issue for some Chipcon devices.
[goodfet] / client / GoodFETAVR.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 GoodFETAVR(GoodFET):
13     AVRAPP=0x32;
14     AVRVendors={0x1E: "Atmel",
15                 0x00: "Locked",
16                 };
17     
18     #List from avr910.asm and other sources.
19     #More devices at http://avr.fenceline.de/device_data.html
20     AVRDevices={
21         0x9003: "tiny10",
22         0x9004: "tiny11",
23         0x9005: "tiny12",
24         0x9006: "tiny15",
25         0x9007: "tiny13",
26         0x9108: "tiny25",
27         0x930B: "tiny85",
28         0x9206: "tiny45",
29         
30         0x9001: "S1200",
31         
32         0x9101: "S1213",
33         0x9102: "S2323",
34         0x9105: "S2333",
35         0x9103: "S2343",
36         
37         0x9201: "S4414",
38         0x9203: "S4433",
39         0x9202: "S4434",
40         
41         0x9301: "S8515",
42         0x9303: "S8535",
43         
44         0x9305: "mega83",
45         0x930a: "mega88",
46         0x9701: "mega103",
47         0x9401: "mega161",
48         0x9402: "mega163",
49         0x9406: "mega168",
50         
51         0x950f: "mega328",
52         0x950d: "mega325",
53         0x9508: "mega32"
54         };
55     
56     def setup(self):
57         """Move the FET into the SPI application."""
58         self.writecmd(self.AVRAPP,0x10,0,self.data); #SPI/SETUP
59     
60     def trans(self,data):
61         """Exchange data by AVR.
62         Input should probably be 4 bytes."""
63         self.data=data;
64         self.writecmd(self.AVRAPP,0x00,len(data),data);
65         return self.data;
66
67     def start(self):
68         """Start the connection."""
69         self.writecmd(self.AVRAPP,0x20,0,None);
70     def glitchstart(self):
71         """Glitch into the AVR application."""
72         self.glitchVerb(self.AVRAPP,0x20,None);
73     def glitchstarttime(self):
74         """Measure the timer of the START verb."""
75         return self.glitchTime(self.AVRAPP,0x20,None);
76     def forcestart(self):
77         """Forcibly start a connection."""
78         
79         for i in range(0x880,0xfff):
80             #self.glitchVoltages(0x880, i);
81             self.start();
82             bits=self.lockbits();
83             print "At %04x, Lockbits: %02x" % (i,bits);
84             if(bits==0xFF): return;
85     def erase(self):
86         """Erase the target chip."""
87         self.writecmd(self.AVRAPP,0xF0,0,None);
88     def lockbits(self):
89         """Read the target's lockbits."""
90         self.writecmd(self.AVRAPP,0x82,0,None);
91         return ord(self.data[0]);
92     def setlockbits(self,bits=0x00):
93         """Read the target's lockbits."""
94         self.writecmd(self.AVRAPP,0x92,1,[bits]);
95         return self.lockbits();
96     
97     def eeprompeek(self, adr):
98         """Read a byte of the target's EEPROM."""
99         self.writecmd(self.AVRAPP,0x81 ,2,
100                       [ (adr&0xFF), (adr>>8)]
101                       );#little-endian address
102         return ord(self.data[0]);
103     def flashpeek(self, adr):
104         """Read a byte of the target's EEPROM."""
105         self.writecmd(self.AVRAPP,0x02 ,2,
106                       [ (adr&0xFF), (adr>>8)]
107                       );#little-endian address
108         return ord(self.data[0]);
109     def flashpeekblock(self, adr):
110         """Read a byte of the target's EEPROM."""
111         self.writecmd(self.AVRAPP,0x02 ,4,
112                       [ (adr&0xFF), (adr>>8) &0xFF, 0x80, 0x00]
113                       );
114         return self.data;
115     
116     def eeprompoke(self, adr, val):
117         """Write a byte of the target's EEPROM."""
118         self.writecmd(self.AVRAPP,0x91 ,3,
119                       [ (adr&0xFF), (adr>>8), val]
120                       );#little-endian address
121         return ord(self.data[0]);
122     
123     def identstr(self):
124         """Return an identifying string."""
125         self.writecmd(self.AVRAPP,0x83,0, None);
126         vendor=self.AVRVendors.get(ord(self.data[0]));
127         deviceid=(ord(self.data[1])<<8)+ord(self.data[2]);
128         device=self.AVRDevices.get(deviceid);
129         
130         #Return hex if device is unknown.
131         #They are similar enough that it needn't be known.
132         if device==None:
133             device=("0x%04x" % deviceid);
134         
135         return "%s %s" % (vendor,device);