From eb0bf1fd7a33330425a12420e5d948ca6e88d74f Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Tue, 26 Oct 2021 00:37:52 -0700 Subject: [PATCH 01/47] simple_playback api from nrf sdk --- embassy-nrf/src/pwm.rs | 167 ++++++++++++++++++++++++++- embassy-nrf/src/util.rs | 4 +- examples/nrf/src/bin/pwm_sequence.rs | 41 +++++++ 3 files changed, 207 insertions(+), 5 deletions(-) create mode 100644 examples/nrf/src/bin/pwm_sequence.rs diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 5e996e88..ccab6f48 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -9,7 +9,8 @@ use embassy_hal_common::unborrow; use crate::gpio::sealed::Pin as _; use crate::gpio::OptionalPin as GpioOptionalPin; use crate::interrupt::Interrupt; -use crate::pac; +use crate::util::slice_in_ram_or; +use crate::{pac, EASY_DMA_SIZE}; #[derive(Debug, Eq, PartialEq, Clone, Copy)] pub enum Prescaler { @@ -23,11 +24,57 @@ pub enum Prescaler { Div128, } -/// Interface to the UARTE peripheral +#[derive(Debug, Eq, PartialEq, Clone, Copy)] +pub enum SequenceLoad { + Common, + Grouped, + Individual, + Waveform, +} + +#[derive(Debug, Eq, PartialEq, Clone, Copy)] +pub enum CounterMode { + Up, + UpAndDown, +} + +/// Interface to the PWM peripheral pub struct Pwm<'d, T: Instance> { phantom: PhantomData<&'d mut T>, } +// Configure an infinite looping sequence for `simple_playback` +pub struct LoopingConfig<'a> { + /// Selects up mode or up-and-down mode for the counter + pub counter_mode: CounterMode, + // top value to be compared against buffer values + pub top: u16, + /// Configuration for PWM_CLK + pub prescaler: Prescaler, + /// In ram buffer to be played back + pub sequence: &'a [u16], + /// Common Mode means seq in buffer will be used across all channels + /// Individual Mode buffer holds [ch0_0, ch1_0, ch2_0, ch3_0, ch0_1, ch1_1, + /// ch2_1, ch3_1 ... ch0_n, ch1_n, ch2_n, ch3_n] + pub sequence_load: SequenceLoad, + /// will instruct a new RAM stored pulse width value on every (N+1)th PWM + /// period. Setting the register to zero will result in a new duty cycle + /// update every PWM period as long as the minimum PWM period is observed. + pub repeats: u32, + /// enddelay PWM period delays between last period on sequence 0 before repeating + pub enddelay: u32, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +#[non_exhaustive] +pub enum Error { + Seq0BufferTooLong, + Seq1BufferTooLong, + /// EasyDMA can only read from data memory, read only buffers in flash will fail. + DMABufferNotInDataMemory, +} + impl<'d, T: Instance> Pwm<'d, T> { /// Creates the interface to a UARTE instance. /// Sets the baud rate, parity and assigns the pins to the UARTE peripheral. @@ -99,6 +146,120 @@ impl<'d, T: Instance> Pwm<'d, T> { } } + pub fn simple_playback( + _pwm: impl Unborrow + 'd, + ch0: impl Unborrow + 'd, + ch1: impl Unborrow + 'd, + ch2: impl Unborrow + 'd, + ch3: impl Unborrow + 'd, + config: LoopingConfig, + count: u16, + ) -> Result { + slice_in_ram_or(config.sequence, Error::DMABufferNotInDataMemory)?; + + if config.sequence.len() > EASY_DMA_SIZE { + return Err(Error::Seq0BufferTooLong); + } + + unborrow!(ch0, ch1, ch2, ch3); + + let odd: bool = count & 1 == 1; + + let r = T::regs(); + + if let Some(pin) = ch0.pin_mut() { + pin.set_low(); + pin.conf().write(|w| w.dir().output()); + r.psel.out[0].write(|w| unsafe { w.bits(ch0.psel_bits()) }); + } + if let Some(pin) = ch1.pin_mut() { + pin.set_low(); + pin.conf().write(|w| w.dir().output()); + r.psel.out[1].write(|w| unsafe { w.bits(ch1.psel_bits()) }); + } + if let Some(pin) = ch2.pin_mut() { + pin.set_low(); + pin.conf().write(|w| w.dir().output()); + r.psel.out[2].write(|w| unsafe { w.bits(ch2.psel_bits()) }); + } + if let Some(pin) = ch3.pin_mut() { + pin.set_low(); + pin.conf().write(|w| w.dir().output()); + r.psel.out[3].write(|w| unsafe { w.bits(ch3.psel_bits()) }); + } + + r.enable.write(|w| w.enable().enabled()); + + r.mode + .write(|w| unsafe { w.bits(config.counter_mode as u32) }); + r.prescaler + .write(|w| w.prescaler().bits(config.prescaler as u8)); + r.countertop + .write(|w| unsafe { w.countertop().bits(config.top) }); + + r.decoder.write(|w| { + w.load().bits(config.sequence_load as u8); + w.mode().refresh_count() + }); + + r.seq0 + .ptr + .write(|w| unsafe { w.bits(config.sequence.as_ptr() as u32) }); + r.seq0 + .cnt + .write(|w| unsafe { w.bits(config.sequence.len() as u32) }); + r.seq0.refresh.write(|w| unsafe { w.bits(config.repeats) }); + r.seq0 + .enddelay + .write(|w| unsafe { w.bits(config.enddelay) }); + + r.seq1 + .ptr + .write(|w| unsafe { w.bits(config.sequence.as_ptr() as u32) }); + r.seq1 + .cnt + .write(|w| unsafe { w.bits(config.sequence.len() as u32) }); + r.seq1.refresh.write(|w| unsafe { w.bits(config.repeats) }); + r.seq1 + .enddelay + .write(|w| unsafe { w.bits(config.enddelay) }); + + let mut loop_: u16 = count / 2; + if odd { + loop_ += 1; + } + + r.loop_.write(|w| unsafe { w.cnt().bits(loop_) }); + + if odd { + r.shorts.write(|w| w.loopsdone_seqstart1().set_bit()); + } else { + r.shorts.write(|w| w.loopsdone_seqstart0().set_bit()); + } + + // tasks_seqstart doesnt exist in all svds so write its bit instead + if odd { + r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); + } else { + r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); + } + + Ok(Self { + phantom: PhantomData, + }) + } + + /// Stop playback + #[inline(always)] + pub fn stop(&self) { + let r = T::regs(); + + r.shorts.write(|w| unsafe { w.bits(0x0) }); + + // tasks_stop doesnt exist in all svds so write its bit instead + r.tasks_stop.write(|w| unsafe { w.bits(0x01) }); + } + /// Enables the PWM generator. #[inline(always)] pub fn enable(&self) { @@ -128,7 +289,7 @@ impl<'d, T: Instance> Pwm<'d, T> { T::regs().prescaler.write(|w| w.prescaler().bits(div as u8)); } - /// Sets the PWM clock prescaler. + /// Gets the PWM clock prescaler. #[inline(always)] pub fn prescaler(&self) -> Prescaler { match T::regs().prescaler.read().prescaler().bits() { diff --git a/embassy-nrf/src/util.rs b/embassy-nrf/src/util.rs index 344fb01f..d17ec503 100644 --- a/embassy-nrf/src/util.rs +++ b/embassy-nrf/src/util.rs @@ -2,14 +2,14 @@ const SRAM_LOWER: usize = 0x2000_0000; const SRAM_UPPER: usize = 0x3000_0000; /// Does this slice reside entirely within RAM? -pub(crate) fn slice_in_ram(slice: &[u8]) -> bool { +pub(crate) fn slice_in_ram(slice: &[T]) -> bool { let ptr = slice.as_ptr() as usize; ptr >= SRAM_LOWER && (ptr + slice.len()) < SRAM_UPPER } /// Return an error if slice is not in RAM. #[cfg(not(feature = "nrf51"))] -pub(crate) fn slice_in_ram_or(slice: &[u8], err: T) -> Result<(), T> { +pub(crate) fn slice_in_ram_or(slice: &[T], err: E) -> Result<(), E> { if slice.len() == 0 || slice_in_ram(slice) { Ok(()) } else { diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs new file mode 100644 index 00000000..93ee9f5b --- /dev/null +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -0,0 +1,41 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +#[path = "../example_common.rs"] +mod example_common; +use defmt::*; +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_nrf::gpio::NoPin; +use embassy_nrf::pwm::{CounterMode, LoopingConfig, Prescaler, Pwm, SequenceLoad}; +use embassy_nrf::Peripherals; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + let seq_values: [u16; 2] = [0, 0x8000]; + + let config = LoopingConfig { + counter_mode: CounterMode::Up, + top: 31250, + prescaler: Prescaler::Div128, + sequence: &seq_values, + sequence_load: SequenceLoad::Common, + repeats: 1, + enddelay: 0, + }; + + let pwm = unwrap!(Pwm::simple_playback( + p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config, 1 + )); + info!("pwm started!"); + + Timer::after(Duration::from_millis(10000)).await; + + pwm.stop(); + info!("pwm stopped!"); + + loop { + Timer::after(Duration::from_millis(1000)).await; + } +} From 1d1d8a848e165e2754720fa442571782616cb822 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Fri, 29 Oct 2021 16:39:41 -0700 Subject: [PATCH 02/47] simplify api, more interesting example --- embassy-nrf/src/pwm.rs | 22 +++------------------- examples/nrf/src/bin/pwm_sequence.rs | 14 +++++++------- 2 files changed, 10 insertions(+), 26 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index ccab6f48..64b5e547 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -153,7 +153,6 @@ impl<'d, T: Instance> Pwm<'d, T> { ch2: impl Unborrow + 'd, ch3: impl Unborrow + 'd, config: LoopingConfig, - count: u16, ) -> Result { slice_in_ram_or(config.sequence, Error::DMABufferNotInDataMemory)?; @@ -163,8 +162,6 @@ impl<'d, T: Instance> Pwm<'d, T> { unborrow!(ch0, ch1, ch2, ch3); - let odd: bool = count & 1 == 1; - let r = T::regs(); if let Some(pin) = ch0.pin_mut() { @@ -224,25 +221,12 @@ impl<'d, T: Instance> Pwm<'d, T> { .enddelay .write(|w| unsafe { w.bits(config.enddelay) }); - let mut loop_: u16 = count / 2; - if odd { - loop_ += 1; - } + r.loop_.write(|w| unsafe { w.cnt().bits(0x1) }); - r.loop_.write(|w| unsafe { w.cnt().bits(loop_) }); - - if odd { - r.shorts.write(|w| w.loopsdone_seqstart1().set_bit()); - } else { - r.shorts.write(|w| w.loopsdone_seqstart0().set_bit()); - } + r.shorts.write(|w| w.loopsdone_seqstart1().set_bit()); // tasks_seqstart doesnt exist in all svds so write its bit instead - if odd { - r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); - } else { - r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); - } + r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); Ok(Self { phantom: PhantomData, diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 93ee9f5b..bc356c28 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -7,26 +7,26 @@ mod example_common; use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; -use embassy_nrf::gpio::NoPin; use embassy_nrf::pwm::{CounterMode, LoopingConfig, Prescaler, Pwm, SequenceLoad}; use embassy_nrf::Peripherals; #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let seq_values: [u16; 2] = [0, 0x8000]; - + let seq_values: [u16; 16] = [ + 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, + ]; let config = LoopingConfig { counter_mode: CounterMode::Up, - top: 31250, + top: 15625, prescaler: Prescaler::Div128, sequence: &seq_values, - sequence_load: SequenceLoad::Common, - repeats: 1, + sequence_load: SequenceLoad::Individual, + repeats: 0, enddelay: 0, }; let pwm = unwrap!(Pwm::simple_playback( - p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config, 1 + p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config )); info!("pwm started!"); From ef95441442e0ccab0ac6d62264dedb7254d4cb84 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Fri, 29 Oct 2021 17:09:34 -0700 Subject: [PATCH 03/47] a runtime generated sin table example --- examples/nrf/Cargo.toml | 1 + examples/nrf/src/bin/pwm_simple_sin.rs | 41 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 examples/nrf/src/bin/pwm_simple_sin.rs diff --git a/examples/nrf/Cargo.toml b/examples/nrf/Cargo.toml index b89aa513..e0025b73 100644 --- a/examples/nrf/Cargo.toml +++ b/examples/nrf/Cargo.toml @@ -31,3 +31,4 @@ panic-probe = { version = "0.2.0", features = ["print-defmt"] } futures = { version = "0.3.17", default-features = false, features = ["async-await"] } rand = { version = "0.8.4", default-features = false } embedded-storage = "0.2.0" +micromath = "2.0.0" \ No newline at end of file diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs new file mode 100644 index 00000000..866202a4 --- /dev/null +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -0,0 +1,41 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] +#![feature(array_from_fn)] + +#[path = "../example_common.rs"] +mod example_common; +use defmt::*; +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_nrf::gpio::NoPin; +use embassy_nrf::pwm::{CounterMode, LoopingConfig, Prescaler, Pwm, SequenceLoad}; +use embassy_nrf::Peripherals; +use micromath::F32Ext; + +const W1: f32 = core::f32::consts::PI / 128.0; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + // probably not best use of resources to create the table at runtime, but makes testing fast + let seq_values: [u16; 220] = core::array::from_fn(|n| ((W1 * n as f32).sin() * 10000.0) as u16); + + let config = LoopingConfig { + counter_mode: CounterMode::UpAndDown, + top: 12000, + prescaler: Prescaler::Div16, + sequence: &seq_values, + sequence_load: SequenceLoad::Common, + repeats: 0, + enddelay: 0, + }; + + let _pwm = unwrap!(Pwm::simple_playback( + p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config + )); + info!("pwm started!"); + + loop { + Timer::after(Duration::from_millis(1000)).await; + } +} From 6d8198a46ae20be24c84fbb07d474d4eb507ac4d Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Sat, 30 Oct 2021 11:33:10 -0700 Subject: [PATCH 04/47] move psel back out of if --- embassy-nrf/src/pwm.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 64b5e547..d834b130 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -167,24 +167,28 @@ impl<'d, T: Instance> Pwm<'d, T> { if let Some(pin) = ch0.pin_mut() { pin.set_low(); pin.conf().write(|w| w.dir().output()); - r.psel.out[0].write(|w| unsafe { w.bits(ch0.psel_bits()) }); } + if let Some(pin) = ch1.pin_mut() { pin.set_low(); pin.conf().write(|w| w.dir().output()); - r.psel.out[1].write(|w| unsafe { w.bits(ch1.psel_bits()) }); } if let Some(pin) = ch2.pin_mut() { pin.set_low(); pin.conf().write(|w| w.dir().output()); - r.psel.out[2].write(|w| unsafe { w.bits(ch2.psel_bits()) }); } if let Some(pin) = ch3.pin_mut() { pin.set_low(); pin.conf().write(|w| w.dir().output()); - r.psel.out[3].write(|w| unsafe { w.bits(ch3.psel_bits()) }); } + // if NoPin provided writes disconnected (top bit 1) 0x80000000 else + // writes pin number ex 13 (0x0D) which is connected (top bit 0) + r.psel.out[0].write(|w| unsafe { w.bits(ch0.psel_bits()) }); + r.psel.out[1].write(|w| unsafe { w.bits(ch1.psel_bits()) }); + r.psel.out[2].write(|w| unsafe { w.bits(ch2.psel_bits()) }); + r.psel.out[3].write(|w| unsafe { w.bits(ch3.psel_bits()) }); + r.enable.write(|w| w.enable().enabled()); r.mode @@ -323,6 +327,7 @@ impl<'d, T: Instance> Pwm<'d, T> { impl<'a, T: Instance> Drop for Pwm<'a, T> { fn drop(&mut self) { let r = T::regs(); + r.enable.write(|w| w.enable().disabled()); info!("pwm drop: done"); From ee8f76537b2dd8e886f44bd7e43d4ba9fd767778 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Sat, 30 Oct 2021 11:33:28 -0700 Subject: [PATCH 05/47] at least stop on drop --- embassy-nrf/src/pwm.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index d834b130..42294312 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -328,6 +328,7 @@ impl<'a, T: Instance> Drop for Pwm<'a, T> { fn drop(&mut self) { let r = T::regs(); + self.stop(); r.enable.write(|w| w.enable().disabled()); info!("pwm drop: done"); From 763e250dfea4425912674ed68d77c3291b0505e0 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Sat, 30 Oct 2021 16:16:10 -0700 Subject: [PATCH 06/47] add ability to configure loop count from 1 to infinite --- embassy-nrf/src/pwm.rs | 69 +++++++++++++++++++------- examples/nrf/src/bin/pwm_sequence.rs | 13 ++--- examples/nrf/src/bin/pwm_simple_sin.rs | 12 +++-- 3 files changed, 65 insertions(+), 29 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 42294312..3f05ed58 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -24,10 +24,13 @@ pub enum Prescaler { Div128, } +/// How a sequence is read from RAM and is spread to the compare register #[derive(Debug, Eq, PartialEq, Clone, Copy)] pub enum SequenceLoad { + /// sequence in buffer will be used across all channels Common, Grouped, + /// buffer holds [ch0_0, ch1_0, ch2_0, ch3_0... ch0_n, ch1_n, ch2_n, ch3_n] Individual, Waveform, } @@ -43,26 +46,32 @@ pub struct Pwm<'d, T: Instance> { phantom: PhantomData<&'d mut T>, } +#[derive(Debug, Eq, PartialEq, Clone, Copy)] +pub enum LoopMode { + // Repeat n additional times after the first + Additional(u16), + /// Repeat until `stop` is called + Infinite, +} + // Configure an infinite looping sequence for `simple_playback` pub struct LoopingConfig<'a> { /// Selects up mode or up-and-down mode for the counter pub counter_mode: CounterMode, - // top value to be compared against buffer values + // Top value to be compared against buffer values pub top: u16, /// Configuration for PWM_CLK pub prescaler: Prescaler, /// In ram buffer to be played back pub sequence: &'a [u16], - /// Common Mode means seq in buffer will be used across all channels - /// Individual Mode buffer holds [ch0_0, ch1_0, ch2_0, ch3_0, ch0_1, ch1_1, - /// ch2_1, ch3_1 ... ch0_n, ch1_n, ch2_n, ch3_n] + /// How a sequence is read from RAM and is spread to the compare register pub sequence_load: SequenceLoad, - /// will instruct a new RAM stored pulse width value on every (N+1)th PWM - /// period. Setting the register to zero will result in a new duty cycle - /// update every PWM period as long as the minimum PWM period is observed. - pub repeats: u32, - /// enddelay PWM period delays between last period on sequence 0 before repeating + /// Number of additional PWM periods between samples loaded into compare register + pub refresh: u32, + /// Number of additional PWM periods after the sequence ends pub enddelay: u32, + /// How many times to repeat the sequence + pub additional_loops: LoopMode, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -146,6 +155,7 @@ impl<'d, T: Instance> Pwm<'d, T> { } } + /// Returns a configured pwm that has had start called on it pub fn simple_playback( _pwm: impl Unborrow + 'd, ch0: impl Unborrow + 'd, @@ -209,7 +219,7 @@ impl<'d, T: Instance> Pwm<'d, T> { r.seq0 .cnt .write(|w| unsafe { w.bits(config.sequence.len() as u32) }); - r.seq0.refresh.write(|w| unsafe { w.bits(config.repeats) }); + r.seq0.refresh.write(|w| unsafe { w.bits(config.refresh) }); r.seq0 .enddelay .write(|w| unsafe { w.bits(config.enddelay) }); @@ -220,17 +230,42 @@ impl<'d, T: Instance> Pwm<'d, T> { r.seq1 .cnt .write(|w| unsafe { w.bits(config.sequence.len() as u32) }); - r.seq1.refresh.write(|w| unsafe { w.bits(config.repeats) }); + r.seq1.refresh.write(|w| unsafe { w.bits(config.refresh) }); r.seq1 .enddelay .write(|w| unsafe { w.bits(config.enddelay) }); - r.loop_.write(|w| unsafe { w.cnt().bits(0x1) }); + match config.additional_loops { + LoopMode::Additional(0) => { + r.loop_.write(|w| w.cnt().disabled()); + r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); + } + LoopMode::Additional(n) => { + let times = (n / 2) + 1; - r.shorts.write(|w| w.loopsdone_seqstart1().set_bit()); + r.loop_.write(|w| unsafe { w.cnt().bits(times) }); + r.shorts.write(|w| { + w.loopsdone_seqstart1().enabled(); + w.loopsdone_seqstart0().disabled(); + w.loopsdone_stop().enabled() + }); - // tasks_seqstart doesnt exist in all svds so write its bit instead - r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); + if n & 1 == 1 { + r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); + } else { + r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); + } + } + + LoopMode::Infinite => { + r.loop_.write(|w| unsafe { w.cnt().bits(0x1) }); + r.shorts.write(|w| { + w.loopsdone_seqstart1().enabled(); + w.loopsdone_seqstart0().disabled() + }); + r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); + } + } Ok(Self { phantom: PhantomData, @@ -326,10 +361,8 @@ impl<'d, T: Instance> Pwm<'d, T> { impl<'a, T: Instance> Drop for Pwm<'a, T> { fn drop(&mut self) { - let r = T::regs(); - self.stop(); - r.enable.write(|w| w.enable().disabled()); + self.disable(); info!("pwm drop: done"); diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index bc356c28..8b7aeddc 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -7,7 +7,7 @@ mod example_common; use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; -use embassy_nrf::pwm::{CounterMode, LoopingConfig, Prescaler, Pwm, SequenceLoad}; +use embassy_nrf::pwm::{CounterMode, LoopMode, LoopingConfig, Prescaler, Pwm, SequenceLoad}; use embassy_nrf::Peripherals; #[embassy::main] @@ -15,26 +15,23 @@ async fn main(_spawner: Spawner, p: Peripherals) { let seq_values: [u16; 16] = [ 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, ]; + let config = LoopingConfig { counter_mode: CounterMode::Up, top: 15625, prescaler: Prescaler::Div128, sequence: &seq_values, sequence_load: SequenceLoad::Individual, - repeats: 0, + refresh: 0, enddelay: 0, + additional_loops: LoopMode::Additional(5), }; - let pwm = unwrap!(Pwm::simple_playback( + let _pwm = unwrap!(Pwm::simple_playback( p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config )); info!("pwm started!"); - Timer::after(Duration::from_millis(10000)).await; - - pwm.stop(); - info!("pwm stopped!"); - loop { Timer::after(Duration::from_millis(1000)).await; } diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index 866202a4..3fbfc960 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -9,7 +9,7 @@ use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_nrf::gpio::NoPin; -use embassy_nrf::pwm::{CounterMode, LoopingConfig, Prescaler, Pwm, SequenceLoad}; +use embassy_nrf::pwm::{CounterMode, LoopMode, LoopingConfig, Prescaler, Pwm, SequenceLoad}; use embassy_nrf::Peripherals; use micromath::F32Ext; @@ -26,15 +26,21 @@ async fn main(_spawner: Spawner, p: Peripherals) { prescaler: Prescaler::Div16, sequence: &seq_values, sequence_load: SequenceLoad::Common, - repeats: 0, + refresh: 0, enddelay: 0, + additional_loops: LoopMode::Infinite, }; - let _pwm = unwrap!(Pwm::simple_playback( + let pwm = unwrap!(Pwm::simple_playback( p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config )); info!("pwm started!"); + Timer::after(Duration::from_millis(20000)).await; + + pwm.stop(); + info!("pwm stopped!"); + loop { Timer::after(Duration::from_millis(1000)).await; } From 96df2fdb4343c701549bc77fa9bb06352e439f81 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Sat, 30 Oct 2021 16:23:45 -0700 Subject: [PATCH 07/47] lost comment --- embassy-nrf/src/pwm.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 3f05ed58..83d0bdae 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -251,8 +251,10 @@ impl<'d, T: Instance> Pwm<'d, T> { }); if n & 1 == 1 { + // tasks_seqstart doesnt exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); } else { + // tasks_seqstart doesnt exist in all svds so write its bit instead r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); } } From 78e382c9aa421b8319dcf1c9ea44ae046c1a019d Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Sun, 31 Oct 2021 23:00:33 -0700 Subject: [PATCH 08/47] stop->sequence_stop --- embassy-nrf/src/pwm.rs | 4 ++-- examples/nrf/src/bin/pwm_simple_sin.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 83d0bdae..8d4a9d8b 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -276,7 +276,7 @@ impl<'d, T: Instance> Pwm<'d, T> { /// Stop playback #[inline(always)] - pub fn stop(&self) { + pub fn sequence_stop(&self) { let r = T::regs(); r.shorts.write(|w| unsafe { w.bits(0x0) }); @@ -363,7 +363,7 @@ impl<'d, T: Instance> Pwm<'d, T> { impl<'a, T: Instance> Drop for Pwm<'a, T> { fn drop(&mut self) { - self.stop(); + self.sequence_stop(); self.disable(); info!("pwm drop: done"); diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index 3fbfc960..c7d85381 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -38,7 +38,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { Timer::after(Duration::from_millis(20000)).await; - pwm.stop(); + pwm.sequence_stop(); info!("pwm stopped!"); loop { From d98a1707b974660b585de1015787c0e5066d81d2 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Sun, 31 Oct 2021 23:28:26 -0700 Subject: [PATCH 09/47] using write dont need to clear --- embassy-nrf/src/pwm.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 8d4a9d8b..9672e6f2 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -246,7 +246,6 @@ impl<'d, T: Instance> Pwm<'d, T> { r.loop_.write(|w| unsafe { w.cnt().bits(times) }); r.shorts.write(|w| { w.loopsdone_seqstart1().enabled(); - w.loopsdone_seqstart0().disabled(); w.loopsdone_stop().enabled() }); From 72fa1c8482270e3004b7ef717a45cfe0bd3cde97 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 00:21:17 -0700 Subject: [PATCH 10/47] fix additional to remove stuck light at end --- embassy-nrf/src/pwm.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 9672e6f2..64a25f23 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -236,18 +236,19 @@ impl<'d, T: Instance> Pwm<'d, T> { .write(|w| unsafe { w.bits(config.enddelay) }); match config.additional_loops { + // just the one time, no loop count LoopMode::Additional(0) => { r.loop_.write(|w| w.cnt().disabled()); + // tasks_seqstart doesnt exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); } + // loop count is how many times to play BOTH sequences + // the one time + 1 = 2 total, play the sequence once starting from seq0 + // the one time + 2 = 3 total, playing the sequence twice would be too much, but we can start on seq1 to subtract one + // the one time + 3 = 4 total, play the sequence twice starting from seq0 LoopMode::Additional(n) => { let times = (n / 2) + 1; - r.loop_.write(|w| unsafe { w.cnt().bits(times) }); - r.shorts.write(|w| { - w.loopsdone_seqstart1().enabled(); - w.loopsdone_stop().enabled() - }); if n & 1 == 1 { // tasks_seqstart doesnt exist in all svds so write its bit instead @@ -257,13 +258,10 @@ impl<'d, T: Instance> Pwm<'d, T> { r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); } } - LoopMode::Infinite => { r.loop_.write(|w| unsafe { w.cnt().bits(0x1) }); - r.shorts.write(|w| { - w.loopsdone_seqstart1().enabled(); - w.loopsdone_seqstart0().disabled() - }); + r.shorts.write(|w| w.loopsdone_seqstart1().enabled()); + // tasks_seqstart doesnt exist in all svds so write its bit instead r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); } } From 7b092f463e630e7d8725f6af2d0138b608050b1f Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 00:30:42 -0700 Subject: [PATCH 11/47] clarify infinite --- embassy-nrf/src/pwm.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 64a25f23..5ed9cf14 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -258,11 +258,12 @@ impl<'d, T: Instance> Pwm<'d, T> { r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); } } + // to play infinitely, repeat the sequence one time, then have loops done self trigger seq0 again LoopMode::Infinite => { r.loop_.write(|w| unsafe { w.cnt().bits(0x1) }); - r.shorts.write(|w| w.loopsdone_seqstart1().enabled()); + r.shorts.write(|w| w.loopsdone_seqstart0().enabled()); // tasks_seqstart doesnt exist in all svds so write its bit instead - r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); + r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); } } From 14dc524b84b28cfa6f8a9649c38173d6a1977a9e Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 01:20:01 -0700 Subject: [PATCH 12/47] documentation --- embassy-nrf/src/pwm.rs | 19 ++++++++++++------- examples/nrf/src/bin/pwm_sequence.rs | 2 +- examples/nrf/src/bin/pwm_simple_sin.rs | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 5ed9cf14..5f3a4df8 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -27,11 +27,16 @@ pub enum Prescaler { /// How a sequence is read from RAM and is spread to the compare register #[derive(Debug, Eq, PartialEq, Clone, Copy)] pub enum SequenceLoad { - /// sequence in buffer will be used across all channels + /// Provided sequence will be used across all channels Common, + /// Provided sequence contains grouped values for each channel ex: + /// [ch0_0_and_ch1_0, ch2_0_and_ch3_0, ... ch0_n_and_ch1_n, ch2_n_and_ch3_n] Grouped, - /// buffer holds [ch0_0, ch1_0, ch2_0, ch3_0... ch0_n, ch1_n, ch2_n, ch3_n] + /// Provided sequence contains individual values for each channel ex: + /// [ch0_0, ch1_0, ch2_0, ch3_0... ch0_n, ch1_n, ch2_n, ch3_n] Individual, + /// Similar to Individual mode, but only three channels are used. The fourth + /// value is loaded into the pulse generator counter as its top value. Waveform, } @@ -66,10 +71,10 @@ pub struct LoopingConfig<'a> { pub sequence: &'a [u16], /// How a sequence is read from RAM and is spread to the compare register pub sequence_load: SequenceLoad, - /// Number of additional PWM periods between samples loaded into compare register + /// Number of additional PWM periods to delay between each sequence sample pub refresh: u32, - /// Number of additional PWM periods after the sequence ends - pub enddelay: u32, + /// Number of additional PWM periods after the sequence ends before starting the next sequence + pub end_delay: u32, /// How many times to repeat the sequence pub additional_loops: LoopMode, } @@ -222,7 +227,7 @@ impl<'d, T: Instance> Pwm<'d, T> { r.seq0.refresh.write(|w| unsafe { w.bits(config.refresh) }); r.seq0 .enddelay - .write(|w| unsafe { w.bits(config.enddelay) }); + .write(|w| unsafe { w.bits(config.end_delay) }); r.seq1 .ptr @@ -233,7 +238,7 @@ impl<'d, T: Instance> Pwm<'d, T> { r.seq1.refresh.write(|w| unsafe { w.bits(config.refresh) }); r.seq1 .enddelay - .write(|w| unsafe { w.bits(config.enddelay) }); + .write(|w| unsafe { w.bits(config.end_delay) }); match config.additional_loops { // just the one time, no loop count diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 8b7aeddc..1c5ab268 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -23,7 +23,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { sequence: &seq_values, sequence_load: SequenceLoad::Individual, refresh: 0, - enddelay: 0, + end_delay: 0, additional_loops: LoopMode::Additional(5), }; diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index c7d85381..909b8bf3 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -27,7 +27,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { sequence: &seq_values, sequence_load: SequenceLoad::Common, refresh: 0, - enddelay: 0, + end_delay: 1, additional_loops: LoopMode::Infinite, }; From 48673e27cd5e1f879d758533bec020bc2e7cc0c2 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 01:15:04 -0700 Subject: [PATCH 13/47] fix max sequence length --- embassy-nrf/src/pwm.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 5f3a4df8..19b653ca 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -9,9 +9,10 @@ use embassy_hal_common::unborrow; use crate::gpio::sealed::Pin as _; use crate::gpio::OptionalPin as GpioOptionalPin; use crate::interrupt::Interrupt; +use crate::pac; use crate::util::slice_in_ram_or; -use crate::{pac, EASY_DMA_SIZE}; +/// PWM Base clock is system clock (16MHz) divided by prescaler #[derive(Debug, Eq, PartialEq, Clone, Copy)] pub enum Prescaler { Div1, @@ -24,7 +25,7 @@ pub enum Prescaler { Div128, } -/// How a sequence is read from RAM and is spread to the compare register +/// How the sequence values are distributed across the channels #[derive(Debug, Eq, PartialEq, Clone, Copy)] pub enum SequenceLoad { /// Provided sequence will be used across all channels @@ -42,7 +43,9 @@ pub enum SequenceLoad { #[derive(Debug, Eq, PartialEq, Clone, Copy)] pub enum CounterMode { + /// Up counter (edge-aligned PWM duty cycle) Up, + /// Up and down counter (center-aligned PWM duty cycle) UpAndDown, } @@ -53,13 +56,13 @@ pub struct Pwm<'d, T: Instance> { #[derive(Debug, Eq, PartialEq, Clone, Copy)] pub enum LoopMode { - // Repeat n additional times after the first + /// Repeat n additional times after the first Additional(u16), - /// Repeat until `stop` is called + /// Repeat until `stop` is called. Infinite, } -// Configure an infinite looping sequence for `simple_playback` +/// Configure an infinite looping sequence for `simple_playback` pub struct LoopingConfig<'a> { /// Selects up mode or up-and-down mode for the counter pub counter_mode: CounterMode, @@ -83,8 +86,8 @@ pub struct LoopingConfig<'a> { #[cfg_attr(feature = "defmt", derive(defmt::Format))] #[non_exhaustive] pub enum Error { - Seq0BufferTooLong, - Seq1BufferTooLong, + /// Max Sequence size is 32767 + SequenceTooLong, /// EasyDMA can only read from data memory, read only buffers in flash will fail. DMABufferNotInDataMemory, } @@ -171,8 +174,8 @@ impl<'d, T: Instance> Pwm<'d, T> { ) -> Result { slice_in_ram_or(config.sequence, Error::DMABufferNotInDataMemory)?; - if config.sequence.len() > EASY_DMA_SIZE { - return Err(Error::Seq0BufferTooLong); + if config.sequence.len() > 32767 { + return Err(Error::SequenceTooLong); } unborrow!(ch0, ch1, ch2, ch3); From 90be851e4b52703b4297615253bc7a89fdd03b9f Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 08:45:07 -0700 Subject: [PATCH 14/47] reduce complexity of loopmode --- embassy-nrf/src/pwm.rs | 40 +++++++++++++++----------- examples/nrf/src/bin/pwm_sequence.rs | 2 +- examples/nrf/src/bin/pwm_simple_sin.rs | 4 +-- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 19b653ca..8775c889 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -56,8 +56,8 @@ pub struct Pwm<'d, T: Instance> { #[derive(Debug, Eq, PartialEq, Clone, Copy)] pub enum LoopMode { - /// Repeat n additional times after the first - Additional(u16), + /// Run sequence n Times total + Times(u16), /// Repeat until `stop` is called. Infinite, } @@ -74,12 +74,12 @@ pub struct LoopingConfig<'a> { pub sequence: &'a [u16], /// How a sequence is read from RAM and is spread to the compare register pub sequence_load: SequenceLoad, - /// Number of additional PWM periods to delay between each sequence sample + /// Number of Times PWM periods to delay between each sequence sample pub refresh: u32, - /// Number of additional PWM periods after the sequence ends before starting the next sequence + /// Number of Times PWM periods after the sequence ends before starting the next sequence pub end_delay: u32, - /// How many times to repeat the sequence - pub additional_loops: LoopMode, + /// How many times to play the sequence + pub times: LoopMode, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -88,6 +88,8 @@ pub struct LoopingConfig<'a> { pub enum Error { /// Max Sequence size is 32767 SequenceTooLong, + /// Min Sequence size is 1 + SequenceTooShort, /// EasyDMA can only read from data memory, read only buffers in flash will fail. DMABufferNotInDataMemory, } @@ -177,6 +179,9 @@ impl<'d, T: Instance> Pwm<'d, T> { if config.sequence.len() > 32767 { return Err(Error::SequenceTooLong); } + if let LoopMode::Times(0) = config.times { + return Err(Error::SequenceTooShort); + } unborrow!(ch0, ch1, ch2, ch3); @@ -243,27 +248,28 @@ impl<'d, T: Instance> Pwm<'d, T> { .enddelay .write(|w| unsafe { w.bits(config.end_delay) }); - match config.additional_loops { + match config.times { // just the one time, no loop count - LoopMode::Additional(0) => { + LoopMode::Times(1) => { r.loop_.write(|w| w.cnt().disabled()); // tasks_seqstart doesnt exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); } // loop count is how many times to play BOTH sequences - // the one time + 1 = 2 total, play the sequence once starting from seq0 - // the one time + 2 = 3 total, playing the sequence twice would be too much, but we can start on seq1 to subtract one - // the one time + 3 = 4 total, play the sequence twice starting from seq0 - LoopMode::Additional(n) => { - let times = (n / 2) + 1; + // 2 total (1 x 2) + // 3 total, (2 x 2) - 1 + LoopMode::Times(n) => { + let odd = n & 1 == 1; + let times = if odd { (n / 2) + 1 } else { n / 2 }; + r.loop_.write(|w| unsafe { w.cnt().bits(times) }); - if n & 1 == 1 { - // tasks_seqstart doesnt exist in all svds so write its bit instead - r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); - } else { + if odd { // tasks_seqstart doesnt exist in all svds so write its bit instead r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); + } else { + // tasks_seqstart doesnt exist in all svds so write its bit instead + r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); } } // 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 1c5ab268..40ba7ab1 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) { sequence_load: SequenceLoad::Individual, refresh: 0, end_delay: 0, - additional_loops: LoopMode::Additional(5), + times: LoopMode::Times(5), }; 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 909b8bf3..c1af0db8 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) { sequence: &seq_values, sequence_load: SequenceLoad::Common, refresh: 0, - end_delay: 1, - additional_loops: LoopMode::Infinite, + end_delay: 0, + times: LoopMode::Infinite, }; let pwm = unwrap!(Pwm::simple_playback( From 12b2c5d5f70b836c853c000f036df2bdc255098d Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 08:54:07 -0700 Subject: [PATCH 15/47] better not as a constructor? --- embassy-nrf/src/pwm.rs | 60 ++++++-------------------- examples/nrf/src/bin/pwm_sequence.rs | 11 +++-- examples/nrf/src/bin/pwm_simple_sin.rs | 11 +++-- 3 files changed, 22 insertions(+), 60 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 8775c889..d9d69170 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -55,7 +55,7 @@ pub struct Pwm<'d, T: Instance> { } #[derive(Debug, Eq, PartialEq, Clone, Copy)] -pub enum LoopMode { +pub enum SequenceMode { /// Run sequence n Times total Times(u16), /// Repeat until `stop` is called. @@ -63,7 +63,7 @@ pub enum LoopMode { } /// Configure an infinite looping sequence for `simple_playback` -pub struct LoopingConfig<'a> { +pub struct SequenceConfig<'a> { /// Selects up mode or up-and-down mode for the counter pub counter_mode: CounterMode, // Top value to be compared against buffer values @@ -79,7 +79,7 @@ pub struct LoopingConfig<'a> { /// Number of Times PWM periods after the sequence ends before starting the next sequence pub end_delay: u32, /// How many times to play the sequence - pub times: LoopMode, + pub times: SequenceMode, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -132,6 +132,9 @@ impl<'d, T: Instance> Pwm<'d, T> { pin.set_low(); pin.conf().write(|w| w.dir().output()); } + + // if NoPin provided writes disconnected (top bit 1) 0x80000000 else + // writes pin number ex 13 (0x0D) which is connected (top bit 0) r.psel.out[0].write(|w| unsafe { w.bits(ch0.psel_bits()) }); r.psel.out[1].write(|w| unsafe { w.bits(ch1.psel_bits()) }); r.psel.out[2].write(|w| unsafe { w.bits(ch2.psel_bits()) }); @@ -166,54 +169,18 @@ impl<'d, T: Instance> Pwm<'d, T> { } /// Returns a configured pwm that has had start called on it - pub fn simple_playback( - _pwm: impl Unborrow + 'd, - ch0: impl Unborrow + 'd, - ch1: impl Unborrow + 'd, - ch2: impl Unborrow + 'd, - ch3: impl Unborrow + 'd, - config: LoopingConfig, - ) -> Result { + pub fn play_sequence(&self, config: SequenceConfig) -> Result<(), Error> { slice_in_ram_or(config.sequence, Error::DMABufferNotInDataMemory)?; if config.sequence.len() > 32767 { return Err(Error::SequenceTooLong); } - if let LoopMode::Times(0) = config.times { + if let SequenceMode::Times(0) = config.times { return Err(Error::SequenceTooShort); } - unborrow!(ch0, ch1, ch2, ch3); - let r = T::regs(); - if let Some(pin) = ch0.pin_mut() { - pin.set_low(); - pin.conf().write(|w| w.dir().output()); - } - - if let Some(pin) = ch1.pin_mut() { - pin.set_low(); - pin.conf().write(|w| w.dir().output()); - } - if let Some(pin) = ch2.pin_mut() { - pin.set_low(); - pin.conf().write(|w| w.dir().output()); - } - if let Some(pin) = ch3.pin_mut() { - pin.set_low(); - pin.conf().write(|w| w.dir().output()); - } - - // if NoPin provided writes disconnected (top bit 1) 0x80000000 else - // writes pin number ex 13 (0x0D) which is connected (top bit 0) - r.psel.out[0].write(|w| unsafe { w.bits(ch0.psel_bits()) }); - r.psel.out[1].write(|w| unsafe { w.bits(ch1.psel_bits()) }); - r.psel.out[2].write(|w| unsafe { w.bits(ch2.psel_bits()) }); - r.psel.out[3].write(|w| unsafe { w.bits(ch3.psel_bits()) }); - - r.enable.write(|w| w.enable().enabled()); - r.mode .write(|w| unsafe { w.bits(config.counter_mode as u32) }); r.prescaler @@ -250,7 +217,7 @@ impl<'d, T: Instance> Pwm<'d, T> { match config.times { // just the one time, no loop count - LoopMode::Times(1) => { + SequenceMode::Times(1) => { r.loop_.write(|w| w.cnt().disabled()); // tasks_seqstart doesnt exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); @@ -258,7 +225,7 @@ impl<'d, T: Instance> Pwm<'d, T> { // loop count is how many times to play BOTH sequences // 2 total (1 x 2) // 3 total, (2 x 2) - 1 - LoopMode::Times(n) => { + SequenceMode::Times(n) => { let odd = n & 1 == 1; let times = if odd { (n / 2) + 1 } else { n / 2 }; @@ -273,17 +240,14 @@ impl<'d, T: Instance> Pwm<'d, T> { } } // to play infinitely, repeat the sequence one time, then have loops done self trigger seq0 again - LoopMode::Infinite => { + SequenceMode::Infinite => { r.loop_.write(|w| unsafe { w.cnt().bits(0x1) }); r.shorts.write(|w| w.loopsdone_seqstart0().enabled()); // tasks_seqstart doesnt exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); } } - - Ok(Self { - phantom: PhantomData, - }) + Ok(()) } /// Stop playback diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 40ba7ab1..066ab3c0 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -7,7 +7,7 @@ mod example_common; use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; -use embassy_nrf::pwm::{CounterMode, LoopMode, LoopingConfig, Prescaler, Pwm, SequenceLoad}; +use embassy_nrf::pwm::{CounterMode, Prescaler, Pwm, SequenceConfig, SequenceLoad, SequenceMode}; use embassy_nrf::Peripherals; #[embassy::main] @@ -16,7 +16,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, ]; - let config = LoopingConfig { + let config = SequenceConfig { counter_mode: CounterMode::Up, top: 15625, prescaler: Prescaler::Div128, @@ -24,12 +24,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { sequence_load: SequenceLoad::Individual, refresh: 0, end_delay: 0, - times: LoopMode::Times(5), + times: SequenceMode::Times(5), }; - let _pwm = unwrap!(Pwm::simple_playback( - p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config - )); + let pwm = Pwm::new(p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14); + unwrap!(pwm.play_sequence(config)); info!("pwm started!"); loop { diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index c1af0db8..3c1bddbc 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -9,7 +9,7 @@ use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_nrf::gpio::NoPin; -use embassy_nrf::pwm::{CounterMode, LoopMode, LoopingConfig, Prescaler, Pwm, SequenceLoad}; +use embassy_nrf::pwm::{CounterMode, Prescaler, Pwm, SequenceConfig, SequenceLoad, SequenceMode}; use embassy_nrf::Peripherals; use micromath::F32Ext; @@ -20,7 +20,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { // probably not best use of resources to create the table at runtime, but makes testing fast let seq_values: [u16; 220] = core::array::from_fn(|n| ((W1 * n as f32).sin() * 10000.0) as u16); - let config = LoopingConfig { + let config = SequenceConfig { counter_mode: CounterMode::UpAndDown, top: 12000, prescaler: Prescaler::Div16, @@ -28,12 +28,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { sequence_load: SequenceLoad::Common, refresh: 0, end_delay: 0, - times: LoopMode::Infinite, + times: SequenceMode::Infinite, }; - let pwm = unwrap!(Pwm::simple_playback( - p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config - )); + let pwm = Pwm::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin); + unwrap!(pwm.play_sequence(config)); info!("pwm started!"); Timer::after(Duration::from_millis(20000)).await; From 5285179218a0d5137dc6c1c16f2d01617c310a0a Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 09:37:34 -0700 Subject: [PATCH 16/47] generalize new and change pwm example to a servo --- embassy-nrf/src/pwm.rs | 22 +++---- examples/nrf/src/bin/pwm.rs | 111 ++++++++++-------------------------- 2 files changed, 41 insertions(+), 92 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index d9d69170..31e8a3b6 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -95,14 +95,15 @@ pub enum Error { } impl<'d, T: Instance> Pwm<'d, T> { - /// Creates the interface to a UARTE instance. - /// Sets the baud rate, parity and assigns the pins to the UARTE peripheral. + /// Creates the interface to a PWM instance. + /// + /// Defaults the freq to 1Mhz, max_duty 32767, duty 0, and channels low. /// /// # Safety /// - /// The returned API is safe unless you use `mem::forget` (or similar safe mechanisms) - /// on stack allocated buffers which which have been passed to [`send()`](Pwm::send) - /// or [`receive`](Pwm::receive). + /// The returned API is safe unless you use `mem::forget` (or similar safe + /// mechanisms) on stack allocated buffers which which have been passed to + /// [`send()`](Pwm::send) or [`receive`](Pwm::receive). #[allow(unused_unsafe)] pub fn new( _pwm: impl Unborrow + 'd, @@ -141,7 +142,9 @@ impl<'d, T: Instance> Pwm<'d, T> { r.psel.out[3].write(|w| unsafe { w.bits(ch3.psel_bits()) }); // Disable all interrupts + r.intenset.reset(); r.intenclr.write(|w| unsafe { w.bits(0xFFFF_FFFF) }); + r.shorts.reset(); // Enable r.enable.write(|w| w.enable().enabled()); @@ -150,7 +153,7 @@ impl<'d, T: Instance> Pwm<'d, T> { .ptr .write(|w| unsafe { w.bits(&s.duty as *const _ as u32) }); r.seq0.cnt.write(|w| unsafe { w.bits(4) }); - r.seq0.refresh.write(|w| unsafe { w.bits(32) }); + r.seq0.refresh.write(|w| unsafe { w.bits(0) }); r.seq0.enddelay.write(|w| unsafe { w.bits(0) }); r.decoder.write(|w| { @@ -158,9 +161,8 @@ impl<'d, T: Instance> Pwm<'d, T> { w.mode().refresh_count() }); r.mode.write(|w| w.updown().up()); - r.prescaler.write(|w| w.prescaler().div_1()); - r.countertop - .write(|w| unsafe { w.countertop().bits(32767) }); + r.prescaler.write(|w| w.prescaler().div_16()); + r.countertop.write(|w| unsafe { w.countertop().bits(1000) }); r.loop_.write(|w| w.cnt().disabled()); Self { @@ -255,7 +257,7 @@ impl<'d, T: Instance> Pwm<'d, T> { pub fn sequence_stop(&self) { let r = T::regs(); - r.shorts.write(|w| unsafe { w.bits(0x0) }); + r.shorts.reset(); // tasks_stop doesnt exist in all svds so write its bit instead r.tasks_stop.write(|w| unsafe { w.bits(0x01) }); diff --git a/examples/nrf/src/bin/pwm.rs b/examples/nrf/src/bin/pwm.rs index ab033eb5..aeda80be 100644 --- a/examples/nrf/src/bin/pwm.rs +++ b/examples/nrf/src/bin/pwm.rs @@ -7,95 +7,42 @@ mod example_common; use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; +use embassy_nrf::gpio::NoPin; use embassy_nrf::pwm::{Prescaler, Pwm}; use embassy_nrf::Peripherals; -// for i in range(1024): print(int((math.sin(i/512*math.pi)*0.4+0.5)**2*32767), ', ', end='') -static DUTY: [u16; 1024] = [ - 8191, 8272, 8353, 8434, 8516, 8598, 8681, 8764, 8847, 8931, 9015, 9099, 9184, 9269, 9354, 9440, - 9526, 9613, 9700, 9787, 9874, 9962, 10050, 10139, 10227, 10316, 10406, 10495, 10585, 10675, - 10766, 10857, 10948, 11039, 11131, 11223, 11315, 11407, 11500, 11592, 11685, 11779, 11872, - 11966, 12060, 12154, 12248, 12343, 12438, 12533, 12628, 12723, 12818, 12914, 13010, 13106, - 13202, 13298, 13394, 13491, 13587, 13684, 13781, 13878, 13975, 14072, 14169, 14266, 14364, - 14461, 14558, 14656, 14754, 14851, 14949, 15046, 15144, 15242, 15339, 15437, 15535, 15632, - 15730, 15828, 15925, 16023, 16120, 16218, 16315, 16412, 16510, 16607, 16704, 16801, 16898, - 16995, 17091, 17188, 17284, 17380, 17477, 17572, 17668, 17764, 17859, 17955, 18050, 18145, - 18239, 18334, 18428, 18522, 18616, 18710, 18803, 18896, 18989, 19082, 19174, 19266, 19358, - 19449, 19540, 19631, 19722, 19812, 19902, 19991, 20081, 20169, 20258, 20346, 20434, 20521, - 20608, 20695, 20781, 20867, 20952, 21037, 21122, 21206, 21290, 21373, 21456, 21538, 21620, - 21701, 21782, 21863, 21943, 22022, 22101, 22179, 22257, 22335, 22412, 22488, 22564, 22639, - 22714, 22788, 22861, 22934, 23007, 23079, 23150, 23220, 23290, 23360, 23429, 23497, 23564, - 23631, 23698, 23763, 23828, 23892, 23956, 24019, 24081, 24143, 24204, 24264, 24324, 24383, - 24441, 24499, 24555, 24611, 24667, 24721, 24775, 24828, 24881, 24933, 24983, 25034, 25083, - 25132, 25180, 25227, 25273, 25319, 25363, 25407, 25451, 25493, 25535, 25575, 25615, 25655, - 25693, 25731, 25767, 25803, 25838, 25873, 25906, 25939, 25971, 26002, 26032, 26061, 26089, - 26117, 26144, 26170, 26195, 26219, 26242, 26264, 26286, 26307, 26327, 26346, 26364, 26381, - 26397, 26413, 26427, 26441, 26454, 26466, 26477, 26487, 26496, 26505, 26512, 26519, 26525, - 26530, 26534, 26537, 26539, 26540, 26541, 26540, 26539, 26537, 26534, 26530, 26525, 26519, - 26512, 26505, 26496, 26487, 26477, 26466, 26454, 26441, 26427, 26413, 26397, 26381, 26364, - 26346, 26327, 26307, 26286, 26264, 26242, 26219, 26195, 26170, 26144, 26117, 26089, 26061, - 26032, 26002, 25971, 25939, 25906, 25873, 25838, 25803, 25767, 25731, 25693, 25655, 25615, - 25575, 25535, 25493, 25451, 25407, 25363, 25319, 25273, 25227, 25180, 25132, 25083, 25034, - 24983, 24933, 24881, 24828, 24775, 24721, 24667, 24611, 24555, 24499, 24441, 24383, 24324, - 24264, 24204, 24143, 24081, 24019, 23956, 23892, 23828, 23763, 23698, 23631, 23564, 23497, - 23429, 23360, 23290, 23220, 23150, 23079, 23007, 22934, 22861, 22788, 22714, 22639, 22564, - 22488, 22412, 22335, 22257, 22179, 22101, 22022, 21943, 21863, 21782, 21701, 21620, 21538, - 21456, 21373, 21290, 21206, 21122, 21037, 20952, 20867, 20781, 20695, 20608, 20521, 20434, - 20346, 20258, 20169, 20081, 19991, 19902, 19812, 19722, 19631, 19540, 19449, 19358, 19266, - 19174, 19082, 18989, 18896, 18803, 18710, 18616, 18522, 18428, 18334, 18239, 18145, 18050, - 17955, 17859, 17764, 17668, 17572, 17477, 17380, 17284, 17188, 17091, 16995, 16898, 16801, - 16704, 16607, 16510, 16412, 16315, 16218, 16120, 16023, 15925, 15828, 15730, 15632, 15535, - 15437, 15339, 15242, 15144, 15046, 14949, 14851, 14754, 14656, 14558, 14461, 14364, 14266, - 14169, 14072, 13975, 13878, 13781, 13684, 13587, 13491, 13394, 13298, 13202, 13106, 13010, - 12914, 12818, 12723, 12628, 12533, 12438, 12343, 12248, 12154, 12060, 11966, 11872, 11779, - 11685, 11592, 11500, 11407, 11315, 11223, 11131, 11039, 10948, 10857, 10766, 10675, 10585, - 10495, 10406, 10316, 10227, 10139, 10050, 9962, 9874, 9787, 9700, 9613, 9526, 9440, 9354, 9269, - 9184, 9099, 9015, 8931, 8847, 8764, 8681, 8598, 8516, 8434, 8353, 8272, 8191, 8111, 8031, 7952, - 7873, 7794, 7716, 7638, 7561, 7484, 7407, 7331, 7255, 7180, 7105, 7031, 6957, 6883, 6810, 6738, - 6665, 6594, 6522, 6451, 6381, 6311, 6241, 6172, 6104, 6036, 5968, 5901, 5834, 5767, 5702, 5636, - 5571, 5507, 5443, 5379, 5316, 5253, 5191, 5130, 5068, 5008, 4947, 4888, 4828, 4769, 4711, 4653, - 4596, 4539, 4482, 4426, 4371, 4316, 4261, 4207, 4153, 4100, 4047, 3995, 3943, 3892, 3841, 3791, - 3741, 3691, 3642, 3594, 3546, 3498, 3451, 3404, 3358, 3312, 3267, 3222, 3178, 3134, 3090, 3047, - 3005, 2962, 2921, 2879, 2839, 2798, 2758, 2719, 2680, 2641, 2603, 2565, 2528, 2491, 2454, 2418, - 2382, 2347, 2312, 2278, 2244, 2210, 2177, 2144, 2112, 2080, 2048, 2017, 1986, 1956, 1926, 1896, - 1867, 1838, 1810, 1781, 1754, 1726, 1699, 1673, 1646, 1620, 1595, 1570, 1545, 1520, 1496, 1472, - 1449, 1426, 1403, 1380, 1358, 1336, 1315, 1294, 1273, 1252, 1232, 1212, 1192, 1173, 1154, 1135, - 1117, 1099, 1081, 1063, 1046, 1029, 1012, 996, 980, 964, 948, 933, 918, 903, 888, 874, 860, - 846, 833, 819, 806, 793, 781, 768, 756, 744, 733, 721, 710, 699, 688, 677, 667, 657, 647, 637, - 627, 618, 609, 599, 591, 582, 574, 565, 557, 549, 541, 534, 526, 519, 512, 505, 498, 492, 485, - 479, 473, 467, 461, 455, 450, 444, 439, 434, 429, 424, 419, 415, 410, 406, 402, 398, 394, 390, - 386, 383, 379, 376, 373, 370, 367, 364, 361, 359, 356, 354, 351, 349, 347, 345, 343, 342, 340, - 338, 337, 336, 334, 333, 332, 331, 330, 330, 329, 328, 328, 328, 327, 327, 327, 327, 327, 328, - 328, 328, 329, 330, 330, 331, 332, 333, 334, 336, 337, 338, 340, 342, 343, 345, 347, 349, 351, - 354, 356, 359, 361, 364, 367, 370, 373, 376, 379, 383, 386, 390, 394, 398, 402, 406, 410, 415, - 419, 424, 429, 434, 439, 444, 450, 455, 461, 467, 473, 479, 485, 492, 498, 505, 512, 519, 526, - 534, 541, 549, 557, 565, 574, 582, 591, 599, 609, 618, 627, 637, 647, 657, 667, 677, 688, 699, - 710, 721, 733, 744, 756, 768, 781, 793, 806, 819, 833, 846, 860, 874, 888, 903, 918, 933, 948, - 964, 980, 996, 1012, 1029, 1046, 1063, 1081, 1099, 1117, 1135, 1154, 1173, 1192, 1212, 1232, - 1252, 1273, 1294, 1315, 1336, 1358, 1380, 1403, 1426, 1449, 1472, 1496, 1520, 1545, 1570, 1595, - 1620, 1646, 1673, 1699, 1726, 1754, 1781, 1810, 1838, 1867, 1896, 1926, 1956, 1986, 2017, 2048, - 2080, 2112, 2144, 2177, 2210, 2244, 2278, 2312, 2347, 2382, 2418, 2454, 2491, 2528, 2565, 2603, - 2641, 2680, 2719, 2758, 2798, 2839, 2879, 2921, 2962, 3005, 3047, 3090, 3134, 3178, 3222, 3267, - 3312, 3358, 3404, 3451, 3498, 3546, 3594, 3642, 3691, 3741, 3791, 3841, 3892, 3943, 3995, 4047, - 4100, 4153, 4207, 4261, 4316, 4371, 4426, 4482, 4539, 4596, 4653, 4711, 4769, 4828, 4888, 4947, - 5008, 5068, 5130, 5191, 5253, 5316, 5379, 5443, 5507, 5571, 5636, 5702, 5767, 5834, 5901, 5968, - 6036, 6104, 6172, 6241, 6311, 6381, 6451, 6522, 6594, 6665, 6738, 6810, 6883, 6957, 7031, 7105, - 7180, 7255, 7331, 7407, 7484, 7561, 7638, 7716, 7794, 7873, 7952, 8031, 8111, -]; - #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let pwm = Pwm::new(p.PWM0, p.P0_13, p.P0_14, p.P0_16, p.P0_15); - pwm.set_prescaler(Prescaler::Div1); + let pwm = Pwm::new(p.PWM0, p.P0_05, NoPin, NoPin, NoPin); + // sg90 microervo requires 50hz or 20ms period + // set_period can only set down to 125khz so we cant use it directly + // Div128 is 125khz or 0.000008s or 0.008ms, 20/0.008 is 2500 is top + pwm.set_prescaler(Prescaler::Div128); + pwm.set_max_duty(2500); info!("pwm initialized!"); - let mut i = 0; + Timer::after(Duration::from_millis(5000)).await; + + // 1ms 0deg (1/.008=125), 1.5ms 90deg (1.5/.008=187.5), 2ms 180deg (2/.008=250), loop { - i += 1; - pwm.set_duty(0, DUTY[i % 1024]); - pwm.set_duty(1, DUTY[(i + 256) % 1024]); - pwm.set_duty(2, DUTY[(i + 512) % 1024]); - pwm.set_duty(3, DUTY[(i + 768) % 1024]); - Timer::after(Duration::from_millis(3)).await; + info!("45 deg"); + pwm.set_duty(0, 2500 - 156); + Timer::after(Duration::from_millis(5000)).await; + + info!("90 deg"); + pwm.set_duty(0, 2500 - 187); + Timer::after(Duration::from_millis(5000)).await; + + info!("135 deg"); + pwm.set_duty(0, 2500 - 218); + Timer::after(Duration::from_millis(5000)).await; + + info!("180 deg"); + pwm.set_duty(0, 2500 - 250); + Timer::after(Duration::from_millis(5000)).await; + + info!("0 deg"); + pwm.set_duty(0, 2500 - 125); + Timer::after(Duration::from_millis(5000)).await; } } From f2cfbe42629c74fd53f709ad7187d00b21d7030a Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 13:00:50 -0700 Subject: [PATCH 17/47] align pwm.set_duty with embedded hal to not be one shot --- embassy-nrf/src/pwm.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 31e8a3b6..3594e6af 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -282,8 +282,15 @@ impl<'d, T: Instance> Pwm<'d, T> { let s = T::state(); unsafe { (*s.duty.get())[channel] = duty & 0x7FFF }; + // todo justify? should i fence elsehwere we task start? or compiler_fence(Ordering::SeqCst); - T::regs().tasks_seqstart[0].write(|w| unsafe { w.bits(1) }); + + // play duty cycle infinitely + let r = T::regs(); + r.loop_.write(|w| unsafe { w.cnt().bits(0x1) }); + r.shorts.write(|w| w.loopsdone_seqstart0().enabled()); + // tasks_seqstart doesnt exist in all svds so write its bit instead + r.tasks_seqstart[0].write(|w| unsafe { w.bits(1) }); } /// Sets the PWM clock prescaler. From 74e7f4a22717a73f735caa97589c9603091016e6 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 13:16:24 -0700 Subject: [PATCH 18/47] comments --- embassy-nrf/src/pwm.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 3594e6af..947cd873 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -66,7 +66,7 @@ pub enum SequenceMode { pub struct SequenceConfig<'a> { /// Selects up mode or up-and-down mode for the counter pub counter_mode: CounterMode, - // Top value to be compared against buffer values + /// Top value to be compared against buffer values pub top: u16, /// Configuration for PWM_CLK pub prescaler: Prescaler, @@ -170,7 +170,7 @@ impl<'d, T: Instance> Pwm<'d, T> { } } - /// Returns a configured pwm that has had start called on it + /// Play a `SequenceConfig` sequence instead of a stable `set_duty` output pub fn play_sequence(&self, config: SequenceConfig) -> Result<(), Error> { slice_in_ram_or(config.sequence, Error::DMABufferNotInDataMemory)?; @@ -233,6 +233,7 @@ impl<'d, T: Instance> Pwm<'d, T> { r.loop_.write(|w| unsafe { w.cnt().bits(times) }); + // we can subtract 1 by starting at seq1 instead of seq0 if odd { // tasks_seqstart doesnt exist in all svds so write its bit instead r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); From b297e5f7bd152d1e5147631c81987c26f9fa0664 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 13:51:40 -0700 Subject: [PATCH 19/47] led dimming example, dont need to keep all examples, just covering ground to test api --- examples/nrf/src/bin/pwm_led.rs | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 examples/nrf/src/bin/pwm_led.rs diff --git a/examples/nrf/src/bin/pwm_led.rs b/examples/nrf/src/bin/pwm_led.rs new file mode 100644 index 00000000..067f4d2c --- /dev/null +++ b/examples/nrf/src/bin/pwm_led.rs @@ -0,0 +1,47 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +#[path = "../example_common.rs"] +mod example_common; +use defmt::*; +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_nrf::gpio::NoPin; +use embassy_nrf::pwm::{Prescaler, Pwm}; +use embassy_nrf::Peripherals; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + let pwm = Pwm::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin); + // set_period doesnt actually set what you give it, because it only has a + // few options from the hardhware so be explicit instead + // Div128 is slowest, 125khz still crazy fast for our eyes + pwm.set_prescaler(Prescaler::Div128); + + info!("pwm initialized!"); + + // default max_duty if not specified is 1000 + // so 0 would be fully off and 1000 or above would be fully on + loop { + info!("100%"); + pwm.set_duty(0, 1000); + Timer::after(Duration::from_millis(5000)).await; + + info!("25%"); + pwm.set_duty(0, 250); + Timer::after(Duration::from_millis(5000)).await; + + info!("10%"); + pwm.set_duty(0, 100); + Timer::after(Duration::from_millis(5000)).await; + + info!("5%"); + pwm.set_duty(0, 50); + Timer::after(Duration::from_millis(5000)).await; + + info!("0%"); + pwm.set_duty(0, 0); + Timer::after(Duration::from_millis(5000)).await; + } +} From 49253152cff2a45bd08cd1801bb6d5f0f3ce9b30 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 19:11:37 -0700 Subject: [PATCH 20/47] seperate sequence from duty cycle pwm struct --- embassy-nrf/src/pwm.rs | 260 +++++++++++++++++-------- examples/nrf/src/bin/pwm_sequence.rs | 9 +- examples/nrf/src/bin/pwm_simple_sin.rs | 9 +- 3 files changed, 186 insertions(+), 92 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 947cd873..a59fddf7 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -54,6 +54,176 @@ pub struct Pwm<'d, T: Instance> { phantom: PhantomData<&'d mut T>, } +pub struct PwmSeq<'d, T: Instance> { + phantom: PhantomData<&'d mut T>, +} + +impl<'d, T: Instance> PwmSeq<'d, T> { + /// Creates the interface to a PWM instance. + /// + /// Defaults the freq to 1Mhz, max_duty 32767, duty 0, and channels low. + /// + /// # Safety + /// + /// The returned API is safe unless you use `mem::forget` (or similar safe + /// mechanisms) on stack allocated buffers which which have been passed to + /// [`send()`](Pwm::send) or [`receive`](Pwm::receive). + #[allow(unused_unsafe)] + pub fn new( + _pwm: impl Unborrow + 'd, + ch0: impl Unborrow + 'd, + ch1: impl Unborrow + 'd, + ch2: impl Unborrow + 'd, + ch3: impl Unborrow + 'd, + config: SequenceConfig, + ) -> Result { + slice_in_ram_or(config.sequence, Error::DMABufferNotInDataMemory)?; + + if config.sequence.len() > 32767 { + return Err(Error::SequenceTooLong); + } + if let SequenceMode::Times(0) = config.times { + return Err(Error::SequenceTooShort); + } + + unborrow!(ch0, ch1, ch2, ch3); + + let r = T::regs(); + + if let Some(pin) = ch0.pin_mut() { + pin.set_low(); + pin.conf().write(|w| w.dir().output()); + } + if let Some(pin) = ch1.pin_mut() { + pin.set_low(); + pin.conf().write(|w| w.dir().output()); + } + if let Some(pin) = ch2.pin_mut() { + pin.set_low(); + pin.conf().write(|w| w.dir().output()); + } + if let Some(pin) = ch3.pin_mut() { + pin.set_low(); + pin.conf().write(|w| w.dir().output()); + } + + // if NoPin provided writes disconnected (top bit 1) 0x80000000 else + // writes pin number ex 13 (0x0D) which is connected (top bit 0) + r.psel.out[0].write(|w| unsafe { w.bits(ch0.psel_bits()) }); + r.psel.out[1].write(|w| unsafe { w.bits(ch1.psel_bits()) }); + r.psel.out[2].write(|w| unsafe { w.bits(ch2.psel_bits()) }); + r.psel.out[3].write(|w| unsafe { w.bits(ch3.psel_bits()) }); + + // Disable all interrupts + r.intenset.reset(); + r.intenclr.write(|w| unsafe { w.bits(0xFFFF_FFFF) }); + r.shorts.reset(); + + // Enable + r.enable.write(|w| w.enable().enabled()); + r.mode + .write(|w| unsafe { w.bits(config.counter_mode as u32) }); + r.prescaler + .write(|w| w.prescaler().bits(config.prescaler as u8)); + r.countertop + .write(|w| unsafe { w.countertop().bits(config.top) }); + + r.decoder.write(|w| { + w.load().bits(config.sequence_load as u8); + w.mode().refresh_count() + }); + + r.seq0 + .ptr + .write(|w| unsafe { w.bits(config.sequence.as_ptr() as u32) }); + r.seq0 + .cnt + .write(|w| unsafe { w.bits(config.sequence.len() as u32) }); + r.seq0.refresh.write(|w| unsafe { w.bits(config.refresh) }); + r.seq0 + .enddelay + .write(|w| unsafe { w.bits(config.end_delay) }); + + r.seq1 + .ptr + .write(|w| unsafe { w.bits(config.sequence.as_ptr() as u32) }); + r.seq1 + .cnt + .write(|w| unsafe { w.bits(config.sequence.len() as u32) }); + r.seq1.refresh.write(|w| unsafe { w.bits(config.refresh) }); + r.seq1 + .enddelay + .write(|w| unsafe { w.bits(config.end_delay) }); + + match config.times { + // just the one time, no loop count + SequenceMode::Times(1) => { + r.loop_.write(|w| w.cnt().disabled()); + // tasks_seqstart doesnt exist in all svds so write its bit instead + r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); + } + // loop count is how many times to play BOTH sequences + // 2 total (1 x 2) + // 3 total, (2 x 2) - 1 + SequenceMode::Times(n) => { + let odd = n & 1 == 1; + let times = if odd { (n / 2) + 1 } else { n / 2 }; + + r.loop_.write(|w| unsafe { w.cnt().bits(times) }); + + // we can subtract 1 by starting at seq1 instead of seq0 + if odd { + // tasks_seqstart doesnt exist in all svds so write its bit instead + r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); + } else { + // tasks_seqstart doesnt exist in all svds so write its bit instead + r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); + } + } + // to play infinitely, repeat the sequence one time, then have loops done self trigger seq0 again + SequenceMode::Infinite => { + r.loop_.write(|w| unsafe { w.cnt().bits(0x1) }); + r.shorts.write(|w| w.loopsdone_seqstart0().enabled()); + // tasks_seqstart doesnt exist in all svds so write its bit instead + r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); + } + } + + Ok(Self { + phantom: PhantomData, + }) + } + + /// Stop playback + #[inline(always)] + pub fn stop(&self) { + let r = T::regs(); + + r.shorts.reset(); + + // tasks_stop doesnt exist in all svds so write its bit instead + r.tasks_stop.write(|w| unsafe { w.bits(0x01) }); + } + + /// Disables the PWM generator. + #[inline(always)] + pub fn disable(&self) { + let r = T::regs(); + r.enable.write(|w| w.enable().disabled()); + } +} + +impl<'a, T: Instance> Drop for PwmSeq<'a, T> { + fn drop(&mut self) { + self.stop(); + self.disable(); + + info!("pwm drop: done"); + + // TODO: disable pins + } +} + #[derive(Debug, Eq, PartialEq, Clone, Copy)] pub enum SequenceMode { /// Run sequence n Times total @@ -98,6 +268,7 @@ impl<'d, T: Instance> Pwm<'d, T> { /// Creates the interface to a PWM instance. /// /// Defaults the freq to 1Mhz, max_duty 32767, duty 0, and channels low. + /// Must be started by calling `set_duty` /// /// # Safety /// @@ -170,92 +341,9 @@ impl<'d, T: Instance> Pwm<'d, T> { } } - /// Play a `SequenceConfig` sequence instead of a stable `set_duty` output - pub fn play_sequence(&self, config: SequenceConfig) -> Result<(), Error> { - slice_in_ram_or(config.sequence, Error::DMABufferNotInDataMemory)?; - - if config.sequence.len() > 32767 { - return Err(Error::SequenceTooLong); - } - if let SequenceMode::Times(0) = config.times { - return Err(Error::SequenceTooShort); - } - - let r = T::regs(); - - r.mode - .write(|w| unsafe { w.bits(config.counter_mode as u32) }); - r.prescaler - .write(|w| w.prescaler().bits(config.prescaler as u8)); - r.countertop - .write(|w| unsafe { w.countertop().bits(config.top) }); - - r.decoder.write(|w| { - w.load().bits(config.sequence_load as u8); - w.mode().refresh_count() - }); - - r.seq0 - .ptr - .write(|w| unsafe { w.bits(config.sequence.as_ptr() as u32) }); - r.seq0 - .cnt - .write(|w| unsafe { w.bits(config.sequence.len() as u32) }); - r.seq0.refresh.write(|w| unsafe { w.bits(config.refresh) }); - r.seq0 - .enddelay - .write(|w| unsafe { w.bits(config.end_delay) }); - - r.seq1 - .ptr - .write(|w| unsafe { w.bits(config.sequence.as_ptr() as u32) }); - r.seq1 - .cnt - .write(|w| unsafe { w.bits(config.sequence.len() as u32) }); - r.seq1.refresh.write(|w| unsafe { w.bits(config.refresh) }); - r.seq1 - .enddelay - .write(|w| unsafe { w.bits(config.end_delay) }); - - match config.times { - // just the one time, no loop count - SequenceMode::Times(1) => { - r.loop_.write(|w| w.cnt().disabled()); - // tasks_seqstart doesnt exist in all svds so write its bit instead - r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); - } - // loop count is how many times to play BOTH sequences - // 2 total (1 x 2) - // 3 total, (2 x 2) - 1 - SequenceMode::Times(n) => { - let odd = n & 1 == 1; - let times = if odd { (n / 2) + 1 } else { n / 2 }; - - r.loop_.write(|w| unsafe { w.cnt().bits(times) }); - - // we can subtract 1 by starting at seq1 instead of seq0 - if odd { - // tasks_seqstart doesnt exist in all svds so write its bit instead - r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); - } else { - // tasks_seqstart doesnt exist in all svds so write its bit instead - r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); - } - } - // to play infinitely, repeat the sequence one time, then have loops done self trigger seq0 again - SequenceMode::Infinite => { - r.loop_.write(|w| unsafe { w.cnt().bits(0x1) }); - r.shorts.write(|w| w.loopsdone_seqstart0().enabled()); - // tasks_seqstart doesnt exist in all svds so write its bit instead - r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); - } - } - Ok(()) - } - /// Stop playback #[inline(always)] - pub fn sequence_stop(&self) { + pub fn stop(&self) { let r = T::regs(); r.shorts.reset(); @@ -264,6 +352,7 @@ impl<'d, T: Instance> Pwm<'d, T> { r.tasks_stop.write(|w| unsafe { w.bits(0x01) }); } + // todo should this do.. something useful /// Enables the PWM generator. #[inline(always)] pub fn enable(&self) { @@ -271,6 +360,7 @@ impl<'d, T: Instance> Pwm<'d, T> { r.enable.write(|w| w.enable().enabled()); } + // todo should this stop the task? or should you just use set_duty to 0? /// Disables the PWM generator. #[inline(always)] pub fn disable(&self) { @@ -349,7 +439,7 @@ impl<'d, T: Instance> Pwm<'d, T> { impl<'a, T: Instance> Drop for Pwm<'a, T> { fn drop(&mut self) { - self.sequence_stop(); + self.stop(); self.disable(); info!("pwm drop: done"); diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 066ab3c0..0a7bea1c 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -7,7 +7,9 @@ mod example_common; use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; -use embassy_nrf::pwm::{CounterMode, Prescaler, Pwm, SequenceConfig, SequenceLoad, SequenceMode}; +use embassy_nrf::pwm::{ + CounterMode, Prescaler, PwmSeq, SequenceConfig, SequenceLoad, SequenceMode, +}; use embassy_nrf::Peripherals; #[embassy::main] @@ -27,8 +29,9 @@ async fn main(_spawner: Spawner, p: Peripherals) { times: SequenceMode::Times(5), }; - let pwm = Pwm::new(p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14); - unwrap!(pwm.play_sequence(config)); + let _pwm = unwrap!(PwmSeq::new( + p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config + )); info!("pwm started!"); loop { diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index 3c1bddbc..6fd59c6a 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -9,7 +9,9 @@ use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_nrf::gpio::NoPin; -use embassy_nrf::pwm::{CounterMode, Prescaler, Pwm, SequenceConfig, SequenceLoad, SequenceMode}; +use embassy_nrf::pwm::{ + CounterMode, Prescaler, PwmSeq, SequenceConfig, SequenceLoad, SequenceMode, +}; use embassy_nrf::Peripherals; use micromath::F32Ext; @@ -31,13 +33,12 @@ async fn main(_spawner: Spawner, p: Peripherals) { times: SequenceMode::Infinite, }; - let pwm = Pwm::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin); - unwrap!(pwm.play_sequence(config)); + let pwm = unwrap!(PwmSeq::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config)); info!("pwm started!"); Timer::after(Duration::from_millis(20000)).await; - pwm.sequence_stop(); + pwm.stop(); info!("pwm stopped!"); loop { From 4647792ad68209f3be6e1cdf1cee9513025c14ab Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 20:18:24 -0700 Subject: [PATCH 21/47] seperate start from pwmseq::new --- embassy-nrf/src/pwm.rs | 37 +++++++++++++++++--------- examples/nrf/src/bin/pwm_sequence.rs | 4 +-- examples/nrf/src/bin/pwm_simple_sin.rs | 2 +- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index a59fddf7..9454ad4a 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -59,9 +59,9 @@ pub struct PwmSeq<'d, T: Instance> { } impl<'d, T: Instance> PwmSeq<'d, T> { - /// Creates the interface to a PWM instance. + /// Creates the interface to a PWM Sequence interface. /// - /// Defaults the freq to 1Mhz, max_duty 32767, duty 0, and channels low. + /// Must be started by calling `start` /// /// # Safety /// @@ -82,9 +82,6 @@ impl<'d, T: Instance> PwmSeq<'d, T> { if config.sequence.len() > 32767 { return Err(Error::SequenceTooLong); } - if let SequenceMode::Times(0) = config.times { - return Err(Error::SequenceTooShort); - } unborrow!(ch0, ch1, ch2, ch3); @@ -155,7 +152,27 @@ impl<'d, T: Instance> PwmSeq<'d, T> { .enddelay .write(|w| unsafe { w.bits(config.end_delay) }); - match config.times { + Ok(Self { + phantom: PhantomData, + }) + } + + /// Start or restart playback + #[inline(always)] + pub fn start(&self, times: SequenceMode) -> Result<(), Error> { + if let SequenceMode::Times(0) = times { + return Err(Error::SequenceNoZero); + } + let r = T::regs(); + + r.shorts.reset(); + + // tasks_stop doesnt exist in all svds so write its bit instead + r.tasks_stop.write(|w| unsafe { w.bits(0x01) }); + + r.enable.write(|w| w.enable().enabled()); + + match times { // just the one time, no loop count SequenceMode::Times(1) => { r.loop_.write(|w| w.cnt().disabled()); @@ -189,9 +206,7 @@ impl<'d, T: Instance> PwmSeq<'d, T> { } } - Ok(Self { - phantom: PhantomData, - }) + Ok(()) } /// Stop playback @@ -248,8 +263,6 @@ pub struct SequenceConfig<'a> { pub refresh: u32, /// Number of Times PWM periods after the sequence ends before starting the next sequence pub end_delay: u32, - /// How many times to play the sequence - pub times: SequenceMode, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -259,7 +272,7 @@ pub enum Error { /// Max Sequence size is 32767 SequenceTooLong, /// Min Sequence size is 1 - SequenceTooShort, + SequenceNoZero, /// EasyDMA can only read from data memory, read only buffers in flash will fail. DMABufferNotInDataMemory, } diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 0a7bea1c..6911d034 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) { sequence_load: SequenceLoad::Individual, refresh: 0, end_delay: 0, - times: SequenceMode::Times(5), }; - let _pwm = unwrap!(PwmSeq::new( + let pwm = unwrap!(PwmSeq::new( p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config )); + unwrap!(pwm.start(SequenceMode::Times(5))); info!("pwm started!"); loop { diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index 6fd59c6a..985f9d80 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) { sequence_load: SequenceLoad::Common, refresh: 0, end_delay: 0, - times: SequenceMode::Infinite, }; let pwm = unwrap!(PwmSeq::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config)); + unwrap!(pwm.start(SequenceMode::Infinite)); info!("pwm started!"); Timer::after(Duration::from_millis(20000)).await; From c939edb8d03a6ddf965507e3b2de02b82ca255b4 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Tue, 2 Nov 2021 10:57:01 -0700 Subject: [PATCH 22/47] rename error enum again --- embassy-nrf/src/pwm.rs | 4 ++-- examples/nrf/src/bin/pwm_sequence.rs | 2 +- examples/nrf/src/bin/pwm_simple_sin.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 9454ad4a..6468f267 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -161,7 +161,7 @@ impl<'d, T: Instance> PwmSeq<'d, T> { #[inline(always)] pub fn start(&self, times: SequenceMode) -> Result<(), Error> { if let SequenceMode::Times(0) = times { - return Err(Error::SequenceNoZero); + return Err(Error::SequenceTimesAtLeastOne); } let r = T::regs(); @@ -272,7 +272,7 @@ pub enum Error { /// Max Sequence size is 32767 SequenceTooLong, /// Min Sequence size is 1 - SequenceNoZero, + SequenceTimesAtLeastOne, /// EasyDMA can only read from data memory, read only buffers in flash will fail. DMABufferNotInDataMemory, } diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 6911d034..8830e52d 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -31,7 +31,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { let pwm = unwrap!(PwmSeq::new( p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config )); - unwrap!(pwm.start(SequenceMode::Times(5))); + let _ = pwm.start(SequenceMode::Times(5)); info!("pwm started!"); loop { diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index 985f9d80..ac3089c8 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -33,7 +33,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { }; let pwm = unwrap!(PwmSeq::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config)); - unwrap!(pwm.start(SequenceMode::Infinite)); + let _ = pwm.start(SequenceMode::Infinite); info!("pwm started!"); Timer::after(Duration::from_millis(20000)).await; From 682274870f89f1a3585d3df497886d2bca9b1f88 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Tue, 2 Nov 2021 11:56:01 -0700 Subject: [PATCH 23/47] set_duty does indeed loop forever --- embassy-nrf/src/pwm.rs | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 6468f267..760f36a5 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -165,18 +165,16 @@ impl<'d, T: Instance> PwmSeq<'d, T> { } let r = T::regs(); - r.shorts.reset(); - - // tasks_stop doesnt exist in all svds so write its bit instead - r.tasks_stop.write(|w| unsafe { w.bits(0x01) }); + self.stop(); r.enable.write(|w| w.enable().enabled()); match times { + // todo why doesn't this play forever? set_duty does... // just the one time, no loop count SequenceMode::Times(1) => { r.loop_.write(|w| w.cnt().disabled()); - // tasks_seqstart doesnt exist in all svds so write its bit instead + // tasks_seqstart() doesn't exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); } // loop count is how many times to play BOTH sequences @@ -190,10 +188,10 @@ impl<'d, T: Instance> PwmSeq<'d, T> { // we can subtract 1 by starting at seq1 instead of seq0 if odd { - // tasks_seqstart doesnt exist in all svds so write its bit instead + // tasks_seqstart() doesn't exist in all svds so write its bit instead r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); } else { - // tasks_seqstart doesnt exist in all svds so write its bit instead + // tasks_seqstart() doesn't exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); } } @@ -201,7 +199,7 @@ impl<'d, T: Instance> PwmSeq<'d, T> { SequenceMode::Infinite => { r.loop_.write(|w| unsafe { w.cnt().bits(0x1) }); r.shorts.write(|w| w.loopsdone_seqstart0().enabled()); - // tasks_seqstart doesnt exist in all svds so write its bit instead + // tasks_seqstart() doesn't exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); } } @@ -216,7 +214,7 @@ impl<'d, T: Instance> PwmSeq<'d, T> { r.shorts.reset(); - // tasks_stop doesnt exist in all svds so write its bit instead + // tasks_stop() doesn't exist in all svds so write its bit instead r.tasks_stop.write(|w| unsafe { w.bits(0x01) }); } @@ -361,7 +359,7 @@ impl<'d, T: Instance> Pwm<'d, T> { r.shorts.reset(); - // tasks_stop doesnt exist in all svds so write its bit instead + // tasks_stop() doesn't exist in all svds so write its bit instead r.tasks_stop.write(|w| unsafe { w.bits(0x01) }); } @@ -383,17 +381,15 @@ impl<'d, T: Instance> Pwm<'d, T> { /// Sets duty cycle (15 bit) for a PWM channel. pub fn set_duty(&self, channel: usize, duty: u16) { + let r = T::regs(); let s = T::state(); unsafe { (*s.duty.get())[channel] = duty & 0x7FFF }; // todo justify? should i fence elsehwere we task start? or compiler_fence(Ordering::SeqCst); - // play duty cycle infinitely - let r = T::regs(); - r.loop_.write(|w| unsafe { w.cnt().bits(0x1) }); - r.shorts.write(|w| w.loopsdone_seqstart0().enabled()); - // tasks_seqstart doesnt exist in all svds so write its bit instead + // todo why does this play forever when times(1) doesn't? + // tasks_seqstart() doesn't exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(1) }); } From 44375b427c20273d91845e5e012e669d8a2e1cc0 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 3 Nov 2021 15:26:44 -0700 Subject: [PATCH 24/47] restore example and add set_time_stretch api --- embassy-nrf/src/pwm.rs | 6 ++ examples/nrf/src/bin/pwm.rs | 113 ++++++++++++++++++++++-------- examples/nrf/src/bin/pwm_servo.rs | 48 +++++++++++++ 3 files changed, 138 insertions(+), 29 deletions(-) create mode 100644 examples/nrf/src/bin/pwm_servo.rs diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 760f36a5..8c26adac 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -423,6 +423,12 @@ impl<'d, T: Instance> Pwm<'d, T> { .write(|w| unsafe { w.countertop().bits(duty.min(32767u16)) }); } + /// Additional number of PWM periods spent on each duty cycle value. + #[inline(always)] + pub fn set_time_stretch(&self, refresh: u32) { + T::regs().seq0.refresh.write(|w| unsafe { w.bits(refresh) }); + } + /// Returns the maximum duty cycle value. #[inline(always)] pub fn max_duty(&self) -> u16 { diff --git a/examples/nrf/src/bin/pwm.rs b/examples/nrf/src/bin/pwm.rs index aeda80be..4c18b77b 100644 --- a/examples/nrf/src/bin/pwm.rs +++ b/examples/nrf/src/bin/pwm.rs @@ -7,42 +7,97 @@ mod example_common; use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; -use embassy_nrf::gpio::NoPin; use embassy_nrf::pwm::{Prescaler, Pwm}; use embassy_nrf::Peripherals; +// for i in range(1024): print(int((math.sin(i/512*math.pi)*0.4+0.5)**2*32767), ', ', end='') +static DUTY: [u16; 1024] = [ + 8191, 8272, 8353, 8434, 8516, 8598, 8681, 8764, 8847, 8931, 9015, 9099, 9184, 9269, 9354, 9440, + 9526, 9613, 9700, 9787, 9874, 9962, 10050, 10139, 10227, 10316, 10406, 10495, 10585, 10675, + 10766, 10857, 10948, 11039, 11131, 11223, 11315, 11407, 11500, 11592, 11685, 11779, 11872, + 11966, 12060, 12154, 12248, 12343, 12438, 12533, 12628, 12723, 12818, 12914, 13010, 13106, + 13202, 13298, 13394, 13491, 13587, 13684, 13781, 13878, 13975, 14072, 14169, 14266, 14364, + 14461, 14558, 14656, 14754, 14851, 14949, 15046, 15144, 15242, 15339, 15437, 15535, 15632, + 15730, 15828, 15925, 16023, 16120, 16218, 16315, 16412, 16510, 16607, 16704, 16801, 16898, + 16995, 17091, 17188, 17284, 17380, 17477, 17572, 17668, 17764, 17859, 17955, 18050, 18145, + 18239, 18334, 18428, 18522, 18616, 18710, 18803, 18896, 18989, 19082, 19174, 19266, 19358, + 19449, 19540, 19631, 19722, 19812, 19902, 19991, 20081, 20169, 20258, 20346, 20434, 20521, + 20608, 20695, 20781, 20867, 20952, 21037, 21122, 21206, 21290, 21373, 21456, 21538, 21620, + 21701, 21782, 21863, 21943, 22022, 22101, 22179, 22257, 22335, 22412, 22488, 22564, 22639, + 22714, 22788, 22861, 22934, 23007, 23079, 23150, 23220, 23290, 23360, 23429, 23497, 23564, + 23631, 23698, 23763, 23828, 23892, 23956, 24019, 24081, 24143, 24204, 24264, 24324, 24383, + 24441, 24499, 24555, 24611, 24667, 24721, 24775, 24828, 24881, 24933, 24983, 25034, 25083, + 25132, 25180, 25227, 25273, 25319, 25363, 25407, 25451, 25493, 25535, 25575, 25615, 25655, + 25693, 25731, 25767, 25803, 25838, 25873, 25906, 25939, 25971, 26002, 26032, 26061, 26089, + 26117, 26144, 26170, 26195, 26219, 26242, 26264, 26286, 26307, 26327, 26346, 26364, 26381, + 26397, 26413, 26427, 26441, 26454, 26466, 26477, 26487, 26496, 26505, 26512, 26519, 26525, + 26530, 26534, 26537, 26539, 26540, 26541, 26540, 26539, 26537, 26534, 26530, 26525, 26519, + 26512, 26505, 26496, 26487, 26477, 26466, 26454, 26441, 26427, 26413, 26397, 26381, 26364, + 26346, 26327, 26307, 26286, 26264, 26242, 26219, 26195, 26170, 26144, 26117, 26089, 26061, + 26032, 26002, 25971, 25939, 25906, 25873, 25838, 25803, 25767, 25731, 25693, 25655, 25615, + 25575, 25535, 25493, 25451, 25407, 25363, 25319, 25273, 25227, 25180, 25132, 25083, 25034, + 24983, 24933, 24881, 24828, 24775, 24721, 24667, 24611, 24555, 24499, 24441, 24383, 24324, + 24264, 24204, 24143, 24081, 24019, 23956, 23892, 23828, 23763, 23698, 23631, 23564, 23497, + 23429, 23360, 23290, 23220, 23150, 23079, 23007, 22934, 22861, 22788, 22714, 22639, 22564, + 22488, 22412, 22335, 22257, 22179, 22101, 22022, 21943, 21863, 21782, 21701, 21620, 21538, + 21456, 21373, 21290, 21206, 21122, 21037, 20952, 20867, 20781, 20695, 20608, 20521, 20434, + 20346, 20258, 20169, 20081, 19991, 19902, 19812, 19722, 19631, 19540, 19449, 19358, 19266, + 19174, 19082, 18989, 18896, 18803, 18710, 18616, 18522, 18428, 18334, 18239, 18145, 18050, + 17955, 17859, 17764, 17668, 17572, 17477, 17380, 17284, 17188, 17091, 16995, 16898, 16801, + 16704, 16607, 16510, 16412, 16315, 16218, 16120, 16023, 15925, 15828, 15730, 15632, 15535, + 15437, 15339, 15242, 15144, 15046, 14949, 14851, 14754, 14656, 14558, 14461, 14364, 14266, + 14169, 14072, 13975, 13878, 13781, 13684, 13587, 13491, 13394, 13298, 13202, 13106, 13010, + 12914, 12818, 12723, 12628, 12533, 12438, 12343, 12248, 12154, 12060, 11966, 11872, 11779, + 11685, 11592, 11500, 11407, 11315, 11223, 11131, 11039, 10948, 10857, 10766, 10675, 10585, + 10495, 10406, 10316, 10227, 10139, 10050, 9962, 9874, 9787, 9700, 9613, 9526, 9440, 9354, 9269, + 9184, 9099, 9015, 8931, 8847, 8764, 8681, 8598, 8516, 8434, 8353, 8272, 8191, 8111, 8031, 7952, + 7873, 7794, 7716, 7638, 7561, 7484, 7407, 7331, 7255, 7180, 7105, 7031, 6957, 6883, 6810, 6738, + 6665, 6594, 6522, 6451, 6381, 6311, 6241, 6172, 6104, 6036, 5968, 5901, 5834, 5767, 5702, 5636, + 5571, 5507, 5443, 5379, 5316, 5253, 5191, 5130, 5068, 5008, 4947, 4888, 4828, 4769, 4711, 4653, + 4596, 4539, 4482, 4426, 4371, 4316, 4261, 4207, 4153, 4100, 4047, 3995, 3943, 3892, 3841, 3791, + 3741, 3691, 3642, 3594, 3546, 3498, 3451, 3404, 3358, 3312, 3267, 3222, 3178, 3134, 3090, 3047, + 3005, 2962, 2921, 2879, 2839, 2798, 2758, 2719, 2680, 2641, 2603, 2565, 2528, 2491, 2454, 2418, + 2382, 2347, 2312, 2278, 2244, 2210, 2177, 2144, 2112, 2080, 2048, 2017, 1986, 1956, 1926, 1896, + 1867, 1838, 1810, 1781, 1754, 1726, 1699, 1673, 1646, 1620, 1595, 1570, 1545, 1520, 1496, 1472, + 1449, 1426, 1403, 1380, 1358, 1336, 1315, 1294, 1273, 1252, 1232, 1212, 1192, 1173, 1154, 1135, + 1117, 1099, 1081, 1063, 1046, 1029, 1012, 996, 980, 964, 948, 933, 918, 903, 888, 874, 860, + 846, 833, 819, 806, 793, 781, 768, 756, 744, 733, 721, 710, 699, 688, 677, 667, 657, 647, 637, + 627, 618, 609, 599, 591, 582, 574, 565, 557, 549, 541, 534, 526, 519, 512, 505, 498, 492, 485, + 479, 473, 467, 461, 455, 450, 444, 439, 434, 429, 424, 419, 415, 410, 406, 402, 398, 394, 390, + 386, 383, 379, 376, 373, 370, 367, 364, 361, 359, 356, 354, 351, 349, 347, 345, 343, 342, 340, + 338, 337, 336, 334, 333, 332, 331, 330, 330, 329, 328, 328, 328, 327, 327, 327, 327, 327, 328, + 328, 328, 329, 330, 330, 331, 332, 333, 334, 336, 337, 338, 340, 342, 343, 345, 347, 349, 351, + 354, 356, 359, 361, 364, 367, 370, 373, 376, 379, 383, 386, 390, 394, 398, 402, 406, 410, 415, + 419, 424, 429, 434, 439, 444, 450, 455, 461, 467, 473, 479, 485, 492, 498, 505, 512, 519, 526, + 534, 541, 549, 557, 565, 574, 582, 591, 599, 609, 618, 627, 637, 647, 657, 667, 677, 688, 699, + 710, 721, 733, 744, 756, 768, 781, 793, 806, 819, 833, 846, 860, 874, 888, 903, 918, 933, 948, + 964, 980, 996, 1012, 1029, 1046, 1063, 1081, 1099, 1117, 1135, 1154, 1173, 1192, 1212, 1232, + 1252, 1273, 1294, 1315, 1336, 1358, 1380, 1403, 1426, 1449, 1472, 1496, 1520, 1545, 1570, 1595, + 1620, 1646, 1673, 1699, 1726, 1754, 1781, 1810, 1838, 1867, 1896, 1926, 1956, 1986, 2017, 2048, + 2080, 2112, 2144, 2177, 2210, 2244, 2278, 2312, 2347, 2382, 2418, 2454, 2491, 2528, 2565, 2603, + 2641, 2680, 2719, 2758, 2798, 2839, 2879, 2921, 2962, 3005, 3047, 3090, 3134, 3178, 3222, 3267, + 3312, 3358, 3404, 3451, 3498, 3546, 3594, 3642, 3691, 3741, 3791, 3841, 3892, 3943, 3995, 4047, + 4100, 4153, 4207, 4261, 4316, 4371, 4426, 4482, 4539, 4596, 4653, 4711, 4769, 4828, 4888, 4947, + 5008, 5068, 5130, 5191, 5253, 5316, 5379, 5443, 5507, 5571, 5636, 5702, 5767, 5834, 5901, 5968, + 6036, 6104, 6172, 6241, 6311, 6381, 6451, 6522, 6594, 6665, 6738, 6810, 6883, 6957, 7031, 7105, + 7180, 7255, 7331, 7407, 7484, 7561, 7638, 7716, 7794, 7873, 7952, 8031, 8111, +]; + #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let pwm = Pwm::new(p.PWM0, p.P0_05, NoPin, NoPin, NoPin); - // sg90 microervo requires 50hz or 20ms period - // set_period can only set down to 125khz so we cant use it directly - // Div128 is 125khz or 0.000008s or 0.008ms, 20/0.008 is 2500 is top - pwm.set_prescaler(Prescaler::Div128); - pwm.set_max_duty(2500); + let pwm = Pwm::new(p.PWM0, p.P0_13, p.P0_14, p.P0_16, p.P0_15); + pwm.set_prescaler(Prescaler::Div1); + pwm.set_max_duty(32767); + pwm.set_time_stretch(32); info!("pwm initialized!"); - Timer::after(Duration::from_millis(5000)).await; - - // 1ms 0deg (1/.008=125), 1.5ms 90deg (1.5/.008=187.5), 2ms 180deg (2/.008=250), + let mut i = 0; loop { - info!("45 deg"); - pwm.set_duty(0, 2500 - 156); - Timer::after(Duration::from_millis(5000)).await; - - info!("90 deg"); - pwm.set_duty(0, 2500 - 187); - Timer::after(Duration::from_millis(5000)).await; - - info!("135 deg"); - pwm.set_duty(0, 2500 - 218); - Timer::after(Duration::from_millis(5000)).await; - - info!("180 deg"); - pwm.set_duty(0, 2500 - 250); - Timer::after(Duration::from_millis(5000)).await; - - info!("0 deg"); - pwm.set_duty(0, 2500 - 125); - Timer::after(Duration::from_millis(5000)).await; + i += 1; + pwm.set_duty(0, DUTY[i % 1024]); + pwm.set_duty(1, DUTY[(i + 256) % 1024]); + pwm.set_duty(2, DUTY[(i + 512) % 1024]); + pwm.set_duty(3, DUTY[(i + 768) % 1024]); + Timer::after(Duration::from_millis(3)).await; } } diff --git a/examples/nrf/src/bin/pwm_servo.rs b/examples/nrf/src/bin/pwm_servo.rs new file mode 100644 index 00000000..aeda80be --- /dev/null +++ b/examples/nrf/src/bin/pwm_servo.rs @@ -0,0 +1,48 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +#[path = "../example_common.rs"] +mod example_common; +use defmt::*; +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_nrf::gpio::NoPin; +use embassy_nrf::pwm::{Prescaler, Pwm}; +use embassy_nrf::Peripherals; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + let pwm = Pwm::new(p.PWM0, p.P0_05, NoPin, NoPin, NoPin); + // sg90 microervo requires 50hz or 20ms period + // set_period can only set down to 125khz so we cant use it directly + // Div128 is 125khz or 0.000008s or 0.008ms, 20/0.008 is 2500 is top + pwm.set_prescaler(Prescaler::Div128); + pwm.set_max_duty(2500); + info!("pwm initialized!"); + + Timer::after(Duration::from_millis(5000)).await; + + // 1ms 0deg (1/.008=125), 1.5ms 90deg (1.5/.008=187.5), 2ms 180deg (2/.008=250), + loop { + info!("45 deg"); + pwm.set_duty(0, 2500 - 156); + Timer::after(Duration::from_millis(5000)).await; + + info!("90 deg"); + pwm.set_duty(0, 2500 - 187); + Timer::after(Duration::from_millis(5000)).await; + + info!("135 deg"); + pwm.set_duty(0, 2500 - 218); + Timer::after(Duration::from_millis(5000)).await; + + info!("180 deg"); + pwm.set_duty(0, 2500 - 250); + Timer::after(Duration::from_millis(5000)).await; + + info!("0 deg"); + pwm.set_duty(0, 2500 - 125); + Timer::after(Duration::from_millis(5000)).await; + } +} From 82695c8f653cb1479ca00efaf4c2bcda65e49ff7 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 3 Nov 2021 18:15:42 -0700 Subject: [PATCH 25/47] make both news configure registers in same order --- embassy-nrf/src/pwm.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 8c26adac..4f60e30b 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -118,17 +118,6 @@ impl<'d, T: Instance> PwmSeq<'d, T> { // Enable r.enable.write(|w| w.enable().enabled()); - r.mode - .write(|w| unsafe { w.bits(config.counter_mode as u32) }); - r.prescaler - .write(|w| w.prescaler().bits(config.prescaler as u8)); - r.countertop - .write(|w| unsafe { w.countertop().bits(config.top) }); - - r.decoder.write(|w| { - w.load().bits(config.sequence_load as u8); - w.mode().refresh_count() - }); r.seq0 .ptr @@ -152,6 +141,18 @@ impl<'d, T: Instance> PwmSeq<'d, T> { .enddelay .write(|w| unsafe { w.bits(config.end_delay) }); + r.decoder.write(|w| { + w.load().bits(config.sequence_load as u8); + w.mode().refresh_count() + }); + + r.mode + .write(|w| unsafe { w.bits(config.counter_mode as u32) }); + r.prescaler + .write(|w| w.prescaler().bits(config.prescaler as u8)); + r.countertop + .write(|w| unsafe { w.countertop().bits(config.top) }); + Ok(Self { phantom: PhantomData, }) From 511c5cb8927f1c55c4e006d03fa64fb0c00e154d Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 3 Nov 2021 18:16:00 -0700 Subject: [PATCH 26/47] intenset in a noop --- embassy-nrf/src/pwm.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 4f60e30b..c054addf 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -112,7 +112,6 @@ impl<'d, T: Instance> PwmSeq<'d, T> { r.psel.out[3].write(|w| unsafe { w.bits(ch3.psel_bits()) }); // Disable all interrupts - r.intenset.reset(); r.intenclr.write(|w| unsafe { w.bits(0xFFFF_FFFF) }); r.shorts.reset(); @@ -325,7 +324,6 @@ impl<'d, T: Instance> Pwm<'d, T> { r.psel.out[3].write(|w| unsafe { w.bits(ch3.psel_bits()) }); // Disable all interrupts - r.intenset.reset(); r.intenclr.write(|w| unsafe { w.bits(0xFFFF_FFFF) }); r.shorts.reset(); From 9a6c2de4ea473c06e1bb5731d51c9e9f64a6ba80 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 3 Nov 2021 18:19:43 -0700 Subject: [PATCH 27/47] fix safety comments --- embassy-nrf/src/pwm.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index c054addf..ccf0e70a 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -67,7 +67,7 @@ impl<'d, T: Instance> PwmSeq<'d, T> { /// /// The returned API is safe unless you use `mem::forget` (or similar safe /// mechanisms) on stack allocated buffers which which have been passed to - /// [`send()`](Pwm::send) or [`receive`](Pwm::receive). + /// [`new()`](PwmSeq::new). #[allow(unused_unsafe)] pub fn new( _pwm: impl Unborrow + 'd, @@ -285,7 +285,7 @@ impl<'d, T: Instance> Pwm<'d, T> { /// /// The returned API is safe unless you use `mem::forget` (or similar safe /// mechanisms) on stack allocated buffers which which have been passed to - /// [`send()`](Pwm::send) or [`receive`](Pwm::receive). + /// [`new()`](Pwm::new). #[allow(unused_unsafe)] pub fn new( _pwm: impl Unborrow + 'd, From d961fd1015f7911dc1085035c37b0554b0184982 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 3 Nov 2021 18:25:44 -0700 Subject: [PATCH 28/47] rename to SimplePwm and SequencePwm --- embassy-nrf/src/pwm.rs | 22 ++++++++-------------- examples/nrf/src/bin/pwm.rs | 5 ++--- examples/nrf/src/bin/pwm_led.rs | 4 ++-- examples/nrf/src/bin/pwm_sequence.rs | 4 ++-- examples/nrf/src/bin/pwm_servo.rs | 4 ++-- examples/nrf/src/bin/pwm_simple_sin.rs | 6 ++++-- 6 files changed, 20 insertions(+), 25 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index ccf0e70a..0d06f845 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -50,15 +50,15 @@ pub enum CounterMode { } /// Interface to the PWM peripheral -pub struct Pwm<'d, T: Instance> { +pub struct SimplePwm<'d, T: Instance> { phantom: PhantomData<&'d mut T>, } -pub struct PwmSeq<'d, T: Instance> { +pub struct SequencePwm<'d, T: Instance> { phantom: PhantomData<&'d mut T>, } -impl<'d, T: Instance> PwmSeq<'d, T> { +impl<'d, T: Instance> SequencePwm<'d, T> { /// Creates the interface to a PWM Sequence interface. /// /// Must be started by calling `start` @@ -67,7 +67,7 @@ impl<'d, T: Instance> PwmSeq<'d, T> { /// /// The returned API is safe unless you use `mem::forget` (or similar safe /// mechanisms) on stack allocated buffers which which have been passed to - /// [`new()`](PwmSeq::new). + /// [`new()`](SequencePwm::new). #[allow(unused_unsafe)] pub fn new( _pwm: impl Unborrow + 'd, @@ -226,7 +226,7 @@ impl<'d, T: Instance> PwmSeq<'d, T> { } } -impl<'a, T: Instance> Drop for PwmSeq<'a, T> { +impl<'a, T: Instance> Drop for SequencePwm<'a, T> { fn drop(&mut self) { self.stop(); self.disable(); @@ -275,7 +275,7 @@ pub enum Error { DMABufferNotInDataMemory, } -impl<'d, T: Instance> Pwm<'d, T> { +impl<'d, T: Instance> SimplePwm<'d, T> { /// Creates the interface to a PWM instance. /// /// Defaults the freq to 1Mhz, max_duty 32767, duty 0, and channels low. @@ -285,7 +285,7 @@ impl<'d, T: Instance> Pwm<'d, T> { /// /// The returned API is safe unless you use `mem::forget` (or similar safe /// mechanisms) on stack allocated buffers which which have been passed to - /// [`new()`](Pwm::new). + /// [`new()`](SimplePwm::new). #[allow(unused_unsafe)] pub fn new( _pwm: impl Unborrow + 'd, @@ -422,12 +422,6 @@ impl<'d, T: Instance> Pwm<'d, T> { .write(|w| unsafe { w.countertop().bits(duty.min(32767u16)) }); } - /// Additional number of PWM periods spent on each duty cycle value. - #[inline(always)] - pub fn set_time_stretch(&self, refresh: u32) { - T::regs().seq0.refresh.write(|w| unsafe { w.bits(refresh) }); - } - /// Returns the maximum duty cycle value. #[inline(always)] pub fn max_duty(&self) -> u16 { @@ -451,7 +445,7 @@ impl<'d, T: Instance> Pwm<'d, T> { } } -impl<'a, T: Instance> Drop for Pwm<'a, T> { +impl<'a, T: Instance> Drop for SimplePwm<'a, T> { fn drop(&mut self) { self.stop(); self.disable(); diff --git a/examples/nrf/src/bin/pwm.rs b/examples/nrf/src/bin/pwm.rs index 4c18b77b..0cb91cf1 100644 --- a/examples/nrf/src/bin/pwm.rs +++ b/examples/nrf/src/bin/pwm.rs @@ -7,7 +7,7 @@ mod example_common; use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; -use embassy_nrf::pwm::{Prescaler, Pwm}; +use embassy_nrf::pwm::{Prescaler, SimplePwm}; use embassy_nrf::Peripherals; // for i in range(1024): print(int((math.sin(i/512*math.pi)*0.4+0.5)**2*32767), ', ', end='') @@ -85,10 +85,9 @@ static DUTY: [u16; 1024] = [ #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let pwm = Pwm::new(p.PWM0, p.P0_13, p.P0_14, p.P0_16, p.P0_15); + let pwm = SimplePwm::new(p.PWM0, p.P0_13, p.P0_14, p.P0_16, p.P0_15); pwm.set_prescaler(Prescaler::Div1); pwm.set_max_duty(32767); - pwm.set_time_stretch(32); info!("pwm initialized!"); let mut i = 0; diff --git a/examples/nrf/src/bin/pwm_led.rs b/examples/nrf/src/bin/pwm_led.rs index 067f4d2c..ced6a713 100644 --- a/examples/nrf/src/bin/pwm_led.rs +++ b/examples/nrf/src/bin/pwm_led.rs @@ -8,12 +8,12 @@ use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_nrf::gpio::NoPin; -use embassy_nrf::pwm::{Prescaler, Pwm}; +use embassy_nrf::pwm::{Prescaler, SimplePwm}; use embassy_nrf::Peripherals; #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let pwm = Pwm::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin); + let pwm = SimplePwm::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin); // set_period doesnt actually set what you give it, because it only has a // few options from the hardhware so be explicit instead // Div128 is slowest, 125khz still crazy fast for our eyes diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 8830e52d..82575d70 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -8,7 +8,7 @@ use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_nrf::pwm::{ - CounterMode, Prescaler, PwmSeq, SequenceConfig, SequenceLoad, SequenceMode, + CounterMode, Prescaler, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm, }; use embassy_nrf::Peripherals; @@ -28,7 +28,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { end_delay: 0, }; - let pwm = unwrap!(PwmSeq::new( + let pwm = unwrap!(SequencePwm::new( p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config )); let _ = pwm.start(SequenceMode::Times(5)); diff --git a/examples/nrf/src/bin/pwm_servo.rs b/examples/nrf/src/bin/pwm_servo.rs index aeda80be..d681a201 100644 --- a/examples/nrf/src/bin/pwm_servo.rs +++ b/examples/nrf/src/bin/pwm_servo.rs @@ -8,12 +8,12 @@ use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_nrf::gpio::NoPin; -use embassy_nrf::pwm::{Prescaler, Pwm}; +use embassy_nrf::pwm::{Prescaler, SimplePwm}; use embassy_nrf::Peripherals; #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let pwm = Pwm::new(p.PWM0, p.P0_05, NoPin, NoPin, NoPin); + let pwm = SimplePwm::new(p.PWM0, p.P0_05, NoPin, NoPin, NoPin); // sg90 microervo requires 50hz or 20ms period // set_period can only set down to 125khz so we cant use it directly // Div128 is 125khz or 0.000008s or 0.008ms, 20/0.008 is 2500 is top diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index ac3089c8..afafb5d9 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -10,7 +10,7 @@ use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_nrf::gpio::NoPin; use embassy_nrf::pwm::{ - CounterMode, Prescaler, PwmSeq, SequenceConfig, SequenceLoad, SequenceMode, + CounterMode, Prescaler, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm, }; use embassy_nrf::Peripherals; use micromath::F32Ext; @@ -32,7 +32,9 @@ async fn main(_spawner: Spawner, p: Peripherals) { end_delay: 0, }; - let pwm = unwrap!(PwmSeq::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config)); + let pwm = unwrap!(SequencePwm::new( + p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config + )); let _ = pwm.start(SequenceMode::Infinite); info!("pwm started!"); From b726ef1886c65ab76b01f2c54ad559573e19083d Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 3 Nov 2021 18:37:54 -0700 Subject: [PATCH 29/47] make SequenceConfig struct is consistent with other Config structs, that are always non_exhaustive and have a Default --- embassy-nrf/src/pwm.rs | 143 ++++++++++++++----------- examples/nrf/src/bin/pwm_sequence.rs | 25 +++-- examples/nrf/src/bin/pwm_simple_sin.rs | 24 ++--- 3 files changed, 101 insertions(+), 91 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 0d06f845..9f88633c 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -12,43 +12,6 @@ use crate::interrupt::Interrupt; use crate::pac; use crate::util::slice_in_ram_or; -/// PWM Base clock is system clock (16MHz) divided by prescaler -#[derive(Debug, Eq, PartialEq, Clone, Copy)] -pub enum Prescaler { - Div1, - Div2, - Div4, - Div8, - Div16, - Div32, - Div64, - Div128, -} - -/// How the sequence values are distributed across the channels -#[derive(Debug, Eq, PartialEq, Clone, Copy)] -pub enum SequenceLoad { - /// Provided sequence will be used across all channels - Common, - /// Provided sequence contains grouped values for each channel ex: - /// [ch0_0_and_ch1_0, ch2_0_and_ch3_0, ... ch0_n_and_ch1_n, ch2_n_and_ch3_n] - Grouped, - /// Provided sequence contains individual values for each channel ex: - /// [ch0_0, ch1_0, ch2_0, ch3_0... ch0_n, ch1_n, ch2_n, ch3_n] - Individual, - /// Similar to Individual mode, but only three channels are used. The fourth - /// value is loaded into the pulse generator counter as its top value. - Waveform, -} - -#[derive(Debug, Eq, PartialEq, Clone, Copy)] -pub enum CounterMode { - /// Up counter (edge-aligned PWM duty cycle) - Up, - /// Up and down counter (center-aligned PWM duty cycle) - UpAndDown, -} - /// Interface to the PWM peripheral pub struct SimplePwm<'d, T: Instance> { phantom: PhantomData<&'d mut T>, @@ -58,6 +21,18 @@ pub struct SequencePwm<'d, T: Instance> { phantom: PhantomData<&'d mut T>, } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +#[non_exhaustive] +pub enum Error { + /// Max Sequence size is 32767 + SequenceTooLong, + /// Min Sequence count is 1 + SequenceTimesAtLeastOne, + /// EasyDMA can only read from data memory, read only buffers in flash will fail. + DMABufferNotInDataMemory, +} + impl<'d, T: Instance> SequencePwm<'d, T> { /// Creates the interface to a PWM Sequence interface. /// @@ -69,17 +44,18 @@ impl<'d, T: Instance> SequencePwm<'d, T> { /// mechanisms) on stack allocated buffers which which have been passed to /// [`new()`](SequencePwm::new). #[allow(unused_unsafe)] - pub fn new( + pub fn new<'a>( _pwm: impl Unborrow + 'd, ch0: impl Unborrow + 'd, ch1: impl Unborrow + 'd, ch2: impl Unborrow + 'd, ch3: impl Unborrow + 'd, config: SequenceConfig, + sequence: &'a [u16], ) -> Result { - slice_in_ram_or(config.sequence, Error::DMABufferNotInDataMemory)?; + slice_in_ram_or(sequence, Error::DMABufferNotInDataMemory)?; - if config.sequence.len() > 32767 { + if sequence.len() > 32767 { return Err(Error::SequenceTooLong); } @@ -120,10 +96,10 @@ impl<'d, T: Instance> SequencePwm<'d, T> { r.seq0 .ptr - .write(|w| unsafe { w.bits(config.sequence.as_ptr() as u32) }); + .write(|w| unsafe { w.bits(sequence.as_ptr() as u32) }); r.seq0 .cnt - .write(|w| unsafe { w.bits(config.sequence.len() as u32) }); + .write(|w| unsafe { w.bits(sequence.len() as u32) }); r.seq0.refresh.write(|w| unsafe { w.bits(config.refresh) }); r.seq0 .enddelay @@ -131,10 +107,10 @@ impl<'d, T: Instance> SequencePwm<'d, T> { r.seq1 .ptr - .write(|w| unsafe { w.bits(config.sequence.as_ptr() as u32) }); + .write(|w| unsafe { w.bits(sequence.as_ptr() as u32) }); r.seq1 .cnt - .write(|w| unsafe { w.bits(config.sequence.len() as u32) }); + .write(|w| unsafe { w.bits(sequence.len() as u32) }); r.seq1.refresh.write(|w| unsafe { w.bits(config.refresh) }); r.seq1 .enddelay @@ -237,24 +213,15 @@ impl<'a, T: Instance> Drop for SequencePwm<'a, T> { } } -#[derive(Debug, Eq, PartialEq, Clone, Copy)] -pub enum SequenceMode { - /// Run sequence n Times total - Times(u16), - /// Repeat until `stop` is called. - Infinite, -} - /// Configure an infinite looping sequence for `simple_playback` -pub struct SequenceConfig<'a> { +#[non_exhaustive] +pub struct SequenceConfig { /// Selects up mode or up-and-down mode for the counter pub counter_mode: CounterMode, /// Top value to be compared against buffer values pub top: u16, /// Configuration for PWM_CLK pub prescaler: Prescaler, - /// In ram buffer to be played back - pub sequence: &'a [u16], /// How a sequence is read from RAM and is spread to the compare register pub sequence_load: SequenceLoad, /// Number of Times PWM periods to delay between each sequence sample @@ -263,16 +230,62 @@ pub struct SequenceConfig<'a> { pub end_delay: u32, } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -#[cfg_attr(feature = "defmt", derive(defmt::Format))] -#[non_exhaustive] -pub enum Error { - /// Max Sequence size is 32767 - SequenceTooLong, - /// Min Sequence size is 1 - SequenceTimesAtLeastOne, - /// EasyDMA can only read from data memory, read only buffers in flash will fail. - DMABufferNotInDataMemory, +impl Default for SequenceConfig { + fn default() -> SequenceConfig { + SequenceConfig { + counter_mode: CounterMode::Up, + top: 1000, + prescaler: Prescaler::Div16, + sequence_load: SequenceLoad::Common, + refresh: 0, + end_delay: 0, + } + } +} + +#[derive(Debug, Eq, PartialEq, Clone, Copy)] +pub enum SequenceMode { + /// Run sequence n Times total + Times(u16), + /// Repeat until `stop` is called. + Infinite, +} + +/// PWM Base clock is system clock (16MHz) divided by prescaler +#[derive(Debug, Eq, PartialEq, Clone, Copy)] +pub enum Prescaler { + Div1, + Div2, + Div4, + Div8, + Div16, + Div32, + Div64, + Div128, +} + +/// How the sequence values are distributed across the channels +#[derive(Debug, Eq, PartialEq, Clone, Copy)] +pub enum SequenceLoad { + /// Provided sequence will be used across all channels + Common, + /// Provided sequence contains grouped values for each channel ex: + /// [ch0_0_and_ch1_0, ch2_0_and_ch3_0, ... ch0_n_and_ch1_n, ch2_n_and_ch3_n] + Grouped, + /// Provided sequence contains individual values for each channel ex: + /// [ch0_0, ch1_0, ch2_0, ch3_0... ch0_n, ch1_n, ch2_n, ch3_n] + Individual, + /// Similar to Individual mode, but only three channels are used. The fourth + /// value is loaded into the pulse generator counter as its top value. + Waveform, +} + +#[derive(Debug, Eq, PartialEq, Clone, Copy)] +pub enum CounterMode { + /// Up counter (edge-aligned PWM duty cycle) + Up, + /// Up and down counter (center-aligned PWM duty cycle) + UpAndDown, } impl<'d, T: Instance> SimplePwm<'d, T> { diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 82575d70..3f8b051d 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -7,9 +7,7 @@ mod example_common; use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; -use embassy_nrf::pwm::{ - CounterMode, Prescaler, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm, -}; +use embassy_nrf::pwm::{Prescaler, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm}; use embassy_nrf::Peripherals; #[embassy::main] @@ -18,18 +16,19 @@ async fn main(_spawner: Spawner, p: Peripherals) { 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, ]; - let config = SequenceConfig { - counter_mode: CounterMode::Up, - top: 15625, - prescaler: Prescaler::Div128, - sequence: &seq_values, - sequence_load: SequenceLoad::Individual, - refresh: 0, - end_delay: 0, - }; + let mut config = SequenceConfig::default(); + config.top = 15625; + config.prescaler = Prescaler::Div128; + config.sequence_load = SequenceLoad::Individual; let pwm = unwrap!(SequencePwm::new( - p.PWM0, p.P0_13, p.P0_15, p.P0_16, p.P0_14, config + p.PWM0, + p.P0_13, + p.P0_15, + p.P0_16, + p.P0_14, + config, + &seq_values, )); let _ = pwm.start(SequenceMode::Times(5)); info!("pwm started!"); diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs index afafb5d9..33fa6dcf 100644 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ b/examples/nrf/src/bin/pwm_simple_sin.rs @@ -9,9 +9,7 @@ use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_nrf::gpio::NoPin; -use embassy_nrf::pwm::{ - CounterMode, Prescaler, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm, -}; +use embassy_nrf::pwm::{CounterMode, SequenceConfig, SequenceMode, SequencePwm}; use embassy_nrf::Peripherals; use micromath::F32Ext; @@ -22,18 +20,18 @@ async fn main(_spawner: Spawner, p: Peripherals) { // probably not best use of resources to create the table at runtime, but makes testing fast let seq_values: [u16; 220] = core::array::from_fn(|n| ((W1 * n as f32).sin() * 10000.0) as u16); - let config = SequenceConfig { - counter_mode: CounterMode::UpAndDown, - top: 12000, - prescaler: Prescaler::Div16, - sequence: &seq_values, - sequence_load: SequenceLoad::Common, - refresh: 0, - end_delay: 0, - }; + let mut config = SequenceConfig::default(); + config.counter_mode = CounterMode::UpAndDown; + config.top = 12000; let pwm = unwrap!(SequencePwm::new( - p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config + p.PWM0, + p.P0_13, + NoPin, + NoPin, + NoPin, + config, + &seq_values )); let _ = pwm.start(SequenceMode::Infinite); info!("pwm started!"); From 64e1426b54c41a5b622c666cd7763bb07f73a2c4 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 3 Nov 2021 18:46:42 -0700 Subject: [PATCH 30/47] clean up some todo comments around infinite play --- embassy-nrf/src/pwm.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 9f88633c..b0acd83c 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -146,7 +146,6 @@ impl<'d, T: Instance> SequencePwm<'d, T> { r.enable.write(|w| w.enable().enabled()); match times { - // todo why doesn't this play forever? set_duty does... // just the one time, no loop count SequenceMode::Times(1) => { r.loop_.write(|w| w.cnt().disabled()); @@ -400,7 +399,6 @@ impl<'d, T: Instance> SimplePwm<'d, T> { // todo justify? should i fence elsehwere we task start? or compiler_fence(Ordering::SeqCst); - // todo why does this play forever when times(1) doesn't? // tasks_seqstart() doesn't exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(1) }); } From 03f2c593d6b919716d44478087f0dec6d4648382 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 3 Nov 2021 18:56:02 -0700 Subject: [PATCH 31/47] fix slice_in_ram for arbitrary size types --- embassy-nrf/src/util.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/embassy-nrf/src/util.rs b/embassy-nrf/src/util.rs index d17ec503..2fd0bc5a 100644 --- a/embassy-nrf/src/util.rs +++ b/embassy-nrf/src/util.rs @@ -4,13 +4,13 @@ const SRAM_UPPER: usize = 0x3000_0000; /// Does this slice reside entirely within RAM? pub(crate) fn slice_in_ram(slice: &[T]) -> bool { let ptr = slice.as_ptr() as usize; - ptr >= SRAM_LOWER && (ptr + slice.len()) < SRAM_UPPER + ptr >= SRAM_LOWER && (ptr + slice.len() * core::mem::size_of::()) < SRAM_UPPER } /// Return an error if slice is not in RAM. #[cfg(not(feature = "nrf51"))] pub(crate) fn slice_in_ram_or(slice: &[T], err: E) -> Result<(), E> { - if slice.len() == 0 || slice_in_ram(slice) { + if slice.is_empty() || slice_in_ram(slice) { Ok(()) } else { Err(err) From 65843c033ed592b879d89ee74eec20b2f78f6cd8 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 11:02:43 -0700 Subject: [PATCH 32/47] pwm store and deconfigure pins --- embassy-nrf/src/pwm.rs | 58 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index b0acd83c..8a6ed27f 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -7,7 +7,7 @@ use embassy::util::Unborrow; use embassy_hal_common::unborrow; use crate::gpio::sealed::Pin as _; -use crate::gpio::OptionalPin as GpioOptionalPin; +use crate::gpio::{AnyPin, OptionalPin as GpioOptionalPin, Pin}; use crate::interrupt::Interrupt; use crate::pac; use crate::util::slice_in_ram_or; @@ -15,10 +15,18 @@ use crate::util::slice_in_ram_or; /// Interface to the PWM peripheral pub struct SimplePwm<'d, T: Instance> { phantom: PhantomData<&'d mut T>, + ch0: Option, + ch1: Option, + ch2: Option, + ch3: Option, } pub struct SequencePwm<'d, T: Instance> { phantom: PhantomData<&'d mut T>, + ch0: Option, + ch1: Option, + ch2: Option, + ch3: Option, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -130,6 +138,10 @@ impl<'d, T: Instance> SequencePwm<'d, T> { Ok(Self { phantom: PhantomData, + ch0: ch0.degrade_optional(), + ch1: ch1.degrade_optional(), + ch2: ch2.degrade_optional(), + ch3: ch3.degrade_optional(), }) } @@ -206,9 +218,24 @@ impl<'a, T: Instance> Drop for SequencePwm<'a, T> { self.stop(); self.disable(); - info!("pwm drop: done"); + if let Some(pin) = &self.ch0 { + pin.set_low(); + pin.conf().write(|w| w); + } + if let Some(pin) = &self.ch1 { + pin.set_low(); + pin.conf().write(|w| w); + } + if let Some(pin) = &self.ch2 { + pin.set_low(); + pin.conf().write(|w| w); + } + if let Some(pin) = &self.ch3 { + pin.set_low(); + pin.conf().write(|w| w); + } - // TODO: disable pins + info!("pwm drop: done"); } } @@ -360,6 +387,10 @@ impl<'d, T: Instance> SimplePwm<'d, T> { Self { phantom: PhantomData, + ch0: ch0.degrade_optional(), + ch1: ch1.degrade_optional(), + ch2: ch2.degrade_optional(), + ch3: ch3.degrade_optional(), } } @@ -374,7 +405,6 @@ impl<'d, T: Instance> SimplePwm<'d, T> { r.tasks_stop.write(|w| unsafe { w.bits(0x01) }); } - // todo should this do.. something useful /// Enables the PWM generator. #[inline(always)] pub fn enable(&self) { @@ -382,7 +412,6 @@ impl<'d, T: Instance> SimplePwm<'d, T> { r.enable.write(|w| w.enable().enabled()); } - // todo should this stop the task? or should you just use set_duty to 0? /// Disables the PWM generator. #[inline(always)] pub fn disable(&self) { @@ -461,9 +490,24 @@ impl<'a, T: Instance> Drop for SimplePwm<'a, T> { self.stop(); self.disable(); - info!("pwm drop: done"); + if let Some(pin) = &self.ch0 { + pin.set_low(); + pin.conf().write(|w| w); + } + if let Some(pin) = &self.ch1 { + pin.set_low(); + pin.conf().write(|w| w); + } + if let Some(pin) = &self.ch2 { + pin.set_low(); + pin.conf().write(|w| w); + } + if let Some(pin) = &self.ch3 { + pin.set_low(); + pin.conf().write(|w| w); + } - // TODO: disable pins + info!("pwm drop: done"); } } From 9548748c263a5f34b8b1e26a679b9bb77af01b0a Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 12:29:13 -0700 Subject: [PATCH 33/47] fix --- embassy-nrf/src/pwm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 8a6ed27f..6387cd99 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -7,7 +7,7 @@ use embassy::util::Unborrow; use embassy_hal_common::unborrow; use crate::gpio::sealed::Pin as _; -use crate::gpio::{AnyPin, OptionalPin as GpioOptionalPin, Pin}; +use crate::gpio::{AnyPin, OptionalPin as GpioOptionalPin}; use crate::interrupt::Interrupt; use crate::pac; use crate::util::slice_in_ram_or; From 4751dbddc6466c30cd1bdd387adb9513ec19b3d2 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 12:32:28 -0700 Subject: [PATCH 34/47] move state into PwmSimple --- embassy-nrf/src/pwm.rs | 51 +++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 6387cd99..077a27cd 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -1,6 +1,5 @@ #![macro_use] -use core::cell::UnsafeCell; use core::marker::PhantomData; use core::sync::atomic::{compiler_fence, Ordering}; use embassy::util::Unborrow; @@ -15,6 +14,7 @@ use crate::util::slice_in_ram_or; /// Interface to the PWM peripheral pub struct SimplePwm<'d, T: Instance> { phantom: PhantomData<&'d mut T>, + duty: [u16; 4], ch0: Option, ch1: Option, ch2: Option, @@ -336,7 +336,6 @@ impl<'d, T: Instance> SimplePwm<'d, T> { unborrow!(ch0, ch1, ch2, ch3); let r = T::regs(); - let s = T::state(); if let Some(pin) = ch0.pin_mut() { pin.set_low(); @@ -362,6 +361,15 @@ impl<'d, T: Instance> SimplePwm<'d, T> { r.psel.out[2].write(|w| unsafe { w.bits(ch2.psel_bits()) }); r.psel.out[3].write(|w| unsafe { w.bits(ch3.psel_bits()) }); + let pwm = Self { + phantom: PhantomData, + ch0: ch0.degrade_optional(), + ch1: ch1.degrade_optional(), + ch2: ch2.degrade_optional(), + ch3: ch3.degrade_optional(), + duty: [0; 4], + }; + // Disable all interrupts r.intenclr.write(|w| unsafe { w.bits(0xFFFF_FFFF) }); r.shorts.reset(); @@ -371,7 +379,8 @@ impl<'d, T: Instance> SimplePwm<'d, T> { r.seq0 .ptr - .write(|w| unsafe { w.bits(&s.duty as *const _ as u32) }); + .write(|w| unsafe { w.bits(&pwm.duty as *const _ as u32) }); + r.seq0.cnt.write(|w| unsafe { w.bits(4) }); r.seq0.refresh.write(|w| unsafe { w.bits(0) }); r.seq0.enddelay.write(|w| unsafe { w.bits(0) }); @@ -385,13 +394,7 @@ impl<'d, T: Instance> SimplePwm<'d, T> { r.countertop.write(|w| unsafe { w.countertop().bits(1000) }); r.loop_.write(|w| w.cnt().disabled()); - Self { - phantom: PhantomData, - ch0: ch0.degrade_optional(), - ch1: ch1.degrade_optional(), - ch2: ch2.degrade_optional(), - ch3: ch3.degrade_optional(), - } + pwm } /// Stop playback @@ -420,10 +423,14 @@ impl<'d, T: Instance> SimplePwm<'d, T> { } /// Sets duty cycle (15 bit) for a PWM channel. - pub fn set_duty(&self, channel: usize, duty: u16) { + pub fn set_duty(&mut self, channel: usize, duty: u16) { let r = T::regs(); - let s = T::state(); - unsafe { (*s.duty.get())[channel] = duty & 0x7FFF }; + + self.duty[channel] = duty & 0x7FFF; + + r.seq0 + .ptr + .write(|w| unsafe { w.bits(&self.duty as *const _ as u32) }); // todo justify? should i fence elsehwere we task start? or compiler_fence(Ordering::SeqCst); @@ -514,22 +521,8 @@ impl<'a, T: Instance> Drop for SimplePwm<'a, T> { pub(crate) mod sealed { use super::*; - pub struct State { - pub duty: UnsafeCell<[u16; 4]>, - } - unsafe impl Sync for State {} - - impl State { - pub const fn new() -> Self { - Self { - duty: UnsafeCell::new([0; 4]), - } - } - } - pub trait Instance { fn regs() -> &'static pac::pwm0::RegisterBlock; - fn state() -> &'static State; } } @@ -543,10 +536,6 @@ macro_rules! impl_pwm { fn regs() -> &'static pac::pwm0::RegisterBlock { unsafe { &*pac::$pac_type::ptr() } } - fn state() -> &'static crate::pwm::sealed::State { - static STATE: crate::pwm::sealed::State = crate::pwm::sealed::State::new(); - &STATE - } } impl crate::pwm::Instance for peripherals::$type { type Interrupt = crate::interrupt::$irq; From 903b8f032fa36fb1bf8cd6f1d1b3c14ce7caa0dd Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 12:34:41 -0700 Subject: [PATCH 35/47] defensive dma --- embassy-nrf/src/pwm.rs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 077a27cd..e4ddda39 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -3,7 +3,7 @@ use core::marker::PhantomData; use core::sync::atomic::{compiler_fence, Ordering}; use embassy::util::Unborrow; -use embassy_hal_common::unborrow; +use embassy_hal_common::{low_power_wait_until, unborrow}; use crate::gpio::sealed::Pin as _; use crate::gpio::{AnyPin, OptionalPin as GpioOptionalPin}; @@ -157,12 +157,19 @@ impl<'d, T: Instance> SequencePwm<'d, T> { r.enable.write(|w| w.enable().enabled()); + // defensive before seqstart + compiler_fence(Ordering::SeqCst); + match times { // just the one time, no loop count SequenceMode::Times(1) => { r.loop_.write(|w| w.cnt().disabled()); // tasks_seqstart() doesn't exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); + + // defensive wait until waveform is loaded after seqstart + low_power_wait_until(|| r.events_seqend[0].read().bits() == 1); + r.events_seqend[0].write(|w| w); } // loop count is how many times to play BOTH sequences // 2 total (1 x 2) @@ -177,17 +184,30 @@ impl<'d, T: Instance> SequencePwm<'d, T> { if odd { // tasks_seqstart() doesn't exist in all svds so write its bit instead r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); + + // defensive wait until waveform is loaded after seqstart + low_power_wait_until(|| r.events_seqend[1].read().bits() == 1); + r.events_seqend[1].write(|w| w); } else { // tasks_seqstart() doesn't exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); + + // defensive wait until waveform is loaded after seqstart + low_power_wait_until(|| r.events_seqend[0].read().bits() == 1); + r.events_seqend[0].write(|w| w); } } // to play infinitely, repeat the sequence one time, then have loops done self trigger seq0 again SequenceMode::Infinite => { r.loop_.write(|w| unsafe { w.cnt().bits(0x1) }); r.shorts.write(|w| w.loopsdone_seqstart0().enabled()); + // tasks_seqstart() doesn't exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); + + // defensive wait until waveform is loaded after seqstart + low_power_wait_until(|| r.events_seqend[0].read().bits() == 1); + r.events_seqend[0].write(|w| w); } } @@ -201,6 +221,8 @@ impl<'d, T: Instance> SequencePwm<'d, T> { r.shorts.reset(); + compiler_fence(Ordering::SeqCst); + // tasks_stop() doesn't exist in all svds so write its bit instead r.tasks_stop.write(|w| unsafe { w.bits(0x01) }); } @@ -404,6 +426,8 @@ impl<'d, T: Instance> SimplePwm<'d, T> { r.shorts.reset(); + compiler_fence(Ordering::SeqCst); + // tasks_stop() doesn't exist in all svds so write its bit instead r.tasks_stop.write(|w| unsafe { w.bits(0x01) }); } @@ -432,11 +456,15 @@ impl<'d, T: Instance> SimplePwm<'d, T> { .ptr .write(|w| unsafe { w.bits(&self.duty as *const _ as u32) }); - // todo justify? should i fence elsehwere we task start? or + // defensive before seqstart compiler_fence(Ordering::SeqCst); // tasks_seqstart() doesn't exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(1) }); + + // defensive wait until waveform is loaded after seqstart + low_power_wait_until(|| r.events_seqend[0].read().bits() == 1); + r.events_seqend[0].write(|w| w); } /// Sets the PWM clock prescaler. From 751617c2be10c047ccf6986ace489999b80a01b0 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 12:48:15 -0700 Subject: [PATCH 36/47] fix examples for mut self set_duty --- examples/nrf/src/bin/pwm.rs | 2 +- examples/nrf/src/bin/pwm_led.rs | 2 +- examples/nrf/src/bin/pwm_servo.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/nrf/src/bin/pwm.rs b/examples/nrf/src/bin/pwm.rs index 0cb91cf1..8679eddd 100644 --- a/examples/nrf/src/bin/pwm.rs +++ b/examples/nrf/src/bin/pwm.rs @@ -85,7 +85,7 @@ static DUTY: [u16; 1024] = [ #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let pwm = SimplePwm::new(p.PWM0, p.P0_13, p.P0_14, p.P0_16, p.P0_15); + let mut pwm = SimplePwm::new(p.PWM0, p.P0_13, p.P0_14, p.P0_16, p.P0_15); pwm.set_prescaler(Prescaler::Div1); pwm.set_max_duty(32767); info!("pwm initialized!"); diff --git a/examples/nrf/src/bin/pwm_led.rs b/examples/nrf/src/bin/pwm_led.rs index ced6a713..d0b71a5c 100644 --- a/examples/nrf/src/bin/pwm_led.rs +++ b/examples/nrf/src/bin/pwm_led.rs @@ -13,7 +13,7 @@ use embassy_nrf::Peripherals; #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let pwm = SimplePwm::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin); + let mut pwm = SimplePwm::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin); // set_period doesnt actually set what you give it, because it only has a // few options from the hardhware so be explicit instead // Div128 is slowest, 125khz still crazy fast for our eyes diff --git a/examples/nrf/src/bin/pwm_servo.rs b/examples/nrf/src/bin/pwm_servo.rs index d681a201..700b8857 100644 --- a/examples/nrf/src/bin/pwm_servo.rs +++ b/examples/nrf/src/bin/pwm_servo.rs @@ -13,7 +13,7 @@ use embassy_nrf::Peripherals; #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let pwm = SimplePwm::new(p.PWM0, p.P0_05, NoPin, NoPin, NoPin); + let mut pwm = SimplePwm::new(p.PWM0, p.P0_05, NoPin, NoPin, NoPin); // sg90 microervo requires 50hz or 20ms period // set_period can only set down to 125khz so we cant use it directly // Div128 is 125khz or 0.000008s or 0.008ms, 20/0.008 is 2500 is top From 3b7af2f4edccc57d31c6db5644b3f1384600f0fd Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 13:01:39 -0700 Subject: [PATCH 37/47] fix wait seqend, pwm example is slow now... --- embassy-nrf/src/pwm.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index e4ddda39..f0436b5b 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -3,7 +3,7 @@ use core::marker::PhantomData; use core::sync::atomic::{compiler_fence, Ordering}; use embassy::util::Unborrow; -use embassy_hal_common::{low_power_wait_until, unborrow}; +use embassy_hal_common::unborrow; use crate::gpio::sealed::Pin as _; use crate::gpio::{AnyPin, OptionalPin as GpioOptionalPin}; @@ -168,7 +168,7 @@ impl<'d, T: Instance> SequencePwm<'d, T> { r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); // defensive wait until waveform is loaded after seqstart - low_power_wait_until(|| r.events_seqend[0].read().bits() == 1); + while r.events_seqend[0].read().bits() == 0 {} r.events_seqend[0].write(|w| w); } // loop count is how many times to play BOTH sequences @@ -186,14 +186,14 @@ impl<'d, T: Instance> SequencePwm<'d, T> { r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); // defensive wait until waveform is loaded after seqstart - low_power_wait_until(|| r.events_seqend[1].read().bits() == 1); + while r.events_seqend[1].read().bits() == 0 {} r.events_seqend[1].write(|w| w); } else { // tasks_seqstart() doesn't exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); // defensive wait until waveform is loaded after seqstart - low_power_wait_until(|| r.events_seqend[0].read().bits() == 1); + while r.events_seqend[0].read().bits() == 0 {} r.events_seqend[0].write(|w| w); } } @@ -206,7 +206,7 @@ impl<'d, T: Instance> SequencePwm<'d, T> { r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); // defensive wait until waveform is loaded after seqstart - low_power_wait_until(|| r.events_seqend[0].read().bits() == 1); + while r.events_seqend[0].read().bits() == 0 {} r.events_seqend[0].write(|w| w); } } @@ -463,7 +463,7 @@ impl<'d, T: Instance> SimplePwm<'d, T> { r.tasks_seqstart[0].write(|w| unsafe { w.bits(1) }); // defensive wait until waveform is loaded after seqstart - low_power_wait_until(|| r.events_seqend[0].read().bits() == 1); + while r.events_seqend[0].read().bits() == 0 {} r.events_seqend[0].write(|w| w); } From 5f28153b811caf1e47b51d87eae74d36eeff9a7e Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 17:19:03 -0700 Subject: [PATCH 38/47] disconnect pwm pins on drop --- embassy-nrf/src/pwm.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index f0436b5b..252e6385 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -237,24 +237,30 @@ impl<'d, T: Instance> SequencePwm<'d, T> { impl<'a, T: Instance> Drop for SequencePwm<'a, T> { fn drop(&mut self) { + let r = T::regs(); + self.stop(); self.disable(); if let Some(pin) = &self.ch0 { pin.set_low(); pin.conf().write(|w| w); + r.psel.out[0].write(|w| unsafe { w.bits(0x80000000) }); } if let Some(pin) = &self.ch1 { pin.set_low(); pin.conf().write(|w| w); + r.psel.out[1].write(|w| unsafe { w.bits(0x80000000) }); } if let Some(pin) = &self.ch2 { pin.set_low(); pin.conf().write(|w| w); + r.psel.out[2].write(|w| unsafe { w.bits(0x80000000) }); } if let Some(pin) = &self.ch3 { pin.set_low(); pin.conf().write(|w| w); + r.psel.out[3].write(|w| unsafe { w.bits(0x80000000) }); } info!("pwm drop: done"); @@ -522,24 +528,30 @@ impl<'d, T: Instance> SimplePwm<'d, T> { impl<'a, T: Instance> Drop for SimplePwm<'a, T> { fn drop(&mut self) { + let r = T::regs(); + self.stop(); self.disable(); if let Some(pin) = &self.ch0 { pin.set_low(); pin.conf().write(|w| w); + r.psel.out[0].write(|w| unsafe { w.bits(0x80000000) }); } if let Some(pin) = &self.ch1 { pin.set_low(); pin.conf().write(|w| w); + r.psel.out[1].write(|w| unsafe { w.bits(0x80000000) }); } if let Some(pin) = &self.ch2 { pin.set_low(); pin.conf().write(|w| w); + r.psel.out[2].write(|w| unsafe { w.bits(0x80000000) }); } if let Some(pin) = &self.ch3 { pin.set_low(); pin.conf().write(|w| w); + r.psel.out[3].write(|w| unsafe { w.bits(0x80000000) }); } info!("pwm drop: done"); From 6dcc731065ff7a4e9515f21172b309f50155c353 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 17:25:38 -0700 Subject: [PATCH 39/47] drop the debug --- embassy-nrf/src/pwm.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 252e6385..c91cf9a7 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -262,8 +262,6 @@ impl<'a, T: Instance> Drop for SequencePwm<'a, T> { pin.conf().write(|w| w); r.psel.out[3].write(|w| unsafe { w.bits(0x80000000) }); } - - info!("pwm drop: done"); } } @@ -553,8 +551,6 @@ impl<'a, T: Instance> Drop for SimplePwm<'a, T> { pin.conf().write(|w| w); r.psel.out[3].write(|w| unsafe { w.bits(0x80000000) }); } - - info!("pwm drop: done"); } } From c450f91bd9c1ad37f5556c0b8378271b5d306b9a Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 17:30:59 -0700 Subject: [PATCH 40/47] doc comment cleanup --- embassy-nrf/src/pwm.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index c91cf9a7..c8e39113 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -265,7 +265,7 @@ impl<'a, T: Instance> Drop for SequencePwm<'a, T> { } } -/// Configure an infinite looping sequence for `simple_playback` +/// Configure an infinite looping sequence for `SequencePwm` #[non_exhaustive] pub struct SequenceConfig { /// Selects up mode or up-and-down mode for the counter @@ -295,6 +295,7 @@ impl Default for SequenceConfig { } } +/// How many times to run the sequence #[derive(Debug, Eq, PartialEq, Clone, Copy)] pub enum SequenceMode { /// Run sequence n Times total @@ -332,6 +333,7 @@ pub enum SequenceLoad { Waveform, } +/// Selects up mode or up-and-down mode for the counter #[derive(Debug, Eq, PartialEq, Clone, Copy)] pub enum CounterMode { /// Up counter (edge-aligned PWM duty cycle) @@ -343,7 +345,7 @@ pub enum CounterMode { impl<'d, T: Instance> SimplePwm<'d, T> { /// Creates the interface to a PWM instance. /// - /// Defaults the freq to 1Mhz, max_duty 32767, duty 0, and channels low. + /// Defaults the freq to 1Mhz, max_duty 1000, duty 0, and pins low. /// Must be started by calling `set_duty` /// /// # Safety From 2973ff4cf05458cb80718c35c7fe9fa690d0cf8b Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 18:49:24 -0700 Subject: [PATCH 41/47] remove unstable feature and dependency, and make pwm_sequence a near mirror of pwm example --- examples/nrf/Cargo.toml | 1 - examples/nrf/src/bin/pwm_sequence.rs | 98 +++++++++++++++++++++++--- examples/nrf/src/bin/pwm_simple_sin.rs | 47 ------------ 3 files changed, 87 insertions(+), 59 deletions(-) delete mode 100644 examples/nrf/src/bin/pwm_simple_sin.rs diff --git a/examples/nrf/Cargo.toml b/examples/nrf/Cargo.toml index e0025b73..b89aa513 100644 --- a/examples/nrf/Cargo.toml +++ b/examples/nrf/Cargo.toml @@ -31,4 +31,3 @@ panic-probe = { version = "0.2.0", features = ["print-defmt"] } futures = { version = "0.3.17", default-features = false, features = ["async-await"] } rand = { version = "0.8.4", default-features = false } embedded-storage = "0.2.0" -micromath = "2.0.0" \ No newline at end of file diff --git a/examples/nrf/src/bin/pwm_sequence.rs b/examples/nrf/src/bin/pwm_sequence.rs index 3f8b051d..d02b0c9c 100644 --- a/examples/nrf/src/bin/pwm_sequence.rs +++ b/examples/nrf/src/bin/pwm_sequence.rs @@ -7,30 +7,106 @@ mod example_common; use defmt::*; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; -use embassy_nrf::pwm::{Prescaler, SequenceConfig, SequenceLoad, SequenceMode, SequencePwm}; +use embassy_nrf::gpio::NoPin; +use embassy_nrf::pwm::{Prescaler, SequenceConfig, SequenceMode, SequencePwm}; use embassy_nrf::Peripherals; #[embassy::main] async fn main(_spawner: Spawner, p: Peripherals) { - let seq_values: [u16; 16] = [ - 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, 0, 0, 0, 0, 0x8000, + // for i in range(1024): print(int((math.sin(i/512*math.pi)*0.4+0.5)**2*32767), ', ', end='') + let seq_values: [u16; 1024] = [ + 8191, 8272, 8353, 8434, 8516, 8598, 8681, 8764, 8847, 8931, 9015, 9099, 9184, 9269, 9354, + 9440, 9526, 9613, 9700, 9787, 9874, 9962, 10050, 10139, 10227, 10316, 10406, 10495, 10585, + 10675, 10766, 10857, 10948, 11039, 11131, 11223, 11315, 11407, 11500, 11592, 11685, 11779, + 11872, 11966, 12060, 12154, 12248, 12343, 12438, 12533, 12628, 12723, 12818, 12914, 13010, + 13106, 13202, 13298, 13394, 13491, 13587, 13684, 13781, 13878, 13975, 14072, 14169, 14266, + 14364, 14461, 14558, 14656, 14754, 14851, 14949, 15046, 15144, 15242, 15339, 15437, 15535, + 15632, 15730, 15828, 15925, 16023, 16120, 16218, 16315, 16412, 16510, 16607, 16704, 16801, + 16898, 16995, 17091, 17188, 17284, 17380, 17477, 17572, 17668, 17764, 17859, 17955, 18050, + 18145, 18239, 18334, 18428, 18522, 18616, 18710, 18803, 18896, 18989, 19082, 19174, 19266, + 19358, 19449, 19540, 19631, 19722, 19812, 19902, 19991, 20081, 20169, 20258, 20346, 20434, + 20521, 20608, 20695, 20781, 20867, 20952, 21037, 21122, 21206, 21290, 21373, 21456, 21538, + 21620, 21701, 21782, 21863, 21943, 22022, 22101, 22179, 22257, 22335, 22412, 22488, 22564, + 22639, 22714, 22788, 22861, 22934, 23007, 23079, 23150, 23220, 23290, 23360, 23429, 23497, + 23564, 23631, 23698, 23763, 23828, 23892, 23956, 24019, 24081, 24143, 24204, 24264, 24324, + 24383, 24441, 24499, 24555, 24611, 24667, 24721, 24775, 24828, 24881, 24933, 24983, 25034, + 25083, 25132, 25180, 25227, 25273, 25319, 25363, 25407, 25451, 25493, 25535, 25575, 25615, + 25655, 25693, 25731, 25767, 25803, 25838, 25873, 25906, 25939, 25971, 26002, 26032, 26061, + 26089, 26117, 26144, 26170, 26195, 26219, 26242, 26264, 26286, 26307, 26327, 26346, 26364, + 26381, 26397, 26413, 26427, 26441, 26454, 26466, 26477, 26487, 26496, 26505, 26512, 26519, + 26525, 26530, 26534, 26537, 26539, 26540, 26541, 26540, 26539, 26537, 26534, 26530, 26525, + 26519, 26512, 26505, 26496, 26487, 26477, 26466, 26454, 26441, 26427, 26413, 26397, 26381, + 26364, 26346, 26327, 26307, 26286, 26264, 26242, 26219, 26195, 26170, 26144, 26117, 26089, + 26061, 26032, 26002, 25971, 25939, 25906, 25873, 25838, 25803, 25767, 25731, 25693, 25655, + 25615, 25575, 25535, 25493, 25451, 25407, 25363, 25319, 25273, 25227, 25180, 25132, 25083, + 25034, 24983, 24933, 24881, 24828, 24775, 24721, 24667, 24611, 24555, 24499, 24441, 24383, + 24324, 24264, 24204, 24143, 24081, 24019, 23956, 23892, 23828, 23763, 23698, 23631, 23564, + 23497, 23429, 23360, 23290, 23220, 23150, 23079, 23007, 22934, 22861, 22788, 22714, 22639, + 22564, 22488, 22412, 22335, 22257, 22179, 22101, 22022, 21943, 21863, 21782, 21701, 21620, + 21538, 21456, 21373, 21290, 21206, 21122, 21037, 20952, 20867, 20781, 20695, 20608, 20521, + 20434, 20346, 20258, 20169, 20081, 19991, 19902, 19812, 19722, 19631, 19540, 19449, 19358, + 19266, 19174, 19082, 18989, 18896, 18803, 18710, 18616, 18522, 18428, 18334, 18239, 18145, + 18050, 17955, 17859, 17764, 17668, 17572, 17477, 17380, 17284, 17188, 17091, 16995, 16898, + 16801, 16704, 16607, 16510, 16412, 16315, 16218, 16120, 16023, 15925, 15828, 15730, 15632, + 15535, 15437, 15339, 15242, 15144, 15046, 14949, 14851, 14754, 14656, 14558, 14461, 14364, + 14266, 14169, 14072, 13975, 13878, 13781, 13684, 13587, 13491, 13394, 13298, 13202, 13106, + 13010, 12914, 12818, 12723, 12628, 12533, 12438, 12343, 12248, 12154, 12060, 11966, 11872, + 11779, 11685, 11592, 11500, 11407, 11315, 11223, 11131, 11039, 10948, 10857, 10766, 10675, + 10585, 10495, 10406, 10316, 10227, 10139, 10050, 9962, 9874, 9787, 9700, 9613, 9526, 9440, + 9354, 9269, 9184, 9099, 9015, 8931, 8847, 8764, 8681, 8598, 8516, 8434, 8353, 8272, 8191, + 8111, 8031, 7952, 7873, 7794, 7716, 7638, 7561, 7484, 7407, 7331, 7255, 7180, 7105, 7031, + 6957, 6883, 6810, 6738, 6665, 6594, 6522, 6451, 6381, 6311, 6241, 6172, 6104, 6036, 5968, + 5901, 5834, 5767, 5702, 5636, 5571, 5507, 5443, 5379, 5316, 5253, 5191, 5130, 5068, 5008, + 4947, 4888, 4828, 4769, 4711, 4653, 4596, 4539, 4482, 4426, 4371, 4316, 4261, 4207, 4153, + 4100, 4047, 3995, 3943, 3892, 3841, 3791, 3741, 3691, 3642, 3594, 3546, 3498, 3451, 3404, + 3358, 3312, 3267, 3222, 3178, 3134, 3090, 3047, 3005, 2962, 2921, 2879, 2839, 2798, 2758, + 2719, 2680, 2641, 2603, 2565, 2528, 2491, 2454, 2418, 2382, 2347, 2312, 2278, 2244, 2210, + 2177, 2144, 2112, 2080, 2048, 2017, 1986, 1956, 1926, 1896, 1867, 1838, 1810, 1781, 1754, + 1726, 1699, 1673, 1646, 1620, 1595, 1570, 1545, 1520, 1496, 1472, 1449, 1426, 1403, 1380, + 1358, 1336, 1315, 1294, 1273, 1252, 1232, 1212, 1192, 1173, 1154, 1135, 1117, 1099, 1081, + 1063, 1046, 1029, 1012, 996, 980, 964, 948, 933, 918, 903, 888, 874, 860, 846, 833, 819, + 806, 793, 781, 768, 756, 744, 733, 721, 710, 699, 688, 677, 667, 657, 647, 637, 627, 618, + 609, 599, 591, 582, 574, 565, 557, 549, 541, 534, 526, 519, 512, 505, 498, 492, 485, 479, + 473, 467, 461, 455, 450, 444, 439, 434, 429, 424, 419, 415, 410, 406, 402, 398, 394, 390, + 386, 383, 379, 376, 373, 370, 367, 364, 361, 359, 356, 354, 351, 349, 347, 345, 343, 342, + 340, 338, 337, 336, 334, 333, 332, 331, 330, 330, 329, 328, 328, 328, 327, 327, 327, 327, + 327, 328, 328, 328, 329, 330, 330, 331, 332, 333, 334, 336, 337, 338, 340, 342, 343, 345, + 347, 349, 351, 354, 356, 359, 361, 364, 367, 370, 373, 376, 379, 383, 386, 390, 394, 398, + 402, 406, 410, 415, 419, 424, 429, 434, 439, 444, 450, 455, 461, 467, 473, 479, 485, 492, + 498, 505, 512, 519, 526, 534, 541, 549, 557, 565, 574, 582, 591, 599, 609, 618, 627, 637, + 647, 657, 667, 677, 688, 699, 710, 721, 733, 744, 756, 768, 781, 793, 806, 819, 833, 846, + 860, 874, 888, 903, 918, 933, 948, 964, 980, 996, 1012, 1029, 1046, 1063, 1081, 1099, 1117, + 1135, 1154, 1173, 1192, 1212, 1232, 1252, 1273, 1294, 1315, 1336, 1358, 1380, 1403, 1426, + 1449, 1472, 1496, 1520, 1545, 1570, 1595, 1620, 1646, 1673, 1699, 1726, 1754, 1781, 1810, + 1838, 1867, 1896, 1926, 1956, 1986, 2017, 2048, 2080, 2112, 2144, 2177, 2210, 2244, 2278, + 2312, 2347, 2382, 2418, 2454, 2491, 2528, 2565, 2603, 2641, 2680, 2719, 2758, 2798, 2839, + 2879, 2921, 2962, 3005, 3047, 3090, 3134, 3178, 3222, 3267, 3312, 3358, 3404, 3451, 3498, + 3546, 3594, 3642, 3691, 3741, 3791, 3841, 3892, 3943, 3995, 4047, 4100, 4153, 4207, 4261, + 4316, 4371, 4426, 4482, 4539, 4596, 4653, 4711, 4769, 4828, 4888, 4947, 5008, 5068, 5130, + 5191, 5253, 5316, 5379, 5443, 5507, 5571, 5636, 5702, 5767, 5834, 5901, 5968, 6036, 6104, + 6172, 6241, 6311, 6381, 6451, 6522, 6594, 6665, 6738, 6810, 6883, 6957, 7031, 7105, 7180, + 7255, 7331, 7407, 7484, 7561, 7638, 7716, 7794, 7873, 7952, 8031, 8111, ]; let mut config = SequenceConfig::default(); - config.top = 15625; - config.prescaler = Prescaler::Div128; - config.sequence_load = SequenceLoad::Individual; + config.prescaler = Prescaler::Div1; + // 1 period is 32767 * 1/16mhz = 0.002047938 = 2.047938ms + config.top = 32767; + // pwm example is delaying >~3ms before updating duty cycle, our refreshes + // happen exactly at 2.047938ms so we need a delay after each value of >~1ms + // which for us is ~1-2 periods + config.refresh = 3; let pwm = unwrap!(SequencePwm::new( p.PWM0, p.P0_13, - p.P0_15, - p.P0_16, - p.P0_14, + NoPin, + NoPin, + NoPin, config, - &seq_values, + &seq_values )); - let _ = pwm.start(SequenceMode::Times(5)); + let _ = pwm.start(SequenceMode::Infinite); info!("pwm started!"); loop { diff --git a/examples/nrf/src/bin/pwm_simple_sin.rs b/examples/nrf/src/bin/pwm_simple_sin.rs deleted file mode 100644 index 33fa6dcf..00000000 --- a/examples/nrf/src/bin/pwm_simple_sin.rs +++ /dev/null @@ -1,47 +0,0 @@ -#![no_std] -#![no_main] -#![feature(type_alias_impl_trait)] -#![feature(array_from_fn)] - -#[path = "../example_common.rs"] -mod example_common; -use defmt::*; -use embassy::executor::Spawner; -use embassy::time::{Duration, Timer}; -use embassy_nrf::gpio::NoPin; -use embassy_nrf::pwm::{CounterMode, SequenceConfig, SequenceMode, SequencePwm}; -use embassy_nrf::Peripherals; -use micromath::F32Ext; - -const W1: f32 = core::f32::consts::PI / 128.0; - -#[embassy::main] -async fn main(_spawner: Spawner, p: Peripherals) { - // probably not best use of resources to create the table at runtime, but makes testing fast - let seq_values: [u16; 220] = core::array::from_fn(|n| ((W1 * n as f32).sin() * 10000.0) as u16); - - let mut config = SequenceConfig::default(); - config.counter_mode = CounterMode::UpAndDown; - config.top = 12000; - - let pwm = unwrap!(SequencePwm::new( - p.PWM0, - p.P0_13, - NoPin, - NoPin, - NoPin, - config, - &seq_values - )); - let _ = pwm.start(SequenceMode::Infinite); - info!("pwm started!"); - - Timer::after(Duration::from_millis(20000)).await; - - pwm.stop(); - info!("pwm stopped!"); - - loop { - Timer::after(Duration::from_millis(1000)).await; - } -} From c6736abf5e0f59c0bd96cf6f57c1f23c50fc54cd Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 18:52:03 -0700 Subject: [PATCH 42/47] dont wait seqend for sequencepwm --- embassy-nrf/src/pwm.rs | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index c8e39113..a53956c6 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -166,10 +166,6 @@ impl<'d, T: Instance> SequencePwm<'d, T> { r.loop_.write(|w| w.cnt().disabled()); // tasks_seqstart() doesn't exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); - - // defensive wait until waveform is loaded after seqstart - while r.events_seqend[0].read().bits() == 0 {} - r.events_seqend[0].write(|w| w); } // loop count is how many times to play BOTH sequences // 2 total (1 x 2) @@ -184,17 +180,9 @@ impl<'d, T: Instance> SequencePwm<'d, T> { if odd { // tasks_seqstart() doesn't exist in all svds so write its bit instead r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); - - // defensive wait until waveform is loaded after seqstart - while r.events_seqend[1].read().bits() == 0 {} - r.events_seqend[1].write(|w| w); } else { // tasks_seqstart() doesn't exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); - - // defensive wait until waveform is loaded after seqstart - while r.events_seqend[0].read().bits() == 0 {} - r.events_seqend[0].write(|w| w); } } // to play infinitely, repeat the sequence one time, then have loops done self trigger seq0 again @@ -204,10 +192,6 @@ impl<'d, T: Instance> SequencePwm<'d, T> { // tasks_seqstart() doesn't exist in all svds so write its bit instead r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); - - // defensive wait until waveform is loaded after seqstart - while r.events_seqend[0].read().bits() == 0 {} - r.events_seqend[0].write(|w| w); } } From 4c2d0ac211af20aac3e2e0fbef1b540f92247ccb Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 18:52:29 -0700 Subject: [PATCH 43/47] doccomments --- embassy-nrf/src/pwm.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index a53956c6..5f31b772 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -260,9 +260,9 @@ pub struct SequenceConfig { pub prescaler: Prescaler, /// How a sequence is read from RAM and is spread to the compare register pub sequence_load: SequenceLoad, - /// Number of Times PWM periods to delay between each sequence sample + /// Number of PWM periods to delay between each sequence sample pub refresh: u32, - /// Number of Times PWM periods after the sequence ends before starting the next sequence + /// Number of PWM periods after the sequence ends before starting the next sequence pub end_delay: u32, } @@ -329,7 +329,7 @@ pub enum CounterMode { impl<'d, T: Instance> SimplePwm<'d, T> { /// Creates the interface to a PWM instance. /// - /// Defaults the freq to 1Mhz, max_duty 1000, duty 0, and pins low. + /// Defaults the freq to 1Mhz, max_duty 1000, duty 0, up mode, and pins low. /// Must be started by calling `set_duty` /// /// # Safety From 613e88f1d2dad3c8f5ea2471bb5b714e180a5ede Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 18:53:21 -0700 Subject: [PATCH 44/47] fix bug with config.counter_mode --- embassy-nrf/src/pwm.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 5f31b772..8c4a5dae 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -129,8 +129,10 @@ impl<'d, T: Instance> SequencePwm<'d, T> { w.mode().refresh_count() }); - r.mode - .write(|w| unsafe { w.bits(config.counter_mode as u32) }); + r.mode.write(|w| match config.counter_mode { + CounterMode::UpAndDown => w.updown().up_and_down(), + CounterMode::Up => w.updown().up(), + }); r.prescaler .write(|w| w.prescaler().bits(config.prescaler as u8)); r.countertop From 407e5d06e09c18a9956fcc4de42deaaaedd35912 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 19:43:47 -0700 Subject: [PATCH 45/47] more comments --- embassy-nrf/src/pwm.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 8c4a5dae..5063c945 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -11,7 +11,8 @@ use crate::interrupt::Interrupt; use crate::pac; use crate::util::slice_in_ram_or; -/// Interface to the PWM peripheral +/// SimplePwm is the traditional pwm interface you're probably used to, allowing +/// to simply set a duty cycle across up to four channels. pub struct SimplePwm<'d, T: Instance> { phantom: PhantomData<&'d mut T>, duty: [u16; 4], @@ -21,6 +22,8 @@ pub struct SimplePwm<'d, T: Instance> { ch3: Option, } +/// SequencePwm allows you to offloader 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> { phantom: PhantomData<&'d mut T>, ch0: Option, @@ -42,7 +45,7 @@ pub enum Error { } impl<'d, T: Instance> SequencePwm<'d, T> { - /// Creates the interface to a PWM Sequence interface. + /// Creates the interface to a `SequencePwm`. /// /// Must be started by calling `start` /// @@ -329,7 +332,7 @@ pub enum CounterMode { } impl<'d, T: Instance> SimplePwm<'d, T> { - /// Creates the interface to a PWM instance. + /// Creates the interface to a `SimplePwm` /// /// Defaults the freq to 1Mhz, max_duty 1000, duty 0, up mode, and pins low. /// Must be started by calling `set_duty` From 66a43c2e580880964bb7c518ee898fc76ff00ce8 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 19:44:57 -0700 Subject: [PATCH 46/47] more comments --- embassy-nrf/src/pwm.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 5063c945..ea629c54 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -22,8 +22,8 @@ pub struct SimplePwm<'d, T: Instance> { ch3: Option, } -/// SequencePwm allows you to offloader the updating of a sequence of duty -/// cycles to up to four channels, as well as repeat that sequence n times. +/// 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> { phantom: PhantomData<&'d mut T>, ch0: Option, From 156caa9330f4ce8bf9af6b90655e8dbbac6ad74a Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Wed, 10 Nov 2021 19:56:36 -0700 Subject: [PATCH 47/47] more comments --- embassy-nrf/src/pwm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index ea629c54..08e9add0 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -203,7 +203,7 @@ impl<'d, T: Instance> SequencePwm<'d, T> { Ok(()) } - /// Stop playback + /// Stop playback. #[inline(always)] pub fn stop(&self) { let r = T::regs();