ispBase.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. Needs to be subclassed to support different programmers.
  5. Currently only the stk500v2 subclass exists.
  6. This is a python 3 conversion of the code created by David Braam for the Cura project.
  7. """
  8. from . import chipDB
  9. from UM.Logger import Logger
  10. class IspBase():
  11. """
  12. Base class for ISP based AVR programmers.
  13. Functions in this class raise an IspError when something goes wrong.
  14. """
  15. def programChip(self, flash_data):
  16. """ Program a chip with the given flash data. """
  17. self.cur_ext_addr = -1
  18. self.chip = chipDB.getChipFromDB(self.getSignature())
  19. if not self.chip:
  20. raise IspError("Chip with signature: " + str(self.getSignature()) + "not found")
  21. self.chipErase()
  22. Logger.log("d", "Flashing %i bytes", len(flash_data))
  23. self.writeFlash(flash_data)
  24. Logger.log("d", "Verifying %i bytes", len(flash_data))
  25. self.verifyFlash(flash_data)
  26. Logger.log("d", "Completed")
  27. def getSignature(self):
  28. """
  29. Get the AVR signature from the chip. This is a 3 byte array which describes which chip we are connected to.
  30. This is important to verify that we are programming the correct type of chip and that we use proper flash block sizes.
  31. """
  32. sig = []
  33. sig.append(self.sendISP([0x30, 0x00, 0x00, 0x00])[3])
  34. sig.append(self.sendISP([0x30, 0x00, 0x01, 0x00])[3])
  35. sig.append(self.sendISP([0x30, 0x00, 0x02, 0x00])[3])
  36. return sig
  37. def chipErase(self):
  38. """
  39. Do a full chip erase, clears all data, and lockbits.
  40. """
  41. self.sendISP([0xAC, 0x80, 0x00, 0x00])
  42. def writeFlash(self, flash_data):
  43. """
  44. Write the flash data, needs to be implemented in a subclass.
  45. """
  46. raise IspError("Called undefined writeFlash")
  47. def verifyFlash(self, flash_data):
  48. """
  49. Verify the flash data, needs to be implemented in a subclass.
  50. """
  51. raise IspError("Called undefined verifyFlash")
  52. class IspError(Exception):
  53. def __init__(self, value):
  54. self.value = value
  55. def __str__(self):
  56. return repr(self.value)