ble_mesh: stack: Make freertos static allocation visible

This commit is contained in:
lly 2020-07-03 16:33:36 +08:00
parent c0db196464
commit 6abfc2a8f7
4 changed files with 106 additions and 48 deletions

View file

@ -59,6 +59,42 @@ if BLE_MESH
endchoice # BLE_MESH_MEM_ALLOC_MODE endchoice # BLE_MESH_MEM_ALLOC_MODE
config BLE_MESH_FREERTOS_STATIC_ALLOC
bool "Enable FreeRTOS static allocation"
depends on FREERTOS_SUPPORT_STATIC_ALLOCATION && (ESP32_SPIRAM_SUPPORT || ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY)
default n
help
Enable this option to use FreeRTOS static allocation APIs for BLE Mesh,
which provides the ability to use different dynamic memory (i.e. SPIRAM
or IRAM) for FreeRTOS objects.
If this option is disabled, the FreeRTOS static allocation APIs will not
be used, and internal DRAM will be allocated for FreeRTOS objects.
choice BLE_MESH_FREERTOS_STATIC_ALLOC_MODE
prompt "Memory allocation for FreeRTOS objects"
depends on BLE_MESH_FREERTOS_STATIC_ALLOC
help
Choose the memory to be used for FreeRTOS objects.
config BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL
bool "External SPIRAM"
depends on ESP32_SPIRAM_SUPPORT
help
If enabled, BLE Mesh allocates dynamic memory from external SPIRAM for
FreeRTOS objects, i.e. mutex, queue, and task stack. External SPIRAM
can only be used for task stack when SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
is enabled. See the SPIRAM options for more details.
config BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT
bool "Internal IRAM"
depends on ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
help
If enabled, BLE Mesh allocates dynamic memory from internal IRAM for
FreeRTOS objects, i.e. mutex, queue. Note: IRAM region cannot be used
as task stack.
endchoice # BLE_MESH_FREERTOS_STATIC_ALLOC_MODE
config BLE_MESH_FAST_PROV config BLE_MESH_FAST_PROV
bool "Enable BLE Mesh Fast Provisioning" bool "Enable BLE Mesh Fast Provisioning"
select BLE_MESH_NODE select BLE_MESH_NODE

View file

