Skip to content

Commit 49570fa

Browse files
committed
soc: esp32: unify runtime heap usage
This commit applies several changes in the way "heap_runtime" feature is used. It can't be split due to bisectability issues. Whenever the feature is enabled, a new heap is created and custom malloc/calloc/free functions are added into the build system. Those functions are currently used for internal Wi-Fi and BLE drivers only. Such changes are described below: 1) Rename heap.c to esp_heap_runtime.c for better readability. 2) Rename RUNTIME_HEAP to HEAP_RUNTIME to make it similar to what is available in Zephyr. 3) Add runtime heap to BT as such as Wi-Fi. Fixes #79490 Fixes #79470 Signed-off-by: Sylvio Alves <[email protected]>
1 parent 7656632 commit 49570fa

File tree

10 files changed

+151
-55
lines changed

10 files changed

+151
-55
lines changed

drivers/bluetooth/hci/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ config BT_AIROC
202202
Infineon's AIROC™ Wi-Fi & combos portfolio integrates
203203
IEEE 802.11a/b/g/n/ac/ax Wi-Fi and Bluetooth® 5.2 in a single-chip
204204
solution to enable small-form-factor IoT designs.
205+
source "drivers/bluetooth/hci/Kconfig.esp32"
205206
source "drivers/bluetooth/hci/Kconfig.infineon"
206207
source "drivers/bluetooth/hci/Kconfig.nxp"
207208

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
if BT_ESP32
5+
6+
choice ESP_BT_HEAP
7+
prompt "Bluetooth adapter heap in use"
8+
default ESP_BT_HEAP_RUNTIME
9+
10+
config ESP_BT_HEAP_RUNTIME
11+
bool "Bluetooth adapter use ESP runtime heap"
12+
depends on ESP_HEAP_RUNTIME
13+
14+
config ESP_BT_HEAP_SYSTEM
15+
bool "Bluetooth adapter use system heap"
16+
17+
endchoice # ESP_BT_HEAP
18+
19+
endif

drivers/wifi/esp32/Kconfig.esp32

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ menuconfig WIFI_ESP32
2020

2121
if WIFI_ESP32
2222

23+
config HEAP_MEM_POOL_ADD_SIZE_WIFI
24+
int
25+
default 4096
26+
help
27+
Make sure there is a minimal heap available for Wi-Fi driver.
28+
2329
config NET_TCP_WORKQ_STACK_SIZE
2430
default 2048
2531

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
if(CONFIG_SOC_SERIES_ESP32 OR CONFIG_SOC_SERIES_ESP32S2 OR CONFIG_SOC_SERIES_ESP32S3)
5-
zephyr_include_directories(include)
6-
endif()
4+
zephyr_include_directories(include)
75

86
if(NOT CONFIG_MCUBOOT AND NOT CONFIG_SOC_ESP32_APPCPU AND NOT CONFIG_SOC_ESP32S3_APPCPU)
97
zephyr_sources_ifdef(CONFIG_ESP_SPIRAM psram.c)
10-
zephyr_sources_ifdef(CONFIG_ESP_RUNTIME_HEAP heap.c)
8+
zephyr_sources_ifdef(CONFIG_ESP_HEAP_RUNTIME esp_heap_runtime.c)
119
endif()

soc/espressif/common/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ config ESP_SIMPLE_BOOT
2424
Please note that this method brings the system up with all memories set-up, but
2525
all other features, such as secure boot OTA or slots management are not available.
2626

27-
config ESP_RUNTIME_HEAP
27+
config ESP_HEAP_RUNTIME
2828
bool
2929
default y
3030
help

soc/espressif/common/Kconfig.wifi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ choice ESP_WIFI_HEAP
3535

3636
config ESP_WIFI_HEAP_RUNTIME
3737
bool "Wifi adapter use ESP runtime heap"
38-
depends on ESP_RUNTIME_HEAP
38+
depends on ESP_HEAP_RUNTIME
3939

