Struct riot_wrappers::ztimer::Clock

source ·
pub struct Clock<const HZ: u32>(/* private fields */);
Available on riot_module_ztimer only.
Expand description

A clock that knows about its frequency. The pulse length is not given in core::time::Duration as that’s not yet supported by const generics, and because clock rates are often easier to express in Hertz than in multiples of 10^-n seconds.

Implementations§

source§

impl<const HZ: u32> Clock<HZ>

source

pub fn sleep_ticks(&self, duration: u32)

Pause the current thread for the duration of ticks in the timer’s time scale.

Wraps ztimer_sleep

source

pub fn spin_ticks(&self, duration: u32)

Keep the current thread in a busy loop until the duration of ticks in the timer’s tim scale has passed

Quoting the original documentation, “This blocks lower priority threads. Use only for very short delays.”.

Wraps ztimer_spin

source

pub fn sleep(&self, duration: Duration)

Pause the current thread for the given duration.

The duration is converted into ticks (rounding up), and overflows are caught by sleeping multiple times.

It is up to the caller to select the Clock suitable for efficiency. (Even sleeping for seconds on the microseconds timer would not overflow the timer’s interface’s u32, but the same multiple-sleeps trick may need to be employed by the implementation, and would keep the system from entering deeper sleep modes).

source

pub async fn sleep_async(&self, duration: Ticks<HZ>)

Similar to [sleep_ticks()], but this does not block but creates a future to be .awaited.

Note that time starts running only when this is polled, for otherwise there’s no pinned Self around.

source

pub fn set_during<I: FnOnce() + Send, M: FnOnce() -> R, R>( &self, callback: I, ticks: Ticks<HZ>, in_thread: M ) -> R

Set the given callback to be executed in an interrupt some ticks in the future.

Then, start the in_thread function from in the thread this is called from (as a regular function call).

After the in_thread function terminates, the callback is dropped if it has not already triggered.

Further Development:

  • This could probably be done with some sort of pinning instead, thus avoiding the nested scope – but getting the Drop right is comparatively tricky, because when done naively it needs runtime state.

  • The callback could be passed something extra that enables it to set the timer again and again. Granted, there’s ztimer_periodic for these cases (and it has better drifting properties), but for something like exponential retransmission it could be convenient.

    (Might make sense to do this without an extra function variant: if the callback ignores the timer argument and always returns None, that’s all in the caller type and probebly inlined right away).

source§

impl Clock<1>

source

pub fn sec() -> Self

Available on riot_module_ztimer_sec only.

Get the global second ZTimer clock, ZTIMER_SEC.

This function is only available if the ztimer_sec module is built.

source§

impl Clock<1000>

source

pub fn msec() -> Self

Available on riot_module_ztimer_msec only.

Get the global milliseconds ZTimer clock, ZTIMER_MSEC.

This function is only available if the ztimer_msec module is built.

source§

impl Clock<1000000>

source

pub fn usec() -> Self

Available on riot_module_ztimer_usec only.

Get the global microseconds ZTimer clock, ZTIMER_USEC.

This function is only available if the ztimer_usec module is built.

Trait Implementations§

source§

impl<const HZ: u32> Clone for Clock<HZ>

source§

fn clone(&self) -> Clock<HZ>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<const F: u32> DelayNs for Clock<F>

source§

fn delay_ns(&mut self, ns: u32)

Pauses execution for at minimum ns nanoseconds. Pause can be longer if the implementation requires it due to precision/timing issues.
source§

fn delay_us(&mut self, us: u32)

Pauses execution for at minimum us microseconds. Pause can be longer if the implementation requires it due to precision/timing issues.
source§

fn delay_ms(&mut self, ms: u32)

Pauses execution for at minimum ms milliseconds. Pause can be longer if the implementation requires it due to precision/timing issues.
source§

impl DelayMs<u32> for Clock<1000>

source§

fn delay_ms(&mut self, ms: u32)

Pauses execution for ms milliseconds
source§

impl DelayUs<u32> for Clock<1000000>

source§

fn delay_us(&mut self, us: u32)

Pauses execution for us microseconds
source§

impl<const HZ: u32> Copy for Clock<HZ>

Auto Trait Implementations§

§

impl<const HZ: u32> RefUnwindSafe for Clock<HZ>

§

impl<const HZ: u32> !Send for Clock<HZ>

§

impl<const HZ: u32> !Sync for Clock<HZ>

§

impl<const HZ: u32> Unpin for Clock<HZ>

§

impl<const HZ: u32> UnwindSafe for Clock<HZ>

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> IntoSwitch for T

source§

fn into_switch<ActiveLevel>(self) -> Switch<T, ActiveLevel>

Consumes the IoPin returning a Switch of the appropriate ActiveLevel. Read more
source§

fn into_active_low_switch(self) -> Switch<Self, ActiveLow>
where Self: Sized,

Consumes the IoPin returning a Switch<IoPin, ActiveLow>. Read more
source§

fn into_active_high_switch(self) -> Switch<Self, ActiveHigh>
where Self: Sized,

Consumes the IoPin returning a Switch<IoPin, ActiveHigh>. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.