From 1c5853ce79fb62bbcf9d497342f7acb153abd1f0 Mon Sep 17 00:00:00 2001 From: Tombula <38461340+Tombula@users.noreply.github.com> Date: Tue, 1 May 2018 16:59:04 +0200 Subject: [PATCH] Add sleep forever by sleep(0); (#1113) For battery powered sensors which wake up by the RST Pin its useful to sleep the cpu forever. When using this command: sleep(INTERRUPT_NOT_DEFINED, MODE_NOT_DEFINED, 0, false); the code will be stripped down to hwSleep(0);. But this will not sleep the cpu forever. --- hal/architecture/AVR/MyHwAVR.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/hal/architecture/AVR/MyHwAVR.cpp b/hal/architecture/AVR/MyHwAVR.cpp index 2083fc3..de3ff86 100644 --- a/hal/architecture/AVR/MyHwAVR.cpp +++ b/hal/architecture/AVR/MyHwAVR.cpp @@ -141,8 +141,23 @@ void hwInternalSleep(uint32_t ms) int8_t hwSleep(uint32_t ms) { - hwInternalSleep(ms); - return MY_WAKE_UP_BY_TIMER; + // Return what woke the mcu. + // Default: no interrupt triggered, timer wake up + int8_t ret = MY_WAKE_UP_BY_TIMER; + if (ms > 0u) { + // sleep for defined time + hwInternalSleep(ms); + } else { + // sleep until ext interrupt triggered + hwPowerDown(WDTO_SLEEP_FOREVER); + } + if (interruptWakeUp()) { + ret = static_cast(_wokeUpByInterrupt); + } + // Clear woke-up-by-interrupt flag, so next sleeps won't return immediately. + _wokeUpByInterrupt = INVALID_INTERRUPT_NUM; + + return ret; } int8_t hwSleep(const uint8_t interrupt, const uint8_t mode, uint32_t ms)