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