C++11 header-only library to setup a watchdog timer(WDT) using one single line with an expressive and type-safe syntax without any overhead to the generated code.
using namespace avr::wdt; //turn on the WDT to interrupt at each 4s. wdt::on(timeout::at_4s, mode::interrupt); //turn on the WDT to reset at each 16ms. wdt::on(timeout::at_16ms, mode::reset); //turn on the WDT to interrupt and reset at each 250ms. wdt::on(timeout::at_250ms, mode::interrupt_reset); wdt::off(); //turn off the WDT wdt::reset(); /reset the WDT
Note, simplifications are taken into account in order to achieve a more expressive documentation, please, open avr/wdt.hpp to see the real prototypes or declarations.
void on(timeout, mode = mode::reset, atomic_precondition policy = atomic_precondition::no)
Enables the watchdog timer using a valid timeout to the MCU. The default mode is mode::reset
, which means that a system reset occurs when the timer reachs the time-out value. There are other two modes: mode::interrupt
and mode::interrupt_reset
. The first one only gives an interrupt and the second one gives an interrupt and after that a system reset occurs
There are some MCUs that doesn’t have the interrupt mode.
The last argument named policy
can be used to inform that the function will be executed in a safe context that doesn’t allow interrupts during the execution. This is an optimization when interrupts aren’t used by the system or when there is a precondition that interrupts are disabled.
void off(atomic_precondition policy = atomic_precondition::no)
Disables the watchdog timer.
See above about to know more about the optional argument policy
.
void reset()
Resets the watchdog timer using the instruction wdr
.
The set of time-out values allowed to be used is dependent from the MCU. The header avr/wdt/timeout.hpp should be used to find out the definitions. For example, the MCU ATmega328P
is using the definitions presented at avr/wdt/timeout/common.hpp becaused there isn’t any specific header to the platform.
- Add the
include
directory to your include path and check if it contains the paths to the dependencies avrIO and avrINT. - Add
#include <avr/wdt.hpp>
to your source and enjoy it!
- ATtiny13A/13
- ATtiny25/45/85
- ATmega328P
avr-gcc 10.2 -Os -mmcu=attiny85
in r24, 0x3f cli wdr ldi r25, 0x28 out 0x21, r25 out 0x3f, r24
avr-gcc 10.2 -Os -mmcu=attiny85
in r24, 0x3f cli wdr ldi r25, 0x60 out 0x21, r25 out 0x3f, r24
avr-gcc 10.2 -Os -mmcu=attiny85
wdr ldi r24, 0x68 out 0x21, r24
avr-gcc 10.2 -Os -mmcu=atmega328p
in r24, 0x3f cli wdr ldi r25, 0x18 sts 0x0060, r25 ldi r25, 0x08 sts 0x0060, r25 out 0x3f, r24
avr-gcc 10.2 -Os -mmcu=attiny85
in r25, 0x3f cli wdr in r24, 0x34 andi r24, 0xF7 out 0x34, r24 in r24, 0x21 ori r24, 0x18 out 0x21, r24 out 0x21, r1 out 0x3f, r25
Help me to improve this work. If you saw a flaw or you have found out a bug, don’t hesitate to open an issue about it. If you like what you see but your MCU is not supported, I will appreciate your help to support a new MCU :)