ble_mesh: stack: Add a option of using IRAM for memory allocation

This commit is contained in:
lly 2020-07-02 10:53:07 +08:00
parent 452ce608fe
commit c0db196464
4 changed files with 77 additions and 13 deletions

View File

@ -18,12 +18,46 @@ if BLE_MESH
option in the Bluetooth Controller section in menuconfig, which is option in the Bluetooth Controller section in menuconfig, which is
"Scan Duplicate By Device Address and Advertising Data". "Scan Duplicate By Device Address and Advertising Data".
config BLE_MESH_ALLOC_FROM_PSRAM_FIRST choice BLE_MESH_MEM_ALLOC_MODE
bool "BLE Mesh will first allocate memory from PSRAM" prompt "Memory allocation strategy"
default n default BLE_MESH_MEM_ALLOC_MODE_INTERNAL
help help
When this option is enabled, BLE Mesh stack will try to allocate memory Allocation strategy for BLE Mesh stack, essentially provides ability to
from PSRAM firstly. This will save the internal RAM if PSRAM exists. 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
- Internal IRAM memory wherever applicable else internal DRAM
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.
config BLE_MESH_MEM_ALLOC_MODE_IRAM_8BIT
bool "Internal IRAM"
depends on ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
help
Allows to use IRAM memory region as 8bit accessible region. Every
unaligned (8bit or 16bit) access will result in an exception and
incur penalty of certain clock cycles per unaligned read/write.
endchoice # BLE_MESH_MEM_ALLOC_MODE
config BLE_MESH_FAST_PROV config BLE_MESH_FAST_PROV
bool "Enable BLE Mesh Fast Provisioning" bool "Enable BLE Mesh Fast Provisioning"

View File

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

View File

@ -19,6 +19,37 @@
#include "client_common.h" #include "client_common.h"
#include "mesh_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 *bt_mesh_alloc_buf(u16_t size)
{ {
struct net_buf_simple *buf = NULL; struct net_buf_simple *buf = NULL;

View File

@ -226,6 +226,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_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_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_ENABLED CONFIG_BT_NIMBLE_ENABLED
CONFIG_NIMBLE_MEM_ALLOC_MODE CONFIG_BT_NIMBLE_MEM_ALLOC_MODE CONFIG_NIMBLE_MEM_ALLOC_MODE CONFIG_BT_NIMBLE_MEM_ALLOC_MODE