From eb3b96b28398086079a8bfe80dee4345ab75896c Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 14 Nov 2022 08:23:38 +0100 Subject: [PATCH] Add is_done and try_wait methods to i2s::Transfer --- nrf-hal-common/src/i2s.rs | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/nrf-hal-common/src/i2s.rs b/nrf-hal-common/src/i2s.rs index 31b7bd51..b3ed8d1a 100644 --- a/nrf-hal-common/src/i2s.rs +++ b/nrf-hal-common/src/i2s.rs @@ -680,15 +680,40 @@ struct Inner { } impl Transfer { + /// Returns `true` if the transfer is done. + pub fn is_done(&self) -> bool { + if let Some(inner) = self + .inner + .as_ref() + { + inner.i2s.is_event_triggered(I2SEvent::RxPtrUpdated) + || inner.i2s.is_event_triggered(I2SEvent::TxPtrUpdated) + } else { + unsafe { core::hint::unreachable_unchecked() }; + } + } + + /// Attempts to return the buffer if the transfer is done. + pub fn try_wait(mut self) -> Option<(B, I2S)> { + if self.is_done() { + let inner = self + .inner + .take() + .unwrap_or_else(|| unsafe { core::hint::unreachable_unchecked() }); + compiler_fence(Ordering::Acquire); + Some((inner.buffer, inner.i2s)) + } else { + None + } + } + /// Blocks until the transfer is done and returns the buffer. pub fn wait(mut self) -> (B, I2S) { + while !self.is_done() {} let inner = self .inner .take() .unwrap_or_else(|| unsafe { core::hint::unreachable_unchecked() }); - while !(inner.i2s.is_event_triggered(I2SEvent::RxPtrUpdated) - || inner.i2s.is_event_triggered(I2SEvent::TxPtrUpdated)) - {} compiler_fence(Ordering::Acquire); (inner.buffer, inner.i2s) }