Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,17 @@ To save power without using sleep modes, lowering the clock speed is another app
``CONFIG_ESPRESSIF_DFS`` option needs to enabled and minimum CPU frequency needs to set under ``CONFIG_ESPRESSIF_MIN_CPU_FREQ`` option.
With these options, the device scales the CPU clock according to workload.

psram_usrheap
Comment thread
tmedicci marked this conversation as resolved.
-------------

This configuration enables allocating the userspace heap into SPIRAM and reserves the
internal RAM for kernel heap. For instance, for a 32MB PSRAM::

nsh> free
total used free maxused maxfree nused nfree name
602004 6492 595512 6872 595512 36 1 Kmem
33554428 4276 33550152 4656 33550152 8 1 Umem

pwm
---

Expand Down
1 change: 1 addition & 0 deletions Documentation/platforms/risc-v/esp32p4/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ Parallel IO No
LCD Interface No
MIPI DSI No
Timers Yes
SPIRAM / PSRAM Yes
Watchdog Yes MWDT0/1 and RWDT
Ethernet No
Brownout No
Expand Down
2 changes: 1 addition & 1 deletion arch/risc-v/src/common/espressif/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ if(DEFINED ENV{ESP_HAL_3RDPARTY_VERSION})
CACHE STRING "ESP HAL 3rdparty version")
else()
set(ESP_HAL_3RDPARTY_VERSION
2209449c9864c6d83a0ee2295f5b5299a2c6fb39
d41c921a724da2b4955832ca9d4b117b004b61c6
CACHE STRING "ESP HAL 3rdparty version")
endif()

Expand Down
86 changes: 86 additions & 0 deletions arch/risc-v/src/common/espressif/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,92 @@ config ESPRESSIF_FLASH_32M

endchoice # ESPRESSIF_FLASH

config ESPRESSIF_DONT_USE_ROM_LIBC
bool "Don't use ROM libc functions"
default n
---help---
If enabled, NuttX libc will be used instead of ROM newlib
functions (strdup, strndup, atoi, etc.).

This prevents cross-heap allocation issues when ROM newlib
allocators are not mapped to the same heap as NuttX libc.

menuconfig ESPRESSIF_SPIRAM
bool "External SPI RAM (SPIRAM)"
depends on ARCH_CHIP_ESP32P4
default n
---help---
Enable external PSRAM support through the ESP HAL component.

if ESPRESSIF_SPIRAM

config ESPRESSIF_SPIRAM_BOOT_INIT
bool "Initialize SPIRAM during early boot"
default y
---help---
Initialize and map external PSRAM before heap region registration.

config ESPRESSIF_SPIRAM_IGNORE_NOTFOUND
bool "Ignore SPIRAM not found"
default n
---help---
Keep booting even if PSRAM detection fails.

config ESPRESSIF_SPIRAM_MEMTEST
bool "Run SPIRAM memory test at boot"
default y
---help---
Run a basic memory test before adding PSRAM to the allocator.

config ESPRESSIF_SPIRAM_USE_8LINE_MODE
bool "Use x8 line mode"
default n
---help---
Enable x8 line mode instead of x16 line mode.

config ESPRESSIF_SPIRAM_ECC
bool "Enable SPIRAM ECC"
default n
---help---
Enable ECC for external PSRAM accesses.

choice ESPRESSIF_SPIRAM_SPEED
prompt "SPIRAM speed"
default ESPRESSIF_SPIRAM_SPEED_200M if !ESP32P4_REV_MIN_0
default ESPRESSIF_SPIRAM_SPEED_80M

config ESPRESSIF_SPIRAM_SPEED_20M
bool "20 MHz"

config ESPRESSIF_SPIRAM_SPEED_80M
bool "80 MHz"

config ESPRESSIF_SPIRAM_SPEED_200M
bool "200 MHz"
depends on !ESP32P4_REV_MIN_0

config ESPRESSIF_SPIRAM_SPEED_250M
bool "250 MHz"
depends on !ESP32P4_REV_MIN_0

endchoice # ESPRESSIF_SPIRAM_SPEED

config ESPRESSIF_SPIRAM_SPEED
int
default 20 if ESPRESSIF_SPIRAM_SPEED_20M
default 80 if ESPRESSIF_SPIRAM_SPEED_80M
default 200 if ESPRESSIF_SPIRAM_SPEED_200M
default 250 if ESPRESSIF_SPIRAM_SPEED_250M

config ESPRESSIF_SPIRAM_USER_HEAP
bool "Add SPIRAM to user heap"
default y
select ESPRESSIF_DONT_USE_ROM_LIBC
---help---
Add SPIRAM as an additional NuttX heap region.

endif # ESPRESSIF_SPIRAM

config ESPRESSIF_IDF_ENV_FPGA
bool "IDF FPGA Environment"
default n
Expand Down
2 changes: 1 addition & 1 deletion arch/risc-v/src/common/espressif/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ endif

