-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathdelay.rs
46 lines (42 loc) · 1.08 KB
/
delay.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//! Basic blocking delay
//!
//! This module provides a basic asm-based blocking delay.
//!
//! # Note
//! This implementation takes into account the Cortex-M7 CPU pipeline architecture to ensure delays
//! are at least as long as specified.
use embedded_hal_02::blocking::delay::{DelayMs, DelayUs};
/// A basic delay implementation.
pub struct AsmDelay {
frequency_us: u32,
frequency_ms: u32,
}
impl AsmDelay {
/// Create a new delay.
///
/// # Args
/// * `freq` - The CPU core frequency.
pub fn new(freq: u32) -> AsmDelay {
// Note: Frequencies are scaled by 2 to account for the M7 dual instruction pipeline.
AsmDelay {
frequency_us: (freq / 1_000_000) * 2,
frequency_ms: (freq / 1_000) * 2,
}
}
}
impl<U> DelayUs<U> for AsmDelay
where
U: Into<u32>,
{
fn delay_us(&mut self, us: U) {
cortex_m::asm::delay(self.frequency_us * us.into())
}
}
impl<U> DelayMs<U> for AsmDelay
where
U: Into<u32>,
{
fn delay_ms(&mut self, ms: U) {
cortex_m::asm::delay(self.frequency_ms * ms.into())
}
}