From 747c713ba925fd77f6f4a8bb000db3cedd9d45ab Mon Sep 17 00:00:00 2001 From: todd-herbert Date: Fri, 19 Apr 2024 00:27:18 +1200 Subject: [PATCH] (ESP32) Fix bluetooth after light-sleep; de-init for deep sleep (#3655) --- src/nimble/NimbleBluetooth.cpp | 10 ++++++++-- src/platform/esp32/main-esp32.cpp | 5 +++-- src/sleep.cpp | 4 ++-- variants/heltec_wireless_paper/variant.h | 3 --- variants/heltec_wireless_paper_v1/variant.h | 3 --- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/nimble/NimbleBluetooth.cpp b/src/nimble/NimbleBluetooth.cpp index 092aef470..8f7e00461 100644 --- a/src/nimble/NimbleBluetooth.cpp +++ b/src/nimble/NimbleBluetooth.cpp @@ -106,20 +106,26 @@ static NimbleBluetoothFromRadioCallback *fromRadioCallbacks; void NimbleBluetooth::shutdown() { + // No measurable power saving for ESP32 during light-sleep(?) +#ifndef ARCH_ESP32 // Shutdown bluetooth for minimum power draw LOG_INFO("Disable bluetooth\n"); - // Bluefruit.Advertising.stop(); NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising(); pAdvertising->reset(); pAdvertising->stop(); +#endif } -// Extra power-saving on some devices +// Proper shutdown for ESP32. Needs reboot to reverse. void NimbleBluetooth::deinit() { +#ifdef ARCH_ESP32 + LOG_INFO("Disable bluetooth until reboot\n"); NimBLEDevice::deinit(); +#endif } +// Has initial setup been completed bool NimbleBluetooth::isActive() { return bleServer; diff --git a/src/platform/esp32/main-esp32.cpp b/src/platform/esp32/main-esp32.cpp index 3fb6e7774..2894a49fc 100644 --- a/src/platform/esp32/main-esp32.cpp +++ b/src/platform/esp32/main-esp32.cpp @@ -30,9 +30,10 @@ void setBluetoothEnable(bool enable) } if (enable && !nimbleBluetooth->isActive()) { nimbleBluetooth->setup(); - } else if (!enable) { - nimbleBluetooth->shutdown(); } + // For ESP32, no way to recover from bluetooth shutdown without reboot + // BLE advertising automatically stops when MCU enters light-sleep(?) + // For deep-sleep, shutdown hardware with nimbleBluetooth->deinit(). Requires reboot to reverse } } #else diff --git a/src/sleep.cpp b/src/sleep.cpp index 860a676df..a2a221d79 100644 --- a/src/sleep.cpp +++ b/src/sleep.cpp @@ -207,8 +207,8 @@ void doDeepSleep(uint32_t msecToWake, bool skipPreflight = false) // esp_wifi_stop(); waitEnterSleep(skipPreflight); -#ifdef NIMBLE_DEINIT_FOR_DEEPSLEEP - // Extra power saving on some devices +#ifdef ARCH_ESP32 + // Full shutdown of bluetooth hardware nimbleBluetooth->deinit(); #endif diff --git a/variants/heltec_wireless_paper/variant.h b/variants/heltec_wireless_paper/variant.h index 466925a2e..29b8bbbd1 100644 --- a/variants/heltec_wireless_paper/variant.h +++ b/variants/heltec_wireless_paper/variant.h @@ -55,6 +55,3 @@ #define SX126X_DIO2_AS_RF_SWITCH #define SX126X_DIO3_TCXO_VOLTAGE 1.8 - -// Power management -#define NIMBLE_DEINIT_FOR_DEEPSLEEP // Required to reach manufacturers claim of 18uA diff --git a/variants/heltec_wireless_paper_v1/variant.h b/variants/heltec_wireless_paper_v1/variant.h index 466925a2e..29b8bbbd1 100644 --- a/variants/heltec_wireless_paper_v1/variant.h +++ b/variants/heltec_wireless_paper_v1/variant.h @@ -55,6 +55,3 @@ #define SX126X_DIO2_AS_RF_SWITCH #define SX126X_DIO3_TCXO_VOLTAGE 1.8 - -// Power management -#define NIMBLE_DEINIT_FOR_DEEPSLEEP // Required to reach manufacturers claim of 18uA