Refactor with WATCHDOG_CONF_TIMEOUT

This commit is contained in:
dak664 2011-03-14 16:14:47 -04:00
parent 0829e50f4b
commit d9b5bac953

View file

@ -32,15 +32,28 @@
/* Watchdog routines for the AVR */ /* Watchdog routines for the AVR */
/* Default timeout of 2 seconds is available on most MCUs */ /* The default timeout of 2 seconds works on most MCUs.
* It should be disabled during sleep (unless used for wakeup) since
* it draws significant current (~5 uamp on 1284p, 20x the MCU consumption).
*
* Note the wdt is not properly simulated in AVR Studio 4 Simulator 1:
* On many devices calls to wdt_reset will have no effect, and a wdt reboot will occur.
* The MCUSR will not show the cause of a wdt reboot.
* A 1MHz clock is assumed; at 8MHz timeout occurs 8x faster than it should.
* Simulator 2 is supposed to work on supported devices (not atmega128rfa1),
* but neither it nor Studio 5 beta do any resets on the 1284p.
*
* Setting WATCHDOG_CONF_TIMEOUT -1 will disable the WDT.
*/
//#define WATCHDOG_CONF_TIMEOUT -1
#ifndef WATCHDOG_CONF_TIMEOUT #ifndef WATCHDOG_CONF_TIMEOUT
#define WATCHDOG_CONF_TIMEOUT WDTO_2S #define WATCHDOG_CONF_TIMEOUT WDTO_2S
//#define WATCHDOG_CONF_TIMEOUT WDTO_4S
#endif #endif
/* While balancing start and stop calls is a good idea, an imbalance will cause /* While balancing start and stop calls is a good idea, an imbalance will cause
* resets that can take a lot of time to track down. * resets that can take a lot of time to track down.
* Some low power protocols may do this. * Some low power protocols may cause this.
* The default is no balance; define WATCHDOG_CONF_BALANCE 1 to override. * The default is no balance; define WATCHDOG_CONF_BALANCE 1 to override.
*/ */
#ifndef WATCHDOG_CONF_BALANCE #ifndef WATCHDOG_CONF_BALANCE
@ -51,7 +64,7 @@
#include <avr/wdt.h> #include <avr/wdt.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#if WATCHDOG_CONF_BALANCE #if WATCHDOG_CONF_BALANCE && WATCHDOG_CONF_TIMEOUT >= 0
static int stopped = 0; static int stopped = 0;
#endif #endif
@ -59,39 +72,48 @@ static int stopped = 0;
void void
watchdog_init(void) watchdog_init(void)
{ {
/* Clear startup bit and disable the wdt, whether or not it will be used.
Random code may have caused the last reset.
*/
MCUSR&=~(1<<WDRF); MCUSR&=~(1<<WDRF);
#if WATCHDOG_CONF_BALANCE wdt_disable();
stopped = 0; #if WATCHDOG_CONF_BALANCE && WATCHDOG_CONF_TIMEOUT >= 0
stopped = 1;
#endif #endif
watchdog_stop();
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
watchdog_start(void) watchdog_start(void)
{ {
#if WATCHDOG_CONF_TIMEOUT >= 0
#if WATCHDOG_CONF_BALANCE #if WATCHDOG_CONF_BALANCE
stopped--; stopped--;
if(!stopped) if(!stopped)
#endif #endif
wdt_enable(WATCHDOG_CONF_TIMEOUT); wdt_enable(WATCHDOG_CONF_TIMEOUT);
#endif
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
watchdog_periodic(void) watchdog_periodic(void)
{ {
#if WATCHDOG_CONF_TIMEOUT >= 0
#if WATCHDOG_CONF_BALANCE #if WATCHDOG_CONF_BALANCE
if(!stopped) if(!stopped)
#endif #endif
wdt_reset(); wdt_reset();
#endif
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
watchdog_stop(void) watchdog_stop(void)
{ {
#if WATCHDOG_CONF_TIMEOUT >= 0
#if WATCHDOG_CONF_BALANCE #if WATCHDOG_CONF_BALANCE
stopped++; stopped++;
#endif #endif
wdt_disable(); wdt_disable();
#endif
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
@ -102,3 +124,9 @@ watchdog_reboot(void)
while(1); //loop while(1); //loop
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#if 0
/* Not all AVRs implement the wdt interrupt */
ISR(WDT_vect)
{
}
#endif