FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
timer::Calculator< NTIMER_ > Struct Template Reference

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...

#include <fastarduino/timer.h>

Public Types

using TYPE = typename TRAIT::TYPE
 The timer type: either uint8_t or uint16_t; this is defined and not changeable for each Timer. More...
 
using PRESCALER = typename PRESCALERS_TRAIT::TYPE
 The type (enum) of the possible prescaler values for this timer; this is defined and not changeable for each Timer. More...
 

Static Public Member Functions

static constexpr PRESCALER tick_prescaler (uint32_t us_per_tick)
 Computes the ideal prescaler to use for this timer, in order to generate timer ticks of us_per_tick microseconds or less. More...
 
static constexpr uint32_t ticks_to_us (PRESCALER prescaler, TYPE ticks)
 Computes the number of microseconds reached for a given number of ticks with a given prescaler. More...
 
static constexpr TYPE us_to_ticks (PRESCALER prescaler, uint32_t us)
 Computes the number of ticks needed for reaching the given us microseconds with a given prescaler. More...
 
static constexpr PRESCALER CTC_prescaler (uint32_t us)
 Computes the ideal prescaler value to use for this timer, in TimerMode::CTC mode, in order to be able to count up to us microseconds. More...
 
static constexpr uint32_t CTC_frequency (PRESCALER prescaler)
 Computes the frequency at which this timer would perform, in TimerMode::CTC mode, if it was using prescaler. More...
 
static constexpr TYPE CTC_counter (PRESCALER prescaler, uint32_t us)
 Computes the value of counter to use for this timer, in TimerMode::CTC mode, with prescaler, in order to reach us microseconds. More...
 
static constexpr bool is_adequate_for_CTC (PRESCALER prescaler, uint32_t us)
 Verifies that the given prescaler prescaler is suitable for this timer in TimerMode::CTC in order to be able to reach us microseconds. More...
 
static constexpr PRESCALER FastPWM_prescaler (uint16_t pwm_frequency)
 Computes the ideal prescaler value to use for this timer, in TimerMode::FAST_PWM mode, in order to be able to reach at least pwm_frequency. More...
 
static constexpr uint16_t FastPWM_frequency (PRESCALER prescaler)
 Computes the frequency at which this timer would perform, in TimerMode::FAST_PWM mode, if it was using prescaler. More...
 
static constexpr PRESCALER PhaseCorrectPWM_prescaler (uint16_t pwm_frequency)
 Computes the ideal prescaler value to use for this timer, in TimerMode::PHASE_CORRECT_PWM mode, in order to be able to reach at least pwm_frequency. More...
 
static constexpr uint16_t PhaseCorrectPWM_frequency (PRESCALER prescaler)
 Computes the frequency at which this timer would perform, in TimerMode::PHASE_CORRECT_PWM mode, if it was using prescaler. More...
 
static constexpr PRESCALER PulseTimer_prescaler (uint16_t max_pulse_width_us, uint16_t pulse_frequency)
 Computes the ideal prescaler value to use for this timer, when used in timer::PulseTimer, in order to be able to reach at least pulse_frequency with a maximum pulse width of max_pulse_width_us. More...
 
static constexpr TYPE PulseTimer_value (PRESCALER prescaler, uint16_t period_us)
 Computes the ideal value to use for this timer, when used in timer::PulseTimer, in order to produce a pulse of the required width period_us with the given prescaler. More...
 
static constexpr PRESCALER PWM_ICR_prescaler (uint16_t pwm_frequency)
 Computes the ideal prescaler value to use for this timer, when used in timer::PulseTimer, in order to be able to reach at least pulse_frequency with a maximum pulse width of max_pulse_width_us. More...
 
static constexpr uint16_t PWM_ICR_frequency (PRESCALER prescaler, uint16_t counter)
 Computes the frequency at which this timer would perform, when used in timer::PulseTimer, if it was using prescaler and counter. More...
 
static constexpr uint16_t PWM_ICR_counter (PRESCALER prescaler, uint16_t pwm_frequency)
 Computes the ideal counter TOP value to use for this timer, when used in timer::PulseTimer, in order to be able to reach at least pulse_frequency with the given prescaler. More...
 

Static Public Attributes

static constexpr const board::Timer NTIMER = NTIMER_
 The timer for which calculation methods are provided. More...
 
