Skip to content

Commit 3308df3

Browse files
Add sleep handler to optimize low-power sleep (#1418)
* Add sleep handler to optimize low-power sleep * Add sleep handler to optimize low-power sleep Committer: Sinisha Djukic <s.djukic@gmail.com> Author: Sinisha Djukic <s.djukic@gmail.com> Date: Wed May 13 08:51:48 2020 +0300 Committer: Sinisha Djukic <s.djukic@gmail.com> modified: MyConfig.h modified: core/MySensorsCore.cpp modified: core/MySensorsCore.h modified: keywords.txt * Whitespace fixed * Fixed compiler warning Committer: Sinisha Djukic <s.djukic@gmail.com> Author: Sinisha Djukic <s.djukic@gmail.com> Co-authored-by: Djukic Sinisha (INST/RBR2-BG INST/ESU) <djs1wa3@bosch.com>
1 parent 8103c6c commit 3308df3

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

MyConfig.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,16 @@
12171217
#ifndef MY_SMART_SLEEP_WAIT_DURATION_MS
12181218
#define MY_SMART_SLEEP_WAIT_DURATION_MS (500ul)
12191219
#endif
1220+
1221+
/**
1222+
* @def MY_SLEEP_HANDLER
1223+
* @brief Define this to enable the custom pre- & post-sleep handler.
1224+
*
1225+
* Sleep handler is invoked right before entering the sleep function, as well a just after the sleep
1226+
* completes. Applications can use this handler to turn off peripherals or put pins into a best
1227+
* possible low power state according to the concrete hardware design.
1228+
*/
1229+
//#define MY_SLEEP_HANDLER
12201230
/** @}*/ // End of SleepSettingGrpPub group
12211231

12221232
/**
@@ -1353,7 +1363,6 @@
13531363
#define MY_INCLUSION_BUTTON_PRESSED (LOW)
13541364
#endif
13551365

1356-
13571366
/**************************************
13581367
* Ethernet Gateway Transport Defaults
13591368
***************************************/
@@ -2329,6 +2338,7 @@
23292338
#define MY_DISABLE_REMOTE_RESET
23302339
#define MY_DISABLE_RAM_ROUTING_TABLE_FEATURE
23312340
#define MY_LOCK_DEVICE
2341+
#define MY_SLEEP_HANDLER
23322342
// core
23332343
#define MY_CORE_ONLY
23342344
// GW

core/MySensorsCore.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,15 @@ void doYield(void)
625625
#endif
626626
}
627627

628+
#if !defined(MY_SLEEP_HANDLER)
629+
void sleepHandler(bool sleep)
630+
{
631+
(void)sleep;
632+
// empty function, resolves AVR-specific GCC optimization bug (<5.5) if handler not used
633+
// see here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77326
634+
}
635+
#endif
636+
628637
int8_t _sleep(const uint32_t sleepingMS, const bool smartSleep, const uint8_t interrupt1,
629638
const uint8_t mode1, const uint8_t interrupt2, const uint8_t mode2)
630639
{
@@ -707,6 +716,9 @@ int8_t _sleep(const uint32_t sleepingMS, const bool smartSleep, const uint8_t in
707716
}
708717
#endif
709718

719+
// Call the sleep handler to turn off peripherals optimally
720+
sleepHandler(true);
721+
710722
int8_t result = MY_SLEEP_NOT_POSSIBLE; // default
711723
if (interrupt1 != INTERRUPT_NOT_DEFINED && interrupt2 != INTERRUPT_NOT_DEFINED) {
712724
// both IRQs
@@ -718,6 +730,10 @@ int8_t _sleep(const uint32_t sleepingMS, const bool smartSleep, const uint8_t in
718730
// no IRQ
719731
result = hwSleep(sleepingTimeMS);
720732
}
733+
734+
// Call the sleep handler to turn on peripherals optimally
735+
sleepHandler(false);
736+
721737
setIndication(INDICATION_WAKEUP);
722738
CORE_DEBUG(PSTR("MCO:SLP:WUP=%" PRIi8 "\n"), result); // sleep wake-up
723739
#if defined(MY_SENSOR_NETWORK)

core/MySensorsCore.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,12 @@ bool wait(const uint32_t waitingMS, const mysensors_command_t cmd, const uint8_t
301301
*/
302302
void doYield(void);
303303

304+
/**
305+
* Sleep handler will be called right before and right after entering sleep mode.
306+
* Applications can define own handler to optimize powering down peripherals before entering sleep.
307+
* @param sleep true if entering sleep, false if exiting
308+
*/
309+
void sleepHandler(bool sleep);
304310

305311
/**
306312
* Sleep (PowerDownMode) the MCU and radio. Wake up on timer.

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ MY_SERIAL_OUTPUT_SIZE LITERAL1
4545
MY_SLEEP_NOT_POSSIBLE LITERAL1
4646
MY_SPLASH_SCREEN_DISABLED LITERAL1
4747
MY_WAKE_UP_BY_TIMER LITERAL1
48+
MY_SLEEP_HANDLER LITERAL1
4849

4950
# transport
5051
AUTO LITERAL1

0 commit comments

Comments
 (0)