@@ -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