static constexpr const TYPE PWM_MAX = TRAIT::MAX_PWM
 The maximum value you can use for this timer in PWM modes: using this value will set PWM duty as 100%. More...
 

Detailed Description

template<board::Timer NTIMER_>
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.

Template Parameters
NTIMER_the timer for which we need calculation methods
See also
board::Timer

Definition at line 301 of file timer.h.

Member Typedef Documentation

◆ TYPE

template<board::Timer NTIMER_>
using timer::Calculator< NTIMER_ >::TYPE = typename TRAIT::TYPE

The timer type: either uint8_t or uint16_t; this is defined and not changeable for each Timer.

Timer counter and max values have this type.

Definition at line 319 of file timer.h.

◆ PRESCALER

template<board::Timer NTIMER_>
using timer::Calculator< NTIMER_ >::PRESCALER = typename PRESCALERS_TRAIT::TYPE

The type (enum) of the possible prescaler values for this timer; this is defined and not changeable for each Timer.

Definition at line 324 of file timer.h.

Member Function Documentation

◆ tick_prescaler()

template<board::Timer NTIMER_>
static constexpr PRESCALER timer::Calculator< NTIMER_ >::tick_prescaler ( uint32_t  us_per_tick)
inlinestaticconstexpr

Computes the ideal prescaler to use for this timer, in order to generate timer ticks of us_per_tick microseconds or less.

Note this is a constexpr method, i.e. it allows compile-time computation when provided a constant argument.

Parameters
us_per_tickthe maximum number of us to be reached by one counter tick
Returns
the best prescaler to use

Definition at line 341 of file timer.h.

◆ ticks_to_us()

template<board::Timer NTIMER_>
static constexpr uint32_t timer::Calculator< NTIMER_ >::ticks_to_us ( PRESCALER  prescaler,
TYPE  ticks 
)
inlinestaticconstexpr

Computes the number of microseconds reached for a given number of ticks with a given prescaler.

Note this is a constexpr method, i.e. it allows compile-time computation when provided a constant argument.

Parameters
prescalerthe prescaler used for this calculation
ticksthe number of ticks for which we want to know the number of microseconds elapsed
Returns
the number of microseconds elapsed for ticks with prescaler

Definition at line 357 of file timer.h.

◆ us_to_ticks()

template<board::Timer NTIMER_>
static constexpr TYPE timer::Calculator< NTIMER_ >::us_to_ticks ( PRESCALER  prescaler,
uint32_t  us 
)
inlinestaticconstexpr

Computes the number of ticks needed for reaching the given us microseconds with a given prescaler.

Note this is a constexpr method, i.e. it allows compile-time computation when provided a constant argument.

Parameters
prescalerthe prescaler used for this calculation
usthe number of microseconds we want to reach
Returns
the number of ticks needed in order to reach us microseconds elapsed

Definition at line 373 of file timer.h.

◆ CTC_prescaler()

template<board::Timer NTIMER_>
static constexpr PRESCALER timer::Calculator< NTIMER_ >::CTC_prescaler ( uint32_t  us)
inlinestaticconstexpr

Computes the ideal prescaler value to use for this timer, in TimerMode::CTC mode, in order to be able to count up to us microseconds.

Note this is a constexpr method, i.e. it allows compile-time computation when provided a constant argument.

Parameters
usthe number of microseconds we want to be able to count up to
Returns
the best prescaler to use
See also
CTC_counter()
CTC_frequency()

Definition at line 390 of file timer.h.

◆ CTC_frequency()

template<board::Timer NTIMER_>
static constexpr uint32_t timer::Calculator< NTIMER_ >::CTC_frequency ( PRESCALER  prescaler)
inlinestaticconstexpr

Computes the frequency at which this timer would perform, in TimerMode::CTC mode, if it was using prescaler.

Note this is a constexpr method, i.e. it allows compile-time computation when provided a constant argument.

Parameters
prescalerthe prescaler value for which to compute timer frequency
Returns
timer frequency when using prescaler
See also
CTC_prescaler()

Definition at line 406 of file timer.h.

◆ CTC_counter()

template<board::Timer NTIMER_>
static constexpr TYPE timer::Calculator< NTIMER_ >::CTC_counter ( PRESCALER  prescaler,
uint32_t  us 
)
inlinestaticconstexpr

Computes the value of counter to use for this timer, in TimerMode::CTC mode, with prescaler, in order to reach us microseconds.

Note this is a constexpr method, i.e. it allows compile-time computation when provided constant arguments.

