scripts: stm32_mem.py: change some formatting, remove unused imports

This commit is contained in:
Nicolas Schodet 2022-02-13 17:04:35 +01:00 committed by Piotr Esden-Tempski
parent eafc634eba
commit f8f2ab2016
1 changed files with 19 additions and 20 deletions

View File

@ -21,10 +21,9 @@
from time import sleep from time import sleep
import struct import struct
import os import os
from sys import stdout, argv from sys import stdout
import argparse import argparse
import usb
import dfu import dfu
CMD_GETCOMMANDS = 0x00 CMD_GETCOMMANDS = 0x00
@ -86,7 +85,7 @@ def stm32_scan(args, test):
bmp_devs = [] bmp_devs = []
bmp = 0 bmp = 0
if not devs: if not devs:
if test == True: if test:
return return
print("No DFU devices found!") print("No DFU devices found!")
@ -107,14 +106,14 @@ def stm32_scan(args, test):
bmp_devs.append(dev) bmp_devs.append(dev)
if bmp == 0: if bmp == 0:
if test == True: if test:
return return
print("No compatible device found\n") print("No compatible device found\n")
exit(-1) exit(-1)
if bmp > 1 and not args.serial_target: if bmp > 1 and not args.serial_target:
if test == True: if test:
return return
print("Found multiple devices:\n") print("Found multiple devices:\n")
@ -129,7 +128,7 @@ def stm32_scan(args, test):
print("Serial:\t\t %s\n" % serial_no) print("Serial:\t\t %s\n" % serial_no)
print("Select device with serial number!") print("Select device with serial number!")
exit (-1) exit(-1)
for dev in bmp_devs: for dev in bmp_devs:
dfudev = dfu.dfu_device(*dev) dfudev = dfu.dfu_device(*dev)
@ -171,7 +170,7 @@ if __name__ == "__main__":
try: try:
state = dfudev.get_state() state = dfudev.get_state()
except: except:
if args.manifest : exit(0) if args.manifest: exit(0)
print("Failed to read device state! Assuming APP_IDLE") print("Failed to read device state! Assuming APP_IDLE")
state = dfu.STATE_APP_IDLE state = dfu.STATE_APP_IDLE
if state == dfu.STATE_APP_IDLE: if state == dfu.STATE_APP_IDLE:
@ -182,15 +181,15 @@ if __name__ == "__main__":
dfudev.release() dfudev.release()
print("Invoking DFU Device") print("Invoking DFU Device")
timeout = 0 timeout = 0
while True : while True:
sleep(1) sleep(1)
timeout = timeout + 0.5 timeout = timeout + 0.5
dfudev = stm32_scan(args, True) dfudev = stm32_scan(args, True)
if dfudev: break if dfudev: break
if timeout > 5 : if timeout > 5:
print("Error: DFU device did not appear") print("Error: DFU device did not appear")
exit(-1) exit(-1)
if args.manifest : if args.manifest:
stm32_manifest(dfudev) stm32_manifest(dfudev)
print("Invoking Application Device") print("Invoking Application Device")
exit(0) exit(0)
@ -203,16 +202,16 @@ if __name__ == "__main__":
bin = file.read() bin = file.read()
product = dfudev.handle.getString(dfudev.dev.iProduct, 64).decode('utf8') product = dfudev.handle.getString(dfudev.dev.iProduct, 64).decode('utf8')
if args.address : if args.address:
start = int(args.address, 0) start = int(args.address, 0)
else : else:
if "F4" in product or "STLINK-V3" in product: if "F4" in product or "STLINK-V3" in product:
start = 0x8004000 start = 0x8004000
else: else:
start = 0x8002000 start = 0x8002000
addr = start addr = start
while bin: while bin:
print ("Programming memory at 0x%08X" % addr, end="\r") print("Programming memory at 0x%08X" % addr, end="\r")
stdout.flush() stdout.flush()
try: try:
# STM DFU bootloader erases always. # STM DFU bootloader erases always.
@ -243,19 +242,19 @@ if __name__ == "__main__":
except: except:
# Abort silent if bootloader does not support upload # Abort silent if bootloader does not support upload
break break
print ("Verifying memory at 0x%08X" % addr, end="\r") print("Verifying memory at 0x%08X" % addr, end="\r")
stdout.flush() stdout.flush()
if len > 1024 : if len > 1024:
size = 1024 size = 1024
else : else:
size = len size = len
if bin[:size] != bytearray(data[:size]) : if bin[:size] != bytearray(data[:size]):
print ("\nMismatch in block at 0x%08X" % addr) print("\nMismatch in block at 0x%08X" % addr)
break; break
bin = bin[1024:] bin = bin[1024:]
addr += 1024 addr += 1024
len -= 1024 len -= 1024
if len <= 0 : if len <= 0:
print("\nVerified!") print("\nVerified!")
stm32_manifest(dfudev) stm32_manifest(dfudev)