From 545d0ab2fde631b5aaafccd05ef0b6bd931cbf91 Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Fri, 30 May 2025 09:08:23 +0200 Subject: [PATCH] cpu/esp_common/syscalls: enable multiheap for SPI RAM For ESP8266, the `heap_caps_*_default` functions are simply mapped to the corresponding `heap_caps_*` functions because SPI RAM is not supported. But for ESP32x, where SPI RAM might be enabled, the implementation of the `heap_caps_*_default` functions of the ESP-IDF must be used, as these try to allocate memory blocks smaller than `CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL` from the internal memory first. This is important for some control data structures that must be located in the internal memory, such as the NVS partition table, in order to avoid access conflicts between SPI RAM and SPI flash memory. --- cpu/esp_common/syscalls.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/cpu/esp_common/syscalls.c b/cpu/esp_common/syscalls.c index 34cff8259d..a1a5cea848 100644 --- a/cpu/esp_common/syscalls.c +++ b/cpu/esp_common/syscalls.c @@ -33,9 +33,16 @@ #include "syscalls.h" #ifdef MODULE_ESP_IDF_HEAP -#include "esp_heap_caps.h" +# include "esp_heap_caps.h" + +# ifndef CPU_ESP8266 /* for all ESP32x SoCs */ +# define MULTI_HEAP_FREERTOS /* allow multi-heap */ +# include "heap/multi_heap_platform.h" /* use multi-heap and */ +# include "heap/heap_private.h" /* use the `heaps_cap_*_default` functions */ +# endif + #else -#include "malloc.h" +# include "malloc.h" #endif #define ENABLE_DEBUG 0 @@ -328,8 +335,19 @@ void IRAM_ATTR _lock_release_recursive(_lock_t *lock) #ifdef MODULE_ESP_IDF_HEAP -#define heap_caps_malloc_default(s) heap_caps_malloc(s, MALLOC_CAP_DEFAULT) -#define heap_caps_realloc_default(p, s) heap_caps_realloc(p, s, MALLOC_CAP_DEFAULT) +/* For ESP8266, the `heap_caps_*_default` functions are simply mapped to the + * corresponding `heap_caps_*` functions because SPI RAM is not supported. + * But for ESP32x, where SPI RAM might be enabled, the implementation of the + * `heap_caps_*_default` functions of the ESP-IDF must be used, as these try + * to allocate memory blocks smaller than `CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL` + * from the internal memory first. This is important for some control data + * structures that must be located in the internal memory, such as the NVS + * partition table, in order to avoid access conflicts between SPI RAM and + * SPI flash memory. */ +# ifdef CPU_ESP8266 +# define heap_caps_malloc_default(s) heap_caps_malloc(s, MALLOC_CAP_DEFAULT) +# define heap_caps_realloc_default(p, s) heap_caps_realloc(p, s, MALLOC_CAP_DEFAULT) +# endif void* IRAM_ATTR __wrap__malloc_r(struct _reent *r, size_t size) {