diff --git a/Kconfig.zephyr b/Kconfig.zephyr index 6aa95138f82..d2d3e0a8db1 100644 --- a/Kconfig.zephyr +++ b/Kconfig.zephyr @@ -695,7 +695,8 @@ endif # BOOTLOADER_MCUBOOT config BOOTLOADER_ESP_IDF bool "ESP-IDF bootloader support" - depends on SOC_ESP32 || SOC_ESP32S2 + depends on (SOC_ESP32 || SOC_ESP32S2 || SOC_ESP32C3) && !BOOTLOADER_MCUBOOT + default y help This option will trigger the compilation of the ESP-IDF bootloader inside the build folder. diff --git a/boards/xtensa/esp32/CMakeLists.txt b/boards/xtensa/esp32/CMakeLists.txt deleted file mode 100644 index 61c684c20a2..00000000000 --- a/boards/xtensa/esp32/CMakeLists.txt +++ /dev/null @@ -1,65 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 - -if(CONFIG_BOOTLOADER_ESP_IDF) - include(ExternalProject) - - ## we use hell-world project, but I think any can be used. - set(espidf_components_dir ${ESP_IDF_PATH}/components) - set(espidf_prefix ${CMAKE_BINARY_DIR}/esp-idf) - set(espidf_build_dir ${espidf_prefix}/build) - - ExternalProject_Add( - EspIdfBootloader - PREFIX ${espidf_prefix} - SOURCE_DIR ${espidf_components_dir}/bootloader/subproject - BINARY_DIR ${espidf_build_dir}/bootloader - CONFIGURE_COMMAND - ${CMAKE_COMMAND} -G${CMAKE_GENERATOR} - -S ${espidf_components_dir}/bootloader/subproject - -B ${espidf_build_dir}/bootloader -DSDKCONFIG=${espidf_build_dir}/sdkconfig - -DIDF_PATH=${ESP_IDF_PATH} -DIDF_TARGET=${CONFIG_BOARD} - -DPYTHON_DEPS_CHECKED=1 - -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} - -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} - -DCMAKE_ASM_COMPILER=${CMAKE_ASM_COMPILER} - -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME} - -DPYTHON=${PYTHON_EXECUTABLE} - BUILD_COMMAND - ${CMAKE_COMMAND} --build . - INSTALL_COMMAND "" # This particular build system has no install command - ) - - ExternalProject_Add( - EspPartitionTable - SOURCE_DIR ${espidf_components_dir}/partition_table - BINARY_DIR ${espidf_build_dir} - CONFIGURE_COMMAND "" - BUILD_COMMAND - ${PYTHON_EXECUTABLE} ${ESP_IDF_PATH}/components/partition_table/gen_esp32part.py -q - --offset 0x8000 --flash-size 4MB ${ESP_IDF_PATH}/components/partition_table/partitions_singleapp.csv ${espidf_build_dir}/partitions_singleapp.bin - INSTALL_COMMAND "" - ) - - if(CONFIG_BUILD_OUTPUT_BIN) - set_property(GLOBAL APPEND PROPERTY extra_post_build_commands - COMMAND ${PYTHON_EXECUTABLE} ${ESP_IDF_PATH}/components/esptool_py/esptool/esptool.py - ARGS --chip esp32 elf2image --flash_mode dio --flash_freq 40m - -o ${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.bin - ${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.elf) - endif() - - set_property(TARGET bintools PROPERTY disassembly_flag_inline_source) - - add_dependencies(app EspIdfBootloader EspPartitionTable) - - board_finalize_runner_args(esp32 "--esp-flash-bootloader=${espidf_build_dir}/bootloader/bootloader.bin") - - board_finalize_runner_args(esp32 "--esp-flash-partition_table=${espidf_build_dir}/partitions_singleapp.bin") - - board_finalize_runner_args(esp32 "--esp-boot-address=0x1000") - - board_finalize_runner_args(esp32 "--esp-partition-table-address=0x8000") - - board_finalize_runner_args(esp32 "--esp-app-address=0x10000") - -endif() diff --git a/boards/xtensa/esp32/esp32_defconfig b/boards/xtensa/esp32/esp32_defconfig index 01596c34f40..57db7b496fc 100644 --- a/boards/xtensa/esp32/esp32_defconfig +++ b/boards/xtensa/esp32/esp32_defconfig @@ -26,5 +26,3 @@ CONFIG_GEN_IRQ_VECTOR_TABLE=n CONFIG_I2C=y CONFIG_CLOCK_CONTROL=y - -CONFIG_BOOTLOADER_ESP_IDF=y diff --git a/scripts/west_commands/runners/esp32.py b/scripts/west_commands/runners/esp32.py index 56e2342bee1..22e1359a935 100644 --- a/scripts/west_commands/runners/esp32.py +++ b/scripts/west_commands/runners/esp32.py @@ -18,9 +18,10 @@ class Esp32BinaryRunner(ZephyrBinaryRunner): def __init__(self, cfg, device, boot_address, part_table_address, app_address, baud=921600, flash_size='detect', flash_freq='40m', flash_mode='dio', espidf='espidf', - bootloader_bin=None, partition_table_bin=None, app_bin=None): + bootloader_bin=None, partition_table_bin=None): super().__init__(cfg) self.elf = cfg.elf_file + self.app_bin = cfg.bin_file self.device = device self.boot_address = boot_address self.part_table_address = part_table_address @@ -32,7 +33,6 @@ class Esp32BinaryRunner(ZephyrBinaryRunner): self.espidf = espidf self.bootloader_bin = bootloader_bin self.partition_table_bin = partition_table_bin - self.app_bin = app_bin @classmethod def name(cls): @@ -72,8 +72,6 @@ class Esp32BinaryRunner(ZephyrBinaryRunner): help='Bootloader image to flash') parser.add_argument('--esp-flash-partition_table', help='Partition table to flash') - parser.add_argument('--esp-flash-app', - help='App to flash') @classmethod def do_create(cls, cfg, args): @@ -86,19 +84,14 @@ class Esp32BinaryRunner(ZephyrBinaryRunner): return Esp32BinaryRunner( cfg, args.esp_device, boot_address=args.esp_boot_address, part_table_address=args.esp_partition_table_address, - app_address=args.esp_app_address,baud=args.esp_baud_rate, + app_address=args.esp_app_address, baud=args.esp_baud_rate, flash_size=args.esp_flash_size, flash_freq=args.esp_flash_freq, flash_mode=args.esp_flash_mode, espidf=espidf, bootloader_bin=args.esp_flash_bootloader, - partition_table_bin=args.esp_flash_partition_table, - app_bin=args.esp_flash_app) + partition_table_bin=args.esp_flash_partition_table) def do_run(self, command, **kwargs): self.require(self.espidf) - if self.app_bin: - bin_name = self.app_bin - else: - bin_name = path.splitext(self.elf)[0] + path.extsep + 'bin' cmd_flash = [self.espidf, '--chip', 'auto'] if self.device is not None: @@ -116,12 +109,12 @@ class Esp32BinaryRunner(ZephyrBinaryRunner): if self.bootloader_bin: cmd_flash.extend([self.boot_address, self.bootloader_bin]) - if self.partition_table_bin: - cmd_flash.extend([self.part_table_address, self.partition_table_bin]) - cmd_flash.extend([self.app_address, bin_name]) + cmd_flash.extend([self.part_table_address, self.partition_table_bin]) + cmd_flash.extend([self.app_address, self.app_bin]) else: - cmd_flash.extend([self.boot_address, bin_name]) + cmd_flash.extend([self.app_address, self.app_bin]) self.logger.info("Flashing esp32 chip on {} ({}bps)". format(self.device, self.baud)) + print(cmd_flash) self.check_call(cmd_flash) diff --git a/soc/xtensa/esp32/CMakeLists.txt b/soc/xtensa/esp32/CMakeLists.txt index eac2ee2dab3..8f589f7e3f9 100644 --- a/soc/xtensa/esp32/CMakeLists.txt +++ b/soc/xtensa/esp32/CMakeLists.txt @@ -6,4 +6,78 @@ zephyr_sources( loader.c ) - zephyr_library_sources_ifdef(CONFIG_GDBSTUB gdbstub.c) +zephyr_library_sources_ifdef(CONFIG_GDBSTUB gdbstub.c) + +if(CONFIG_BOOTLOADER_ESP_IDF) + include(ExternalProject) + + ## we use hello-world project, but I think any can be used. + set(espidf_components_dir ${ESP_IDF_PATH}/components) + set(espidf_prefix ${CMAKE_BINARY_DIR}/esp-idf) + set(espidf_build_dir ${espidf_prefix}/build) + + ExternalProject_Add( + EspIdfBootloader + PREFIX ${espidf_prefix} + SOURCE_DIR ${espidf_components_dir}/bootloader/subproject + BINARY_DIR ${espidf_build_dir}/bootloader + CONFIGURE_COMMAND + ${CMAKE_COMMAND} -G${CMAKE_GENERATOR} + -S ${espidf_components_dir}/bootloader/subproject + -B ${espidf_build_dir}/bootloader -DSDKCONFIG=${espidf_build_dir}/sdkconfig + -DIDF_PATH=${ESP_IDF_PATH} -DIDF_TARGET=${CONFIG_BOARD} + -DPYTHON_DEPS_CHECKED=1 + -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + -DCMAKE_ASM_COMPILER=${CMAKE_ASM_COMPILER} + -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME} + -DPYTHON=${PYTHON_EXECUTABLE} + BUILD_COMMAND + ${CMAKE_COMMAND} --build . + INSTALL_COMMAND "" # This particular build system has no install command + ) + + ExternalProject_Add( + EspPartitionTable + SOURCE_DIR ${espidf_components_dir}/partition_table + BINARY_DIR ${espidf_build_dir} + CONFIGURE_COMMAND "" + BUILD_COMMAND + ${PYTHON_EXECUTABLE} ${ESP_IDF_PATH}/components/partition_table/gen_esp32part.py -q + --offset 0x8000 --flash-size 4MB ${ESP_IDF_PATH}/components/partition_table/partitions_singleapp.csv ${espidf_build_dir}/partitions_singleapp.bin + INSTALL_COMMAND "" + ) + + if(CONFIG_BUILD_OUTPUT_BIN) + set_property(GLOBAL APPEND PROPERTY extra_post_build_commands + COMMAND ${PYTHON_EXECUTABLE} ${ESP_IDF_PATH}/components/esptool_py/esptool/esptool.py + ARGS --chip esp32 elf2image --flash_mode dio --flash_freq 40m + -o ${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.bin + ${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.elf) + endif() + + set_property(TARGET bintools PROPERTY disassembly_flag_inline_source) + + add_dependencies(app EspIdfBootloader EspPartitionTable) + + board_finalize_runner_args(esp32 "--esp-flash-bootloader=${espidf_build_dir}/bootloader/bootloader.bin") + + board_finalize_runner_args(esp32 "--esp-flash-partition_table=${espidf_build_dir}/partitions_singleapp.bin") + + board_finalize_runner_args(esp32 "--esp-boot-address=0x1000") + + board_finalize_runner_args(esp32 "--esp-partition-table-address=0x8000") + + board_finalize_runner_args(esp32 "--esp-app-address=0x10000") + +endif() + +if(CONFIG_BOOTLOADER_MCUBOOT) + + # get code-partition slot0 address + dt_nodelabel(dts_partition_path NODELABEL "slot0_partition") + dt_reg_addr(img_0_off PATH ${dts_partition_path}) + + board_finalize_runner_args(esp32 "--esp-app-address=${img_0_off}") + +endif() diff --git a/soc/xtensa/esp32/Kconfig.defconfig b/soc/xtensa/esp32/Kconfig.defconfig index 52fc27e672d..8a562ccfa65 100644 --- a/soc/xtensa/esp32/Kconfig.defconfig +++ b/soc/xtensa/esp32/Kconfig.defconfig @@ -5,6 +5,23 @@ if SOC_ESP32 +if BOOTLOADER_MCUBOOT + config HAS_FLASH_LOAD_OFFSET + default y + + config XIP + default y + + config MCUBOOT_GENERATE_UNSIGNED_IMAGE + default y + + config MCUBOOT_GENERATE_CONFIRMED_IMAGE + default y + + config ROM_START_OFFSET + default 0x20 +endif + config SOC default "esp32"