Skip to content

Commit

Permalink
make firmware flashing simpler to prevent Read Out Protection FF on n…
Browse files Browse the repository at this point in the history
…ewer STM32F4 revisions
  • Loading branch information
DominikN committed Jan 30, 2025
1 parent a0def79 commit 0a87c3e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 62 deletions.
46 changes: 16 additions & 30 deletions rosbot_utils/rosbot_utils/flash-firmware-usb.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python3
#!/usr/bin/env python3

# Copyright 2024 Husarion sp. z o.o.
#
Expand Down Expand Up @@ -47,6 +47,7 @@ def enter_bootloader_mode(self):
# self.ftdi.set_cbus_direction(0b11,0b00) # set CBUS0 and CBUS1 to input
time.sleep(0.1)
self.ftdi.close()
time.sleep(1.0)

def exit_bootloader_mode(self):
self.ftdi.open_from_url(url=self.device)
Expand All @@ -60,41 +61,26 @@ def exit_bootloader_mode(self):
time.sleep(0.1)
self.ftdi.close()

def try_flash_operation(self, operation_name):
print(f"\n{operation_name} operation started")
def flash_firmware(self):
self.enter_bootloader_mode()
sh.usbreset("0403:6015")
for i in range(self.max_approach_no):
print(f"Attempt {i+1}/{self.max_approach_no}")
try:
if operation_name == "Flashing":
flash_args = ["-v", "-w", self.binary_file, "-b", "115200"]
sh.stm32flash(self.port, *flash_args, _out=sys.stdout)
print("Success! The robot firmware has been uploaded.")
elif operation_name == "Write-UnProtection":
sh.stm32flash(self.port, "-u")
elif operation_name == "Read-UnProtection":
sh.stm32flash(self.port, "-k")
else:
raise ("Unknown operation.")
break
except Exception as e:
stderr = e.stderr.decode("utf-8")
if stderr:
print(f"ERROR: {stderr.strip()}")

print("Success!")
self.exit_bootloader_mode()

def flash_firmware(self):
# Disable the flash read-protection
flash_args = ["-k", "-b", "115200"]
sh.stm32flash(self.port, *flash_args, _out=sys.stdout)

time.sleep(0.5)

# Disable the flash write-protection
self.try_flash_operation("Write-UnProtection")
flash_args = ["-u", "-b", "115200"]
sh.stm32flash(self.port, *flash_args, _out=sys.stdout)

# Disable the flash read-protection
self.try_flash_operation("Read-UnProtection")
time.sleep(0.5)

# Flashing the firmware
self.try_flash_operation("Flashing")
flash_args = ["-v", "-w", self.binary_file, "-b", "115200"]
sh.stm32flash(self.port, *flash_args, _out=sys.stdout)

self.exit_bootloader_mode()

sh.usbreset("0403:6015")

Expand Down
48 changes: 16 additions & 32 deletions rosbot_utils/rosbot_utils/flash-firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,52 +95,36 @@ def __init__(self, binary_file):
def enter_bootloader_mode(self):
self.boot0_pin.set_value(1)
self.reset_pin.set_value(1)
time.sleep(0.2)
time.sleep(0.1)
self.reset_pin.set_value(0)
time.sleep(0.2)
time.sleep(1.0)

def exit_bootloader_mode(self):
self.boot0_pin.set_value(0)
self.reset_pin.set_value(1)
time.sleep(0.2)
time.sleep(0.1)
self.reset_pin.set_value(0)
time.sleep(0.2)

def try_flash_operation(self, operation_name):
print(f"\n{operation_name} operation started")
def flash_firmware(self):
self.enter_bootloader_mode()
for i in range(self.max_approach_no):
print(f"Attempt {i+1}/{self.max_approach_no}")
try:
if operation_name == "Flashing":
flash_args = ["-v", "-w", self.binary_file, "-b", "115200"]
sh.stm32flash(self.port, *flash_args, _out=sys.stdout)
print("Success! The robot firmware has been uploaded.")
elif operation_name == "Write-UnProtection":
sh.stm32flash(self.port, "-u")
elif operation_name == "Read-UnProtection":
sh.stm32flash(self.port, "-k")
else:
raise ("Unknown operation.")
break
except Exception as e:
stderr = e.stderr.decode("utf-8")
if stderr:
print(f"ERROR: {stderr.strip()}")

print("Success!")
self.exit_bootloader_mode()

def flash_firmware(self):
# Disable the flash read-protection
flash_args = ["-k", "-b", "115200"]
sh.stm32flash(self.port, *flash_args, _out=sys.stdout)

time.sleep(0.5)

# Disable the flash write-protection
self.try_flash_operation("Write-UnProtection")
flash_args = ["-u", "-b", "115200"]
sh.stm32flash(self.port, *flash_args, _out=sys.stdout)

# Disable the flash read-protection
self.try_flash_operation("Read-UnProtection")
time.sleep(0.5)

# Flashing the firmware
self.try_flash_operation("Flashing")
flash_args = ["-v", "-w", self.binary_file, "-b", "115200"]
sh.stm32flash(self.port, *flash_args, _out=sys.stdout)

self.exit_bootloader_mode()

def main():
parser = argparse.ArgumentParser(
Expand Down

0 comments on commit 0a87c3e

Please sign in to comment.