Skip to content

Commit a327007

Browse files
committed
Support sleep for NRF52 boards by suspending the main task
This uses the core functions suspendLoop() and resumeLoop() to suspend the main task and put the CPU in a low power idle mode. The wakeup occurs either through the specified timeout using a timer interrupt or through an RX interrupt from the radio module. Signed-off-by: Frieder Schrempf <frieder@fris.de>
1 parent f894847 commit a327007

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

src/helpers/NRF52Board.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <bluefruit.h>
55

66
static BLEDfu bledfu;
7+
static SoftwareTimer sleep_timer;
78

89
static void connect_callback(uint16_t conn_handle) {
910
(void)conn_handle;
@@ -17,8 +18,27 @@ static void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
1718
MESH_DEBUG_PRINTLN("BLE client disconnected");
1819
}
1920

21+
static void resume() {
22+
sleep_timer.stop();
23+
resumeLoop();
24+
}
25+
26+
static void sleep_timer_callback(TimerHandle_t xTimerID) {
27+
resume();
28+
}
29+
2030
void NRF52Board::begin() {
2131
startup_reason = BD_STARTUP_NORMAL;
32+
sleep_timer.begin(5000, sleep_timer_callback, NULL, false);
33+
}
34+
35+
void NRF52Board::sleep(uint32_t secs) {
36+
sleep_timer.setPeriod(secs * 1000);
37+
suspendLoop();
38+
}
39+
40+
void NRF52Board::onRXInterrupt() {
41+
resume();
2242
}
2343

2444
void NRF52BoardDCDC::begin() {

src/helpers/NRF52Board.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class NRF52Board : public mesh::MainBoard {
1414
virtual uint8_t getStartupReason() const override { return startup_reason; }
1515
virtual float getMCUTemperature() override;
1616
virtual void reboot() override { NVIC_SystemReset(); }
17+
virtual void sleep(uint32_t secs) override;
18+
virtual void onRXInterrupt() override;
1719
};
1820

1921
/*

0 commit comments

Comments
 (0)