ESP_HAL_3RDPARTY_REPO = esp-hal-3rdparty
ifndef ESP_HAL_3RDPARTY_VERSION
ESP_HAL_3RDPARTY_VERSION = 2209449c9864c6d83a0ee2295f5b5299a2c6fb39
ESP_HAL_3RDPARTY_VERSION = d41c921a724da2b4955832ca9d4b117b004b61c6
endif

ifndef ESP_HAL_3RDPARTY_URL
Expand Down
82 changes: 80 additions & 2 deletions arch/risc-v/src/common/espressif/esp_allocateheap.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#ifdef CONFIG_ESPRESSIF_RETENTION_HEAP
# include "esp_retentionheap.h"
#endif
#if defined(CONFIG_ESPRESSIF_SPIRAM)
# include "esp_psram.h"
# include "esp_private/esp_psram_extram.h"
#endif

/****************************************************************************
* Pre-processor Definitions
Expand Down Expand Up @@ -90,9 +94,25 @@ void up_allocate_heap(void **heap_start, size_t *heap_size)
uintptr_t rstart;
uintptr_t rend;
#endif
#if defined(CONFIG_MM_KERNEL_HEAP) && \
defined(CONFIG_ESPRESSIF_SPIRAM) && \
defined(CONFIG_ESPRESSIF_SPIRAM_USER_HEAP)
uintptr_t ubase;
uintptr_t utop;
#endif

board_autoled_on(LED_HEAPALLOCATE);

#if defined(CONFIG_MM_KERNEL_HEAP) && \
defined(CONFIG_ESPRESSIF_SPIRAM) && \
defined(CONFIG_ESPRESSIF_SPIRAM_USER_HEAP)
DEBUGASSERT(esp_psram_is_initialized());
ubase = esp_psram_extram_vaddr_start();
utop = esp_psram_extram_vaddr_end();

*heap_start = (void *)ubase;
*heap_size = utop - ubase;
#else
*heap_start = (void *)g_idle_topstack;
#ifdef CONFIG_ESPRESSIF_RETENTION_HEAP
esp_retentionheap_find_region(&rstart, &rend);
Expand All @@ -101,8 +121,48 @@ void up_allocate_heap(void **heap_start, size_t *heap_size)
*heap_size = (uintptr_t)ets_rom_layout_p->dram0_rtos_reserved_start -
g_idle_topstack;
#endif
_heap_start = g_idle_topstack;
#endif
_heap_start = (uintptr_t)*heap_start;
}

/****************************************************************************
* Name: up_allocate_kheap
*
* Description:
* For the kernel builds (CONFIG_BUILD_PROTECTED=y or
* CONFIG_BUILD_KERNEL=y) there may be both kernel- and user-space heaps
* as determined by CONFIG_MM_KERNEL_HEAP=y. This function allocates (and
* protects) the kernel-space heap.
*
* For Flat build (CONFIG_BUILD_FLAT=y), this function enables a separate
* (although unprotected) heap for the kernel.
*
****************************************************************************/

#ifdef CONFIG_MM_KERNEL_HEAP
void up_allocate_kheap(void **heap_start, size_t *heap_size)
{
uintptr_t kbase = g_idle_topstack;
uintptr_t ktop;

#ifdef CONFIG_ESPRESSIF_RETENTION_HEAP
uintptr_t rstart;
uintptr_t rend;

esp_retentionheap_find_region(&rstart, &rend);
ktop = rstart;
#else
ktop = (uintptr_t)ets_rom_layout_p->dram0_rtos_reserved_start;
#endif

DEBUGASSERT(ktop > kbase);

board_autoled_on(LED_HEAPALLOCATE);

*heap_start = (void *)kbase;
*heap_size = ktop - kbase;
}
#endif

/****************************************************************************
* Name: riscv_addregion
Expand Down Expand Up @@ -134,9 +194,27 @@ void riscv_addregion(void)

if (region_size > 0)
{
#ifdef CONFIG_MM_KERNEL_HEAP
kmm_addregion(_sram_high_heap_start, region_size);
#else
kumm_addregion(_sram_high_heap_start, region_size);
#endif
}
#endif

#if !defined(CONFIG_MM_KERNEL_HEAP)
# if defined(CONFIG_ESPRESSIF_SPIRAM_USER_HEAP)
if (esp_psram_is_initialized())
{
uintptr_t start = esp_psram_extram_vaddr_start();
uintptr_t end = esp_psram_extram_vaddr_end();

if (end > start)
{
kumm_addregion((void *)start, end - start);
}
}
# endif
#endif
}
#endif

45 changes: 45 additions & 0 deletions arch/risc-v/src/common/espressif/esp_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,14 @@
#include "esp_app_format.h"
#endif

#include "bootloader_mem.h"
#include "bootloader_flash_priv.h"
#include "esp_private/startup_internal.h"
#include "esp_private/spi_flash_os.h"
#ifdef CONFIG_ESPRESSIF_SPIRAM
# include "esp_psram.h"
# include "esp_private/esp_psram_extram.h"
#endif

#if SOC_APM_SUPPORTED
# include "hal/apm_hal.h"
Expand Down Expand Up @@ -475,6 +480,8 @@ void sys_startup_fn(void)

void __esp_start(void)
{
esp_err_t ret;

esp_cpu_intr_set_ivt_addr(&_vector_table);

#if SOC_INT_CLIC_SUPPORTED
Expand Down Expand Up @@ -550,18 +557,54 @@ void __esp_start(void)

esp_rtc_init();

esp_mspi_pin_init();

/* Configure SPI Flash chip state */

