Fix watchdog sleep calculation (#814)
Round requested sleep time up to next multiple of 16ms, to sleep at least requested time. Sleep time of 0 will return immediately. Fixed watchdog sleep time calculation. AVR libc watchdog time implementation is inconsistent, and requires larger flash/ram usage.
This commit is contained in:
committed by
Olivier
parent
7e6db413c7
commit
f84960dc28
+25
-34
@@ -27,7 +27,7 @@ bool hwInit(void)
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
#define WDTO_SLEEP_FOREVER (0xFFu)
|
||||
#define INVALID_INTERRUPT_NUM (0xFFu)
|
||||
|
||||
volatile uint8_t _wokeUpByInterrupt =
|
||||
@@ -72,14 +72,19 @@ ISR (WDT_vect)
|
||||
{
|
||||
}
|
||||
|
||||
void hwPowerDown(period_t period)
|
||||
void hwPowerDown(const uint8_t wdto)
|
||||
{
|
||||
// Let serial prints finish (debug, log etc)
|
||||
#ifndef MY_DISABLED_SERIAL
|
||||
MY_SERIALDEVICE.flush();
|
||||
#endif
|
||||
|
||||
// disable ADC for power saving
|
||||
ADCSRA &= ~(1 << ADEN);
|
||||
// save WDT settings
|
||||
uint8_t WDTsave = WDTCSR;
|
||||
if (period != SLEEP_FOREVER) {
|
||||
wdt_enable(period);
|
||||
if (wdto != WDTO_SLEEP_FOREVER) {
|
||||
wdt_enable(wdto);
|
||||
// enable WDT interrupt before system reset
|
||||
WDTCSR |= (1 << WDCE) | (1 << WDIE);
|
||||
} else {
|
||||
@@ -112,33 +117,19 @@ void hwPowerDown(period_t period)
|
||||
|
||||
void hwInternalSleep(unsigned long ms)
|
||||
{
|
||||
// Let serial prints finish (debug, log etc)
|
||||
#ifndef MY_DISABLED_SERIAL
|
||||
MY_SERIALDEVICE.flush();
|
||||
#endif
|
||||
// Sleeping with watchdog only supports multiples of 16ms.
|
||||
// Round up to next multiple of 16ms, to assure we sleep at least the
|
||||
// requested amount of time. Sleep of 0ms will not sleep at all!
|
||||
ms += 15u;
|
||||
|
||||
static const struct {
|
||||
uint16_t ms;
|
||||
period_t period;
|
||||
} wdtTimes[] = {
|
||||
{ 8000u, SLEEP_8S },
|
||||
{ 4000u, SLEEP_4S },
|
||||
{ 2000u, SLEEP_2S },
|
||||
{ 1000u, SLEEP_1S },
|
||||
{ 500u, SLEEP_500MS },
|
||||
{ 250u, SLEEP_250MS },
|
||||
{ 120u, SLEEP_120MS },
|
||||
{ 60u, SLEEP_60MS },
|
||||
{ 30u, SLEEP_30MS },
|
||||
{ 15u, SLEEP_15MS },
|
||||
};
|
||||
|
||||
for (size_t i = 0; i < sizeof(wdtTimes)/sizeof(wdtTimes[0]); ++i)
|
||||
{
|
||||
while (ms >= wdtTimes[i].ms && !interruptWakeUp() )
|
||||
{
|
||||
hwPowerDown(wdtTimes[i].period);
|
||||
ms -= wdtTimes[i].ms;
|
||||
while (!interruptWakeUp() && ms >= 16) {
|
||||
for (uint8_t period = 9u; ;--period) {
|
||||
const uint16_t comparatorMS = 1 << (period + 4);
|
||||
if ( ms >= comparatorMS) {
|
||||
hwPowerDown(period); // 8192ms => 9, 16ms => 0
|
||||
ms -= comparatorMS;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -180,12 +171,12 @@ int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mo
|
||||
attachInterrupt(interrupt2, wakeUp2, mode2);
|
||||
}
|
||||
|
||||
if (ms>0) {
|
||||
if (ms > 0u) {
|
||||
// sleep for defined time
|
||||
hwInternalSleep(ms);
|
||||
} else {
|
||||
// sleep until ext interrupt triggered
|
||||
hwPowerDown(SLEEP_FOREVER);
|
||||
hwPowerDown(WDTO_SLEEP_FOREVER);
|
||||
}
|
||||
|
||||
// Assure any interrupts attached, will get detached when they did not occur.
|
||||
@@ -277,8 +268,8 @@ void hwDebugPrint(const char *fmt, ... )
|
||||
char fmtBuffer[MY_SERIAL_OUTPUT_SIZE];
|
||||
#ifdef MY_GATEWAY_FEATURE
|
||||
// prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
|
||||
snprintf_P(fmtBuffer, sizeof(fmtBuffer), PSTR("0;255;%d;0;%d;%lu "), C_INTERNAL, I_LOG_MESSAGE,
|
||||
hwMillis());
|
||||
snprintf_P(fmtBuffer, sizeof(fmtBuffer), PSTR("0;255;%d;0;%d;%lu "),
|
||||
C_INTERNAL, I_LOG_MESSAGE, hwMillis());
|
||||
MY_SERIALDEVICE.print(fmtBuffer);
|
||||
#else
|
||||
// prepend timestamp
|
||||
|
||||
@@ -54,20 +54,6 @@ bool hwInit(void);
|
||||
#define hwReadConfigBlock(__buf, __pos, __length) eeprom_read_block((void*)(__buf), (void*)(__pos), (__length))
|
||||
#define hwWriteConfigBlock(__buf, __pos, __length) eeprom_update_block((void*)(__buf), (void*)(__pos), (__length))
|
||||
|
||||
enum period_t {
|
||||
SLEEP_15MS,
|
||||
SLEEP_30MS,
|
||||
SLEEP_60MS,
|
||||
SLEEP_120MS,
|
||||
SLEEP_250MS,
|
||||
SLEEP_500MS,
|
||||
SLEEP_1S,
|
||||
SLEEP_2S,
|
||||
SLEEP_4S,
|
||||
SLEEP_8S,
|
||||
SLEEP_FOREVER
|
||||
};
|
||||
|
||||
void hwInternalSleep(unsigned long ms);
|
||||
|
||||
#ifndef DOXYGEN
|
||||
|
||||
Reference in New Issue
Block a user