diff options
| author | Vincenzo Marturano <[email protected]> | 2024-10-25 12:54:06 +0200 |
|---|---|---|
| committer | Vincenzo Marturano <[email protected]> | 2024-10-25 12:54:06 +0200 |
| commit | 71fe8a7b90abc7b625d0f5b822508b350f3444d2 (patch) | |
| tree | f382ec95a9a5e45e28f42b9dace3cdc807a8765b | |
| parent | 874dbec5a4d11c57c3683a27ac09584e728694c2 (diff) | |
Fixed owned split and implemented split_by_ref.
| -rw-r--r-- | embassy-rp/src/pwm.rs | 80 |
1 files changed, 60 insertions, 20 deletions
diff --git a/embassy-rp/src/pwm.rs b/embassy-rp/src/pwm.rs index 443308158..18d476ed4 100644 --- a/embassy-rp/src/pwm.rs +++ b/embassy-rp/src/pwm.rs | |||
| @@ -347,40 +347,80 @@ impl<'d> Pwm<'d> { | |||
| 347 | 347 | ||
| 348 | #[inline] | 348 | #[inline] |
| 349 | /// Split Pwm driver to allow separate duty cycle control of each channel | 349 | /// Split Pwm driver to allow separate duty cycle control of each channel |
| 350 | pub fn split(self) -> (Option<PwmOutput<'d>>, Option<PwmOutput<'d>>) { | 350 | pub fn split(mut self) -> (Option<PwmOutput<'d>>, Option<PwmOutput<'d>>) { |
| 351 | 351 | ( | |
| 352 | let pwm_output_a = if let Some(pin_a) = self.pin_a { | 352 | self.pin_a |
| 353 | Some(PwmOutput::new(PwmChannelPin::A(pin_a), self.slice.clone())) | 353 | .take() |
| 354 | }else{ | 354 | .map(|pin| PwmOutput::new(PwmChannelPin::A(pin), self.slice.clone(), true)), |
| 355 | None | 355 | self.pin_b |
| 356 | }; | 356 | .take() |
| 357 | 357 | .map(|pin| PwmOutput::new(PwmChannelPin::B(pin), self.slice.clone(), true)), | |
| 358 | let pwm_output_b = if let Some(pin_b) = self.pin_b { | 358 | ) |
| 359 | Some(PwmOutput::new(PwmChannelPin::B(pin_b), self.slice.clone())) | ||
| 360 | }else { | ||
| 361 | None | ||
| 362 | }; | ||
| 363 | |||
| 364 | (pwm_output_a,pwm_output_b) | ||
| 365 | } | 359 | } |
| 366 | 360 | ||
| 361 | pub fn split_by_ref(&mut self) -> (Option<PwmOutput<'_>>, Option<PwmOutput<'_>>) { | ||
| 362 | ( | ||
| 363 | self.pin_a | ||
| 364 | .as_mut() | ||
| 365 | .map(|pin| PwmOutput::new(PwmChannelPin::A(pin.reborrow()), self.slice.clone(), false)), | ||
| 366 | self.pin_b | ||
| 367 | .as_mut() | ||
| 368 | .map(|pin| PwmOutput::new(PwmChannelPin::B(pin.reborrow()), self.slice.clone(), false)), | ||
| 369 | ) | ||
| 370 | } | ||
| 367 | } | 371 | } |
| 368 | 372 | ||
| 369 | enum PwmChannelPin<'d> { | 373 | enum PwmChannelPin<'d> { |
| 370 | A(PeripheralRef<'d, AnyPin>), | 374 | A(PeripheralRef<'d, AnyPin>), |
| 371 | B(PeripheralRef<'d, AnyPin>) | 375 | B(PeripheralRef<'d, AnyPin>), |
| 372 | } | 376 | } |
| 373 | 377 | ||
| 374 | /// Single channel of Pwm driver. | 378 | /// Single channel of Pwm driver. |
| 375 | pub struct PwmOutput<'d> { | 379 | pub struct PwmOutput<'d> { |
| 376 | //pin that can be ether ChannelAPin or ChannelBPin | 380 | //pin that can be ether ChannelAPin or ChannelBPin |
| 377 | channel_pin: PwmChannelPin<'d> , | 381 | channel_pin: PwmChannelPin<'d>, |
| 378 | slice: usize, | 382 | slice: usize, |
| 383 | is_owned: bool, | ||
| 384 | } | ||
| 385 | |||
| 386 | impl<'d> PwmOutput<'d> { | ||
| 387 | fn new(channel_pin: PwmChannelPin<'d>, slice: usize, is_owned: bool) -> Self { | ||
| 388 | Self { | ||
| 389 | channel_pin, | ||
| 390 | slice, | ||
| 391 | is_owned, | ||
| 392 | } | ||
| 393 | } | ||
| 379 | } | 394 | } |
| 380 | 395 | ||
| 381 | impl <'d> PwmOutput<'d> { | 396 | impl<'d> Drop for PwmOutput<'d> { |
| 382 | fn new(channel_pin: PwmChannelPin<'d>, slice: usize) -> Self { | 397 | fn drop(&mut self) { |
| 383 | Self { channel_pin ,slice } | 398 | if self.is_owned { |
| 399 | let p = pac::PWM.ch(self.slice); | ||
| 400 | match &self.channel_pin { | ||
| 401 | PwmChannelPin::A(pin) => { | ||
| 402 | p.cc().modify(|w| { | ||
| 403 | w.set_a(0); | ||
| 404 | }); | ||
| 405 | |||
| 406 | pin.gpio().ctrl().write(|w| w.set_funcsel(31)); | ||
| 407 | ///Enable pin PULL-DOWN | ||
| 408 | pin.pad_ctrl().modify(|w| { | ||
| 409 | w.set_pde(true); | ||
| 410 | }); | ||
| 411 | } | ||
| 412 | PwmChannelPin::B(pin) => { | ||
| 413 | p.cc().modify(|w| { | ||
| 414 | w.set_b(0); | ||
| 415 | }); | ||
| 416 | pin.gpio().ctrl().write(|w| w.set_funcsel(31)); | ||
| 417 | ///Enable pin PULL-DOWN | ||
| 418 | pin.pad_ctrl().modify(|w| { | ||
| 419 | w.set_pde(true); | ||
| 420 | }); | ||
| 421 | } | ||
| 422 | } | ||
| 423 | } | ||
| 384 | } | 424 | } |
| 385 | } | 425 | } |
| 386 | 426 | ||
