stm32l4x6_hal/
config.rs

1//! Configuration module
2use cortex_m::peripheral::syst::SystClkSource;
3use cortex_m::peripheral::SYST;
4
5use cmp;
6
7use rcc::Clocks;
8
9/// Max possible value to set on SYST's RVR register.
10///
11/// SysTick is 24-bit timer.
12/// Reference: http://infocenter.arm.com/help/topic/com.arm.doc.dui0553a/Babieigh.html
13pub const SYST_MAX_RVR: u32 = (1 << 24);
14
15/// Extension to configure SYST
16pub trait SysClockConfig {
17    /// Sets reload value in microseconds.
18    ///
19    /// Limited by `SYST_MAX_RVR`.
20    fn set_reload_us(&mut self, us: u32, clocks: &Clocks);
21    /// Sets reload value in milliseconds.
22    ///
23    /// Limited by `SYST_MAX_RVR`.
24    #[inline]
25    fn set_reload_ms(&mut self, ms: u32, clocks: &Clocks) {
26        self.set_reload_us(ms * 1_000, clocks);
27    }
28}
29
30impl SysClockConfig for SYST {
31    fn set_reload_us(&mut self, us: u32, clocks: &Clocks) {
32        let rvr = us * (clocks.sysclk.0 / 1_000_000);
33        let rvr = cmp::min(rvr, SYST_MAX_RVR);
34
35        self.set_clock_source(SystClkSource::Core);
36        self.set_reload(rvr);
37    }
38}