pub struct CountDown<T, const HZ: u32> { /* private fields */ }
Expand description
A count down timer adapter that uses fugit
durations.
You’re responsible for specifying the HZ
that represents
your underlying clock’s frequency. However, it conveniently
lets you express timeouts in units of time, not clock ticks.
To use this type, create a RawCountDown
, then simply wrap
that object with this adapter. You may also adapt this object
to satisfy your driver’s need.
The CountDown
embedded-hal implementation provided by this
crate may not work for your specific use case. If that’s the
case, you may adapt the RawCountDown
object to satisfy
your needs.
Example
Use the GPT as a countdown timer. ccm
APIs make
it easy to configure the root clock. Additional constants ensure
that the run-time and compile-time frequencies match.
use imxrt_hal as hal;
use imxrt_ral as ral;
use hal::ccm::{self, clock_gate, perclk_clk};
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);
// Enable the clock gate for our GPT.
clock_gate::gpt_bus::<1>().set(&mut ccm, clock_gate::ON);
clock_gate::gpt_serial::<1>().set(&mut ccm, clock_gate::ON);
// GPT1 counts with this frequency:
const GPT1_FREQUENCY_HZ: u32 = PERCLK_CLK_FREQUENCY_HZ / GPT1_DIVIDER;
const GPT1_DIVIDER: u32 = 100;
const GPT1_CLOCK_SOURCE: hal::gpt::ClockSource = hal::gpt::ClockSource::HighFrequencyReferenceClock;
let gpt1 = unsafe { ral::gpt::GPT1::instance() };
let mut gpt1 = hal::gpt::Gpt::new(gpt1);
gpt1.set_divider(GPT1_DIVIDER);
gpt1.set_clock_source(GPT1_CLOCK_SOURCE);
let mut count_down = hal::timer::CountDown::<_, GPT1_FREQUENCY_HZ>::new(
hal::timer::RawCountDown::from_gpt(gpt1)
);
use fugit::ExtU32;
use eh02::timer::CountDown;
CountDown::start(&mut count_down, 100.millis());
Implementations§
source§impl<T, const HZ: u32> CountDown<T, HZ>where
T: HardwareTimer,
impl<T, const HZ: u32> CountDown<T, HZ>where
T: HardwareTimer,
sourcepub fn new(raw: RawCountDown<T>) -> Self
pub fn new(raw: RawCountDown<T>) -> Self
Create a new count down timer that works with timer units.
sourcepub fn release(self) -> RawCountDown<T>
pub fn release(self) -> RawCountDown<T>
Release the adapter to acquire the raw count down timer.
sourcepub fn start(&mut self, duration: TimerDuration<T::Ticks, HZ>)where
TimerDuration<T::Ticks, HZ>: TimerDurationExt<Repr = T::Ticks>,
pub fn start(&mut self, duration: TimerDuration<T::Ticks, HZ>)where
TimerDuration<T::Ticks, HZ>: TimerDurationExt<Repr = T::Ticks>,
Start the timer to periodically elapse every duration
.
If this is invoked when a timer is already counting,
this resets the timer to run at ticks
.
sourcepub fn cancel(&mut self)
pub fn cancel(&mut self)
Cancel a running timer.
Does nothing if the timer is already canceled / disabled.
sourcepub fn is_elapsed(&self) -> bool
pub fn is_elapsed(&self) -> bool
Indicates if the timer has elapsed.
sourcepub fn clear_elapsed(&mut self)
pub fn clear_elapsed(&mut self)
Clears the elapsed condition.