NRF52 bluetooth cleanup and fix (#3328)

* NRF52 bluetooth cleanup. Fixes BLE not returning after serial PhoneAPI connection

* Use new var name in esp32 arch
This commit is contained in:
Ben Meadors 2024-03-03 13:56:55 -06:00 committed by GitHub
parent e5bf07d4fb
commit 72050530f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 39 additions and 27 deletions

View File

@ -16,5 +16,5 @@ class NimbleBluetooth : BluetoothApi
void startAdvertising(); void startAdvertising();
}; };
void setBluetoothEnable(bool on); void setBluetoothEnable(bool enable);
void clearNVS(); void clearNVS();

View File

@ -20,21 +20,21 @@
#if !defined(CONFIG_IDF_TARGET_ESP32S2) #if !defined(CONFIG_IDF_TARGET_ESP32S2)
void setBluetoothEnable(bool on) void setBluetoothEnable(bool enable)
{ {
if (!isWifiAvailable() && config.bluetooth.enabled == true) { if (!isWifiAvailable() && config.bluetooth.enabled == true) {
if (!nimbleBluetooth) { if (!nimbleBluetooth) {
nimbleBluetooth = new NimbleBluetooth(); nimbleBluetooth = new NimbleBluetooth();
} }
if (on && !nimbleBluetooth->isActive()) { if (enable && !nimbleBluetooth->isActive()) {
nimbleBluetooth->setup(); nimbleBluetooth->setup();
} else if (!on) { } else if (!enable) {
nimbleBluetooth->shutdown(); nimbleBluetooth->shutdown();
} }
} }
} }
#else #else
void setBluetoothEnable(bool on) {} void setBluetoothEnable(bool enable) {}
void updateBatteryLevel(uint8_t level) {} void updateBatteryLevel(uint8_t level) {}
#endif #endif

View File

@ -210,8 +210,10 @@ void NRF52Bluetooth::shutdown()
{ {
// Shutdown bluetooth for minimum power draw // Shutdown bluetooth for minimum power draw
LOG_INFO("Disable NRF52 bluetooth\n"); LOG_INFO("Disable NRF52 bluetooth\n");
if (connectionHandle != 0) {
Bluefruit.disconnect(connectionHandle);
}
Bluefruit.Advertising.stop(); Bluefruit.Advertising.stop();
Bluefruit.setTxPower(0); // Minimum power
} }
bool NRF52Bluetooth::isConnected() bool NRF52Bluetooth::isConnected()
@ -289,6 +291,14 @@ void NRF52Bluetooth::setup()
} }
} }
void NRF52Bluetooth::resumeAdverising()
{
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0);
}
/// Given a level between 0-100, update the BLE attribute /// Given a level between 0-100, update the BLE attribute
void updateBatteryLevel(uint8_t level) void updateBatteryLevel(uint8_t level)
{ {

View File

@ -8,6 +8,7 @@ class NRF52Bluetooth : BluetoothApi
public: public:
void setup(); void setup();
void shutdown(); void shutdown();
void resumeAdverising();
void clearBonds(); void clearBonds();
bool isConnected(); bool isConnected();
int getRssi(); int getRssi();
@ -17,4 +18,4 @@ class NRF52Bluetooth : BluetoothApi
void convertToUint8(uint8_t target[4], uint32_t source); void convertToUint8(uint8_t target[4], uint32_t source);
static bool onPairingPasskey(uint16_t conn_handle, uint8_t const passkey[6], bool match_request); static bool onPairingPasskey(uint16_t conn_handle, uint8_t const passkey[6], bool match_request);
static void onPairingCompleted(uint16_t conn_handle, uint8_t auth_status); static void onPairingCompleted(uint16_t conn_handle, uint8_t auth_status);
}; };

View File

@ -63,28 +63,29 @@ static void initBrownout()
// We don't bother with setting up brownout if soft device is disabled - because during production we always use softdevice // We don't bother with setting up brownout if soft device is disabled - because during production we always use softdevice
} }
static bool bleOn = false;
static const bool useSoftDevice = true; // Set to false for easier debugging static const bool useSoftDevice = true; // Set to false for easier debugging
void setBluetoothEnable(bool on) void setBluetoothEnable(bool enable)
{ {
if (on != bleOn && config.bluetooth.enabled == true) { if (enable && config.bluetooth.enabled) {
if (on) { if (!useSoftDevice) {
LOG_INFO("DISABLING NRF52 BLUETOOTH WHILE DEBUGGING\n");
} else {
if (!nrf52Bluetooth) { if (!nrf52Bluetooth) {
if (!useSoftDevice) LOG_DEBUG("Initializing NRF52 Bluetooth\n");
LOG_INFO("DISABLING NRF52 BLUETOOTH WHILE DEBUGGING\n"); nrf52Bluetooth = new NRF52Bluetooth();
else { nrf52Bluetooth->setup();
nrf52Bluetooth = new NRF52Bluetooth();
nrf52Bluetooth->setup();
// 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 {
nrf52Bluetooth->resumeAdverising();
} }
} else if (nrf52Bluetooth) { }
} else {
if (nrf52Bluetooth) {
nrf52Bluetooth->shutdown(); nrf52Bluetooth->shutdown();
} }
bleOn = on;
} }
} }

View File

@ -20,7 +20,7 @@ std::map<configNames, std::string> settingsStrings;
char *configPath = nullptr; char *configPath = nullptr;
// FIXME - move setBluetoothEnable into a HALPlatform class // FIXME - move setBluetoothEnable into a HALPlatform class
void setBluetoothEnable(bool on) void setBluetoothEnable(bool enable)
{ {
// not needed // not needed
} }

View File

@ -2,7 +2,7 @@
#include <pico/unique_id.h> #include <pico/unique_id.h>
#include <stdio.h> #include <stdio.h>
void setBluetoothEnable(bool on) void setBluetoothEnable(bool enable)
{ {
// not needed // not needed
} }

View File

@ -3,7 +3,7 @@
#include <stm32wle5xx.h> #include <stm32wle5xx.h>
#include <stm32wlxx_hal.h> #include <stm32wlxx_hal.h>
void setBluetoothEnable(bool on) {} void setBluetoothEnable(bool enable) {}
void playStartMelody() {} void playStartMelody() {}
@ -33,4 +33,4 @@ int _gettimeofday(struct timeval *tv, void *tzvp)
{ {
return -1; return -1;
} }
} }

View File

@ -5,6 +5,6 @@
// Functions that are unique to particular target types (esp32, bare, nrf52 etc...) // Functions that are unique to particular target types (esp32, bare, nrf52 etc...)
// Enable/disable bluetooth. // Enable/disable bluetooth.
void setBluetoothEnable(bool on); void setBluetoothEnable(bool enable);
void getMacAddr(uint8_t *dmac); void getMacAddr(uint8_t *dmac);