ble_mesh: stack: Update option of using DRAM/SPIRAM for memory allocation

This commit is contained in:
lly 2020-07-02 10:53:07 +08:00
parent ee7b443d28
commit 9973f5ef04
4 changed files with 68 additions and 13 deletions

View file

@ -18,12 +18,37 @@ if BLE_MESH
option in the Bluetooth Controller section in menuconfig, which is
"Scan Duplicate By Device Address and Advertising Data".
config BLE_MESH_ALLOC_FROM_PSRAM_FIRST
bool "BLE Mesh will first allocate memory from PSRAM"
default n
choice BLE_MESH_MEM_ALLOC_MODE
prompt "Memory allocation strategy"
default BLE_MESH_MEM_ALLOC_MODE_INTERNAL
help
When this option is enabled, BLE Mesh stack will try to allocate memory
from PSRAM firstly. This will save the internal RAM if PSRAM exists.
Allocation strategy for BLE Mesh stack, essentially provides ability to
allocate all required dynamic allocations from,
- Internal DRAM memory only
- External SPIRAM memory only
- Either internal or external memory based on default malloc()
behavior in ESP-IDF
Recommended mode here is always internal, since that is most preferred
from security perspective. But if application requirement does not allow
sufficient free internal memory then alternate mode can be selected.
config BLE_MESH_MEM_ALLOC_MODE_INTERNAL
bool "Internal DRAM"
config BLE_MESH_MEM_ALLOC_MODE_EXTERNAL
bool "External SPIRAM"
depends on ESP32_SPIRAM_SUPPORT
config BLE_MESH_MEM_ALLOC_MODE_DEFAULT
bool "Default alloc mode"
depends on ESP32_SPIRAM_SUPPORT
help
Enable this option to use the default memory allocation strategy when
external SPIRAM is enabled. See the SPIRAM options for more details.
endchoice # BLE_MESH_MEM_ALLOC_MODE
config BLE_MESH_FAST_PROV
bool "Enable BLE Mesh Fast Provisioning"

View file

@ -22,6 +22,7 @@
#include <stddef.h>
#include <stdlib.h>
#include "esp_attr.h"
#include "esp_heap_caps.h"
#include "mesh_byteorder.h"
@ -34,14 +35,11 @@
extern "C" {
#endif
#if CONFIG_BLE_MESH_ALLOC_FROM_PSRAM_FIRST
#define bt_mesh_malloc(size) heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
#define bt_mesh_calloc(size) heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
#else
#define bt_mesh_malloc(size) malloc((size))
#define bt_mesh_calloc(size) calloc(1, (size))
#endif /* CONFIG_BLE_MESH_ALLOC_FROM_PSRAM_FIRST */
#define bt_mesh_free(p) free((p))
IRAM_ATTR void *bt_mesh_malloc(size_t size);
IRAM_ATTR void *bt_mesh_calloc(size_t size);
IRAM_ATTR void bt_mesh_free(void *ptr);
/**
* @brief This function allocates memory to store outgoing message.

View file

@ -19,6 +19,37 @@
#include "client_common.h"
#include "mesh_common.h"
IRAM_ATTR void *bt_mesh_malloc(size_t size)
{
#ifdef CONFIG_BLE_MESH_MEM_ALLOC_MODE_INTERNAL
return heap_caps_malloc(size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_EXTERNAL
return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_IRAM_8BIT
return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#else
return malloc(size);
#endif
}
IRAM_ATTR void *bt_mesh_calloc(size_t size)
{
#ifdef CONFIG_BLE_MESH_MEM_ALLOC_MODE_INTERNAL
return heap_caps_calloc(1, size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_EXTERNAL
return heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#elif CONFIG_BLE_MESH_MEM_ALLOC_MODE_IRAM_8BIT
return heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#else
return calloc(1, size);
#endif
}
IRAM_ATTR void bt_mesh_free(void *ptr)
{
heap_caps_free(ptr);
}
struct net_buf_simple *bt_mesh_alloc_buf(u16_t size)
{
struct net_buf_simple *buf = NULL;

View file

@ -225,6 +225,7 @@ CONFIG_BLE_ACTIVE_SCAN_REPORT_ADV_SCAN_RSP_INDIVIDUALLY CONFIG_BT_BLE_ACT_SC
CONFIG_BLE_ESTABLISH_LINK_CONNECTION_TIMEOUT CONFIG_BT_BLE_ESTAB_LINK_CONN_TOUT
CONFIG_BLE_MESH_GATT_PROXY CONFIG_BLE_MESH_GATT_PROXY_SERVER
CONFIG_BLE_MESH_ALLOC_FROM_PSRAM_FIRST CONFIG_BLE_MESH_MEM_ALLOC_MODE_EXTERNAL
CONFIG_NIMBLE_ENABLED CONFIG_BT_NIMBLE_ENABLED
CONFIG_NIMBLE_MEM_ALLOC_MODE CONFIG_BT_NIMBLE_MEM_ALLOC_MODE