1# Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd.
2# SPDX-License-Identifier: Apache-2.0
3
4zephyr_include_directories(include)
5
6if(NOT CONFIG_MCUBOOT AND NOT CONFIG_SOC_ESP32_APPCPU AND NOT CONFIG_SOC_ESP32S3_APPCPU)
7  zephyr_sources_ifdef(CONFIG_ESP_SPIRAM esp_psram.c)
8endif()
9
10# Get flash size to use in esptool as string
11math(EXPR esptoolpy_flashsize "${CONFIG_FLASH_SIZE} / 0x100000")
12
13# Get UART baudrate from DT
14dt_chosen(dts_shell_uart PROPERTY "zephyr,shell-uart")
15if(DEFINED dts_shell_uart AND dts_shell_uart)
16  dt_prop(monitor_baud PATH ${dts_shell_uart} PROPERTY "current-speed")
17endif()
18
19message("-- Espressif HAL path: ${ESP_IDF_PATH}")
20
21if(CONFIG_ESP_SIMPLE_BOOT OR CONFIG_MCUBOOT)
22  if(CONFIG_BUILD_OUTPUT_BIN)
23    set(ESPTOOL_PY ${ESP_IDF_PATH}/tools/esptool_py/esptool.py)
24    message("-- Use the esptool.py: ${ESPTOOL_PY}")
25
26    set(ELF2IMAGE_ARG "")
27    if(NOT CONFIG_MCUBOOT)
28      set(ELF2IMAGE_ARG "--ram-only-header")
29    endif()
30
31    set_property(GLOBAL APPEND PROPERTY extra_post_build_commands
32      COMMAND ${PYTHON_EXECUTABLE} ${ESPTOOL_PY}
33      ARGS --chip ${CONFIG_SOC} elf2image ${ELF2IMAGE_ARG}
34      --flash_mode dio
35      --flash_freq ${CONFIG_ESPTOOLPY_FLASHFREQ}
36      --flash_size ${esptoolpy_flashsize}MB
37      -o ${CMAKE_BINARY_DIR}/zephyr/${KERNEL_BIN_NAME}
38      ${CMAKE_BINARY_DIR}/zephyr/${KERNEL_ELF_NAME})
39  endif()
40endif()
41
42set_property(TARGET bintools PROPERTY disassembly_flag_inline_source)
43
44# Select the image origin depending on the boot configuration
45if(CONFIG_SOC_ESP32_APPCPU OR CONFIG_SOC_ESP32S3_APPCPU)
46  dt_nodelabel(dts_partition_path NODELABEL "slot0_appcpu_partition")
47elseif(CONFIG_MCUBOOT OR CONFIG_ESP_SIMPLE_BOOT)
48  dt_nodelabel(dts_partition_path NODELABEL "boot_partition")
49else()
50  dt_nodelabel(dts_partition_path NODELABEL "slot0_partition")
51endif()
52
53dt_reg_addr(image_off PATH ${dts_partition_path})
54board_runner_args(esp32 "--esp-app-address=${image_off}")
55board_runner_args(esp32 "--esp-flash-size=${esptoolpy_flashsize}MB")
56board_runner_args(esp32 "--esp-flash-freq=${CONFIG_ESPTOOLPY_FLASHFREQ}")
57board_runner_args(esp32 "--esp-flash-mode=${CONFIG_ESPTOOLPY_FLASHMODE}")
58board_finalize_runner_args(esp32 "--esp-monitor-baud=${monitor_baud}")
59
60message("-- Image partition ${dts_partition_path}")
61
62# Look for cross references between bootloader sections
63if(CONFIG_MCUBOOT)
64    message("check_callgraph using: ${ESP_IDF_PATH}/tools/ci/check_callgraph.py")
65    set_property(GLOBAL APPEND PROPERTY extra_post_build_commands
66      COMMAND
67        ${PYTHON_EXECUTABLE} ${ESP_IDF_PATH}/tools/ci/check_callgraph.py
68      ARGS
69        --rtl-dirs ${CMAKE_BINARY_DIR}/zephyr
70        --elf-file ${CMAKE_BINARY_DIR}/zephyr/${KERNEL_ELF_NAME}
71        find-refs
72        --from-section='.iram0.loader_text'
73        --to-section='.iram0.text'
74        --exit-code)
75endif()
76
77if(CONFIG_MCUBOOT)
78  set(SOC_LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/../${CONFIG_SOC}/mcuboot.ld CACHE INTERNAL "")
79elseif(CONFIG_SOC_ESP32_APPCPU OR CONFIG_SOC_ESP32S3_APPCPU)
80  set(SOC_LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/../${CONFIG_SOC}/default_appcpu.ld CACHE INTERNAL "")
81else()
82  set(SOC_LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/../${CONFIG_SOC}/default.ld CACHE INTERNAL "")
83endif()
84