soc: espressif: Remove ESP heap and use heap adapter

Remove ESP heap from the sources. System heap is default heap.
Use heap adapter layer to configure used heap.
Use MEM_POOL memory request config to Wi-Fi and Bluetooth drivers.
Update the Wi-Fi and BLE memory needs.

Signed-off-by: Marek Matej <marek.matej@espressif.com>
This commit is contained in:
Marek Matej 2025-01-29 14:25:09 +01:00 committed by Benjamin Cabé
commit 6e6ab2f8ab
15 changed files with 45 additions and 162 deletions

View file

@ -4,6 +4,5 @@
zephyr_include_directories(include)
if(NOT CONFIG_MCUBOOT AND NOT CONFIG_SOC_ESP32_APPCPU AND NOT CONFIG_SOC_ESP32S3_APPCPU)
zephyr_sources_ifdef(CONFIG_ESP_HEAP_RUNTIME esp_heap_runtime.c)
zephyr_sources_ifdef(CONFIG_ESP_SPIRAM esp_psram.c)
endif()

View file

@ -24,14 +24,6 @@ config ESP_SIMPLE_BOOT
Please note that this method brings the system up with all memories set-up, but
all other features, such as secure boot OTA or slots management are not available.
config ESP_HEAP_RUNTIME
bool
default y
help
Enabling this will allocate SRAM area starting from the last linked data at the symbolic `_end`,
ending at the last memory location that can be safely accessed (depending on a boot mode).
This is a memory pool used in runtime to create a new heap memory.
config ESP32_TIMER_TASK_STACK_SIZE
int "Stack size of the high resolution ESP Timer"
default 4096

View file

@ -1,77 +0,0 @@
/*
* Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <zephyr/types.h>
#include <zephyr/kernel.h>
#include <soc.h>
#include <esp_err.h>
#include <esp_heap_runtime.h>
#include "esp_log.h"
#define TAG "heap_runtime"
/* ESP dynamic pool heap */
extern unsigned int z_mapped_end;
extern unsigned int _heap_sentry;
static void *esp_heap_runtime_init_mem = &z_mapped_end;
#define ESP_HEAP_RUNTIME_MAX_SIZE ((uintptr_t)&_heap_sentry - (uintptr_t)&z_mapped_end)
static struct k_heap esp_heap_runtime;
static int esp_heap_runtime_init(void)
{
ESP_EARLY_LOGI(TAG, "ESP heap runtime init at 0x%x size %d kB.\n",
esp_heap_runtime_init_mem, ESP_HEAP_RUNTIME_MAX_SIZE / 1024);
k_heap_init(&esp_heap_runtime, esp_heap_runtime_init_mem, ESP_HEAP_RUNTIME_MAX_SIZE);
#if defined(CONFIG_WIFI_ESP32) && defined(CONFIG_BT_ESP32)
assert(ESP_HEAP_RUNTIME_MAX_SIZE > 65535);
#elif defined(CONFIG_WIFI_ESP32)
assert(ESP_HEAP_RUNTIME_MAX_SIZE > 51200);
#elif defined(CONFIG_BT_ESP32)
assert(ESP_HEAP_RUNTIME_MAX_SIZE > 40960);
#endif
return 0;
}
void *esp_heap_runtime_malloc(size_t size)
{
return k_heap_alloc(&esp_heap_runtime, size, K_NO_WAIT);
}
void *esp_heap_runtime_calloc(size_t n, size_t size)
{
size_t sz;
if (__builtin_mul_overflow(n, size, &sz)) {
return NULL;
}
void *ptr = k_heap_alloc(&esp_heap_runtime, sz, K_NO_WAIT);
if (ptr) {
memset(ptr, 0, sz);
}
return ptr;
}
void *esp_heap_runtime_realloc(void *ptr, size_t bytes)
{
return k_heap_realloc(&esp_heap_runtime, ptr, bytes, K_NO_WAIT);
}
void esp_heap_runtime_free(void *mem)
{
k_heap_free(&esp_heap_runtime, mem);
}
SYS_INIT(esp_heap_runtime_init, PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);

View file

@ -1,43 +0,0 @@
/*
* Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd.
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @brief Allocate memory from the esp_heap_runtime.
*
* @param size Amount of memory requested (in bytes).
*
* @return Address of the allocated memory if successful; otherwise NULL.
*/
void *esp_heap_runtime_malloc(size_t size);
/**
* @brief Allocate memory from esp_heap_runtime, array style
*
* @param n Number of elements in the requested array
* @param size Size of each array element (in bytes).
*
* @return Address of the allocated memory if successful; otherwise NULL.
*/
void *esp_heap_runtime_calloc(size_t n, size_t size);
/**
* @brief Reallocate memory from a esp_heap_runtime
*
* @param ptr Original pointer returned from a previous allocation
* @param bytes Desired size of block to allocate
*
* @return Pointer to memory the caller can now use, or NULL
*/
void *esp_heap_runtime_realloc(void *ptr, size_t bytes);
/**
* @brief Free memory allocated from esp_heap_runtime.
*
* If @a ptr is NULL, no operation is performed.
*
* @param ptr Pointer to previously allocated memory.
*/
void esp_heap_runtime_free(void *mem);

View file

