Class PrescaledTimer
Defined in File sim_timer.h
Inheritance Relationships
Base Type
public CycleTimer(Class CycleTimer)
Class Documentation
-
class PrescaledTimer : public CycleTimer
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.
Public 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)
Configure the prescaler
- Parameters:
ps_max – Maximum value of the prescaler counter, making the prescaler counter wrap to 0
ps_factor – Prescaler factor to generate ticks. if = 0, the prescaler and timer stages are disabled and reset.
-
inline unsigned long prescaler_factor() const
Getter for ps_factor.
-
void set_timer_delay(cycle_count_t delay)
Sets the timeout delay to generate a event
Note
The prescaler stage is not affected by this setting.
- Parameters:
delay – Timeout delay in prescaler ticks. If = 0, the timer stage is disabled and reset.
-
inline cycle_count_t timer_delay() const
Getter for timer_delay.
-
void set_paused(bool paused)
Pause or resume the timer. If paused, the prescaler and timer stages are frozen but not reset.
- Parameters:
paused – true for pausing, false for resuming
-
void 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.
-
virtual cycle_count_t next(cycle_count_t when) override
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.
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.
- Parameters:
when – current ‘when’ cycle, at which the timer was scheduled
- Returns:
the next ‘when’ the timer requires to be called at.
-
void register_chained_timer(PrescaledTimer &timer)
Add a timer in the chain.
- Parameters:
timer – will be added as a child to this timer.
-
void unregister_chained_timer(PrescaledTimer &timer)
Remove a timer from the chain.
-
PrescaledTimer(const PrescaledTimer&) = delete
-
PrescaledTimer &operator=(const PrescaledTimer&) = delete
Public Static Functions
-
static cycle_count_t ticks_to_event(cycle_count_t counter, cycle_count_t event, cycle_count_t wrap)
Static helper to compute a timer delay for a particular counter to reach a value.