at89c2051dip20.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. """
  2. # TOP2049 Open Source programming suite
  3. #
  4. # Atmel AT89C2051 DIP20 Support
  5. #
  6. # Copyright (c) 2010 Guido
  7. # Copyright (c) 2010 Michael Buesch <m@bues.ch>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. """
  23. from libtoprammer.chip import *
  24. class Chip_AT89C2051dip20(Chip):
  25. STAT_BUSY = 0x01 # Programmer is running a command
  26. STAT_ERR = 0x02 # Error during write
  27. def __init__(self):
  28. Chip.__init__(self,
  29. chipPackage = "DIP20",
  30. chipPinVCC = 20,
  31. chipPinsVPP = 1,
  32. chipPinGND = 10)
  33. def __initChip(self):
  34. self.applyVCC(False)
  35. self.applyVPP(False)
  36. self.applyGND(True)
  37. self.top.cmdSetVCCVoltage(5)
  38. self.top.cmdSetVPPVoltage(5)
  39. def readSignature(self):
  40. self.__initChip()
  41. self.applyGND(True)
  42. self.applyVCC(True)
  43. self.top.cmdSetVPPVoltage(5)
  44. self.__loadCommand(5) # VPP on
  45. self.__loadCommand(1) # set P3.2
  46. self.__setP3x(P33=0, P34=0, P35=0, IA=0)
  47. data = b""
  48. self.top.cmdFPGARead(0x10)
  49. self.__setP3x(P33=0, P34=0, P35=0, IA=1)
  50. self.__setP3x(P33=0, P34=0, P35=0, IA=0)
  51. self.top.cmdFPGARead(0x10)
  52. self.__setP3x(P33=0, P34=0, P35=0, IA=1)
  53. self.__setP3x(P33=0, P34=0, P35=0, IA=0)
  54. self.top.cmdFPGARead(0x10)
  55. data += self.top.cmdReadBufferReg()
  56. self.__setP3x(P33=0, P34=1, P35=0, IA=0)
  57. self.__loadCommand(6) # VPP off
  58. signature = b""
  59. signature += int2byte(data[0])
  60. signature += int2byte(data[1])
  61. self.top.printInfo("Signature: %X, %X" % (byte2int(signature[0]), byte2int(signature[1])))
  62. return signature
  63. def erase(self):
  64. self.__initChip()
  65. self.applyGND(True)
  66. self.applyVCC(True)
  67. self.__loadCommand(1) # set P3.2
  68. self.top.cmdSetVPPVoltage(5)
  69. self.applyVPP(True)
  70. self.__loadCommand(5) # VPP on
  71. self.__setP3x(P33=1, P34=0, P35=0, IA=0)
  72. self.top.cmdSetVPPVoltage(12)
  73. self.__runCommandSync(4)
  74. self.applyVPP(False)
  75. self.top.cmdSetVPPVoltage(5)
  76. self.__setP3x(P33=0, P34=1, P35=0, IA=0)
  77. self.__loadCommand(5) # VPP off
  78. self.top.flushCommands()
  79. self.top.printInfo("at89c2051dip20: Erasing flash, verifying ...")
  80. ok = self.__verifyErase()
  81. if ok == 0:
  82. self.top.printInfo("at89c2051dip20: Erase done.")
  83. else:
  84. self.top.printInfo("at89c2051dip20: Erase failed!")
  85. def readProgmem(self):
  86. self.__initChip()
  87. self.applyGND(True)
  88. self.applyVCC(True)
  89. self.__loadCommand(1) # set P3.2
  90. self.top.cmdSetVPPVoltage(5)
  91. self.applyVPP(True)
  92. self.__loadCommand(5) # VPP on
  93. self.__setP3x(P33=0, P34=0, P35=1, IA=0)
  94. image = b""
  95. byteCount = 0
  96. self.progressMeterInit("Reading Flash", 0x800)
  97. for addr in range(0, 0x800):
  98. self.progressMeter(addr)
  99. self.top.cmdFPGARead(0x10)
  100. self.__setP3x(P33=0, P34=0, P35=1, IA=1)
  101. self.__setP3x(P33=0, P34=0, P35=1, IA=0)
  102. byteCount += 1
  103. if byteCount == self.top.getBufferRegSize():
  104. image += self.top.cmdReadBufferReg(byteCount)
  105. byteCount = 0
  106. image += self.top.cmdReadBufferReg(byteCount)
  107. self.applyVPP(False)
  108. self.__setP3x(P33=0, P34=1, P35=0, IA=0)
  109. self.__loadCommand(5) # VPP off
  110. self.top.flushCommands()
  111. self.progressMeterFinish()
  112. return image
  113. def writeProgmem(self, image):
  114. if len(image) > 0x800:
  115. self.throwError("Invalid EPROM image size %d (expected <=%d)" %\
  116. (len(image), 0x800))
  117. self.__initChip()
  118. self.applyGND(True)
  119. self.applyVCC(True)
  120. self.__loadCommand(1) # set P3.2
  121. self.top.cmdSetVPPVoltage(5)
  122. self.applyVPP(True)
  123. self.__loadCommand(5) # VPP on
  124. self.__setP3x(P33=0, P34=1, P35=1, IA=0)
  125. self.top.cmdSetVPPVoltage(12)
  126. self.progressMeterInit("Writing Flash", len(image))
  127. for addr in range(0, len(image)):
  128. self.progressMeter(addr)
  129. data = byte2int(image[addr])
  130. if data != 0xFF:
  131. self.__loadData(data)
  132. self.__loadCommand(3)
  133. ok = self.__progWait()
  134. if (ok & self.STAT_ERR) != 0:
  135. self.throwError("Write byte failed.")
  136. self.__setP3x(P33=0, P34=1, P35=1, IA=1)
  137. self.__setP3x(P33=0, P34=1, P35=1, IA=0)
  138. self.applyVPP(False)
  139. self.top.cmdSetVPPVoltage(5)
  140. self.__setP3x(P33=0, P34=1, P35=0, IA=0)
  141. self.__loadCommand(5) # VPP off
  142. self.top.flushCommands()
  143. self.progressMeterFinish()
  144. ok = self.__verifyProgmem(image)
  145. if ok == 0:
  146. self.top.printInfo("at89c2051dip20: Write flash done.")
  147. else:
  148. self.top.printInfo("at89c2051dip20: Write flash failed!")
  149. def __verifyErase(self):
  150. ok = 0
  151. image = self.readProgmem()
  152. for addr in range(0, 0x800):
  153. if byte2int(image[addr]) != 0xFF:
  154. ok = 1
  155. return ok
  156. def __verifyProgmem(self,image):
  157. data = self.readProgmem()
  158. ok = 0
  159. for addr in range(0, 0x800):
  160. if byte2int(image[addr]) != byte2int(data[addr]):
  161. ok = 1
  162. return ok
  163. def __loadData(self, data):
  164. self.top.cmdFPGAWrite(0x10, data)
  165. def __loadCommand(self, command):
  166. self.top.cmdFPGAWrite(0x12, command & 0xFF)
  167. def __runCommandSync(self, command):
  168. self.__loadCommand(command)
  169. self.__busyWait()
  170. def __setP3x(self, P33, P34, P35, IA):
  171. data = 0
  172. if P33:
  173. data |= 1
  174. if P34:
  175. data |= 2
  176. if P35:
  177. data |= 4
  178. if IA:
  179. data |= 8
  180. self.top.cmdFPGAWrite(0x16, data)
  181. def __getStatusFlags(self):
  182. self.top.cmdFPGARead(0x12)
  183. stat = self.top.cmdReadBufferReg()
  184. return byte2int(stat[0])
  185. def __busy(self):
  186. return bool(self.__getStatusFlags() & self.STAT_BUSY)
  187. def __busyWait(self):
  188. for i in range(0, 26):
  189. if not self.__busy():
  190. return
  191. self.top.hostDelay(0.001)
  192. self.throwError("Timeout in busywait.")
  193. def __progWait(self):
  194. for i in range(0,4):
  195. self.top.cmdFPGARead(0x12)
  196. stat = self.top.cmdReadBufferReg()
  197. if (byte2int(stat[0]) & self.STAT_BUSY) == 0:
  198. return byte2int(stat[0])
  199. self.top.hostDelay(0.001)
  200. self.throwError("Timeout in busywait.")
  201. ChipDescription(
  202. Chip_AT89C2051dip20,
  203. bitfile = "at89c2051dip20",
  204. runtimeID = (0x0005, 0x01),
  205. chipVendors = "Atmel",
  206. description = "AT89C2051",
  207. maintainer = None,
  208. packages = ( ("DIP20", ""), )
  209. )