]> granicus.if.org Git - esp-idf/commitdiff
component/bt : fix bluetooth controller enable limit && release memory when de-initia...
authorTian Hao <tianhao@espressif.com>
Tue, 12 Sep 2017 14:36:17 +0000 (22:36 +0800)
committerTian Hao <tianhao@espressif.com>
Tue, 19 Sep 2017 13:14:28 +0000 (21:14 +0800)
1. fix bluetooth controller enable limit
2. release memory when de-initialize bluetooth controller
3. fix heap_caps_add_region limit

components/bt/bt.c
components/bt/include/bt.h
components/bt/lib
components/heap/heap_caps_init.c
examples/bluetooth/a2dp_sink/main/main.c
examples/bluetooth/ble_adv/main/app_bt.c

index 61e8792da0c41597a596843d6b7d01ff08a61ab6..580d576b46071f057d021591d60da73f3a4063f3 100644 (file)
@@ -17,6 +17,7 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "esp_heap_caps_init.h"
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
 #include "freertos/queue.h"
 #include "esp_attr.h"
 #include "esp_phy_init.h"
 #include "bt.h"
+#include "esp_err.h"
+#include "esp_log.h"
 
 #if CONFIG_BT_ENABLED
 
+#define BTDM_LOG_TAG                        "BTDM_INIT"
+
 #define BTDM_INIT_PERIOD                    (5000)    /* ms */
 
 /* Bluetooth system and controller config */
@@ -48,6 +53,7 @@ extern int btdm_controller_init(uint32_t config_mask, esp_bt_controller_config_t
 extern int btdm_controller_deinit(void);
 extern int btdm_controller_enable(esp_bt_mode_t mode);
 extern int btdm_controller_disable(esp_bt_mode_t mode);
+extern uint8_t btdm_controller_get_mode(void);
 extern void btdm_rf_bb_init(void);
 
 /* VHCI function interface */
@@ -315,6 +321,24 @@ static uint32_t btdm_config_mask_load(void)
     return mask;
 }
 
+static void btdm_controller_release_mem(void)
+{
+    uint32_t bt_mem_start, bt_mem_end;
+#if CONFIG_BT_DRAM_RELEASE
+    bt_mem_start = 0x3ffb0000; bt_mem_end = 0x3ffb3000; //Reserve BT data region
+    ESP_ERROR_CHECK( heap_caps_add_region((intptr_t)bt_mem_start, (intptr_t)bt_mem_end));
+    bt_mem_start = 0x3ffb8000; bt_mem_end = 0x3ffbbb28; //Reserve BT data region
+    ESP_ERROR_CHECK( heap_caps_add_region((intptr_t)bt_mem_start, (intptr_t)bt_mem_end));
+    bt_mem_start = 0x3ffbdb28; bt_mem_end = 0x3ffc0000; //Reserve BT data region
+    ESP_ERROR_CHECK( heap_caps_add_region((intptr_t)bt_mem_start, (intptr_t)bt_mem_end));
+#else
+    bt_mem_start = 0x3ffb0000; bt_mem_end = 0x3ffc0000; //Reserve BT hardware shared memory & BT data region
+    ESP_ERROR_CHECK( heap_caps_add_region((intptr_t)bt_mem_start, (intptr_t)bt_mem_end));
+#endif
+    bt_mem_start = 0x3ffae2a0; bt_mem_end = 0x3ffaff10; //Reserve ROM data region
+    ESP_ERROR_CHECK( heap_caps_add_region((intptr_t)bt_mem_start, (intptr_t)bt_mem_end));
+}
+
 esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
 {
     BaseType_t ret;
@@ -356,7 +380,9 @@ esp_err_t esp_bt_controller_deinit(void)
         return ESP_ERR_NO_MEM;
     }
 
-    btdm_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE;
+    btdm_controller_release_mem();
+
+    btdm_controller_status = ESP_BT_CONTROLLER_STATUS_SHUTDOWN;
     return ESP_OK;
 }
 
@@ -367,8 +393,13 @@ esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode)
     if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_INITED) {
         return ESP_ERR_INVALID_STATE;
     }
