pub struct Timer { /* private fields */ }Expand description
A 64-bit timer based on SysTick.
Stores wraparounds in 2 32-bit atomics. Scales the systick counts to arbitrary frequency.
Implementations§
Source§impl Timer
impl Timer
Sourcepub fn systick_handler(&self)
pub fn systick_handler(&self)
SysTick handler.
Call this from the SysTick interrupt handler.
Sourcepub fn now(&self) -> u64
pub fn now(&self) -> u64
Robust now() (VAL-jump tie-breaker, no COUNTFLAG dependency).
§Design: One-Wrap Compensation via PendST Detection
This implementation is designed to handle exactly one missed SysTick wrap.
How it works:
- Uses PendST bit to detect when the SysTick ISR is pending (hardware wrapped but ISR hasn’t run)
- If PendST is set, adds +1 to the wrap count to compensate for the missed wrap
- This allows monotonic time even when the ISR is delayed by up to one full wrap period
Design limit:
If the SysTick ISR is starved for MORE than one complete wrap period, this compensation
becomes insufficient and monotonic violations occur. The ISR starvation detection logic
in diagnose_timing_violation() identifies these as catastrophic “N+1 missed wraps”.
Sourcepub const fn reload_value(&self) -> u32
pub const fn reload_value(&self) -> u32
Returns the SysTick reload value configured for this timer.
Sourcepub const fn new(tick_hz: u64, reload_value: u32, systick_freq: u64) -> Self
pub const fn new(tick_hz: u64, reload_value: u32, systick_freq: u64) -> Self
Creates a new timer that converts SysTick cycles to ticks at a specified frequency.
§Arguments
tick_hz- The desired output frequency in Hz (e.g., 1000 for millisecond ticks)reload_value- The SysTick reload value. Must be between 1 and 2^24-1. This determines how many cycles occur between interrupts.systick_freq- The frequency of the SysTick counter in Hz (typically CPU frequency)
§Panics
- If
reload_valueis 0 or greater than 2^24-1 (16,777,215) - If
systick_freqis 0
§Examples
// Create a millisecond-resolution timer on a 48MHz CPU with reload value of 47,999
let timer = Timer::new(1000, 47_999, 48_000_000);Trait Implementations§
Source§impl DelayNs for &Timer
Available on crate feature embedded-hal only.Busy-wait delay implementation backed by SysTick progress.
impl DelayNs for &Timer
embedded-hal only.Busy-wait delay implementation backed by SysTick progress.
Callers must start the timer with Timer::start before using this trait.
If SysTick is not running, delay calls will not complete.
Source§fn delay_ns(&mut self, ns: u32)
fn delay_ns(&mut self, ns: u32)
ns nanoseconds. Pause can be longer
if the implementation requires it due to precision/timing issues.Source§impl DelayNs for Timer
Available on crate feature embedded-hal only.Busy-wait delay implementation backed by SysTick progress.
impl DelayNs for Timer
embedded-hal only.Busy-wait delay implementation backed by SysTick progress.
Callers must start the timer with Timer::start before using this trait.
If SysTick is not running, delay calls will not complete.
Source§fn delay_ns(&mut self, ns: u32)
fn delay_ns(&mut self, ns: u32)
ns nanoseconds. Pause can be longer
if the implementation requires it due to precision/timing issues.