|
| 1 | +# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#[[ |
| 5 | +.. cmakev2:function:: idf_create_dfu |
| 6 | +
|
| 7 | + .. code-block:: cmake |
| 8 | +
|
| 9 | + idf_create_dfu(<binary> |
| 10 | + TARGET <target>) |
| 11 | +
|
| 12 | + *binary[in]* |
| 13 | +
|
| 14 | + Binary target for which to create DFU targets. The binary target is |
| 15 | + created by :cmakev2:ref:`idf_build_binary`. |
| 16 | +
|
| 17 | + *TARGET[in]* |
| 18 | +
|
| 19 | + Name of the DFU generation target to be created (e.g., "dfu", "dfu-app"). |
| 20 | + Also creates "<target>-list" and "<target>-flash" targets. |
| 21 | +
|
| 22 | + Create DFU (Device Firmware Update) targets for the specified binary target. |
| 23 | + DFU is only supported on certain targets. For other |
| 24 | + targets, this function does nothing. |
| 25 | +
|
| 26 | + Note: DFU is not supported when secure boot is enabled. This function will |
| 27 | + not create DFU targets if secure boot is enabled. |
| 28 | +
|
| 29 | + Three targets are created: |
| 30 | + - <TARGET>: Generates the DFU binary file from flasher_args.json |
| 31 | + - <TARGET>-list: Lists connected DFU devices |
| 32 | + - <TARGET>-flash: Flashes the DFU binary to a connected device |
| 33 | +
|
| 34 | + Example usage for default project: |
| 35 | + idf_build_binary(myapp TARGET myapp_binary OUTPUT_FILE myapp.bin) |
| 36 | + idf_create_dfu(myapp_binary TARGET dfu) |
| 37 | +
|
| 38 | +#]] |
| 39 | +function(idf_create_dfu binary) |
| 40 | + set(options) |
| 41 | + set(one_value TARGET) |
| 42 | + set(multi_value) |
| 43 | + cmake_parse_arguments(ARG "${options}" "${one_value}" "${multi_value}" ${ARGN}) |
| 44 | + |
| 45 | + if(NOT DEFINED ARG_TARGET) |
| 46 | + idf_die("TARGET option is required") |
| 47 | + endif() |
| 48 | + |
| 49 | + if(NOT TARGET "${binary}") |
| 50 | + idf_die("Binary target '${binary}' is not a cmake target") |
| 51 | + endif() |
| 52 | + |
| 53 | + # DFU is not supported when secure boot is enabled |
| 54 | + if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES) |
| 55 | + idf_msg("DFU not supported when secure boot is enabled, skipping DFU target creation") |
| 56 | + return() |
| 57 | + endif() |
| 58 | + |
| 59 | + # Determine DFU PID based on target |
| 60 | + idf_build_get_property(target IDF_TARGET) |
| 61 | + |
| 62 | + if("${target}" STREQUAL "esp32s2") |
| 63 | + set(dfu_pid "2") |
| 64 | + elseif("${target}" STREQUAL "esp32s3") |
| 65 | + set(dfu_pid "9") |
| 66 | + elseif("${target}" STREQUAL "esp32p4") |
| 67 | + set(dfu_pid "12") |
| 68 | + else() |
| 69 | + # DFU not supported on this target |
| 70 | + idf_msg("DFU not supported on ${target}, skipping DFU target creation") |
| 71 | + return() |
| 72 | + endif() |
| 73 | + |
| 74 | + # Get build properties |
| 75 | + idf_build_get_property(python PYTHON) |
| 76 | + idf_build_get_property(idf_path IDF_PATH) |
| 77 | + idf_build_get_property(build_dir BUILD_DIR) |
| 78 | + |
| 79 | + # Path to DFU output file |
| 80 | + set(dfu_output_file "${build_dir}/${ARG_TARGET}.bin") |
| 81 | + |
| 82 | + # Create DFU generation target |
| 83 | + add_custom_target(${ARG_TARGET} |
| 84 | + COMMAND ${python} ${idf_path}/tools/mkdfu.py write |
| 85 | + -o "${dfu_output_file}" |
| 86 | + --json "${build_dir}/flasher_args.json" |
| 87 | + --pid "${dfu_pid}" |
| 88 | + --flash-size "${CONFIG_ESPTOOLPY_FLASHSIZE}" |
| 89 | + DEPENDS ${binary} |
| 90 | + VERBATIM |
| 91 | + USES_TERMINAL |
| 92 | + COMMENT "Generating DFU binary for ${binary}" |
| 93 | + ) |
| 94 | + |
| 95 | + # Add dependency on bootloader if it's being built |
| 96 | + if(CONFIG_APP_BUILD_BOOTLOADER AND TARGET bootloader) |
| 97 | + add_dependencies(${ARG_TARGET} bootloader) |
| 98 | + endif() |
| 99 | + |
| 100 | + # Create DFU list target (lists connected DFU devices) |
| 101 | + add_custom_target(${ARG_TARGET}-list |
| 102 | + COMMAND ${CMAKE_COMMAND} |
| 103 | + -D ESP_DFU_LIST="1" |
| 104 | + -P ${idf_path}/tools/cmake/run_dfu_util.cmake |
| 105 | + USES_TERMINAL |
| 106 | + COMMENT "Listing DFU devices" |
| 107 | + ) |
| 108 | + |
| 109 | + # Create DFU flash target (flashes the DFU binary) |
| 110 | + add_custom_target(${ARG_TARGET}-flash |
| 111 | + COMMAND ${CMAKE_COMMAND} |
| 112 | + -D ESP_DFU_BIN="${dfu_output_file}" |
| 113 | + -D ESP_DFU_PID="${dfu_pid}" |
| 114 | + -P ${idf_path}/tools/cmake/run_dfu_util.cmake |
| 115 | + DEPENDS ${ARG_TARGET} |
| 116 | + USES_TERMINAL |
| 117 | + COMMENT "Flashing DFU binary for ${binary}" |
| 118 | + ) |
| 119 | + |
| 120 | + idf_msg("Created DFU targets: ${ARG_TARGET}, ${ARG_TARGET}-list, ${ARG_TARGET}-flash") |
| 121 | +endfunction() |
0 commit comments