Skip to content

Commit

Permalink
Add watchdog notification Issue EnviroDIY#344 Watchdog bites with no …
Browse files Browse the repository at this point in the history
…warning
  • Loading branch information
neilh20.gitk authored and neilh20.gitk committed Jan 5, 2021
1 parent f34b70b commit b136149
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
32 changes: 24 additions & 8 deletions src/WatchDogs/WatchDogAVR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#include <avr/interrupt.h>
#include <avr/wdt.h>

volatile uint32_t extendedWatchDogAVR::_barksUntilReset = 0;
volatile int16_t extendedWatchDogAVR::_barksUntilReset = 0;
// The AVR watchdog runs for 8 seconds
#define BARKS_UNTIL_RESET (_resetTime_s / 8)

extendedWatchDogAVR::extendedWatchDogAVR() {}
extendedWatchDogAVR::~extendedWatchDogAVR() {
Expand All @@ -28,7 +30,7 @@ extendedWatchDogAVR::~extendedWatchDogAVR() {
// One-time initialization of watchdog timer.
void extendedWatchDogAVR::setupWatchDog(uint32_t resetTime_s) {
_resetTime_s = resetTime_s;
extendedWatchDogAVR::_barksUntilReset = _resetTime_s / 8;
extendedWatchDogAVR::_barksUntilReset = BARKS_UNTIL_RESET;
MS_DBG(F("Watch-dog timeout is set for"), _resetTime_s,
F("sec with the interrupt firing"),
extendedWatchDogAVR::_barksUntilReset, F("times before the reset."));
Expand Down Expand Up @@ -67,23 +69,32 @@ void extendedWatchDogAVR::enableWatchDog() {
// 4 seconds: 0bxx1xx000
// 8 seconds: 0bxx1xx001

extendedWatchDogAVR::_barksUntilReset = _resetTime_s / 8;
MS_DBG(F("The watch dog is enabled in interrupt-only mode."));
MS_DBG(F("The interrupt will fire"), extendedWatchDogAVR::_barksUntilReset,
extendedWatchDogAVR::_barksUntilReset = BARKS_UNTIL_RESET;
MS_DBG(F("Watchdog Enabled, Interrupt will fire"), extendedWatchDogAVR::_barksUntilReset,
F("times before the system resets."));
}


void extendedWatchDogAVR::disableWatchDog() {
// Disable the watchdog
wdt_disable();
MS_DBG(F("Watchdog disabled. barksUntilReset"), extendedWatchDogAVR::_barksUntilReset);
#if defined MS_WATCHDOGAVR_DEBUG
delay(30); //Alow 30 chars out
#endif
}


void extendedWatchDogAVR::resetWatchDog() {
extendedWatchDogAVR::_barksUntilReset = _resetTime_s / 8;
// Reset the watchdog.
wdt_reset();
wdt_reset(); // Reset the watchdog.

if ((int16_t)BARKS_UNTIL_RESET != extendedWatchDogAVR::_barksUntilReset) {
PRINTOUT (F("... Watchdog low barksUntilReset"),_barksUntilReset,F(" expected"),BARKS_UNTIL_RESET);
extendedWatchDogAVR::_barksUntilReset = BARKS_UNTIL_RESET;
}



}


Expand All @@ -98,6 +109,9 @@ ISR(WDT_vect) {
if (extendedWatchDogAVR::_barksUntilReset <= 0) {
MCUSR = 0; // reset flags

//Let anybody listening know. Not ideal to do a print in ISR ...but... woof
PRINTOUT(F(" **** WATCHDOG Woof Woof. Reseting Processor***"));
delay(100);
// Put timer in reset-only mode:
WDTCSR |= 0b00011000; // Enter config mode.
WDTCSR = 0b00001000 |
Expand All @@ -109,6 +123,8 @@ ISR(WDT_vect) {
// wdt_reset(); // not needed
} else {
wdt_reset(); // start timer again (still in interrupt-only mode)
// This is never expected to happen, but if it does .. wimper
MS_DEEP_DBG(F(" WATCHDOG ISR barksUntilReset"),extendedWatchDogAVR::_barksUntilReset);
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/WatchDogs/WatchDogAVR.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
#define MS_DEBUGGING_STD "WatchDogAVR"
#endif

#ifdef MS_WATCHDOGAVR_DEBUG_DEEP
#define MS_DEBUGGING_DEEP "WatchDogAVR"
#endif

// Included Dependencies
#include "ModSensorDebugger.h"
#undef MS_DEBUGGING_STD
#undef MS_DEBUGGING_DEEP

/**
* @brief The extendedWatchDogAVR class uses the pre-reset interrupt to of the
Expand Down Expand Up @@ -74,8 +79,9 @@ class extendedWatchDogAVR {
/**
* @brief The number of times the pre-reset interrupt is allowed to fire
* before the watchdog reset is allowed.
* Range: low 10s, must be signed integer
*/
static volatile uint32_t _barksUntilReset;
static volatile int16_t _barksUntilReset;

private:
uint32_t _resetTime_s;
Expand Down

0 comments on commit b136149

Please sign in to comment.