From 9f28c7ab8d96d19a010246318dee1de73b3ed4ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Kr=C3=B6ger?= Date: Sat, 2 Jan 2021 22:40:36 +0100 Subject: [PATCH] uarte: Do not spin when stopping a receive future Spinning on drop() is still required when the future has not been stopped so that DMA finishes before the buffer is released. --- embassy-nrf/src/uarte.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/embassy-nrf/src/uarte.rs b/embassy-nrf/src/uarte.rs index 648298b8..494b92df 100644 --- a/embassy-nrf/src/uarte.rs +++ b/embassy-nrf/src/uarte.rs @@ -313,7 +313,7 @@ where { fn drop(self: &mut Self) { if self.uarte.rx_started() { - trace!("stoprx"); + trace!("stoprx (drop)"); self.uarte.instance.events_rxstarted.reset(); self.uarte @@ -364,9 +364,20 @@ where T: Instance, { /// Stops the ongoing reception and returns the number of bytes received. - pub async fn stop(self) -> usize { - drop(self); - let len = T::state().rx_done.wait().await; + pub async fn stop(mut self) -> usize { + let len = if self.uarte.rx_started() { + trace!("stoprx (stop)"); + + self.uarte.instance.events_rxstarted.reset(); + self.uarte + .instance + .tasks_stoprx + .write(|w| unsafe { w.bits(1) }); + T::state().rx_done.wait().await + } else { + // Transfer was stopped before it even started. No bytes were sent. + 0 + }; len as _ } }