|
| 1 | +//! Delay providers |
| 2 | +//! |
| 3 | +//! # Examples |
| 4 | +//! |
| 5 | +//! ## Delay |
| 6 | +//! |
| 7 | +//! ```no_run |
| 8 | +//! let mut delay = Delay::new(core.SYST, device.clocks); |
| 9 | +//! |
| 10 | +//! delay.delay_ms(500); |
| 11 | +//! |
| 12 | +//! // Release SYST from the delay |
| 13 | +//! let syst = delay.free(); |
| 14 | +//! ``` |
| 15 | +//! |
| 16 | +//! # Examples |
| 17 | +//! |
| 18 | +//! - [Blinky](https://github.com/stm32-rs/stm32h7xx-hal/blob/master/examples/blinky.rs) |
| 19 | +
|
| 20 | +use cortex_m::peripheral::syst::SystClkSource; |
| 21 | +use cortex_m::peripheral::SYST; |
| 22 | +use embedded_hal::delay::DelayNs; |
| 23 | +use fugit::SecsDurationU64; |
| 24 | + |
| 25 | +use crate::rcc::CoreClocks; |
| 26 | + |
| 27 | +const SYSTICK_HCLK_DIV: u32 = 8; |
| 28 | + |
| 29 | +pub trait DelayExt { |
| 30 | + fn delay(self, clocks: &CoreClocks) -> Delay; |
| 31 | +} |
| 32 | + |
| 33 | +impl DelayExt for SYST { |
| 34 | + fn delay(self, clocks: &CoreClocks) -> Delay { |
| 35 | + Delay::new(self, clocks) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/// System timer (SysTick) as a delay provider |
| 40 | +pub struct Delay { |
| 41 | + hclk_hz: u32, |
| 42 | + syst: SYST, |
| 43 | +} |
| 44 | + |
| 45 | +fn calc_ticks(ns: u32, hclk: u32) -> u32 { |
| 46 | + // Default is for SYSTICK to be fed by HCLK/8 |
| 47 | + let ticks: u64 = (SecsDurationU64::secs(1) * SYSTICK_HCLK_DIV).to_nanos(); |
| 48 | + ((ns as u64 * hclk as u64) / ticks) as u32 |
| 49 | +} |
| 50 | + |
| 51 | +impl Delay { |
| 52 | + /// Configures the system timer (SysTick) as a delay provider |
| 53 | + pub fn new(mut syst: SYST, clocks: &CoreClocks) -> Self { |
| 54 | + syst.set_clock_source(SystClkSource::External); |
| 55 | + |
| 56 | + Delay { |
| 57 | + hclk_hz: clocks.hclk().raw(), |
| 58 | + syst, |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// Releases the system timer (SysTick) resource |
| 63 | + pub fn free(self) -> SYST { |
| 64 | + self.syst |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl DelayNs for Delay { |
| 69 | + fn delay_ns(&mut self, ns: u32) { |
| 70 | + // The SysTick Reload Value register supports values between 1 and 0x00FFFFFF. |
| 71 | + const MAX_RVR: u32 = 0x00FF_FFFF; |
| 72 | + |
| 73 | + let mut total_ticks = calc_ticks(ns, self.hclk_hz); |
| 74 | + |
| 75 | + while total_ticks != 0 { |
| 76 | + let current_ticks = if total_ticks <= MAX_RVR { |
| 77 | + // To count N ticks, set RVR to N-1 |
| 78 | + // (see ARM Cortex M33 Devices Generic User Guide section 4.3.2.1) |
| 79 | + core::cmp::max(total_ticks - 1, 1) |
| 80 | + } else { |
| 81 | + MAX_RVR |
| 82 | + }; |
| 83 | + |
| 84 | + self.syst.set_reload(current_ticks); |
| 85 | + self.syst.clear_current(); |
| 86 | + self.syst.enable_counter(); |
| 87 | + |
| 88 | + // For an RVR value of N, the SYSTICK counts N+1 ticks |
| 89 | + // (see ARM Cortex M33 Devices Generic User Guide section 4.3.2.1) |
| 90 | + total_ticks = total_ticks.saturating_sub(current_ticks + 1); |
| 91 | + |
| 92 | + while !self.syst.has_wrapped() {} |
| 93 | + |
| 94 | + self.syst.disable_counter(); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +#[cfg(test)] |
| 100 | +mod tests { |
| 101 | + use super::calc_ticks; |
| 102 | + #[test] |
| 103 | + fn test_calc_rvr() { |
| 104 | + let rvr = calc_ticks(1_000, 8_000_000); |
| 105 | + assert_eq!(rvr, 1); |
| 106 | + |
| 107 | + let rvr = calc_ticks(1_000_000, 8_000_000); |
| 108 | + assert_eq!(rvr, 1000); |
| 109 | + |
| 110 | + let rvr = calc_ticks(1_000_000, 10_000_000); |
| 111 | + assert_eq!(rvr, 1250); |
| 112 | + |
| 113 | + let rvr = calc_ticks(1_000_000_000, 250_000_000); |
| 114 | + assert_eq!(rvr, 31_250_000); |
| 115 | + |
| 116 | + let rvr = calc_ticks(32, 250_000_000); |
| 117 | + assert_eq!(rvr, 1); |
| 118 | + } |
| 119 | +} |
0 commit comments