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,
impl<T, const HZ: u32> Blocking<T, HZ>where
T: HardwareTimer,
sourcepub fn release(self) -> T
pub fn release(self) -> T
Release the underlying timer.
The released timer’s state is unspecified.
sourcepub fn block(&mut self, duration: TimerDuration<T::Ticks, HZ>)where
TimerDuration<T::Ticks, HZ>: TimerDurationExt<Repr = T::Ticks>,
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);
sourcepub 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>,
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.
sourcepub 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>,
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>
impl<const N: u8, const HZ: u32> Blocking<Pit<N>, HZ>
sourcepub fn from_pit_channel(pit: Pit<N>) -> Self
👎Deprecated since 0.5.1: Use from_pit
pub fn from_pit_channel(pit: Pit<N>) -> Self
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.