aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Rosenthal <[email protected]>2021-11-01 20:18:24 -0700
committerJacob Rosenthal <[email protected]>2021-11-01 20:50:18 -0700
commit4647792ad68209f3be6e1cdf1cee9513025c14ab (patch)
treecb22c6714eacfceb2511a23f6d19c26836c7ed22
parent49253152cff2a45bd08cd1801bb6d5f0f3ce9b30 (diff)
seperate start from pwmseq::new
-rw-r--r--embassy-nrf/src/pwm.rs37
-rw-r--r--examples/nrf/src/bin/pwm_sequence.rs4
-rw-r--r--examples/nrf/src/bin/pwm_simple_sin.rs2
3 files changed, 28 insertions, 15 deletions
diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs
index a59fddf74..9454ad4a8 100644
--- a/embassy-nrf/src/pwm.rs
+++ b/embassy-nrf/src/pwm.rs
@@ -59,9 +59,9 @@ pub struct PwmSeq<'d, T: Instance> {
59} 59}
60 60
61impl<'d, T: Instance> PwmSeq<'d, T> { 61impl<'d, T: Instance> PwmSeq<'d, T> {
62 /// Creates the interface to a PWM instance. 62 /// Creates the interface to a PWM Sequence interface.
63 /// 63 ///
64 /// Defaults the freq to 1Mhz, max_duty 32767, duty 0, and channels low. 64 /// Must be started by calling `start`
65 /// 65 ///
66 /// # Safety 66 /// # Safety
67 /// 67 ///
@@ -82,9 +82,6 @@ impl<'d, T: Instance> PwmSeq<'d, T> {
82 if config.sequence.len() > 32767 { 82 if config.sequence.len() > 32767 {
83 return Err(Error::SequenceTooLong); 83 return Err(Error::SequenceTooLong);
84 } 84 }
85 if let SequenceMode::Times(0) = config.times {
86 return Err(Error::SequenceTooShort);
87 }
88 85
89 unborrow!(ch0, ch1, ch2, ch3); 86 unborrow!(ch0, ch1, ch2, ch3);
90 87
@@ -155,7 +152,27 @@ impl<'d, T: Instance> PwmSeq<'d, T> {
155 .enddelay 152 .enddelay
156 .write(|w| unsafe { w.bits(config.end_delay) }); 153 .write(|w| unsafe { w.bits(config.end_delay) });
157 154
158 match config.times { 155 Ok(Self {
156 phantom: PhantomData,
157 })
158 }
159
160 /// Start or restart playback
161 #[inline(always)]
162 pub fn start(&self, times: SequenceMode) -> Result<(), Error> {
163 if let SequenceMode::Times(0) = times {
164 return Err(Error::SequenceNoZero);
165 }
166 let r = T::regs();
167
168 r.shorts.reset();
169
170 // tasks_stop doesnt exist in all svds so write its bit instead
171 r.tasks_stop.write(|w| unsafe { w.bits(0x01) });
172
173 r.enable.write(|w| w.enable().enabled());
174
175 match times {
159 // just the one time, no loop count 176 // just the one time, no loop count
160 SequenceMode::Times(1) => { 177 SequenceMode::Times(1) => {
161 r.loop_.write(|w| w.cnt().disabled()); 178 r.loop_.write(|w| w.cnt().disabled());
@@ -189,9 +206,7 @@ impl<'d, T: Instance> PwmSeq<'d, T> {
189 } 206 }
190 } 207 }
191 208
192 Ok(Self { 209 Ok(())
193 phantom: PhantomData,
194 })
195 } 210 }
196 211
197 /// Stop playback 212 /// Stop playback
@@ -248,8 +263,6 @@ pub struct SequenceConfig<'a> {
248 pub refresh: u32, 263 pub refresh: u32,
249 /// Number of Times PWM periods after the sequence ends before starting the next sequence 264 /// Number of Times PWM periods after the sequence ends before starting the next sequence
250 pub end_delay: u32, 265 pub end_delay: u32,
251 /// How many times to play the sequence
252 pub times: SequenceMode,
253} 266}
254 267
255#[derive(Debug, Clone, Copy, PartialEq, Eq)] 268#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -259,7 +272,7 @@ pub enum Error {
259 /// Max Sequence size is 32767 272 /// Max Sequence size is 32767
260 SequenceTooLong, 273 SequenceTooLong,
261 /// Min Sequence size is 1 274 /// Min Sequence size is 1
262 SequenceTooShort, 275 SequenceNoZero,
263 /// EasyDMA can only read from data memory, read only buffers in flash will fail. 276 /// EasyDMA can only read from data memory, read only buffers in flash will fail.
264 DMABufferNotInDataMemory, 277 DMABufferNotInDataMemory,
265} 278}
diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs
index 0a7bea1c4..6911d0348 100644
--- a/examples/nrf/src/bin/pwm_sequence.rs
+++ b/examples/nrf/src/bin/pwm_sequence.rs
@@ -26,12 +26,12 @@ async fn main(_spawner: Spawner, p: Peripherals) {
26 sequence_load: SequenceLoad::Individual, 26 sequence_load: SequenceLoad::Individual,
27 refresh: 0, 27 refresh: 0,
28 end_delay: 0, 28 end_delay: 0,
29 times: SequenceMode::Times(5),
30 }; 29 };
31 30
32 let _pwm = unwrap!(PwmSeq::new( 31 let pwm = unwrap!(PwmSeq::new(
33 p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config 32 p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config
34 )); 33 ));
34 unwrap!(pwm.start(SequenceMode::Times(5)));
35 info!("pwm started!"); 35 info!("pwm started!");
36 36
37 loop { 37 loop {
diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs
index 6fd59c6a4..985f9d80a 100644
--- a/examples/nrf/src/bin/pwm_simple_sin.rs
+++ b/examples/nrf/src/bin/pwm_simple_sin.rs
@@ -30,10 +30,10 @@ async fn main(_spawner: Spawner, p: Peripherals) {
30 sequence_load: SequenceLoad::Common, 30 sequence_load: SequenceLoad::Common,
31 refresh: 0, 31 refresh: 0,
32 end_delay: 0, 32 end_delay: 0,
33 times: SequenceMode::Infinite,
34 }; 33 };
35 34
36 let pwm = unwrap!(PwmSeq::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config)); 35 let pwm = unwrap!(PwmSeq::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config));
36 unwrap!(pwm.start(SequenceMode::Infinite));
37 info!("pwm started!"); 37 info!("pwm started!");
38 38
39 Timer::after(Duration::from_millis(20000)).await; 39 Timer::after(Duration::from_millis(20000)).await;