arch/risc-v: add CMake support for Espressif RISC-V devices

Adds CMake build support. Touches arch and board at the same time to avoid
creating a commit that is not able to build.

Signed-off-by: Filipe Cavalcanti <filipe.cavalcanti@espressif.com>
This commit is contained in:
Filipe Cavalcanti
2026-03-27 21:02:01 -03:00
committed by archer
parent 2615513a45
commit ed217f8f3f
31 changed files with 4662 additions and 0 deletions
@@ -0,0 +1,111 @@
# ##############################################################################
# tools/espressif/espressif_esptool_common.cmake
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
# license agreements. See the NOTICE file distributed with this work for
# additional information regarding copyright ownership. The ASF licenses this
# file to you under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
# ##############################################################################
# cmake-format: off
# ##############################################################################
# Shared Espressif + esptool layout and flash parameters for CMake scripts.
#
# Prerequisites (callers must do this first): - include(nuttx_kconfig.cmake) -
# nuttx_export_kconfig(${BINARY_DIR}/.config)
#
# Defines set by including this file:
# CHIP_SERIES - Chip string for esptool -c (from CONFIG_ESPRESSIF_CHIP_SERIES)
# BL_OFFSET - Bootloader/app base offset for MCUboot & simple boot
# EFUSE_FLASH_OFFSET - Virtual eFuse blob offset in flash (from Config.mk EFUSE_FLASH_OFFSET)
# EFUSE_OFFSET - Alias for EFUSE_FLASH_OFFSET (for mkimage.cmake compatibility)
# MCUBOOT_APP_OFFSET - NuttX signed image offset (primary or secondary OTA slot)
# FLASH_SIZE - Flash size (e.g. 2MB, 4MB), for merge_bin/elf2image -fs
# FLASH_MODE - Flash mode (dio, dout, qio, qout), for elf2image -fm only
# FLASH_FREQ - Flash speed (e.g. 40m), for elf2image -ff/write_flash -ff
#
# ##############################################################################
# cmake-format: on
if(NOT DEFINED CHIP_SERIES)
if(NOT DEFINED CONFIG_ESPRESSIF_CHIP_SERIES)
message(
FATAL_ERROR
"espressif_esptool_common.cmake: CONFIG_ESPRESSIF_CHIP_SERIES not in .config"
)
endif()
string(REPLACE "\"" "" CHIP_SERIES "${CONFIG_ESPRESSIF_CHIP_SERIES}")
endif()
# Bootloader base (MCUboot placement; simple boot app start on ESP32-P4)
if(CONFIG_ARCH_CHIP_ESP32P4)
set(BL_OFFSET 0x2000)
else()
set(BL_OFFSET 0x0000)
endif()
# Virtual eFuse sector offset (tools/espressif/Config.mk EFUSE_FLASH_OFFSET)
if(CONFIG_ESPRESSIF_EFUSE_VIRTUAL_KEEP_IN_FLASH
AND DEFINED CONFIG_ESPRESSIF_EFUSE_VIRTUAL_KEEP_IN_FLASH_OFFSET)
set(EFUSE_FLASH_OFFSET ${CONFIG_ESPRESSIF_EFUSE_VIRTUAL_KEEP_IN_FLASH_OFFSET})
else()
set(EFUSE_FLASH_OFFSET 0x10000)
endif()
set(EFUSE_OFFSET ${EFUSE_FLASH_OFFSET})
# MCUboot application slot (Config.mk APP_OFFSET)
if(CONFIG_ESPRESSIF_ESPTOOL_TARGET_PRIMARY)
set(MCUBOOT_APP_OFFSET ${CONFIG_ESPRESSIF_OTA_PRIMARY_SLOT_OFFSET})
elseif(CONFIG_ESPRESSIF_ESPTOOL_TARGET_SECONDARY)
set(MCUBOOT_APP_OFFSET ${CONFIG_ESPRESSIF_OTA_SECONDARY_SLOT_OFFSET})
else()
message(FATAL_ERROR "Missing MCUBoot slot target: PRIMARY or SECONDARY")
endif()
# Flash capacity (merge_bin --fill-flash-size, elf2image -fs)
if(CONFIG_ESPRESSIF_FLASH_2M)
set(FLASH_SIZE "2MB")
elseif(CONFIG_ESPRESSIF_FLASH_4M)
set(FLASH_SIZE "4MB")
elseif(CONFIG_ESPRESSIF_FLASH_8M)
set(FLASH_SIZE "8MB")
elseif(CONFIG_ESPRESSIF_FLASH_16M)
set(FLASH_SIZE "16MB")
elseif(CONFIG_ESPRESSIF_FLASH_32M)
set(FLASH_SIZE "32MB")
else()
set(FLASH_SIZE "4MB")
endif()
# SPI mode for elf2image (Config.mk ELF2IMAGE -fm; write_flash uses -fm dio in
# Config.mk)
if(CONFIG_ESPRESSIF_FLASH_MODE_DIO)
set(FLASH_MODE "dio")
elseif(CONFIG_ESPRESSIF_FLASH_MODE_DOUT)
set(FLASH_MODE "dout")
elseif(CONFIG_ESPRESSIF_FLASH_MODE_QIO)
set(FLASH_MODE "qio")
elseif(CONFIG_ESPRESSIF_FLASH_MODE_QOUT)
set(FLASH_MODE "qout")
else()
set(FLASH_MODE "dio")
endif()
if(DEFINED CONFIG_ESPRESSIF_FLASH_FREQ)
string(REPLACE "\"" "" FLASH_FREQ "${CONFIG_ESPRESSIF_FLASH_FREQ}")
else()
set(FLASH_FREQ "40m")
endif()
+251
View File
@@ -0,0 +1,251 @@
# ##############################################################################
# tools/espressif/espressif_mkimage.cmake
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
# license agreements. See the NOTICE file distributed with this work for
# additional information regarding copyright ownership. The ASF licenses this
# file to you under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
# ##############################################################################
# This script is executed as a post-build step to generate ESP-compatible
# binaries from the NuttX ELF file.
#
# Required variables (passed via -D): BINARY_DIR - CMake binary directory
# (contains .config and nuttx ELF) SOURCE_DIR - CMake source directory
# (NuttX root)
#
# All CONFIG_* variables are automatically loaded from .config file.
# ##############################################################################
# Validate required variables
# ##############################################################################
if(NOT DEFINED BINARY_DIR)
message(FATAL_ERROR "BINARY_DIR not defined")
endif()
if(NOT DEFINED SOURCE_DIR)
message(FATAL_ERROR "SOURCE_DIR not defined")
endif()
# ##############################################################################
# Load Kconfig values from .config file
# ##############################################################################
# Include NuttX kconfig module
include(${SOURCE_DIR}/cmake/nuttx_kconfig.cmake)
# Load all CONFIG_* variables from .config
set(DOTCONFIG "${BINARY_DIR}/.config")
if(NOT EXISTS ${DOTCONFIG})
message(FATAL_ERROR ".config not found at ${DOTCONFIG}")
endif()
nuttx_export_kconfig(${DOTCONFIG})
include(${SOURCE_DIR}/tools/espressif/espressif_esptool_common.cmake)
# ##############################################################################
# Find required tools for the post build process
# ##############################################################################
find_program(ESPTOOL esptool esptool.py)
find_program(IMGTOOL imgtool)
find_program(PYTHON3 python3)
# ##############################################################################
# Check esptool version. Older versions will fail to build a proper image.
# ##############################################################################
if(ESPTOOL
AND PYTHON3
AND EXISTS "${SOURCE_DIR}/tools/espressif/check_esptool.py")
execute_process(
COMMAND ${PYTHON3} ${SOURCE_DIR}/tools/espressif/check_esptool.py -v 4.8.0
RESULT_VARIABLE ESPTOOL_CHECK_RESULT
OUTPUT_QUIET ERROR_QUIET)
if(NOT ESPTOOL_CHECK_RESULT EQUAL 0)
message(WARNING "esptool.py version 4.8.0 or higher recommended")
endif()
endif()
# ##############################################################################
# Generate binary
# ##############################################################################
if(CONFIG_ESPRESSIF_BOOTLOADER_MCUBOOT)
# MCUboot: Use imgtool to sign the image
message(STATUS "Generate NuttX signed image")
# Check if nuttx.hex exists
if(NOT EXISTS "${BINARY_DIR}/nuttx.hex")
message(FATAL_ERROR "nuttx.hex not found in ${BINARY_DIR}")
endif()
# Get imgtool arguments from config
if(CONFIG_ESPRESSIF_SECURE_FLASH_ENC_ENABLED)
set(IMGTOOL_ALIGN_ARGS --align 32 --max-align 32)
else()
set(IMGTOOL_ALIGN_ARGS --align 4)
endif()
if(DEFINED CONFIG_ESPRESSIF_MCUBOOT_SIGN_IMAGE_VERSION)
set(MCUBOOT_VERSION "${CONFIG_ESPRESSIF_MCUBOOT_SIGN_IMAGE_VERSION}")
else()
set(MCUBOOT_VERSION "0.0.0")
endif()
if(DEFINED CONFIG_ESPRESSIF_APP_MCUBOOT_HEADER_SIZE)
set(HEADER_SIZE ${CONFIG_ESPRESSIF_APP_MCUBOOT_HEADER_SIZE})
else()
set(HEADER_SIZE 0x20)
endif()
if(DEFINED CONFIG_ESPRESSIF_OTA_SLOT_SIZE)
set(SLOT_SIZE ${CONFIG_ESPRESSIF_OTA_SLOT_SIZE})
else()
set(SLOT_SIZE 0x100000)
endif()
if(CONFIG_ESPRESSIF_ESPTOOL_TARGET_PRIMARY)
set(VERIFIED --confirm)
else()
set(VERIFIED "")
endif()
set(IMGTOOL_SIGN_ARGS
--pad
${VERIFIED}
${IMGTOOL_ALIGN_ARGS}
-v
${MCUBOOT_VERSION}
-s
auto
-H
${HEADER_SIZE}
--pad-header
-S
${SLOT_SIZE})
execute_process(
COMMAND ${IMGTOOL} sign ${IMGTOOL_SIGN_ARGS} ${BINARY_DIR}/nuttx.hex
${BINARY_DIR}/nuttx.bin
RESULT_VARIABLE IMGTOOL_RESULT
WORKING_DIRECTORY ${BINARY_DIR})
if(NOT IMGTOOL_RESULT EQUAL 0)
message(FATAL_ERROR "imgtool sign failed")
endif()
message(STATUS "Generated: nuttx.bin (MCUboot compatible)")
else()
# Simple boot or legacy: Use esptool elf2image
message(STATUS "Generate NuttX image (esptool elf2image)")
# Check if nuttx ELF exists
if(NOT EXISTS "${BINARY_DIR}/nuttx")
message(FATAL_ERROR "nuttx ELF not found in ${BINARY_DIR}")
endif()
# Build elf2image options
set(ELF2IMAGE_OPTS -fs ${FLASH_SIZE} -fm ${FLASH_MODE} -ff ${FLASH_FREQ})
if(CONFIG_ESPRESSIF_SIMPLE_BOOT)
list(APPEND ELF2IMAGE_OPTS --ram-only-header)
endif()
execute_process(
COMMAND ${ESPTOOL} -c ${CHIP_SERIES} elf2image ${ELF2IMAGE_OPTS} -o
${BINARY_DIR}/nuttx.bin ${BINARY_DIR}/nuttx
RESULT_VARIABLE ESPTOOL_RESULT
WORKING_DIRECTORY ${BINARY_DIR})
if(NOT ESPTOOL_RESULT EQUAL 0)
message(FATAL_ERROR "esptool.py elf2image failed")
endif()
message(STATUS "Generated: nuttx.bin")
endif()
# ##############################################################################
# Update manifest
# ##############################################################################
file(APPEND "${BINARY_DIR}/nuttx.manifest" "nuttx.bin\n")
# ##############################################################################
# Merge binaries (optional)
# ##############################################################################
if(CONFIG_ESPRESSIF_MERGE_BINS)
message(STATUS "MERGEBIN: Creating merged flash image ${SOURCE_DIR}")
if(NOT ESPTOOL)
message(FATAL_ERROR "esptool.py not found - cannot merge binaries")
endif()
# Build the list of binaries to merge Format: offset1 file1 offset2 file2 ...
set(ESPTOOL_BINS "")
if(CONFIG_ESPRESSIF_BOOTLOADER_MCUBOOT)
# MCUboot configuration
if(EXISTS "${SOURCE_DIR}/mcuboot-${CHIP_SERIES}.bin")
message(
STATUS
"Merge bin: ${BL_OFFSET} -> ${SOURCE_DIR}/mcuboot-${CHIP_SERIES}.bin")
list(APPEND ESPTOOL_BINS ${BL_OFFSET}
"${SOURCE_DIR}/mcuboot-${CHIP_SERIES}.bin")
else()
message(
FATAL_ERROR "mcuboot-${CHIP_SERIES}.bin not found in ${SOURCE_DIR}")
endif()
# Create empty vefuse.bin if it doesn't exist
if(NOT EXISTS "${BINARY_DIR}/vefuse.bin")
file(WRITE "${BINARY_DIR}/vefuse.bin" "")
endif()
list(APPEND ESPTOOL_BINS ${EFUSE_OFFSET} "${BINARY_DIR}/vefuse.bin")
message(STATUS "Merge bin: ${EFUSE_OFFSET} -> ${BINARY_DIR}/vefuse.bin")
list(APPEND ESPTOOL_BINS ${MCUBOOT_APP_OFFSET} "${BINARY_DIR}/nuttx.bin")
message(
STATUS "Merge bin: ${MCUBOOT_APP_OFFSET} -> ${BINARY_DIR}/nuttx.bin")
elseif(CONFIG_ESPRESSIF_SIMPLE_BOOT)
# Simple boot: same base offset as BL_OFFSET (0x2000 on ESP32-P4, else 0x0)
list(APPEND ESPTOOL_BINS ${BL_OFFSET} "${BINARY_DIR}/nuttx.bin")
else()
# Legacy boot: application at offset 0
list(APPEND ESPTOOL_BINS 0x0000 "${BINARY_DIR}/nuttx.bin")
endif()
# Execute merge_bin
execute_process(
COMMAND ${ESPTOOL} -c ${CHIP_SERIES} merge-bin --pad-to-size ${FLASH_SIZE}
--output ${BINARY_DIR}/nuttx.merged.bin ${ESPTOOL_BINS}
RESULT_VARIABLE MERGEBIN_RESULT
WORKING_DIRECTORY ${BINARY_DIR})
if(NOT MERGEBIN_RESULT EQUAL 0)
message(FATAL_ERROR "esptool merge-bin failed")
endif()
message(STATUS "Generated: nuttx.merged.bin")
file(APPEND "${BINARY_DIR}/nuttx.manifest" "nuttx.merged.bin\n")
endif()