diff --git a/cpu/esp32/Kconfig b/cpu/esp32/Kconfig index 1847aae904..544e2dd412 100644 --- a/cpu/esp32/Kconfig +++ b/cpu/esp32/Kconfig @@ -1,5 +1,5 @@ # Copyright (c) 2020 HAW Hamburg -# 2021 Gunar Schorcht +# 2022 Gunar Schorcht # # This file is subject to the terms and conditions of the GNU Lesser # General Public License v2.1. See the file LICENSE in the top level @@ -20,17 +20,12 @@ config CPU_FAM_ESP32 select HAS_PUF_SRAM select PACKAGE_ESP32_SDK if TEST_KCONFIG - select PACKAGE_ESP32_SDK_LIBS if TEST_KCONFIG - select MODULE_ESP_IDF_DRIVER if TEST_KCONFIG - select MODULE_ESP_IDF_ESP32 if TEST_KCONFIG - select MODULE_ESP_IDF_SOC if TEST_KCONFIG - - select MODULE_PERIPH_RTT if HAS_PERIPH_RTT && MODULE_PM_LAYERED - select MODULE_RTT_RTC if HAS_PERIPH_RTT && MODULE_PERIPH_RTC - select MODULE_PS if MODULE_SHELL - select MODULE_PTHREAD if MODULE_CPP && TEST_KCONFIG select MODULE_LIBC_GETTIMEOFDAY if TEST_KCONFIG + select MODULE_RTT_RTC if HAS_PERIPH_RTT && MODULE_PERIPH_RTC + select MODULE_PERIPH_RTT if HAS_PERIPH_RTT && MODULE_PM_LAYERED + select MODULE_PS if MODULE_SHELL + select MODULE_PTHREAD if MODULE_CPP imply MODULE_NEWLIB_NANO ## CPU Models @@ -125,7 +120,11 @@ menu "ESP32 specific configurations" config MODULE_ESP_SPI_RAM bool "SPI RAM support" depends on HAS_ESP_SPI_RAM + select MODULE_ESP_IDF_EFUSE + select MODULE_ESP_IDF_GPIO select MODULE_ESP_IDF_HEAP + select MODULE_ESP_IDF_SPI_FLASH + select MODULE_ESP_IDF_SPI_RAM help Say y to use external SPI RAM connected through the FSPI interface. @@ -135,6 +134,7 @@ menu "ESP32 specific configurations" endmenu +rsource "bootloader/Kconfig" +rsource "esp-idf/Kconfig" rsource "periph/Kconfig" -rsource "vendor/esp-idf/Kconfig" source "$(RIOTCPU)/esp_common/Kconfig" diff --git a/cpu/esp32/Makefile b/cpu/esp32/Makefile index 95ea5c48bf..621be88e25 100644 --- a/cpu/esp32/Makefile +++ b/cpu/esp32/Makefile @@ -6,7 +6,11 @@ SRC = irq_arch.c startup.c syscalls.c # Add a list of subdirectories, that should also be built: DIRS += $(RIOTCPU)/esp_common DIRS += periph -DIRS += vendor +DIRS += esp-idf + +ifneq (, $(filter esp_bootloader, $(USEMODULE))) + DIRS += bootloader +endif ifneq (, $(filter esp_eth, $(USEMODULE))) DIRS += esp-eth diff --git a/cpu/esp32/Makefile.dep b/cpu/esp32/Makefile.dep index 6c01c61ae5..bb96004bc3 100644 --- a/cpu/esp32/Makefile.dep +++ b/cpu/esp32/Makefile.dep @@ -3,11 +3,9 @@ include $(RIOTCPU)/esp_common/Makefile.dep USEPKG += esp32_sdk -USEPKG += esp32_sdk_libs -USEMODULE += esp_idf_driver -USEMODULE += esp_idf_esp32 -USEMODULE += esp_idf_soc +USEMODULE += esp_idf_common +USEMODULE += esp_bootloader ifneq (,$(filter newlib,$(USEMODULE))) DEFAULT_MODULE += newlib_nano @@ -18,28 +16,38 @@ ifneq (,$(filter cpp,$(USEMODULE))) endif ifneq (,$(filter esp_eth,$(USEMODULE))) - USEMODULE += esp_freertos + USEMODULE += esp_idf_efuse USEMODULE += esp_idf_eth - USEMODULE += esp_idf_eth_phy + USEMODULE += esp_idf_event + USEMODULE += esp_idf_gpio + USEMODULE += esp_idf_spi_flash USEMODULE += netdev_eth USEMODULE += netopt USEMODULE += ztimer_msec endif ifneq (,$(filter esp_wifi_any,$(USEMODULE))) - # add additional modules used for any WiFi interface - USEMODULE += esp_freertos + # add additional modules and packages used for any WiFi interface + USEPKG += esp32_sdk_lib_phy + USEPKG += esp32_sdk_lib_wifi + USEMODULE += esp_idf_efuse + USEMODULE += esp_idf_event USEMODULE += esp_idf_heap USEMODULE += esp_idf_nvs_flash - USEMODULE += esp_idf_wpa_supplicant_crypto - USEMODULE += esp_idf_wpa_supplicant_port + USEMODULE += esp_idf_spi_flash + USEMODULE += esp_idf_wifi + USEMODULE += esp_idf_wpa_supplicant + USEMODULE += pthread endif -ifneq (,$(filter esp_wifi_enterprise,$(USEMODULE))) - # add additional modules used for WPA2 Enterprise mode - USEMODULE += esp_idf_wpa_supplicant_wpa2_eap_peer - USEMODULE += esp_idf_wpa_supplicant_wpa2_tls - USEMODULE += esp_idf_wpa_supplicant_wpa2_utils +ifneq (,$(filter esp_idf_heap,$(USEMODULE))) + # The ESP-IDF heap component uses the TLSF implementation that is part of + # the component. To avoid conflicts with modules and packages that use the + # RIOT package `tlsf`, this package is also used for the ESP-IDF heap instead + # of its own implementation. There does not seem to be any differences in + # the implementations of TLSF with the exception of heap poisoning, which + # is not configured. + USEPKG += tlsf endif ifneq (,$(filter esp_idf_nvs_flash,$(USEMODULE))) @@ -74,13 +82,13 @@ ifneq (,$(filter periph_i2c,$(USEMODULE))) endif endif -ifneq (,$(filter esp_now esp_wifi esp_spi_ram,$(USEMODULE))) - # the ESP-IDF heap has to be used if SPI RAM is used - USEMODULE += esp_idf_heap -endif - ifneq (,$(filter esp_spi_ram,$(USEMODULE))) FEATURES_REQUIRED += esp_spi_ram + USEMODULE += esp_idf_efuse + USEMODULE += esp_idf_gpio + USEMODULE += esp_idf_heap + USEMODULE += esp_idf_spi_flash + USEMODULE += esp_idf_spi_ram endif ifneq (,$(filter mtd,$(USEMODULE))) diff --git a/cpu/esp32/Makefile.include b/cpu/esp32/Makefile.include index 30298e68b7..3d0bff55e0 100644 --- a/cpu/esp32/Makefile.include +++ b/cpu/esp32/Makefile.include @@ -1,5 +1,5 @@ # ESP32 specific flashing options -FLASH_CHIP = esp32 +FLASH_CHIP = $(CPU) FLASH_MODE ?= dout FLASH_FREQ = 40m # DO NOT CHANGE FLASH_SIZE ?= 4 @@ -11,9 +11,12 @@ include $(RIOTCPU)/esp_common/Makefile.include # regular Makefile -TARGET_ARCH_ESP32 ?= xtensa-esp32-elf -TARGET_ARCH ?= $(TARGET_ARCH_ESP32) +TARGET_ARCH_$(CPU) ?= xtensa-$(CPU)-elf +TARGET_ARCH ?= $(TARGET_ARCH_$(CPU)) +CPU_UC = $(shell echo '$(CPU)' | tr '[:lower:]' '[:upper:]') + +PSEUDOMODULES += esp_bootloader PSEUDOMODULES += esp_gdbstub PSEUDOMODULES += esp_hw_counter PSEUDOMODULES += esp_i2c_hw @@ -22,34 +25,94 @@ PSEUDOMODULES += esp_rtc_timer_32k PSEUDOMODULES += esp_spi_ram PSEUDOMODULES += esp_wifi_enterprise -INCLUDES += -I$(RIOTCPU)/$(CPU)/vendor/ -INCLUDES += -I$(RIOTCPU)/$(CPU)/vendor/esp-idf/include -INCLUDES += -I$(RIOTCPU)/$(CPU)/vendor/esp-idf/include/esp32 -INCLUDES += -I$(RIOTCPU)/$(CPU)/vendor/esp-idf/include/heap -INCLUDES += -I$(RIOTCPU)/$(CPU)/vendor/esp-idf/include/spi_flash -INCLUDES += -I$(RIOTCPU)/$(CPU)/vendor/esp-idf/include/tcpip_adapter -INCLUDES += -I$(RIOTCPU)/$(CPU) +INCLUDES += -I$(RIOTCPU)/$(CPU)/esp-idf/include +INCLUDES += -I$(RIOTCPU)/$(CPU)/esp-idf/include/log + +INCLUDES += -I$(ESP32_SDK_DIR)/components/ +INCLUDES += -I$(ESP32_SDK_DIR)/components/driver/include +INCLUDES += -I$(ESP32_SDK_DIR)/components/esp_common/include +INCLUDES += -I$(ESP32_SDK_DIR)/components/esp_hw_support/include +INCLUDES += -I$(ESP32_SDK_DIR)/components/esp_hw_support/include/soc +INCLUDES += -I$(ESP32_SDK_DIR)/components/esp_rom/include +INCLUDES += -I$(ESP32_SDK_DIR)/components/esp_rom/include/$(CPU) +INCLUDES += -I$(ESP32_SDK_DIR)/components/esp_timer/include +INCLUDES += -I$(ESP32_SDK_DIR)/components/hal/$(CPU)/include +INCLUDES += -I$(ESP32_SDK_DIR)/components/hal/include +INCLUDES += -I$(ESP32_SDK_DIR)/components/heap/include +INCLUDES += -I$(ESP32_SDK_DIR)/components/log/include +INCLUDES += -I$(ESP32_SDK_DIR)/components/newlib/platform_include +INCLUDES += -I$(ESP32_SDK_DIR)/components/soc/include +INCLUDES += -I$(ESP32_SDK_DIR)/components/soc/$(CPU)/include +INCLUDES += -I$(ESP32_SDK_DIR)/components/xtensa/include +INCLUDES += -I$(ESP32_SDK_DIR)/components/xtensa/$(CPU)/include + +ifneq (,$(filter esp_spi_ram,$(USEMODULE))) + INCLUDES += -I$(ESP32_SDK_DIR)/components/esp_hw_support/include/soc/$(CPU) +endif + +ifneq (,$(filter esp_idf_spi_flash,$(USEMODULE))) + INCLUDES += -I$(ESP32_SDK_DIR)/components/bootloader_support/include + INCLUDES += -I$(ESP32_SDK_DIR)/components/spi_flash/include +endif + +ifneq (,$(filter esp_wifi_any,$(USEMODULE))) + INCLUDES += -I$(ESP32_SDK_DIR)/components/bootloader_support/include + INCLUDES += -I$(ESP32_SDK_DIR)/components/esp_eth/include + INCLUDES += -I$(ESP32_SDK_DIR)/components/esp_event/include + INCLUDES += -I$(ESP32_SDK_DIR)/components/esp_netif/include + INCLUDES += -I$(ESP32_SDK_DIR)/components/esp_system/include + INCLUDES += -I$(ESP32_SDK_DIR)/components/esp_wifi/include + INCLUDES += -I$(ESP32_SDK_DIR)/components/nvs_flash/include + INCLUDES += -I$(ESP32_SDK_DIR)/components/spi_flash/include +endif + +ifneq (,$(filter esp_wifi_enterprise,$(USEMODULE))) + INCLUDES += -I$(ESP32_SDK_DIR)/components/wpa_supplicant/esp_supplicant/include +endif ifneq (,$(filter esp_eth,$(USEMODULE))) INCLUDES += -I$(RIOTCPU)/$(CPU)/esp-eth - INCLUDES += -I$(RIOTCPU)/$(CPU)/vendor/esp-idf/include/ethernet + INCLUDES += -I$(ESP32_SDK_DIR)/components/esp_eth/include + INCLUDES += -I$(ESP32_SDK_DIR)/components/esp_event/include + INCLUDES += -I$(ESP32_SDK_DIR)/components/esp_netif/include + INCLUDES += -I$(ESP32_SDK_DIR)/components/esp_wifi/include endif -CFLAGS += -DSDK_NOT_USED -DCONFIG_FREERTOS_UNICORE=1 -DESP_PLATFORM +# we use ESP32 only in single core mode +CFLAGS += -DCONFIG_ESP_SYSTEM_SINGLE_CORE_MODE +CFLAGS += -DCONFIG_FREERTOS_UNICORE + +# other ESP-IDF configurations +CFLAGS += -DCONFIG_IDF_TARGET_$(CPU_UC) +CFLAGS += -DESP_PLATFORM CFLAGS += -DLOG_TAG_IN_BRACKETS + +# shortcuts used by ESP-IDF +CFLAGS += -Dasm=__asm +CFLAGS += -Dtypeof=__typeof__ CFLAGS += -D_CONST=const +LINKFLAGS += -L$(ESP32_SDK_DIR)/components/xtensa/$(CPU) +ARCHIVES += -lxt_hal + LINKFLAGS += -L$(RIOTCPU)/$(CPU)/ld/ -LINKFLAGS += -T$(RIOTCPU)/$(CPU)/ld/esp32.ld -LINKFLAGS += -T$(RIOTCPU)/$(CPU)/ld/esp32.common.ld -LINKFLAGS += -T$(RIOTCPU)/$(CPU)/ld/esp32.peripherals.ld -LINKFLAGS += -T$(RIOTCPU)/$(CPU)/ld/esp32.rom.ld -LINKFLAGS += -T$(RIOTCPU)/$(CPU)/ld/esp32.rom.nanofmt.ld +LINKFLAGS += -T$(ESP32_SDK_DIR)/components/soc/$(CPU)/ld/$(CPU).peripherals.ld +LINKFLAGS += -T$(ESP32_SDK_DIR)/components/esp_rom/$(CPU)/ld/$(CPU).rom.api.ld +LINKFLAGS += -T$(ESP32_SDK_DIR)/components/esp_rom/$(CPU)/ld/$(CPU).rom.ld +LINKFLAGS += -T$(ESP32_SDK_DIR)/components/esp_rom/$(CPU)/ld/$(CPU).rom.newlib-data.ld +LINKFLAGS += -T$(ESP32_SDK_DIR)/components/esp_rom/$(CPU)/ld/$(CPU).rom.newlib-funcs.ld +LINKFLAGS += -T$(ESP32_SDK_DIR)/components/esp_rom/$(CPU)/ld/$(CPU).rom.newlib-time.ld +LINKFLAGS += -T$(ESP32_SDK_DIR)/components/esp_rom/$(CPU)/ld/$(CPU).rom.spiflash.ld +LINKFLAGS += -T$(ESP32_SDK_DIR)/components/esp_rom/$(CPU)/ld/$(CPU).rom.redefined.ld +LINKFLAGS += -T$(RIOTCPU)/$(CPU)/ld/memory.ld +LINKFLAGS += -T$(RIOTCPU)/$(CPU)/ld/sections.ld LINKFLAGS += -nostdlib -lgcc -Wl,-gc-sections # Libraries needed when using esp_wifi_any pseudomodule ifneq (,$(filter esp_wifi_any,$(USEMODULE))) - ARCHIVES += -lcore -lrtc -lnet80211 -lpp -lcoexist -lwps -lwpa -lwpa2 + LINKFLAGS += -L$(ESP32_SDK_LIB_WIFI_DIR)/$(CPU) + LINKFLAGS += -L$(ESP32_SDK_LIB_PHY_DIR)/$(CPU) + ARCHIVES += -lcore -lrtc -lnet80211 -lpp -lcoexist ARCHIVES += -lphy -lstdc++ endif @@ -62,11 +125,16 @@ ifneq (,$(filter cpp,$(USEMODULE))) ARCHIVES += -lstdc++ endif +ifneq (,$(filter esp_bootloader,$(USEMODULE))) + # Bootloader file used by esptool.inc.mk + BOOTLOADER_BIN ?= $(BINDIR)/esp_bootloader/bootloader.bin +endif + ifneq (,$(filter esp_jtag,$(USEMODULE))) PROGRAMMERS_SUPPORTED += openocd PARTITION_POS = 0x8000 OPENOCD_PRE_FLASH_CMDS = -c 'echo "Installing Bootloader at $(BOOTLOADER_POS)"' \ - -c 'flash write_image erase "$(RIOTCPU)/$(CPU)/bin/bootloader.bin" $(BOOTLOADER_POS) bin' \ + -c 'flash write_image erase "$(BOOTLOADER_BIN)" $(BOOTLOADER_POS) bin' \ -c 'echo "Installing partition table at $(PARTITION_POS)"' \ -c 'flash write_image erase "$(BINDIR)/partitions.bin" $(PARTITION_POS) bin' IMAGE_OFFSET = 0x10000 @@ -75,3 +143,5 @@ ifneq (,$(filter esp_jtag,$(USEMODULE))) # Without resets debug target fails with 'Target not examined yet' OPENOCD_DBG_EXTRA_CMD += -c 'reset halt' endif + +$(BOOTLOADER_BIN): diff --git a/cpu/esp32/startup.c b/cpu/esp32/startup.c index a0c216777b..f2425b354c 100644 --- a/cpu/esp32/startup.c +++ b/cpu/esp32/startup.c @@ -24,27 +24,31 @@ #include #include -#include "macros/units.h" +/* RIOT headers have to be included before ESP-IDF headers! */ #include "board.h" -#include "esp_attr.h" +#include "esp/common_macros.h" #include "exceptions.h" #include "irq_arch.h" #include "kernel_defines.h" #include "kernel_init.h" #include "log.h" +#include "periph_cpu.h" #include "stdio_base.h" #include "syscalls.h" #include "thread_arch.h" +#include "tools.h" #include "periph/cpuid.h" #include "periph/init.h" #include "periph/rtc.h" +/* ESP-IDF headers */ #include "driver/periph_ctrl.h" -#include "esp/common_macros.h" -#include "esp32/esp_sleep.h" -#include "heap/esp_heap_caps_init.h" -#include "log/esp_log.h" +#include "esp_attr.h" +#include "esp_heap_caps_init.h" +#include "esp_log.h" +#include "esp_sleep.h" +#include "esp_timer.h" #include "rom/cache.h" #include "rom/ets_sys.h" #include "rom/rtc.h" @@ -60,14 +64,15 @@ #include "xtensa/core-macros.h" #include "xtensa/xtensa_api.h" -#include "periph_cpu.h" -#include "tools.h" +#if IS_USED(MODULE_ESP_SPI_RAM) +#include "spiram.h" +#endif -#ifdef MODULE_PUF_SRAM +#if IS_USED(MODULE_PUF_SRAM) #include "puf_sram.h" #endif -#ifdef MODULE_STDIO_UART +#if IS_USED(MODULE_STDIO_UART) #include "stdio_uart.h" #endif @@ -77,7 +82,7 @@ #define STRINGIFY(s) STRINGIFY2(s) #define STRINGIFY2(s) #s -#if MODULE_ESP_LOG_STARTUP +#if IS_USED(MODULE_ESP_LOG_STARTUP) #define LOG_STARTUP(format, ...) LOG_TAG_EARLY(LOG_INFO, D, __func__, format, ##__VA_ARGS__) #else #define LOG_STARTUP(format, ...) @@ -93,24 +98,35 @@ extern uint8_t _rtc_bss_start; extern uint8_t _rtc_bss_end; extern uint8_t _rtc_bss_rtc_start; extern uint8_t _rtc_bss_rtc_end; -extern uint8_t _init_start; +extern uint8_t _iram_start; /* external esp function declarations */ extern void esp_clk_init(void); extern void esp_perip_clk_init(void); extern void esp_reent_init(struct _reent* r); -extern void esp_panic_wdt_stop (void); -extern void spi_ram_init(void); -extern void spi_ram_heap_init(void); extern uint32_t hwrand (void); extern void bootloader_clock_configure(void); +/* external RTC function declarations since they are not declared in headers */ +/* components/esp_hw_support/include/esp_private/esp_clk.h */ +extern int esp_clk_apb_freq(void); +extern int esp_clk_cpu_freq(void); +extern int esp_clk_xtal_freq(void); +extern uint32_t esp_clk_slowclk_cal_get(void); +/* components/esp_hw_support/include/soc/esp32/rtc.h */ +extern uint64_t esp_rtc_get_time_us(void); + +/* components/esp_system/port/include/esp_clk_internal.h */ +extern void IRAM_ATTR rtc_clk_select_rtc_slow_clk(rtc_slow_freq_t slow_clk); + /* forward declarations */ static void system_init(void); static void intr_matrix_clear(void); typedef int32_t esp_err_t; +uint64_t g_startup_time = 0; + /** * @brief CPU startup function * @@ -121,16 +137,16 @@ NORETURN void IRAM call_start_cpu0 (void) { register uint32_t *sp __asm__ ("a1"); (void)sp; - cpu_configure_region_protection(); + esp_cpu_configure_region_protection(); /* move exception vectors to IRAM */ - asm volatile ("wsr %0, vecbase\n" ::"r"(&_init_start)); + asm volatile ("wsr %0, vecbase\n" ::"r"(&_iram_start)); RESET_REASON reset_reason = rtc_get_reset_reason(PRO_CPU_NUM); /* reset from panic handler by RWDT or TG0WDT */ if (reset_reason == RTCWDT_SYS_RESET || reset_reason == TG0WDT_SYS_RESET) { - esp_panic_wdt_stop(); + /* TODO esp_panic_wdt_stop was called here in former versions */ } #ifdef MODULE_PUF_SRAM @@ -157,7 +173,7 @@ NORETURN void IRAM call_start_cpu0 (void) uint8_t cpu_id[CPUID_LEN]; cpuid_get ((void*)cpu_id); -#ifdef MODULE_ESP_LOG_STARTUP +#if IS_USED(MODULE_ESP_LOG_STARTUP) ets_printf("\n"); LOG_STARTUP("Starting ESP32 with ID: "); for (unsigned i = 0; i < CPUID_LEN; i++) { @@ -177,8 +193,8 @@ NORETURN void IRAM call_start_cpu0 (void) } LOG_STARTUP("Current clocks in Hz: CPU=%d APB=%d XTAL=%d SLOW=%d\n", - rtc_clk_cpu_freq_value(rtc_clk_cpu_freq_get()), - rtc_clk_apb_freq_get(), MHZ(rtc_clk_xtal_freq_get()), + esp_clk_cpu_freq(), + esp_clk_apb_freq(), esp_clk_xtal_freq(), rtc_clk_slow_freq_get_hz()); if (IS_ACTIVE(ENABLE_DEBUG)) { @@ -207,9 +223,12 @@ NORETURN void IRAM call_start_cpu0 (void) } /* init SPI RAM if enabled */ - #if CONFIG_SPIRAM_SUPPORT && CONFIG_SPIRAM_BOOT_INIT - spi_ram_init(); - #endif +#if CONFIG_SPIRAM_SUPPORT && CONFIG_SPIRAM_BOOT_INIT + if (esp_spiram_init() != ESP_OK) { + LOG_STARTUP("Failed to initialize SPI RAM\n"); + exit(1); + } +#endif LOG_STARTUP("PRO cpu starts user code\n"); system_init(); @@ -217,12 +236,6 @@ NORETURN void IRAM call_start_cpu0 (void) UNREACHABLE(); } -#define RTC_FAST_FREQ_8M_MHZ 8000000 -#define rtc_select_slow_clk select_rtc_slow_clk - -extern uint32_t esp_clk_slowclk_cal_get(void); -extern void IRAM_ATTR rtc_select_slow_clk(rtc_slow_freq_t slow_clk); - static void IRAM system_clk_init (void) { /* first initialize RTC with default configuration */ @@ -238,13 +251,13 @@ static void IRAM system_clk_init (void) /* set FAST_CLK to internal low power clock of 8 MHz */ rtc_clk_fast_freq_set(RTC_FAST_FREQ_8M); -#if MODULE_ESP_RTC_TIMER_32K +#if IS_USED(MODULE_ESP_RTC_TIMER_32K) /* set SLOW_CLK to external 32.768 kHz crystal clock */ - rtc_select_slow_clk(RTC_SLOW_FREQ_32K_XTAL); + rtc_clk_select_rtc_slow_clk(RTC_SLOW_FREQ_32K_XTAL); #else /* set SLOW_CLK to internal low power clock of 150 kHz */ rtc_clk_32k_enable(false); - rtc_select_slow_clk(RTC_SLOW_FREQ_RTC); + rtc_clk_select_rtc_slow_clk(RTC_SLOW_FREQ_RTC); #endif LOG_STARTUP("Switching system clocks can lead to some unreadable characters\n"); @@ -267,11 +280,14 @@ static void IRAM system_clk_init (void) set to 2 MHz and handled later */ } - uint32_t freq_before = rtc_clk_cpu_freq_value(rtc_clk_cpu_freq_get()) / MHZ(1); + uint32_t freq_before = esp_clk_cpu_freq() / MHZ; if (freq_before != CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ) { + rtc_cpu_freq_config_t clk_cfg; + rtc_clk_cpu_freq_to_config(freq, &clk_cfg); + /* set configured CPU frequency */ - rtc_clk_cpu_freq_set(freq); + rtc_clk_cpu_freq_set_config(&clk_cfg); /* Recalculate the ccount to make time calculation correct. */ uint32_t freq_after = CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ; @@ -294,6 +310,7 @@ static NORETURN void IRAM system_init (void) /* disable clocks of peripherals that are not needed at startup */ esp_perip_clk_init(); + g_startup_time = esp_rtc_get_time_us(); /* set configured console UART baudrate */ const int uart_clk_freq = rtc_clk_apb_freq_get(); @@ -328,15 +345,18 @@ static NORETURN void IRAM system_init (void) srand(hwrand()); /* add SPI RAM to heap if enabled */ - #if CONFIG_SPIRAM_SUPPORT && CONFIG_SPIRAM_BOOT_INIT - spi_ram_heap_init(); - #endif +#if CONFIG_SPIRAM_SUPPORT && CONFIG_SPIRAM_BOOT_INIT + esp_spiram_init_cache(); + esp_spiram_add_to_heapalloc(); +#endif /* print some infos */ LOG_STARTUP("Used clocks in Hz: CPU=%d APB=%d XTAL=%d FAST=%d SLOW=%d\n", - rtc_clk_cpu_freq_value(rtc_clk_cpu_freq_get()), - rtc_clk_apb_freq_get(), MHZ(rtc_clk_xtal_freq_get()), - RTC_FAST_FREQ_8M_MHZ, rtc_clk_slow_freq_get_hz()); + esp_clk_cpu_freq(), + esp_clk_apb_freq(), esp_clk_xtal_freq(), + rtc_clk_fast_freq_get() == RTC_FAST_FREQ_8M ? 8 * MHZ + : esp_clk_xtal_freq()/4, + rtc_clk_slow_freq_get_hz()); LOG_STARTUP("XTAL calibration value: %d\n", esp_clk_slowclk_cal_get()); LOG_STARTUP("Heap free: %u bytes\n", get_free_heap_size()); uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM); @@ -352,7 +372,7 @@ static NORETURN void IRAM system_init (void) periph_init(); /* print system time */ -#ifdef MODULE_PERIPH_RTC +#if IS_USED(MODULE_PERIPH_RTC) struct tm _sys_time; rtc_get_time(&_sys_time); LOG_STARTUP("System time: %04d-%02d-%02d %02d:%02d:%02d\n", @@ -361,15 +381,15 @@ static NORETURN void IRAM system_init (void) #endif /* print the board config */ -#ifdef MODULE_ESP_LOG_STARTUP +#if IS_USED(MODULE_ESP_LOG_STARTUP) print_board_config(); #endif - #if MODULE_MTD +#if IS_USED(MODULE_MTD) /* init flash drive */ extern void spi_flash_drive_init (void); spi_flash_drive_init(); - #endif +#endif /* initialize the board */ extern void board_init(void); @@ -385,8 +405,11 @@ static NORETURN void IRAM system_init (void) extern void esp_event_handler_init(void); esp_event_handler_init(); + /* initialize ESP-IDF timer task */ + esp_timer_init(); + /* starting RIOT */ -#ifdef MODULE_ESP_LOG_STARTUP +#if IS_USED(MODULE_ESP_LOG_STARTUP) LOG_STARTUP("Starting RIOT kernel on PRO cpu\n"); uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM); #else