Procházet zdrojové kódy

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

lly před 5 roky
rodič
revize
cba3733d29

+ 39 - 5
components/bt/esp_ble_mesh/Kconfig.in

@@ -18,12 +18,46 @@ 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
+            - 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
         bool "Enable BLE Mesh Fast Provisioning"

+ 6 - 8
components/bt/esp_ble_mesh/mesh_common/include/mesh_common.h

@@ -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.

+ 31 - 0
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;

+ 1 - 0
components/bt/sdkconfig.rename

@@ -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_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