summaryrefslogtreecommitdiff
path: root/embassy-nrf/src/pwm.rs
diff options
context:
space:
mode:
authorhuntc <huntchr@gmail.com>2022-01-30 16:21:23 +1100
committerhuntc <huntchr@gmail.com>2022-01-30 16:21:23 +1100
commit1c67bd46433734d9280e976da33975cf5beb773e (patch)
treea832657ca21a0446769a6c39faedce33045ce9ee /embassy-nrf/src/pwm.rs
parent482389a6911d8d3505872e6ad03d5b0af565eaf9 (diff)
downloadembassy-1c67bd46433734d9280e976da33975cf5beb773e.zip
Revert "Own the sequence buffer"
This reverts commit 482389a6911d8d3505872e6ad03d5b0af565eaf9.
Diffstat (limited to 'embassy-nrf/src/pwm.rs')
-rw-r--r--embassy-nrf/src/pwm.rs108
1 files changed, 32 insertions, 76 deletions
diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs
index 9146160c..94dfdeda 100644
--- a/embassy-nrf/src/pwm.rs
+++ b/embassy-nrf/src/pwm.rs
@@ -25,14 +25,14 @@ pub struct SimplePwm<'d, T: Instance> {
/// SequencePwm allows you to offload the updating of a sequence of duty cycles
/// to up to four channels, as well as repeat that sequence n times.
-pub struct SequencePwm<'d, T: Instance, const S0: usize, const S1: usize> {
+pub struct SequencePwm<'d, T: Instance> {
phantom: PhantomData<&'d mut T>,
ch0: Option<AnyPin>,
ch1: Option<AnyPin>,
ch2: Option<AnyPin>,
ch3: Option<AnyPin>,
- sequence0: Option<Sequence<S0>>,
- sequence1: Option<Sequence<S1>>,
+ sequence0: Option<Sequence<'d>>,
+ sequence1: Option<Sequence<'d>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -43,17 +43,13 @@ pub enum Error {
SequenceTooLong,
/// Min Sequence count is 1
SequenceTimesAtLeastOne,
- /// Sequence 0 is required, Sequence 1 is NOT required
- SequenceTimesRequireSeq0Only,
- /// Sequence 0 is required, Sequence 1 is required
- SequenceTimesRequireBothSeq0AndSeq1,
/// EasyDMA can only read from data memory, read only buffers in flash will fail.
DMABufferNotInDataMemory,
}
const MAX_SEQUENCE_LEN: usize = 32767;
-impl<'d, T: Instance, const S0: usize, const S1: usize> SequencePwm<'d, T, S0, S1> {
+impl<'d, T: Instance> SequencePwm<'d, T> {
/// Creates the interface to a `SequencePwm`.
///
/// Must be started by calling `start`
@@ -72,10 +68,6 @@ impl<'d, T: Instance, const S0: usize, const S1: usize> SequencePwm<'d, T, S0, S
ch3: impl Unborrow<Target = impl GpioOptionalPin> + 'd,
config: Config,
) -> Result<Self, Error> {
- if S0 > MAX_SEQUENCE_LEN || S1 > MAX_SEQUENCE_LEN {
- return Err(Error::SequenceTooLong);
- }
-
unborrow!(ch0, ch1, ch2, ch3);
let r = T::regs();
@@ -141,49 +133,31 @@ impl<'d, T: Instance, const S0: usize, const S1: usize> SequencePwm<'d, T, S0, S
}
/// Start or restart playback. Takes at least one sequence along with its
- /// configuration. A second sequence must be provided when looping i.e.
- /// when the sequence mode is anything other than Times(1).
+ /// configuration. Optionally takes a second sequence and its configuration.
+ /// In the case where no second sequence is provided then the first sequence
+ /// is used. The sequence mode applies to both sequences combined as one.
#[inline(always)]
pub fn start(
&mut self,
- sequence0: Sequence<S0>,
- sequence1: Sequence<S1>,
+ sequence0: Sequence<'d>,
+ sequence1: Option<Sequence<'d>>,
times: SequenceMode,
) -> Result<(), Error> {
- slice_in_ram_or(&sequence0.words, Error::DMABufferNotInDataMemory)?;
- slice_in_ram_or(&sequence1.words, Error::DMABufferNotInDataMemory)?;
+ let alt_sequence = sequence1.as_ref().unwrap_or(&sequence0);
+
+ slice_in_ram_or(sequence0.words, Error::DMABufferNotInDataMemory)?;
+ slice_in_ram_or(alt_sequence.words, Error::DMABufferNotInDataMemory)?;
- let seq_0_word_count = sequence0.word_count.unwrap_or(S0);
- let seq_1_word_count = sequence0.word_count.unwrap_or(S1);
- if seq_0_word_count > S0 || seq_1_word_count > S1 {
+ if sequence0.words.len() > MAX_SEQUENCE_LEN || alt_sequence.words.len() > MAX_SEQUENCE_LEN {
return Err(Error::SequenceTooLong);
}
- match times {
- SequenceMode::Times(0) => return Err(Error::SequenceTimesAtLeastOne),
- SequenceMode::Times(1) if seq_0_word_count == 0 || seq_1_word_count != 0 => {
- return Err(Error::SequenceTimesRequireSeq0Only)
- }
- SequenceMode::Times(1) => (),
- SequenceMode::Times(_) | SequenceMode::Infinite
- if seq_0_word_count == 0 || seq_1_word_count == 0 =>
- {
- return Err(Error::SequenceTimesRequireBothSeq0AndSeq1)
- }
- SequenceMode::Times(_) | SequenceMode::Infinite => (),
+ if let SequenceMode::Times(0) = times {
+ return Err(Error::SequenceTimesAtLeastOne);
}
let _ = self.stop();
- // We now own these sequences and they will be moved. We want
- // the peripheral to point at the right bits of memory hence
- // moving the sequences early.
- self.sequence0 = Some(sequence0);
- self.sequence1 = Some(sequence1);
-
- let sequence0 = self.sequence0.as_ref().unwrap();
- let sequence1 = self.sequence1.as_ref().unwrap();
-
let r = T::regs();
r.seq0
@@ -197,20 +171,20 @@ impl<'d, T: Instance, const S0: usize, const S1: usize> SequencePwm<'d, T, S0, S
.write(|w| unsafe { w.bits(sequence0.words.as_ptr() as u32) });
r.seq0
.cnt
- .write(|w| unsafe { w.bits(seq_0_word_count as u32) });
+ .write(|w| unsafe { w.bits(sequence0.words.len() as u32) });
r.seq1
.refresh
- .write(|w| unsafe { w.bits(sequence1.config.refresh) });
+ .write(|w| unsafe { w.bits(alt_sequence.config.refresh) });
r.seq1
.enddelay
- .write(|w| unsafe { w.bits(sequence1.config.end_delay) });
+ .write(|w| unsafe { w.bits(alt_sequence.config.end_delay) });
r.seq1
.ptr
- .write(|w| unsafe { w.bits(sequence1.words.as_ptr() as u32) });
+ .write(|w| unsafe { w.bits(alt_sequence.words.as_ptr() as u32) });
r.seq1
.cnt
- .write(|w| unsafe { w.bits(seq_1_word_count as u32) });
+ .write(|w| unsafe { w.bits(alt_sequence.words.len() as u32) });
r.enable.write(|w| w.enable().enabled());
@@ -252,6 +226,9 @@ impl<'d, T: Instance, const S0: usize, const S1: usize> SequencePwm<'d, T, S0, S
}
}
+ self.sequence0 = Some(sequence0);
+ self.sequence1 = sequence1;
+
Ok(())
}
@@ -359,7 +336,7 @@ impl<'d, T: Instance, const S0: usize, const S1: usize> SequencePwm<'d, T, S0, S
/// cycle from the pin. Returns any sequences previously provided to
/// `start` so that they may be further mutated.
#[inline(always)]
- pub fn stop(&mut self) -> (Option<Sequence<S0>>, Option<Sequence<S1>>) {
+ pub fn stop(&mut self) -> (Option<Sequence<'d>>, Option<Sequence<'d>>) {
let r = T::regs();
r.shorts.reset();
@@ -375,7 +352,7 @@ impl<'d, T: Instance, const S0: usize, const S1: usize> SequencePwm<'d, T, S0, S
}
}
-impl<'a, T: Instance, const S0: usize, const S1: usize> Drop for SequencePwm<'a, T, S0, S1> {
+impl<'a, T: Instance> Drop for SequencePwm<'a, T> {
fn drop(&mut self) {
let r = T::regs();
@@ -404,7 +381,6 @@ impl<'a, T: Instance, const S0: usize, const S1: usize> Drop for SequencePwm<'a,
}
}
-/// Configuration for the PWM as a whole.
#[non_exhaustive]
pub struct Config {
/// Selects up mode or up-and-down mode for the counter
@@ -428,7 +404,6 @@ impl Default for Config {
}
}
-/// Configuration per sequence
#[non_exhaustive]
#[derive(Clone)]
pub struct SequenceConfig {
@@ -447,39 +422,20 @@ impl Default for SequenceConfig {
}
}
-/// A composition of a sequence buffer and its configuration.
#[non_exhaustive]
-#[derive(Clone)]
-pub struct Sequence<const S: usize> {
+pub struct Sequence<'d> {
/// The words comprising the sequence. Must not exceed 32767 words.
- pub words: [u16; S],
- /// The count of words to use. If None the S will be used.
- pub word_count: Option<usize>,
+ pub words: &'d mut [u16],
/// Configuration associated with the sequence.
pub config: SequenceConfig,
}
-impl<const S: usize> Sequence<S> {
- pub const fn new(words: [u16; S], config: SequenceConfig) -> Self {
- Self {
- words,
- word_count: None,
- config,
- }
+impl<'d> Sequence<'d> {
+ pub fn new(words: &'d mut [u16], config: SequenceConfig) -> Self {
+ Self { words, config }
}
}
-/// Declares an empty sequence which will cause it to be disabled.
-/// Note that any looping i.e. !Times(1), will require a second
-/// sequence given the way the PWM peripheral works.
-pub const EMPTY_SEQ: Sequence<0> = Sequence::new(
- [],
- SequenceConfig {
- refresh: 0,
- end_delay: 0,
- },
-);
-
/// How many times to run the sequence
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum SequenceMode {
@@ -490,7 +446,7 @@ pub enum SequenceMode {
/// 5 to 6 = Run sequence 0, sequence 1, sequence 0, sequence 1, sequence 0 and then sequence 1
/// i.e the when >= 2 the loop count is determined by dividing by 2 and rounding up
Times(u16),
- /// Repeat until `stop` is called. Both sequences must be provided.
+ /// Repeat until `stop` is called.
Infinite,
}