stm32f7xx_hal/timer/
hal_02.rs

1//! Delay implementation based on general-purpose 32 bit timers and System timer (SysTick).
2//!
3//! TIM2 and TIM5 are a general purpose 32-bit auto-reload up/downcounter with
4//! a 16-bit prescaler.
5
6use embedded_hal::{
7    blocking::delay::{DelayMs, DelayUs},
8    timer::{Cancel, CountDown, Periodic},
9};
10use fugit::{ExtU32, HertzU32 as Hertz, TimerDurationU32};
11use void::Void;
12
13use super::{
14    Channel, Counter, CounterHz, Delay, Error, Instance, Pins, Pwm, PwmChannel, PwmHz, SysCounter,
15    SysCounterHz, SysDelay, WithPwm,
16};
17
18impl DelayUs<u32> for SysDelay {
19    fn delay_us(&mut self, us: u32) {
20        self.delay(us.micros())
21    }
22}
23
24impl DelayMs<u32> for SysDelay {
25    fn delay_ms(&mut self, ms: u32) {
26        self.delay_us(ms * 1_000);
27    }
28}
29
30impl DelayUs<u16> for SysDelay {
31    fn delay_us(&mut self, us: u16) {
32        self.delay_us(us as u32)
33    }
34}
35
36impl DelayMs<u16> for SysDelay {
37    fn delay_ms(&mut self, ms: u16) {
38        self.delay_ms(ms as u32);
39    }
40}
41
42impl DelayUs<u8> for SysDelay {
43    fn delay_us(&mut self, us: u8) {
44        self.delay_us(us as u32)
45    }
46}
47
48impl DelayMs<u8> for SysDelay {
49    fn delay_ms(&mut self, ms: u8) {
50        self.delay_ms(ms as u32);
51    }
52}
53
54impl<TIM> Periodic for CounterHz<TIM> {}
55impl Periodic for SysCounterHz {}
56impl<const FREQ: u32> Periodic for SysCounter<FREQ> {}
57
58impl CountDown for SysCounterHz {
59    type Time = Hertz;
60
61    fn start<T>(&mut self, timeout: T)
62    where
63        T: Into<Hertz>,
64    {
65        self.start(timeout.into()).unwrap()
66    }
67
68    fn wait(&mut self) -> nb::Result<(), Void> {
69        match self.wait() {
70            Err(nb::Error::WouldBlock) => Err(nb::Error::WouldBlock),
71            _ => Ok(()),
72        }
73    }
74}
75
76impl Cancel for SysCounterHz {
77    type Error = Error;
78
79    fn cancel(&mut self) -> Result<(), Self::Error> {
80        self.cancel()
81    }
82}
83
84impl<TIM: Instance> CountDown for CounterHz<TIM> {
85    type Time = Hertz;
86
87    fn start<T>(&mut self, timeout: T)
88    where
89        T: Into<Hertz>,
90    {
91        self.start(timeout.into()).unwrap()
92    }
93
94    fn wait(&mut self) -> nb::Result<(), Void> {
95        match self.wait() {
96            Err(nb::Error::WouldBlock) => Err(nb::Error::WouldBlock),
97            _ => Ok(()),
98        }
99    }
100}
101
102impl<TIM: Instance> Cancel for CounterHz<TIM> {
103    type Error = Error;
104
105    fn cancel(&mut self) -> Result<(), Self::Error> {
106        self.cancel()
107    }
108}
109
110impl<const FREQ: u32> CountDown for SysCounter<FREQ> {
111    type Time = TimerDurationU32<FREQ>;
112
113    fn start<T>(&mut self, timeout: T)
114    where
115        T: Into<Self::Time>,
116    {
117        self.start(timeout.into()).unwrap()
118    }
119
120    fn wait(&mut self) -> nb::Result<(), Void> {
121        match self.wait() {
122            Err(nb::Error::WouldBlock) => Err(nb::Error::WouldBlock),
123            _ => Ok(()),
124        }
125    }
126}
127
128impl<const FREQ: u32> Cancel for SysCounter<FREQ> {
129    type Error = Error;
130
131    fn cancel(&mut self) -> Result<(), Self::Error> {
132        self.cancel()
133    }
134}
135
136impl<TIM: Instance + WithPwm, const C: u8> embedded_hal::PwmPin for PwmChannel<TIM, C> {
137    type Duty = u16;
138
139    fn disable(&mut self) {
140        self.disable()
141    }
142    fn enable(&mut self) {
143        self.enable()
144    }
145    fn get_duty(&self) -> Self::Duty {
146        self.get_duty()
147    }
148    fn get_max_duty(&self) -> Self::Duty {
149        self.get_max_duty()
150    }
151    fn set_duty(&mut self, duty: Self::Duty) {
152        self.set_duty(duty)
153    }
154}
155
156impl<TIM, P, PINS> embedded_hal::Pwm for PwmHz<TIM, P, PINS>
157where
158    TIM: Instance + WithPwm,
159    PINS: Pins<TIM, P>,
160{
161    type Channel = Channel;
162    type Duty = u16;
163    type Time = Hertz;
164
165    fn enable(&mut self, channel: Self::Channel) {
166        self.enable(channel)
167    }
168
169    fn disable(&mut self, channel: Self::Channel) {
170        self.disable(channel)
171    }
172
173    fn get_duty(&self, channel: Self::Channel) -> Self::Duty {
174        self.get_duty(channel)
175    }
176
177    fn set_duty(&mut self, channel: Self::Channel, duty: Self::Duty) {
178        self.set_duty(channel, duty)
179    }
180
181    /// If `0` returned means max_duty is 2^16
182    fn get_max_duty(&self) -> Self::Duty {
183        self.get_max_duty()
184    }
185
186    fn get_period(&self) -> Self::Time {
187        self.get_period()
188    }
189
190    fn set_period<T>(&mut self, period: T)
191    where
192        T: Into<Self::Time>,
193    {
194        self.set_period(period.into())
195    }
196}
197
198impl<TIM: Instance, const FREQ: u32> DelayUs<u32> for Delay<TIM, FREQ> {
199    /// Sleep for `us` microseconds
200    fn delay_us(&mut self, us: u32) {
201        self.delay(us.micros())
202    }
203}
204
205impl<TIM: Instance, const FREQ: u32> DelayMs<u32> for Delay<TIM, FREQ> {
206    /// Sleep for `ms` milliseconds
207    fn delay_ms(&mut self, ms: u32) {
208        self.delay(ms.millis())
209    }
210}
211
212impl<TIM: Instance, const FREQ: u32> DelayUs<u16> for Delay<TIM, FREQ> {
213    /// Sleep for `us` microseconds
214    fn delay_us(&mut self, us: u16) {
215        self.delay((us as u32).micros())
216    }
217}
218impl<TIM: Instance, const FREQ: u32> DelayMs<u16> for Delay<TIM, FREQ> {
219    /// Sleep for `ms` milliseconds
220    fn delay_ms(&mut self, ms: u16) {
221        self.delay((ms as u32).millis())
222    }
223}
224
225impl<TIM: Instance, const FREQ: u32> DelayUs<u8> for Delay<TIM, FREQ> {
226    /// Sleep for `us` microseconds
227    fn delay_us(&mut self, us: u8) {
228        self.delay((us as u32).micros())
229    }
230}
231impl<TIM: Instance, const FREQ: u32> DelayMs<u8> for Delay<TIM, FREQ> {
232    /// Sleep for `ms` milliseconds
233    fn delay_ms(&mut self, ms: u8) {
234        self.delay((ms as u32).millis())
235    }
236}
237
238impl<TIM: Instance, const FREQ: u32> Periodic for Counter<TIM, FREQ> {}
239
240impl<TIM: Instance, const FREQ: u32> CountDown for Counter<TIM, FREQ> {
241    type Time = TimerDurationU32<FREQ>;
242
243    fn start<T>(&mut self, timeout: T)
244    where
245        T: Into<Self::Time>,
246    {
247        self.start(timeout.into()).unwrap()
248    }
249
250    fn wait(&mut self) -> nb::Result<(), Void> {
251        match self.wait() {
252            Err(nb::Error::WouldBlock) => Err(nb::Error::WouldBlock),
253            _ => Ok(()),
254        }
255    }
256}
257
258impl<TIM: Instance, const FREQ: u32> Cancel for Counter<TIM, FREQ> {
259    type Error = Error;
260
261    fn cancel(&mut self) -> Result<(), Self::Error> {
262        self.cancel()
263    }
264}
265
266impl<TIM, P, PINS, const FREQ: u32> embedded_hal::Pwm for Pwm<TIM, P, PINS, FREQ>
267where
268    TIM: Instance + WithPwm,
269    PINS: Pins<TIM, P>,
270{
271    type Channel = Channel;
272    type Duty = u16;
273    type Time = TimerDurationU32<FREQ>;
274
275    fn enable(&mut self, channel: Self::Channel) {
276        self.enable(channel)
277    }
278
279    fn disable(&mut self, channel: Self::Channel) {
280        self.disable(channel)
281    }
282
283    fn get_duty(&self, channel: Self::Channel) -> Self::Duty {
284        self.get_duty(channel)
285    }
286
287    fn set_duty(&mut self, channel: Self::Channel, duty: Self::Duty) {
288        self.set_duty(channel, duty)
289    }
290
291    /// If `0` returned means max_duty is 2^16
292    fn get_max_duty(&self) -> Self::Duty {
293        self.get_max_duty()
294    }
295
296    fn get_period(&self) -> Self::Time {
297        self.get_period()
298    }
299
300    fn set_period<T>(&mut self, period: T)
301    where
302        T: Into<Self::Time>,
303    {
304        self.set_period(period.into())
305    }
306}