1# SPDX-License-Identifier: Apache-2.0
2
3zephyr_sources(
4  soc.c
5  soc_cache.c
6  ../common/loader.c
7  )
8
9zephyr_include_directories(.)
10
11zephyr_sources_ifndef(CONFIG_BOOTLOADER_MCUBOOT hw_init.c)
12
13zephyr_library_sources_ifdef(CONFIG_NEWLIB_LIBC newlib_fix.c)
14
15zephyr_library_sources_ifdef(CONFIG_PM power.c)
16zephyr_library_sources_ifdef(CONFIG_POWEROFF poweroff.c)
17
18# get flash size to use in esptool as string
19math(EXPR esptoolpy_flashsize "${CONFIG_FLASH_SIZE} / 0x100000")
20
21if(NOT CONFIG_BOOTLOADER_MCUBOOT)
22
23  if(CONFIG_BUILD_OUTPUT_BIN)
24    # make ESP ROM loader compatible image
25    message("ESP-IDF path: ${ESP_IDF_PATH}")
26
27    set(ESPTOOL_PY ${ESP_IDF_PATH}/tools/esptool_py/esptool.py)
28    message("esptool path: ${ESPTOOL_PY}")
29
30    set(ELF2IMAGE_ARG "")
31    if(NOT CONFIG_MCUBOOT)
32      set(ELF2IMAGE_ARG "--ram-only-header")
33    endif()
34
35    set_property(GLOBAL APPEND PROPERTY extra_post_build_commands
36      COMMAND ${PYTHON_EXECUTABLE} ${ESPTOOL_PY}
37      ARGS --chip esp32s2 elf2image ${ELF2IMAGE_ARG}
38      --flash_mode dio --flash_freq 40m
39      --flash_size ${esptoolpy_flashsize}MB
40      -o ${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.bin
41      ${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.elf)
42  endif()
43
44endif()
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 code-partition slot0 address
51dt_nodelabel(dts_partition_path NODELABEL "slot0_partition")
52dt_reg_addr(img_0_off PATH ${dts_partition_path})
53
54if(CONFIG_BOOTLOADER_MCUBOOT)
55  board_finalize_runner_args(esp32 "--esp-app-address=${img_0_off}")
56else()
57  board_finalize_runner_args(esp32 "--esp-app-address=${boot_off}")
58endif()
59
60if(CONFIG_MCUBOOT)
61    # search from cross references between bootloader sections
62    message("check_callgraph using: ${ESP_IDF_PATH}/tools/ci/check_callgraph.py")
63    set_property(GLOBAL APPEND PROPERTY extra_post_build_commands
64      COMMAND
65        ${PYTHON_EXECUTABLE} ${ESP_IDF_PATH}/tools/ci/check_callgraph.py
66      ARGS
67        --rtl-dirs ${CMAKE_BINARY_DIR}/zephyr
68	--elf-file ${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.elf
69        find-refs --from-section=.iram0.iram_loader --to-section=.iram0.text
70        --exit-code)
71endif()
72
73if(CONFIG_MCUBOOT)
74  set(SOC_LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/mcuboot.ld CACHE INTERNAL "")
75else()
76  set(SOC_LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/default.ld CACHE INTERNAL "")
77endif()
78