@@ -129,16 +129,56 @@ where
129129 }
130130 }
131131
132- /// Sets the associated output pin for the PWM channel and enables it.
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.
133135 #[ inline( always) ]
134- pub fn set_output_pin ( & self , channel : Channel , pin : & Pin < Output < PushPull > > ) -> & Self {
136+ pub fn set_output_pin ( & self , channel : Channel , pin : Pin < Output < PushPull > > ) -> & Self {
135137 self . pwm . psel . out [ usize:: from ( channel) ] . write ( |w| {
136138 unsafe { w. bits ( pin. psel_bits ( ) ) } ;
137139 w. connect ( ) . connected ( )
138140 } ) ;
139141 self
140142 }
141143
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.
147+ pub fn swap_output_pin (
148+ & mut self ,
149+ channel : Channel ,
150+ pin : Pin < Output < PushPull > > ,
151+ ) -> Option < Pin < Output < PushPull > > > {
152+ // (needs `&mut self` because it reads, then writes, to the register)
153+ let psel = & self . pwm . psel . out [ usize:: from ( channel) ] ;
154+ let old = psel. read ( ) ;
155+ let old = if old. connect ( ) . is_connected ( ) {
156+ unsafe { Some ( Pin :: from_psel_bits ( old. bits ( ) ) ) }
157+ } else {
158+ None
159+ } ;
160+ self . set_output_pin ( channel, pin) ;
161+ old
162+ }
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+
142182 /// Enables the PWM generator.
143183 #[ inline( always) ]
144184 pub fn enable ( & self ) {
@@ -228,7 +268,7 @@ where
228268 self
229269 }
230270
231- /// Returns selected operating mode of the wave counter.
271+ /// Returns selected operating mode of the wave counter.
232272 #[ inline( always) ]
233273 pub fn counter_mode ( & self ) -> CounterMode {
234274 match self . pwm . mode . read ( ) . updown ( ) . bit ( ) {
@@ -351,7 +391,7 @@ where
351391 self . start_seq ( Seq :: Seq0 ) ;
352392 }
353393
354- /// Returns duty cycle value for a PWM group.
394+ /// Returns duty cycle value for a PWM group.
355395 #[ inline( always) ]
356396 pub fn duty_on_group ( & self , group : Group ) -> u16 {
357397 self . duty_on_value ( usize:: from ( group) )
@@ -395,7 +435,7 @@ where
395435 self . start_seq ( Seq :: Seq0 ) ;
396436 }
397437
398- /// Returns the duty cycle value for a PWM channel.
438+ /// Returns the duty cycle value for a PWM channel.
399439 #[ inline( always) ]
400440 pub fn duty_on ( & self , channel : Channel ) -> u16 {
401441 self . duty_on_value ( usize:: from ( channel) )
0 commit comments