esp32: Add support to build using IDF with cmake.

This commit adds support for building the esp32 port with cmake, and in
particular it builds MicroPython as a component within the ESP-IDF.  Using
cmake and the ESP-IDF build infrastructure makes it much easier to maintain
the port, especially with the various new ESP32 MCUs and their required
toolchains.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2020-09-23 15:55:55 +10:00
parent 66098c0985
commit 9b90882146
10 changed files with 246 additions and 0 deletions

View File

@ -0,0 +1,38 @@
# Top-level cmake file for building MicroPython on ESP32.
cmake_minimum_required(VERSION 3.5)
# Set the location of this port's directory.
set(MICROPY_PORT_DIR ${CMAKE_SOURCE_DIR})
# Set the board if it's not already set.
if(NOT MICROPY_BOARD)
set(MICROPY_BOARD GENERIC)
endif()
# Set the board directory and check that it exists.
if(NOT MICROPY_BOARD_DIR)
set(MICROPY_BOARD_DIR ${MICROPY_PORT_DIR}/boards/${MICROPY_BOARD})
endif()
if(NOT EXISTS ${MICROPY_BOARD_DIR}/mpconfigboard.cmake)
message(FATAL_ERROR "Invalid MICROPY_BOARD specified: ${MICROPY_BOARD}")
endif()
# Define the output sdkconfig so it goes in the build directory.
set(SDKCONFIG ${CMAKE_BINARY_DIR}/sdkconfig)
# Include board config; this is expected to set SDKCONFIG_DEFAULTS (among other options).
include(${MICROPY_BOARD_DIR}/mpconfigboard.cmake)
# Concatenate all sdkconfig files into a combined one for the IDF to use.
file(WRITE ${CMAKE_BINARY_DIR}/sdkconfig.combined.in "")
foreach(SDKCONFIG_DEFAULT ${SDKCONFIG_DEFAULTS})
file(READ ${SDKCONFIG_DEFAULT} CONTENTS)
file(APPEND ${CMAKE_BINARY_DIR}/sdkconfig.combined.in "${CONTENTS}")
endforeach()
configure_file(${CMAKE_BINARY_DIR}/sdkconfig.combined.in ${CMAKE_BINARY_DIR}/sdkconfig.combined COPYONLY)
set(SDKCONFIG_DEFAULTS ${CMAKE_BINARY_DIR}/sdkconfig.combined)
# Include main IDF cmake file and define the project.
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(micropython)

View File

@ -0,0 +1,2 @@
set(SDKCONFIG_DEFAULTS boards/sdkconfig.base)
set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py)

View File

@ -0,0 +1,6 @@
set(SDKCONFIG_DEFAULTS
boards/sdkconfig.base
boards/GENERIC_D2WD/sdkconfig.board
)
set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py)

View File

@ -0,0 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_DIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_40M=y
CONFIG_ESPTOOLPY_FLASHSIZE_2MB=y
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-2MiB.csv"

View File

@ -0,0 +1,6 @@
set(SDKCONFIG_DEFAULTS
boards/sdkconfig.base
boards/GENERIC_OTA/sdkconfig.board
)
set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py)

View File

@ -1,4 +1,6 @@
CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-ota.csv"
# ESP-IDF v3:
CONFIG_APP_ROLLBACK_ENABLE=y

View File

@ -0,0 +1,6 @@
set(SDKCONFIG_DEFAULTS
boards/sdkconfig.base
boards/sdkconfig.spiram
)
set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py)

View File

@ -0,0 +1,8 @@
set(SDKCONFIG_DEFAULTS
boards/sdkconfig.base
boards/sdkconfig.240mhz
boards/sdkconfig.spiram
boards/TINYPICO/sdkconfig.board
)
set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest.py)

View File

@ -51,3 +51,8 @@ CONFIG_PPP_SUPPORT=y
CONFIG_PPP_PAP_SUPPORT=y
CONFIG_PPP_CHAP_SUPPORT=y
CONFIG_ULP_COPROC_ENABLED=y
# For cmake build
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"

View File

