From 9973f5ef0493f6bf0adba074993c10895d1d4de7 Mon Sep 17 00:00:00 2001 From: lly Date: Thu, 2 Jul 2020 10:53:07 +0800 Subject: [PATCH] ble_mesh: stack: Update option of using DRAM/SPIRAM for memory allocation --- components/bt/esp_ble_mesh/Kconfig.in | 35 ++++++++++++++++--- .../mesh_common/include/mesh_common.h | 14 ++++---- .../bt/esp_ble_mesh/mesh_common/mesh_common.c | 31 ++++++++++++++++ components/bt/sdkconfig.rename | 1 + 4 files changed, 68 insertions(+), 13 deletions(-) diff --git a/components/bt/esp_ble_mesh/Kconfig.in b/components/bt/esp_ble_mesh/Kconfig.in index 6a593c511..a2489e06b 100644 --- a/components/bt/esp_ble_mesh/Kconfig.in +++ b/components/bt/esp_ble_mesh/Kconfig.in @@ -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" 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 ad968dd0e..f78a55776 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 8ab86266e..cfa5f454c 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_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; diff --git a/components/bt/sdkconfig.rename b/components/bt/sdkconfig.rename index 1c523cd1c..c117084f2 100644 --- a/components/bt/sdkconfig.rename +++ b/components/bt/sdkconfig.rename @@ -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