25#include <avr/interrupt.h>
44#define REGISTER_RTT_ISR(TIMER_NUM) \
45 ISR(CAT3(TIMER, TIMER_NUM, _COMPA_vect)) \
47 timer::isr_handler_rtt::rtt<TIMER_NUM>(); \
64#define REGISTER_RTT_ISR_METHOD(TIMER_NUM, HANDLER, CALLBACK) \
65 ISR(CAT3(TIMER, TIMER_NUM, _COMPA_vect)) \
67 timer::isr_handler_rtt::rtt_method<TIMER_NUM, HANDLER, CALLBACK>(); \
82#define REGISTER_RTT_ISR_FUNCTION(TIMER_NUM, CALLBACK) \
83 ISR(CAT3(TIMER, TIMER_NUM, _COMPA_vect)) \
85 timer::isr_handler_rtt::rtt_function<TIMER_NUM, CALLBACK>(); \
102#define REGISTER_RTT_EVENT_ISR(TIMER_NUM, EVENT, PERIOD) \
103 ISR(CAT3(TIMER, TIMER_NUM, _COMPA_vect)) \
105 timer::isr_handler_rtt::rtt_event<TIMER_NUM, EVENT, PERIOD>(); \
114#define DECL_RTT_ISR_HANDLERS_FRIEND \
115 friend struct timer::isr_handler_rtt; \
116 DECL_TIMER_COMP_FRIENDS
153 : millis_{millis}, counter_{counter}, max_counter_{max_counter}
169 return time::RTTTime{millis_, uint16_t(ONE_MILLI_32 * counter_ / (1UL + max_counter_))};
186 uint8_t buffer_[
sizeof(uint32_t) + 2 *
sizeof(T)];
205 template<board::Timer NTIMER_>
class RTT :
private Timer<NTIMER_>
253 synchronized return milliseconds_;
274 return milliseconds_;
313 synchronized return compute_micros();
336 return compute_micros();
355 synchronized return time_();
416 return RAW_TIME{milliseconds_, (
volatile TYPE&) TRAIT::TCNT, (
volatile TYPE&) TRAIT::OCRA};
504 volatile uint32_t milliseconds_ = 0UL;
511 using CALC = Calculator<NTIMER>;
513 static constexpr const TYPE MILLI_COUNTER =
CALC::CTC_counter(MILLI_PRESCALER, ONE_MILLI_32);
515 uint16_t compute_micros()
const
517 return uint16_t(ONE_MILLI_32 * ((
volatile TYPE&) TRAIT::TCNT) / (1UL + (
volatile TYPE&) TRAIT::OCRA));
520 friend struct isr_handler_rtt;
536 static_assert(events::Event_trait<EVENT>::IS_EVENT,
"EVENT type must be an events::Event<T>");
537 static_assert((PERIOD_MS & (PERIOD_MS - 1)) == 0,
"PERIOD_MS must be a power of 2");
556 void on_rtt_change(uint32_t millis)
563 friend struct isr_handler_rtt;
571 struct isr_handler_rtt
573 template<u
int8_t TIMER_NUM_>
static void rtt()
575 static constexpr board::Timer NTIMER = isr_handler::check_timer<TIMER_NUM_>();
576 interrupt::HandlerHolder<RTT<NTIMER>>::handler()->on_timer();
579 template<u
int8_t TIMER_NUM_,
typename HANDLER_,
void (HANDLER_::*CALLBACK_)(u
int32_t)>
static void rtt_method()
581 static constexpr board::Timer NTIMER = isr_handler::check_timer<TIMER_NUM_>();
582 auto handler = interrupt::HandlerHolder<RTT<NTIMER>>::handler();
584 interrupt::CallbackHandler<void (HANDLER_::*)(uint32_t), CALLBACK_>::call(handler->millis());
587 template<u
int8_t TIMER_NUM_,
void (*CALLBACK_)(u
int32_t)>
static void rtt_function()
589 static constexpr board::Timer NTIMER = isr_handler::check_timer<TIMER_NUM_>();
590 auto handler = interrupt::HandlerHolder<RTT<NTIMER>>::handler();
592 CALLBACK_(handler->millis());
595 template<u
int8_t TIMER_NUM_,
typename EVENT_, u
int32_t PERIOD_>
static void rtt_event()
597 static constexpr board::Timer NTIMER = isr_handler::check_timer<TIMER_NUM_>();
598 auto handler = interrupt::HandlerHolder<RTT<NTIMER>>::handler();
600 interrupt::HandlerHolder<RTTEventCallback<EVENT_, PERIOD_>>::handler()->on_rtt_change(handler->millis());
bool push_(TREF item)
Push item to the end of this queue, provided there is still available space in its ring buffer.
Structure used to hold a time value with microsecond precision.
Utility to generate events from an RTT instance at a given period.
RTTEventCallback(containers::Queue< EVENT > &event_queue)
Create a RTTEventCallback that will push periodic events to event_queue.
API to handle a real-time timer.
time::RTTTime time_() const
Elapsed time, in milliseconds and microseconds, since this timer has started.
uint16_t micros_() const
Compute the microseconds part (from 0 to 999) of the time that has elapsed, since this timer has star...
RAW_TIME raw_time() const
Elapsed time, in raw representation, since this timer has started.
Timer< NTIMER > & timer()
Get a reference to the underlying Timer of this RTT.
void end()
Stop this real-time timer, hence time gets not counted anymore.
void millis(uint32_t ms)
Reset the current milliseconds count of this RTT to the given value.
void begin_()
Start this real-time timer, hence elapsed time starts getting counted from then.
void end_()
Stop this real-time timer, hence time gets not counted anymore.
uint32_t millis() const
Elapsed time, in milliseconds, since this timer has started.
static constexpr const board::Timer NTIMER
The AVR timer used by this RTT.
void delay(uint32_t ms) const
Delay program execution for the given amount of milliseconds.
uint32_t millis_() const
Elapsed time, in milliseconds, since this timer has started.
void begin()
Start this real-time timer, hence elapsed time starts getting counted from then.
RTT()
Construct a new real-time timer handler and initializes its current time to 0ms.
uint16_t micros() const
Compute the microseconds part (from 0 to 999) of the time that has elapsed, since this timer has star...
time::RTTTime time() const
Elapsed time, in milliseconds and microseconds, since this timer has started.
RAW_TIME raw_time_() const
Elapsed time, in raw representation, since this timer has started.
Utility class to avoid costly instantiation of time::RTTTime from an interrupt routine.
time::RTTTime as_real_time() const
Convert this RTTRawTime instance to a fully usable time::RTTTime.
RTTRawTime(uint32_t millis, T counter, T max_counter)
Create a new RTTRawTime from millis milliseconds, and values from Timer counter value that will allow...
static constexpr RTTRawTime< T > EMPTY_TIME
A constant to signify "no time".
General API to handle an AVR timer.
typename TRAIT::TYPE TYPE
The type of this timer's counter (either uint8_t or uint16_t).
typename PRESCALERS_TRAIT::TYPE PRESCALER
The enum type listing all available precaler values for this timer.
void end_()
Completely stop this timer: timer interrupts are disabled and counter is stopped.
void end()
Completely stop this timer: timer interrupts are disabled and counter is stopped.
void begin_(TYPE max=0)
Start this timer in the currently selected mode, with the provided prescaler value and max value.
Support for events management.
General API for handling AVR interrupt vectors.
Timer
Defines all timers available for target MCU.
const uint8_t RTT_TIMER
Type of events generated by timer::RTTEventCallback whenever elapsed RTT::millis() reaches a multiple...
void register_handler(Handler &handler)
Register a class instance containing methods that shall be called back by an ISR.
void yield()
Utility method used by many FastArduino API in order to "yield" some processor time; concretely it ju...
Defines all API to manipulate AVR Timers.
@ CTC
Timer "Clear Timer on Compare match" mode: counter is incremented until it reaches "TOP" (OCRxA regis...
@ OUTPUT_COMPARE_A
This interrupt occurs when the counter reached OCRA.
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,...
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...