1# SPDX-License-Identifier: Apache-2.0
2
3zephyr_sources(
4  vectors.S
5  soc_irq.S
6  soc_irq.c
7  soc.c
8  ../common/loader.c
9  )
10
11zephyr_include_directories(.)
12
13zephyr_sources_ifndef(CONFIG_BOOTLOADER_MCUBOOT hw_init.c)
14
15# get flash size to use in esptool as string
16math(EXPR esptoolpy_flashsize "${CONFIG_FLASH_SIZE} / 0x100000")
17
18if(NOT CONFIG_BOOTLOADER_MCUBOOT)
19
20  if(CONFIG_BUILD_OUTPUT_BIN)
21    # make ESP ROM loader compatible image
22    message("ESP-IDF path: ${ESP_IDF_PATH}")
23
24    set(ESPTOOL_PY ${ESP_IDF_PATH}/tools/esptool_py/esptool.py)
25    message("esptool path: ${ESPTOOL_PY}")
26
27    set(ELF2IMAGE_ARG "")
28    if(NOT CONFIG_MCUBOOT)
29      set(ELF2IMAGE_ARG "--ram-only-header")
30    endif()
31
32    set_property(GLOBAL APPEND PROPERTY extra_post_build_commands
33      COMMAND ${PYTHON_EXECUTABLE} ${ESPTOOL_PY}
34      ARGS --chip esp32c2 elf2image ${ELF2IMAGE_ARG}
35      --flash_mode dio --flash_freq 60m --flash_size ${esptoolpy_flashsize}MB
36      -o ${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.bin
37      ${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.elf)
38  endif()
39
40endif()
41
42# get code-partition slot0 address
43dt_nodelabel(dts_partition_path NODELABEL "slot0_partition")
44dt_reg_addr(img_0_off PATH ${dts_partition_path})
45
46# get code-partition boot address
47dt_nodelabel(dts_partition_path NODELABEL "boot_partition")
48dt_reg_addr(boot_off PATH ${dts_partition_path})
49
50# get UART baudrate from DT
51dt_chosen(dts_shell_uart PROPERTY "zephyr,shell-uart")
52dt_prop(monitor_baud PATH ${dts_shell_uart} PROPERTY "current-speed")
53
54# C2 uses specific values for flash frequency and UART baudrate
55board_runner_args(esp32 "--esp-flash-freq=60m")
56board_runner_args(esp32 "--esp-monitor-baud=${monitor_baud}")
57
58if(CONFIG_BOOTLOADER_MCUBOOT)
59  board_finalize_runner_args(esp32 "--esp-app-address=${img_0_off}")
60else()
61  board_finalize_runner_args(esp32 "--esp-app-address=${boot_off}")
62endif()
63
64if(CONFIG_MCUBOOT)
65    # search from cross references between bootloader sections
66    message("check_callgraph using: ${ESP_IDF_PATH}/tools/ci/check_callgraph.py")
67    set_property(GLOBAL APPEND PROPERTY extra_post_build_commands
68      COMMAND
69        ${PYTHON_EXECUTABLE} ${ESP_IDF_PATH}/tools/ci/check_callgraph.py
70      ARGS
71        --rtl-dirs ${CMAKE_BINARY_DIR}/zephyr
72	--elf-file ${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.elf
73        find-refs --from-section=.iram0.iram_loader --to-section=.iram0.text
74        --exit-code)
75endif()
76
77if(CONFIG_MCUBOOT)
78  set(SOC_LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/mcuboot.ld CACHE INTERNAL "")
79else()
80  set(SOC_LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/default.ld CACHE INTERNAL "")
81endif()
82