m8cissp.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. """
  2. # TOP2049 Open Source programming suite
  3. #
  4. # Cypress M8C In System Serial Programmer
  5. #
  6. # Copyright (c) 2010-2011 Michael Buesch <m@bues.ch>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. """
  22. from libtoprammer.chip import *
  23. import time
  24. class Chip_M8C_ISSP(Chip):
  25. ISSPCMD_POR = 1 # Perform a power-on-reset
  26. ISSPCMD_PWROFF = 2 # Turn power off
  27. ISSPCMD_EXEC = 3 # Do an "execute" transfer
  28. STAT_BUSY0 = 0x01
  29. STAT_BUSY1 = 0x02
  30. STAT_ISSPSTATE = 0x1C
  31. STAT_ISSPSTATE_SHIFT = 2
  32. STAT_SDATA = 0x20
  33. STRVEC_INIT1 = (
  34. "1100101010000000000111",
  35. "0000000000000000000000",
  36. "0000000000000000000000",
  37. "0000000000000000000000",
  38. "0000000000000000000000",
  39. "0000000000000000000000",
  40. "1101111011100010000111",
  41. "1101111101000000000111",
  42. "1101111011100000000111",
  43. "1101111011100010000111",
  44. "1101111111000000100111",
  45. "1101110001000000100111",
  46. "1101110000000000011111",
  47. "1101111011100000000111",
  48. "1001111100000111010111",
  49. "1001111100100000011111",
  50. "1001111101101000000111",
  51. "1001111110000000000111",
  52. "1001111111001010110111",
  53. "1001111110100000001111",
  54. "1001111111100000001111",
  55. "1001111111110000000111",
  56. "1101111011100010000111",
  57. "1101110001000000000111",
  58. "1101111111000000000111",
  59. "1101110000000000000111",
  60. "1101111011100000000111",
  61. "1101111010000000011111",
  62. "1101111010100000000111",
  63. "1101111011000000000111",
  64. "1101111100000000000111",
  65. "1101111100100110000111",
  66. )
  67. STRVEC_INIT2 = (
  68. "1001111101000000000111",
  69. "1101111000000000110111",
  70. "1101111100000000000111",
  71. "1101111111100010010111",
  72. )
  73. STRVEC_IDSETUP = (
  74. "1101111011100010000111",
  75. "1101110000000000010111",
  76. "1101111011100010000111",
  77. "1101111101000000000111",
  78. "1101111011100000000111",
  79. "1101111011100010000111",
  80. "1101111111000000100111",
  81. "1101110001000000100111",
  82. "1101110000000000011111",
  83. "1101111011100000000111",
  84. "1001111100000111010111",
  85. "1001111100100000011111",
  86. "1001111101101000000111",
  87. "1001111110000000000111",
  88. "1001111111001010110111",
  89. "1001111110100000001111",
  90. "1001111111100000001111",
  91. "1001111111110000000111",
  92. "1101111011100010000111",
  93. "1101110001000000000111",
  94. "1101111111000000000111",
  95. "1101110000000000000111",
  96. "1101111011100000000111",
  97. "1101111010000000011111",
  98. "1101111010100000000111",
  99. "1101111011000000000111",
  100. "1101111100000000000111",
  101. "1101111100100110000111",
  102. "1001111101000000000111",
  103. "1101111000000000110111",
  104. "1101111100000000000111",
  105. "1101111111100010010111",
  106. )
  107. STRVEC_READBYTE = (
  108. "101aaaaaaaaZDDDDDDDDZ1",
  109. )
  110. STRVEC_WRITEBYTE = (
  111. "100aaaaaaaadddddddd111",
  112. )
  113. STRVEC_ERASEALL = (
  114. "1001111110000010101111",
  115. "1001111111001010110111",
  116. "1101111011100010000111",
  117. "1101111101000000000111",
  118. "1101111011100000000111",
  119. "1101111011100010000111",
  120. "1101111111000000100111",
  121. "1101110001000000100111",
  122. "1101110000000000011111",
  123. "1101111011100000000111",
  124. "1001111100000111010111",
  125. "1001111100100000011111",
  126. "1001111101101000000111",
  127. "1001111110000000000111",
  128. "1001111111001010110111",
  129. "1001111110100000001111",
  130. "1001111111100000001111",
  131. "1001111111110000000111",
  132. "1101111011100010000111",
  133. "1101110001000000000111",
  134. "1101111111000000000111",
  135. "1101110000000000000111",
  136. "1101111011100000000111",
  137. "1101111010000000011111",
  138. "1101111010100000000111",
  139. "1101111011000000000111",
  140. "1101111100000000000111",
  141. "1101111100100110000111",
  142. "1101111000000000101111",
  143. "1101111100000000000111",
  144. "1101111111100010010111",
  145. )
  146. STRVEC_SETBLKNUM = (
  147. "10011111010dddddddd111",
  148. )
  149. STRVEC_READBLK = (
  150. "1101111011100010000111",
  151. "1101111101000000000111",
  152. "1101111011100000000111",
  153. "1101111011100010000111",
  154. "1101111111000000100111",
  155. "1101110001000000100111",
  156. "1101110000000000011111",
  157. "1101111011100000000111",
  158. "1001111100000111010111",
  159. "1001111100100000011111",
  160. "1001111101101000000111",
  161. "1001111110000000000111",
  162. "1001111111001010110111",
  163. "1001111110100000001111",
  164. "1001111111100000001111",
  165. "1001111111110000000111",
  166. "1101111011100010000111",
  167. "1101110001000000000111",
  168. "1101111111000000000111",
  169. "1101110000000000000111",
  170. "1101111011100000000111",
  171. "1101111010000000011111",
  172. "1101111010100000000111",
  173. "1101111011000000000111",
  174. "1101111100000000000111",
  175. "1101111100100110000111",
  176. "1101111000000000001111",
  177. "1101111100000000000111",
  178. "1101111111100010010111",
  179. )
  180. STRVEC_WRITEBLK = (
  181. "1001111110001010100111",
  182. "1001111111001010110111",
  183. "1101111011100010000111",
  184. "1101111101000000000111",
  185. "1101111011100000000111",
  186. "1101111011100010000111",
  187. "1101111111000000100111",
  188. "1101110001000000100111",
  189. "1101110000000000011111",
  190. "1101111011100000000111",
  191. "1001111100000111010111",
  192. "1001111100100000011111",
  193. "1001111101101000000111",
  194. "1001111110000000000111",
  195. "1001111111001010110111",
  196. "1001111110100000001111",
  197. "1001111111100000001111",
  198. "1001111111110000000111",
  199. "1101111011100010000111",
  200. "1101110001000000000111",
  201. "1101111111000000000111",
  202. "1101110000000000000111",
  203. "1101111011100000000111",
  204. "1101111010000000011111",
  205. "1101111010100000000111",
  206. "1101111011000000000111",
  207. "1101111100000000000111",
  208. "1101111100100110000111",
  209. "1101111000000000010111",
  210. "1101111100000000000111",
  211. "1101111111100010010111",
  212. )
  213. STRVEC_READCHKSUM = (
  214. "10111111001ZDDDDDDDDZ1",
  215. "10111111000ZDDDDDDDDZ1",
  216. )
  217. STRVEC_READID = (
  218. "10111111000ZDDDDDDDDZ1",
  219. "10111111001ZDDDDDDDDZ1",
  220. )
  221. def __init__(self):
  222. Chip.__init__(self)
  223. # self.progmemSize = 1024 * 16
  224. self.progmemSize = 256#XXX
  225. def readSignature(self):
  226. self.progressMeterInit("Reading chip ID", 0)
  227. self.__powerOnReset()
  228. gotID = self.__readID()
  229. self.progressMeterFinish()
  230. return int2byte(gotID & 0xFF) + int2byte((gotID >> 8) & 0xFF)
  231. def erase(self):
  232. self.progressMeterInit("Erasing chip", 0)
  233. self.__powerOnReset()
  234. self.__bitbangStringVectors(self.STRVEC_ERASEALL)
  235. self.__runCommandSync(self.ISSPCMD_EXEC)
  236. self.progressMeterFinish()
  237. def writeProgmem(self, image):
  238. if len(image) > self.progmemSize or len(image) % 64 != 0:
  239. self.throwError("Invalid program memory image size %d "
  240. "(expected <=%d and multiple of 64)" %\
  241. (len(image), self.progmemSize))
  242. self.progressMeterInit("Writing program memory", len(image))
  243. self.__powerOnReset()
  244. for blknum in range(0, len(image) // 64):
  245. for i in range(0, 64):
  246. self.progressMeter(blknum * 64 + i)
  247. self.__writeByte(i, byte2int(image[blknum * 64 + i]))
  248. vec = self.__stringVectorReplace(self.STRVEC_SETBLKNUM[0], "d", blknum)
  249. self.__bitbangStringVector(vec)
  250. self.__bitbangStringVectors(self.STRVEC_WRITEBLK)
  251. self.__runCommandSync(self.ISSPCMD_EXEC)
  252. self.progressMeterFinish()
  253. def readProgmem(self):
  254. self.progressMeterInit("Reading program memory", self.progmemSize)
  255. self.__powerOnReset()
  256. assert(self.progmemSize % 64 == 0)
  257. image = []
  258. for blknum in range(0, self.progmemSize // 64):
  259. vec = self.__stringVectorReplace(self.STRVEC_SETBLKNUM[0], "d", blknum)
  260. self.__bitbangStringVector(vec)
  261. self.__bitbangStringVectors(self.STRVEC_READBLK)
  262. self.__runCommandSync(self.ISSPCMD_EXEC)
  263. for i in range(0, 64):
  264. self.progressMeter(blknum * 64 + i)
  265. image.append(int2byte(self.__readByte(i)))
  266. #FIXME return_code
  267. self.progressMeterFinish()
  268. return b"".join(image)
  269. def __powerDown(self):
  270. "Turn the power to the device off"
  271. self.printDebug("Powering device down...")
  272. self.__runCommandSync(self.ISSPCMD_PWROFF)
  273. self.top.hostDelay(5)
  274. def __powerOnReset(self):
  275. "Perform a complete power-on-reset and initialization"
  276. self.top.vcc.setLayoutMask(0)
  277. self.top.vpp.setLayoutMask(0)
  278. self.top.gnd.setLayoutMask(0)
  279. self.top.cmdSetVCCVoltage(5)
  280. self.top.cmdSetVPPVoltage(5)
  281. self.printDebug("Initializing supply power...")
  282. self.top.gnd.setLayoutPins( (20,) )
  283. self.top.vcc.setLayoutPins( (21,) )
  284. #FIXME when to do exec?
  285. self.__powerDown()
  286. self.printDebug("Performing a power-on-reset...")
  287. self.__uploadStringVector(self.STRVEC_INIT1[0])
  288. self.__runCommandSync(self.ISSPCMD_POR)
  289. self.printDebug("Sending vector 1...")
  290. self.__bitbangStringVectors(self.STRVEC_INIT1[1:])
  291. #XXX self.__runCommandSync(self.ISSPCMD_EXEC)
  292. self.printDebug("Sending vector 2...")
  293. self.__bitbangStringVectors(self.STRVEC_INIT2)
  294. self.__runCommandSync(self.ISSPCMD_EXEC)
  295. def __readID(self):
  296. "Read the silicon ID"
  297. self.__bitbangStringVectors(self.STRVEC_IDSETUP)
  298. self.__runCommandSync(self.ISSPCMD_EXEC)
  299. low = (self.__bitbangStringVector(self.STRVEC_READID[0]) >> 2) & 0xFF
  300. high = (self.__bitbangStringVector(self.STRVEC_READID[1]) >> 2) & 0xFF
  301. return low | (high << 8)
  302. def __readByte(self, address):
  303. vec = self.__stringVectorReplace(self.STRVEC_READBYTE[0], "a", address)
  304. inputData = self.__bitbangStringVector(vec)
  305. return (inputData >> 2) & 0xFF
  306. def __writeByte(self, address, byte):
  307. vec = self.__stringVectorReplace(self.STRVEC_WRITEBYTE[0], "a", address)
  308. vec = self.__stringVectorReplace(vec, "d", byte)
  309. self.__bitbangStringVector(vec)
  310. def __loadCommand(self, command):
  311. self.top.cmdFPGAWrite(0x11, command & 0xFF)
  312. def __runCommandSync(self, command):
  313. self.printDebug("Running synchronous command %d" % command)
  314. self.__loadCommand(command)
  315. self.__busyWait()
  316. def __setBitbang(self, SDATA, SDATA_in, SCLK, SCLK_z):
  317. value = 0
  318. if SDATA:
  319. value |= 0x01
  320. if SDATA_in:
  321. value |= 0x02
  322. if SCLK:
  323. value |= 0x04
  324. if SCLK_z:
  325. value |= 0x08
  326. self.top.cmdFPGAWrite(0x10, value)
  327. def __getStatusFlags(self):
  328. self.top.cmdFPGARead(0x10)
  329. stat = self.top.cmdReadBufferReg8()
  330. isspState = (stat & self.STAT_ISSPSTATE) >> self.STAT_ISSPSTATE_SHIFT
  331. sdata = bool(stat & self.STAT_SDATA)
  332. isBusy = bool(stat & self.STAT_BUSY0) != bool(stat & self.STAT_BUSY1)
  333. self.printDebug("isspState = 0x%02X, isBusy = %d, busyFlags = 0x%01X, sdata = %d" %\
  334. (isspState, isBusy, (stat & (self.STAT_BUSY0 | self.STAT_BUSY1)), sdata))
  335. return (isBusy, sdata, isspState)
  336. def __busy(self):
  337. (isBusy, sdata, isspState) = self.__getStatusFlags()
  338. return isBusy
  339. def __getSDATA(self):
  340. (isBusy, sdata, isspState) = self.__getStatusFlags()
  341. return int(sdata)
  342. def __busyWait(self):
  343. for i in range(0, 200):
  344. if not self.__busy():
  345. return
  346. self.top.hostDelay(0.01)
  347. self.throwError("Timeout in busywait. Chip not responding?")
  348. def __stringVectorToBinary(self, vector):
  349. binary = 0
  350. inputMask = 0
  351. assert(len(vector) == 22)
  352. bit = len(vector) - 1
  353. for b in vector:
  354. if b == "1":
  355. binary |= (1 << bit)
  356. elif b == "0":
  357. pass
  358. elif b == "H" or b == "L" or b == "Z" or b == "D":
  359. inputMask |= (1 << bit)
  360. else:
  361. assert(0)
  362. bit -= 1
  363. return (binary, inputMask)
  364. def __stringVectorReplace(self, strVec, replace, data):
  365. ret = ""
  366. for i in range(len(strVec) - 1, -1, -1):
  367. b = strVec[i]
  368. if b == replace:
  369. if (data & 1):
  370. ret = "1" + ret
  371. else:
  372. ret = "0" + ret
  373. data >>= 1
  374. else:
  375. ret = b + ret
  376. return ret
  377. def __bitbangStringVector(self, strVec):
  378. vectorSize = len(strVec)
  379. (vector, inputMask) = self.__stringVectorToBinary(strVec)
  380. inputData = 0
  381. self.__setBitbang(SDATA=0, SDATA_in=1, SCLK=0, SCLK_z=0)
  382. for i in range(vectorSize - 1, -1, -1):
  383. if inputMask & (1 << i):
  384. self.__setBitbang(SDATA=0, SDATA_in=1, SCLK=1, SCLK_z=0)
  385. self.__setBitbang(SDATA=0, SDATA_in=1, SCLK=0, SCLK_z=0)
  386. self.top.cmdDelay(0.000001)
  387. sdata = self.__getSDATA()
  388. inputData |= (sdata << i)
  389. else:
  390. self.__setBitbang(SDATA=(vector & (1 << i)), SDATA_in=0,
  391. SCLK=1, SCLK_z=0)
  392. self.__setBitbang(SDATA=0, SDATA_in=0, SCLK=0, SCLK_z=0)
  393. self.top.cmdDelay(0.000001)
  394. return inputData
  395. def __bitbangStringVectors(self, strVecList):
  396. for strVec in strVecList:
  397. self.__bitbangStringVector(strVec)
  398. def __uploadStringVector(self, strVec):
  399. (vector, inputMask) = self.__stringVectorToBinary(strVec)
  400. assert(inputMask == 0)
  401. self.top.cmdFPGAWrite(0x12, vector & 0xFF)
  402. self.top.cmdFPGAWrite(0x13, (vector >> 8) & 0xFF)
  403. self.top.cmdFPGAWrite(0x14, (vector >> 8) & 0xFF)
  404. ChipDescription(
  405. Chip_M8C_ISSP,
  406. bitfile = "m8c-issp",
  407. runtimeID = (0x0007, 0x01),
  408. chipVendors = "Cypress",
  409. description = "M8C In System Serial Programmer",
  410. packages = ( ("M8C ISSP header", "Special adapter"), ),
  411. comment = "Special adapter required",
  412. broken = True
  413. )