Skip to content

Commit 1143b76

Browse files
committed
Merge branch 'fix/idfpy_uf2_fail' into 'master'
fix(uf2): Ignore ESPBAUD, ESPPORT when calling idf.py uf2[-app] Closes IDF-10768 See merge request espressif/esp-idf!33086
2 parents 4b3a5e9 + d77d1f5 commit 1143b76

File tree

4 files changed

+87
-25
lines changed

4 files changed

+87
-25
lines changed

components/esptool_py/project_include.cmake

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ set(ESPTOOLPY ${python} "$ENV{ESPTOOL_WRAPPER}" "${CMAKE_CURRENT_LIST_DIR}/espto
1212
set(ESPSECUREPY ${python} "${CMAKE_CURRENT_LIST_DIR}/esptool/espsecure.py")
1313
set(ESPEFUSEPY ${python} "${CMAKE_CURRENT_LIST_DIR}/esptool/espefuse.py")
1414
set(ESPMONITOR ${python} -m esp_idf_monitor)
15-
set(ESPMKUF2 ${python} "${idf_path}/tools/mkuf2.py" write --chip ${chip_model})
1615
set(ESPTOOLPY_CHIP "${chip_model}")
1716

1817
if(NOT CONFIG_APP_BUILD_TYPE_RAM AND CONFIG_APP_BUILD_GENERATE_BINARIES)
@@ -197,30 +196,6 @@ add_custom_target(erase_flash
197196
VERBATIM
198197
)
199198

200-
set(UF2_ARGS --json "${CMAKE_CURRENT_BINARY_DIR}/flasher_args.json")
201-
202-
add_custom_target(uf2
203-
COMMAND ${CMAKE_COMMAND}
204-
-D "IDF_PATH=${idf_path}"
205-
-D "SERIAL_TOOL=${ESPMKUF2}"
206-
-D "SERIAL_TOOL_ARGS=${UF2_ARGS};-o;${CMAKE_CURRENT_BINARY_DIR}/uf2.bin"
207-
-P run_serial_tool.cmake
208-
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
209-
USES_TERMINAL
210-
VERBATIM
211-
)
212-
213-
add_custom_target(uf2-app
214-
COMMAND ${CMAKE_COMMAND}
215-
-D "IDF_PATH=${idf_path}"
216-
-D "SERIAL_TOOL=${ESPMKUF2}"
217-
-D "SERIAL_TOOL_ARGS=${UF2_ARGS};-o;${CMAKE_CURRENT_BINARY_DIR}/uf2-app.bin;--bin;app"
218-
-P run_serial_tool.cmake
219-
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
220-
USES_TERMINAL
221-
VERBATIM
222-
)
223-
224199
set(MERGE_BIN_ARGS merge_bin)
225200
if(DEFINED ENV{ESP_MERGE_BIN_OUTPUT})
226201
list(APPEND MERGE_BIN_ARGS "-o" "$ENV{ESP_MERGE_BIN_OUTPUT}")

tools/cmake/project.cmake

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,33 @@ macro(project project_name)
884884
# Add DFU build and flash targets
885885
__add_dfu_targets()
886886

887+
# Add uf2 related targets
888+
idf_build_get_property(idf_path IDF_PATH)
889+
idf_build_get_property(python PYTHON)
890+
891+
set(UF2_ARGS --json "${CMAKE_CURRENT_BINARY_DIR}/flasher_args.json")
892+
set(UF2_CMD ${python} "${idf_path}/tools/mkuf2.py" write --chip ${chip_model})
893+
894+
add_custom_target(uf2
895+
COMMAND ${CMAKE_COMMAND}
896+
-D "IDF_PATH=${idf_path}"
897+
-D "UF2_CMD=${UF2_CMD}"
898+
-D "UF2_ARGS=${UF2_ARGS};-o;${CMAKE_CURRENT_BINARY_DIR}/uf2.bin"
899+
-P "${idf_path}/tools/cmake/run_uf2_cmds.cmake"
900+
USES_TERMINAL
901+
VERBATIM
902+
)
903+
904+
add_custom_target(uf2-app
905+
COMMAND ${CMAKE_COMMAND}
906+
-D "IDF_PATH=${idf_path}"
907+
-D "UF2_CMD=${UF2_CMD}"
908+
-D "UF2_ARGS=${UF2_ARGS};-o;${CMAKE_CURRENT_BINARY_DIR}/uf2-app.bin;--bin;app"
909+
-P "${idf_path}/tools/cmake/run_uf2_cmds.cmake"
910+
USES_TERMINAL
911+
VERBATIM
912+
)
913+
887914
idf_build_executable(${project_elf})
888915

889916
__project_info("${test_components}")

tools/cmake/run_uf2_cmds.cmake

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# A CMake script to run dfu-util from within ninja or make
2+
# or another cmake-based build runner
3+
#
4+
# It is recommended to NOT USE this CMake script directly
5+
6+
cmake_minimum_required(VERSION 3.16)
7+
8+
set(UF2_CMD "${UF2_CMD}")
9+
set(UF2_ARGS "${UF2_ARGS}")
10+
11+
if(NOT UF2_CMD)
12+
message(FATAL_ERROR "UF2_CMD must be specified on the CMake command line.")
13+
endif()
14+
15+
if(NOT UF2_ARGS)
16+
message(FATAL_ERROR "UF2_ARGS must be specified on the CMake command line.")
17+
endif()
18+
19+
set(uf2_cmd_with_args ${UF2_CMD})
20+
list(APPEND uf2_cmd_with_args ${UF2_ARGS})
21+
22+
execute_process(COMMAND ${uf2_cmd_with_args}
23+
RESULT_VARIABLE result)
24+
25+
if(${result})
26+
message(FATAL_ERROR "${UF2_CMD} failed")
27+
endif()

tools/test_idf_py/test_idf_py.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,5 +549,38 @@ def test_merge_bin(self):
549549
self.assertIn(f'Merged binary {merged_binary_name} will be created in the build directory...', output)
550550

551551

552+
class TestUF2Commands(TestWrapperCommands):
553+
"""
554+
Test if uf2 commands are invoked as expected.
555+
This test is not testing the functionality of mkuf2.py/idf.py uf2, but the invocation of the command from idf.py.
556+
"""
557+
558+
def test_uf2(self):
559+
uf2_command = [sys.executable, idf_py_path, 'uf2']
560+
output = self.call_command(uf2_command)
561+
self.assertIn('Executing:', output)
562+
563+
def test_uf2_with_envvars(self):
564+
# Values do not really matter, they should not be used.
565+
os.environ['ESPBAUD'] = '115200'
566+
os.environ['ESPPORT'] = '/dev/ttyUSB0'
567+
self.test_uf2()
568+
os.environ.pop('ESPBAUD')
569+
os.environ.pop('ESPPORT')
570+
571+
def test_uf2_app(self):
572+
uf2_app_command = [sys.executable, idf_py_path, 'uf2-app']
573+
output = self.call_command(uf2_app_command)
574+
self.assertIn('Executing:', output)
575+
576+
def test_uf2_app_with_envvars(self):
577+
# Values do not really matter, they should not be used.
578+
os.environ['ESPBAUD'] = '115200'
579+
os.environ['ESPPORT'] = '/dev/ttyUSB0'
580+
self.test_uf2_app()
581+
os.environ.pop('ESPBAUD')
582+
os.environ.pop('ESPPORT')
583+
584+
552585
if __name__ == '__main__':
553586
main()

0 commit comments

Comments
 (0)