Skip to content

Commit 70ca732

Browse files
committed
Load firmware file inside flash function
1 parent 9a7ea5e commit 70ca732

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/modulino/firmware_flasher.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def execute_command(opcode, command_data, response_length = 0, verbose=True):
9999

100100
return data[1 : amount_of_bytes + 1]
101101

102-
def flash_firmware(firmware, verbose=True):
102+
def flash_firmware(firmware_path, verbose=True):
103103
"""
104104
Flash the firmware to the I2C device.
105105
@@ -129,13 +129,16 @@ def flash_firmware(firmware, verbose=True):
129129
return True # Debug. Remove when done
130130

131131
print("Erasing memory...")
132-
erase_buffer = bytearray([0xFF, 0xFF, 0x0]) # Mass erase flash
133-
execute_command(CMD_ERASE, erase_buffer, 0, verbose)
132+
erase_params = bytearray([0xFF, 0xFF, 0x0]) # Mass erase flash
133+
execute_command(CMD_ERASE, erase_params, 0, verbose)
134134

135-
for i in range(0, len(firmware), 128):
135+
with open(firmware_path, 'rb') as file:
136+
firmware_data = file.read()
137+
138+
for i in range(0, len(firmware_data), 128):
136139
progress_bar(i, length)
137140
write_buffer = bytearray([8, 0, i // 256, i % 256])
138-
if write_firmware_page(write_buffer, 5, firmware[i:i + 128], 128, verbose) < 0:
141+
if write_firmware_page(write_buffer, 5, firmware_data[i:i + 128], 128, verbose) < 0:
139142
print(f"Failed to write page {hex(i)}")
140143
return False
141144
time.sleep(0.01)
@@ -266,10 +269,7 @@ def setup():
266269

267270
print(f"Flashing {bin_file} to device at address {hex(BOOTLOADER_I2C_ADDRESS)}")
268271

269-
with open(bin_file, 'rb') as file:
270-
firmware = file.read()
271-
272-
if flash_firmware(firmware):
272+
if flash_firmware(bin_file):
273273
print("Firmware flashed successfully")
274274
else:
275275
print("Failed to flash firmware")

0 commit comments

Comments
 (0)