diff options
| -rw-r--r-- | embassy-nrf/src/pwm.rs | 58 |
1 files changed, 51 insertions, 7 deletions
diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index b0acd83c1..8a6ed27f3 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs | |||
| @@ -7,7 +7,7 @@ use embassy::util::Unborrow; | |||
| 7 | use embassy_hal_common::unborrow; | 7 | use embassy_hal_common::unborrow; |
| 8 | 8 | ||
| 9 | use crate::gpio::sealed::Pin as _; | 9 | use crate::gpio::sealed::Pin as _; |
| 10 | use crate::gpio::OptionalPin as GpioOptionalPin; | 10 | use crate::gpio::{AnyPin, OptionalPin as GpioOptionalPin, Pin}; |
| 11 | use crate::interrupt::Interrupt; | 11 | use crate::interrupt::Interrupt; |
| 12 | use crate::pac; | 12 | use crate::pac; |
| 13 | use crate::util::slice_in_ram_or; | 13 | use crate::util::slice_in_ram_or; |
| @@ -15,10 +15,18 @@ use crate::util::slice_in_ram_or; | |||
| 15 | /// Interface to the PWM peripheral | 15 | /// Interface to the PWM peripheral |
| 16 | pub struct SimplePwm<'d, T: Instance> { | 16 | pub struct SimplePwm<'d, T: Instance> { |
| 17 | phantom: PhantomData<&'d mut T>, | 17 | phantom: PhantomData<&'d mut T>, |
| 18 | ch0: Option<AnyPin>, | ||
| 19 | ch1: Option<AnyPin>, | ||
| 20 | ch2: Option<AnyPin>, | ||
| 21 | ch3: Option<AnyPin>, | ||
| 18 | } | 22 | } |
| 19 | 23 | ||
| 20 | pub struct SequencePwm<'d, T: Instance> { | 24 | pub struct SequencePwm<'d, T: Instance> { |
| 21 | phantom: PhantomData<&'d mut T>, | 25 | phantom: PhantomData<&'d mut T>, |
| 26 | ch0: Option<AnyPin>, | ||
| 27 | ch1: Option<AnyPin>, | ||
| 28 | ch2: Option<AnyPin>, | ||
| 29 | ch3: Option<AnyPin>, | ||
| 22 | } | 30 | } |
| 23 | 31 | ||
| 24 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 32 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| @@ -130,6 +138,10 @@ impl<'d, T: Instance> SequencePwm<'d, T> { | |||
| 130 | 138 | ||
| 131 | Ok(Self { | 139 | Ok(Self { |
| 132 | phantom: PhantomData, | 140 | phantom: PhantomData, |
| 141 | ch0: ch0.degrade_optional(), | ||
| 142 | ch1: ch1.degrade_optional(), | ||
| 143 | ch2: ch2.degrade_optional(), | ||
| 144 | ch3: ch3.degrade_optional(), | ||
| 133 | }) | 145 | }) |
| 134 | } | 146 | } |
| 135 | 147 | ||
| @@ -206,9 +218,24 @@ impl<'a, T: Instance> Drop for SequencePwm<'a, T> { | |||
| 206 | self.stop(); | 218 | self.stop(); |
| 207 | self.disable(); | 219 | self.disable(); |
| 208 | 220 | ||
| 209 | info!("pwm drop: done"); | 221 | if let Some(pin) = &self.ch0 { |
| 222 | pin.set_low(); | ||
| 223 | pin.conf().write(|w| w); | ||
| 224 | } | ||
| 225 | if let Some(pin) = &self.ch1 { | ||
| 226 | pin.set_low(); | ||
| 227 | pin.conf().write(|w| w); | ||
| 228 | } | ||
| 229 | if let Some(pin) = &self.ch2 { | ||
| 230 | pin.set_low(); | ||
| 231 | pin.conf().write(|w| w); | ||
| 232 | } | ||
| 233 | if let Some(pin) = &self.ch3 { | ||
| 234 | pin.set_low(); | ||
| 235 | pin.conf().write(|w| w); | ||
| 236 | } | ||
| 210 | 237 | ||
| 211 | // TODO: disable pins | 238 | info!("pwm drop: done"); |
| 212 | } | 239 | } |
| 213 | } | 240 | } |
| 214 | 241 | ||
| @@ -360,6 +387,10 @@ impl<'d, T: Instance> SimplePwm<'d, T> { | |||
| 360 | 387 | ||
| 361 | Self { | 388 | Self { |
| 362 | phantom: PhantomData, | 389 | phantom: PhantomData, |
| 390 | ch0: ch0.degrade_optional(), | ||
| 391 | ch1: ch1.degrade_optional(), | ||
| 392 | ch2: ch2.degrade_optional(), | ||
| 393 | ch3: ch3.degrade_optional(), | ||
| 363 | } | 394 | } |
| 364 | } | 395 | } |
| 365 | 396 | ||
| @@ -374,7 +405,6 @@ impl<'d, T: Instance> SimplePwm<'d, T> { | |||
| 374 | r.tasks_stop.write(|w| unsafe { w.bits(0x01) }); | 405 | r.tasks_stop.write(|w| unsafe { w.bits(0x01) }); |
| 375 | } | 406 | } |
| 376 | 407 | ||
| 377 | // todo should this do.. something useful | ||
| 378 | /// Enables the PWM generator. | 408 | /// Enables the PWM generator. |
| 379 | #[inline(always)] | 409 | #[inline(always)] |
| 380 | pub fn enable(&self) { | 410 | pub fn enable(&self) { |
| @@ -382,7 +412,6 @@ impl<'d, T: Instance> SimplePwm<'d, T> { | |||
| 382 | r.enable.write(|w| w.enable().enabled()); | 412 | r.enable.write(|w| w.enable().enabled()); |
| 383 | } | 413 | } |
| 384 | 414 | ||
| 385 | // todo should this stop the task? or should you just use set_duty to 0? | ||
| 386 | /// Disables the PWM generator. | 415 | /// Disables the PWM generator. |
| 387 | #[inline(always)] | 416 | #[inline(always)] |
| 388 | pub fn disable(&self) { | 417 | pub fn disable(&self) { |
| @@ -461,9 +490,24 @@ impl<'a, T: Instance> Drop for SimplePwm<'a, T> { | |||
| 461 | self.stop(); | 490 | self.stop(); |
| 462 | self.disable(); | 491 | self.disable(); |
| 463 | 492 | ||
| 464 | info!("pwm drop: done"); | 493 | if let Some(pin) = &self.ch0 { |
| 494 | pin.set_low(); | ||
| 495 | pin.conf().write(|w| w); | ||
| 496 | } | ||
| 497 | if let Some(pin) = &self.ch1 { | ||
| 498 | pin.set_low(); | ||
| 499 | pin.conf().write(|w| w); | ||
| 500 | } | ||
| 501 | if let Some(pin) = &self.ch2 { | ||
| 502 | pin.set_low(); | ||
| 503 | pin.conf().write(|w| w); | ||
| 504 | } | ||
| 505 | if let Some(pin) = &self.ch3 { | ||
| 506 | pin.set_low(); | ||
| 507 | pin.conf().write(|w| w); | ||
| 508 | } | ||
| 465 | 509 | ||
| 466 | // TODO: disable pins | 510 | info!("pwm drop: done"); |
| 467 | } | 511 | } |
| 468 | } | 512 | } |
| 469 | 513 | ||
