AVR support for setting lock bits.
[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 erase(self):
71         """Erase the target chip."""
72         self.writecmd(self.AVRAPP,0xF0,0,None);
73     def lockbits(self):
74         """Read the target's lockbits."""
75         self.writecmd(self.AVRAPP,0x82,0,None);
76         return ord(self.data[0]);
77     def setlockbits(self,bits=0x00):
78         """Read the target's lockbits."""
79         self.writecmd(self.AVRAPP,0x92,1,[bits]);
80         return self.lockbits();
81     
82     def eeprompeek(self, adr):
83         """Read a byte of the target's EEPROM."""
84         self.writecmd(self.AVRAPP,0x81 ,2,
85                       [ (adr&0xFF), (adr>>8)]
86                       );#little-endian address
87         return ord(self.data[0]);
88     def flashpeek(self, adr):
89         """Read a byte of the target's EEPROM."""
90         self.writecmd(self.AVRAPP,0x02 ,2,
91                       [ (adr&0xFF), (adr>>8)]
92                       );#little-endian address
93         return ord(self.data[0]);
94     def flashpeekblock(self, adr):
95         """Read a byte of the target's EEPROM."""
96         self.writecmd(self.AVRAPP,0x02 ,4,
97                       [ (adr&0xFF), (adr>>8) &0xFF, 0x80, 0x00]
98                       );
99         return self.data;
100     
101     def eeprompoke(self, adr, val):
102         """Write a byte of the target's EEPROM."""
103         self.writecmd(self.AVRAPP,0x91 ,3,
104                       [ (adr&0xFF), (adr>>8), val]
105                       );#little-endian address
106         return ord(self.data[0]);
107     
108     def identstr(self):
109         """Return an identifying string."""
110         self.writecmd(self.AVRAPP,0x83,0, None);
111         vendor=self.AVRVendors.get(ord(self.data[0]));
112         deviceid=(ord(self.data[1])<<8)+ord(self.data[2]);
113         device=self.AVRDevices.get(deviceid);
114         
115         #Return hex if device is unknown.
116         #They are similar enough that it needn't be known.
117         if device==None:
118             device=("0x%04x" % deviceid);
119         
120         return "%s %s" % (vendor,device);