Skip to content

Commit a8ff795

Browse files
committed
make firmware flashing simpler to prevent Read Out Protection FF on newer STM32F4 revisions
1 parent 9ffafaa commit a8ff795

File tree

2 files changed

+32
-62
lines changed

2 files changed

+32
-62
lines changed

rosbot_utils/rosbot_utils/flash-firmware-usb.py

+16-30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/python3
1+
#!/usr/bin/env python3
22

33
# Copyright 2024 Husarion sp. z o.o.
44
#
@@ -47,6 +47,7 @@ def enter_bootloader_mode(self):
4747
# self.ftdi.set_cbus_direction(0b11,0b00) # set CBUS0 and CBUS1 to input
4848
time.sleep(0.1)
4949
self.ftdi.close()
50+
time.sleep(1.0)
5051

5152
def exit_bootloader_mode(self):
5253
self.ftdi.open_from_url(url=self.device)
@@ -60,41 +61,26 @@ def exit_bootloader_mode(self):
6061
time.sleep(0.1)
6162
self.ftdi.close()
6263

63-
def try_flash_operation(self, operation_name):
64-
print(f"\n{operation_name} operation started")
64+
def flash_firmware(self):
6565
self.enter_bootloader_mode()
66-
sh.usbreset("0403:6015")
67-
for i in range(self.max_approach_no):
68-
print(f"Attempt {i + 1}/{self.max_approach_no}")
69-
try:
70-
if operation_name == "Flashing":
71-
flash_args = ["-v", "-w", self.binary_file, "-b", "115200"]
72-
sh.stm32flash(self.port, *flash_args, _out=sys.stdout)
73-
print("Success! The robot firmware has been uploaded.")
74-
elif operation_name == "Write-UnProtection":
75-
sh.stm32flash(self.port, "-u")
76-
elif operation_name == "Read-UnProtection":
77-
sh.stm32flash(self.port, "-k")
78-
else:
79-
raise ("Unknown operation.")
80-
break
81-
except Exception as e:
82-
stderr = e.stderr.decode("utf-8")
83-
if stderr:
84-
print(f"ERROR: {stderr.strip()}")
85-
86-
print("Success!")
87-
self.exit_bootloader_mode()
8866

89-
def flash_firmware(self):
67+
# Disable the flash read-protection
68+
flash_args = ["-k", "-b", "115200"]
69+
sh.stm32flash(self.port, *flash_args, _out=sys.stdout)
70+
71+
time.sleep(0.5)
72+
9073
# Disable the flash write-protection
91-
self.try_flash_operation("Write-UnProtection")
74+
flash_args = ["-u", "-b", "115200"]
75+
sh.stm32flash(self.port, *flash_args, _out=sys.stdout)
9276

93-
# Disable the flash read-protection
94-
self.try_flash_operation("Read-UnProtection")
77+
time.sleep(0.5)
9578

9679
# Flashing the firmware
97-
self.try_flash_operation("Flashing")
80+
flash_args = ["-v", "-w", self.binary_file, "-b", "115200"]
81+
sh.stm32flash(self.port, *flash_args, _out=sys.stdout)
82+
83+
self.exit_bootloader_mode()
9884

9985
sh.usbreset("0403:6015")
10086

rosbot_utils/rosbot_utils/flash-firmware.py

+16-32
Original file line numberDiff line numberDiff line change
@@ -95,52 +95,36 @@ def __init__(self, binary_file):
9595
def enter_bootloader_mode(self):
9696
self.boot0_pin.set_value(1)
9797
self.reset_pin.set_value(1)
98-
time.sleep(0.2)
98+
time.sleep(0.1)
9999
self.reset_pin.set_value(0)
100-
time.sleep(0.2)
100+
time.sleep(1.0)
101101

102102
def exit_bootloader_mode(self):
103103
self.boot0_pin.set_value(0)
104104
self.reset_pin.set_value(1)
105-
time.sleep(0.2)
105+
time.sleep(0.1)
106106
self.reset_pin.set_value(0)
107-
time.sleep(0.2)
108107

109-
def try_flash_operation(self, operation_name):
110-
print(f"\n{operation_name} operation started")
108+
def flash_firmware(self):
111109
self.enter_bootloader_mode()
112-
for i in range(self.max_approach_no):
113-
print(f"Attempt {i + 1}/{self.max_approach_no}")
114-
try:
115-
if operation_name == "Flashing":
116-
flash_args = ["-v", "-w", self.binary_file, "-b", "115200"]
117-
sh.stm32flash(self.port, *flash_args, _out=sys.stdout)
118-
print("Success! The robot firmware has been uploaded.")
119-
elif operation_name == "Write-UnProtection":
120-
sh.stm32flash(self.port, "-u")
121-
elif operation_name == "Read-UnProtection":
122-
sh.stm32flash(self.port, "-k")
123-
else:
124-
raise ("Unknown operation.")
125-
break
126-
except Exception as e:
127-
stderr = e.stderr.decode("utf-8")
128-
if stderr:
129-
print(f"ERROR: {stderr.strip()}")
130-
131-
print("Success!")
132-
self.exit_bootloader_mode()
133110

134-
def flash_firmware(self):
111+
# Disable the flash read-protection
112+
flash_args = ["-k", "-b", "115200"]
113+
sh.stm32flash(self.port, *flash_args, _out=sys.stdout)
114+
115+
time.sleep(0.5)
116+
135117
# Disable the flash write-protection
136-
self.try_flash_operation("Write-UnProtection")
118+
flash_args = ["-u", "-b", "115200"]
119+
sh.stm32flash(self.port, *flash_args, _out=sys.stdout)
137120

138-
# Disable the flash read-protection
139-
self.try_flash_operation("Read-UnProtection")
121+
time.sleep(0.5)
140122

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

127+
self.exit_bootloader_mode()
144128

145129
def main():
146130
parser = argparse.ArgumentParser(

0 commit comments

Comments
 (0)