Skip to content

C++11 header-only library to enable and disable the watchdog timer(WDT) of an AVR8

License

Notifications You must be signed in to change notification settings

ricardocosme/avrWDT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

avrWDT https://github.com/ricardocosme/avrWDT/workflows/tests/badge.svg?branch=main https://github.com/ricardocosme/avrWDT/workflows/demos/badge.svg?branch=main

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.

Demo

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

API

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.

Enable WDT

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.

Disable WDT

void off(atomic_precondition policy = atomic_precondition::no)

Disables the watchdog timer.

See above about to know more about the optional argument policy.

Reset WDT

void reset() Resets the watchdog timer using the instruction wdr.

Timeouts

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.

How to use it?

  1. Add the include directory to your include path and check if it contains the paths to the dependencies avrIO and avrINT.
  2. Add #include <avr/wdt.hpp> to your source and enjoy it!

Requirements and dependencies

  1. avr-gcc with at least -std=c++11.
  2. avrIO (header-only library)
  3. avrINT (header-only library)

Supported microcontrollers

  1. ATtiny13A/13
  2. ATtiny25/45/85
  3. ATmega328P

Performance

on(timeout::at_4s)

avr-gcc 10.2 -Os -mmcu=attiny85

in	r24, 0x3f
cli
wdr
ldi	r25, 0x28
out	0x21, r25
out	0x3f, r24

on(timeout::at_4s, mode::interrupt)

avr-gcc 10.2 -Os -mmcu=attiny85

in	r24, 0x3f
cli              
wdr              
ldi	r25, 0x60
out	0x21, r25
out	0x3f, r24

on(timeout::at_4s, mode::interrupt_reset, atomic_precondition::yes)

avr-gcc 10.2 -Os -mmcu=attiny85

wdr              
ldi	r24, 0x68
out	0x21, r24

on(timeout::at_16ms)

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  

off()

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 

Contributions

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 :)

About

C++11 header-only library to enable and disable the watchdog timer(WDT) of an AVR8

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published