diff options
| author | huntc <[email protected]> | 2022-01-28 16:21:53 +1100 |
|---|---|---|
| committer | huntc <[email protected]> | 2022-01-28 16:32:58 +1100 |
| commit | 9ac52a768bbcd4bc8b753c64805fc23906b2c91f (patch) | |
| tree | a832657ca21a0446769a6c39faedce33045ce9ee /embassy-nrf/src/pwm.rs | |
| parent | 8e9f4488662699877cf10335991554eb5407f940 (diff) | |
Now permits sequences to be mutated subsequently
Diffstat (limited to 'embassy-nrf/src/pwm.rs')
| -rw-r--r-- | embassy-nrf/src/pwm.rs | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 41dcce049..94dfdeda6 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs | |||
| @@ -31,6 +31,8 @@ pub struct SequencePwm<'d, T: Instance> { | |||
| 31 | ch1: Option<AnyPin>, | 31 | ch1: Option<AnyPin>, |
| 32 | ch2: Option<AnyPin>, | 32 | ch2: Option<AnyPin>, |
| 33 | ch3: Option<AnyPin>, | 33 | ch3: Option<AnyPin>, |
| 34 | sequence0: Option<Sequence<'d>>, | ||
| 35 | sequence1: Option<Sequence<'d>>, | ||
| 34 | } | 36 | } |
| 35 | 37 | ||
| 36 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 38 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| @@ -125,11 +127,13 @@ impl<'d, T: Instance> SequencePwm<'d, T> { | |||
| 125 | ch1: ch1.degrade_optional(), | 127 | ch1: ch1.degrade_optional(), |
| 126 | ch2: ch2.degrade_optional(), | 128 | ch2: ch2.degrade_optional(), |
| 127 | ch3: ch3.degrade_optional(), | 129 | ch3: ch3.degrade_optional(), |
| 130 | sequence0: None, | ||
| 131 | sequence1: None, | ||
| 128 | }) | 132 | }) |
| 129 | } | 133 | } |
| 130 | 134 | ||
| 131 | /// Start or restart playback. Takes at least one sequence along with its | 135 | /// Start or restart playback. Takes at least one sequence along with its |
| 132 | /// configuration. Optionally takes a second sequence and/or its configuration. | 136 | /// configuration. Optionally takes a second sequence and its configuration. |
| 133 | /// In the case where no second sequence is provided then the first sequence | 137 | /// In the case where no second sequence is provided then the first sequence |
| 134 | /// is used. The sequence mode applies to both sequences combined as one. | 138 | /// is used. The sequence mode applies to both sequences combined as one. |
| 135 | #[inline(always)] | 139 | #[inline(always)] |
| @@ -152,7 +156,7 @@ impl<'d, T: Instance> SequencePwm<'d, T> { | |||
| 152 | return Err(Error::SequenceTimesAtLeastOne); | 156 | return Err(Error::SequenceTimesAtLeastOne); |
| 153 | } | 157 | } |
| 154 | 158 | ||
| 155 | self.stop(); | 159 | let _ = self.stop(); |
| 156 | 160 | ||
| 157 | let r = T::regs(); | 161 | let r = T::regs(); |
| 158 | 162 | ||
| @@ -222,6 +226,9 @@ impl<'d, T: Instance> SequencePwm<'d, T> { | |||
| 222 | } | 226 | } |
| 223 | } | 227 | } |
| 224 | 228 | ||
| 229 | self.sequence0 = Some(sequence0); | ||
| 230 | self.sequence1 = sequence1; | ||
| 231 | |||
| 225 | Ok(()) | 232 | Ok(()) |
| 226 | } | 233 | } |
| 227 | 234 | ||
| @@ -326,9 +333,10 @@ impl<'d, T: Instance> SequencePwm<'d, T> { | |||
| 326 | } | 333 | } |
| 327 | 334 | ||
| 328 | /// Stop playback. Disables the peripheral. Does NOT clear the last duty | 335 | /// Stop playback. Disables the peripheral. Does NOT clear the last duty |
| 329 | /// cycle from the pin. | 336 | /// cycle from the pin. Returns any sequences previously provided to |
| 337 | /// `start` so that they may be further mutated. | ||
| 330 | #[inline(always)] | 338 | #[inline(always)] |
| 331 | pub fn stop(&self) { | 339 | pub fn stop(&mut self) -> (Option<Sequence<'d>>, Option<Sequence<'d>>) { |
| 332 | let r = T::regs(); | 340 | let r = T::regs(); |
| 333 | 341 | ||
| 334 | r.shorts.reset(); | 342 | r.shorts.reset(); |
| @@ -339,6 +347,8 @@ impl<'d, T: Instance> SequencePwm<'d, T> { | |||
| 339 | r.tasks_stop.write(|w| unsafe { w.bits(0x01) }); | 347 | r.tasks_stop.write(|w| unsafe { w.bits(0x01) }); |
| 340 | 348 | ||
| 341 | r.enable.write(|w| w.enable().disabled()); | 349 | r.enable.write(|w| w.enable().disabled()); |
| 350 | |||
| 351 | (self.sequence0.take(), self.sequence1.take()) | ||
| 342 | } | 352 | } |
| 343 | } | 353 | } |
| 344 | 354 | ||
| @@ -346,7 +356,7 @@ impl<'a, T: Instance> Drop for SequencePwm<'a, T> { | |||
| 346 | fn drop(&mut self) { | 356 | fn drop(&mut self) { |
| 347 | let r = T::regs(); | 357 | let r = T::regs(); |
| 348 | 358 | ||
| 349 | self.stop(); | 359 | let _ = self.stop(); |
| 350 | 360 | ||
| 351 | if let Some(pin) = &self.ch0 { | 361 | if let Some(pin) = &self.ch0 { |
| 352 | pin.set_low(); | 362 | pin.set_low(); |
| @@ -415,13 +425,13 @@ impl Default for SequenceConfig { | |||
| 415 | #[non_exhaustive] | 425 | #[non_exhaustive] |
| 416 | pub struct Sequence<'d> { | 426 | pub struct Sequence<'d> { |
| 417 | /// The words comprising the sequence. Must not exceed 32767 words. | 427 | /// The words comprising the sequence. Must not exceed 32767 words. |
| 418 | pub words: &'d [u16], | 428 | pub words: &'d mut [u16], |
| 419 | /// Configuration associated with the sequence. | 429 | /// Configuration associated with the sequence. |
| 420 | pub config: SequenceConfig, | 430 | pub config: SequenceConfig, |
| 421 | } | 431 | } |
| 422 | 432 | ||
| 423 | impl<'d> Sequence<'d> { | 433 | impl<'d> Sequence<'d> { |
| 424 | pub fn new(words: &'d [u16], config: SequenceConfig) -> Self { | 434 | pub fn new(words: &'d mut [u16], config: SequenceConfig) -> Self { |
| 425 | Self { words, config } | 435 | Self { words, config } |
| 426 | } | 436 | } |
| 427 | } | 437 | } |
