Skip to content

Commit e3dd816

Browse files
nRF52840 promicro deepsleep fix with some additions (#7407)
* 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. * 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. * Pro-Micro button SENSE signal Added SENSE signal on the user button. It is explicitly enabled now for this platform. * 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. --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
1 parent 9b8149f commit e3dd816

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/platform/nrf52/main-nrf52.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,14 @@ void cpuDeepSleep(uint32_t msecToWake)
282282
#if SPI_INTERFACES_COUNT > 1
283283
SPI1.end();
284284
#endif
285-
// This may cause crashes as debug messages continue to flow.
286-
Serial.end();
285+
if (Serial) // Another check in case of disabled default serial, does nothing bad
286+
Serial.end(); // This may cause crashes as debug messages continue to flow.
287+
288+
// This causes troubles with waking up on nrf52 (on pro-micro in particular):
289+
// we have no Serial1 in use on nrf52, check Serial and GPS modules.
287290
#ifdef PIN_SERIAL1_RX
288-
Serial1.end();
291+
if (Serial1) // A straightforward solution to the wake from deepsleep problem
292+
Serial1.end();
289293
#endif
290294
setBluetoothEnable(false);
291295

@@ -362,6 +366,7 @@ void cpuDeepSleep(uint32_t msecToWake)
362366
// Resume on user button press
363367
// https://github.com/lyusupov/SoftRF/blob/81c519ca75693b696752235d559e881f2e0511ee/software/firmware/source/SoftRF/src/platform/nRF52.cpp#L1738
364368
constexpr uint32_t DFU_MAGIC_SKIP = 0x6d;
369+
sd_power_gpregret_clr(0, 0xFF); // Clear the register before setting a new values in it for stability reasons
365370
sd_power_gpregret_set(0, DFU_MAGIC_SKIP); // Equivalent NRF_POWER->GPREGRET = DFU_MAGIC_SKIP
366371

367372
// FIXME, use system off mode with ram retention for key state?
@@ -378,6 +383,12 @@ void cpuDeepSleep(uint32_t msecToWake)
378383
nrf_gpio_cfg_sense_set(PIN_BUTTON2, sense1);
379384
#endif
380385

386+
#ifdef PROMICRO_DIY_TCXO
387+
nrf_gpio_cfg_input(BUTTON_PIN, NRF_GPIO_PIN_PULLUP); // Enable internal pull-up on the button pin
388+
nrf_gpio_pin_sense_t sense = NRF_GPIO_PIN_SENSE_LOW; // Configure SENSE signal on low edge
389+
nrf_gpio_cfg_sense_set(BUTTON_PIN, sense); // Apply SENSE to wake up the device from the deep sleep
390+
#endif
391+
381392
auto ok = sd_power_system_off();
382393
if (ok != NRF_SUCCESS) {
383394
LOG_ERROR("FIXME: Ignoring soft device (EasyDMA pending?) and forcing system-off!");

0 commit comments

Comments
 (0)