at89s51dip40.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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_AT89S51dip40(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 = "DIP40",
  30. chipPinVCC = 40,
  31. chipPinsVPP = 31,
  32. chipPinGND = 20)
  33. self.flashPageSize = 0x2000
  34. self.flashPages = 1
  35. def __initChip(self):
  36. self.applyVCC(False)
  37. self.applyVPP(False)
  38. self.applyGND(True)
  39. self.top.cmdSetVCCVoltage(5)
  40. self.top.cmdSetVPPVoltage(5)
  41. def readSignature(self):
  42. self.__initChip()
  43. self.top.cmdEnableZifPullups(True)
  44. self.applyGND(True)
  45. self.applyVCC(True)
  46. self.top.cmdSetVPPVoltage(5)
  47. self.__loadCommand(5) # VPP on
  48. self.applyVPP(True)
  49. self.__loadCommand(1) # set nPROG
  50. self.__loadAddress(0x0100)
  51. self.__setPx()
  52. data = b""
  53. self.top.cmdFPGARead(0x10)
  54. self.__loadAddress(0x0200)
  55. self.top.cmdFPGARead(0x10)
  56. data += self.top.cmdReadBufferReg()
  57. self.applyVPP(False)
  58. self.__loadCommand(6) # VPP off
  59. signature = b""
  60. signature += int2byte(data[0])
  61. signature += int2byte(data[1])
  62. self.top.printInfo("Signature: %X, %X" % (byte2int(signature[0]), byte2int(signature[1])))
  63. return signature
  64. def erase(self):
  65. self.__initChip()
  66. self.applyGND(True)
  67. self.applyVCC(True)
  68. self.__loadCommand(1) # set P3.2
  69. self.top.cmdSetVPPVoltage(5)
  70. self.__loadCommand(5) # VPP on
  71. self.applyVPP(True)
  72. self.top.cmdDelay(0.5)
  73. self.__setPx(P26=1, P33=1)
  74. self.top.cmdSetVPPVoltage(12)
  75. self.__runCommandSync(3)
  76. self.top.cmdDelay(0.5)
  77. self.applyVPP(False)
  78. self.top.cmdSetVPPVoltage(5)
  79. self.__setPx()
  80. self.__loadCommand(6) # VPP off
  81. self.top.flushCommands()
  82. self.top.printInfo("{chipid}: Erasing flash, verifying ...".format(chipid=self.chipDescription.chipID))
  83. ok = self.__verifyErase()
  84. if ok == 0:
  85. self.top.printInfo("{chipid}: Erase done.".format(chipid=self.chipDescription.chipID))
  86. else:
  87. self.top.printInfo("{chipid}: Erase failed!".format(chipid=self.chipDescription.chipID))
  88. def readProgmem(self):
  89. self.__initChip()
  90. self.top.cmdEnableZifPullups(True)
  91. self.applyGND(True)
  92. self.applyVCC(True)
  93. self.top.cmdSetVPPVoltage(5)
  94. self.__loadCommand(5) # VPP on
  95. self.applyVPP(True)
  96. self.__loadCommand(1) # set nPROG
  97. self.__setPx(P36=1, P37=1)
  98. #self.__setPx(P26=1, P27=1, P36=1)
  99. #self.__setPx()
  100. image = b""
  101. byteCount = 0
  102. self.progressMeterInit("Reading Flash", self.flashPageSize*self.flashPages)
  103. for addr in range(0, self.flashPageSize*self.flashPages):
  104. self.progressMeter(addr)
  105. self.__loadAddress(addr)
  106. #self.__runCommandSync(4)
  107. self.top.cmdFPGARead(0x10)
  108. byteCount += 1
  109. if byteCount == self.top.getBufferRegSize():
  110. image += self.top.cmdReadBufferReg(byteCount)
  111. byteCount = 0
  112. image += self.top.cmdReadBufferReg(byteCount)
  113. self.applyVPP(False)
  114. self.__setPx()
  115. self.__loadCommand(6) # VPP off
  116. self.top.flushCommands()
  117. self.progressMeterFinish()
  118. return image
  119. def writeProgmem(self, image):
  120. if len(image) > self.flashPageSize*self.flashPages:
  121. self.throwError("Invalid FLASH image size %d (expected <=%d)" %\
  122. (len(image), self.flashPageSize*self.flashPages))
  123. self.__initChip()
  124. self.top.cmdEnableZifPullups(True)
  125. self.applyGND(True)
  126. self.applyVCC(True)
  127. self.__loadCommand(1) # set P3.2
  128. self.top.cmdSetVPPVoltage(5)
  129. self.__loadCommand(5) # VPP on
  130. self.applyVPP(True)
  131. self.__setPx(P27=1, P33=1, P36=1, P37=1)
  132. self.top.cmdSetVPPVoltage(12)
  133. self.progressMeterInit("Writing Flash", len(image))
  134. for addr in range(0, len(image)):
  135. self.progressMeter(addr)
  136. data = byte2int(image[addr])
  137. if data != 0xFF:
  138. self.__loadData(data)
  139. self.__loadAddress(addr)
  140. self.__loadCommand(3)
  141. ok = self.__progWait()
  142. if (ok & self.STAT_ERR) != 0:
  143. self.throwError("Write byte failed.")
  144. self.top.flushCommands()
  145. self.applyVPP(False)
  146. self.top.cmdSetVPPVoltage(5)
  147. self.__setPx()
  148. self.__loadCommand(6) # VPP off
  149. self.progressMeterFinish()
  150. ok = self.__verifyProgmem(image)
  151. if ok == 0:
  152. self.top.printInfo("{chipid}: Write flash done.".format(chipid = self.chipDescription.chipID))
  153. else:
  154. self.top.printInfo("{chipid}: Write flash failed!".format(chipid = self.chipDescription.chipID))
  155. def readLockbits(self):
  156. self.__initChip()
  157. self.top.cmdEnableZifPullups(True)
  158. self.applyGND(True)
  159. self.applyVCC(True)
  160. self.top.cmdSetVPPVoltage(5)
  161. self.__loadCommand(5) # VPP on
  162. self.applyVPP(True)
  163. self.__loadCommand(1) # set nPROG
  164. self.__setPx(P26=1, P27=1, P36=1)
  165. data = b""
  166. self.top.cmdFPGARead(0x10)
  167. data += self.top.cmdReadBufferReg()
  168. self.applyVPP(False)
  169. self.__loadCommand(6) # VPP off
  170. lockbits = b""
  171. lockbits += int2byte(data[0])
  172. return lockbits
  173. def writeLockbits(self, image):
  174. if len(image) != 1:
  175. self.throwError("Invalid lock-bits image size %d (expected %d)" %\
  176. (len(image), 1))
  177. self.progressMeterInit("Writing lock bits", 3)
  178. lbMask = 0x04
  179. lb=byte2int(image[0])
  180. if(lb & lbMask ):
  181. self.progressMeter(1)
  182. self.__writeLockbit(1)
  183. lbMask <<= 1
  184. if(lb & lbMask ):
  185. self.progressMeter(2)
  186. self.__writeLockbit(2)
  187. lbMask <<= 1
  188. if(lb & lbMask ):
  189. self.progressMeter(3)
  190. self.__writeLockbit(3)
  191. self.progressMeterFinish()
  192. def __writeLockbit(self, lb):
  193. self.__initChip()
  194. self.applyGND(True)
  195. self.applyVCC(True)
  196. self.__loadCommand(1)
  197. self.top.cmdSetVPPVoltage(5)
  198. self.__loadCommand(5) # VPP on
  199. self.applyVPP(True)
  200. self.top.cmdSetVPPVoltage(12)
  201. if(lb == 1 ):
  202. self.__setPx(P26=1, P27=1, P33=1, P36=1, P37=1)
  203. self.__runCommandSync(3)
  204. elif(lb == 2 ):
  205. self.__setPx(P26=1, P27=1, P33=1, P36=0, P37=0)
  206. self.__runCommandSync(3)
  207. elif(lb == 3 ):
  208. self.__setPx(P26=1, P27=0, P33=1, P36=1, P37=0)
  209. self.__runCommandSync(3)
  210. self.top.flushCommands()
  211. self.applyVPP(False)
  212. self.top.cmdSetVPPVoltage(5)
  213. self.__setPx()
  214. self.__loadCommand(6) # VPP off
  215. def __verifyErase(self):
  216. ok = 0
  217. image = self.readProgmem()
  218. for addr in range(0, self.flashPageSize*self.flashPages):
  219. if byte2int(image[addr]) != 0xFF:
  220. ok = 1
  221. return ok
  222. def __verifyProgmem(self,image):
  223. data = self.readProgmem()
  224. ok = 0
  225. for addr in range(0, len(image)-1):
  226. if byte2int(image[addr]) != byte2int(data[addr]):
  227. ok = 1
  228. return ok
  229. def __loadData(self, data):
  230. self.top.cmdFPGAWrite(0x10, data)
  231. def __loadAddress(self, addr):
  232. self.top.cmdFPGAWrite(0x11, addr & 0x00FF)
  233. self.top.cmdFPGAWrite(0x12, (addr >> 8) & 0x3FFF)
  234. def __loadCommand(self, command):
  235. self.top.cmdFPGAWrite(0x13, command & 0xFF)
  236. def __runCommandSync(self, command):
  237. self.__loadCommand(command)
  238. self.__busyWait()
  239. def __setPx(self, P26=0, P27=0, P33=0, P36=0, P37=0, nPSEN=0, RST=1):
  240. data = 0
  241. if P26:
  242. data |= 1
  243. if P27:
  244. data |= 2
  245. if P33:
  246. data |= 4
  247. if P36:
  248. data |= 8
  249. if P37:
  250. data |= 16
  251. if nPSEN:
  252. data |= 32
  253. if RST:
  254. data |= 64
  255. self.top.cmdFPGAWrite(0x16, data)
  256. def __getStatusFlags(self):
  257. self.top.cmdFPGARead(0x12)
  258. stat = self.top.cmdReadBufferReg()
  259. return byte2int(stat[0])
  260. def __busy(self):
  261. return bool(self.__getStatusFlags() & self.STAT_BUSY)
  262. def __busyWait(self):
  263. for i in range(0, 26):
  264. if not self.__busy():
  265. return
  266. self.top.hostDelay(0.001)
  267. self.throwError("Timeout in busywait.")
  268. def __progWait(self):
  269. for i in range(0,4):
  270. self.top.cmdFPGARead(0x12)
  271. stat = self.top.cmdReadBufferReg()
  272. if (byte2int(stat[0]) & self.STAT_BUSY) == 0:
  273. return byte2int(stat[0])
  274. self.top.hostDelay(0.001)
  275. self.throwError("Timeout in busywait.")
  276. lockbitDesc = (
  277. BitDescription(0, "Unused"),
  278. BitDescription(1, "Unused"),
  279. BitDescription(2, "BLB01 - further programming of flash mem. is disabled"),
  280. BitDescription(3, "BLB02 - verify disabled"),
  281. BitDescription(4, "BLB03 - external execution disabled"),
  282. BitDescription(5, "Unused"),
  283. BitDescription(6, "Unused"),
  284. BitDescription(7, "Unused"),
  285. BitDescription(8, "Unused"),
  286. )
  287. ChipDescription(
  288. Chip_AT89S51dip40,
  289. bitfile = "at89s5xdip40",
  290. chipID = "at89s51dip40",
  291. runtimeID = (0x0005, 0x01),
  292. chipVendors = "Atmel",
  293. description = "AT89S51",
  294. lockbitDesc = lockbitDesc,
  295. maintainer = None,
  296. packages = ( ("DIP40", ""), )
  297. )