stm32f0xx_hal/
delay.rs

1//! API for delays with the systick timer
2//!
3//! Please be aware of potential overflows when using `delay_us`.
4//! E.g. at 48MHz the maximum delay is 89 seconds.
5//!
6//! Consider using the timers api as a more flexible interface
7//!
8//! # Example
9//!
10//! ``` no_run
11//! use stm32f0xx_hal as hal;
12//!
13//! use crate::hal::pac;
14//! use crate::hal::prelude::*;
15//! use crate::hal::delay::Delay;
16//! use cortex_m::peripheral::Peripherals;
17//!
18//! let mut p = pac::Peripherals::take().unwrap();
19//! let mut cp = cortex_m::Peripherals::take().unwrap();
20//!
21//! let mut rcc = p.RCC.configure().freeze(&mut p.FLASH);
22//! let mut delay = Delay::new(cp.SYST, &rcc);
23//! loop {
24//!     delay.delay_ms(1_000_u16);
25//! }
26//! ```
27
28use cast::{u16, u32};
29use cortex_m::peripheral::syst::SystClkSource;
30use cortex_m::peripheral::SYST;
31
32use crate::rcc::Rcc;
33
34use embedded_hal::blocking::delay::{DelayMs, DelayUs};
35
36/// System timer (SysTick) as a delay provider
37#[derive(Clone)]
38pub struct Delay {
39    scale: u32,
40}
41
42const SYSTICK_RANGE: u32 = 0x0100_0000;
43
44impl Delay {
45    /// Configures the system timer (SysTick) as a delay provider
46    pub fn new(mut syst: SYST, rcc: &Rcc) -> Delay {
47        syst.set_clock_source(SystClkSource::Core);
48
49        syst.set_reload(SYSTICK_RANGE - 1);
50        syst.clear_current();
51        syst.enable_counter();
52
53        assert!(rcc.clocks.hclk().0 >= 1_000_000);
54        let scale = rcc.clocks.hclk().0 / 1_000_000;
55
56        Delay { scale }
57        // As access to the count register is possible without a reference to the systick, we can
58        // just drop it
59    }
60}
61
62impl DelayMs<u32> for Delay {
63    // At 48 MHz (the maximum frequency), calling delay_us with ms * 1_000 directly overflows at 0x15D86 (just over the max u16 value)
64    // So we implement a separate, higher level, delay loop
65    fn delay_ms(&mut self, mut ms: u32) {
66        const MAX_MS: u32 = 0x0000_FFFF;
67        while ms != 0 {
68            let current_ms = if ms <= MAX_MS { ms } else { MAX_MS };
69            self.delay_us(current_ms * 1_000);
70            ms -= current_ms;
71        }
72    }
73}
74
75impl DelayMs<u16> for Delay {
76    fn delay_ms(&mut self, ms: u16) {
77        // Call delay_us directly, so we don't have to use the additional
78        // delay loop the u32 variant uses
79        self.delay_us(u32(ms) * 1_000);
80    }
81}
82
83impl DelayMs<u8> for Delay {
84    fn delay_ms(&mut self, ms: u8) {
85        self.delay_ms(u16(ms));
86    }
87}
88
89// At 48MHz (the maximum frequency), this overflows at approx. 2^32 / 48 = 89 seconds
90impl DelayUs<u32> for Delay {
91    fn delay_us(&mut self, us: u32) {
92        // The SysTick Reload Value register supports values between 1 and 0x00FFFFFF.
93        // Here less than maximum is used so we have some play if there's a long running interrupt.
94        const MAX_TICKS: u32 = 0x007F_FFFF;
95
96        let mut total_ticks = us * self.scale;
97
98        while total_ticks != 0 {
99            let current_ticks = if total_ticks <= MAX_TICKS {
100                total_ticks
101            } else {
102                MAX_TICKS
103            };
104
105            let start_count = SYST::get_current();
106            total_ticks -= current_ticks;
107
108            // Use the wrapping substraction and the modulo to deal with the systick wrapping around
109            // from 0 to 0xFFFF
110            while (start_count.wrapping_sub(SYST::get_current()) % SYSTICK_RANGE) < current_ticks {}
111        }
112    }
113}
114
115impl DelayUs<u16> for Delay {
116    fn delay_us(&mut self, us: u16) {
117        self.delay_us(u32(us))
118    }
119}
120
121impl DelayUs<u8> for Delay {
122    fn delay_us(&mut self, us: u8) {
123        self.delay_us(u32(us))
124    }
125}