1# Copyright (c) 2024 Nordic Semiconductor ASA
2# SPDX-License-Identifier: Apache-2.0
3
4function(nrf_regtool_generate_uicr generated_hex_file)
5  string(REPEAT "-v;" ${CONFIG_NRF_REGTOOL_VERBOSITY} verbosity)
6  execute_process(
7    COMMAND
8    ${CMAKE_COMMAND} -E env PYTHONPATH=${ZEPHYR_BASE}/scripts/dts/python-devicetree/src
9    ${NRF_REGTOOL} ${verbosity} uicr-compile
10    --edt-pickle-file ${EDT_PICKLE}
11    --product-name ${CONFIG_SOC}
12    --output-file ${generated_hex_file}
13    WORKING_DIRECTORY ${APPLICATION_SOURCE_DIR}
14    COMMAND_ERROR_IS_FATAL ANY
15  )
16  message(STATUS "Generated UICR hex file: ${generated_hex_file}")
17endfunction()
18
19function(nrf_regtool_generate_peripheral peripheral generated_hex_file)
20  # Prepare common argument sub-lists.
21  string(REPEAT "-v;" ${CONFIG_NRF_REGTOOL_VERBOSITY} verbosity)
22  list(TRANSFORM CACHED_DTS_ROOT_BINDINGS PREPEND "--bindings-dir;" OUTPUT_VARIABLE bindings_dirs)
23
24  execute_process(
25    COMMAND
26    ${CMAKE_COMMAND} -E env PYTHONPATH=${ZEPHYR_BASE}/scripts/dts/python-devicetree/src
27    ${NRF_REGTOOL} ${verbosity} generate
28    --peripheral ${peripheral}
29    --svd-file ${SOC_SVD_FILE}
30    --dts-file ${ZEPHYR_DTS}
31    ${bindings_dirs}
32    --output-file ${generated_hex_file}
33    ${extra_args}
34    WORKING_DIRECTORY ${APPLICATION_SOURCE_DIR}
35    COMMAND_ERROR_IS_FATAL ANY
36  )
37  message(STATUS "Generated ${peripheral} hex file: ${generated_hex_file}")
38endfunction()
39
40get_property(version GLOBAL PROPERTY nrf_regtool_version)
41
42foreach(component IN LISTS ${CMAKE_FIND_PACKAGE_NAME}_FIND_COMPONENTS)
43  if(component STREQUAL "GENERATE:UICR")
44    set(generated_hex_file ${PROJECT_BINARY_DIR}/uicr.hex)
45    if(version VERSION_GREATER_EQUAL 7.0.0)
46      nrf_regtool_generate_uicr(${generated_hex_file})
47    else()
48      nrf_regtool_generate_peripheral(UICR ${generated_hex_file})
49    endif()
50
51    # UICR must be flashed together with the Zephyr binary.
52    set(merged_hex_file ${PROJECT_BINARY_DIR}/uicr_merged.hex)
53    set_property(GLOBAL APPEND PROPERTY extra_post_build_commands
54      COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/build/mergehex.py
55      -o ${merged_hex_file}
56      ${generated_hex_file}
57      ${PROJECT_BINARY_DIR}/${KERNEL_HEX_NAME}
58    )
59    set_property(TARGET runners_yaml_props_target PROPERTY hex_file ${merged_hex_file})
60
61  else()
62    message(FATAL_ERROR "Unrecognized package component: \"${component}\"")
63  endif()
64endforeach()
65