stm32f4xx_hal/timer/
hal_1.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 core::convert::Infallible;
7use embedded_hal::delay::DelayNs;
8
9use super::{CPin, Delay, ErasedChannel, Instance, PwmChannel, SysDelay, WithPwm};
10use fugit::ExtU32Ceil;
11
12impl DelayNs for SysDelay {
13    fn delay_ns(&mut self, ns: u32) {
14        self.delay(ns.nanos_at_least());
15    }
16
17    fn delay_ms(&mut self, ms: u32) {
18        self.delay(ms.millis_at_least());
19    }
20}
21
22impl<TIM: Instance, const FREQ: u32> DelayNs for Delay<TIM, FREQ> {
23    fn delay_ns(&mut self, ns: u32) {
24        self.delay(ns.micros_at_least());
25    }
26
27    fn delay_us(&mut self, us: u32) {
28        self.delay(us.micros_at_least());
29    }
30
31    fn delay_ms(&mut self, ms: u32) {
32        self.delay(ms.millis_at_least());
33    }
34}
35
36impl<TIM: Instance + WithPwm + CPin<C>, const C: u8, const COMP: bool, Otype>
37    embedded_hal::pwm::ErrorType for PwmChannel<TIM, C, COMP, Otype>
38{
39    type Error = Infallible;
40}
41
42impl<TIM: Instance + WithPwm + CPin<C>, const C: u8, const COMP: bool, Otype>
43    embedded_hal::pwm::SetDutyCycle for PwmChannel<TIM, C, COMP, Otype>
44{
45    fn max_duty_cycle(&self) -> u16 {
46        self.get_max_duty()
47    }
48    fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> {
49        self.set_duty(duty);
50        Ok(())
51    }
52}
53
54impl<TIM: Instance + WithPwm> embedded_hal::pwm::ErrorType for ErasedChannel<TIM> {
55    type Error = Infallible;
56}
57
58impl<TIM: Instance + WithPwm> embedded_hal::pwm::SetDutyCycle for ErasedChannel<TIM> {
59    fn max_duty_cycle(&self) -> u16 {
60        self.get_max_duty()
61    }
62    fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> {
63        self.set_duty(duty);
64        Ok(())
65    }
66}