From c9f29534d6f3fd5bcf2cee09e70667373a8dc179 Mon Sep 17 00:00:00 2001
From: huntc <huntchr@gmail.com>
Date: Tue, 25 Jan 2022 16:51:24 +1100
Subject: [PATCH] Stop PWM before assigning the new sequence

I had introduced a small bug in my last PR where I assigned the sequence before stopping the PWM. I now stop the PWM before doing that now.

Also, corrected a math comment.
---
 embassy-nrf/src/pwm.rs                   | 4 ++--
 examples/nrf/src/bin/pwm_sequence.rs     | 2 +-
 examples/nrf/src/bin/pwm_sequence_ppi.rs | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs
index 61a282833..a77cd6332 100644
--- a/embassy-nrf/src/pwm.rs
+++ b/embassy-nrf/src/pwm.rs
@@ -149,6 +149,8 @@ impl<'d, T: Instance> SequencePwm<'d, T> {
             return Err(Error::SequenceTimesAtLeastOne);
         }
 
+        self.stop();
+
         let r = T::regs();
 
         r.seq0
@@ -165,8 +167,6 @@ impl<'d, T: Instance> SequencePwm<'d, T> {
             .cnt
             .write(|w| unsafe { w.bits(sequence.len() as u32) });
 
-        self.stop();
-
         r.enable.write(|w| w.enable().enabled());
 
         // defensive before seqstart
diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs
index 561bc1dd1..56c865d1c 100644
--- a/examples/nrf/src/bin/pwm_sequence.rs
+++ b/examples/nrf/src/bin/pwm_sequence.rs
@@ -18,7 +18,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
 
     let mut config = SequenceConfig::default();
     config.prescaler = Prescaler::Div128;
-    // 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8ms
+    // 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8us
     // but say we want to hold the value for 5000ms
     // so we want to repeat our value as many times as necessary until 5000ms passes
     // want 5000/8 = 625 periods total to occur, so 624 (we get the one period for free remember)
diff --git a/examples/nrf/src/bin/pwm_sequence_ppi.rs b/examples/nrf/src/bin/pwm_sequence_ppi.rs
index 213ef5ed7..f03c5716a 100644
--- a/examples/nrf/src/bin/pwm_sequence_ppi.rs
+++ b/examples/nrf/src/bin/pwm_sequence_ppi.rs
@@ -20,7 +20,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
 
     let mut config = SequenceConfig::default();
     config.prescaler = Prescaler::Div128;
-    // 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8ms
+    // 1 period is 1000 * (128/16mhz = 0.000008s = 0.008ms) = 8us
     // but say we want to hold the value for 250ms 250ms/8 = 31.25 periods
     // so round to 31 - 1 (we get the one period for free remember)
     // thus our sequence takes 5 * 250ms or 1.25 seconds