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
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//! Delays
use core::cmp;
use cortex_m::peripheral::SYST;
use hal::blocking::delay::{DelayMs, DelayUs};

use crate::prelude::*;
use crate::rcc::Rcc;
use crate::stm32::*;
use crate::time::{Hertz, MicroSecond};

/// Delay provider
pub struct Delay<TIM> {
    clk: Hertz,
    tim: TIM,
}

pub trait DelayExt<TIM> {
    fn delay(self, rcc: &mut Rcc) -> Delay<TIM>;
}

impl Delay<SYST> {
    /// Configures the system timer (SysTick) as a delay provider
    pub fn syst(syst: SYST, rcc: &Rcc) -> Self {
        Delay {
            tim: syst,
            clk: rcc.clocks.core_clk,
        }
    }

    pub fn delay<T>(&mut self, delay: T)
    where
        T: Into<MicroSecond>,
    {
        let mut cycles = delay.into().cycles(self.clk);
        while cycles > 0 {
            let reload = cmp::min(cycles, 0x00ff_ffff);
            cycles -= reload;
            self.tim.set_reload(reload);
            self.tim.clear_current();
            self.tim.enable_counter();
            while !self.tim.has_wrapped() {}
            self.tim.disable_counter();
        }
    }

    /// Releases the system timer (SysTick) resource
    pub fn release(self) -> SYST {
        self.tim
    }
}

impl DelayUs<u32> for Delay<SYST> {
    fn delay_us(&mut self, us: u32) {
        self.delay(us.us())
    }
}

impl DelayUs<u16> for Delay<SYST> {
    fn delay_us(&mut self, us: u16) {
        self.delay_us(us as u32)
    }
}

impl DelayUs<u8> for Delay<SYST> {
    fn delay_us(&mut self, us: u8) {
        self.delay_us(us as u32)
    }
}

impl DelayMs<u32> for Delay<SYST> {
    fn delay_ms(&mut self, ms: u32) {
        self.delay_us(ms.saturating_mul(1_000));
    }
}

impl DelayMs<u16> for Delay<SYST> {
    fn delay_ms(&mut self, ms: u16) {
        self.delay_ms(ms as u32);
    }
}

impl DelayMs<u8> for Delay<SYST> {
    fn delay_ms(&mut self, ms: u8) {
        self.delay_ms(ms as u32);
    }
}

impl DelayExt<SYST> for SYST {
    fn delay(self, rcc: &mut Rcc) -> Delay<SYST> {
        Delay::syst(self, rcc)
    }
}

macro_rules! delays {
    ($($TIM:ident: ($tim:ident, $timXen:ident, $timXrst:ident, $apbenr:ident, $apbrstr:ident),)+) => {
        $(
            impl Delay<$TIM> {
                /// Configures $TIM timer as a delay provider
                pub fn $tim(tim: $TIM, rcc: &mut Rcc) -> Self {
                    rcc.rb.$apbenr.modify(|_, w| w.$timXen().set_bit());
                    rcc.rb.$apbrstr.modify(|_, w| w.$timXrst().set_bit());
                    rcc.rb.$apbrstr.modify(|_, w| w.$timXrst().clear_bit());
                    Delay {
                        tim,
                        clk: rcc.clocks.apb_tim_clk,
                    }
                }

                pub fn delay<T>(&mut self, delay: T)
                where
                    T: Into<MicroSecond>,
                {
                    let mut cycles = delay.into().cycles(self.clk);
                    while cycles > 0 {
                        let reload = cmp::min(cycles, 0xffff);
                        cycles -= reload;
                        self.tim.arr.write(|w| unsafe { w.bits(reload) });
                        self.tim.cnt.reset();
                        self.tim.cr1.modify(|_, w| w.cen().set_bit().urs().set_bit());
                        while self.tim.sr.read().uif().bit_is_clear() {}
                        self.tim.sr.modify(|_, w| w.uif().clear_bit());
                        self.tim.cr1.modify(|_, w| w.cen().clear_bit());
                    }
                }

                pub fn release(self) -> $TIM {
                    self.tim
                }
            }

            impl DelayUs<u32> for Delay<$TIM> {
                fn delay_us(&mut self, us: u32) {
                    self.delay(us.us())
                }
            }

            impl DelayUs<u16> for Delay<$TIM> {
                fn delay_us(&mut self, us: u16) {
                    self.delay_us(us as u32)
                }
            }

            impl DelayUs<u8> for Delay<$TIM> {
                fn delay_us(&mut self, us: u8) {
                    self.delay_us(us as u32)
                }
            }

            impl DelayMs<u32> for Delay<$TIM> {
                fn delay_ms(&mut self, ms: u32) {
                    self.delay_us(ms.saturating_mul(1_000));
                }
            }

            impl DelayMs<u16> for Delay<$TIM> {
                fn delay_ms(&mut self, ms: u16) {
                    self.delay_ms(ms as u32);
                }
            }

            impl DelayMs<u8> for Delay<$TIM> {
                fn delay_ms(&mut self, ms: u8) {
                    self.delay_ms(ms as u32);
                }
            }

            impl DelayExt<$TIM> for $TIM {
                fn delay(self, rcc: &mut Rcc) -> Delay<$TIM> {
                    Delay::$tim(self, rcc)
                }
            }
        )+
    }
}

delays! {
    TIM1: (tim1, tim1en, tim1rst, apbenr2, apbrstr2),
    TIM3: (tim3, tim3en, tim3rst, apbenr1, apbrstr1),
    TIM14: (tim14, tim14en, tim14rst, apbenr2, apbrstr2),
    TIM16: (tim16, tim16en, tim16rst, apbenr2, apbrstr2),
    TIM17: (tim17, tim17en, tim17rst, apbenr2, apbrstr2),
}

#[cfg(feature = "stm32g0x1")]
delays! {
    TIM2: (tim2, tim2en, tim2rst, apbenr1, apbrstr1),
}

#[cfg(any(feature = "stm32g070", feature = "stm32g071", feature = "stm32g081"))]
delays! {
    TIM6: (tim6, tim6en, tim6rst, apbenr1, apbrstr1),
    TIM7: (tim7, tim7en, tim7rst, apbenr1, apbrstr1),
    TIM15: (tim15, tim15en, tim15rst, apbenr2, apbrstr2),
}