Support for dumping AVR Flash.
[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         
29         0x9001: "S1200",
30         
31         0x9101: "S1213",
32         0x9102: "S2323",
33         0x9105: "S2333",
34         0x9103: "S2343",
35         
36         0x9201: "S4414",
37         0x9203: "S4433",
38         0x9202: "S4434",
39         
40         0x9301: "S8515",
41         0x9303: "S8535",
42         
43         0x9305: "mega83",
44         0x930a: "mega88",
45         0x9701: "mega103",
46         0x9401: "mega161",
47         0x9402: "mega163",
48         0x9406: "mega168",
49         
50         0x950f: "mega328",
51         0x950d: "mega325",
52         0x9508: "mega32"
53         };
54     
55     def setup(self):
56         """Move the FET into the SPI application."""
57         self.writecmd(self.AVRAPP,0x10,0,self.data); #SPI/SETUP
58     
59     def trans(self,data):
60         """Exchange data by AVR.
61         Input should probably be 4 bytes."""
62         self.data=data;
63         self.writecmd(self.AVRAPP,0x00,len(data),data);
64         return self.data;
65
66     def start(self):
67         """Start the connection."""
68         self.writecmd(self.AVRAPP,0x20,0,None);
69     def erase(self):
70         """Erase the target chip."""
71         self.writecmd(self.AVRAPP,0xF0,0,None);
72     def lockbits(self):
73         """Read the target's lockbits."""
74         self.writecmd(self.AVRAPP,0x82,0,None);
75         return ord(self.data[0]);
76     def eeprompeek(self, adr):
77         """Read a byte of the target's EEPROM."""
78         self.writecmd(self.AVRAPP,0x81 ,2,
79                       [ (adr&0xFF), (adr>>8)]
80                       );#little-endian address
81         return ord(self.data[0]);
82     def flashpeek(self, adr):
83         """Read a byte of the target's EEPROM."""
84         self.writecmd(self.AVRAPP,0x02 ,2,
85                       [ (adr&0xFF), (adr>>8)]
86                       );#little-endian address
87         return ord(self.data[0]);
88     def flashpeekblock(self, adr):
89         """Read a byte of the target's EEPROM."""
90         self.writecmd(self.AVRAPP,0x02 ,4,
91                       [ (adr&0xFF), (adr>>8) &0xFF, 0x80, 0x00]
92                       );
93         return self.data;
94     
95     def eeprompoke(self, adr, val):
96         """Write a byte of the target's EEPROM."""
97         self.writecmd(self.AVRAPP,0x91 ,3,
98                       [ (adr&0xFF), (adr>>8), val]
99                       );#little-endian address
100         return ord(self.data[0]);
101     
102     def identstr(self):
103         """Return an identifying string."""
104         self.writecmd(self.AVRAPP,0x83,0, None);
105         vendor=self.AVRVendors.get(ord(self.data[0]));
106         deviceid=(ord(self.data[1])<<8)+ord(self.data[2]);
107         device=self.AVRDevices.get(deviceid);
108         
109         #Return hex if device is unknown.
110         #They are similar enough that it needn't be known.
111         if device==None:
112             device=("0x%04x" % deviceid);
113         
114         return device;