diff --git a/components/bt/Kconfig b/components/bt/Kconfig index 8c9d536579..647c23e9e7 100644 --- a/components/bt/Kconfig +++ b/components/bt/Kconfig @@ -1738,12 +1738,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_ALLOC_FROM_PSRAM_FIRST + bool "External SPIRAM" + depends on SPIRAM_SUPPORT + + config BLE_MESH_MEM_ALLOC_MODE_DEFAULT + bool "Default alloc mode" + depends on 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" diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_common.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_common.h index ad968dd0e4..f78a557762 100644 --- a/components/bt/esp_ble_mesh/mesh_common/include/mesh_common.h +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_common.h @@ -22,6 +22,7 @@ #include #include +#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. diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_common.c b/components/bt/esp_ble_mesh/mesh_common/mesh_common.c index 8ab86266eb..73a84e4a1d 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_common.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_common.c @@ -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_ALLOC_FROM_PSRAM_FIRST + 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_ALLOC_FROM_PSRAM_FIRST + 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;