From 9e1466a34dff3a2782f11a26fd179ce1c2d08569 Mon Sep 17 00:00:00 2001 From: GengYuchao Date: Mon, 4 Jul 2022 21:14:46 +0800 Subject: [PATCH 1/3] move init func to nimble --- .../nimble/port/include/esp_nimble_main.h | 15 --- .../bt/host/nimble/port/src/esp_nimble_main.c | 116 ------------------ 2 files changed, 131 deletions(-) delete mode 100644 components/bt/host/nimble/port/include/esp_nimble_main.h delete mode 100644 components/bt/host/nimble/port/src/esp_nimble_main.c diff --git a/components/bt/host/nimble/port/include/esp_nimble_main.h b/components/bt/host/nimble/port/include/esp_nimble_main.h deleted file mode 100644 index 3a760d5985..0000000000 --- a/components/bt/host/nimble/port/include/esp_nimble_main.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "esp_err.h" - -esp_err_t esp_nimble_init(); - -esp_err_t esp_nimble_enable(void *host_task); - -esp_err_t esp_nimble_disable(); - -esp_err_t esp_nimble_deinit(); diff --git a/components/bt/host/nimble/port/src/esp_nimble_main.c b/components/bt/host/nimble/port/src/esp_nimble_main.c deleted file mode 100644 index 001e2cddfb..0000000000 --- a/components/bt/host/nimble/port/src/esp_nimble_main.c +++ /dev/null @@ -1,116 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "esp_attr.h" -#include "esp_heap_caps.h" -#include "sdkconfig.h" -#include "esp_nimble_mem.h" -#include "nimble/nimble_port.h" -#include "host/ble_hs.h" - -static TaskHandle_t host_task_h; - -extern void ble_hs_deinit(void); -static struct ble_hs_stop_listener stop_listener; - -static struct ble_npl_eventq g_eventq_dflt; -static struct ble_npl_sem ble_hs_stop_sem; -static struct ble_npl_event ble_hs_ev_stop; - -/** - * Called when the host stop procedure has completed. - */ -static void -ble_hs_stop_cb(int status, void *arg) -{ - ble_npl_sem_release(&ble_hs_stop_sem); -} - -static void -nimble_port_stop_cb(struct ble_npl_event *ev) -{ - ble_npl_sem_release(&ble_hs_stop_sem); -} - -esp_err_t esp_nimble_init(void) -{ -#if !SOC_ESP_NIMBLE_CONTROLLER - /* Initialize the function pointers for OS porting */ - npl_freertos_funcs_init(); - - npl_freertos_mempool_init(); - - /* Initialize default event queue */ - - ble_npl_eventq_init(&g_eventq_dflt); - - - os_msys_init(); - - void ble_store_ram_init(void); // Do we need this? - /* XXX Need to have template for store */ - ble_store_ram_init(); -#endif - - /* Initialize the host */ - ble_hs_init(); - return ESP_OK; -} - -esp_err_t esp_nimble_enable(void *host_task) -{ - /* - * Create task where NimBLE host will run. It is not strictly necessary to - * have separate task for NimBLE host, but since something needs to handle - * default queue it is just easier to make separate task which does this. - */ - xTaskCreatePinnedToCore(host_task, "nimble_host", NIMBLE_HS_STACK_SIZE, - NULL, (configMAX_PRIORITIES - 4), &host_task_h, NIMBLE_CORE); - return ESP_OK; - -} - -esp_err_t esp_nimble_disable(void) -{ - esp_err_t err = ESP_OK; - ble_npl_sem_init(&ble_hs_stop_sem, 0); - - /* Initiate a host stop procedure. */ - err = ble_hs_stop(&stop_listener, ble_hs_stop_cb, - NULL); - if (err != 0) { - ble_npl_sem_deinit(&ble_hs_stop_sem); - return err; - } - - /* Wait till the host stop procedure is complete */ - ble_npl_sem_pend(&ble_hs_stop_sem, BLE_NPL_TIME_FOREVER); - - ble_npl_event_init(&ble_hs_ev_stop, nimble_port_stop_cb, - NULL); - ble_npl_eventq_put(&g_eventq_dflt, &ble_hs_ev_stop); - - /* Wait till the event is serviced */ - ble_npl_sem_pend(&ble_hs_stop_sem, BLE_NPL_TIME_FOREVER); - - ble_npl_sem_deinit(&ble_hs_stop_sem); - - return ESP_OK; - -} - -esp_err_t esp_nimble_deinit(void) -{ -#if !(SOC_ESP_NIMBLE_CONTROLLER && CONFIG_BT_CONTROLLER_ENABLED) - ble_npl_eventq_deinit(&g_eventq_dflt); -#endif - - if (host_task_h) { - vTaskDelete(host_task_h); - } - ble_hs_deinit(); - return ESP_OK; -} From ee13e58b169fc4acf2334876bf2f5f2855271d27 Mon Sep 17 00:00:00 2001 From: GengYuchao Date: Mon, 4 Jul 2022 21:16:13 +0800 Subject: [PATCH 2/3] update head files for nimble Remove some useless error printing display --- .../porting/nimble/include/nimble/nimble_port.h | 16 ++++++++++++---- .../include/nimble/nimble_port_freertos.h | 16 ++++++++++++++++ .../porting/npl/freertos/src/npl_os_freertos.c | 14 +++++++++----- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/components/bt/porting/nimble/include/nimble/nimble_port.h b/components/bt/porting/nimble/include/nimble/nimble_port.h index 4746633965..c0a24ba722 100644 --- a/components/bt/porting/nimble/include/nimble/nimble_port.h +++ b/components/bt/porting/nimble/include/nimble/nimble_port.h @@ -41,14 +41,22 @@ void nimble_port_deinit(void); void nimble_port_run(void); int nimble_port_stop(void); -struct ble_npl_eventq *nimble_port_get_dflt_eventq(void); - +/** + * @brief esp_nimble_init - Initialize the NimBLE host stack + * + * @return esp_err_t + */ esp_err_t esp_nimble_init(void); -esp_err_t esp_nimble_enable(void *host_task); -esp_err_t esp_nimble_disable(void); +/** + * @brief esp_nimble_deinit - Deinitialize the NimBLE host stack + * + * @return esp_err_t + */ esp_err_t esp_nimble_deinit(void); +struct ble_npl_eventq *nimble_port_get_dflt_eventq(void); + #ifdef __cplusplus } diff --git a/components/bt/porting/npl/freertos/include/nimble/nimble_port_freertos.h b/components/bt/porting/npl/freertos/include/nimble/nimble_port_freertos.h index dccf1652c1..c6b38faf4d 100644 --- a/components/bt/porting/npl/freertos/include/nimble/nimble_port_freertos.h +++ b/components/bt/porting/npl/freertos/include/nimble/nimble_port_freertos.h @@ -15,6 +15,22 @@ extern "C" { #endif + +/** + * @brief esp_nimble_enable - Initialize the NimBLE host task + * + * @param host_task + * @return esp_err_t + */ +esp_err_t esp_nimble_enable(void *host_task); + +/** + * @brief esp_nimble_disable - Disable the NimBLE host task + * + * @return esp_err_t + */ +esp_err_t esp_nimble_disable(void); + void nimble_port_freertos_init(TaskFunction_t host_task_fn); void nimble_port_freertos_deinit(void); void npl_freertos_funcs_init(void); diff --git a/components/bt/porting/npl/freertos/src/npl_os_freertos.c b/components/bt/porting/npl/freertos/src/npl_os_freertos.c index 885ccb1b3a..eef5153beb 100644 --- a/components/bt/porting/npl/freertos/src/npl_os_freertos.c +++ b/components/bt/porting/npl/freertos/src/npl_os_freertos.c @@ -762,12 +762,16 @@ IRAM_ATTR npl_freertos_callout_deinit(struct ble_npl_callout *co) BLE_LL_ASSERT(callout->handle); #if CONFIG_BT_NIMBLE_USE_ESP_TIMER - if(esp_timer_stop(callout->handle)) - ESP_LOGW(TAG, "Timer not stopped"); - - if(esp_timer_delete(callout->handle)) + esp_err_t err = esp_timer_stop(callout->handle); + if(err != ESP_OK) { + if (err != ESP_ERR_INVALID_STATE) { // ESP_ERR_INVALID_STATE is expected when timer is already stopped + ESP_LOGW(TAG, "Timer not stopped"); + } + } + err = esp_timer_delete(callout->handle); + if(err != ESP_OK) { ESP_LOGW(TAG, "Timer not deleted"); - + } #else xTimerDelete(callout->handle, portMAX_DELAY); From 3be5ab72682d34ab8239d813ce7c4fe48e183c33 Mon Sep 17 00:00:00 2001 From: GengYuchao Date: Mon, 4 Jul 2022 21:25:13 +0800 Subject: [PATCH 3/3] Update nimble port init changes --- components/bt/host/nimble/nimble | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/bt/host/nimble/nimble b/components/bt/host/nimble/nimble index c23dee337f..eda98ca8fa 160000 --- a/components/bt/host/nimble/nimble +++ b/components/bt/host/nimble/nimble @@ -1 +1 @@ -Subproject commit c23dee337f809e7e774736265703bf616a966ffa +Subproject commit eda98ca8faee53f0552f8fec6c5359c23297d27e