diff --git a/embassy-stm32/src/lptim/pwm.rs b/embassy-stm32/src/lptim/pwm.rs index 1f43eb6ee..9f3803cd6 100644 --- a/embassy-stm32/src/lptim/pwm.rs +++ b/embassy-stm32/src/lptim/pwm.rs @@ -10,7 +10,7 @@ use super::OutputPin; #[cfg(any(lptim_v2a, lptim_v2b))] use super::{channel::Channel, timer::ChannelDirection, Channel1Pin, Channel2Pin}; use super::{BasicInstance, Instance}; -use crate::gpio::{AfType, AnyPin, OutputType, Speed}; +use crate::gpio::{AfType, AnyPin, OutputType, Pull, Speed}; use crate::time::Hertz; use crate::Peripheral; @@ -29,6 +29,15 @@ pub struct PwmPin<'d, T, C> { phantom: PhantomData<(T, C)>, } +/// PWM pin config +/// +/// This configures the pwm pin settings +pub struct PwmPinConfig { + pub output_type: OutputType, + pub speed: Speed, + pub pull: Pull, +} + macro_rules! channel_impl { ($new_chx:ident, $channel:ident, $pin_trait:ident) => { impl<'d, T: BasicInstance> PwmPin<'d, T, $channel> { @@ -47,6 +56,24 @@ macro_rules! channel_impl { phantom: PhantomData, } } + #[doc = concat!("Create a new ", stringify!($channel), "_with_config PWM pin instance.")] + pub fn $new_chx_with_config( + pin: impl Peripheral

> + 'd, + pin_config: PwmPinConfig, + ) -> Self { + into_ref!(pin); + critical_section::with(|_| { + pin.set_low(); + pin.set_as_af( + pin.af_num(), + AfType::output_pull(pin_config.output_type, pin_config.speed, pin_config.pull), + ); + }); + PwmPin { + _pin: pin.map_into(), + phantom: PhantomData, + } + } } }; } diff --git a/embassy-stm32/src/timer/simple_pwm.rs b/embassy-stm32/src/timer/simple_pwm.rs index 1dff3f9ae..7664a4dc0 100644 --- a/embassy-stm32/src/timer/simple_pwm.rs +++ b/embassy-stm32/src/timer/simple_pwm.rs @@ -7,7 +7,7 @@ use embassy_hal_internal::{into_ref, PeripheralRef}; use super::low_level::{CountingMode, OutputCompareMode, OutputPolarity, Timer}; use super::{Channel, Channel1Pin, Channel2Pin, Channel3Pin, Channel4Pin, GeneralInstance4Channel, TimerBits}; -use crate::gpio::{AfType, AnyPin, OutputType, Speed}; +use crate::gpio::{AfType, AnyPin, OutputType, Pull, Speed}; use crate::time::Hertz; use crate::Peripheral; @@ -28,6 +28,15 @@ pub struct PwmPin<'d, T, C> { phantom: PhantomData<(T, C)>, } +/// PWM pin config +/// +/// This configures the pwm pin settings +pub struct PwmPinConfig { + pub output_type: OutputType, + pub speed: Speed, + pub pull: Pull, +} + macro_rules! channel_impl { ($new_chx:ident, $channel:ident, $pin_trait:ident) => { impl<'d, T: GeneralInstance4Channel> PwmPin<'d, T, $channel> { @@ -43,6 +52,24 @@ macro_rules! channel_impl { phantom: PhantomData, } } + #[doc = concat!("Create a new ", stringify!($channel), "_with_config PWM pin instance.")] + pub fn $new_chx_with_config( + pin: impl Peripheral

> + 'd, + pin_config: PwmPinConfig, + ) -> Self { + into_ref!(pin); + critical_section::with(|_| { + pin.set_low(); + pin.set_as_af( + pin.af_num(), + AfType::output_pull(pin_config.output_type, pin_config.speed, pin_config.pull), + ); + }); + PwmPin { + _pin: pin.map_into(), + phantom: PhantomData, + } + } } }; }