[tools] stm32_mem.py: add option to only list devices

and make the dry run option an alias for list
This commit is contained in:
Felix Ruess
2013-11-11 15:55:18 +01:00
parent 61349d29f1
commit a9eccf6b41
Regular → Executable
+37 -22
View File
@@ -38,6 +38,7 @@ CMD_GETCOMMANDS = 0x00
CMD_SETADDRESSPOINTER = 0x21 CMD_SETADDRESSPOINTER = 0x21
CMD_ERASE = 0x41 CMD_ERASE = 0x41
def stm32_erase(dev, addr): def stm32_erase(dev, addr):
erase_cmd = struct.pack("<BL", CMD_ERASE, addr) erase_cmd = struct.pack("<BL", CMD_ERASE, addr)
dev.download(0, erase_cmd) dev.download(0, erase_cmd)
@@ -48,6 +49,7 @@ def stm32_erase(dev, addr):
if status.bState == dfu.STATE_DFU_DOWNLOAD_IDLE: if status.bState == dfu.STATE_DFU_DOWNLOAD_IDLE:
break break
def stm32_write(dev, data): def stm32_write(dev, data):
dev.download(2, data) dev.download(2, data)
while True: while True:
@@ -57,6 +59,7 @@ def stm32_write(dev, data):
if status.bState == dfu.STATE_DFU_DOWNLOAD_IDLE: if status.bState == dfu.STATE_DFU_DOWNLOAD_IDLE:
break break
def stm32_manifest(dev): def stm32_manifest(dev):
dev.download(0, "") dev.download(0, "")
while True: while True:
@@ -68,6 +71,7 @@ def stm32_manifest(dev):
if status.bState == dfu.STATE_DFU_MANIFEST: if status.bState == dfu.STATE_DFU_MANIFEST:
break break
def print_copyright(): def print_copyright():
print("") print("")
print("USB Device Firmware Upgrade - Host Utility -- version 1.3") print("USB Device Firmware Upgrade - Host Utility -- version 1.3")
@@ -76,26 +80,31 @@ def print_copyright():
print("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>") print("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>")
print("") print("")
def init_progress_bar(): def init_progress_bar():
max_symbols = 50 max_symbols = 50
print("[0%" + "="*int(max_symbols/2 - 4) + "50%" + "="*int(max_symbols/2 - 4) + "100%]") print("[0%" + "=" * int(max_symbols / 2 - 4) + "50%" + "=" * int(max_symbols / 2 - 4) + "100%]")
print(" ", end="") print(" ", end="")
update_progress_bar.count = 0 update_progress_bar.count = 0
update_progress_bar.symbol_limit = max_symbols update_progress_bar.symbol_limit = max_symbols
def update_progress_bar(completed, total): def update_progress_bar(completed, total):
if completed and total: if completed and total:
percent = 100 * (float(completed)/float(total)) percent = 100 * (float(completed) / float(total))
if (percent >= (update_progress_bar.count + (100.0 / update_progress_bar.symbol_limit))): if percent >= (update_progress_bar.count + (100.0 / update_progress_bar.symbol_limit)):
update_progress_bar.count += (100.0 / update_progress_bar.symbol_limit) update_progress_bar.count += (100.0 / update_progress_bar.symbol_limit)
print("#", end="") print("#", end="")
stdout.flush() stdout.flush()
if __name__ == "__main__": if __name__ == "__main__":
usage = "Usage: %prog [options] firmware.bin" + "\n" + "Run %prog --help to list the options." usage = "Usage: %prog [options] [firmware.bin]" + "\n" + "Run %prog --help to list the options."
parser = OptionParser(usage, version='%prog version 1.3') parser = OptionParser(usage, version='%prog version 1.3')
parser.add_option("-v", "--verbose", parser.add_option("-v", "--verbose",
action="store_true", dest="verbose") action="store_true", dest="verbose")
parser.add_option("-l", "--list", action="store_true", dest="list_only",
help="Only list currently connected DFU devices without actually flashing.")
parser.add_option("--product", type="choice", choices=["any", "Lisa/Lia"], parser.add_option("--product", type="choice", choices=["any", "Lisa/Lia"],
action="store", default="Lisa/Lia", action="store", default="Lisa/Lia",
help="only upload to device where idProduct contains PRODUCT\n" help="only upload to device where idProduct contains PRODUCT\n"
@@ -103,9 +112,13 @@ if __name__ == "__main__":
parser.add_option("--addr", type="int", action="store", dest="addr", default=APP_ADDRESS, parser.add_option("--addr", type="int", action="store", dest="addr", default=APP_ADDRESS,
help="Upload start address (default: 0x08002000)") help="Upload start address (default: 0x08002000)")
parser.add_option("-n", "--dry-run", action="store_true", parser.add_option("-n", "--dry-run", action="store_true",
help="Dry run to check which board is found without actually flashing.") help="Alias for --list.")
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
if options.dry_run:
options.list_only = True
if not options.list_only:
if len(args) != 1: if len(args) != 1:
parser.error("incorrect number of arguments") parser.error("incorrect number of arguments")
else: else:
@@ -117,7 +130,7 @@ if __name__ == "__main__":
if options.verbose: if options.verbose:
print_copyright() print_copyright()
for i in range(1,60): for i in range(1, 60):
devs = dfu.finddevs() devs = dfu.finddevs()
if not devs: if not devs:
print('.', end="") print('.', end="")
@@ -129,7 +142,7 @@ if __name__ == "__main__":
if not devs: if not devs:
print("No DFU devices found!") print("No DFU devices found!")
exit(1) exit(1)
elif options.verbose: elif options.verbose or options.list_only:
print("Found %i DFU devices." % len(devs)) print("Found %i DFU devices." % len(devs))
valid_manufacturers = [] valid_manufacturers = []
@@ -159,7 +172,7 @@ if __name__ == "__main__":
print("Exception:", e) print("Exception:", e)
continue continue
if options.verbose: if options.verbose or options.list_only:
print("Found DFU device %s: ID %04x:%04x %s - %s - %s" % print("Found DFU device %s: ID %04x:%04x %s - %s - %s" %
(dfudev.dev.filename, dfudev.dev.idVendor, (dfudev.dev.filename, dfudev.dev.idVendor,
dfudev.dev.idProduct, man, product, serial)) dfudev.dev.idProduct, man, product, serial))
@@ -182,13 +195,15 @@ if __name__ == "__main__":
(d.dev.filename, d.dev.idVendor, d.dev.idProduct, m, p, s)) (d.dev.filename, d.dev.idVendor, d.dev.idProduct, m, p, s))
# use first potential board as target # use first potential board as target
target = stm32devs[0][0] (target, m, p, s) = stm32devs[0]
print("Using device %s: ID %04x:%04x %s - %s - %s" % (target.dev.filename, print("Using device %s: ID %04x:%04x %s - %s - %s" % (target.dev.filename,
target.dev.idVendor, target.dev.idProduct, man, product, serial)) target.dev.idVendor,
target.dev.idProduct,
m, p, s))
# if it's a dry run only, don't actually flash, just exit now # if just listing available devices, exit now
if options.dry_run: if options.list_only:
print("Dry run, done.") print("Done.")
exit(0) exit(0)
try: try:
@@ -204,31 +219,31 @@ if __name__ == "__main__":
target.make_idle() target.make_idle()
try: try:
bin = open(binfile, "rb").read() binf = open(binfile, "rb").read()
except: except:
print("Could not open binary file.") print("Could not open binary file.")
raise raise
# Get the file length for progress bar # Get the file length for progress bar
bin_length = len(bin) bin_length = len(binf)
#addr = APP_ADDRESS #addr = APP_ADDRESS
addr = options.addr addr = options.addr
print ("Programming memory from 0x%08X...\r" % addr) print("Programming memory from 0x%08X...\r" % addr)
init_progress_bar() init_progress_bar()
while bin: while binf:
update_progress_bar((addr - options.addr),bin_length) update_progress_bar((addr - options.addr), bin_length)
stm32_erase(target, addr) stm32_erase(target, addr)
stm32_write(target, bin[:SECTOR_SIZE]) stm32_write(target, binf[:SECTOR_SIZE])
bin = bin[SECTOR_SIZE:] binf = binf[SECTOR_SIZE:]
addr += SECTOR_SIZE addr += SECTOR_SIZE
# Need to check all the way to 100% complete # Need to check all the way to 100% complete
update_progress_bar((addr - options.addr),bin_length) update_progress_bar((addr - options.addr), bin_length)
stm32_manifest(target) stm32_manifest(target)
print("\nAll operations complete!\n") print("\nAll operations complete!")