@ -74,6 +74,7 @@ static HDR_ATTR void (*_entry_point)(void) = &__start;
esp_image_header_t WORD_ALIGNED_ATTR bootloader_image_hdr;
extern uint32_t _image_irom_start, _image_irom_size, _image_irom_vaddr;
extern uint32_t _image_drom_start, _image_drom_size, _image_drom_vaddr;
extern uint32_t _libc_heap_size;
#ifndef CONFIG_MCUBOOT
static uint32_t _app_irom_start =
@ -84,6 +85,7 @@ static uint32_t _app_drom_start =
(FIXED_PARTITION_OFFSET(slot0_partition) + (uint32_t)&_image_drom_start);
static uint32_t _app_drom_size = (uint32_t)&_image_drom_size;
static uint32_t libc_heap_size = (uint32_t)&_libc_heap_size;
#endif
static uint32_t _app_irom_vaddr = ((uint32_t)&_image_irom_vaddr);
@ -260,7 +262,7 @@ void __start(void)
".option norelax\n"
"la gp, __global_pointer$\n"
".option pop");
#endif /* CONFIG_RISCV_GP */
#endif
#ifndef CONFIG_BOOTLOADER_MCUBOOT
/* Init fundamental components */
@ -278,12 +280,14 @@ void __start(void)
#ifndef CONFIG_SOC_SERIES_ESP32C2
/* Disable RNG entropy source as it was already used */
soc_random_disable();
#endif /* CONFIG_SOC_SERIES_ESP32C2 */
#endif
#if defined(CONFIG_SOC_SERIES_ESP32S3) || defined(CONFIG_SOC_SERIES_ESP32C3)
/* Disable glitch detection as it can be falsely triggered by EMI interference */
ESP_EARLY_LOGI(TAG, "Disabling glitch detection");
ana_clock_glitch_reset_config(false);
#endif /* CONFIG_SOC_SERIES_ESP32S2 */
ESP_EARLY_LOGI(TAG, "Jumping to the main image...");
#endif
#if !defined(CONFIG_MCUBOOT)
ESP_EARLY_LOGI(TAG, "libc heap size %d kB.", libc_heap_size / 1024);
#endif
__esp_platform_start();
}

View file

@ -100,7 +100,9 @@ _rom_store_table = 0;
PROVIDE(_memmap_vecbase_reset = 0x40000450);
PROVIDE(_memmap_reset_vector = 0x40000400);
/* Heap size calculations for PROCPU is also valid for AMP scenario */
_heap_sentry = SRAM2_DRAM_END;
_libc_heap_size = _heap_sentry - _end;
SECTIONS
{

View file

@ -72,7 +72,9 @@ _rom_store_table = 0;
PROVIDE(_memmap_vecbase_reset = 0x40000450);
PROVIDE(_memmap_reset_vector = 0x40000400);
/* Heap size calculations for APPCPU */
_heap_sentry = BOOTLOADER_DRAM_SEG_START + APPCPU_DRAM_SIZE;
_libc_heap_size = _heap_sentry - _end;
SECTIONS
{

View file

@ -85,8 +85,9 @@ _rom_store_table = 0;
_iram_dram_offset = IRAM_DRAM_OFFSET;
/* Used as a pointer to the heap end */
/* Heap size calculations */
_heap_sentry = DRAM_RESERVED_START;
_libc_heap_size = _heap_sentry - _end;
SECTIONS
{

View file

@ -98,8 +98,9 @@ _rom_store_table = 0;
_iram_dram_offset = IRAM_DRAM_OFFSET;
/* Stack sentry */
/* Heap size calculations */
_heap_sentry = DRAM_RESERVED_START;
_libc_heap_size = _heap_sentry - _end;
SECTIONS
{

View file

@ -100,7 +100,9 @@ REGION_ALIAS("rtc_data_location", rtc_iram_seg );
/* Default entry point: */
ENTRY(CONFIG_KERNEL_ENTRY)
/* Heap size calculations */
_heap_sentry = DRAM_RESERVED_START;
_libc_heap_size = _heap_sentry - _end;
SECTIONS
{

View file

@ -105,6 +105,7 @@ _rom_store_table = 0;
/* Used as a pointer to the heap end */
_heap_sentry = DRAM_RESERVED_START;
_libc_heap_size = _heap_sentry - _end;
SECTIONS
{

View file

@ -102,13 +102,15 @@ MEMORY
/* Default entry point: */
ENTRY(CONFIG_KERNEL_ENTRY)
/* Used as a pointer to the heap end */
/* Heap size calculations differs between the APPCPU and PROCPU */
#ifdef CONFIG_SOC_ENABLE_APPCPU
_heap_sentry = procpu_dram_end;
#else
_heap_sentry = DRAM_RESERVED_START;
#endif
_libc_heap_size = _heap_sentry - _end;
SECTIONS
{
_iram_dram_offset = IRAM_DRAM_OFFSET;

View file

@ -66,8 +66,9 @@ MEMORY
/* Default entry point: */
ENTRY(__appcpu_start)
/* Used as a pointer to the heap end */
/* Heap size calculations for APPCPU */
_heap_sentry = DRAM_RESERVED_START;
_libc_heap_size = _heap_sentry - _end;
SECTIONS
{