FastArduino  v1.9
C++ library to build fast but small Arduino/AVR projects
interrupts.h File Reference

General API for handling AVR interrupt vectors. More...

#include "boards/io.h"
#include <avr/interrupt.h>
Include dependency graph for interrupts.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.


 Defines API to handle AVR interruptions.


template<typename Handler >
void interrupt::register_handler (Handler &handler)
 Register a class instance containing methods that shall be called back by an ISR. More...
template<typename Handler >
void interrupt::unregister_handler (Handler &handler)
 Unregister a class instance that was previously registered with interrupt::register_handler. More...

Detailed Description

General API for handling AVR interrupt vectors.

In FastArduino, ISR management is performed in 1 or 2 steps:

  1. Declare the ISR itself, in your program, by using one of REGISTER_XXX macros provided by FastArduino API; these macros always come in 2 flavours, described later. These macros define the ISR function and perform a callback to your code.
  2. If you used a REGISTER_XXX_ISR_METHOD macro flavour, then you need to register the instance of the class that contains the callback method; this is done with interrupt::register_handler(). This method may sometimes be wrapped inside FastArduino API, hence you do not always need to call it yourself.

There are 2 flavours of each registration macro:

  1. REGISTER_XXX_ISR_FUNCTION: this flavour defines an ISR for the *"XXX"* signal with callback to a global (or static) function defined in your own program. This may be useful in simple situations where your callback function does not need any context to perform its task; if your function needs to access some context, then this can only be achieved with global volatile variables, which is not a recommended practice.
  2. REGISTER_XXX_ISR_METHOD: this flavour defines an ISR for the *"XXX"* signal with callback to a member function of a class. The instance of this class must later be registered with interrupt::register_handler() so that the defined ISR can find it when it gets executed.

Note that some FastArduino API define a 3rd flavour of registration macro where you don't need to specify a callback function or method, because this callback is implicitly defined by the API, e.g. REGISTER_WATCHDOG_CLOCK_ISR() defines the necessary ISR for watchdog::Watchdog to be notified of watchdog timeouts so that it can update its internal clock counter and generate events.

The rationale behind this approach to register ISR and clallbacks is based on the following principles:

  • you decide what ISR you want to use, FastArduino will not impose it to you
  • you may override FastArduino default ISR if you need to
  • ISR callbacks may be any function or any method of any class: FastArduino will not force you to subclass one of its own classes in order to override some virtual method. Calling virtual methods from an ISR has an impact on the size and speed of this ISR (all AVR registers must be pushed to the stack even though only a few of them may be used by the overridden method).

Most macros defined in this file are normally not used directly by your programs, they are low-level and used by other higher-level macros defined in each FastArduino specific APIs. These macros are documented for the sake of completeness and in the rare cases where you might need them.

Definition in file interrupts.h.