Skip to content

Commit 3e9897b

Browse files
committed
Rework process handling in the extra frimware flasher script
1 parent 1061b98 commit 3e9897b

1 file changed

Lines changed: 25 additions & 20 deletions

File tree

extra/firmware/extra_firmware_flasher.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,45 +81,37 @@ def erase_flash(flasher_tool_executable_path, arduino_tools_dir, flasher_tool):
8181
print("")
8282
print("Erasing flash memory...")
8383
if flasher_tool == 'openocd':
84-
flasher_process = subprocess.Popen(
85-
[flasher_tool_executable_path, "-d2", "-s", arduino_tools_dir + "/openocd/0.12.0-arduino1-static/share/openocd/scripts/", "-f", "interface/cmsis-dap.cfg", "-f", "target/efm32s2_g23.cfg", "-c", "init; reset_config srst_nogate; reset halt; flash erase_sector 0 1 last; exit"]
84+
run_flasher(
85+
[flasher_tool_executable_path, "-d2", "-s", arduino_tools_dir + "/openocd/0.12.0-arduino1-static/share/openocd/scripts/", "-f", "interface/cmsis-dap.cfg", "-f", "target/efm32s2_g23.cfg", "-c", "init; reset_config srst_nogate; reset halt; flash erase_sector 0 1 last; exit"],
86+
"Flash erase",
8687
)
87-
flasher_process.communicate(timeout=30)
8888
elif flasher_tool == 'simplicitycommander':
89-
flasher_process = subprocess.Popen(
90-
[flasher_tool_executable_path, "device", "masserase"]
89+
run_flasher(
90+
[flasher_tool_executable_path, "device", "masserase"],
91+
"Flash erase",
9192
)
92-
flasher_process.communicate(timeout=30)
9393
else:
9494
print("Unsupported flasher tool")
9595
sys.exit(1)
9696

97-
if flasher_process.returncode != 0:
98-
print(f"Error: Flash erase failed with return code '{flasher_process.returncode}'")
99-
sys.exit(flasher_process.returncode)
100-
10197

10298
def flash_binary(flasher_tool_executable_path, binary_file, arduino_tools_dir, flasher_tool):
10399
print("")
104100
print("Flashing binary file: {}".format(binary_file))
105101
if flasher_tool == 'openocd':
106-
flasher_process = subprocess.Popen(
107-
[flasher_tool_executable_path, "-d2", "-s", arduino_tools_dir + "/openocd/0.12.0-arduino1-static/share/openocd/scripts/", "-f", "interface/cmsis-dap.cfg", "-f", "target/efm32s2_g23.cfg", "-c", "init; reset_config srst_nogate; reset halt; program {" + binary_file + "}; reset; exit"]
102+
run_flasher(
103+
[flasher_tool_executable_path, "-d2", "-s", arduino_tools_dir + "/openocd/0.12.0-arduino1-static/share/openocd/scripts/", "-f", "interface/cmsis-dap.cfg", "-f", "target/efm32s2_g23.cfg", "-c", "init; reset_config srst_nogate; reset halt; program {" + binary_file + "}; reset; exit"],
104+
"Flashing",
108105
)
109-
flasher_process.communicate(timeout=30)
110106
elif flasher_tool == 'simplicitycommander':
111-
flasher_process = subprocess.Popen(
112-
[flasher_tool_executable_path, "flash", binary_file, "-v"]
107+
run_flasher(
108+
[flasher_tool_executable_path, "flash", binary_file, "-v"],
109+
"Flashing",
113110
)
114-
flasher_process.communicate(timeout=30)
115111
else:
116112
print("Unsupported flasher tool")
117113
sys.exit(1)
118114

119-
if flasher_process.returncode != 0:
120-
print(f"Error: Flashing failed with return code '{flasher_process.returncode}'")
121-
sys.exit(flasher_process.returncode)
122-
123115

124116
def get_selected_board_config_from_arguments():
125117
def print_available_configs():
@@ -200,6 +192,19 @@ def get_flasher_tool_executable_path(flasher_tool, arduino_tools_dir):
200192
return flasher_tool_executable_path
201193

202194

195+
def run_flasher(cmd, operation_name):
196+
flasher_timeout_seconds = 60
197+
try:
198+
result = subprocess.run(cmd, timeout=flasher_timeout_seconds)
199+
except subprocess.TimeoutExpired:
200+
print(f"Error: {operation_name} timed out after {flasher_timeout_seconds} seconds")
201+
sys.exit(1)
202+
203+
if result.returncode != 0:
204+
print(f"Error: {operation_name} failed with return code '{result.returncode}'")
205+
sys.exit(result.returncode)
206+
207+
203208
nanomatter_config = {
204209
'name': 'nano_matter',
205210
'flasher_tool': 'openocd',

0 commit comments

Comments
 (0)