readChunk (may replace readMem) is implemented, moving memory into regs then reading...
[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     
85     def setup(self):
86         """Move the FET into the AVR application."""
87         self.writecmd(self.AVRAPP,0x10,0,self.data); #SPI/SETUP
88     
89     def trans(self,data):
90         """Exchange data by AVR.
91         Input should probably be 4 bytes."""
92         self.data=data;
93         self.writecmd(self.AVRAPP,0x00,len(data),data);
94         return self.data;
95
96     def start(self):
97         """Start the connection."""
98         self.writecmd(self.AVRAPP,0x20,0,None);
99     def forcestart(self):
100         """Forcibly start a connection."""
101         
102         for i in range(0x880,0xfff):
103             #self.glitchVoltages(0x880, i);
104             self.start();
105             bits=self.lockbits();
106             print "At %04x, Lockbits: %02x" % (i,bits);
107             if(bits==0xFF): return;
108     def erase(self):
109         """Erase the target chip."""
110         self.writecmd(self.AVRAPP,0xF0,0,None);
111     def lockbits(self):
112         """Read the target's lockbits."""
113         self.writecmd(self.AVRAPP,0x82,0,None);
114         return ord(self.data[0]);
115     def setlockbits(self,bits=0x00):
116         """Read the target's lockbits."""
117         self.writecmd(self.AVRAPP,0x92,1,[bits]);
118         return self.lockbits();
119     def lock(self):
120         self.setlockbits(0xFC);
121     def eeprompeek(self, adr):
122         """Read a byte of the target's EEPROM."""
123         self.writecmd(self.AVRAPP,0x81 ,2,
124                       [ (adr&0xFF), (adr>>8)]
125                       );#little-endian address
126         return ord(self.data[0]);
127     def flashpeek(self, adr):
128         """Read a byte of the target's Flash memory."""
129         self.writecmd(self.AVRAPP,0x02 ,2,
130                       [ (adr&0xFF), (adr>>8)]
131                       );#little-endian address
132         return ord(self.data[0]);
133     def flashpeekblock(self, adr):
134         """Read a byte of the target's Flash memory."""
135         self.writecmd(self.AVRAPP,0x02 ,4,
136                       [ (adr&0xFF), (adr>>8) &0xFF, 0x80, 0x00]
137                       );
138         return self.data;
139     
140     def eeprompoke(self, adr, val):
141         """Write a byte of the target's EEPROM."""
142         self.writecmd(self.AVRAPP,0x91 ,3,
143                       [ (adr&0xFF), (adr>>8), val]
144                       );#little-endian address
145         return ord(self.data[0]);
146     
147     def identstr(self):
148         """Return an identifying string."""
149         self.writecmd(self.AVRAPP,0x83,0, None);
150         vendor=self.AVRVendors.get(ord(self.data[0]));
151         deviceid=(ord(self.data[1])<<8)+ord(self.data[2]);
152         device=self.AVRDevices.get(deviceid);
153         
154         #Return hex if device is unknown.
155         #They are similar enough that it needn't be known.
156         if device==None:
157             device=("0x%04x" % deviceid);
158         
159         return "%s %s" % (vendor,device);