From 9a873d1dbf301bf6416fec675a40ebd3aa3f1174 Mon Sep 17 00:00:00 2001 From: huntc Date: Fri, 26 Aug 2022 14:40:20 +1000 Subject: [PATCH] Ensure that the sampling is stopped Ensures that nRF saadc sampling is stopped and is awaited prior to exiting the two sampling methods. Not doing so causes a potential power drain and the potential for dropped buffer writes when having finished continuous sampling. --- embassy-nrf/src/saadc.rs | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/embassy-nrf/src/saadc.rs b/embassy-nrf/src/saadc.rs index 7dc66349..43c98a88 100644 --- a/embassy-nrf/src/saadc.rs +++ b/embassy-nrf/src/saadc.rs @@ -183,6 +183,11 @@ impl<'d, const N: usize> Saadc<'d, N> { r.intenclr.write(|w| w.started().clear()); WAKER.wake(); } + + if r.events_stopped.read().bits() != 0 { + r.intenclr.write(|w| w.stopped().clear()); + WAKER.wake(); + } } fn regs() -> &'static saadc::RegisterBlock { @@ -251,6 +256,8 @@ impl<'d, const N: usize> Saadc<'d, N> { Poll::Pending }) .await; + + Self::stop_sampling().await; } /// Continuous sampling with double buffers. @@ -403,6 +410,42 @@ impl<'d, const N: usize> Saadc<'d, N> { Poll::Pending }) .await; + + Self::stop_sampling().await; + } + + // Stop sampling and wait for it to stop + async fn stop_sampling() { + let r = Self::regs(); + + // Reset and enable the events + + compiler_fence(Ordering::SeqCst); + + r.events_stopped.reset(); + r.intenset.write(|w| { + w.stopped().set(); + w + }); + + // Stop + + r.tasks_stop.write(|w| unsafe { w.bits(1) }); + + // Wait for 'stopped' event. + poll_fn(|cx| { + let r = Self::regs(); + + WAKER.register(cx.waker()); + + if r.events_stopped.read().bits() != 0 { + r.events_stopped.reset(); + return Poll::Ready(()); + } + + Poll::Pending + }) + .await; } }