From 0fe5868cf28ee7820e2b1684bd753d4d93fba9d5 Mon Sep 17 00:00:00 2001 From: Sudeep Mohanty Date: Mon, 17 Nov 2025 16:21:19 +0530 Subject: [PATCH] feat(cmakev2): Add dfu target support for cmakev2 This commit adds support for dfu targets for cmake2. The following changes have been made: - Adds a new tools/cmakev2/dfu.cmake file on similar lines as the tools/cmake/dfu.cmake file. - Adds the idf_create_dfu() function to create the dfu targets. This function now takes the executable as an argument thus allowing the dfu target to be created per-executable. - idf_project_default() is updated to create the dfu target. --- tools/cmakev2/dfu.cmake | 121 ++++++++++++++++++++++++++++++++++++ tools/cmakev2/idf.cmake | 1 + tools/cmakev2/project.cmake | 3 + 3 files changed, 125 insertions(+) create mode 100644 tools/cmakev2/dfu.cmake diff --git a/tools/cmakev2/dfu.cmake b/tools/cmakev2/dfu.cmake new file mode 100644 index 0000000000..c1dc96a106 --- /dev/null +++ b/tools/cmakev2/dfu.cmake @@ -0,0 +1,121 @@ +# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Apache-2.0 + +#[[ +.. cmakev2:function:: idf_create_dfu + + .. code-block:: cmake + + idf_create_dfu( + TARGET ) + + *binary[in]* + + Binary target for which to create DFU targets. The binary target is + created by :cmakev2:ref:`idf_build_binary`. + + *TARGET[in]* + + Name of the DFU generation target to be created (e.g., "dfu", "dfu-app"). + Also creates "-list" and "-flash" targets. + + Create DFU (Device Firmware Update) targets for the specified binary target. + DFU is only supported on certain targets. For other + targets, this function does nothing. + + Note: DFU is not supported when secure boot is enabled. This function will + not create DFU targets if secure boot is enabled. + + Three targets are created: + - : Generates the DFU binary file from flasher_args.json + - -list: Lists connected DFU devices + - -flash: Flashes the DFU binary to a connected device + + Example usage for default project: + idf_build_binary(myapp TARGET myapp_binary OUTPUT_FILE myapp.bin) + idf_create_dfu(myapp_binary TARGET dfu) + +#]] +function(idf_create_dfu binary) + set(options) + set(one_value TARGET) + set(multi_value) + cmake_parse_arguments(ARG "${options}" "${one_value}" "${multi_value}" ${ARGN}) + + if(NOT DEFINED ARG_TARGET) + idf_die("TARGET option is required") + endif() + + if(NOT TARGET "${binary}") + idf_die("Binary target '${binary}' is not a cmake target") + endif() + + # DFU is not supported when secure boot is enabled + if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES) + idf_msg("DFU not supported when secure boot is enabled, skipping DFU target creation") + return() + endif() + + # Determine DFU PID based on target + idf_build_get_property(target IDF_TARGET) + + if("${target}" STREQUAL "esp32s2") + set(dfu_pid "2") + elseif("${target}" STREQUAL "esp32s3") + set(dfu_pid "9") + elseif("${target}" STREQUAL "esp32p4") + set(dfu_pid "12") + else() + # DFU not supported on this target + idf_msg("DFU not supported on ${target}, skipping DFU target creation") + return() + endif() + + # Get build properties + idf_build_get_property(python PYTHON) + idf_build_get_property(idf_path IDF_PATH) + idf_build_get_property(build_dir BUILD_DIR) + + # Path to DFU output file + set(dfu_output_file "${build_dir}/${ARG_TARGET}.bin") + + # Create DFU generation target + add_custom_target(${ARG_TARGET} + COMMAND ${python} ${idf_path}/tools/mkdfu.py write + -o "${dfu_output_file}" + --json "${build_dir}/flasher_args.json" + --pid "${dfu_pid}" + --flash-size "${CONFIG_ESPTOOLPY_FLASHSIZE}" + DEPENDS ${binary} + VERBATIM + USES_TERMINAL + COMMENT "Generating DFU binary for ${binary}" + ) + + # Add dependency on bootloader if it's being built + if(CONFIG_APP_BUILD_BOOTLOADER AND TARGET bootloader) + add_dependencies(${ARG_TARGET} bootloader) + endif() + + # Create DFU list target (lists connected DFU devices) + add_custom_target(${ARG_TARGET}-list + COMMAND ${CMAKE_COMMAND} + -D ESP_DFU_LIST="1" + -P ${idf_path}/tools/cmake/run_dfu_util.cmake + USES_TERMINAL + COMMENT "Listing DFU devices" + ) + + # Create DFU flash target (flashes the DFU binary) + add_custom_target(${ARG_TARGET}-flash + COMMAND ${CMAKE_COMMAND} + -D ESP_DFU_BIN="${dfu_output_file}" + -D ESP_DFU_PID="${dfu_pid}" + -P ${idf_path}/tools/cmake/run_dfu_util.cmake + DEPENDS ${ARG_TARGET} + USES_TERMINAL + COMMENT "Flashing DFU binary for ${binary}" + ) + + idf_msg("Created DFU targets: ${ARG_TARGET}, ${ARG_TARGET}-list, ${ARG_TARGET}-flash") +endfunction() diff --git a/tools/cmakev2/idf.cmake b/tools/cmakev2/idf.cmake index 1972644de8..73d94648af 100644 --- a/tools/cmakev2/idf.cmake +++ b/tools/cmakev2/idf.cmake @@ -34,6 +34,7 @@ include(project) include(manager) include(compat) include(ldgen) +include(dfu) include(GetGitRevisionDescription) # For backward compatibility, since externalproject_add is used by # project_include.cmake in the bootloader component. The ExternalProject diff --git a/tools/cmakev2/project.cmake b/tools/cmakev2/project.cmake index a1f8ffb3ae..c4bfde3c15 100644 --- a/tools/cmakev2/project.cmake +++ b/tools/cmakev2/project.cmake @@ -747,6 +747,9 @@ macro(idf_project_default) TARGET app-flash NAME "app" FLASH) + + idf_create_dfu("${executable}_binary" + TARGET dfu) endif() # FIXME: Dependencies should be specified within the components, not in the