embassy_stm32/lptim/
pwm.rs

1//! PWM driver.
2
3use core::marker::PhantomData;
4
5use embassy_hal_internal::Peri;
6
7use super::timer::Timer;
8#[cfg(not(any(lptim_v2a, lptim_v2b)))]
9use super::OutputPin;
10#[cfg(any(lptim_v2a, lptim_v2b))]
11use super::{channel::Channel, timer::ChannelDirection, Channel1Pin, Channel2Pin};
12use super::{BasicInstance, Instance};
13#[cfg(gpio_v2)]
14use crate::gpio::Pull;
15use crate::gpio::{AfType, AnyPin, OutputType, Speed};
16use crate::time::Hertz;
17
18/// Output marker type.
19pub enum Output {}
20/// Channel 1 marker type.
21pub enum Ch1 {}
22/// Channel 2 marker type.
23pub enum Ch2 {}
24
25/// PWM pin wrapper.
26///
27/// This wraps a pin to make it usable with PWM.
28pub struct PwmPin<'d, T, C> {
29    _pin: Peri<'d, AnyPin>,
30    phantom: PhantomData<(T, C)>,
31}
32
33/// PWM pin config
34///
35/// This configures the pwm pin settings
36pub struct PwmPinConfig {
37    /// PWM Pin output type
38    pub output_type: OutputType,
39    /// PWM Pin speed
40    pub speed: Speed,
41    /// PWM Pin pull type
42    #[cfg(gpio_v2)]
43    pub pull: Pull,
44}
45
46macro_rules! channel_impl {
47    ($new_chx:ident, $new_chx_with_config:ident, $channel:ident, $pin_trait:ident) => {
48        impl<'d, T: BasicInstance> PwmPin<'d, T, $channel> {
49            #[doc = concat!("Create a new ", stringify!($channel), " PWM pin instance.")]
50            pub fn $new_chx(pin: Peri<'d, impl $pin_trait<T>>) -> Self {
51                critical_section::with(|_| {
52                    pin.set_low();
53                    pin.set_as_af(
54                        pin.af_num(),
55                        AfType::output(OutputType::PushPull, Speed::VeryHigh),
56                    );
57                });
58                PwmPin {
59                    _pin: pin.into(),
60                    phantom: PhantomData,
61                }
62            }
63            #[doc = concat!("Create a new ", stringify!($channel), " PWM pin instance with config.")]
64            pub fn $new_chx_with_config(pin: Peri<'d, impl $pin_trait<T>>, pin_config: PwmPinConfig) -> Self {
65                critical_section::with(|_| {
66                    pin.set_low();
67                    pin.set_as_af(
68                        pin.af_num(),
69                        #[cfg(gpio_v1)]
70                        AfType::output(pin_config.output_type, pin_config.speed),
71                        #[cfg(gpio_v2)]
72                        AfType::output_pull(pin_config.output_type, pin_config.speed, pin_config.pull),
73                    );
74                });
75                PwmPin {
76                    _pin: pin.into(),
77                    phantom: PhantomData,
78                }
79            }
80        }
81    };
82}
83
84#[cfg(not(any(lptim_v2a, lptim_v2b)))]
85channel_impl!(new, new_with_config, Output, OutputPin);
86#[cfg(any(lptim_v2a, lptim_v2b))]
87channel_impl!(new_ch1, new_ch1_with_config, Ch1, Channel1Pin);
88#[cfg(any(lptim_v2a, lptim_v2b))]
89channel_impl!(new_ch2, new_ch2_with_config, Ch2, Channel2Pin);
90
91/// PWM driver.
92pub struct Pwm<'d, T: Instance> {
93    inner: Timer<'d, T>,
94}
95
96#[cfg(not(any(lptim_v2a, lptim_v2b)))]
97impl<'d, T: Instance> Pwm<'d, T> {
98    /// Create a new PWM driver.
99    pub fn new(tim: Peri<'d, T>, _output_pin: PwmPin<'d, T, Output>, freq: Hertz) -> Self {
100        Self::new_inner(tim, freq)
101    }
102
103    /// Set the duty.
104    ///
105    /// The value ranges from 0 for 0% duty, to [`get_max_duty`](Self::get_max_duty) for 100% duty, both included.
106    pub fn set_duty(&mut self, duty: u16) {
107        assert!(duty <= self.get_max_duty());
108        self.inner.set_compare_value(duty)
109    }
110
111    /// Get the duty.
112    ///
113    /// The value ranges from 0 for 0% duty, to [`get_max_duty`](Self::get_max_duty) for 100% duty, both included.
114    pub fn get_duty(&self) -> u16 {
115        self.inner.get_compare_value()
116    }
117
118    fn post_init(&mut self) {}
119}
120
121#[cfg(any(lptim_v2a, lptim_v2b))]
122impl<'d, T: Instance> Pwm<'d, T> {
123    /// Create a new PWM driver.
124    pub fn new(
125        tim: Peri<'d, T>,
126        _ch1_pin: Option<PwmPin<'d, T, Ch1>>,
127        _ch2_pin: Option<PwmPin<'d, T, Ch2>>,
128        freq: Hertz,
129    ) -> Self {
130        Self::new_inner(tim, freq)
131    }
132
133    /// Enable the given channel.
134    pub fn enable(&mut self, channel: Channel) {
135        self.inner.enable_channel(channel, true);
136    }
137
138    /// Disable the given channel.
139    pub fn disable(&mut self, channel: Channel) {
140        self.inner.enable_channel(channel, false);
141    }
142
143    /// Check whether given channel is enabled
144    pub fn is_enabled(&self, channel: Channel) -> bool {
145        self.inner.get_channel_enable_state(channel)
146    }
147
148    /// Set the duty for a given channel.
149    ///
150    /// The value ranges from 0 for 0% duty, to [`get_max_duty`](Self::get_max_duty) for 100% duty, both included.
151    pub fn set_duty(&mut self, channel: Channel, duty: u16) {
152        assert!(duty <= self.get_max_duty());
153        self.inner.set_compare_value(channel, duty)
154    }
155
156    /// Get the duty for a given channel.
157    ///
158    /// The value ranges from 0 for 0% duty, to [`get_max_duty`](Self::get_max_duty) for 100% duty, both included.
159    pub fn get_duty(&self, channel: Channel) -> u16 {
160        self.inner.get_compare_value(channel)
161    }
162
163    fn post_init(&mut self) {
164        [Channel::Ch1, Channel::Ch2].iter().for_each(|&channel| {
165            self.inner.set_channel_direction(channel, ChannelDirection::OutputPwm);
166        });
167    }
168}
169
170impl<'d, T: Instance> Pwm<'d, T> {
171    fn new_inner(tim: Peri<'d, T>, freq: Hertz) -> Self {
172        let mut this = Self { inner: Timer::new(tim) };
173
174        this.inner.enable();
175        this.set_frequency(freq);
176
177        this.post_init();
178
179        this.inner.continuous_mode_start();
180
181        this
182    }
183
184    /// Set PWM frequency.
185    ///
186    /// Note: when you call this, the max duty value changes, so you will have to
187    /// call `set_duty` on all channels with the duty calculated based on the new max duty.
188    pub fn set_frequency(&mut self, frequency: Hertz) {
189        self.inner.set_frequency(frequency);
190    }
191
192    /// Get max duty value.
193    ///
194    /// This value depends on the configured frequency and the timer's clock rate from RCC.
195    pub fn get_max_duty(&self) -> u16 {
196        self.inner.get_max_compare_value() + 1
197    }
198}