spi_flash_init_chip_state();

esp_mmu_map_init();

#ifdef CONFIG_ESPRESSIF_SPIRAM
ret = esp_psram_chip_init();
if (ret != ESP_OK)
{
# ifndef CONFIG_ESPRESSIF_SPIRAM_IGNORE_NOTFOUND
PANIC();
# endif
}

# ifdef CONFIG_ESPRESSIF_SPIRAM_BOOT_INIT
if (ret == ESP_OK)
{
ret = esp_psram_init();
if (ret != ESP_OK)
{
# ifndef CONFIG_ESPRESSIF_SPIRAM_IGNORE_NOTFOUND
PANIC();
# endif
}
}
# endif
#endif

/* Configures the CPU clock, RTC slow and fast clocks, and performs
* RTC slow clock calibration.
*/

esp_clk_init();

esp_mspi_pin_reserve();

bootloader_init_mem();

#ifdef CONFIG_ESPRESSIF_SPIRAM_MEMTEST
if (esp_psram_is_initialized() && !esp_psram_extram_test())
{
PANIC();
}
#endif

/* Disable clock of unused peripherals */

esp_perip_clk_init();
Expand Down Expand Up @@ -613,5 +656,7 @@ void __esp_start(void)

nx_start();

UNUSED(ret);

for (; ; );
}
6 changes: 5 additions & 1 deletion arch/risc-v/src/esp32c3/hal_esp32c3.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,11 @@ list(
list(APPEND HAL_SRCS
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/src/flash_encrypt.c)

# Bootloader common
list(
APPEND HAL_SRCS
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/src/bootloader_mem.c)

# ##############################################################################
# Simple Boot Sources
# ##############################################################################
Expand All @@ -529,7 +534,6 @@ if(CONFIG_ESPRESSIF_SIMPLE_BOOT)
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/bootloader_flash/src/bootloader_flash_config_${CHIP_SERIES}.c
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/bootloader_flash/src/flash_qio_mode.c
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/src/bootloader_clock_init.c
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/src/bootloader_mem.c
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/src/bootloader_random.c
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/src/bootloader_random_${CHIP_SERIES}.c
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/src/esp_image_format.c
Expand Down
2 changes: 1 addition & 1 deletion arch/risc-v/src/esp32c3/hal_esp32c3.mk
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)uppe
# Bootloader files

CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)bootloader_support$(DELIM)src$(DELIM)flash_encrypt.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)bootloader_support$(DELIM)src$(DELIM)bootloader_mem.c

ifeq ($(CONFIG_ESPRESSIF_SIMPLE_BOOT),y)
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)nuttx$(DELIM)src$(DELIM)bootloader_banner_wrap.c
Expand All @@ -364,7 +365,6 @@ ifeq ($(CONFIG_ESPRESSIF_SIMPLE_BOOT),y)
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)bootloader_support$(DELIM)bootloader_flash$(DELIM)src$(DELIM)bootloader_flash_config_${CHIP_SERIES}.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)bootloader_support$(DELIM)bootloader_flash$(DELIM)src$(DELIM)flash_qio_mode.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)bootloader_support$(DELIM)src$(DELIM)bootloader_clock_init.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)bootloader_support$(DELIM)src$(DELIM)bootloader_mem.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)bootloader_support$(DELIM)src$(DELIM)bootloader_random.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)bootloader_support$(DELIM)src$(DELIM)bootloader_random_${CHIP_SERIES}.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)bootloader_support$(DELIM)src$(DELIM)esp_image_format.c
Expand Down
6 changes: 5 additions & 1 deletion arch/risc-v/src/esp32c6/hal_esp32c6.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,11 @@ list(
list(APPEND HAL_SRCS
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/src/flash_encrypt.c)

# Bootloader common
list(
APPEND HAL_SRCS
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/src/bootloader_mem.c)

# ##############################################################################
# Simple Boot
# ##############################################################################
Expand All @@ -555,7 +560,6 @@ if(CONFIG_ESPRESSIF_SIMPLE_BOOT)
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/bootloader_flash/src/bootloader_flash_config_${CHIP_SERIES}.c
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/bootloader_flash/src/flash_qio_mode.c
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/src/bootloader_clock_init.c
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/src/bootloader_mem.c
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/src/bootloader_random.c
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/src/bootloader_random_${CHIP_SERIES}.c
${ESP_HAL_3RDPARTY_REPO}/components/bootloader_support/src/esp_image_format.c
Expand Down
Loading
Loading