PrescaledTimer Class Reference

yasimavr: PrescaledTimer Class Reference
yasimavr
Loading...
Searching...
No Matches

Generic model of a Timer with prescaling. More...

#include <sim_timer.h>

Inheritance diagram for PrescaledTimer:
CycleTimer

Public Member Functions

 PrescaledTimer ()
 
virtual ~PrescaledTimer ()
 
void init (CycleManager &cycle_manager, Logger &logger)
 Initialise the timer, must be called once during initialisation phases.
 
void reset ()
 Reset the timer. Both stages are reset and disabled.
 
void set_prescaler (unsigned long ps_max, unsigned long ps_factor)
 
unsigned long prescaler_factor () const
 Getter for ps_factor.
 
void set_timer_delay (cycle_count_t delay)
 
cycle_count_t timer_delay () const
 Getter for timer_delay.
 
void set_paused (bool paused)
 
void update ()
 
virtual cycle_count_t next (cycle_count_t when) override
 Callback from the cycle loop.
 
Signalsignal ()
 Getter for the signal raised with counter updates.
 
void register_chained_timer (PrescaledTimer &timer)
 
void unregister_chained_timer (PrescaledTimer &timer)
 
 PrescaledTimer (const PrescaledTimer &)=delete
 
PrescaledTimeroperator= (const PrescaledTimer &)=delete
 
- Public Member Functions inherited from CycleTimer
 CycleTimer ()
 
 CycleTimer (const CycleTimer &other)
 
virtual ~CycleTimer ()
 
bool scheduled () const
 Returns true if this timer is scheduled with a manager.
 
bool paused () const
 
cycle_count_t remaining_delay () const
 
CycleTimeroperator= (const CycleTimer &other)
 

Static Public Member Functions

static cycle_count_t ticks_to_event (cycle_count_t counter, cycle_count_t event, cycle_count_t wrap)
 

Detailed Description

Generic model of a Timer with prescaling.

Implementation of a clock cycle timer, used by peripherals such as TCx, WDT, RTC.

It is structured with two consecutive stages:

  • Prescaler
  • Timer

The prescaler works as a counter of simulated clock cycles, starting at 0, wrapping at 'ps_max', and generating timer 'ticks' once every 'ps_factor' cycles. The timer generates a timeout signal after a delay given in prescaler ticks.

The timeout is transmitted though a Signal, available via signal() and raised in 2 ways:

  • When the programmed timeout delay is reached.
  • When update() is called, and enough clock cycles have passed, resulting in at least one tick.

If, during the update, the number of generated ticks is enough to reach the timer delay, the signal index is set to 1, otherwise it is set to 0. The signal data field is set to the generated tick count.

Timers can be daisy-chained, so that the prescaler tick output of a timer feeds into the prescaler clock input of another.

Constructor & Destructor Documentation

◆ PrescaledTimer() [1/2]

YASIMAVR_USING_NAMESPACE PrescaledTimer::PrescaledTimer ( )

◆ ~PrescaledTimer()

PrescaledTimer::~PrescaledTimer ( )
virtual

◆ PrescaledTimer() [2/2]

PrescaledTimer::PrescaledTimer ( const PrescaledTimer )
delete

Member Function Documentation

◆ init()

void PrescaledTimer::init ( CycleManager cycle_manager,
Logger logger 
)

Initialise the timer, must be called once during initialisation phases.

◆ next()

cycle_count_t PrescaledTimer::next ( cycle_count_t  when)
overridevirtual

Callback from the cycle loop.

Note
there's no guarantee the method will be called exactly on the required 'when' cycle. The only guarantee is "called 'when' <= 'current cycle'", the implementations must account for this.
Parameters
whencurrent 'when' cycle, at which the timer was scheduled
Returns
the next 'when' the timer requires to be called at.
Note
The next 'when' can be in the 'past' (i.e. <= 'current cycle'). In this case, the timer will be called again within the same cycle with the given next 'when'. The only constraint is that it must be greater than the previous 'when'. If it's negative or zero, the timer is removed from the queue.

Implements CycleTimer.

◆ operator=()

PrescaledTimer & PrescaledTimer::operator= ( const PrescaledTimer )
delete

◆ prescaler_factor()

unsigned long PrescaledTimer::prescaler_factor ( ) const
inline

Getter for ps_factor.

◆ register_chained_timer()

void PrescaledTimer::register_chained_timer ( PrescaledTimer timer)

Add a timer in the chain.

Parameters
timerwill be added as a child to this timer.

◆ reset()

void PrescaledTimer::reset ( )

Reset the timer. Both stages are reset and disabled.

◆ set_paused()

void PrescaledTimer::set_paused ( bool  paused)

Pause or resume the timer. If paused, the prescaler and timer stages are frozen but not reset.

Parameters
pausedtrue for pausing, false for resuming

◆ set_prescaler()

void PrescaledTimer::set_prescaler ( unsigned long  ps_max,
unsigned long  ps_factor 
)

Configure the prescaler

Parameters
ps_maxMaximum value of the prescaler counter, making the prescaler counter wrap to 0
ps_factorPrescaler factor to generate ticks. if = 0, the prescaler and timer stages are disabled and reset.

◆ set_timer_delay()

void PrescaledTimer::set_timer_delay ( cycle_count_t  delay)

Sets the timeout delay to generate a event

Parameters
delayTimeout delay in prescaler ticks. If = 0, the timer stage is disabled and reset.
Note
The prescaler stage is not affected by this setting.

◆ signal()

Signal & PrescaledTimer::signal ( )
inline

Getter for the signal raised with counter updates.

◆ ticks_to_event()

cycle_count_t PrescaledTimer::ticks_to_event ( cycle_count_t  counter,
cycle_count_t  event,
cycle_count_t  wrap 
)
static

Static helper to compute a timer delay for a particular counter to reach a value.

◆ timer_delay()

cycle_count_t PrescaledTimer::timer_delay ( ) const
inline

Getter for timer_delay.

◆ unregister_chained_timer()

void PrescaledTimer::unregister_chained_timer ( PrescaledTimer timer)

Remove a timer from the chain.

◆ update()

void PrescaledTimer::update ( )

Update the timer to catchup with the last completed cycle. Ticks may be generated and the signal may be raised if enough cycles have passed.

If the timer is a child of another timer, the update call is passed on to the parent.


The documentation for this class was generated from the following files: