Skip to content

Commit 1ae1d7b

Browse files
committed
Support sleep for NRF52 boards by suspending the main task
This uses a FreeRTOS semaphore to block the main loop task and resume it on RX radio IRQ or timeout. Using the Arduino core functions suspendLoop() and resumeLoop() can lead to deadlocks which can only be avoided by disabling IRQs and possibly missing incoming packets. Signed-off-by: Frieder Schrempf <frieder@fris.de>
1 parent a6ce03d commit 1ae1d7b

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/helpers/NRF52Board.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ static void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
1919

2020
void NRF52Board::begin() {
2121
startup_reason = BD_STARTUP_NORMAL;
22+
rx_wait_sem = xSemaphoreCreateBinary();
23+
xSemaphoreGive(rx_wait_sem);
24+
xSemaphoreTake(rx_wait_sem, portMAX_DELAY);
25+
}
26+
27+
void NRF52Board::sleep(uint32_t secs) {
28+
MESH_DEBUG_PRINTLN("Suspending loop for powersaving, set wakeup timer in %u seconds", secs);
29+
xSemaphoreTake(rx_wait_sem, pdMS_TO_TICKS(secs * 1000));
30+
}
31+
32+
void NRF52Board::onRXInterrupt() {
33+
xSemaphoreGive(rx_wait_sem);
2234
}
2335

2436
void NRF52BoardDCDC::begin() {

src/helpers/NRF52Board.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#if defined(NRF52_PLATFORM)
77

88
class NRF52Board : public mesh::MainBoard {
9+
private:
10+
SemaphoreHandle_t rx_wait_sem;
11+
912
protected:
1013
uint8_t startup_reason;
1114

@@ -14,6 +17,8 @@ class NRF52Board : public mesh::MainBoard {
1417
virtual uint8_t getStartupReason() const override { return startup_reason; }
1518
virtual float getMCUTemperature() override;
1619
virtual void reboot() override { NVIC_SystemReset(); }
20+
virtual void sleep(uint32_t secs) override;
21+
virtual void onRXInterrupt() override;
1722
};
1823

1924
/*

0 commit comments

Comments
 (0)