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:
Embedded Innovation
2017-04-02 19:49:57 +02:00
committed by Olivier
parent 7e6db413c7
commit f84960dc28
2 changed files with 57 additions and 80 deletions
+25 -34
View File
@@ -27,7 +27,7 @@ bool hwInit(void)
return true; return true;
} }
#define WDTO_SLEEP_FOREVER (0xFFu)
#define INVALID_INTERRUPT_NUM (0xFFu) #define INVALID_INTERRUPT_NUM (0xFFu)
volatile uint8_t _wokeUpByInterrupt = 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 // disable ADC for power saving
ADCSRA &= ~(1 << ADEN); ADCSRA &= ~(1 << ADEN);
// save WDT settings // save WDT settings
uint8_t WDTsave = WDTCSR; uint8_t WDTsave = WDTCSR;
if (period != SLEEP_FOREVER) { if (wdto != WDTO_SLEEP_FOREVER) {
wdt_enable(period); wdt_enable(wdto);
// enable WDT interrupt before system reset // enable WDT interrupt before system reset
WDTCSR |= (1 << WDCE) | (1 << WDIE); WDTCSR |= (1 << WDCE) | (1 << WDIE);
} else { } else {
@@ -112,33 +117,19 @@ void hwPowerDown(period_t period)
void hwInternalSleep(unsigned long ms) void hwInternalSleep(unsigned long ms)
{ {
// Let serial prints finish (debug, log etc) // Sleeping with watchdog only supports multiples of 16ms.
#ifndef MY_DISABLED_SERIAL // Round up to next multiple of 16ms, to assure we sleep at least the
MY_SERIALDEVICE.flush(); // requested amount of time. Sleep of 0ms will not sleep at all!
#endif ms += 15u;
static const struct { while (!interruptWakeUp() && ms >= 16) {
uint16_t ms; for (uint8_t period = 9u; ;--period) {
period_t period; const uint16_t comparatorMS = 1 << (period + 4);
} wdtTimes[] = { if ( ms >= comparatorMS) {
{ 8000u, SLEEP_8S }, hwPowerDown(period); // 8192ms => 9, 16ms => 0
{ 4000u, SLEEP_4S }, ms -= comparatorMS;
{ 2000u, SLEEP_2S }, break;
{ 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;
} }
} }
} }
@@ -180,12 +171,12 @@ int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mo
attachInterrupt(interrupt2, wakeUp2, mode2); attachInterrupt(interrupt2, wakeUp2, mode2);
} }
if (ms>0) { if (ms > 0u) {
// sleep for defined time // sleep for defined time
hwInternalSleep(ms); hwInternalSleep(ms);
} else { } else {
// sleep until ext interrupt triggered // sleep until ext interrupt triggered
hwPowerDown(SLEEP_FOREVER); hwPowerDown(WDTO_SLEEP_FOREVER);
} }
// Assure any interrupts attached, will get detached when they did not occur. // 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]; char fmtBuffer[MY_SERIAL_OUTPUT_SIZE];
#ifdef MY_GATEWAY_FEATURE #ifdef MY_GATEWAY_FEATURE
// prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE) // 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, snprintf_P(fmtBuffer, sizeof(fmtBuffer), PSTR("0;255;%d;0;%d;%lu "),
hwMillis()); C_INTERNAL, I_LOG_MESSAGE, hwMillis());
MY_SERIALDEVICE.print(fmtBuffer); MY_SERIALDEVICE.print(fmtBuffer);
#else #else
// prepend timestamp // prepend timestamp
-14
View File
@@ -54,20 +54,6 @@ bool hwInit(void);
#define hwReadConfigBlock(__buf, __pos, __length) eeprom_read_block((void*)(__buf), (void*)(__pos), (__length)) #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)) #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); void hwInternalSleep(unsigned long ms);
#ifndef DOXYGEN #ifndef DOXYGEN