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