stm32f1xx_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    pins::sealed::Remap, pwm::Pins, Channel, Counter, CounterHz, Delay, Error, Instance, Pwm,
15    PwmChannel, PwmHz, SysCounter, 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, REMAP, P, PINS> embedded_hal::Pwm for PwmHz<TIM, REMAP, P, PINS>
157where
158    TIM: Instance + WithPwm,
159    REMAP: Remap<Periph = TIM>,
160    PINS: Pins<REMAP, P>,
161{
162    type Channel = Channel;
163    type Duty = u16;
164    type Time = Hertz;
165
166    fn enable(&mut self, channel: Self::Channel) {
167        self.enable(channel)
168    }
169
170    fn disable(&mut self, channel: Self::Channel) {
171        self.disable(channel)
172    }
173
174    fn get_duty(&self, channel: Self::Channel) -> Self::Duty {
175        self.get_duty(channel)
176    }
177
178    fn set_duty(&mut self, channel: Self::Channel, duty: Self::Duty) {
179        self.set_duty(channel, duty)
180    }
181
182    /// If `0` returned means max_duty is 2^16
183    fn get_max_duty(&self) -> Self::Duty {
184        self.get_max_duty()
185    }
186
187    fn get_period(&self) -> Self::Time {
188        self.get_period()
189    }
190
191    fn set_period<T>(&mut self, period: T)
192    where
193        T: Into<Self::Time>,
194    {
195        self.set_period(period.into())
196    }
197}
198
199impl<TIM: Instance, const FREQ: u32> DelayUs<u32> for Delay<TIM, FREQ> {
200    /// Sleep for `us` microseconds
201    fn delay_us(&mut self, us: u32) {
202        self.delay(us.micros())
203    }
204}
205
206impl<TIM: Instance, const FREQ: u32> DelayMs<u32> for Delay<TIM, FREQ> {
207    /// Sleep for `ms` milliseconds
208    fn delay_ms(&mut self, ms: u32) {
209        self.delay(ms.millis())
210    }
211}
212
213impl<TIM: Instance, const FREQ: u32> DelayUs<u16> for Delay<TIM, FREQ> {
214    /// Sleep for `us` microseconds
215    fn delay_us(&mut self, us: u16) {
216        self.delay((us as u32).micros())
217    }
218}
219impl<TIM: Instance, const FREQ: u32> DelayMs<u16> for Delay<TIM, FREQ> {
220    /// Sleep for `ms` milliseconds
221    fn delay_ms(&mut self, ms: u16) {
222        self.delay((ms as u32).millis())
223    }
224}
225
226impl<TIM: Instance, const FREQ: u32> DelayUs<u8> for Delay<TIM, FREQ> {
227    /// Sleep for `us` microseconds
228    fn delay_us(&mut self, us: u8) {
229        self.delay((us as u32).micros())
230    }
231}
232impl<TIM: Instance, const FREQ: u32> DelayMs<u8> for Delay<TIM, FREQ> {
233    /// Sleep for `ms` milliseconds
234    fn delay_ms(&mut self, ms: u8) {
235        self.delay((ms as u32).millis())
236    }
237}
238
239impl<TIM: Instance, const FREQ: u32> Periodic for Counter<TIM, FREQ> {}
240
241impl<TIM: Instance, const FREQ: u32> CountDown for Counter<TIM, FREQ> {
242    type Time = TimerDurationU32<FREQ>;
243
244    fn start<T>(&mut self, timeout: T)
245    where
246        T: Into<Self::Time>,
247    {
248        self.start(timeout.into()).unwrap()
249    }
250
251    fn wait(&mut self) -> nb::Result<(), Void> {
252        match self.wait() {
253            Err(nb::Error::WouldBlock) => Err(nb::Error::WouldBlock),
254            _ => Ok(()),
255        }
256    }
257}
258
259impl<TIM: Instance, const FREQ: u32> Cancel for Counter<TIM, FREQ> {
260    type Error = Error;
261
262    fn cancel(&mut self) -> Result<(), Self::Error> {
263        self.cancel()
264    }
265}
266
267impl<TIM, REMAP, P, PINS, const FREQ: u32> embedded_hal::Pwm for Pwm<TIM, REMAP, P, PINS, FREQ>
268where
269    TIM: Instance + WithPwm,
270    REMAP: Remap<Periph = TIM>,
271    PINS: Pins<REMAP, P>,
272{
273    type Channel = Channel;
274    type Duty = u16;
275    type Time = TimerDurationU32<FREQ>;
276
277    fn enable(&mut self, channel: Self::Channel) {
278        self.enable(channel)
279    }
280
281    fn disable(&mut self, channel: Self::Channel) {
282        self.disable(channel)
283    }
284
285    fn get_duty(&self, channel: Self::Channel) -> Self::Duty {
286        self.get_duty(channel)
287    }
288
289    fn set_duty(&mut self, channel: Self::Channel, duty: Self::Duty) {
290        self.set_duty(channel, duty)
291    }
292
293    /// If `0` returned means max_duty is 2^16
294    fn get_max_duty(&self) -> Self::Duty {
295        self.get_max_duty()
296    }
297
298    fn get_period(&self) -> Self::Time {
299        self.get_period()
300    }
301
302    fn set_period<T>(&mut self, period: T)
303    where
304        T: Into<Self::Time>,
305    {
306        self.set_period(period.into())
307    }
308}