Parameters
prescalerthe prescaler used for this timer
usthe number of microseconds to reach with this timer in CTC mode
Returns
the MAX value to be used in order to reach us
See also
CTC_prescaler()
Timer::begin(PRESCALER, TYPE)

Definition at line 423 of file timer.h.

◆ is_adequate_for_CTC()

template<board::Timer NTIMER_>
static constexpr bool timer::Calculator< NTIMER_ >::is_adequate_for_CTC ( PRESCALER  prescaler,
uint32_t  us 
)
inlinestaticconstexpr

Verifies that the given prescaler prescaler is suitable for this timer in TimerMode::CTC in order to be able to reach us microseconds.

This is normally not needed but can be helpful in a static_assert in your code. Note this is a constexpr method, i.e. it allows compile-time computation when provided constant arguments. You could use this method in static_assert() in order to check that the requested period us is OK for this timer.

Parameters
prescalerthe prescaler used for this timer
usthe number of microseconds to reach with this timer in CTC mode
Return values
`true`if prescaler and us are adequate for TIMER
`false`if prescaler and us are NOT adequate for TIMER
See also
CTC_prescaler()
CTC_counter()

Definition at line 445 of file timer.h.

◆ FastPWM_prescaler()

template<board::Timer NTIMER_>
static constexpr PRESCALER timer::Calculator< NTIMER_ >::FastPWM_prescaler ( uint16_t  pwm_frequency)
inlinestaticconstexpr

Computes the ideal prescaler value to use for this timer, in TimerMode::FAST_PWM mode, in order to be able to reach at least pwm_frequency.

Note this is a constexpr method, i.e. it allows compile-time computation when provided a constant argument.

Parameters
pwm_frequencythe PWM frequency we want to reach
Returns
the best prescaler to use
See also
FastPWM_frequency()

Definition at line 461 of file timer.h.

◆ FastPWM_frequency()

template<board::Timer NTIMER_>
static constexpr uint16_t timer::Calculator< NTIMER_ >::FastPWM_frequency ( PRESCALER  prescaler)
inlinestaticconstexpr

Computes the frequency at which this timer would perform, in TimerMode::FAST_PWM mode, if it was using prescaler.

Note this is a constexpr method, i.e. it allows compile-time computation when provided a constant argument.

Parameters
prescalerthe prescaler value for which to compute timer frequency
Returns
timer frequency when using prescaler
See also
FastPWM_prescaler()

Definition at line 477 of file timer.h.

◆ PhaseCorrectPWM_prescaler()

template<board::Timer NTIMER_>
static constexpr PRESCALER timer::Calculator< NTIMER_ >::PhaseCorrectPWM_prescaler ( uint16_t  pwm_frequency)
inlinestaticconstexpr

Computes the ideal prescaler value to use for this timer, in TimerMode::PHASE_CORRECT_PWM mode, in order to be able to reach at least pwm_frequency.

Note this is a constexpr method, i.e. it allows compile-time computation when provided a constant argument.

Parameters
pwm_frequencythe PWM frequency we want to reach
Returns
the best prescaler to use
See also
PhaseCorrectPWM_frequency()

Definition at line 493 of file timer.h.

◆ PhaseCorrectPWM_frequency()

template<board::Timer NTIMER_>
static constexpr uint16_t timer::Calculator< NTIMER_ >::PhaseCorrectPWM_frequency ( PRESCALER  prescaler)
inlinestaticconstexpr

Computes the frequency at which this timer would perform, in TimerMode::PHASE_CORRECT_PWM mode, if it was using prescaler.

Note this is a constexpr method, i.e. it allows compile-time computation when provided a constant argument.

Parameters
prescalerthe prescaler value for which to compute timer frequency
Returns
timer frequency when using prescaler
See also
PhaseCorrectPWM_prescaler()

Definition at line 509 of file timer.h.

◆ PulseTimer_prescaler()

template<board::Timer NTIMER_>
static constexpr PRESCALER timer::Calculator< NTIMER_ >::PulseTimer_prescaler ( uint16_t  max_pulse_width_us,
uint16_t  pulse_frequency 
)
inlinestaticconstexpr

Computes the ideal prescaler value to use for this timer, when used in timer::PulseTimer, in order to be able to reach at least pulse_frequency with a maximum pulse width of max_pulse_width_us.

Note this is a constexpr method, i.e. it allows compile-time computation when provided a constant argument.

