From 43a1976f6d5df70d0e51c63c7c151656e67c34fc Mon Sep 17 00:00:00 2001 From: MagnusKos Date: Mon, 21 Jul 2025 18:14:50 +0300 Subject: [PATCH 1/4] Pro-Micro DeepSleep Quick Fix It is noticed that some nRF52840 boards (pro-micro in particular) stopped waking up from the deep sleep state (shutdown state) with a press of a button. The problem is in a Serial1.end() call. --- src/platform/nrf52/main-nrf52.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/platform/nrf52/main-nrf52.cpp b/src/platform/nrf52/main-nrf52.cpp index 1bf9a39fd..958f1abcc 100644 --- a/src/platform/nrf52/main-nrf52.cpp +++ b/src/platform/nrf52/main-nrf52.cpp @@ -284,8 +284,12 @@ void cpuDeepSleep(uint32_t msecToWake) #endif // This may cause crashes as debug messages continue to flow. Serial.end(); + + // This causes troubles with waking up on nrf52 (on pro-micro in particular): + // we have no Serial1 in use on nrf52, check Serial and GPS modules. #ifdef PIN_SERIAL1_RX - Serial1.end(); + if (Serial1) // A straightforward solution to the wake from deepsleep problem + Serial1.end(); #endif setBluetoothEnable(false); From 61e60c974dd18f8365d87280a3e625714a1ceacc Mon Sep 17 00:00:00 2001 From: MagnusKos Date: Mon, 21 Jul 2025 18:40:24 +0300 Subject: [PATCH 2/4] Clear GPREGRET before setting There are some troubles with that register: it is recommended to clear it with 0xFF mask and only after that perform a setting. --- src/platform/nrf52/main-nrf52.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/platform/nrf52/main-nrf52.cpp b/src/platform/nrf52/main-nrf52.cpp index 958f1abcc..d886dd4a5 100644 --- a/src/platform/nrf52/main-nrf52.cpp +++ b/src/platform/nrf52/main-nrf52.cpp @@ -366,6 +366,7 @@ void cpuDeepSleep(uint32_t msecToWake) // Resume on user button press // https://github.com/lyusupov/SoftRF/blob/81c519ca75693b696752235d559e881f2e0511ee/software/firmware/source/SoftRF/src/platform/nRF52.cpp#L1738 constexpr uint32_t DFU_MAGIC_SKIP = 0x6d; + sd_power_gpregret_clr(0, 0xFF); // Clear the register before setting a new values in it for stability reasons sd_power_gpregret_set(0, DFU_MAGIC_SKIP); // Equivalent NRF_POWER->GPREGRET = DFU_MAGIC_SKIP // FIXME, use system off mode with ram retention for key state? From 18022ed535a8cc6a644b2942dc327cd51981b53a Mon Sep 17 00:00:00 2001 From: MagnusKos Date: Mon, 21 Jul 2025 18:50:10 +0300 Subject: [PATCH 3/4] Pro-Micro button SENSE signal Added SENSE signal on the user button. It is explicitly enabled now for this platform. --- src/platform/nrf52/main-nrf52.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/platform/nrf52/main-nrf52.cpp b/src/platform/nrf52/main-nrf52.cpp index d886dd4a5..6345c79f7 100644 --- a/src/platform/nrf52/main-nrf52.cpp +++ b/src/platform/nrf52/main-nrf52.cpp @@ -383,6 +383,12 @@ void cpuDeepSleep(uint32_t msecToWake) nrf_gpio_cfg_sense_set(PIN_BUTTON2, sense1); #endif +#ifdef PROMICRO_DIY_TCXO + nrf_gpio_cfg_input(BUTTON_PIN, NRF_GPIO_PIN_PULLUP); // Enable internal pull-up on the button pin + nrf_gpio_pin_sense_t sense = NRF_GPIO_PIN_SENSE_LOW; // Configure SENSE signal on low edge + nrf_gpio_cfg_sense_set(BUTTON_PIN, sense); // Apply SENSE to wake up the device from the deep sleep +#endif + auto ok = sd_power_system_off(); if (ok != NRF_SUCCESS) { LOG_ERROR("FIXME: Ignoring soft device (EasyDMA pending?) and forcing system-off!"); From 2e96bf642c9ede190a6159ffeb5143876cbcf525 Mon Sep 17 00:00:00 2001 From: MagnusKos Date: Wed, 23 Jul 2025 18:19:33 +0300 Subject: [PATCH 4/4] nRF52 pre-sleep main serial check Added another usage check for the main Serial. It could save some nerves in case the port is not in use by any means. Applied trunk fmt to the file. --- src/platform/nrf52/main-nrf52.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/platform/nrf52/main-nrf52.cpp b/src/platform/nrf52/main-nrf52.cpp index 6345c79f7..590d2f0ae 100644 --- a/src/platform/nrf52/main-nrf52.cpp +++ b/src/platform/nrf52/main-nrf52.cpp @@ -282,13 +282,13 @@ void cpuDeepSleep(uint32_t msecToWake) #if SPI_INTERFACES_COUNT > 1 SPI1.end(); #endif - // This may cause crashes as debug messages continue to flow. - Serial.end(); + if (Serial) // Another check in case of disabled default serial, does nothing bad + Serial.end(); // This may cause crashes as debug messages continue to flow. - // This causes troubles with waking up on nrf52 (on pro-micro in particular): - // we have no Serial1 in use on nrf52, check Serial and GPS modules. + // This causes troubles with waking up on nrf52 (on pro-micro in particular): + // we have no Serial1 in use on nrf52, check Serial and GPS modules. #ifdef PIN_SERIAL1_RX - if (Serial1) // A straightforward solution to the wake from deepsleep problem + if (Serial1) // A straightforward solution to the wake from deepsleep problem Serial1.end(); #endif setBluetoothEnable(false); @@ -366,7 +366,7 @@ void cpuDeepSleep(uint32_t msecToWake) // Resume on user button press // https://github.com/lyusupov/SoftRF/blob/81c519ca75693b696752235d559e881f2e0511ee/software/firmware/source/SoftRF/src/platform/nRF52.cpp#L1738 constexpr uint32_t DFU_MAGIC_SKIP = 0x6d; - sd_power_gpregret_clr(0, 0xFF); // Clear the register before setting a new values in it for stability reasons + sd_power_gpregret_clr(0, 0xFF); // Clear the register before setting a new values in it for stability reasons sd_power_gpregret_set(0, DFU_MAGIC_SKIP); // Equivalent NRF_POWER->GPREGRET = DFU_MAGIC_SKIP // FIXME, use system off mode with ram retention for key state?