Workaround to disable bluetooth on NRF52 (#4055)

* Workaround to allow bluetooth disable on NRF52

* Use miminum tx power for bluetooth

* Reorganize

* Instantiate nrf52Bluetooth correctly..

* Change log message
This commit is contained in:
todd-herbert 2024-06-12 23:34:00 +12:00 committed by GitHub
parent 0c23e3109a
commit d60d1d7447
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 50 additions and 18 deletions

View File

@ -215,6 +215,18 @@ void NRF52Bluetooth::shutdown()
Bluefruit.Advertising.stop(); Bluefruit.Advertising.stop();
} }
void NRF52Bluetooth::startDisabled()
{
// Setup Bluetooth
nrf52Bluetooth->setup();
// Shutdown bluetooth for minimum power draw
Bluefruit.Advertising.stop();
Bluefruit.setTxPower(-40); // Minimum power
LOG_INFO("Disabling NRF52 Bluetooth. (Workaround: tx power min, advertising stopped)\n");
}
bool NRF52Bluetooth::isConnected() bool NRF52Bluetooth::isConnected()
{ {
return Bluefruit.connected(connectionHandle); return Bluefruit.connected(connectionHandle);

View File

@ -8,6 +8,7 @@ class NRF52Bluetooth : BluetoothApi
public: public:
void setup(); void setup();
void shutdown(); void shutdown();
void startDisabled();
void resumeAdvertising(); void resumeAdvertising();
void clearBonds(); void clearBonds();
bool isConnected(); bool isConnected();

View File

@ -68,10 +68,29 @@ static const bool useSoftDevice = true; // Set to false for easier debugging
#if !MESHTASTIC_EXCLUDE_BLUETOOTH #if !MESHTASTIC_EXCLUDE_BLUETOOTH
void setBluetoothEnable(bool enable) void setBluetoothEnable(bool enable)
{ {
if (enable && config.bluetooth.enabled) { // For debugging use: don't use bluetooth
if (!useSoftDevice) { if (!useSoftDevice) {
if (enable)
LOG_INFO("DISABLING NRF52 BLUETOOTH WHILE DEBUGGING\n"); LOG_INFO("DISABLING NRF52 BLUETOOTH WHILE DEBUGGING\n");
} else { return;
}
// If user disabled bluetooth: init then disable advertising & reduce power
// Workaround. Avoid issue where device hangs several days after boot..
// Allegedly, no significant increase in power consumption
if (!config.bluetooth.enabled) {
static bool initialized = false;
if (!initialized) {
nrf52Bluetooth = new NRF52Bluetooth();
nrf52Bluetooth->startDisabled();
initBrownout();
initialized = true;
}
return;
}
if (enable) {
// If not yet set-up
if (!nrf52Bluetooth) { if (!nrf52Bluetooth) {
LOG_DEBUG("Initializing NRF52 Bluetooth\n"); LOG_DEBUG("Initializing NRF52 Bluetooth\n");
nrf52Bluetooth = new NRF52Bluetooth(); nrf52Bluetooth = new NRF52Bluetooth();
@ -79,17 +98,17 @@ void setBluetoothEnable(bool enable)
// We delay brownout init until after BLE because BLE starts soft device // We delay brownout init until after BLE because BLE starts soft device
initBrownout(); initBrownout();
} else { }
// Already setup, apparently
else
nrf52Bluetooth->resumeAdvertising(); nrf52Bluetooth->resumeAdvertising();
} }
} // Disable (if previously set-up)
} else { else if (nrf52Bluetooth)
if (nrf52Bluetooth) {
nrf52Bluetooth->shutdown(); nrf52Bluetooth->shutdown();
}
}
} }
#else #else
#warning NRF52 "Bluetooth disable" workaround does not apply to builds with MESHTASTIC_EXCLUDE_BLUETOOTH
void setBluetoothEnable(bool enable) {} void setBluetoothEnable(bool enable) {}
#endif #endif
/** /**