@ -25,7 +25,7 @@ extern "C" {
typedef struct { typedef struct {
SemaphoreHandle_t mutex; SemaphoreHandle_t mutex;
#if CONFIG_SPIRAM_USE_MALLOC #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
StaticQueue_t *buffer; StaticQueue_t *buffer;
#endif #endif
} bt_mesh_mutex_t; } bt_mesh_mutex_t;

View file

@ -26,15 +26,19 @@ void bt_mesh_mutex_create(bt_mesh_mutex_t *mutex)
return; return;
} }
#if CONFIG_SPIRAM_USE_MALLOC #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
mutex->buffer = heap_caps_calloc(1, sizeof(StaticQueue_t), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL
__ASSERT(mutex->buffer, "%s, Failed to create queue buffer", __func__); mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
mutex->mutex = xSemaphoreCreateMutexStatic(mutex->buffer); #elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT
__ASSERT(mutex->mutex, "%s, Failed to create static mutex", __func__); mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#else
mutex->mutex = xSemaphoreCreateMutex();
__ASSERT(mutex->mutex, "%s, Failed to create mutex", __func__);
#endif #endif
__ASSERT(mutex->buffer, "Failed to create mutex buffer");
mutex->mutex = xSemaphoreCreateMutexStatic(mutex->buffer);
__ASSERT(mutex->mutex, "Failed to create static mutex");
#else /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
mutex->mutex = xSemaphoreCreateMutex();
__ASSERT(mutex->mutex, "Failed to create mutex");
#endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
} }
void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex) void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex)
@ -47,7 +51,7 @@ void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex)
if (mutex->mutex) { if (mutex->mutex) {
vSemaphoreDelete(mutex->mutex); vSemaphoreDelete(mutex->mutex);
mutex->mutex = NULL; mutex->mutex = NULL;
#if CONFIG_SPIRAM_USE_MALLOC #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
heap_caps_free(mutex->buffer); heap_caps_free(mutex->buffer);
mutex->buffer = NULL; mutex->buffer = NULL;
#endif #endif

View file

@ -63,7 +63,7 @@ static struct bt_mesh_adv adv_pool[CONFIG_BLE_MESH_ADV_BUF_COUNT];
struct bt_mesh_queue { struct bt_mesh_queue {
QueueHandle_t queue; QueueHandle_t queue;
#if CONFIG_SPIRAM_USE_MALLOC #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
StaticQueue_t *buffer; StaticQueue_t *buffer;
u8_t *storage; u8_t *storage;
#endif #endif
@ -121,7 +121,9 @@ static void bt_mesh_ble_adv_deinit(void);
struct bt_mesh_adv_task { struct bt_mesh_adv_task {
TaskHandle_t handle; TaskHandle_t handle;
#if CONFIG_SPIRAM_USE_MALLOC #if (CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL && \
CONFIG_SPIRAM_CACHE_WORKAROUND && \
CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY)
StaticTask_t *task; StaticTask_t *task;
StackType_t *stack; StackType_t *stack;
#endif #endif
@ -795,54 +797,68 @@ static void bt_mesh_scan_cb(const bt_mesh_addr_t *addr, s8_t rssi,
void bt_mesh_adv_init(void) void bt_mesh_adv_init(void)
{ {
#if !CONFIG_SPIRAM_USE_MALLOC #if !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
xBleMeshQueue.queue = xQueueCreate(BLE_MESH_QUEUE_SIZE, sizeof(bt_mesh_msg_t)); xBleMeshQueue.queue = xQueueCreate(BLE_MESH_QUEUE_SIZE, sizeof(bt_mesh_msg_t));
__ASSERT(xBleMeshQueue.queue, "%s, Failed to create queue", __func__); __ASSERT(xBleMeshQueue.queue, "Failed to create queue");
#else #else /* !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
xBleMeshQueue.buffer = heap_caps_calloc(1, sizeof(StaticQueue_t), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL
__ASSERT(xBleMeshQueue.buffer, "%s, Failed to create queue buffer", __func__); xBleMeshQueue.buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
xBleMeshQueue.storage = heap_caps_calloc(1, (BLE_MESH_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); #elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT
__ASSERT(xBleMeshQueue.storage, "%s, Failed to create queue storage", __func__); xBleMeshQueue.buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
xBleMeshQueue.queue = xQueueCreateStatic(BLE_MESH_QUEUE_SIZE, sizeof(bt_mesh_msg_t), (uint8_t*)xBleMeshQueue.storage, xBleMeshQueue.buffer);
__ASSERT(xBleMeshQueue.queue, "%s, Failed to create static queue", __func__);
#endif #endif
__ASSERT(xBleMeshQueue.buffer, "Failed to create queue buffer");
#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL
xBleMeshQueue.storage = heap_caps_calloc_prefer(1, (BLE_MESH_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT
xBleMeshQueue.storage = heap_caps_calloc_prefer(1, (BLE_MESH_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#endif
__ASSERT(xBleMeshQueue.storage, "Failed to create queue storage");
xBleMeshQueue.queue = xQueueCreateStatic(BLE_MESH_QUEUE_SIZE, sizeof(bt_mesh_msg_t), (uint8_t*)xBleMeshQueue.storage, xBleMeshQueue.buffer);
__ASSERT(xBleMeshQueue.queue, "Failed to create static queue");
#endif /* !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
#if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) #if defined(CONFIG_BLE_MESH_RELAY_ADV_BUF)
#if !CONFIG_SPIRAM_USE_MALLOC #if !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
xBleMeshRelayQueue.queue = xQueueCreate(BLE_MESH_RELAY_QUEUE_SIZE, sizeof(bt_mesh_msg_t)); xBleMeshRelayQueue.queue = xQueueCreate(BLE_MESH_RELAY_QUEUE_SIZE, sizeof(bt_mesh_msg_t));
__ASSERT(xBleMeshRelayQueue.queue, "%s, Failed to create relay queue", __func__); __ASSERT(xBleMeshRelayQueue.queue, "Failed to create relay queue");
#else #else /* !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
xBleMeshRelayQueue.buffer = heap_caps_calloc(1, sizeof(StaticQueue_t), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL
__ASSERT(xBleMeshRelayQueue.buffer, "%s, Failed to create relay queue buffer", __func__); xBleMeshRelayQueue.buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
xBleMeshRelayQueue.storage = heap_caps_calloc(1, (BLE_MESH_RELAY_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); #elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT
__ASSERT(xBleMeshRelayQueue.storage, "%s, Failed to create relay queue storage", __func__); xBleMeshRelayQueue.buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
xBleMeshRelayQueue.queue = xQueueCreateStatic(BLE_MESH_RELAY_QUEUE_SIZE, sizeof(bt_mesh_msg_t), (uint8_t*)xBleMeshRelayQueue.storage, xBleMeshRelayQueue.buffer);
__ASSERT(xBleMeshRelayQueue.queue, "%s, Failed to create static relay queue", __func__);
#endif #endif
__ASSERT(xBleMeshRelayQueue.buffer, "Failed to create relay queue buffer");
#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL
xBleMeshRelayQueue.storage = heap_caps_calloc_prefer(1, (BLE_MESH_RELAY_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT
xBleMeshRelayQueue.storage = heap_caps_calloc_prefer(1, (BLE_MESH_RELAY_QUEUE_SIZE * sizeof(bt_mesh_msg_t)), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#endif
__ASSERT(xBleMeshRelayQueue.storage, "Failed to create relay queue storage");
xBleMeshRelayQueue.queue = xQueueCreateStatic(BLE_MESH_RELAY_QUEUE_SIZE, sizeof(bt_mesh_msg_t), (uint8_t*)xBleMeshRelayQueue.storage, xBleMeshRelayQueue.buffer);
__ASSERT(xBleMeshRelayQueue.queue, "Failed to create static relay queue");
#endif /* !CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
xBleMeshQueueSet = xQueueCreateSet(BLE_MESH_QUEUE_SET_SIZE); xBleMeshQueueSet = xQueueCreateSet(BLE_MESH_QUEUE_SET_SIZE);
__ASSERT(xBleMeshQueueSet, "%s, Failed to create queue set", __func__); __ASSERT(xBleMeshQueueSet, "Failed to create queue set");
xQueueAddToSet(xBleMeshQueue.queue, xBleMeshQueueSet); xQueueAddToSet(xBleMeshQueue.queue, xBleMeshQueueSet);
xQueueAddToSet(xBleMeshRelayQueue.queue, xBleMeshQueueSet); xQueueAddToSet(xBleMeshRelayQueue.queue, xBleMeshQueueSet);
#endif /* defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */ #endif /* defined(CONFIG_BLE_MESH_RELAY_ADV_BUF) */
#if !CONFIG_SPIRAM_USE_MALLOC #if (CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL && \
int ret = xTaskCreatePinnedToCore(adv_thread, "BLE_Mesh_ADV_Task", BLE_MESH_ADV_TASK_STACK_SIZE, NULL, CONFIG_SPIRAM_CACHE_WORKAROUND && \
configMAX_PRIORITIES - 5, &adv_task.handle, BLE_MESH_ADV_TASK_CORE); CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY)
__ASSERT(ret == pdTRUE, "%s, Failed to create adv thread", __func__);
#else
adv_task.task = heap_caps_calloc(1, sizeof(StaticTask_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); adv_task.task = heap_caps_calloc(1, sizeof(StaticTask_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
__ASSERT(adv_task.task, "%s, Failed to create adv thread task", __func__); __ASSERT(adv_task.task, "Failed to create adv thread task");
#if CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY adv_task.stack = heap_caps_calloc_prefer(1, BLE_MESH_ADV_TASK_STACK_SIZE * sizeof(StackType_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
adv_task.stack = heap_caps_calloc(1, BLE_MESH_ADV_TASK_STACK_SIZE * sizeof(StackType_t), MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM); __ASSERT(adv_task.stack, "Failed to create adv thread stack");
#else
adv_task.stack = heap_caps_calloc(1, BLE_MESH_ADV_TASK_STACK_SIZE * sizeof(StackType_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#endif
__ASSERT(adv_task.stack, "%s, Failed to create adv thread stack", __func__);
adv_task.handle = xTaskCreateStaticPinnedToCore(adv_thread, "BLE_Mesh_ADV_Task", BLE_MESH_ADV_TASK_STACK_SIZE, NULL, adv_task.handle = xTaskCreateStaticPinnedToCore(adv_thread, "BLE_Mesh_ADV_Task", BLE_MESH_ADV_TASK_STACK_SIZE, NULL,
configMAX_PRIORITIES - 5, adv_task.stack, adv_task.task, BLE_MESH_ADV_TASK_CORE); configMAX_PRIORITIES - 5, adv_task.stack, adv_task.task, BLE_MESH_ADV_TASK_CORE);
__ASSERT(adv_task.handle, "%s, Failed to create static adv thread", __func__); __ASSERT(adv_task.handle, "Failed to create static adv thread");
#endif #else /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL && CONFIG_SPIRAM_CACHE_WORKAROUND && CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY */
int ret = xTaskCreatePinnedToCore(adv_thread, "BLE_Mesh_ADV_Task", BLE_MESH_ADV_TASK_STACK_SIZE, NULL,
configMAX_PRIORITIES - 5, &adv_task.handle, BLE_MESH_ADV_TASK_CORE);
__ASSERT(ret == pdTRUE, "Failed to create adv thread");
#endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL && CONFIG_SPIRAM_CACHE_WORKAROUND && CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY */
} }
void bt_mesh_adv_deinit(void) void bt_mesh_adv_deinit(void)
@ -853,7 +869,9 @@ void bt_mesh_adv_deinit(void)
vTaskDelete(adv_task.handle); vTaskDelete(adv_task.handle);
adv_task.handle = NULL; adv_task.handle = NULL;
#if CONFIG_SPIRAM_USE_MALLOC #if (CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL && \
CONFIG_SPIRAM_CACHE_WORKAROUND && \
CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY)
heap_caps_free(adv_task.stack); heap_caps_free(adv_task.stack);
adv_task.stack = NULL; adv_task.stack = NULL;
heap_caps_free(adv_task.task); heap_caps_free(adv_task.task);
@ -866,7 +884,7 @@ void bt_mesh_adv_deinit(void)
vQueueDelete(xBleMeshRelayQueue.queue); vQueueDelete(xBleMeshRelayQueue.queue);
xBleMeshRelayQueue.queue = NULL; xBleMeshRelayQueue.queue = NULL;
#if CONFIG_SPIRAM_USE_MALLOC #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
heap_caps_free(xBleMeshRelayQueue.buffer); heap_caps_free(xBleMeshRelayQueue.buffer);
xBleMeshRelayQueue.buffer = NULL; xBleMeshRelayQueue.buffer = NULL;
heap_caps_free(xBleMeshRelayQueue.storage); heap_caps_free(xBleMeshRelayQueue.storage);
@ -882,7 +900,7 @@ void bt_mesh_adv_deinit(void)
vQueueDelete(xBleMeshQueue.queue); vQueueDelete(xBleMeshQueue.queue);
xBleMeshQueue.queue = NULL; xBleMeshQueue.queue = NULL;
#if CONFIG_SPIRAM_USE_MALLOC #if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
heap_caps_free(xBleMeshQueue.buffer); heap_caps_free(xBleMeshQueue.buffer);
xBleMeshQueue.buffer = NULL; xBleMeshQueue.buffer = NULL;
heap_caps_free(xBleMeshQueue.storage); heap_caps_free(xBleMeshQueue.storage);