FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
timer.h File Reference

Timer API. More...

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

Go to the source code of this file.

Classes

struct  timer::Calculator< NTIMER_ >
 Defines a set of calculation methods for the given NTIMER_ The behavior of these methods is specific to each AVR Timer are there can be many important differences between 2 timers on AVR. More...
 
class  timer::Timer< NTIMER_ >
 General API to handle an AVR timer. More...
 

Namespaces

namespace  timer
 Defines all API to manipulate AVR Timers.
 

Macros

#define REGISTER_TIMER_COMPARE_ISR_METHOD(TIMER_NUM, HANDLER, CALLBACK)
 Register the necessary ISR (Interrupt Service Routine) for a timer::Timer with a callback method in CTC mode. More...
 
#define REGISTER_TIMER_COMPARE_ISR_FUNCTION(TIMER_NUM, CALLBACK)
 Register the necessary ISR (Interrupt Service Routine) for a timer::Timer with a callback function in CTC mode. More...
 
#define REGISTER_TIMER_COMPARE_ISR_EMPTY(TIMER_NUM)
 Register an empty ISR (Interrupt Service Routine) for a timer::Timer. More...
 
#define REGISTER_TIMER_OVERFLOW_ISR_METHOD(TIMER_NUM, HANDLER, CALLBACK)
 Register the necessary ISR (Interrupt Service Routine) for the Counter Overflow of a timer::Timer. More...
 
#define REGISTER_TIMER_OVERFLOW_ISR_FUNCTION(TIMER_NUM, CALLBACK)
 Register the necessary ISR (Interrupt Service Routine) for the Counter Overflow of a timer::Timer. More...
 
#define REGISTER_TIMER_OVERFLOW_ISR_EMPTY(TIMER_NUM)
 Register an empty ISR (Interrupt Service Routine) for the Counter Overflow of a timer::Timer. More...
 
#define REGISTER_TIMER_CAPTURE_ISR_METHOD(TIMER_NUM, HANDLER, CALLBACK)
 Register the necessary ISR (Interrupt Service Routine) for the Input Capture of a timer::Timer. More...
 
#define REGISTER_TIMER_CAPTURE_ISR_FUNCTION(TIMER_NUM, CALLBACK)
 Register the necessary ISR (Interrupt Service Routine) for the Input Capture of a timer::Timer. More...
 
#define REGISTER_TIMER_CAPTURE_ISR_EMPTY(TIMER_NUM)
 Register an empty ISR (Interrupt Service Routine) for the Input Capture of a timer::Timer. More...
 
#define DECL_TIMER_ISR_HANDLERS_FRIEND
 This macro shall be used in a class containing a private callback method, registered by REGISTER_TIMER_COMPARE_ISR_METHOD, REGISTER_TIMER_OVERFLOW_ISR_METHOD or REGISTER_TIMER_CAPTURE_ISR_METHOD. More...
 

Enumerations

enum class  timer::TimerMode : uint8_t {
  timer::NORMAL ,
  timer::CTC ,
  timer::FAST_PWM ,
  timer::PHASE_CORRECT_PWM
}
 Defines the mode of operation of a timer. More...
 
enum class  timer::TimerInterrupt : uint8_t {
  timer::OVERFLOW = board_traits::TimerInterrupt::OVERFLOW ,
  timer::OUTPUT_COMPARE_A = board_traits::TimerInterrupt::OUTPUT_COMPARE_A ,
  timer::OUTPUT_COMPARE_B = board_traits::TimerInterrupt::OUTPUT_COMPARE_B ,
  timer::OUTPUT_COMPARE_C = board_traits::TimerInterrupt::OUTPUT_COMPARE_C ,
  timer::INPUT_CAPTURE = board_traits::TimerInterrupt::INPUT_CAPTURE
}
 Defines the interrupts that can be handled by a timer. More...
 
enum class  timer::TimerOutputMode : uint8_t {
  timer::DISCONNECTED ,
  timer::TOGGLE ,
  timer::NON_INVERTING ,
  timer::INVERTING
}
 Defines the "connection" between this timer and specific PWM output pins. More...
 
enum class  timer::TimerInputCapture : uint8_t {
  timer::RISING_EDGE ,
  timer::FALLING_EDGE
}
 Defines the type of input capture we want for a timer. More...
 

