diff options
| author | Vincenzo Marturano <[email protected]> | 2024-10-24 19:36:54 +0200 |
|---|---|---|
| committer | Vincenzo Marturano <[email protected]> | 2024-10-24 19:36:54 +0200 |
| commit | 336ef01b05e87e6b2b3c9b98e9bdca0c7d78a6af (patch) | |
| tree | 49c1c57d4bca9c5cced057419cf20beb7d014f4e /embassy-rp | |
| parent | 052463212b6b6c1647f517ce38272dd4dd3d4353 (diff) | |
Implemented owned split.
Diffstat (limited to 'embassy-rp')
| -rw-r--r-- | embassy-rp/src/pwm.rs | 47 |
1 files changed, 29 insertions, 18 deletions
diff --git a/embassy-rp/src/pwm.rs b/embassy-rp/src/pwm.rs index 66f0dc7f2..5fea0901a 100644 --- a/embassy-rp/src/pwm.rs +++ b/embassy-rp/src/pwm.rs | |||
| @@ -347,25 +347,36 @@ 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) -> (PwmOutput,PwmOutput){ | 350 | pub fn split(self) -> (Option<PwmOutput>, Option<PwmOutput>) { |
| 351 | (PwmOutput::new(PwmChannel::A,self.slice.clone()),PwmOutput::new(PwmChannel::B,self.slice.clone())) | 351 | |
| 352 | let pwm_output_a = if let Some(pin_a) = self.pin_a { | ||
| 353 | Some(PwmOutput::new(PwmChannelPin::A(pin_a), self.slice.clone())) | ||
| 354 | }; | ||
| 355 | |||
| 356 | let pwm_output_b = if let Some(pin_b) = self.pin_b { | ||
| 357 | Some(PwmOutput::new(PwmChannelPin::B(pin_b), self.slice.clone())) | ||
| 358 | }; | ||
| 359 | |||
| 360 | (pwm_output_a,pwm_output_b) | ||
| 352 | } | 361 | } |
| 362 | |||
| 353 | } | 363 | } |
| 354 | 364 | ||
| 355 | enum PwmChannel{ | 365 | enum PwmChannelPin<'d> { |
| 356 | A, | 366 | A(PeripheralRef<'d, AnyPin>), |
| 357 | B | 367 | B(PeripheralRef<'d, AnyPin>) |
| 358 | } | 368 | } |
| 359 | 369 | ||
| 360 | /// Single channel of Pwm driver. | 370 | /// Single channel of Pwm driver. |
| 361 | pub struct PwmOutput { | 371 | pub struct PwmOutput<'d> { |
| 362 | channel: PwmChannel, | 372 | //pin that can be ether ChannelAPin or ChannelBPin |
| 363 | slice: usize | 373 | channel_pin: PwmChannelPin<'d> , |
| 374 | slice: usize, | ||
| 364 | } | 375 | } |
| 365 | 376 | ||
| 366 | impl PwmOutput { | 377 | impl <'d> PwmOutput<'d> { |
| 367 | fn new(channel: PwmChannel,slice: usize) -> Self{ | 378 | fn new(channel_pin: PwmChannelPin<'d>, slice: usize) -> Self { |
| 368 | Self { channel, slice } | 379 | Self { channel_pin ,slice } |
| 369 | } | 380 | } |
| 370 | } | 381 | } |
| 371 | 382 | ||
| @@ -373,7 +384,7 @@ impl ErrorType for PwmOutput { | |||
| 373 | type Error = PwmError; | 384 | type Error = PwmError; |
| 374 | } | 385 | } |
| 375 | 386 | ||
| 376 | impl SetDutyCycle for PwmOutput { | 387 | impl<'d> SetDutyCycle for PwmOutput<'d> { |
| 377 | fn max_duty_cycle(&self) -> u16 { | 388 | fn max_duty_cycle(&self) -> u16 { |
| 378 | pac::PWM.ch(self.slice).top().read().top() | 389 | pac::PWM.ch(self.slice).top().read().top() |
| 379 | } | 390 | } |
| @@ -385,19 +396,19 @@ impl SetDutyCycle for PwmOutput { | |||
| 385 | } | 396 | } |
| 386 | 397 | ||
| 387 | let p = pac::PWM.ch(self.slice); | 398 | let p = pac::PWM.ch(self.slice); |
| 388 | match self.channel { | 399 | match self.channel_pin { |
| 389 | PwmChannel::A => { | 400 | PwmChannelPin::A => { |
| 390 | p.cc().modify(|w| { | 401 | p.cc().modify(|w| { |
| 391 | w.set_a(duty); | 402 | w.set_a(duty); |
| 392 | }); | 403 | }); |
| 393 | }, | 404 | } |
| 394 | PwmChannel::B => { | 405 | PwmChannelPin::B => { |
| 395 | p.cc().modify(|w| { | 406 | p.cc().modify(|w| { |
| 396 | w.set_b(duty); | 407 | w.set_b(duty); |
| 397 | }); | 408 | }); |
| 398 | }, | 409 | } |
| 399 | } | 410 | } |
| 400 | 411 | ||
| 401 | Ok(()) | 412 | Ok(()) |
| 402 | } | 413 | } |
| 403 | } | 414 | } |
