diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-01-11 10:39:59 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-01-11 10:39:59 +0100 |
| commit | 2616467377eaafeafdb535174a829ad156fa3098 (patch) | |
| tree | 5a3466e39c39a6fb08a5a72caf2868ba37404870 /embassy-nrf/src | |
| parent | c91882a72c7bab61749b234c0e523fdbaee2c36b (diff) | |
nrf/buffered_uarte: stop on drop, add free()
Diffstat (limited to 'embassy-nrf/src')
| -rw-r--r-- | embassy-nrf/src/buffered_uarte.rs | 43 | ||||
| -rw-r--r-- | embassy-nrf/src/util/peripheral.rs | 15 |
2 files changed, 50 insertions, 8 deletions
diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs index 640ec6ad9..a6e80a9ad 100644 --- a/embassy-nrf/src/buffered_uarte.rs +++ b/embassy-nrf/src/buffered_uarte.rs | |||
| @@ -14,13 +14,16 @@ use embassy::io::{AsyncBufRead, AsyncWrite, Result}; | |||
| 14 | use embassy::util::WakerRegistration; | 14 | use embassy::util::WakerRegistration; |
| 15 | use embedded_hal::digital::v2::OutputPin; | 15 | use embedded_hal::digital::v2::OutputPin; |
| 16 | 16 | ||
| 17 | use crate::fmt::{panic, todo, *}; | ||
| 18 | use crate::hal::gpio::Port as GpioPort; | 17 | use crate::hal::gpio::Port as GpioPort; |
| 19 | use crate::hal::ppi::ConfigurablePpi; | 18 | use crate::hal::ppi::ConfigurablePpi; |
| 20 | use crate::interrupt::{self, OwnedInterrupt}; | 19 | use crate::interrupt::{self, OwnedInterrupt}; |
| 21 | use crate::pac; | 20 | use crate::pac; |
| 22 | use crate::util::peripheral::{PeripheralMutex, PeripheralState}; | 21 | use crate::util::peripheral::{PeripheralMutex, PeripheralState}; |
| 23 | use crate::util::ring_buffer::RingBuffer; | 22 | use crate::util::ring_buffer::RingBuffer; |
| 23 | use crate::{ | ||
| 24 | fmt::{panic, todo, *}, | ||
| 25 | util::low_power_wait_until, | ||
| 26 | }; | ||
| 24 | 27 | ||
| 25 | // Re-export SVD variants to allow user to directly set values | 28 | // Re-export SVD variants to allow user to directly set values |
| 26 | pub use crate::hal::uarte::Pins; | 29 | pub use crate::hal::uarte::Pins; |
| @@ -203,14 +206,28 @@ impl<'a, U: Instance, T: TimerInstance, P1: ConfigurablePpi, P2: ConfigurablePpi | |||
| 203 | fn inner(self: Pin<&mut Self>) -> Pin<&mut PeripheralMutex<State<'a, U, T, P1, P2>>> { | 206 | fn inner(self: Pin<&mut Self>) -> Pin<&mut PeripheralMutex<State<'a, U, T, P1, P2>>> { |
| 204 | unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().inner) } | 207 | unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().inner) } |
| 205 | } | 208 | } |
| 209 | |||
| 210 | pub fn free(self: Pin<&mut Self>) -> (U, T, P1, P2, U::Interrupt) { | ||
| 211 | let (mut state, irq) = self.inner().free(); | ||
| 212 | state.stop(); | ||
| 213 | ( | ||
| 214 | state.uarte, | ||
| 215 | state.timer, | ||
| 216 | state.ppi_channel_1, | ||
| 217 | state.ppi_channel_2, | ||
| 218 | irq, | ||
| 219 | ) | ||
| 220 | } | ||
| 206 | } | 221 | } |
| 207 | 222 | ||
| 208 | impl<'a, U: Instance, T: TimerInstance, P1: ConfigurablePpi, P2: ConfigurablePpi> Drop | 223 | impl<'a, U: Instance, T: TimerInstance, P1: ConfigurablePpi, P2: ConfigurablePpi> Drop |
| 209 | for BufferedUarte<'a, U, T, P1, P2> | 224 | for BufferedUarte<'a, U, T, P1, P2> |
| 210 | { | 225 | { |
| 211 | fn drop(&mut self) { | 226 | fn drop(&mut self) { |
| 212 | // stop DMA before dropping, because DMA is using the buffer in `self`. | 227 | let inner = unsafe { Pin::new_unchecked(&mut self.inner) }; |
| 213 | todo!() | 228 | if let Some((mut state, _irq)) = inner.try_free() { |
| 229 | state.stop(); | ||
| 230 | } | ||
| 214 | } | 231 | } |
| 215 | } | 232 | } |
| 216 | 233 | ||
| @@ -281,6 +298,26 @@ impl<'a, U: Instance, T: TimerInstance, P1: ConfigurablePpi, P2: ConfigurablePpi | |||
| 281 | } | 298 | } |
| 282 | } | 299 | } |
| 283 | 300 | ||
| 301 | impl<'a, U: Instance, T: TimerInstance, P1: ConfigurablePpi, P2: ConfigurablePpi> | ||
| 302 | State<'a, U, T, P1, P2> | ||
| 303 | { | ||
| 304 | fn stop(&mut self) { | ||
| 305 | self.timer.tasks_stop.write(|w| unsafe { w.bits(1) }); | ||
| 306 | if let RxState::Receiving = self.rx_state { | ||
| 307 | self.uarte.tasks_stoprx.write(|w| unsafe { w.bits(1) }); | ||
| 308 | } | ||
| 309 | if let TxState::Transmitting(_) = self.tx_state { | ||
| 310 | self.uarte.tasks_stoptx.write(|w| unsafe { w.bits(1) }); | ||
| 311 | } | ||
| 312 | if let RxState::Receiving = self.rx_state { | ||
| 313 | low_power_wait_until(|| self.uarte.events_endrx.read().bits() == 0); | ||
| 314 | } | ||
| 315 | if let TxState::Transmitting(_) = self.tx_state { | ||
| 316 | low_power_wait_until(|| self.uarte.events_endtx.read().bits() == 0); | ||
| 317 | } | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 284 | impl<'a, U: Instance, T: TimerInstance, P1: ConfigurablePpi, P2: ConfigurablePpi> PeripheralState | 321 | impl<'a, U: Instance, T: TimerInstance, P1: ConfigurablePpi, P2: ConfigurablePpi> PeripheralState |
| 285 | for State<'a, U, T, P1, P2> | 322 | for State<'a, U, T, P1, P2> |
| 286 | { | 323 | { |
diff --git a/embassy-nrf/src/util/peripheral.rs b/embassy-nrf/src/util/peripheral.rs index e0edbf2b7..1cc04f4ab 100644 --- a/embassy-nrf/src/util/peripheral.rs +++ b/embassy-nrf/src/util/peripheral.rs | |||
| @@ -52,12 +52,17 @@ impl<S: PeripheralState> PeripheralMutex<S> { | |||
| 52 | r | 52 | r |
| 53 | } | 53 | } |
| 54 | 54 | ||
| 55 | pub fn free(self: Pin<&mut Self>) -> (S, S::Interrupt) { | 55 | pub fn try_free(self: Pin<&mut Self>) -> Option<(S, S::Interrupt)> { |
| 56 | let this = unsafe { self.get_unchecked_mut() }; | 56 | let this = unsafe { self.get_unchecked_mut() }; |
| 57 | let (state, irq) = unwrap!(this.inner.take()); | 57 | this.inner.take().map(|(state, irq)| { |
| 58 | irq.disable(); | 58 | irq.disable(); |
| 59 | irq.remove_handler(); | 59 | irq.remove_handler(); |
| 60 | (state.into_inner(), irq) | 60 | (state.into_inner(), irq) |
| 61 | }) | ||
| 62 | } | ||
| 63 | |||
| 64 | pub fn free(self: Pin<&mut Self>) -> (S, S::Interrupt) { | ||
| 65 | unwrap!(self.try_free()) | ||
| 61 | } | 66 | } |
| 62 | } | 67 | } |
| 63 | 68 | ||
