GoodFETCCSPI.status() works, part of 'goodfet.ccspi info'
[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     APP=AVRAPP;
15     AVRVendors={0x1E: "Atmel",
16                 0x00: "Locked",
17                 };
18     
19     #List imported from http://avr.fenceline.de/device_data.html
20     AVRDevices={
21         0x9003: "ATtiny10",
22         0x9004: "ATtiny11",
23         0x9005: "ATtiny12",
24         0x9007: "ATtiny13",
25         0x9006: "ATtiny15",
26         0x9106: "ATtiny22",
27         0x910A: "ATtiny2313",
28         0x9108: "ATtiny25",
29         0x9109: "ATtiny26",
30         0x9107: "ATtiny28",
31         0x9206: "ATtiny45",
32         0x930B: "ATtiny85",
33         0x9304: "AT90C8534",
34         0x9001: "AT90S1200",
35         0x9101: "AT90S2313",
36         0x9102: "AT90S2323",
37         0x9105: "AT90S2333",
38         0x9103: "AT90S2343",
39         0x9201: "AT90S4414",
40         0x9203: "AT90S4433",
41         0x9202: "AT90S4434",
42         0x9301: "AT90S8515",
43         0x9303: "AT90S8535",
44         0x9381: "AT90PWM2",
45         0x9381: "AT90PWM3",
46         0x9781: "AT90CAN128",
47         0x9205: "ATmega48",
48         0x9306: "ATmega8515",
49         0x9308: "ATmega8535",
50         0x9307: "ATmega8",
51         0x930A: "ATmega88",
52         0x9403: "ATmega16",
53         0x9401: "ATmega161",
54         0x9404: "ATmega162",
55         0x9402: "ATmega163",
56         0x9407: "ATmega165",
57         0x9406: "ATmega168",
58         0x9405: "ATmega169",
59         0x9502: "ATmega32",
60         0x9501: "ATmega323",
61         0x9503: "ATmega325",
62         0x9504: "ATmega3250",
63         0x9503: "ATmega329",
64         0x9504: "ATmega3290",
65         0x9507: "ATmega406",
66         0x9602: "ATmega64",
67         0x9607: "ATmega640",
68         0x9603: "ATmega645",
69         0x9604: "ATmega6450",
70         0x9603: "ATmega649",
71         0x9604: "ATmega6490",
72         0x0101: "ATmega103",
73         0x9701: "ATmega103",
74         0x9702: "ATmega128",
75         0x9703: "ATmega1280",
76         0x9704: "ATmega1281",
77         0x9801: "ATmega2560",
78         0x9802: "ATmega2561",
79         0x9002: "ATtiny19",
80         0x9302: "ATmega85",
81         0x9305: "ATmega83",
82         0x9601: "ATmega603",
83         
84         #These are missing from the Fenceline DB.
85         0x960a: "ATmega644P",
86         };
87     
88     def setup(self):
89         """Move the FET into the AVR application."""
90         self.writecmd(self.AVRAPP,0x10,0,self.data); #SPI/SETUP
91     
92     def trans(self,data):
93         """Exchange data by AVR.
94         Input should probably be 4 bytes."""
95         self.data=data;
96         self.writecmd(self.AVRAPP,0x00,len(data),data);
97         return self.data;
98
99     def start(self):
100         """Start the connection."""
101         self.writecmd(self.AVRAPP,0x20,0,None);
102     def forcestart(self):
103         """Forcibly start a connection."""
104         
105         for i in range(0x880,0xfff):
106             #self.glitchVoltages(0x880, i);
107             self.start();
108             bits=self.lockbits();
109             print "At %04x, Lockbits: %02x" % (i,bits);
110             if(bits==0xFF): return;
111     def erase(self):
112         """Erase the target chip."""
113         self.writecmd(self.AVRAPP,0xF0,0,None);
114     def lockbits(self):
115         """Read the target's lockbits."""
116         self.writecmd(self.AVRAPP,0x82,0,None);
117         return ord(self.data[0]);
118     def setlockbits(self,bits=0x00):
119         """Read the target's lockbits."""
120         self.writecmd(self.AVRAPP,0x92,1,[bits]);
121         return self.lockbits();
122     def lock(self):
123         self.setlockbits(0xFC);
124     def eeprompeek(self, adr):
125         """Read a byte of the target's EEPROM."""
126         self.writecmd(self.AVRAPP,0x81 ,2,
127                       [ (adr&0xFF), (adr>>8)]
128                       );#little-endian address
129         return ord(self.data[0]);
130     def flashpeek(self, adr):
131         """Read a byte of the target's Flash memory."""
132         self.writecmd(self.AVRAPP,0x02 ,2,
133                       [ (adr&0xFF), (adr>>8)]
134                       );#little-endian address
135         return ord(self.data[0]);
136     def flashpeekblock(self, adr):
137         """Read a byte of the target's Flash memory."""
138         self.writecmd(self.AVRAPP,0x02 ,4,
139                       [ (adr&0xFF), (adr>>8) &0xFF, 0x80, 0x00]
140                       );
141         return self.data;
142     
143     def eeprompoke(self, adr, val):
144         """Write a byte of the target's EEPROM."""
145         self.writecmd(self.AVRAPP,0x91 ,3,
146                       [ (adr&0xFF), (adr>>8), val]
147                       );#little-endian address
148         return ord(self.data[0]);
149     
150     def identstr(self):
151         """Return an identifying string."""
152         self.writecmd(self.AVRAPP,0x83,0, None);
153         vendor=self.AVRVendors.get(ord(self.data[0]));
154         deviceid=(ord(self.data[1])<<8)+ord(self.data[2]);
155         device=self.AVRDevices.get(deviceid);
156         
157         #Return hex if device is unknown.
158         #They are similar enough that it needn't be known.
159         if device==None:
160             device=("0x%04x" % deviceid);
161         
162         return "%s %s" % (vendor,device);