Browse Source

add: 变更ble部分内存的分配方式,从而实现wifi与ble的分时使用

Wendal Chen 2 years ago
parent
commit
c60f0c4e75

+ 17 - 0
src/bt/blehost/nimble/host/src/ble_hs.c

@@ -43,9 +43,13 @@ static void ble_hs_event_start_stage2(struct ble_npl_event *ev);
 static void ble_hs_timer_sched(int32_t ticks_from_now);
 
 struct os_mempool ble_hs_hci_ev_pool;
+#if MYNEWT_VAL(SYS_MEM_DYNAMIC)
+static os_membuf_t* ble_hs_hci_os_event_buf;
+#else
 static os_membuf_t ble_hs_hci_os_event_buf[
                  OS_MEMPOOL_SIZE(BLE_HS_HCI_EVT_COUNT, sizeof(struct ble_npl_event))
  ];
+#endif
 
 /** OS event - triggers tx of pending notifications and indications. */
 static struct ble_npl_event ble_hs_ev_tx_notifications;
@@ -697,6 +701,9 @@ ble_hs_tx_data(struct os_mbuf *om)
     return ble_hci_trans_hs_acl_tx(om);
 }
 
+#include "wm_bt.h"
+#include "wm_mem.h"
+
 void
 ble_hs_init(void)
 {
@@ -704,6 +711,10 @@ ble_hs_init(void)
     /* Ensure this function only gets called by sysinit. */
     SYSINIT_ASSERT_ACTIVE();
     /* Create memory pool of OS events */
+#if MYNEWT_VAL(SYS_MEM_DYNAMIC)
+    if (ble_hs_hci_os_event_buf == NULL)
+        ble_hs_hci_os_event_buf = tls_mem_alloc(sizeof(os_membuf_t) * OS_MEMPOOL_SIZE(BLE_HS_HCI_EVT_COUNT, sizeof(struct ble_npl_event)));
+#endif
     rc = os_mempool_init(&ble_hs_hci_ev_pool, BLE_HS_HCI_EVT_COUNT,
                          sizeof(struct ble_npl_event), ble_hs_hci_os_event_buf,
                          "ble_hs_hci_ev_pool");
@@ -786,5 +797,11 @@ ble_hs_deinit(void)
     ble_gap_deinit();
     ble_hs_stop_deinit();
     ble_hs_hci_deinit();
+#if MYNEWT_VAL(SYS_MEM_DYNAMIC)
+    if (ble_hs_hci_os_event_buf != NULL) {
+        tls_mem_free(ble_hs_hci_os_event_buf);
+        ble_hs_hci_os_event_buf = NULL;
+    }
+#endif
 }
 

+ 16 - 4
src/bt/blehost/nimble/transport/uart/src/ble_hci_uart.c

@@ -658,6 +658,18 @@ static void nimble_vhci_task(void *parg)
     }
 }
 #endif
+
+// ble_hci_vuart_acl_buf 和 ble_hci_vuart_evt_lo_buf 需要将近20k内存
+// 单从sys的heap分区已经逼近极限, 这里从lua的内存进行分配
+// 从而实现wifi与蓝牙的分时使用. 初始化蓝牙就不要初始化wifi, 销毁蓝牙之后再初始化wifi
+void* luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize);
+static void* v_alloc(size_t len) {
+    return luat_heap_alloc(NULL, NULL, 0, len);
+}
+static void* v_free(void* ptr) {
+    return luat_heap_alloc(NULL, ptr, 0, 0);
+}
+
 int
 ble_hci_vuart_init(uint8_t uart_idx)
 {
@@ -666,7 +678,7 @@ ble_hci_vuart_init(uint8_t uart_idx)
     SYSINIT_ASSERT_ACTIVE();
 
 #if MYNEWT_VAL(SYS_MEM_DYNAMIC)
-    ble_hci_vuart_acl_buf = (os_membuf_t *)tls_mem_alloc(
+    ble_hci_vuart_acl_buf = (os_membuf_t *)v_alloc(
                                             sizeof(os_membuf_t) *
                                             OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_ACL_BUF_COUNT), ACL_BLOCK_SIZE));
     assert(ble_hci_vuart_acl_buf != NULL);
@@ -714,7 +726,7 @@ ble_hci_vuart_init(uint8_t uart_idx)
                          "ble_hci_vuart_evt_hi_pool");
     SYSINIT_PANIC_ASSERT(rc == 0);
 #if MYNEWT_VAL(SYS_MEM_DYNAMIC)
-    ble_hci_vuart_evt_lo_buf = (os_membuf_t *)tls_mem_alloc(
+    ble_hci_vuart_evt_lo_buf = (os_membuf_t *)v_alloc(
             sizeof(os_membuf_t) *
             OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT),
                             MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE)));
@@ -814,7 +826,7 @@ int ble_hci_vuart_deinit()
 #if MYNEWT_VAL(SYS_MEM_DYNAMIC)
         
         if(ble_hci_vuart_acl_buf) {
-            tls_mem_free(ble_hci_vuart_acl_buf);
+            v_free(ble_hci_vuart_acl_buf);
             ble_hci_vuart_acl_buf = NULL;
         }
     
@@ -829,7 +841,7 @@ int ble_hci_vuart_deinit()
         }
     
         if(ble_hci_vuart_evt_lo_buf) {
-            tls_mem_free(ble_hci_vuart_evt_lo_buf);
+            v_free(ble_hci_vuart_evt_lo_buf);
             ble_hci_vuart_evt_lo_buf = NULL;
         }
 #endif