Skip to content

Commit 5d970c0

Browse files
committed
Merge branch 'feat/ble_mesh_use_iram_for_mem_alloc' into 'master'
Feat/ble mesh use iram for mem alloc See merge request espressif/esp-idf!9449
2 parents cf056a7 + 076fee8 commit 5d970c0

File tree

10 files changed

+294
-134
lines changed

10 files changed

+294
-134
lines changed

components/bt/esp_ble_mesh/Kconfig.in

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,82 @@ if BLE_MESH
1818
option in the Bluetooth Controller section in menuconfig, which is
1919
"Scan Duplicate By Device Address and Advertising Data".
2020

21-
config BLE_MESH_ALLOC_FROM_PSRAM_FIRST
22-
bool "BLE Mesh will first allocate memory from PSRAM"
21+
choice BLE_MESH_MEM_ALLOC_MODE
22+
prompt "Memory allocation strategy"
23+
default BLE_MESH_MEM_ALLOC_MODE_INTERNAL
24+
help
25+
Allocation strategy for BLE Mesh stack, essentially provides ability to
26+
allocate all required dynamic allocations from,
27+
28+
- Internal DRAM memory only
29+
- External SPIRAM memory only
30+
- Either internal or external memory based on default malloc()
31+
behavior in ESP-IDF
32+
- Internal IRAM memory wherever applicable else internal DRAM
33+
34+
Recommended mode here is always internal, since that is most preferred
35+
from security perspective. But if application requirement does not allow
36+
sufficient free internal memory then alternate mode can be selected.
37+
38+
config BLE_MESH_MEM_ALLOC_MODE_INTERNAL
39+
bool "Internal DRAM"
40+
41+
config BLE_MESH_MEM_ALLOC_MODE_EXTERNAL
42+
bool "External SPIRAM"
43+
depends on ESP32_SPIRAM_SUPPORT
44+
45+
config BLE_MESH_MEM_ALLOC_MODE_DEFAULT
46+
bool "Default alloc mode"
47+
depends on ESP32_SPIRAM_SUPPORT
48+
help
49+
Enable this option to use the default memory allocation strategy when
50+
external SPIRAM is enabled. See the SPIRAM options for more details.
51+
52+
config BLE_MESH_MEM_ALLOC_MODE_IRAM_8BIT
53+
bool "Internal IRAM"
54+
depends on ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
55+
help
56+
Allows to use IRAM memory region as 8bit accessible region. Every
57+
unaligned (8bit or 16bit) access will result in an exception and
58+
incur penalty of certain clock cycles per unaligned read/write.
59+
60+
endchoice # BLE_MESH_MEM_ALLOC_MODE
61+
62+
config BLE_MESH_FREERTOS_STATIC_ALLOC
63+
bool "Enable FreeRTOS static allocation"
64+
depends on FREERTOS_SUPPORT_STATIC_ALLOCATION && (ESP32_SPIRAM_SUPPORT || ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY)
2365
default n
2466
help
25-
When this option is enabled, BLE Mesh stack will try to allocate memory
26-
from PSRAM firstly. This will save the internal RAM if PSRAM exists.
67+
Enable this option to use FreeRTOS static allocation APIs for BLE Mesh,
68+
which provides the ability to use different dynamic memory (i.e. SPIRAM
69+
or IRAM) for FreeRTOS objects.
70+
If this option is disabled, the FreeRTOS static allocation APIs will not
71+
be used, and internal DRAM will be allocated for FreeRTOS objects.
72+
73+
choice BLE_MESH_FREERTOS_STATIC_ALLOC_MODE
74+
prompt "Memory allocation for FreeRTOS objects"
75+
depends on BLE_MESH_FREERTOS_STATIC_ALLOC
76+
help
77+
Choose the memory to be used for FreeRTOS objects.
78+
79+
config BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL
80+
bool "External SPIRAM"
81+
depends on ESP32_SPIRAM_SUPPORT
82+
help
83+
If enabled, BLE Mesh allocates dynamic memory from external SPIRAM for
84+
FreeRTOS objects, i.e. mutex, queue, and task stack. External SPIRAM
85+
can only be used for task stack when SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
86+
is enabled. See the SPIRAM options for more details.
87+
88+
config BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT
89+
bool "Internal IRAM"
90+
depends on ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
91+
help
92+
If enabled, BLE Mesh allocates dynamic memory from internal IRAM for
93+
FreeRTOS objects, i.e. mutex, queue. Note: IRAM region cannot be used
94+
as task stack.
95+
96+
endchoice # BLE_MESH_FREERTOS_STATIC_ALLOC_MODE
2797

2898
config BLE_MESH_FAST_PROV
2999
bool "Enable BLE Mesh Fast Provisioning"

components/bt/esp_ble_mesh/mesh_common/include/mesh_common.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <stddef.h>
2323
#include <stdlib.h>
2424

25+
#include "esp_attr.h"
2526
#include "esp_heap_caps.h"
2627

