Skip to main content

Timer

Struct Timer 

Source
pub struct Timer { /* private fields */ }
Expand description

Free-running microsecond counter (BCM System Timer) and blocking delays built on it.

The System Timer is a plain memory-mapped 64-bit counter (as CLO + CHI) running at a fixed 1MHz, needing no configuration — it’s already counting from boot.

Of the four compare registers C0-C3, only Compare 1 is used: the GPU firmware reserves C0 and C2 for itself, and C3 is left free.

Two properties of that compare shape every deadline API below, and neither is a limitation of this driver:

  • It is 32 bits wide, against a 64-bit counter. The compare only sees CLO, the low half, so a deadline can only be expressed within 2^32us (~71.6 minutes) of the present, and any value matches once per wrap whether or not anyone armed it.
  • It is an equality test, not a threshold. CS.M1 is set when C1 equals CLO, so a deadline the counter has already walked past does not fire late — it fires a whole wrap later.

Implementations§

Source§

impl Timer

Source

pub fn new(systmr: SYSTMR) -> Self

Wraps the System Timer peripheral. Needs no configuration — it’s already free-running from boot.

Source

pub fn now_micros(&self) -> u64

Microseconds elapsed since boot.

Source

pub fn delay_us(&self, us: u32)

Busy-waits for approximately us microseconds.

Source

pub fn delay_ms(&self, ms: u32)

Busy-waits for approximately ms milliseconds.

Source

pub fn set_compare1(&self, deadline_us: u64) -> bool

Arms Compare 1 for the absolute now_micros deadline deadline_us, raising an IRQ at that point (once routed via crate::lic::Lic::enable_timer1_irq and unmasked via crate::irq::enable_irq).

Returns false if the deadline was already in the past by the time it was armed — including the narrow case where the counter passed it between computing the target and writing it. Because the compare is an equality test (see the module doc comment), that deadline would otherwise not fire for another ~71.6 minutes, so a caller that still wants a timely wake-up must recompute against a fresh now_micros and arm again rather than assume it is pending.

A deadline further out than ~35.8 minutes is clamped to that, keeping the write inside the range the 32-bit compare can express. The match then fires early, which callers must tolerate regardless: they have to re-check their own deadline against now_micros on every match and re-arm if it hasn’t arrived yet.

Does not clear a match already pending from a previous arm — pair with clear_compare1_match.

Source

pub fn compare1_matched(&self) -> bool

Whether Compare 1 has matched (CS.M1) and not yet been cleared.

Source

pub fn clear_compare1_match(&self)

Acknowledges a fired Compare 1 match (write-1-to-clear CS.M1) without arming another. Call this from the IRQ handler — leaving the match flag set would just re-trigger the same IRQ immediately after returning.

Source

pub fn arm_periodic_c1(&self, period_us: u32)

Arms Compare 1 to match period_us from now.

A convenience wrapper over set_compare1 for the fixed-period case, where the deadline is always close enough to the present that neither the clamp nor the already-passed case can be reached by a period of more than a couple of microseconds.

Source

pub fn ack_c1(&self, period_us: u32)

Acknowledges a fired Compare 1 match and rearms the next period — clear_compare1_match followed by arm_periodic_c1.

Trait Implementations§

Source§

impl DelayNs for Timer

Source§

fn delay_ns(&mut self, ns: u32)

Rounds up to the System Timer’s 1us resolution, then delegates to delay_us — a nonzero request always waits at least one tick instead of returning immediately.

Source§

fn delay_us(&mut self, us: u32)

Delegates to the inherent Timer::delay_us.

Source§

fn delay_ms(&mut self, ms: u32)

Delegates to the inherent Timer::delay_ms.

Auto Trait Implementations§

§

impl !Sync for Timer

§

impl Freeze for Timer

§

impl RefUnwindSafe for Timer

§

impl Send for Timer

§

impl Unpin for Timer

§

impl UnsafeUnpin for Timer

§

impl UnwindSafe for Timer

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.