aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Rosenthal <[email protected]>2021-11-01 08:45:07 -0700
committerJacob Rosenthal <[email protected]>2021-11-01 08:45:07 -0700
commit90be851e4b52703b4297615253bc7a89fdd03b9f (patch)
treeaa896346e249915f38183dd84237d51c6898210c
parent48673e27cd5e1f879d758533bec020bc2e7cc0c2 (diff)
reduce complexity of loopmode
-rw-r--r--embassy-nrf/src/pwm.rs38
-rw-r--r--examples/nrf/src/bin/pwm_sequence.rs2
-rw-r--r--examples/nrf/src/bin/pwm_simple_sin.rs4
3 files changed, 25 insertions, 19 deletions
diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs
index 19b653ca7..8775c8896 100644
--- a/embassy-nrf/src/pwm.rs
+++ b/embassy-nrf/src/pwm.rs
@@ -56,8 +56,8 @@ pub struct Pwm<'d, T: Instance> {
56 56
57#[derive(Debug, Eq, PartialEq, Clone, Copy)] 57#[derive(Debug, Eq, PartialEq, Clone, Copy)]
58pub enum LoopMode { 58pub enum LoopMode {
59 /// Repeat n additional times after the first 59 /// Run sequence n Times total
60 Additional(u16), 60 Times(u16),
61 /// Repeat until `stop` is called. 61 /// Repeat until `stop` is called.
62 Infinite, 62 Infinite,
63} 63}
@@ -74,12 +74,12 @@ pub struct LoopingConfig<'a> {
74 pub sequence: &'a [u16], 74 pub sequence: &'a [u16],
75 /// How a sequence is read from RAM and is spread to the compare register 75 /// How a sequence is read from RAM and is spread to the compare register
76 pub sequence_load: SequenceLoad, 76 pub sequence_load: SequenceLoad,
77 /// Number of additional PWM periods to delay between each sequence sample 77 /// Number of Times PWM periods to delay between each sequence sample
78 pub refresh: u32, 78 pub refresh: u32,
79 /// Number of additional PWM periods after the sequence ends before starting the next sequence 79 /// Number of Times PWM periods after the sequence ends before starting the next sequence
80 pub end_delay: u32, 80 pub end_delay: u32,
81 /// How many times to repeat the sequence 81 /// How many times to play the sequence
82 pub additional_loops: LoopMode, 82 pub times: LoopMode,
83} 83}
84 84
85#[derive(Debug, Clone, Copy, PartialEq, Eq)] 85#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -88,6 +88,8 @@ pub struct LoopingConfig<'a> {
88pub enum Error { 88pub enum Error {
89 /// Max Sequence size is 32767 89 /// Max Sequence size is 32767
90 SequenceTooLong, 90 SequenceTooLong,
91 /// Min Sequence size is 1
92 SequenceTooShort,
91 /// EasyDMA can only read from data memory, read only buffers in flash will fail. 93 /// EasyDMA can only read from data memory, read only buffers in flash will fail.
92 DMABufferNotInDataMemory, 94 DMABufferNotInDataMemory,
93} 95}
@@ -177,6 +179,9 @@ impl<'d, T: Instance> Pwm<'d, T> {
177 if config.sequence.len() > 32767 { 179 if config.sequence.len() > 32767 {
178 return Err(Error::SequenceTooLong); 180 return Err(Error::SequenceTooLong);
179 } 181 }
182 if let LoopMode::Times(0) = config.times {
183 return Err(Error::SequenceTooShort);
184 }
180 185
181 unborrow!(ch0, ch1, ch2, ch3); 186 unborrow!(ch0, ch1, ch2, ch3);
182 187
@@ -243,27 +248,28 @@ impl<'d, T: Instance> Pwm<'d, T> {
243 .enddelay 248 .enddelay
244 .write(|w| unsafe { w.bits(config.end_delay) }); 249 .write(|w| unsafe { w.bits(config.end_delay) });
245 250
246 match config.additional_loops { 251 match config.times {
247 // just the one time, no loop count 252 // just the one time, no loop count
248 LoopMode::Additional(0) => { 253 LoopMode::Times(1) => {
249 r.loop_.write(|w| w.cnt().disabled()); 254 r.loop_.write(|w| w.cnt().disabled());
250 // tasks_seqstart doesnt exist in all svds so write its bit instead 255 // tasks_seqstart doesnt exist in all svds so write its bit instead
251 r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); 256 r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) });
252 } 257 }
253 // loop count is how many times to play BOTH sequences 258 // loop count is how many times to play BOTH sequences
254 // the one time + 1 = 2 total, play the sequence once starting from seq0 259 // 2 total (1 x 2)
255 // the one time + 2 = 3 total, playing the sequence twice would be too much, but we can start on seq1 to subtract one 260 // 3 total, (2 x 2) - 1
256 // the one time + 3 = 4 total, play the sequence twice starting from seq0 261 LoopMode::Times(n) => {
257 LoopMode::Additional(n) => { 262 let odd = n & 1 == 1;
258 let times = (n / 2) + 1; 263 let times = if odd { (n / 2) + 1 } else { n / 2 };
264
259 r.loop_.write(|w| unsafe { w.cnt().bits(times) }); 265 r.loop_.write(|w| unsafe { w.cnt().bits(times) });
260 266
261 if n & 1 == 1 { 267 if odd {
262 // tasks_seqstart doesnt exist in all svds so write its bit instead 268 // tasks_seqstart doesnt exist in all svds so write its bit instead
263 r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); 269 r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) });
264 } else { 270 } else {
265 // tasks_seqstart doesnt exist in all svds so write its bit instead 271 // tasks_seqstart doesnt exist in all svds so write its bit instead
266 r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); 272 r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) });
267 } 273 }
268 } 274 }
269 // to play infinitely, repeat the sequence one time, then have loops done self trigger seq0 again 275 // to play infinitely, repeat the sequence one time, then have loops done self trigger seq0 again
diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs
index 1c5ab2682..40ba7ab1c 100644
--- a/examples/nrf/src/bin/pwm_sequence.rs
+++ b/examples/nrf/src/bin/pwm_sequence.rs
@@ -24,7 +24,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
24 sequence_load: SequenceLoad::Individual, 24 sequence_load: SequenceLoad::Individual,
25 refresh: 0, 25 refresh: 0,
26 end_delay: 0, 26 end_delay: 0,
27 additional_loops: LoopMode::Additional(5), 27 times: LoopMode::Times(5),
28 }; 28 };
29 29
30 let _pwm = unwrap!(Pwm::simple_playback( 30 let _pwm = unwrap!(Pwm::simple_playback(
diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs
index 909b8bf3a..c1af0db8c 100644
--- a/examples/nrf/src/bin/pwm_simple_sin.rs
+++ b/examples/nrf/src/bin/pwm_simple_sin.rs
@@ -27,8 +27,8 @@ async fn main(_spawner: Spawner, p: Peripherals) {
27 sequence: &seq_values, 27 sequence: &seq_values,
28 sequence_load: SequenceLoad::Common, 28 sequence_load: SequenceLoad::Common,
29 refresh: 0, 29 refresh: 0,
30 end_delay: 1, 30 end_delay: 0,
31 additional_loops: LoopMode::Infinite, 31 times: LoopMode::Infinite,
32 }; 32 };
33 33
34 let pwm = unwrap!(Pwm::simple_playback( 34 let pwm = unwrap!(Pwm::simple_playback(