Struct imxrt_hal::timer::Blocking

source ·
pub struct Blocking<T, const HZ: u32> { /* private fields */ }
Expand description

A blocking timer that runs at HZ.

Blocking occupies the CPU until the timer elapses. You’re responsible for specifying the clock frequency HZ.

Example

Create a blocking adapter over a single PIT channel. ccm API provide a simple way to specify the PIT clock frequency.

use imxrt_hal as hal;
use imxrt_ral as ral;

use hal::{
    ccm::{self, clock_gate, perclk_clk},
    timer::BlockingPit,
};

let mut ccm = unsafe { ral::ccm::CCM::instance() };

// Before touching the PERCLK clock roots, turn off all downstream clock gates.
clock_gate::PERCLK_CLOCK_GATES.iter().for_each(|loc| loc.set(&mut ccm, clock_gate::OFF));

// Configure PERCLK to match this frequency:
const PERCLK_CLK_FREQUENCY_HZ: u32 = ccm::XTAL_OSCILLATOR_HZ / PERCLK_CLK_DIVIDER;
const PERCLK_CLK_DIVIDER: u32 = 24;
perclk_clk::set_selection(&mut ccm, perclk_clk::Selection::Oscillator);
perclk_clk::set_divider(&mut ccm, PERCLK_CLK_DIVIDER);

// Turn on the PIT clock gate.
clock_gate::pit().set(&mut ccm, clock_gate::ON);

// There's no other divider, so the PIT frequency is the root
// clock frequency.
const PIT_FREQUENCY_HZ: u32 = PERCLK_CLK_FREQUENCY_HZ;

let pit = unsafe { ral::pit::PIT::instance() };
let (pit0, _, _, _) = hal::pit::new(pit);

let mut blocking = BlockingPit::<0, PIT_FREQUENCY_HZ>::from_pit(pit0);
// Block for milliseconds:
blocking.block_ms(1000);
// Block for microseconds:
blocking.block_us(5000);

// Use fugit to express other durations.
use fugit::ExtU32;
blocking.block(1000.millis());
blocking.block(1000.micros());

// All blocking adapters play well with embedded-hal 0.2 interfaces.
use eh02::blocking::delay::{DelayMs, DelayUs};
blocking.delay_ms(1000u32);
blocking.delay_us(1000u32);

Implementations§

source§

impl<T, const HZ: u32> Blocking<T, HZ>
where T: HardwareTimer,

source

pub fn release(self) -> T

Release the underlying timer.

The released timer’s state is unspecified.

source

pub fn block(&mut self, duration: TimerDuration<T::Ticks, HZ>)
where TimerDuration<T::Ticks, HZ>: TimerDurationExt<Repr = T::Ticks>,

Occupy the CPU, blocking execution, for a duration represented by the target clock.

See the struct-level documentation for an example. Prefer this API if you would like to catch overflow issues at compile time, as demonstrated below.

// See struct-level documentation for configuration...
// 99 seconds, expressed in microseconds, cannot fit within a u32 counter
// that counts at PIT_FREQUENCY_HZ. This fails to compile:
const DELAY: fugit::TimerDurationU32<PIT_FREQUENCY_HZ>
    = fugit::MicrosDurationU32::from_ticks(99_000_000).convert();
blocking.block(DELAY);
// However, 99 milliseconds, expressed in microseconds, can fit within a u32
// counter that counts at PIT_FREQENCY_HZ.
const DELAY: fugit::TimerDurationU32<PIT_FREQUENCY_HZ>
    = fugit::MicrosDurationU32::from_ticks(99_000).convert();
blocking.block(DELAY);
source

pub fn block_ms(&mut self, ms: T::Ticks)
where TimerDuration<T::Ticks, HZ>: TimerDurationExt<Repr = T::Ticks>, TimerDuration<T::Ticks, 1_000>: TimerDurationExt<Repr = T::Ticks>,

Occupy the CPU, blocking execution, for ms milliseconds.

Note that the type of ms depends on the tick representation of your underlying timer. See the module documentation for specifics.

Panics

Panics if the tick representation in ms on the target clock would overflow.

Example

See the top-level example for a demonstration. Despite the complex type signature, note that the duration is simply a u32 or u64.

source

pub fn block_us(&mut self, us: T::Ticks)
where TimerDuration<T::Ticks, HZ>: TimerDurationExt<Repr = T::Ticks>, TimerDuration<T::Ticks, 1_000_000>: TimerDurationExt<Repr = T::Ticks>,

Occupy the CPU, blocking execution, for us milliseconds.

Note that the type of us depends on the tick representation of your underlying timer. See the module documentation for specifics.

Panics

Panics if the tick representation in us on the target clock would overflow.

Example

See the top-level example for a demonstration. Despite the complex type signature, note that the duration is simply a u32 or u64.

source§

impl<const N: u8, const HZ: u32> Blocking<Pit<N>, HZ>

source

pub fn from_pit(pit: Pit<N>) -> Self

Create a blocking adapter from a PIT channel.

source

pub fn from_pit_channel(pit: Pit<N>) -> Self

👎Deprecated since 0.5.1: Use from_pit

Create a blocking adapter from a PIT channel.

Prefer from_pit, which is easier to type and matches the name of the type we’re converting.

source§

impl<const L: u8, const R: u8, const HZ: u32> Blocking<Chained<L, R>, HZ>

source

pub fn from_pit_chained(chain: Chained<L, R>) -> Self

Create a blocking adapter from chained PIT channels.

source§

impl<const N: u8, const HZ: u32> Blocking<Gpt<N>, HZ>

source

pub fn from_gpt(gpt: Gpt<N>) -> Self

Create a blocking adapter from a GPT.

Trait Implementations§

source§

impl<R, T, const HZ: u32> DelayMs<R> for Blocking<T, HZ>
where R: Into<T::Ticks>, T: HardwareTimer, TimerDuration<T::Ticks, HZ>: TimerDurationExt<Repr = T::Ticks>, TimerDuration<T::Ticks, 1_000>: TimerDurationExt<Repr = T::Ticks>,

source§

fn delay_ms(&mut self, ms: R)

Pauses execution for ms milliseconds
source§

impl<R, T, const HZ: u32> DelayUs<R> for Blocking<T, HZ>
where R: Into<T::Ticks>, T: HardwareTimer, TimerDuration<T::Ticks, HZ>: TimerDurationExt<Repr = T::Ticks>, TimerDuration<T::Ticks, 1_000_000>: TimerDurationExt<Repr = T::Ticks>,

source§

fn delay_us(&mut self, us: R)

Pauses execution for us microseconds

Auto Trait Implementations§

§

impl<T, const HZ: u32> RefUnwindSafe for Blocking<T, HZ>
where T: RefUnwindSafe,

§

impl<T, const HZ: u32> Send for Blocking<T, HZ>
where T: Send,

§

impl<T, const HZ: u32> Sync for Blocking<T, HZ>
where T: Sync,

§

impl<T, const HZ: u32> Unpin for Blocking<T, HZ>
where T: Unpin,

§

impl<T, const HZ: u32> UnwindSafe for Blocking<T, HZ>
where T: UnwindSafe,

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>,

§

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.