Functions

constexpr TimerInterrupt timer::operator| (TimerInterrupt i1, TimerInterrupt i2)
 Combine 2 timer interrupts for use with Timer.set_interrupts(). More...
 

Detailed Description

Timer API.

Definition in file timer.h.

Macro Definition Documentation

◆ REGISTER_TIMER_COMPARE_ISR_METHOD

#define REGISTER_TIMER_COMPARE_ISR_METHOD (   TIMER_NUM,
  HANDLER,
  CALLBACK 
)
Value:
ISR(CAT3(TIMER, TIMER_NUM, _COMPA_vect)) \
{ \
timer::isr_handler::check_timer<TIMER_NUM>(); \
interrupt::CallbackHandler<void (HANDLER::*)(), CALLBACK>::call(); \
}

Register the necessary ISR (Interrupt Service Routine) for a timer::Timer with a callback method in CTC mode.

Parameters
TIMER_NUMthe number of the TIMER feature for the target MCU
HANDLERthe class holding the callback method
CALLBACKthe method of HANDLER that will be called when the interrupt is triggered; this must be a proper PTMF (pointer to member function).

Definition at line 40 of file timer.h.

◆ REGISTER_TIMER_COMPARE_ISR_FUNCTION

#define REGISTER_TIMER_COMPARE_ISR_FUNCTION (   TIMER_NUM,
  CALLBACK 
)
Value:
ISR(CAT3(TIMER, TIMER_NUM, _COMPA_vect)) \
{ \
timer::isr_handler::check_timer<TIMER_NUM>(); \
interrupt::CallbackHandler<void (*)(), CALLBACK>::call(); \
}

Register the necessary ISR (Interrupt Service Routine) for a timer::Timer with a callback function in CTC mode.

Parameters
TIMER_NUMthe number of the TIMER feature for the target MCU
CALLBACKthe function that will be called when the interrupt is triggered

Definition at line 54 of file timer.h.

◆ REGISTER_TIMER_COMPARE_ISR_EMPTY

#define REGISTER_TIMER_COMPARE_ISR_EMPTY (   TIMER_NUM)
Value:
extern "C" void CAT3(TIMER, TIMER_NUM, _COMPA_vect)(void) NAKED_SIGNAL; \
void CAT3(TIMER, TIMER_NUM, _COMPA_vect)(void) \
{ \
timer::isr_handler::check_timer<TIMER_NUM>(); \
__asm__ __volatile__("reti" ::); \
}
#define NAKED_SIGNAL
Specific GCC attribute for AVR target, declaring a signal handler (aka ISR, or Interrupt Service Rout...
Definition: defines.h:91

Register an empty ISR (Interrupt Service Routine) for a timer::Timer.

This may be needed when using timer CTC mode but when you don't need any callback.

Parameters
TIMER_NUMthe number of the TIMER feature for the target MCU

Definition at line 67 of file timer.h.

◆ REGISTER_TIMER_OVERFLOW_ISR_METHOD

#define REGISTER_TIMER_OVERFLOW_ISR_METHOD (   TIMER_NUM,
  HANDLER,
  CALLBACK 
)
Value:
ISR(CAT3(TIMER, TIMER_NUM, _OVF_vect)) \
{ \
timer::isr_handler::check_timer<TIMER_NUM>(); \
interrupt::CallbackHandler<void (HANDLER::*)(), CALLBACK>::call(); \
}

Register the necessary ISR (Interrupt Service Routine) for the Counter Overflow of a timer::Timer.

Parameters
TIMER_NUMthe number of the TIMER feature for the target MCU
HANDLERthe class holding the callback method
CALLBACKthe method of HANDLER that will be called when the interrupt is triggered; this must be a proper PTMF (pointer to member function).

Definition at line 83 of file timer.h.

◆ REGISTER_TIMER_OVERFLOW_ISR_FUNCTION

#define REGISTER_TIMER_OVERFLOW_ISR_FUNCTION (   TIMER_NUM,
  CALLBACK 
)
Value:
ISR(CAT3(TIMER, TIMER_NUM, _OVF_vect)) \
{ \
timer::isr_handler::check_timer<TIMER_NUM>(); \
interrupt::CallbackHandler<void (*)(), CALLBACK>::call(); \
}

