stm32f1_hal/common/timer/
delay.rs1use super::{FTimer, GeneralTimer};
4use core::ops::{Deref, DerefMut};
5use fugit::TimerDurationU32;
6
7pub struct Delay<TIM, const FREQ: u32>(pub(super) FTimer<TIM, FREQ>);
9
10impl<T, const FREQ: u32> Deref for Delay<T, FREQ> {
11 type Target = FTimer<T, FREQ>;
12 fn deref(&self) -> &Self::Target {
13 &self.0
14 }
15}
16
17impl<T, const FREQ: u32> DerefMut for Delay<T, FREQ> {
18 fn deref_mut(&mut self) -> &mut Self::Target {
19 &mut self.0
20 }
21}
22
23pub type DelayUs<TIM> = Delay<TIM, 1_000_000>;
25
26pub type DelayMs<TIM> = Delay<TIM, 1_000>;
30
31impl<TIM: GeneralTimer, const FREQ: u32> Delay<TIM, FREQ> {
32 pub fn delay(&mut self, time: TimerDurationU32<FREQ>) {
34 let mut ticks = time.ticks().max(1) - 1;
35 while ticks != 0 {
36 let reload = ticks.min(TIM::max_auto_reload());
37
38 unsafe {
40 self.tim.set_auto_reload_unchecked(reload);
41 }
42
43 self.tim.trigger_update();
46
47 self.tim.start_one_pulse();
50
51 ticks -= reload;
53 while self.tim.is_counter_enabled() { }
55 }
56 }
57
58 pub fn max_delay(&self) -> TimerDurationU32<FREQ> {
59 TimerDurationU32::from_ticks(TIM::max_auto_reload())
60 }
61
62 pub fn release(mut self) -> FTimer<TIM, FREQ> {
64 self.tim.reset_config();
66 self.0
67 }
68}
69
70impl<TIM: GeneralTimer, const FREQ: u32> fugit_timer::Delay<FREQ> for Delay<TIM, FREQ> {
71 type Error = core::convert::Infallible;
72
73 fn delay(&mut self, duration: TimerDurationU32<FREQ>) -> Result<(), Self::Error> {
74 self.delay(duration);
75 Ok(())
76 }
77}
78
79use embedded_hal::delay::DelayNs;
82use fugit::ExtU32Ceil;
83
84impl<TIM: GeneralTimer, const FREQ: u32> DelayNs for Delay<TIM, FREQ> {
85 fn delay_ns(&mut self, ns: u32) {
86 self.delay(ns.micros_at_least());
87 }
88
89 fn delay_us(&mut self, us: u32) {
90 self.delay(us.micros_at_least());
91 }
92
93 fn delay_ms(&mut self, ms: u32) {
94 self.delay(ms.millis_at_least());
95 }
96}