Skip to content

Commit f22649e

Browse files
Added function to channel_impl to allow full configuration of the pin
1 parent 0edd45e commit f22649e

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

embassy-stm32/src/lptim/pwm.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use super::OutputPin;
1010
#[cfg(any(lptim_v2a, lptim_v2b))]
1111
use super::{channel::Channel, timer::ChannelDirection, Channel1Pin, Channel2Pin};
1212
use super::{BasicInstance, Instance};
13-
use crate::gpio::{AfType, AnyPin, OutputType, Speed};
13+
use crate::gpio::{AfType, AnyPin, OutputType, Pull, Speed};
1414
use crate::time::Hertz;
1515
use crate::Peripheral;
1616

@@ -29,6 +29,15 @@ pub struct PwmPin<'d, T, C> {
2929
phantom: PhantomData<(T, C)>,
3030
}
3131

32+
/// PWM pin config
33+
///
34+
/// This configures the pwm pin settings
35+
pub struct PwmPinConfig {
36+
pub output_type: OutputType,
37+
pub speed: Speed,
38+
pub pull: Pull,
39+
}
40+
3241
macro_rules! channel_impl {
3342
($new_chx:ident, $channel:ident, $pin_trait:ident) => {
3443
impl<'d, T: BasicInstance> PwmPin<'d, T, $channel> {
@@ -47,6 +56,24 @@ macro_rules! channel_impl {
4756
phantom: PhantomData,
4857
}
4958
}
59+
#[doc = concat!("Create a new ", stringify!($channel), "_with_config PWM pin instance.")]
60+
pub fn $new_chx_with_config(
61+
pin: impl Peripheral<P = impl $pin_trait<T>> + 'd,
62+
pin_config: PwmPinConfig,
63+
) -> Self {
64+
into_ref!(pin);
65+
critical_section::with(|_| {
66+
pin.set_low();
67+
pin.set_as_af(
68+
pin.af_num(),
69+
AfType::output_pull(pin_config.output_type, pin_config.speed, pin_config.pull),
70+
);
71+
});
72+
PwmPin {
73+
_pin: pin.map_into(),
74+
phantom: PhantomData,
75+
}
76+
}
5077
}
5178
};
5279
}

embassy-stm32/src/timer/simple_pwm.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use embassy_hal_internal::{into_ref, PeripheralRef};
77

88
use super::low_level::{CountingMode, OutputCompareMode, OutputPolarity, Timer};
99
use super::{Channel, Channel1Pin, Channel2Pin, Channel3Pin, Channel4Pin, GeneralInstance4Channel, TimerBits};
10-
use crate::gpio::{AfType, AnyPin, OutputType, Speed};
10+
use crate::gpio::{AfType, AnyPin, OutputType, Pull, Speed};
1111
use crate::time::Hertz;
1212
use crate::Peripheral;
1313

@@ -28,6 +28,15 @@ pub struct PwmPin<'d, T, C> {
2828
phantom: PhantomData<(T, C)>,
2929
}
3030

31+
/// PWM pin config
32+
///
33+
/// This configures the pwm pin settings
34+
pub struct PwmPinConfig {
35+
pub output_type: OutputType,
36+
pub speed: Speed,
37+
pub pull: Pull,
38+
}
39+
3140
macro_rules! channel_impl {
3241
($new_chx:ident, $channel:ident, $pin_trait:ident) => {
3342
impl<'d, T: GeneralInstance4Channel> PwmPin<'d, T, $channel> {
@@ -43,6 +52,24 @@ macro_rules! channel_impl {
4352
phantom: PhantomData,
4453
}
4554
}
55+
#[doc = concat!("Create a new ", stringify!($channel), "_with_config PWM pin instance.")]
56+
pub fn $new_chx_with_config(
57+
pin: impl Peripheral<P = impl $pin_trait<T>> + 'd,
58+
pin_config: PwmPinConfig,
59+
) -> Self {
60+
into_ref!(pin);
61+
critical_section::with(|_| {
62+
pin.set_low();
63+
pin.set_as_af(
64+
pin.af_num(),
65+
AfType::output_pull(pin_config.output_type, pin_config.speed, pin_config.pull),
66+
);
67+
});
68+
PwmPin {
69+
_pin: pin.map_into(),
70+
phantom: PhantomData,
71+
}
72+
}
4673
}
4774
};
4875
}

0 commit comments

Comments
 (0)