diff options
| author | huntc <[email protected]> | 2022-08-26 14:40:20 +1000 |
|---|---|---|
| committer | huntc <[email protected]> | 2022-08-26 14:40:20 +1000 |
| commit | 9a873d1dbf301bf6416fec675a40ebd3aa3f1174 (patch) | |
| tree | 00ce4799c4a8b193d674ab14f152e81e8df14579 /embassy-nrf/src/saadc.rs | |
| parent | 1125d578428dd0af3274142edbac46ae90042a0e (diff) | |
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.
Diffstat (limited to 'embassy-nrf/src/saadc.rs')
| -rw-r--r-- | embassy-nrf/src/saadc.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/embassy-nrf/src/saadc.rs b/embassy-nrf/src/saadc.rs index 7dc66349e..43c98a888 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> { | |||
| 183 | r.intenclr.write(|w| w.started().clear()); | 183 | r.intenclr.write(|w| w.started().clear()); |
| 184 | WAKER.wake(); | 184 | WAKER.wake(); |
| 185 | } | 185 | } |
| 186 | |||
| 187 | if r.events_stopped.read().bits() != 0 { | ||
| 188 | r.intenclr.write(|w| w.stopped().clear()); | ||
| 189 | WAKER.wake(); | ||
| 190 | } | ||
| 186 | } | 191 | } |
| 187 | 192 | ||
| 188 | fn regs() -> &'static saadc::RegisterBlock { | 193 | fn regs() -> &'static saadc::RegisterBlock { |
| @@ -251,6 +256,8 @@ impl<'d, const N: usize> Saadc<'d, N> { | |||
| 251 | Poll::Pending | 256 | Poll::Pending |
| 252 | }) | 257 | }) |
| 253 | .await; | 258 | .await; |
| 259 | |||
| 260 | Self::stop_sampling().await; | ||
| 254 | } | 261 | } |
| 255 | 262 | ||
| 256 | /// Continuous sampling with double buffers. | 263 | /// Continuous sampling with double buffers. |
| @@ -403,6 +410,42 @@ impl<'d, const N: usize> Saadc<'d, N> { | |||
| 403 | Poll::Pending | 410 | Poll::Pending |
| 404 | }) | 411 | }) |
| 405 | .await; | 412 | .await; |
| 413 | |||
| 414 | Self::stop_sampling().await; | ||
| 415 | } | ||
| 416 | |||
| 417 | // Stop sampling and wait for it to stop | ||
| 418 | async fn stop_sampling() { | ||
| 419 | let r = Self::regs(); | ||
| 420 | |||
| 421 | // Reset and enable the events | ||
| 422 | |||
| 423 | compiler_fence(Ordering::SeqCst); | ||
| 424 | |||
| 425 | r.events_stopped.reset(); | ||
| 426 | r.intenset.write(|w| { | ||
| 427 | w.stopped().set(); | ||
| 428 | w | ||
| 429 | }); | ||
| 430 | |||
| 431 | // Stop | ||
| 432 | |||
| 433 | r.tasks_stop.write(|w| unsafe { w.bits(1) }); | ||
| 434 | |||
| 435 | // Wait for 'stopped' event. | ||
| 436 | poll_fn(|cx| { | ||
| 437 | let r = Self::regs(); | ||
| 438 | |||
| 439 | WAKER.register(cx.waker()); | ||
| 440 | |||
| 441 | if r.events_stopped.read().bits() != 0 { | ||
| 442 | r.events_stopped.reset(); | ||
| 443 | return Poll::Ready(()); | ||
| 444 | } | ||
| 445 | |||
| 446 | Poll::Pending | ||
| 447 | }) | ||
| 448 | .await; | ||
| 406 | } | 449 | } |
| 407 | } | 450 | } |
| 408 | 451 | ||