Parameters
max_pulse_width_usthe maximum pulse width, in microseconds, to produce
pulse_frequencythe pulse frequency we want to reach
Returns
the best prescaler to use
See also
PulseTimer_value()

Definition at line 527 of file timer.h.

◆ PulseTimer_value()

template<board::Timer NTIMER_>
static constexpr TYPE timer::Calculator< NTIMER_ >::PulseTimer_value ( PRESCALER  prescaler,
uint16_t  period_us 
)
inlinestaticconstexpr

Computes the ideal value to use for this timer, when used in timer::PulseTimer, in order to produce a pulse of the required width period_us with the given prescaler.

Note this is a constexpr method, i.e. it allows compile-time computation when provided a constant argument.

Note that this method is directly called as needed by timer::PulseTimer, hence you generally won't need to call it yourself.

Parameters
prescalerthe prescaler value for which to compute value
period_usthe pulse width, in microseconds, to produce
Returns
the value to use to produce the required pulse
See also
PulseTimer_prescaler()

Definition at line 547 of file timer.h.

◆ PWM_ICR_prescaler()

template<board::Timer NTIMER_>
static constexpr PRESCALER timer::Calculator< NTIMER_ >::PWM_ICR_prescaler ( uint16_t  pwm_frequency)
inlinestaticconstexpr

Computes the ideal prescaler value to use for this timer, when used in timer::PulseTimer, in order to be able to reach at least pulse_frequency with a maximum pulse width of max_pulse_width_us.

This method applies only to 16-bits timers, for which PulseTimer uses FastPWM mode with register ICR as TOP value. Note this is a constexpr method, i.e. it allows compile-time computation when provided a constant argument.

Note that this method is directly called as needed by timer::PulseTimer, hence you generally won't need to call it yourself.

Parameters
pwm_frequencythe pulse frequency we want to reach
Returns
the best prescaler to use
See also
PWM_ICR_frequency()
PWM_ICR_counter()

Definition at line 569 of file timer.h.

◆ PWM_ICR_frequency()

template<board::Timer NTIMER_>
static constexpr uint16_t timer::Calculator< NTIMER_ >::PWM_ICR_frequency ( PRESCALER  prescaler,
uint16_t  counter 
)
inlinestaticconstexpr

Computes the frequency at which this timer would perform, when used in timer::PulseTimer, if it was using prescaler and counter.

This method applies only to 16-bits timers, for which PulseTimer uses FastPWM mode with register ICR as TOP value.

Note this is a constexpr method, i.e. it allows compile-time computation when provided a constant argument.

Parameters
prescalerthe prescaler value for which to compute timer frequency
counterthe counter value for which to compute timer frequency
Returns
timer frequency
See also
PWM_ICR_prescaler()
PWM_ICR_counter()

Definition at line 591 of file timer.h.

◆ PWM_ICR_counter()

template<board::Timer NTIMER_>
static constexpr uint16_t timer::Calculator< NTIMER_ >::PWM_ICR_counter ( PRESCALER  prescaler,
uint16_t  pwm_frequency 
)
inlinestaticconstexpr

Computes the ideal counter TOP value to use for this timer, when used in timer::PulseTimer, in order to be able to reach at least pulse_frequency with the given prescaler.

This method applies only to 16-bits timers, for which PulseTimer uses FastPWM mode with register ICR as TOP value.

Note this is a constexpr method, i.e. it allows compile-time computation when provided a constant argument.

Parameters
prescalerthe prescaler value for which to compute timer frequency
pwm_frequencythe pulse frequency we want to reach
Returns
ideal value to set for ICR register
See also
PWM_ICR_prescaler()
PWM_ICR_frequency()

Definition at line 613 of file timer.h.

Member Data Documentation

◆ NTIMER

template<board::Timer NTIMER_>
constexpr const board::Timer timer::Calculator< NTIMER_ >::NTIMER = NTIMER_
staticconstexpr

The timer for which calculation methods are provided.

Definition at line 307 of file timer.h.

◆ PWM_MAX

template<board::Timer NTIMER_>
constexpr const TYPE timer::Calculator< NTIMER_ >::PWM_MAX = TRAIT::MAX_PWM
staticconstexpr

The maximum value you can use for this timer in PWM modes: using this value will set PWM duty as 100%.

Definition at line 329 of file timer.h.


The documentation for this struct was generated from the following file: