Download Print this page

Advertisement

Quick Links

AVR132: Using the Enhanced Watchdog Timer
Features
Watchdog System Reset Source
Parameter Backup Prior to Watchdog System Reset
Wakeup Timer from all Sleep Modes
Using the Watchdog for Both Wakeup and System Reset
Handling the Watchdog Reset Flag
Changing the Watchdog Configuration
Flowcharts for Watchdog Operation
Example Source Code
Introduction
"Well designed watchdog timers fire off every day, quietly saving systems and lives
without the esteem offered to human heroes." - Jack Ganssle
No piece of software, save the very smallest, is free from bugs. The application could
get stuck in endless loops. Unexpected error codes could cause serious problems if
not handled correctly. Electrical noise or an unusual sequence of external events
could put the system in a state not thought of by the designers. All these cases could
potentially hang the system forever or cause serious damage to its surroundings.
Automatic handling and recovery of such cases is the job of a watchdog timer.
The Enhanced Watchdog Timer (WDT) runs independent of the rest of the system,
causing system resets whenever it times out. However, the application software
should ensure that the timeout never occurs by resetting the WDT periodically as long
as the software is in a known healthy state. If the system hangs or program execution
is corrupted, the WDT will not receive its periodic reset, and will eventually time out
and cause a system reset.
The WDT in all new AVR devices also has the ability to generate interrupts instead of
resetting the device. Since the WDT runs from its own independent clock, it can be
used to wake up the AVR from all sleep modes. This makes it an ideal wakeup timer,
easily combined with ordinary operation as a system reset source. The interrupt can
also be used to get an early warning of a upcoming Watchdog System Reset, so that
vital parameters can be backed up to non-volatile memory.
8-bit
Microcontrollers
Application Note
Rev. 2551A–AVR–11/03

Advertisement

loading
Need help?

Need help?

Do you have a question about the AVR132 and is the answer not in the manual?

Questions and answers

Subscribe to Our Youtube Channel

Summary of Contents for Atmel AVR132

  • Page 1 AVR132: Using the Enhanced Watchdog Timer 8-bit Features • Watchdog System Reset Source Microcontrollers • Parameter Backup Prior to Watchdog System Reset • Wakeup Timer from all Sleep Modes • Using the Watchdog for Both Wakeup and System Reset •...
  • Page 2 Theory When the Enhanced Watchdog Timer (WDT) period has expired, a WDT timeout occurs. The timeout period is adjusted using a configurable prescaler, which divides the WDT oscillator clock by a constant factor. Executing the WDR (Watchdog Reset) instruction resets the timer value. The application software using the WDT must be designed so that it executes the WDR instruction periodically whenever it decides that the system still operates correctly.
  • Page 3 AVR 132 Application Note Figure 1. Event Sequence When a WDT Timeout Occurs WDT timeout WDT Interrupt Mode enabled ? WDT Interrupt Flag set WDT System Reset WDT Reset Flag set WDT System Reset Mode enabled ? WDT System Reset WDT Interrupt Mode Mode enabled disabled...
  • Page 4 Most embedded systems consist of some initialization code and a main loop. This con- struction is also the most effective setup for use with a watchdog. An example for using the WDT with such systems is shown in Figure 2. Figure 2.
  • Page 5 AVR 132 Application Note Parameter Backup Prior The method described in the previous section does not give any warning of a coming WDT System Reset. The application has no means of handling a timeout in software to WDT System Reset before the system reset occurs.
  • Page 6 Using the WDT Interrupt As described above, the WDT has its own internal oscillator running independently from the main CPU clock. This makes it possible to use the WDT Interrupt as a wakeup Mode source from all sleep modes. By enabling only the WDT Interrupt Mode, a timeout will generate an interrupt request, but not cause any system resets on further timeouts.
  • Page 7: Startup Considerations

    AVR 132 Application Note Using the WDT in It is also possible to set up the WDT to work as a wakeup timer when entering sleep mode, and switch to WDT System Reset operation when back in active mode. With this Combined Operation setup there is no need for disabling the WDT Interrupt Mode, as it is automatically dis- abled by the hardware.
  • Page 8 grammed only. If the WDTON fuse is programmed, the WDT System Reset Mode is always enabled. How to change the fuse settings is described in the device datasheets. If the WDT is intentionally used in the application and a system reset occurs, the startup code should have a scheme for handling the WDT Reset Flag.
  • Page 9 AVR 132 Application Note Figure 6. Timed Sequences for Changing the WDT Configuration Disable WDT System Reset Change WDT timeout period Mode Disable interrupts Disable interrupts Set Watchdog Set Watchdog Change Enable bit Change Enable bit Clear WDT System Change WDT Prescaler Reset Enable bit within settings within 4 cycles 4 cycles...
  • Page 10 Using the WDT as a This example implements the structure described in “Figure 2. Main loop when using the WDT System Reset Mode.”, with an initialization routine and a main loop with three rou- System Reset Source tines and a health check at the end. Each routine has its own health flag to indicate that everything is ok.
  • Page 11 AVR 132 Application Note The first conditional branch handles the case where no reset flags are set upon startup. Since the reset flags are always cleared in the initialization routine, this only happens when runaway code wraps back to address 0 and runs the startup code once again without a reset.
  • Page 12 Parse Command The command parser uses the switch keyword in C to convert the button press bit pat- tern to a command code. The pattern is compared against the bit masks for each of the command buttons. When a match is found, the command code is set accordingly and the health flag for this routine is set.
  • Page 13: Combined Operation

    AVR 132 Application Note Command 3 gives an example of runaway code. This example just calls a function at an unused address. The program counter runs to the end of program memory and wraps back to address 0. No reset flags will be set and the fault is caught safely in the initializa- tion routine.
  • Page 14: Literature References

    The WDT Interrupt handler is implemented as described in Figure 5 on page 7. The Sleep Enable bit is used as a Wakeup flag. When Command 1 is executed, the applica- tion resets the WDT, sets the Sleep Enable bit and then enters sleep mode. The interrupt handler is executed when the WDT timeout wakes up the CPU, and the Wakeup flag decides what action to take.
  • Page 15 No licenses to patents or other intellectual property of Atmel are granted by the Company in connection with the sale of Atmel products, expressly or by implication. Atmel’s products are not authorized for use as critical components in life support devices or systems.