-
-    if (mode != ESP_BT_MODE_BTDM) {
+#if CONFIG_BT_DRAM_RELEASE
+    if (mode != ESP_BT_MODE_BLE) {
+#else
+    if (mode != ESP_BT_MODE_BLE
+            && mode != ESP_BT_MODE_CLASSIC_BT
+            && mode != ESP_BT_MODE_BTDM) {
+#endif
         return ESP_ERR_INVALID_ARG;
     }
 
@@ -397,8 +428,10 @@ esp_err_t esp_bt_controller_disable(esp_bt_mode_t mode)
         return ESP_ERR_INVALID_STATE;
     }
 
-    if (mode != ESP_BT_MODE_BTDM) {
-        return ESP_ERR_INVALID_ARG;
+    if (mode != btdm_controller_get_mode()) {
+        ESP_LOGW(BTDM_LOG_TAG, "The input mode should be equal %d, but ignore error, use %d instead of %d\n",
+                btdm_controller_get_mode(), btdm_controller_get_mode(), mode);
+        mode = btdm_controller_get_mode();
     }
 
     ret = btdm_controller_disable(mode);
@@ -435,5 +468,4 @@ esp_power_level_t esp_ble_tx_power_get(esp_ble_power_type_t power_type)
     return (esp_power_level_t)ble_txpwr_get(power_type);
 }
 
-
-#endif
+#endif /*  CONFIG_BT_ENABLED */
index a11cd3eeceaa9272ed0faa67bc4d4d6f8143bd0c..c8aab8fa002b59d25db0432eb3ff53d9b1dbbd9a 100644 (file)
@@ -78,6 +78,7 @@ typedef enum {
     ESP_BT_CONTROLLER_STATUS_IDLE = 0,
     ESP_BT_CONTROLLER_STATUS_INITED,
     ESP_BT_CONTROLLER_STATUS_ENABLED,
+    ESP_BT_CONTROLLER_STATUS_SHUTDOWN,
     ESP_BT_CONTROLLER_STATUS_NUM,
 } esp_bt_controller_status_t;
 
@@ -153,14 +154,18 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg);
  *
  * This function should be called only once, after any other BT functions are called.
  * This function is not whole completed, esp_bt_controller_init cannot called after this function.
+ * After call this function, it will release all the .bss/.data and .etc memory to heap dynamically.
+ * The release memory about 64K bytes (if CONFIG_BT_DRAM_RELEASE=y, it's about 36K bytes)
  * @return  ESP_OK - success, other - failed
  */
 esp_err_t esp_bt_controller_deinit(void);
 
 /**
- * @brief Enable BT controller
+ * @brief Enable BT controller.
+ *               By a knowned issue, if the function already set mode, it can not set another mode dynamically.
+ *               If want to change mode type, should call esp_bt_controller_disable, then call esp_bt_controller_enable.
  * @param mode : the mode(BLE/BT/BTDM) to enable.
- *               Now only support BTDM.
+ *               If CONFIG_BT_DRAM_RELEASE=y, the param mode should only be ESP_BT_MODE_BLE.
  * @return       ESP_OK - success, other - failed
  */
 esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode);
@@ -168,7 +173,8 @@ esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode);
 /**
  * @brief  Disable BT controller
  * @param mode : the mode(BLE/BT/BTDM) to disable.
- *               Now only support BTDM.
+ *               the mode should be equal to which esp_bt_controller_enable set.
+ *               If not, the function will give warning, then use the correct mode to do disable.
  * @return       ESP_OK - success, other - failed
  */
 esp_err_t esp_bt_controller_disable(esp_bt_mode_t mode);
index d41e3512971612a4903e1ea1189968408811a1ae..a3aee13381c21c10590cf8b5a6be3010abf3d4a6 160000 (submodule)
@@ -1 +1 @@
-Subproject commit d41e3512971612a4903e1ea1189968408811a1ae
+Subproject commit a3aee13381c21c10590cf8b5a6be3010abf3d4a6
index 9f88c87d51990206d2d60a44f4d9a17b030e2585..0c8405ec1bbc60a59e772a9097c79d9f10122152 100644 (file)
@@ -213,7 +213,8 @@ esp_err_t heap_caps_add_region(intptr_t start, intptr_t end)
 
     for (int i = 0; i < soc_memory_region_count; i++) {
         const soc_memory_region_t *region = &soc_memory_regions[i];
-        if (region->start <= start && (region->start + region->size) > end) {
+        // Test requested start only as 'end' may be in a different region entry, assume 'end' has same caps
+        if (region->start <= start && (region->start + region->size) > start) {
             const uint32_t *caps = soc_memory_types[region->type].caps;
             return heap_caps_add_region_with_caps(caps, start, end);
         }
index 3e97b4f3ec642e3ba9563ea971d0e993a504db14..e8d579c8b0a5ab2b9fde8175c2d05be86cef9043 100644 (file)
@@ -58,7 +58,7 @@ void app_main()
         return;
     }
 
-    if (esp_bt_controller_enable(ESP_BT_MODE_BTDM) != ESP_OK) {
+    if (esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT) != ESP_OK) {
         ESP_LOGE(BT_AV_TAG, "%s enable controller failed\n", __func__);
         return;
     }
index 6d0ef24595230c58d6775cc87eeb698ec109284e..0955f4a02f02f59adf0f446a2349ee80291252c5 100644 (file)
@@ -232,7 +232,7 @@ void app_main()
         return;
     }
 
-    if (esp_bt_controller_enable(ESP_BT_MODE_BTDM) != ESP_OK) {
+    if (esp_bt_controller_enable(ESP_BT_MODE_BLE) != ESP_OK) {
         ESP_LOGI(tag, "Bluetooth controller enable failed");
         return;
     }