4040
config ESP_WIFI_HEAP_SPIRAM
4141
bool "Wifi adapter use SPIRAM heap"
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stddef.h>
8+
#include <stdint.h>
9+
#include <string.h>
10+
#include <zephyr/types.h>
11+
#include <zephyr/kernel.h>
12+
#include <soc.h>
13+
#include <esp_err.h>
14+
#include <esp_heap_runtime.h>
15+
#include "esp_log.h"
16+
17+
#define TAG "heap_runtime"
18+
19+
/* ESP dynamic pool heap */
20+
extern unsigned int z_mapped_end;
21+
extern unsigned int _heap_sentry;
22+
static void *esp_heap_runtime_init_mem = &z_mapped_end;
23+
24+
#define ESP_HEAP_RUNTIME_MAX_SIZE ((uintptr_t)&_heap_sentry - (uintptr_t)&z_mapped_end)
25+
26+
static struct k_heap esp_heap_runtime;
27+
28+
static int esp_heap_runtime_init(void)
29+
{
30+
ESP_EARLY_LOGI(TAG, "ESP heap runtime init at 0x%x size %d kB.\n",
31+
esp_heap_runtime_init_mem, ESP_HEAP_RUNTIME_MAX_SIZE / 1024);
32+
33+
k_heap_init(&esp_heap_runtime, esp_heap_runtime_init_mem, ESP_HEAP_RUNTIME_MAX_SIZE);
34+
35+
#if defined(CONFIG_WIFI_ESP32) && defined(CONFIG_BT_ESP32)
36+
assert(ESP_HEAP_RUNTIME_MAX_SIZE > 65535);
37+
#elif defined(CONFIG_WIFI_ESP32)
38+
assert(ESP_HEAP_RUNTIME_MAX_SIZE > 51200);
39+
#elif defined(CONFIG_BT_ESP32)
40+
assert(ESP_HEAP_RUNTIME_MAX_SIZE > 40960);
41+
#endif
42+
43+
return 0;
44+
}
45+
46+
void *esp_heap_runtime_malloc(size_t size)
47+
{
48+
return k_heap_alloc(&esp_heap_runtime, size, K_NO_WAIT);
49+
}
50+
51+
void *esp_heap_runtime_calloc(size_t n, size_t size)
52+
{
53+
size_t sz;
54+
55+
if (__builtin_mul_overflow(n, size, &sz)) {
56+
return NULL;
57+
}
58+
void *ptr = k_heap_alloc(&esp_heap_runtime, sz, K_NO_WAIT);
59+
60+
if (ptr) {
61+
memset(ptr, 0, sz);
62+
}
63+
64+
return ptr;
65+
}
66+
67+
void *esp_heap_runtime_realloc(void *ptr, size_t bytes)
68+
{
69+
return k_heap_realloc(&esp_heap_runtime, ptr, bytes, K_NO_WAIT);
70+
}
71+
72+
void esp_heap_runtime_free(void *mem)
73+
{
74+
k_heap_free(&esp_heap_runtime, mem);
75+
}
76+
77+
SYS_INIT(esp_heap_runtime_init, PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);

soc/espressif/common/heap.c

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @brief Allocate memory from the esp_heap_runtime.
9+
*
10+
* @param size Amount of memory requested (in bytes).
11+
*
12+
* @return Address of the allocated memory if successful; otherwise NULL.
13+
*/
14+
void *esp_heap_runtime_malloc(size_t size);
15+
16+
/**
17+
* @brief Allocate memory from esp_heap_runtime, array style
18+
*
19+
* @param n Number of elements in the requested array
20+
* @param size Size of each array element (in bytes).
21+
*
22+
* @return Address of the allocated memory if successful; otherwise NULL.
23+
*/
24+
void *esp_heap_runtime_calloc(size_t n, size_t size);
25+
26+
/**
27+
* @brief Reallocate memory from a esp_heap_runtime
28+
*
29+
* @param ptr Original pointer returned from a previous allocation
30+
* @param bytes Desired size of block to allocate
31+
*
32+
* @return Pointer to memory the caller can now use, or NULL
33+
*/
34+
void *esp_heap_runtime_realloc(void *ptr, size_t bytes);
35+
36+
/**
37+
* @brief Free memory allocated from esp_heap_runtime.
38+
*
39+
* If @a ptr is NULL, no operation is performed.
40+
*
41+
* @param ptr Pointer to previously allocated memory.
42+
*/
43+
void esp_heap_runtime_free(void *mem);

west.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ manifest:
157157
groups:
158158
- hal
159159
- name: hal_espressif
160-
revision: 61a002ad757f567cdef92014b483e6f325c41afc
160+
revision: 5e7220b429caa5d374286b3a3122c5d303c5f78a
161161
path: modules/hal/espressif
162162
west-commands: west/west-commands.yml
163163
groups:

0 commit comments

Comments
 (0)