Skip to content

Commit 8056d98

Browse files
committed
Improve documentation
1 parent 7e96877 commit 8056d98

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/modulino/firmware_flasher.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
BUSY = const(0x76)
1010

1111
CMD_GET = const(0x00) # Gets the version and the allowed commands
12+
CMD_GET_LENGTH_V12 = const(20) # Length of the response data
1213
CMD_GET_VERSION = const(0x01) # Gets the protocol version
1314
CMD_GET_ID = const(0x02) # Get chip ID
1415
CMD_ERASE = const(0x44) # Erase memory
@@ -61,15 +62,15 @@ def wait_for_ack():
6162
return False
6263
return True
6364

64-
def execute_command(opcode, command_data, response_length = 0, verbose=False):
65+
def execute_command(opcode, command_params, response_length = 0, verbose=False):
6566
"""
6667
Execute an I2C command on the device.
6768
6869
:param opcode: The command opcode.
69-
:param command_buffer: The buffer containing the command data.
70-
:param response_length: The expected length of the response data.
70+
:param command_params: The buffer containing the command parameters.
71+
:param response_length: The expected length of the response data frame.
7172
:param verbose: Whether to print debug information.
72-
:return: The number of response bytes read, or -1 if an error occurred.
73+
:return: The number of response bytes read, or None if an error occurred.
7374
"""
7475
if verbose:
7576
print(f"🕵️ Executing command {hex(opcode)}")
@@ -80,8 +81,8 @@ def execute_command(opcode, command_data, response_length = 0, verbose=False):
8081
print(f"❌ Command not acknowledged: {hex(opcode)}")
8182
return None
8283

83-
if command_data is not None:
84-
i2c.writeto(BOOTLOADER_I2C_ADDRESS, command_data, True)
84+
if command_params is not None:
85+
i2c.writeto(BOOTLOADER_I2C_ADDRESS, command_params, True)
8586
if not wait_for_ack():
8687
print("❌ Command failed")
8788
return None
@@ -90,16 +91,12 @@ def execute_command(opcode, command_data, response_length = 0, verbose=False):
9091
return None
9192

9293
data = i2c.readfrom(BOOTLOADER_I2C_ADDRESS, response_length)
93-
amount_of_bytes = data[0] + 1
94-
95-
if verbose:
96-
print(f"📦 Received {amount_of_bytes} bytes")
9794

9895
if not wait_for_ack():
9996
print("❌ Failed completing command")
10097
return None
10198

102-
return data[1 : amount_of_bytes + 1]
99+
return data
103100

104101
def flash_firmware(firmware_path, verbose=False):
105102
"""
@@ -110,14 +107,20 @@ def flash_firmware(firmware_path, verbose=False):
110107
:param verbose: Whether to print debug information.
111108
:return: True if the flashing was successful, otherwise False.
112109
"""
113-
data = execute_command(CMD_GET, None, 20, verbose)
110+
data = execute_command(CMD_GET_VERSION, None, 1, verbose)
111+
if data is None:
112+
print("❌ Failed to get protocol version")
113+
return False
114+
print(f"ℹ️ Protocol version: {data[0] & 0xF}.{data[0] >> 4}")
115+
116+
data = execute_command(CMD_GET, None, CMD_GET_LENGTH_V12, verbose)
114117
if data is None:
115118
print("❌ Failed to get command list")
116119
return False
117120

118-
print(f"ℹ️ Bootloader version: {hex(data[0])}")
121+
print(f"ℹ️ Bootloader version: {(data[1] & 0xF)}.{data[1] >> 4}")
119122
print("👀 Supported commands:")
120-
print(", ".join([hex(byte) for byte in data[1:]]))
123+
print(", ".join([hex(byte) for byte in data[2:]]))
121124

122125
data = execute_command(CMD_GET_ID, None, 3, verbose)
123126
if data is None:
@@ -161,10 +164,9 @@ def write_firmware_page(command_params, firmware_data):
161164
"""
162165
Write a page of the firmware to the I2C device.
163166
164-
:param command_params: The buffer containing the command data.
167+
:param command_params: The buffer containing the command parameters.
165168
:param firmware_data: The buffer containing the firmware data.
166-
:param verbose: Whether to print debug information.
167-
:return: The number of bytes written, or -1 if an error occurred.
169+
:return: True if the page was written successfully, otherwise False.
168170
"""
169171
cmd = bytes([CMD_WRITE_NO_STRETCH, 0xFF ^ CMD_WRITE_NO_STRETCH])
170172
i2c.writeto(BOOTLOADER_I2C_ADDRESS, cmd)

0 commit comments

Comments
 (0)