chiark / gitweb /
Merge pull request #601 from CapnBry/reloadscene
[cura.git] / Cura / avr_isp / ispBase.py
1 """
2 General interface for Isp based AVR programmers.
3 The ISP AVR programmer can load firmware into AVR chips. Which are commonly used on 3D printers.
4
5  Needs to be subclassed to support different programmers.
6  Currently only the stk500v2 subclass exists.
7 """
8 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
9
10 import chipDB
11
12 class IspBase():
13         """
14         Base class for ISP based AVR programmers.
15         Functions in this class raise an IspError when something goes wrong.
16         """
17         def programChip(self, flashData):
18                 """ Program a chip with the given flash data. """
19                 self.curExtAddr = -1
20                 self.chip = chipDB.getChipFromDB(self.getSignature())
21                 if not self.chip:
22                         raise IspError("Chip with signature: " + str(self.getSignature()) + "not found")
23                 self.chipErase()
24                 
25                 print("Flashing %i bytes" % len(flashData))
26                 self.writeFlash(flashData)
27                 print("Verifying %i bytes" % len(flashData))
28                 self.verifyFlash(flashData)
29
30         def getSignature(self):
31                 """
32                 Get the AVR signature from the chip. This is a 3 byte array which describes which chip we are connected to.
33                 This is important to verify that we are programming the correct type of chip and that we use proper flash block sizes.
34                 """
35                 sig = []
36                 sig.append(self.sendISP([0x30, 0x00, 0x00, 0x00])[3])
37                 sig.append(self.sendISP([0x30, 0x00, 0x01, 0x00])[3])
38                 sig.append(self.sendISP([0x30, 0x00, 0x02, 0x00])[3])
39                 return sig
40         
41         def chipErase(self):
42                 """
43                 Do a full chip erase, clears all data, and lockbits.
44                 """
45                 self.sendISP([0xAC, 0x80, 0x00, 0x00])
46
47         def writeFlash(self, flashData):
48                 """
49                 Write the flash data, needs to be implemented in a subclass.
50                 """
51                 raise IspError("Called undefined writeFlash")
52
53         def verifyFlash(self, flashData):
54                 """
55                 Verify the flash data, needs to be implemented in a subclass.
56                 """
57                 raise IspError("Called undefined verifyFlash")
58
59 class IspError():
60         def __init__(self, value):
61                 self.value = value
62         def __str__(self):
63                 return repr(self.value)