1# SPDX-License-Identifier: Apache-2.0
2
3zephyr_sources(
4  soc.c
5  soc_cache.c
6  loader.c
7  )
8
9zephyr_library_sources_ifdef(CONFIG_NEWLIB_LIBC newlib_fix.c)
10
11zephyr_library_sources_ifdef(CONFIG_PM power.c)
12zephyr_library_sources_ifdef(CONFIG_POWEROFF poweroff.c)
13
14# get code-partition slot0 address
15dt_nodelabel(dts_partition_path NODELABEL "slot0_partition")
16dt_reg_addr(img_0_off PATH ${dts_partition_path})
17
18# get code-partition boot address
19dt_nodelabel(dts_partition_path NODELABEL "boot_partition")
20dt_reg_addr(boot_off PATH ${dts_partition_path})
21
22# get flash size to use in esptool as string
23math(EXPR esptoolpy_flashsize "${CONFIG_FLASH_SIZE} / 0x100000")
24
25if(CONFIG_BOOTLOADER_ESP_IDF)
26  include(ExternalProject)
27
28  ## we use hello-world project, but I think any can be used.
29  set(espidf_components_dir   ${ESP_IDF_PATH}/components)
30  set(espidf_prefix    ${CMAKE_BINARY_DIR}/esp-idf)
31  set(espidf_build_dir ${espidf_prefix}/build)
32
33  ExternalProject_Add(
34    EspIdfBootloader
35    PREFIX ${espidf_prefix}
36    SOURCE_DIR ${espidf_components_dir}/bootloader/subproject
37    BINARY_DIR ${espidf_build_dir}/bootloader
38    CONFIGURE_COMMAND
39    ${CMAKE_COMMAND} -G${CMAKE_GENERATOR}
40    -S ${espidf_components_dir}/bootloader/subproject
41    -B ${espidf_build_dir}/bootloader -DSDKCONFIG=${espidf_build_dir}/sdkconfig
42    -DIDF_PATH=${ESP_IDF_PATH} -DIDF_TARGET=${CONFIG_SOC_SERIES}
43    -DPYTHON_DEPS_CHECKED=1
44    -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
45    -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
46    -DCMAKE_ASM_COMPILER=${CMAKE_ASM_COMPILER}
47    -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
48    -DPYTHON=${PYTHON_EXECUTABLE}
49    BUILD_COMMAND
50    ${CMAKE_COMMAND} --build .
51    INSTALL_COMMAND ""      # This particular build system has no install command
52    )
53
54  ExternalProject_Add(
55    EspPartitionTable
56    SOURCE_DIR ${espidf_components_dir}/partition_table
57    BINARY_DIR ${espidf_build_dir}
58    CONFIGURE_COMMAND ""
59    BUILD_COMMAND
60    ${PYTHON_EXECUTABLE} ${ESP_IDF_PATH}/components/partition_table/gen_esp32part.py -q
61    --offset 0x8000 --flash-size ${esptoolpy_flashsize}MB ${ESP_IDF_PATH}/components/partition_table/partitions_singleapp.csv ${espidf_build_dir}/partitions_singleapp.bin
62    INSTALL_COMMAND ""
63    )
64
65  set_property(TARGET bintools PROPERTY disassembly_flag_inline_source)
66
67  add_dependencies(app EspIdfBootloader EspPartitionTable)
68
69  board_finalize_runner_args(esp32 "--esp-flash-bootloader=${espidf_build_dir}/bootloader/bootloader.bin")
70
71  board_finalize_runner_args(esp32 "--esp-flash-partition_table=${espidf_build_dir}/partitions_singleapp.bin")
72
73  board_finalize_runner_args(esp32 "--esp-partition-table-address=0x8000")
74
75endif()
76
77if(CONFIG_MCUBOOT OR CONFIG_BOOTLOADER_ESP_IDF)
78
79  if(CONFIG_BUILD_OUTPUT_BIN)
80    set_property(GLOBAL APPEND PROPERTY extra_post_build_commands
81      COMMAND ${PYTHON_EXECUTABLE} ${ESP_IDF_PATH}/components/esptool_py/esptool/esptool.py
82      ARGS --chip esp32s2 elf2image --flash_mode dio --flash_freq 40m --flash_size ${esptoolpy_flashsize}MB
83      -o ${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.bin
84      ${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.elf)
85  endif()
86
87  if(CONFIG_MCUBOOT)
88    board_finalize_runner_args(esp32 "--esp-flash-bootloader=${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.bin")
89  endif()
90
91endif()
92
93board_finalize_runner_args(esp32 "--esp-boot-address=${boot_off}")
94
95board_finalize_runner_args(esp32 "--esp-app-address=${img_0_off}")
96