Struct CYCCNTClock

Source
pub struct CYCCNTClock<const SYSCLK_HZ: u32> {}
Expand description

Clock based on the Cortex-M CYCCNT counter allowing to measure time durations and produce delays.
Precise at a microsecond scale if your SYSCLK clock is greater than 1MHz

Implementations§

Source§

impl<const SYSCLK_HZ: u32> CYCCNTClock<SYSCLK_HZ>

Source

pub fn now() -> Instant<SYSCLK_HZ>

Return an Instant object corresponding to a snapshot created at the time this method was called. Panic if the counter has not been initialized with CYCCNTClock::init() before.

   const SYSCLK_FREQ_HZ : u32 = 8_000_000;
   let t1 = CYCCNTClock<SYSCLK_FREQ_HZ>::now();
     
   // Wait 100 us
   let duration = Duration::micros(100);
   CYCCNTClock<SYSCLK_FREQ_HZ>::delay(duration)
    
   let t2 = CYCCNTClock<SYSCLK_FREQ_HZ>::now();
   let elpased_time = t2 - t1; // Very small elapsed time
   println!("time_us {}", elapsed_time.to_micros());
Source

pub fn delay(duration: Duration<SYSCLK_HZ>)

Blocking wait for the duration specified as argument Interrupts can still trigger during this call. Panic if the counter has not been initialized with CYCCNTClock::init() before.

   const SYSCLK_FREQ_HZ : u32 = 8_000_000;
   let t1 = CYCCNTClock<SYSCLK_FREQ_HZ>::now();
     
   // Wait 100 us
   let duration = Duration::micros(100);
   CYCCNTClock<SYSCLK_FREQ_HZ>::delay(duration)
    
   let t2 = CYCCNTClock<SYSCLK_FREQ_HZ>::now();
   let elpased_time = t2 - t1; // Very small elapsed time
   println!("time_us {}", elapsed_time.to_micros());
Source

pub fn update()

Synchronize the hardware counter with this clock. Must be called at least one time for every CYCCNT counter cycle after init. Otherwise time counting will be corrupted. In general, calling this method in every SysTick IRQ call is the simpler option This method will NOT panic if the CYCCNTClock::init() method has not be called first.

Source

pub fn init(dcb: &mut DCB, dwt: DWT)

Enable CYCCNT counting capability of the cortex core and start the couting

 let mut cp = cortex_m::Peripherals::take().unwrap();
 let mut dcb = cp.DCB;
 let dwt = cp.DWT;
 CYCCNTClock<SYSCLK_FREQ_HZ>::init(&mut dcb, dwt);

Auto Trait Implementations§

§

impl<const SYSCLK_HZ: u32> Freeze for CYCCNTClock<SYSCLK_HZ>

§

impl<const SYSCLK_HZ: u32> RefUnwindSafe for CYCCNTClock<SYSCLK_HZ>

§

impl<const SYSCLK_HZ: u32> Send for CYCCNTClock<SYSCLK_HZ>

§

impl<const SYSCLK_HZ: u32> Sync for CYCCNTClock<SYSCLK_HZ>

§

impl<const SYSCLK_HZ: u32> Unpin for CYCCNTClock<SYSCLK_HZ>

§

impl<const SYSCLK_HZ: u32> UnwindSafe for CYCCNTClock<SYSCLK_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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.