Struct imxrt_hal::timer::CountDown

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

source

pub fn new(raw: RawCountDown<T>) -> Self

Create a new count down timer that works with timer units.

source

pub fn release(self) -> RawCountDown<T>

Release the adapter to acquire the raw count down timer.

source

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.

source

pub fn cancel(&mut self)

Cancel a running timer.

Does nothing if the timer is already canceled / disabled.

source

pub fn is_elapsed(&self) -> bool

Indicates if the timer has elapsed.

source

pub fn clear_elapsed(&mut self)

Clears the elapsed condition.

Trait Implementations§

source§

impl<T, const HZ: u32> CountDown for CountDown<T, HZ>
where T: HardwareTimer, TimerDuration<T::Ticks, HZ>: TimerDurationExt<Repr = T::Ticks>,

§

type Time = Duration<<T as HardwareTimer>::Ticks, 1, HZ>

The unit of time used by this timer
source§

fn start<C>(&mut self, count: C)
where C: Into<Self::Time>,

Starts a new count down
source§

fn wait(&mut self) -> Result<(), Void>

Non-blockingly “waits” until the count down finishes Read more
source§

impl<T, const HZ: u32> Periodic for CountDown<T, HZ>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<T, const HZ: u32> UnwindSafe for CountDown<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.