Register the necessary ISR (Interrupt Service Routine) for the Counter Overflow of a timer::Timer.

Parameters
TIMER_NUMthe number of the TIMER feature for the target MCU
CALLBACKthe function that will be called when the interrupt is triggered

Definition at line 97 of file timer.h.

◆ REGISTER_TIMER_OVERFLOW_ISR_EMPTY

#define REGISTER_TIMER_OVERFLOW_ISR_EMPTY (   TIMER_NUM)
Value:
extern "C" void CAT3(TIMER, TIMER_NUM, _OVF_vect)(void) NAKED_SIGNAL; \
void CAT3(TIMER, TIMER_NUM, _OVF_vect)(void) \
{ \
timer::isr_handler::check_timer<TIMER_NUM>(); \
__asm__ __volatile__("reti" ::); \
}

Register an empty ISR (Interrupt Service Routine) for the Counter Overflow of a timer::Timer.

This would normally not be needed.

Parameters
TIMER_NUMthe number of the TIMER feature for the target MCU

Definition at line 110 of file timer.h.

◆ REGISTER_TIMER_CAPTURE_ISR_METHOD

#define REGISTER_TIMER_CAPTURE_ISR_METHOD (   TIMER_NUM,
  HANDLER,
  CALLBACK 
)
Value:
ISR(CAT3(TIMER, TIMER_NUM, _CAPT_vect)) \
{ \
timer::isr_handler::timer_capture_method<TIMER_NUM, HANDLER, CALLBACK>(); \
}

Register the necessary ISR (Interrupt Service Routine) for the Input Capture of a timer::Timer.

Parameters
TIMER_NUMthe number of the TIMER feature for the target MCU
HANDLERthe class holding the callback method
CALLBACKthe method of HANDLER that will be called when the interrupt is triggered; this must be a proper PTMF (pointer to member function).

Definition at line 126 of file timer.h.

◆ REGISTER_TIMER_CAPTURE_ISR_FUNCTION

#define REGISTER_TIMER_CAPTURE_ISR_FUNCTION (   TIMER_NUM,
  CALLBACK 
)
Value:
ISR(CAT3(TIMER, TIMER_NUM, _CAPT_vect)) \
{ \
timer::isr_handler::timer_capture_function<TIMER_NUM, CALLBACK>(); \
}

Register the necessary ISR (Interrupt Service Routine) for the Input Capture of a timer::Timer.

Parameters
TIMER_NUMthe number of the TIMER feature for the target MCU
CALLBACKthe function that will be called when the interrupt is triggered

Definition at line 139 of file timer.h.

◆ REGISTER_TIMER_CAPTURE_ISR_EMPTY

#define REGISTER_TIMER_CAPTURE_ISR_EMPTY (   TIMER_NUM)
Value:
extern "C" void CAT3(TIMER, TIMER_NUM, _CAPT_vect)(void) NAKED_SIGNAL; \
void CAT3(TIMER, TIMER_NUM, _CAPT_vect)(void) \
{ \
timer::isr_handler::check_timer_capture<TIMER_NUM>(); \
__asm__ __volatile__("reti" ::); \
}

Register an empty ISR (Interrupt Service Routine) for the Input Capture of a timer::Timer.

This would normally not be needed.

Parameters
TIMER_NUMthe number of the TIMER feature for the target MCU

Definition at line 151 of file timer.h.

◆ DECL_TIMER_ISR_HANDLERS_FRIEND

#define DECL_TIMER_ISR_HANDLERS_FRIEND
Value:
friend struct timer::isr_handler; \
DECL_TIMER_COMP_FRIENDS \
DECL_TIMER_OVF_FRIENDS \
DECL_TIMER_CAPT_FRIENDS

This macro shall be used in a class containing a private callback method, registered by REGISTER_TIMER_COMPARE_ISR_METHOD, REGISTER_TIMER_OVERFLOW_ISR_METHOD or REGISTER_TIMER_CAPTURE_ISR_METHOD.

It declares the class where it is used as a friend of all necessary functions so that the private callback method can be called properly.

Definition at line 166 of file timer.h.