From 586ee8034a09e6767971c47e4dc4b7920cb21a5d Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Tue, 14 Sep 2021 00:22:59 +0530 Subject: [PATCH] ESP32: Wait till NimBLE host-cotroller is synced (#9565) * Use RTC memory as heap in unicore mode and readme fixes Signed-off-by: Shubham Patil * Wait till NimBLE host-controller is synced Signed-off-by: Shubham Patil --- .../esp32/README.md | 10 +++++++++- .../esp32/sdkconfig.defaults | 3 +++ .../esp32/sdkconfig.optimize.defaults | 3 +++ src/platform/ESP32/nimble/BLEManagerImpl.cpp | 20 +++++++++++++++++-- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/examples/temperature-measurement-app/esp32/README.md b/examples/temperature-measurement-app/esp32/README.md index 6756a11c7758f3..a0408df6823e10 100644 --- a/examples/temperature-measurement-app/esp32/README.md +++ b/examples/temperature-measurement-app/esp32/README.md @@ -214,5 +214,13 @@ Optimization related to WiFi, BLuetooth, Asserts etc are the part of this example by default. To enable this option set is_debug=false from command-line. ``` -idf.py -p /dev/tty.SLAB_USBtoUART -Dis_debug=false build flash monitor +# Reconfigure the project for additional optimizations +rm -rf sdkconfig build/ +idf.py -Dis_debug=false reconfigure + +# Set additional configurations if required +idf.py menuconfig + +# Build, flash, and monitor the device +idf.py -p /dev/tty.SLAB_USBtoUART build flash monitor ``` diff --git a/examples/temperature-measurement-app/esp32/sdkconfig.defaults b/examples/temperature-measurement-app/esp32/sdkconfig.defaults index b96ea1f15d90f1..a24175c5b32d76 100644 --- a/examples/temperature-measurement-app/esp32/sdkconfig.defaults +++ b/examples/temperature-measurement-app/esp32/sdkconfig.defaults @@ -56,6 +56,9 @@ CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM=16 # FreeRTOS CONFIG_FREERTOS_UNICORE=y +# Add RTC memory to system heap +CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP=y + # LWIP CONFIG_LWIP_TCPIP_RECVMBOX_SIZE=16 diff --git a/examples/temperature-measurement-app/esp32/sdkconfig.optimize.defaults b/examples/temperature-measurement-app/esp32/sdkconfig.optimize.defaults index db3481aa618f37..86335c251937d2 100644 --- a/examples/temperature-measurement-app/esp32/sdkconfig.optimize.defaults +++ b/examples/temperature-measurement-app/esp32/sdkconfig.optimize.defaults @@ -57,6 +57,9 @@ CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM=16 # FreeRTOS CONFIG_FREERTOS_UNICORE=y +# Add RTC memory to system heap +CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP=y + # Log output CONFIG_LOG_DEFAULT_LEVEL_NONE=y diff --git a/src/platform/ESP32/nimble/BLEManagerImpl.cpp b/src/platform/ESP32/nimble/BLEManagerImpl.cpp index 21c5367ac08550..bd29932eccad1d 100644 --- a/src/platform/ESP32/nimble/BLEManagerImpl.cpp +++ b/src/platform/ESP32/nimble/BLEManagerImpl.cpp @@ -90,6 +90,8 @@ const ble_uuid128_t UUID_CHIPoBLEChar_TX = { { BLE_UUID_TYPE_128 }, { 0x12, 0x9D, 0x9F, 0x42, 0x9C, 0x4F, 0x9F, 0x95, 0x59, 0x45, 0x3D, 0x26, 0xF5, 0x2E, 0xEE, 0x18 } }; +SemaphoreHandle_t semaphoreHandle = NULL; + } // unnamed namespace BLEManagerImpl BLEManagerImpl::sInstance; @@ -595,9 +597,8 @@ void BLEManagerImpl::bleprph_on_reset(int reason) void BLEManagerImpl::bleprph_on_sync(void) { - sInstance.mFlags.Set(Flags::kESPBLELayerInitialized); - sInstance.mFlags.Set(Flags::kGATTServiceStarted); ESP_LOGI(TAG, "BLE host-controller synced"); + xSemaphoreGive(semaphoreHandle); } void BLEManagerImpl::bleprph_host_task(void * param) @@ -614,6 +615,14 @@ CHIP_ERROR BLEManagerImpl::InitESPBleLayer(void) VerifyOrExit(!mFlags.Has(Flags::kESPBLELayerInitialized), /* */); + semaphoreHandle = xSemaphoreCreateBinary(); + if (semaphoreHandle == NULL) + { + err = CHIP_ERROR_NO_MEMORY; + ESP_LOGE(TAG, "Failed to create semaphore"); + ExitNow(); + } + for (int i = 0; i < kMaxConnections; i++) { mSubscribedConIds[i] = BLE_CONNECTION_UNINITIALIZED; @@ -655,6 +664,13 @@ CHIP_ERROR BLEManagerImpl::InitESPBleLayer(void) nimble_port_freertos_init(bleprph_host_task); + xSemaphoreTake(semaphoreHandle, portMAX_DELAY); + vSemaphoreDelete(semaphoreHandle); + semaphoreHandle = NULL; + + sInstance.mFlags.Set(Flags::kESPBLELayerInitialized); + sInstance.mFlags.Set(Flags::kGATTServiceStarted); + exit: return err; }