File tree Expand file tree Collapse file tree 2 files changed +31
-3
lines changed Expand file tree Collapse file tree 2 files changed +31
-3
lines changed Original file line number Diff line number Diff line change @@ -67,14 +67,20 @@ const APP: () = {
67
67
let led3 = p0. p0_15 . into_push_pull_output ( Level :: High ) . degrade ( ) ;
68
68
let led4 = p0. p0_16 . into_push_pull_output ( Level :: High ) . degrade ( ) ;
69
69
70
- let pwm = Pwm :: new ( ctx. device . PWM0 ) ;
70
+ let mut pwm = Pwm :: new ( ctx. device . PWM0 ) ;
71
71
pwm. set_period ( 500u32 . hz ( ) )
72
72
. set_output_pin ( Channel :: C0 , led1)
73
73
. set_output_pin ( Channel :: C1 , led2)
74
74
. set_output_pin ( Channel :: C2 , led3)
75
75
. set_output_pin ( Channel :: C3 , led4)
76
- . enable_interrupt ( PwmEvent :: Stopped )
77
- . enable ( ) ;
76
+ . enable_interrupt ( PwmEvent :: Stopped ) ;
77
+
78
+ // In addition to `set_output_pin`, `swap_output_pin` and `clear_output_pin` can be used to
79
+ // get the old pin back.
80
+ let led1 = pwm. clear_output_pin ( Channel :: C0 ) . unwrap ( ) ;
81
+ assert ! ( pwm. swap_output_pin( Channel :: C0 , led1) . is_none( ) ) ;
82
+
83
+ pwm. enable ( ) ;
78
84
79
85
let gpiote = Gpiote :: new ( ctx. device . GPIOTE ) ;
80
86
gpiote. port ( ) . input_pin ( & btn1) . low ( ) ;
Original file line number Diff line number Diff line change @@ -130,6 +130,8 @@ where
130
130
}
131
131
132
132
/// Sets the associated output pin for the PWM channel.
133
+ ///
134
+ /// Modifying the pin configuration while the PWM instance is enabled is not recommended.
133
135
#[ inline( always) ]
134
136
pub fn set_output_pin ( & self , channel : Channel , pin : Pin < Output < PushPull > > ) -> & Self {
135
137
self . pwm . psel . out [ usize:: from ( channel) ] . write ( |w| {
@@ -140,6 +142,8 @@ where
140
142
}
141
143
142
144
/// Sets the output pin of `channel`, and returns the old pin (if any).
145
+ ///
146
+ /// Modifying the pin configuration while the PWM instance is enabled is not recommended.
143
147
pub fn swap_output_pin (
144
148
& mut self ,
145
149
channel : Channel ,
@@ -157,6 +161,24 @@ where
157
161
old
158
162
}
159
163
164
+ /// Disables the output pin of `channel`.
165
+ ///
166
+ /// The output pin is returned, if one was previously configured.
167
+ ///
168
+ /// Modifying the pin configuration while the PWM instance is enabled is not recommended.
169
+ pub fn clear_output_pin ( & mut self , channel : Channel ) -> Option < Pin < Output < PushPull > > > {
170
+ // (needs `&mut self` because it reads, then writes, to the register)
171
+ let psel = & self . pwm . psel . out [ usize:: from ( channel) ] ;
172
+ let old = psel. read ( ) ;
173
+ let old = if old. connect ( ) . is_connected ( ) {
174
+ unsafe { Some ( Pin :: from_psel_bits ( old. bits ( ) ) ) }
175
+ } else {
176
+ None
177
+ } ;
178
+ psel. reset ( ) ;
179
+ old
180
+ }
181
+
160
182
/// Enables the PWM generator.
161
183
#[ inline( always) ]
162
184
pub fn enable ( & self ) {
You can’t perform that action at this time.
0 commit comments