|
| 1 | +# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#[[ |
| 5 | +.. cmakev2:function:: idf_create_uf2 |
| 6 | +
|
| 7 | + .. code-block:: cmake |
| 8 | +
|
| 9 | + idf_create_uf2(<executable> |
| 10 | + TARGET <target> |
| 11 | + [APP_ONLY]) |
| 12 | +
|
| 13 | + *executable[in]* |
| 14 | +
|
| 15 | + Executable target for which to create UF2 target. |
| 16 | +
|
| 17 | + *TARGET[in]* |
| 18 | +
|
| 19 | + Name of the UF2 generation target to be created (e.g., "uf2", "uf2-app"). |
| 20 | +
|
| 21 | + *APP_ONLY[option]* |
| 22 | +
|
| 23 | + If specified, creates a UF2 binary file with only the application binary |
| 24 | + (uses --bin app option). If not specified, creates a UF2 binary file with |
| 25 | + all binaries from flasher_args.json. |
| 26 | +
|
| 27 | + Create a UF2 (USB Flashing Format) target for the specified executable. |
| 28 | +
|
| 29 | + Example usage: |
| 30 | + idf_create_uf2(myapp TARGET uf2) # All binaries |
| 31 | + idf_create_uf2(myapp TARGET uf2-app APP_ONLY) # App binary only |
| 32 | +#]] |
| 33 | +function(idf_create_uf2 executable) |
| 34 | + set(options APP_ONLY) |
| 35 | + set(one_value TARGET) |
| 36 | + set(multi_value) |
| 37 | + cmake_parse_arguments(ARG "${options}" "${one_value}" "${multi_value}" ${ARGN}) |
| 38 | + |
| 39 | + if(NOT DEFINED ARG_TARGET) |
| 40 | + idf_die("TARGET option is required") |
| 41 | + endif() |
| 42 | + |
| 43 | + if(NOT TARGET "${executable}") |
| 44 | + idf_die("Executable '${executable}' is not a cmake target") |
| 45 | + endif() |
| 46 | + |
| 47 | + # Get build properties |
| 48 | + idf_build_get_property(python PYTHON) |
| 49 | + idf_build_get_property(idf_path IDF_PATH) |
| 50 | + idf_build_get_property(build_dir BUILD_DIR) |
| 51 | + idf_build_get_property(target IDF_TARGET) |
| 52 | + |
| 53 | + # Path to UF2 output file |
| 54 | + set(uf2_output_file "${build_dir}/${ARG_TARGET}.bin") |
| 55 | + |
| 56 | + # Build UF2 command and arguments (matching cmakev1 pattern) |
| 57 | + set(UF2_ARGS --json "${build_dir}/flasher_args.json") |
| 58 | + set(UF2_CMD ${python} "${idf_path}/tools/mkuf2.py" write --chip ${target}) |
| 59 | + |
| 60 | + # Add output file and --bin app if APP_ONLY is specified |
| 61 | + set(uf2_args_list "${UF2_ARGS};-o;${uf2_output_file}") |
| 62 | + if(ARG_APP_ONLY) |
| 63 | + list(APPEND uf2_args_list --bin app) |
| 64 | + set(comment_msg "Generating UF2 app binary for ${executable}") |
| 65 | + else() |
| 66 | + set(comment_msg "Generating UF2 binary for ${executable}") |
| 67 | + endif() |
| 68 | + |
| 69 | + # Create UF2 target (matching cmakev1 pattern using run_uf2_cmds.cmake) |
| 70 | + add_custom_target(${ARG_TARGET} |
| 71 | + COMMAND ${CMAKE_COMMAND} |
| 72 | + -D "IDF_PATH=${idf_path}" |
| 73 | + -D "UF2_CMD=${UF2_CMD}" |
| 74 | + -D "UF2_ARGS=${uf2_args_list}" |
| 75 | + -P "${idf_path}/tools/cmake/run_uf2_cmds.cmake" |
| 76 | + USES_TERMINAL |
| 77 | + VERBATIM |
| 78 | + COMMENT "${comment_msg}" |
| 79 | + ) |
| 80 | + |
| 81 | + idf_msg("Created UF2 target: ${ARG_TARGET}") |
| 82 | +endfunction() |
0 commit comments