diff --git a/docs/power.md b/docs/power.md index 53221290..f0773f99 100644 --- a/docs/power.md +++ b/docs/power.md @@ -218,6 +218,21 @@ then shows `RESETREAS=0x00020000`, bit 17, wake from LPCOMP); with a cutoff set, lowering the supply below it must switch the node off within ~3 minutes and `get pwr` after the wake must say `last off: Low Voltage`. +## nRF52 sleep & peripheral fixes (bench verification pending) + +Audit of the nRF52840 firmware and Adafruit core identified several sources of +unnecessary idle draw that affect all nRF52 boards (not only SenseCAP Solar): + +- **`CRYPTOCELL_IRQn` sleep insomnia**: `USE_CC310_HW_CRYPTO=1` runs CC310 in + polling mode with `CRYPTOCELL_IRQn` disabled in NVIC; completed operations + leave it pending and immediately wake `sd_app_evt_wait()`/`__WFE()`. + `NRF52Board::sleep()` now clears `CRYPTOCELL_IRQn` before sleeping, matching + Adafruit core's `wiring.c`. +- **DC/DC regulator after SoftDevice start**: `NRF52BoardDCDC::begin()` runs + before `Bluefruit.begin()`, which enables SoftDevice S140 (defaulting + `NRF_POWER` to LDO). `NRF52BoardDCDC::onBootComplete()` now re-enables DC/DC + via `sd_power_dcdc_mode_set(NRF_POWER_DCDC_ENABLE)` once SoftDevice is active. + ## Measuring this yourself — two traps 1. **A solar node measured on its USB side measures the CHARGER.** With a diff --git a/src/helpers/NRF52Board.cpp b/src/helpers/NRF52Board.cpp index 618364c3..2268ec6d 100644 --- a/src/helpers/NRF52Board.cpp +++ b/src/helpers/NRF52Board.cpp @@ -288,10 +288,7 @@ void NRF52Board::pwrmgtWakeArmVbus() { } #endif -void NRF52BoardDCDC::begin() { - NRF52Board::begin(); - - // Enable DC/DC converter for improved power efficiency +static void enableNrf52Dcdc() { uint8_t sd_enabled = 0; sd_softdevice_is_enabled(&sd_enabled); if (sd_enabled) { @@ -301,6 +298,21 @@ void NRF52BoardDCDC::begin() { } } +void NRF52BoardDCDC::begin() { + NRF52Board::begin(); + + // Enable DC/DC converter for improved power efficiency + enableNrf52Dcdc(); +} + +void NRF52BoardDCDC::onBootComplete() { + NRF52Board::onBootComplete(); + + // SoftDevice (Bluefruit.begin()) starts after board.begin() and defaults + // NRF_POWER to LDO mode; re-enable DC/DC once SoftDevice owns NRF_POWER. + enableNrf52Dcdc(); +} + bool NRF52Board::isExternalPowered() { // Check if SoftDevice is enabled before using its API uint8_t sd_enabled = 0; @@ -324,6 +336,14 @@ void NRF52Board::sleep(uint32_t secs) { NVIC_ClearPendingIRQ(FPU_IRQn); #endif + // Clear pending CRYPTOCELL interrupt to avoid WFE/sd_app_evt_wait() insomnia. + // Adafruit_nRFCrypto polls CC310 with CRYPTOCELL_IRQn disabled in NVIC, so + // completed crypto/TRNG calls leave it pending (same workaround as Adafruit + // core wiring.c / port_cmsis_systick.c). + #ifdef NRF_CRYPTOCELL + NVIC_ClearPendingIRQ(CRYPTOCELL_IRQn); + #endif + // On nRF52, we use event-driven sleep instead of timed sleep // The 'secs' parameter is ignored - we wake on any interrupt uint8_t sd_enabled = 0; diff --git a/src/helpers/NRF52Board.h b/src/helpers/NRF52Board.h index c7644334..7c40d441 100644 --- a/src/helpers/NRF52Board.h +++ b/src/helpers/NRF52Board.h @@ -93,5 +93,6 @@ class NRF52BoardDCDC : virtual public NRF52Board { public: NRF52BoardDCDC() {} virtual void begin() override; + virtual void onBootComplete() override; }; #endif \ No newline at end of file