toprammer 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #!/usr/bin/env python
  2. """
  3. # TOP2049 Open Source programming suite
  4. #
  5. # Commandline utility
  6. #
  7. # Copyright (c) 2009-2013 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.main import *
  24. from libtoprammer.util import *
  25. import getopt
  26. def usage():
  27. print "TOP2049 Open Source programming suite v%s" % VERSION
  28. print ""
  29. print "Usage: %s [OPTIONS] [ACTIONS]" % sys.argv[0]
  30. print ""
  31. print " -c|--chip-id The ID of the handled chip. (mandatory)"
  32. print " See -t|--list for a list of supported chips."
  33. print ""
  34. print "Optional:"
  35. print " -C|--chip-opt NAME=VAL Set a chip-id specific option."
  36. print " Use -c CHIPID -t to get a list of options."
  37. print ""
  38. print "Actions:"
  39. print " -s|--read-sig FILE Read the signature bytes"
  40. print " -x|--erase Erase the chip"
  41. print " -T|--test Run chip unit-test"
  42. print ""
  43. print " -p|--read-prog FILE Read the program memory"
  44. print " -P|--write-prog FILE Write the program memory"
  45. print ""
  46. print " -e|--read-eeprom FILE Read the EEPROM"
  47. print " -E|--write-eeprom FILE Write the EEPROM"
  48. print ""
  49. print " -f|--read-fuse FILE Read the fuse bits"
  50. print " -F|--write-fuse FILE Write the fuse bits"
  51. print ""
  52. print " -l|--read-lock FILE Read the lock bits"
  53. print " -L|--write-lock FILE Write the lock bits"
  54. print ""
  55. print " -r|--read-ram FILE Read the RAM"
  56. print " -R|--write-ram FILE Write the RAM"
  57. print ""
  58. print " -a|--read-uil FILE Read the User ID Location"
  59. print " -A|--write-uil FILE Write the User ID Location"
  60. print ""
  61. print "Other options:"
  62. print " -t|--list Print a list of supported chips and exit."
  63. print " Use -V|--verbose to control the list verbosity (0-4)"
  64. print " -d|--device DEVID Use a specific programmer. Example for USB:"
  65. print " usb:TOP2049:0"
  66. print " First found programmer is used, if not given."
  67. print " -V|--verbose LEVEL Set the verbosity level:"
  68. print " 0 => show warnings"
  69. print " 1 => also show informational messages (default)"
  70. print " 2 => also show debugging messages"
  71. print " 3 => also dump all USB commands"
  72. print " -o|--force LEVEL Set the force level. Default = 0"
  73. print " Note that any value greater than 0 may brick devices"
  74. print " -U|--force-upload Force upload the bitfile, even if it already appears"
  75. print " to be uploaded."
  76. print " -Q|--noqueue Disable command queuing. Really slow!"
  77. print " -B|--broken Also use broken algorithms"
  78. print " -I|--in-format FMT Input file format. Default = autodetect"
  79. print " -O|--out-format FMT Output file format. Default = bin"
  80. print ""
  81. print "File formats (FMT):"
  82. print " auto Autodetect. (input only)"
  83. print " bin Raw binary data"
  84. print " ihex Intel hex"
  85. print " ihex-raw Raw Intel hex (don't interpret sections)"
  86. print " ahex Hex with ASCII dump"
  87. IO_handlers = {
  88. "bin" : IO_binary,
  89. "ihex" : IO_ihex,
  90. "ihex-raw" : IO_ihex,
  91. "ahex" : IO_ahex,
  92. }
  93. def fileOut(filename, fmtString, data):
  94. handler = IO_handlers[fmtString]()
  95. data = handler.fromBinary(data)
  96. if filename == "-":
  97. sys.stdout.write(data)
  98. else:
  99. open(filename, "w+b").write(data)
  100. def fileIn(top, action, filename, fmtString):
  101. if filename == "-":
  102. data = sys.stdin.read()
  103. else:
  104. data = open(filename, "rb").read()
  105. if fmtString == "auto":
  106. handler = IO_autodetect(data)()
  107. else:
  108. handler = IO_handlers[fmtString]()
  109. if isinstance(handler, IO_ihex):
  110. interp = top.getChip().getIHexInterpreter()
  111. interp.interpret(data)
  112. if interp.cumulativeSupported():
  113. readRaw = fmtString.endswith("-raw")
  114. else:
  115. readRaw = True
  116. if action == "write-prog":
  117. binData = interp.getProgmem(dontInterpretSections = readRaw)
  118. elif action == "write-eeprom":
  119. binData = interp.getEEPROM(dontInterpretSections = readRaw)
  120. elif action == "write-fuse":
  121. binData = interp.getFusebits(dontInterpretSections = readRaw)
  122. elif action == "write-lock":
  123. binData = interp.getLockbits(dontInterpretSections = readRaw)
  124. elif action == "write-ram":
  125. binData = interp.getRAM(dontInterpretSections = readRaw)
  126. elif action == "write-uil":
  127. binData = interp.getUIL(dontInterpretSections = readRaw)
  128. else:
  129. assert(0)
  130. return binData
  131. return handler.toBinary(data)
  132. def main(argv):
  133. opt_verbose = 1
  134. opt_forceLevel = 0
  135. opt_forceBitfileUpload = False
  136. opt_chipID = None
  137. opt_chipOptions = []
  138. opt_device = None
  139. opt_action = None
  140. opt_file = None
  141. opt_noqueue = False
  142. opt_usebroken = False
  143. opt_informat = "auto"
  144. opt_outformat = "bin"
  145. try:
  146. (opts, args) = getopt.getopt(sys.argv[1:],
  147. "hc:d:V:Qs:xTp:P:e:E:f:F:o:Ul:L:r:R:BtI:O:C:a:A:",
  148. [ "help", "chip-id=", "device=", "verbose=", "noqueue",
  149. "read-sig=", "erase", "test", "read-prog=", "write-prog=",
  150. "read-eeprom=", "write-eeprom=", "read-fuse=", "write-fuse=",
  151. "read-lock=", "write-lock=", "read-ram=", "write-ram=",
  152. "force=", "force-upload", "broken", "list",
  153. "in-format=", "out-format=", "chip-opt=",
  154. "read-uil=", "write-uil=" ])
  155. for (o, v) in opts:
  156. if o in ("-h", "--help"):
  157. usage()
  158. return 0
  159. if o in ("-c", "--chip-id"):
  160. opt_chipID = v
  161. if o in ("-C", "--chip-opt"):
  162. try:
  163. v = v.split('=')
  164. name, value = v[0], v[1]
  165. except (IndexError, ValueError), e:
  166. print "-C|--chip-opt invalid parameter"
  167. return 1
  168. copt = AssignedChipOption(name, value)
  169. opt_chipOptions.append(copt)
  170. if o in ("-t", "--list"):
  171. opt_action = "print-list"
  172. if o in ("-d", "--device"):
  173. opt_device = v
  174. if o in ("-V", "--verbose"):
  175. opt_verbose = int(v)
  176. if o in ("-o", "--force"):
  177. opt_forceLevel = int(v)
  178. if o in ("-U", "--force-upload"):
  179. opt_forceBitfileUpload = True
  180. if o in ("-Q", "--noqueue"):
  181. opt_noqueue = True
  182. if o in ("-B", "--broken"):
  183. opt_usebroken = True
  184. if o in ("-I", "--in-format"):
  185. opt_informat = v.lower()
  186. if o in ("-O", "--out-format"):
  187. opt_outformat = v.lower()
  188. if o in ("-s", "--read-sig"):
  189. opt_action = "read-sig"
  190. opt_file = v
  191. if o in ("-x", "--erase"):
  192. opt_action = "erase"
  193. if o in ("-T", "--test"):
  194. opt_action = "test"
  195. if o in ("-P", "--write-prog"):
  196. opt_action = "write-prog"
  197. opt_file = v
  198. if o in ("-p", "--read-prog"):
  199. opt_action = "read-prog"
  200. opt_file = v
  201. if o in ("-E", "--write-eeprom"):
  202. opt_action = "write-eeprom"
  203. opt_file = v
  204. if o in ("-e", "--read-eeprom"):
  205. opt_action = "read-eeprom"
  206. opt_file = v
  207. if o in ("-F", "--write-fuse"):
  208. opt_action = "write-fuse"
  209. opt_file = v
  210. if o in ("-f", "--read-fuse"):
  211. opt_action = "read-fuse"
  212. opt_file = v
  213. if o in ("-l", "--read-lock"):
  214. opt_action = "read-lock"
  215. opt_file = v
  216. if o in ("-L", "--write-lock"):
  217. opt_action = "write-lock"
  218. opt_file = v
  219. if o in ("-r", "--read-ram"):
  220. opt_action = "read-ram"
  221. opt_file = v
  222. if o in ("-R", "--write-ram"):
  223. opt_action = "write-ram"
  224. opt_file = v
  225. if o in ("-a", "--read-uil"):
  226. opt_action = "read-uil"
  227. opt_file = v
  228. if o in ("-A", "--write-uil"):
  229. opt_action = "write-uil"
  230. opt_file = v
  231. except (getopt.GetoptError, ValueError), e:
  232. usage()
  233. return 1
  234. if opt_action != "print-list" and not opt_chipID:
  235. print "-c|--chip-id is mandatory!"
  236. return 1
  237. if not opt_informat in ("auto", "bin", "ihex", "ihex-raw", "ahex"):
  238. print "Invalid -I|--in-format"
  239. return 1
  240. if not opt_outformat in ("bin", "ihex", "ihex-raw", "ahex"):
  241. print "Invalid -O|--out-format"
  242. return 1
  243. try:
  244. if opt_action == "print-list":
  245. if opt_chipID:
  246. desc = ChipDescription.findOne(opt_chipID, True)
  247. desc.dump(sys.stdout, verbose=opt_verbose)
  248. else:
  249. ChipDescription.dumpAll(sys.stdout,
  250. verbose=opt_verbose, showBroken=True)
  251. return 0
  252. top = TOP(devIdentifier = opt_device,
  253. verbose = opt_verbose, forceLevel = opt_forceLevel,
  254. noqueue = opt_noqueue, usebroken = opt_usebroken,
  255. forceBitfileUpload = opt_forceBitfileUpload)
  256. top.initializeChip(chipID = opt_chipID,
  257. assignedChipOptions = opt_chipOptions)
  258. if opt_action == "read-sig":
  259. fileOut(opt_file, opt_outformat, top.readSignature())
  260. elif opt_action == "erase":
  261. top.eraseChip()
  262. elif opt_action == "test":
  263. top.testChip()
  264. elif opt_action == "read-prog":
  265. fileOut(opt_file, opt_outformat, top.readProgmem())
  266. elif opt_action == "write-prog":
  267. image = fileIn(top, opt_action, opt_file, opt_informat)
  268. top.writeProgmem(image)
  269. elif opt_action == "read-eeprom":
  270. fileOut(opt_file, opt_outformat, top.readEEPROM())
  271. elif opt_action == "write-eeprom":
  272. top.writeEEPROM(fileIn(top, opt_action, opt_file, opt_informat))
  273. elif opt_action == "read-fuse":
  274. fileOut(opt_file, opt_outformat, top.readFuse())
  275. elif opt_action == "write-fuse":
  276. image = fileIn(top, opt_action, opt_file, opt_informat)
  277. top.writeFuse(image)
  278. elif opt_action == "read-lock":
  279. fileOut(opt_file, opt_outformat, top.readLockbits())
  280. elif opt_action == "write-lock":
  281. top.writeLockbits(fileIn(top, opt_action, opt_file, opt_informat))
  282. elif opt_action == "read-ram":
  283. fileOut(opt_file, opt_outformat, top.readRAM())
  284. elif opt_action == "write-ram":
  285. top.writeRAM(fileIn(top, opt_action, opt_file, opt_informat))
  286. elif opt_action == "read-uil":
  287. fileOut(opt_file, opt_outformat, top.readUserIdLocation())
  288. elif opt_action == "write-uil":
  289. top.writeUserIdLocation(fileIn(top, opt_action, opt_file, opt_informat))
  290. else:
  291. if opt_verbose >= 1:
  292. print "No action specified"
  293. top.shutdownChip()
  294. except (TOPException, BitfileException, IOError), e:
  295. print e
  296. return 1
  297. return 0
  298. if __name__ == "__main__":
  299. sys.exit(main(sys.argv))