@@ -7,6 +7,8 @@ use embassy_hal_internal::{into_ref, PeripheralRef};
7
7
8
8
use super :: low_level:: { CountingMode , OutputCompareMode , OutputPolarity , Timer } ;
9
9
use super :: { Channel , Channel1Pin , Channel2Pin , Channel3Pin , Channel4Pin , GeneralInstance4Channel , TimerBits } ;
10
+ #[ cfg( gpio_v2) ]
11
+ use crate :: gpio:: Pull ;
10
12
use crate :: gpio:: { AfType , AnyPin , OutputType , Speed } ;
11
13
use crate :: time:: Hertz ;
12
14
use crate :: Peripheral ;
@@ -28,8 +30,21 @@ pub struct PwmPin<'d, T, C> {
28
30
phantom : PhantomData < ( T , C ) > ,
29
31
}
30
32
33
+ /// PWM pin config
34
+ ///
35
+ /// This configures the pwm pin settings
36
+ pub 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
+
31
46
macro_rules! channel_impl {
32
- ( $new_chx: ident, $channel: ident, $pin_trait: ident) => {
47
+ ( $new_chx: ident, $new_chx_with_config : ident , $ channel: ident, $pin_trait: ident) => {
33
48
impl <' d, T : GeneralInstance4Channel > PwmPin <' d, T , $channel> {
34
49
#[ doc = concat!( "Create a new " , stringify!( $channel) , " PWM pin instance." ) ]
35
50
pub fn $new_chx( pin: impl Peripheral <P = impl $pin_trait<T >> + ' d, output_type: OutputType ) -> Self {
@@ -43,14 +58,36 @@ macro_rules! channel_impl {
43
58
phantom: PhantomData ,
44
59
}
45
60
}
61
+
62
+ #[ doc = concat!( "Create a new " , stringify!( $channel) , " PWM pin instance with config." ) ]
63
+ pub fn $new_chx_with_config(
64
+ pin: impl Peripheral <P = impl $pin_trait<T >> + ' d,
65
+ pin_config: PwmPinConfig ,
66
+ ) -> Self {
67
+ into_ref!( pin) ;
68
+ critical_section:: with( |_| {
69
+ pin. set_low( ) ;
70
+ pin. set_as_af(
71
+ pin. af_num( ) ,
72
+ #[ cfg( gpio_v1) ]
73
+ AfType :: output( pin_config. output_type, pin_config. speed) ,
74
+ #[ cfg( gpio_v2) ]
75
+ AfType :: output_pull( pin_config. output_type, pin_config. speed, pin_config. pull) ,
76
+ ) ;
77
+ } ) ;
78
+ PwmPin {
79
+ _pin: pin. map_into( ) ,
80
+ phantom: PhantomData ,
81
+ }
82
+ }
46
83
}
47
84
} ;
48
85
}
49
86
50
- channel_impl ! ( new_ch1, Ch1 , Channel1Pin ) ;
51
- channel_impl ! ( new_ch2, Ch2 , Channel2Pin ) ;
52
- channel_impl ! ( new_ch3, Ch3 , Channel3Pin ) ;
53
- channel_impl ! ( new_ch4, Ch4 , Channel4Pin ) ;
87
+ channel_impl ! ( new_ch1, new_ch1_with_config , Ch1 , Channel1Pin ) ;
88
+ channel_impl ! ( new_ch2, new_ch2_with_config , Ch2 , Channel2Pin ) ;
89
+ channel_impl ! ( new_ch3, new_ch3_with_config , Ch3 , Channel3Pin ) ;
90
+ channel_impl ! ( new_ch4, new_ch4_with_config , Ch4 , Channel4Pin ) ;
54
91
55
92
/// A single channel of a pwm, obtained from [`SimplePwm::split`],
56
93
/// [`SimplePwm::channel`], [`SimplePwm::ch1`], etc.
0 commit comments