Merge pull request #284 from UweBonnes/stm32_mem
stm32_mem.py: Print longer strings and small formatting changes.
This commit is contained in:
commit
19e58a7205
|
@ -84,6 +84,8 @@ class dfu_device(object):
|
|||
self.index = self.iface.interfaceNumber
|
||||
else: self.index = self.iface
|
||||
|
||||
def release(self):
|
||||
self.handle.releaseInterface()
|
||||
def detach(self, wTimeout=255):
|
||||
self.handle.controlMsg(usb.ENDPOINT_OUT | usb.TYPE_CLASS |
|
||||
usb.RECIP_INTERFACE, DFU_DETACH,
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#
|
||||
# stm32_mem.py: STM32 memory access using USB DFU class
|
||||
# Copyright (C) 2011 Black Sphere Technologies
|
||||
# Copyright (C) 2017 Uwe Bonnes (bon@elektron.ikp.physik.tu-darmstadt.de)
|
||||
# Written by Gareth McMullin <gareth@blacksphere.co.nz>
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
|
@ -57,7 +58,17 @@ def stm32_write(dev, data):
|
|||
sleep(status.bwPollTimeout / 1000.0)
|
||||
if status.bState == dfu.STATE_DFU_DOWNLOAD_IDLE:
|
||||
break
|
||||
|
||||
|
||||
def stm32_read(dev):
|
||||
data = dev.upload(2, 1024)
|
||||
while True:
|
||||
status = dev.get_status()
|
||||
if status.bState == dfu.STATE_DFU_DOWNLOAD_BUSY:
|
||||
sleep(status.bwPollTimeout / 1000.0)
|
||||
if status.bState == dfu.STATE_DFU_UPLOAD_IDLE:
|
||||
break
|
||||
return data
|
||||
|
||||
def stm32_manifest(dev):
|
||||
dev.download(0, "")
|
||||
while True:
|
||||
|
@ -68,6 +79,51 @@ def stm32_manifest(dev):
|
|||
sleep(status.bwPollTimeout / 1000.0)
|
||||
if status.bState == dfu.STATE_DFU_MANIFEST:
|
||||
break
|
||||
def stm32_scan(args):
|
||||
devs = dfu.finddevs()
|
||||
bmp = 0
|
||||
if not devs:
|
||||
print "No DFU devices found!"
|
||||
exit(-1)
|
||||
|
||||
for dev in devs:
|
||||
dfudev = dfu.dfu_device(*dev)
|
||||
man = dfudev.handle.getString(dfudev.dev.iManufacturer, 30)
|
||||
if man == "Black Sphere Technologies": bmp = bmp + 1
|
||||
if bmp == 0 :
|
||||
print "No compatible device found\n"
|
||||
exit(-1)
|
||||
if bmp > 1 and not args.serial_target :
|
||||
print "Found multiple devices:\n"
|
||||
for dev in devs:
|
||||
dfudev = dfu.dfu_device(*dev)
|
||||
man = dfudev.handle.getString(dfudev.dev.iManufacturer, 30)
|
||||
product = dfudev.handle.getString(dfudev.dev.iProduct, 96)
|
||||
serial_no = dfudev.handle.getString(dfudev.dev.iSerialNumber, 30)
|
||||
print "Device ID:\t %04x:%04x" % (dfudev.dev.idVendor, dfudev.dev.idProduct)
|
||||
print "Manufacturer:\t %s" % man
|
||||
print "Product:\t %s" % product
|
||||
print "Serial:\t\t %s\n" % serial_no
|
||||
print "Select device with serial number!"
|
||||
exit (-1)
|
||||
|
||||
for dev in devs:
|
||||
dfudev = dfu.dfu_device(*dev)
|
||||
man = dfudev.handle.getString(dfudev.dev.iManufacturer, 30)
|
||||
product = dfudev.handle.getString(dfudev.dev.iProduct, 96)
|
||||
serial_no = dfudev.handle.getString(dfudev.dev.iSerialNumber, 30)
|
||||
if args.serial_target:
|
||||
if man == "Black Sphere Technologies" and serial_no == args.serial_target: break
|
||||
else:
|
||||
if man == "Black Sphere Technologies": break
|
||||
print "Device ID:\t %04x:%04x" % (dfudev.dev.idVendor, dfudev.dev.idProduct)
|
||||
print "Manufacturer:\t %s" % man
|
||||
print "Product:\t %s" % product
|
||||
print "Serial:\t\t %s" % serial_no
|
||||
if args.serial_target and serial_no != args.serial_target:
|
||||
print "Serial number doesn't match!\n"
|
||||
exit(-2)
|
||||
return dfudev
|
||||
|
||||
if __name__ == "__main__":
|
||||
print
|
||||
|
@ -79,55 +135,56 @@ if __name__ == "__main__":
|
|||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("progfile", help="Binary file to program")
|
||||
parser.add_argument("-s", "--serial_target", help="Match Serial Number")
|
||||
parser.add_argument("-a", "--address", help="Start address for firmware")
|
||||
parser.add_argument("-m", "--manifest", help="Start application, if in DFU mode", action='store_true')
|
||||
args = parser.parse_args()
|
||||
devs = dfu.finddevs()
|
||||
if not devs:
|
||||
print "No devices found!"
|
||||
exit(-1)
|
||||
|
||||
for dev in devs:
|
||||
dfudev = dfu.dfu_device(*dev)
|
||||
man = dfudev.handle.getString(dfudev.dev.iManufacturer, 30)
|
||||
product = dfudev.handle.getString(dfudev.dev.iProduct, 64)
|
||||
serial_no = dfudev.handle.getString(dfudev.dev.iSerialNumber, 30)
|
||||
if args.serial_target:
|
||||
if man == "Black Sphere Technologies" and serial_no == args.serial_target: break
|
||||
if man == "STMicroelectronics" and serial_no == args.serial_target: break
|
||||
else:
|
||||
if man == "Black Sphere Technologies": break
|
||||
if man == "STMicroelectronics": break
|
||||
|
||||
print "Device %s: ID %04x:%04x %s - %s\n\tSerial %s" % (
|
||||
dfudev.dev.filename, dfudev.dev.idVendor,
|
||||
dfudev.dev.idProduct, man, product, serial_no)
|
||||
|
||||
if args.serial_target and serial_no != args.serial_target:
|
||||
print "Serial number doesn't match!\n"
|
||||
exit(-2)
|
||||
dfudev = stm32_scan(args)
|
||||
try:
|
||||
state = dfudev.get_state()
|
||||
except:
|
||||
if args.manifest : exit(0)
|
||||
print "Failed to read device state! Assuming APP_IDLE"
|
||||
state = dfu.STATE_APP_IDLE
|
||||
if state == dfu.STATE_APP_IDLE:
|
||||
dfudev.detach()
|
||||
print "Run again to upgrade firmware."
|
||||
dfudev.release()
|
||||
print "Invoking DFU Device"
|
||||
timeout = 0
|
||||
while True :
|
||||
sleep(0.5)
|
||||
timeout = timeout + 0.5
|
||||
dfudev = stm32_scan(args)
|
||||
if dfudev: break
|
||||
if timeout > 5 :
|
||||
print "Error: DFU device did not appear"
|
||||
exit(-1)
|
||||
if args.manifest :
|
||||
stm32_manifest(dfudev)
|
||||
print "Invoking Application Device"
|
||||
exit(0)
|
||||
|
||||
dfudev.make_idle()
|
||||
file = open(args.progfile, "rb")
|
||||
bin = file.read()
|
||||
|
||||
bin = open(args.progfile, "rb").read()
|
||||
|
||||
if "F4" in product:
|
||||
addr = 0x8004000
|
||||
else:
|
||||
addr = 0x8002000
|
||||
product = dfudev.handle.getString(dfudev.dev.iProduct, 64)
|
||||
if args.address :
|
||||
start = int(args.address, 0)
|
||||
else :
|
||||
if "F4" in product:
|
||||
start = 0x8004000
|
||||
else:
|
||||
start = 0x8002000
|
||||
addr = start
|
||||
while bin:
|
||||
print ("Programming memory at 0x%08X\r" % addr),
|
||||
stdout.flush()
|
||||
try:
|
||||
# STM DFU bootloader erases always.
|
||||
# BPM Bootloader only erases once per sector
|
||||
# To support the STM DFU bootloader, the interface descriptor must
|
||||
# get evaluated and erase called only once per sector!
|
||||
stm32_erase(dfudev, addr)
|
||||
except:
|
||||
except:
|
||||
print "\nErase Timed out\n"
|
||||
break
|
||||
try:
|
||||
|
@ -136,10 +193,34 @@ if __name__ == "__main__":
|
|||
print "\nSet Address Timed out\n"
|
||||
break
|
||||
stm32_write(dfudev, bin[:1024])
|
||||
|
||||
bin = bin[1024:]
|
||||
addr += 1024
|
||||
|
||||
file.seek(0)
|
||||
bin = file.read()
|
||||
len = len(bin)
|
||||
addr = start
|
||||
print
|
||||
while bin:
|
||||
try:
|
||||
stm32_set_address(dfudev, addr)
|
||||
data = stm32_read(dfudev)
|
||||
except:
|
||||
# Abort silent if bootloader does not support upload
|
||||
break
|
||||
print ("Verifying memory at 0x%08X\r" % addr),
|
||||
stdout.flush()
|
||||
if len > 1024 :
|
||||
size = 1024
|
||||
else :
|
||||
size = len
|
||||
if bin[:size] != bytearray(data[:size]) :
|
||||
print ("\nMitmatch in block at 0x%08X" % addr)
|
||||
break;
|
||||
bin = bin[1024:]
|
||||
addr += 1024
|
||||
len -= 1024
|
||||
if len <= 0 :
|
||||
print "\nVerified!"
|
||||
stm32_manifest(dfudev)
|
||||
|
||||
print "\nAll operations complete!\n"
|
||||
print "All operations complete!\n"
|
||||
|
|
Loading…
Reference in New Issue