From d407db5ee18b286b0d02e88e6d5d45dc9bd9831d Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Fri, 13 Nov 2020 09:33:59 +0800 Subject: [PATCH] add more deep sleep support for nrf52 boards --- src/main.cpp | 8 ++++++++ src/nrf52/main-nrf52.cpp | 36 +++++++++++++++++++++++------------- src/sleep.cpp | 2 +- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index b61ef9e18..61bb2be14 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -172,6 +172,10 @@ class ButtonThread : public OSThread { #ifdef BUTTON_PIN userButton = OneButton(BUTTON_PIN, true, true); +#ifdef INPUT_PULLUP_SENSE + // Some platforms (nrf52) have a SENSE variant which allows wake from sleep - override what OneButton did + pinMode(BUTTON_PIN, INPUT_PULLUP_SENSE); +#endif userButton.attachClick(userButtonPressed); userButton.attachDuringLongPress(userButtonPressedLong); userButton.attachDoubleClick(userButtonDoublePressed); @@ -179,6 +183,10 @@ class ButtonThread : public OSThread #endif #ifdef BUTTON_PIN_ALT userButtonAlt = OneButton(BUTTON_PIN_ALT, true, true); +#ifdef INPUT_PULLUP_SENSE + // Some platforms (nrf52) have a SENSE variant which allows wake from sleep - override what OneButton did + pinMode(BUTTON_PIN_ALT, INPUT_PULLUP_SENSE); +#endif userButtonAlt.attachClick(userButtonPressed); userButtonAlt.attachDuringLongPress(userButtonPressedLong); userButtonAlt.attachDoubleClick(userButtonDoublePressed); diff --git a/src/nrf52/main-nrf52.cpp b/src/nrf52/main-nrf52.cpp index eec306e52..8b7bb65b9 100644 --- a/src/nrf52/main-nrf52.cpp +++ b/src/nrf52/main-nrf52.cpp @@ -1,6 +1,8 @@ #include "NRF52Bluetooth.h" #include "configuration.h" #include "graphics/TFTDisplay.h" +#include +#include #include #include #include @@ -64,7 +66,7 @@ void setBluetoothEnable(bool on) } } } else { - if(nrf52Bluetooth) + if (nrf52Bluetooth) nrf52Bluetooth->shutdown(); } bleOn = on; @@ -98,7 +100,7 @@ void nrf52Setup() #ifdef BQ25703A_ADDR auto *bq = new BQ25713(); - if(!bq->setup()) + if (!bq->setup()) DEBUG_MSG("ERROR! Charge controller init failed\n"); #endif @@ -114,18 +116,26 @@ void nrf52Setup() void cpuDeepSleep(uint64_t msecToWake) { - DEBUG_MSG("FIXME: implement NRF52 deep sleep enter actions\n"); - // FIXME, configure RTC to wake us - // FIXME, power down SPI, I2C, RAMs + // FIXME, configure RTC or button press to wake us + // FIXME, power down SPI, I2C, RAMs + Wire.end(); + SPI.end(); + Serial.end(); + Serial1.end(); - // FIXME, use system off mode with ram retention for key state? - // FIXME, use non-init RAM per https://devzone.nordicsemi.com/f/nordic-q-a/48919/ram-retention-settings-with-softdevice-enabled + // FIXME, use system off mode with ram retention for key state? + // FIXME, use non-init RAM per + // https://devzone.nordicsemi.com/f/nordic-q-a/48919/ram-retention-settings-with-softdevice-enabled - while(1) { - delay(5000); - DEBUG_MSG("."); - } + auto ok = sd_power_system_off(); + if(ok != NRF_SUCCESS) { + DEBUG_MSG("FIXME: Ignoring soft device (EasyDMA pending?) and forcing system-off!\n"); + NRF_POWER->SYSTEMOFF = 1; + } - // FIXME, after wake power up SPI, I2C, RAMs, reinit LORA - DEBUG_MSG("FIXME: implement NRF52 deep sleep wake actions\n"); + // The following code should not be run, because we are off + while (1) { + delay(5000); + DEBUG_MSG("."); + } } \ No newline at end of file diff --git a/src/sleep.cpp b/src/sleep.cpp index 160479d52..60bf911f2 100644 --- a/src/sleep.cpp +++ b/src/sleep.cpp @@ -142,7 +142,7 @@ static void waitEnterSleep() void doDeepSleep(uint64_t msecToWake) { - DEBUG_MSG("Entering deep sleep for %llu seconds\n", msecToWake / 1000); + DEBUG_MSG("Entering deep sleep for %lu seconds\n", msecToWake / 1000); // not using wifi yet, but once we are this is needed to shutoff the radio hw // esp_wifi_stop();