2728
#include "mesh_byteorder.h"
@@ -34,14 +35,11 @@
3435
extern "C" {
3536
#endif
3637

37-
#if CONFIG_BLE_MESH_ALLOC_FROM_PSRAM_FIRST
38-
#define bt_mesh_malloc(size) heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
39-
#define bt_mesh_calloc(size) heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
40-
#else
41-
#define bt_mesh_malloc(size) malloc((size))
42-
#define bt_mesh_calloc(size) calloc(1, (size))
43-
#endif /* CONFIG_BLE_MESH_ALLOC_FROM_PSRAM_FIRST */
44-
#define bt_mesh_free(p) free((p))
38+
IRAM_ATTR void *bt_mesh_malloc(size_t size);
39+
40+
IRAM_ATTR void *bt_mesh_calloc(size_t size);
41+
42+
IRAM_ATTR void bt_mesh_free(void *ptr);
4543

4644
/**
4745
* @brief This function allocates memory to store outgoing message.

components/bt/esp_ble_mesh/mesh_common/include/mesh_kernel.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "freertos/queue.h"
1414
#include "freertos/semphr.h"
1515

16+
#include "sdkconfig.h"
1617
#include "mesh_types.h"
1718

1819
#ifdef __cplusplus
@@ -36,6 +37,8 @@ extern "C" {
3637
#endif
3738

3839
#define BLE_MESH_ADV_TASK_STACK_SIZE 3072
40+
#define BLE_MESH_ADV_TASK_NAME "mesh_adv_task"
41+
#define BLE_MESH_ADV_TASK_PRIO (configMAX_PRIORITIES - 5)
3942

4043
/**
4144
* @brief Put the current thread to sleep.

components/bt/esp_ble_mesh/mesh_common/include/mesh_mutex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extern "C" {
2525

2626
typedef struct {
2727
SemaphoreHandle_t mutex;
28-
#if CONFIG_SPIRAM_USE_MALLOC
28+
#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
2929
StaticQueue_t *buffer;
3030
#endif
3131
} bt_mesh_mutex_t;

components/bt/esp_ble_mesh/mesh_common/mesh_common.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,37 @@
1919
#include "client_common.h"
2020
#include "mesh_common.h"
2121

22+
IRAM_ATTR void *bt_mesh_malloc(size_t size)
23+
{
24+
#ifdef CONFIG_BLE_MESH_MEM_ALLOC_MODE_INTERNAL
25+
return heap_caps_malloc(size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
26+
#elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_EXTERNAL
27+
return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
28+
#elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_IRAM_8BIT
29+
return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
30+
#else
31+
return malloc(size);
32+
#endif
33+
}
34+
35+
IRAM_ATTR void *bt_mesh_calloc(size_t size)
36+
{
37+
#ifdef CONFIG_BLE_MESH_MEM_ALLOC_MODE_INTERNAL
38+
return heap_caps_calloc(1, size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
39+
#elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_EXTERNAL
40+
return heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
41+
#elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_IRAM_8BIT
42+
return heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
43+
#else
44+
return calloc(1, size);
45+
#endif
46+
}
47+
48+
IRAM_ATTR void bt_mesh_free(void *ptr)
49+
{
50+
heap_caps_free(ptr);
51+
}
52+
2253
struct net_buf_simple *bt_mesh_alloc_buf(u16_t size)
2354
{
2455
struct net_buf_simple *buf = NULL;

components/bt/esp_ble_mesh/mesh_common/mesh_mutex.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@ void bt_mesh_mutex_create(bt_mesh_mutex_t *mutex)
2626
return;
2727
}
2828

29-
#if CONFIG_SPIRAM_USE_MALLOC
30-
mutex->buffer = heap_caps_calloc(1, sizeof(StaticQueue_t), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM);
31-
__ASSERT(mutex->buffer, "%s, Failed to create queue buffer", __func__);
29+
#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
30+
#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL
31+
mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
32+
#elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT
33+
mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
34+
#endif
35+
__ASSERT(mutex->buffer, "Failed to create mutex buffer");
3236
mutex->mutex = xSemaphoreCreateMutexStatic(mutex->buffer);
33-
__ASSERT(mutex->mutex, "%s, Failed to create static mutex", __func__);
34-
#else
37+
__ASSERT(mutex->mutex, "Failed to create static mutex");
38+
#else /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
3539
mutex->mutex = xSemaphoreCreateMutex();
36-
__ASSERT(mutex->mutex, "%s, Failed to create mutex", __func__);
37-
#endif
40+
__ASSERT(mutex->mutex, "Failed to create mutex");
41+
#endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
3842
}
3943

4044
void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex)
@@ -47,7 +51,7 @@ void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex)
4751
if (mutex->mutex) {
4852
vSemaphoreDelete(mutex->mutex);
4953
mutex->mutex = NULL;
50-
#if CONFIG_SPIRAM_USE_MALLOC
54+
#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
5155
heap_caps_free(mutex->buffer);
5256
mutex->buffer = NULL;
5357
#endif

0 commit comments

Comments
 (0)