Code Monkey home page Code Monkey logo

avrwdt's Introduction

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

avrwdt's People

Contributors

ricardocosme avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.