Skip to content

Commit 8582294

Browse files
refactor(esptool_py): Move utility target creation to project level
This commit refactors the esptool_py component to provide utility functions for creating utility targets such as erase_flash, merge-bin and monitor. The following changes were done in this commit: - Added __esptool_py_setup_utility_targets() to create utility targets. - Utility target creation now happens in idf_build_executable() in build.cmake. - Removed more global scope processing and variables from esptool_py component project_include.cmake.
1 parent 3a1f343 commit 8582294

File tree

2 files changed

+93
-188
lines changed

2 files changed

+93
-188
lines changed

components/esptool_py/project_include.cmake

Lines changed: 90 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Set some global esptool.py variables
2-
#
1+
# esptool_py component project_include.cmake
2+
33
# Many of these are read when generating flash_app_args & flash_project_args
44
idf_build_get_property(target IDF_TARGET)
55
idf_build_get_property(python PYTHON)
@@ -15,192 +15,6 @@ set(ESPEFUSEPY ${python} "${CMAKE_CURRENT_LIST_DIR}/esptool/espefuse.py")
1515
set(ESPMONITOR ${python} -m esp_idf_monitor)
1616
set(ESPTOOLPY_CHIP "${chip_model}")
1717

18-
if(NOT CONFIG_APP_BUILD_TYPE_RAM AND CONFIG_APP_BUILD_GENERATE_BINARIES)
19-
if(CONFIG_BOOTLOADER_FLASH_DC_AWARE)
20-
# When set flash frequency to 120M, must keep 1st bootloader work under ``DOUT`` mode
21-
# because on some flash chips, 120M will modify the status register,
22-
# which will make ROM won't work.
23-
# This change intends to be for esptool only and the bootloader should keep use
24-
# ``DOUT`` mode.
25-
set(ESPFLASHMODE "dout")
26-
message("Note: HPM is enabled for the flash, force the ROM bootloader into DOUT mode for stable boot on")
27-
else()
28-
set(ESPFLASHMODE ${CONFIG_ESPTOOLPY_FLASHMODE})
29-
endif()
30-
set(ESPFLASHFREQ ${CONFIG_ESPTOOLPY_FLASHFREQ})
31-
set(ESPFLASHSIZE ${CONFIG_ESPTOOLPY_FLASHSIZE})
32-
33-
34-
set(esptool_elf2image_args
35-
--flash_mode ${ESPFLASHMODE}
36-
--flash_freq ${ESPFLASHFREQ}
37-
--flash_size ${ESPFLASHSIZE}
38-
)
39-
40-
if(BOOTLOADER_BUILD AND CONFIG_SECURE_BOOT_V2_ENABLED)
41-
# The bootloader binary needs to be 4KB aligned in order to append a secure boot V2 signature block.
42-
# If CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES is NOT set, the bootloader
43-
# image generated is not 4KB aligned for external HSM to sign it readily.
44-
# Following esptool option --pad-to-size 4KB generates a 4K aligned bootloader image.
45-
# In case of signing during build, espsecure.py "sign_data" operation handles the 4K alignment of the image.
46-
if(NOT CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
47-
list(APPEND esptool_elf2image_args --pad-to-size 4KB)
48-
endif()
49-
endif()
50-
51-
set(MMU_PAGE_SIZE ${CONFIG_MMU_PAGE_MODE})
52-
53-
if(NOT BOOTLOADER_BUILD)
54-
list(APPEND esptool_elf2image_args --elf-sha256-offset 0xb0)
55-
# For chips that support configurable MMU page size feature
56-
# If page size is configured to values other than the default "64KB" in menuconfig,
57-
# then we need to pass the actual size to flash-mmu-page-size arg
58-
if(NOT MMU_PAGE_SIZE STREQUAL "64KB")
59-
list(APPEND esptool_elf2image_args --flash-mmu-page-size ${MMU_PAGE_SIZE})
60-
endif()
61-
endif()
62-
63-
if(NOT CONFIG_SECURE_BOOT_ALLOW_SHORT_APP_PARTITION AND
64-
NOT BOOTLOADER_BUILD)
65-
if(CONFIG_SECURE_SIGNED_APPS_ECDSA_SCHEME)
66-
list(APPEND esptool_elf2image_args --secure-pad)
67-
elseif(CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME OR CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME)
68-
list(APPEND esptool_elf2image_args --secure-pad-v2)
69-
endif()
70-
endif()
71-
72-
if(CONFIG_ESPTOOLPY_HEADER_FLASHSIZE_UPDATE)
73-
# Set ESPFLASHSIZE to 'detect' *after* esptool_elf2image_args are generated,
74-
# as elf2image can't have 'detect' as an option...
75-
set(ESPFLASHSIZE detect)
76-
endif()
77-
78-
if(CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME)
79-
set(ESPFLASHSIZE keep)
80-
endif()
81-
endif()
82-
83-
# We still set "--min-rev" to keep the app compatible with older bootloaders where this field is controlled.
84-
if(CONFIG_IDF_TARGET_ESP32)
85-
# for this chip min_rev is major revision
86-
math(EXPR min_rev "${CONFIG_ESP_REV_MIN_FULL} / 100")
87-
endif()
88-
if(CONFIG_IDF_TARGET_ESP32C3)
89-
# for this chip min_rev is minor revision
90-
math(EXPR min_rev "${CONFIG_ESP_REV_MIN_FULL} % 100")
91-
endif()
92-
93-
if(min_rev)
94-
list(APPEND esptool_elf2image_args --min-rev ${min_rev})
95-
endif()
96-
97-
list(APPEND esptool_elf2image_args --min-rev-full ${CONFIG_ESP_REV_MIN_FULL})
98-
list(APPEND esptool_elf2image_args --max-rev-full ${CONFIG_ESP_REV_MAX_FULL})
99-
100-
if(CONFIG_ESPTOOLPY_HEADER_FLASHSIZE_UPDATE)
101-
# Set ESPFLASHSIZE to 'detect' *after* esptool_elf2image_args are generated,
102-
# as elf2image can't have 'detect' as an option...
103-
set(ESPFLASHSIZE detect)
104-
endif()
105-
106-
if(CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME)
107-
set(ESPFLASHSIZE keep)
108-
endif()
109-
110-
idf_build_get_property(build_dir BUILD_DIR)
111-
112-
idf_build_get_property(elf_name EXECUTABLE_NAME GENERATOR_EXPRESSION)
113-
idf_build_get_property(elf EXECUTABLE GENERATOR_EXPRESSION)
114-
115-
116-
add_custom_target(erase_flash
117-
COMMAND ${CMAKE_COMMAND}
118-
-D "IDF_PATH=${idf_path}"
119-
-D "SERIAL_TOOL=${ESPTOOLPY}"
120-
-D "SERIAL_TOOL_ARGS=erase_flash"
121-
-P run_serial_tool.cmake
122-
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
123-
USES_TERMINAL
124-
VERBATIM
125-
)
126-
127-
set(MERGE_BIN_ARGS merge_bin)
128-
if(DEFINED ENV{ESP_MERGE_BIN_OUTPUT})
129-
list(APPEND MERGE_BIN_ARGS "-o" "$ENV{ESP_MERGE_BIN_OUTPUT}")
130-
else()
131-
if(DEFINED ENV{ESP_MERGE_BIN_FORMAT} AND "$ENV{ESP_MERGE_BIN_FORMAT}" STREQUAL "hex")
132-
list(APPEND MERGE_BIN_ARGS "-o" "${CMAKE_CURRENT_BINARY_DIR}/merged-binary.hex")
133-
else()
134-
list(APPEND MERGE_BIN_ARGS "-o" "${CMAKE_CURRENT_BINARY_DIR}/merged-binary.bin")
135-
endif()
136-
endif()
137-
138-
if(DEFINED ENV{ESP_MERGE_BIN_FORMAT})
139-
list(APPEND MERGE_BIN_ARGS "-f" "$ENV{ESP_MERGE_BIN_FORMAT}")
140-
endif()
141-
142-
list(APPEND MERGE_BIN_ARGS "@${CMAKE_CURRENT_BINARY_DIR}/flash_args")
143-
144-
add_custom_target(merge-bin
145-
COMMAND ${CMAKE_COMMAND}
146-
-D "IDF_PATH=${idf_path}"
147-
-D "SERIAL_TOOL=${ESPTOOLPY}"
148-
-D "SERIAL_TOOL_ARGS=${MERGE_BIN_ARGS}"
149-
-D "WORKING_DIRECTORY=${CMAKE_CURRENT_BINARY_DIR}"
150-
-P run_serial_tool.cmake
151-
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
152-
DEPENDS gen_project_binary bootloader
153-
USES_TERMINAL
154-
VERBATIM
155-
)
156-
157-
set(MONITOR_ARGS "")
158-
159-
list(APPEND MONITOR_ARGS "--toolchain-prefix;${_CMAKE_TOOLCHAIN_PREFIX};")
160-
161-
if(CONFIG_ESP_COREDUMP_DECODE)
162-
list(APPEND MONITOR_ARGS "--decode-coredumps;${CONFIG_ESP_COREDUMP_DECODE};")
163-
endif()
164-
165-
list(APPEND MONITOR_ARGS "--target;${target};")
166-
167-
list(APPEND MONITOR_ARGS "--revision;${CONFIG_ESP_REV_MIN_FULL};")
168-
169-
if(CONFIG_IDF_TARGET_ARCH_RISCV)
170-
list(APPEND MONITOR_ARGS "--decode-panic;backtrace;")
171-
endif()
172-
173-
list(APPEND MONITOR_ARGS "$<TARGET_FILE:$<GENEX_EVAL:${elf}>>")
174-
175-
add_custom_target(monitor
176-
COMMAND ${CMAKE_COMMAND}
177-
-D "IDF_PATH=${idf_path}"
178-
-D "SERIAL_TOOL=${ESPMONITOR}"
179-
-D "SERIAL_TOOL_ARGS=${MONITOR_ARGS}"
180-
-D "WORKING_DIRECTORY=${build_dir}"
181-
-P run_serial_tool.cmake
182-
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
183-
USES_TERMINAL
184-
VERBATIM
185-
)
186-
187-
set(esptool_flash_main_args "--before=${CONFIG_ESPTOOLPY_BEFORE}")
188-
189-
if(CONFIG_SECURE_BOOT OR CONFIG_SECURE_FLASH_ENC_ENABLED)
190-
# If security enabled then override post flash option
191-
list(APPEND esptool_flash_main_args "--after=no_reset")
192-
else()
193-
list(APPEND esptool_flash_main_args "--after=${CONFIG_ESPTOOLPY_AFTER}")
194-
endif()
195-
196-
if(CONFIG_ESPTOOLPY_NO_STUB)
197-
list(APPEND esptool_flash_main_args "--no-stub")
198-
endif()
199-
200-
idf_component_set_property(esptool_py FLASH_ARGS "${esptool_flash_main_args}")
201-
idf_component_set_property(esptool_py FLASH_SUB_ARGS "--flash_mode ${ESPFLASHMODE} --flash_freq ${ESPFLASHFREQ} \
202-
--flash_size ${ESPFLASHSIZE}")
203-
20418
# esptool_py_partition_needs_encryption
20519
#
20620
# @brief Determine if a partition needs to be encrypted when flash encryption is enabled.
@@ -849,6 +663,94 @@ function(__idf_build_secure_binary UNSIGNED_BIN_FILENAME SIGNED_BIN_FILENAME TAR
849663
endif()
850664
endfunction()
851665

666+
# __esptool_py_setup_utility_targets
667+
#
668+
# @brief Sets up common utility targets like `erase_flash`, `merge-bin`, and `monitor`
669+
#
670+
function(__esptool_py_setup_utility_targets)
671+
__ensure_esptool_py_setup()
672+
673+
idf_build_get_property(build_dir BUILD_DIR)
674+
idf_build_get_property(idf_path IDF_PATH)
675+
idf_build_get_property(python PYTHON)
676+
idf_build_get_property(target IDF_TARGET)
677+
idf_build_get_property(elf_name EXECUTABLE_NAME GENERATOR_EXPRESSION)
678+
idf_build_get_property(elf EXECUTABLE GENERATOR_EXPRESSION)
679+
idf_component_get_property(esptool_py_cmd esptool_py ESPTOOLPY_CMD)
680+
idf_component_get_property(esptool_py_dir esptool_py COMPONENT_DIR)
681+
682+
add_custom_target(erase_flash
683+
COMMAND ${CMAKE_COMMAND}
684+
-D "IDF_PATH=${idf_path}"
685+
-D "SERIAL_TOOL=${esptool_py_cmd}"
686+
-D "SERIAL_TOOL_ARGS=erase_flash"
687+
-P run_serial_tool.cmake
688+
WORKING_DIRECTORY ${esptool_py_dir}
689+
USES_TERMINAL
690+
VERBATIM
691+
)
692+
693+
set(MERGE_BIN_ARGS merge_bin)
694+
if(DEFINED ENV{ESP_MERGE_BIN_OUTPUT})
695+
list(APPEND MERGE_BIN_ARGS "-o" "$ENV{ESP_MERGE_BIN_OUTPUT}")
696+
else()
697+
if(DEFINED ENV{ESP_MERGE_BIN_FORMAT} AND "$ENV{ESP_MERGE_BIN_FORMAT}" STREQUAL "hex")
698+
list(APPEND MERGE_BIN_ARGS "-o" "${CMAKE_CURRENT_BINARY_DIR}/merged-binary.hex")
699+
else()
700+
list(APPEND MERGE_BIN_ARGS "-o" "${CMAKE_CURRENT_BINARY_DIR}/merged-binary.bin")
701+
endif()
702+
endif()
703+
704+
if(DEFINED ENV{ESP_MERGE_BIN_FORMAT})
705+
list(APPEND MERGE_BIN_ARGS "-f" "$ENV{ESP_MERGE_BIN_FORMAT}")
706+
endif()
707+
708+
list(APPEND MERGE_BIN_ARGS "@${CMAKE_CURRENT_BINARY_DIR}/flash_args")
709+
710+
add_custom_target(merge-bin
711+
COMMAND ${CMAKE_COMMAND}
712+
-D "IDF_PATH=${idf_path}"
713+
-D "SERIAL_TOOL=${esptool_py_cmd}"
714+
-D "SERIAL_TOOL_ARGS=${MERGE_BIN_ARGS}"
715+
-D "WORKING_DIRECTORY=${CMAKE_CURRENT_BINARY_DIR}"
716+
-P run_serial_tool.cmake
717+
WORKING_DIRECTORY ${esptool_py_dir}
718+
DEPENDS gen_project_binary bootloader
719+
USES_TERMINAL
720+
VERBATIM
721+
)
722+
723+
set(MONITOR_ARGS "")
724+
725+
list(APPEND MONITOR_ARGS "--toolchain-prefix;${_CMAKE_TOOLCHAIN_PREFIX};")
726+
727+
if(CONFIG_ESP_COREDUMP_DECODE)
728+
list(APPEND MONITOR_ARGS "--decode-coredumps;${CONFIG_ESP_COREDUMP_DECODE};")
729+
endif()
730+
731+
list(APPEND MONITOR_ARGS "--target;${target};")
732+
733+
list(APPEND MONITOR_ARGS "--revision;${CONFIG_ESP_REV_MIN_FULL};")
734+
735+
if(CONFIG_IDF_TARGET_ARCH_RISCV)
736+
list(APPEND MONITOR_ARGS "--decode-panic;backtrace;")
737+
endif()
738+
739+
list(APPEND MONITOR_ARGS "$<TARGET_FILE:$<GENEX_EVAL:${elf}>>")
740+
741+
add_custom_target(monitor
742+
COMMAND ${CMAKE_COMMAND}
743+
-D "IDF_PATH=${idf_path}"
744+
-D "SERIAL_TOOL=${python} -m esp_idf_monitor"
745+
-D "SERIAL_TOOL_ARGS=${MONITOR_ARGS}"
746+
-D "WORKING_DIRECTORY=${build_dir}"
747+
-P run_serial_tool.cmake
748+
WORKING_DIRECTORY ${esptool_py_dir}
749+
USES_TERMINAL
750+
VERBATIM
751+
)
752+
endfunction()
753+
852754
# __esptool_py_setup_main_flash_target
853755
#
854756
# @brief Sets up the main `flash` target and its dependencies.

tools/cmake/build.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,9 @@ function(idf_build_executable elf)
845845
esptool_py_flash_target_image(app-flash app "${app_partition_offset}" "${build_dir}/${project_bin}")
846846
esptool_py_flash_target_image(flash app "${app_partition_offset}" "${build_dir}/${project_bin}")
847847

848+
# Setup utility targets such as monitor, erase_flash, merge-bin
849+
__esptool_py_setup_utility_targets()
850+
848851
# Setup the main flash target and dependencies
849852
__esptool_py_setup_main_flash_target()
850853

0 commit comments

Comments
 (0)