From b08a0955c3d6e66c0163508e86f9dc7e40e142d3 Mon Sep 17 00:00:00 2001 From: Martin Marmsoler Date: Sat, 27 Jan 2024 18:19:56 +0100 Subject: [PATCH 1/2] implement retriving duty. Description: When disabling the pwm and enabling again, it is required to restart the sequence. If the previous duty is not known, it is not possible to turn on the pwm again --- embassy-nrf/src/pwm.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 856e12024..90aeb09c0 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -736,6 +736,11 @@ impl<'d, T: Instance> SimplePwm<'d, T> { r.enable.write(|w| w.enable().disabled()); } + /// Returns the current duty of the channel + pub fn duty(&self, channel: usize) -> u16 { + self.duty[channel] + } + /// Sets duty cycle (15 bit) for a PWM channel. pub fn set_duty(&mut self, channel: usize, duty: u16) { let r = T::regs(); From 319b0fe3d78c60aa123a8cbdfac850d61f1478b0 Mon Sep 17 00:00:00 2001 From: Martin Marmsoler Date: Sat, 27 Jan 2024 18:20:53 +0100 Subject: [PATCH 2/2] Do not wait when the pwm is disabled Reason: because in this case no seqend event is raised and therefore an infinity loop occurs --- embassy-nrf/src/pwm.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 90aeb09c0..e0583b770 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs @@ -760,7 +760,9 @@ impl<'d, T: Instance> SimplePwm<'d, T> { // defensive wait until waveform is loaded after seqstart so set_duty // can't be called again while dma is still reading - while r.events_seqend[0].read().bits() == 0 {} + if self.is_enabled() { + while r.events_seqend[0].read().bits() == 0 {} + } } /// Sets the PWM clock prescaler.