@ -0,0 +1,168 @@
# Set location of base MicroPython directory.
get_filename_component(MICROPY_DIR ${PROJECT_DIR}/../.. ABSOLUTE)
# Include core source components.
include(${MICROPY_DIR}/py/py.cmake)
include(${MICROPY_DIR}/extmod/extmod.cmake)
set(MICROPY_SOURCE_EXTMOD_EXTRA
${MICROPY_DIR}/extmod/modonewire.c
)
set(MICROPY_SOURCE_LIB
${MICROPY_DIR}/lib/littlefs/lfs1.c
${MICROPY_DIR}/lib/littlefs/lfs1_util.c
${MICROPY_DIR}/lib/littlefs/lfs2.c
${MICROPY_DIR}/lib/littlefs/lfs2_util.c
${MICROPY_DIR}/lib/mbedtls_errors/mp_mbedtls_errors.c
${MICROPY_DIR}/lib/mp-readline/readline.c
${MICROPY_DIR}/lib/netutils/netutils.c
${MICROPY_DIR}/lib/oofatfs/ff.c
${MICROPY_DIR}/lib/oofatfs/ffunicode.c
${MICROPY_DIR}/lib/timeutils/timeutils.c
${MICROPY_DIR}/lib/utils/interrupt_char.c
${MICROPY_DIR}/lib/utils/stdout_helpers.c
${MICROPY_DIR}/lib/utils/sys_stdio_mphal.c
${MICROPY_DIR}/lib/utils/pyexec.c
)
set(MICROPY_SOURCE_DRIVERS
${MICROPY_DIR}/drivers/bus/softspi.c
${MICROPY_DIR}/drivers/dht/dht.c
)
set(MICROPY_SOURCE_PORT
${PROJECT_DIR}/main.c
${PROJECT_DIR}/uart.c
${PROJECT_DIR}/gccollect.c
${PROJECT_DIR}/mphalport.c
${PROJECT_DIR}/fatfs_port.c
${PROJECT_DIR}/help.c
${PROJECT_DIR}/modutime.c
${PROJECT_DIR}/moduos.c
${PROJECT_DIR}/machine_timer.c
${PROJECT_DIR}/machine_pin.c
${PROJECT_DIR}/machine_touchpad.c
${PROJECT_DIR}/machine_adc.c
${PROJECT_DIR}/machine_dac.c
${PROJECT_DIR}/machine_i2c.c
${PROJECT_DIR}/machine_pwm.c
${PROJECT_DIR}/machine_uart.c
${PROJECT_DIR}/modmachine.c
${PROJECT_DIR}/modnetwork.c
${PROJECT_DIR}/network_lan.c
${PROJECT_DIR}/network_ppp.c
${PROJECT_DIR}/mpnimbleport.c
${PROJECT_DIR}/modsocket.c
${PROJECT_DIR}/modesp.c
${PROJECT_DIR}/esp32_partition.c
${PROJECT_DIR}/esp32_rmt.c
${PROJECT_DIR}/esp32_ulp.c
${PROJECT_DIR}/modesp32.c
${PROJECT_DIR}/espneopixel.c
${PROJECT_DIR}/machine_hw_spi.c
${PROJECT_DIR}/machine_wdt.c
${PROJECT_DIR}/mpthreadport.c
${PROJECT_DIR}/machine_rtc.c
${PROJECT_DIR}/machine_sdcard.c
)
set(MICROPY_SOURCE_QSTR
${MICROPY_SOURCE_PY}
${MICROPY_SOURCE_EXTMOD}
${MICROPY_SOURCE_EXTMOD_EXTRA}
${MICROPY_SOURCE_LIB}
${MICROPY_SOURCE_PORT}
)
set(IDF_COMPONENTS
app_update
bootloader_support
driver
esp32
esp_common
esp_eth
esp_event
esp_ringbuf
esp_rom
esp_wifi
freertos
heap
log
lwip
mbedtls
mdns
newlib
nvs_flash
sdmmc
soc
spi_flash
tcpip_adapter
ulp
vfs
xtensa
)
# Register the main IDF component.
idf_component_register(
SRCS
${MICROPY_SOURCE_PY}
${MICROPY_SOURCE_EXTMOD}
${MICROPY_SOURCE_EXTMOD_EXTRA}
${MICROPY_SOURCE_LIB}
${MICROPY_SOURCE_DRIVERS}
${MICROPY_SOURCE_PORT}
INCLUDE_DIRS
${MICROPY_DIR}
${MICROPY_PORT_DIR}
${MICROPY_BOARD_DIR}
${CMAKE_BINARY_DIR}
REQUIRES
${IDF_COMPONENTS}
)
# Set the MicroPython target as the current (main) IDF component target.
set(MICROPY_TARGET ${COMPONENT_TARGET})
# Define mpy-cross flags, for use with frozen code.
set(MICROPY_CROSS_FLAGS -march=xtensawin)
# Set compile options for this port.
target_compile_definitions(${MICROPY_TARGET} PUBLIC
MICROPY_ESP_IDF_4=1
MICROPY_VFS_FAT=1
MICROPY_VFS_LFS2=1
FFCONF_H=\"${MICROPY_OOFATFS_DIR}/ffconf.h\"
LFS1_NO_MALLOC LFS1_NO_DEBUG LFS1_NO_WARN LFS1_NO_ERROR LFS1_NO_ASSERT
LFS2_NO_MALLOC LFS2_NO_DEBUG LFS2_NO_WARN LFS2_NO_ERROR LFS2_NO_ASSERT
)
# Disable some warnings to keep the build output clean.
target_compile_options(${MICROPY_TARGET} PUBLIC
-Wno-clobbered
-Wno-deprecated-declarations
-Wno-missing-field-initializers
)
# Collect all of the include directories and compile definitions for the IDF components.
foreach(comp ${IDF_COMPONENTS})
get_target_property(type __idf_${comp} TYPE)
set(_inc OFF)
set(_def OFF)
if(${type} STREQUAL STATIC_LIBRARY)
get_target_property(_inc __idf_${comp} INCLUDE_DIRECTORIES)
get_target_property(_def __idf_${comp} COMPILE_DEFINITIONS)
elseif(${type} STREQUAL INTERFACE_LIBRARY)
get_target_property(_inc __idf_${comp} INTERFACE_INCLUDE_DIRECTORIES)
get_target_property(_def __idf_${comp} INTERFACE_COMPILE_DEFINITIONS)
endif()
if(_inc)
list(APPEND MICROPY_CPP_INC_EXTRA ${_inc})
endif()
if(_def)
list(APPEND MICROPY_CPP_DEF_EXTRA ${_def})
endif()
endforeach()
# Include the main MicroPython cmake rules.
include(${MICROPY_DIR}/py/mkrules.cmake)