aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf/src/buffered_uarte.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2020-12-29 01:53:17 +0100
committerDario Nieuwenhuis <[email protected]>2020-12-29 01:53:17 +0100
commitaf5454fbfec6232074c79ef571b2135dc7253d45 (patch)
tree2dd99c7e6ca0ae1b0c7634d0bb983c862dcbdad5 /embassy-nrf/src/buffered_uarte.rs
parent4b8d8ba87ee26173b0a7743c606c76df2d171790 (diff)
Update drivers to owned irqs.
Diffstat (limited to 'embassy-nrf/src/buffered_uarte.rs')
-rw-r--r--embassy-nrf/src/buffered_uarte.rs50
1 files changed, 23 insertions, 27 deletions
diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs
index 9e138ad4f..db6a83fb4 100644
--- a/embassy-nrf/src/buffered_uarte.rs
+++ b/embassy-nrf/src/buffered_uarte.rs
@@ -17,10 +17,10 @@ use embedded_hal::digital::v2::OutputPin;
17 17
18use crate::hal::gpio::{Floating, Input, Output, Pin as GpioPin, Port as GpioPort, PushPull}; 18use crate::hal::gpio::{Floating, Input, Output, Pin as GpioPin, Port as GpioPort, PushPull};
19use crate::interrupt; 19use crate::interrupt;
20use crate::interrupt::CriticalSection; 20use crate::interrupt::{CriticalSection, OwnedInterrupt};
21#[cfg(any(feature = "52833", feature = "52840", feature = "9160"))] 21#[cfg(any(feature = "52833", feature = "52840", feature = "9160"))]
22use crate::pac::UARTE1; 22use crate::pac::UARTE1;
23use crate::pac::{uarte0, Interrupt, UARTE0}; 23use crate::pac::{uarte0, UARTE0};
24 24
25// Re-export SVD variants to allow user to directly set values 25// Re-export SVD variants to allow user to directly set values
26pub use uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity}; 26pub use uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity};
@@ -141,8 +141,9 @@ pub struct BufferedUarte<T: Instance> {
141// public because it needs to be used in Instance::{get_state, set_state}, but 141// public because it needs to be used in Instance::{get_state, set_state}, but
142// should not be used outside the module 142// should not be used outside the module
143#[doc(hidden)] 143#[doc(hidden)]
144pub struct UarteState<T> { 144pub struct UarteState<T: Instance> {
145 inner: T, 145 inner: T,
146 irq: T::Interrupt,
146 147
147 rx: RingBuf, 148 rx: RingBuf,
148 rx_state: RxState, 149 rx_state: RxState,
@@ -164,7 +165,13 @@ fn port_bit(port: GpioPort) -> bool {
164} 165}
165 166
166impl<T: Instance> BufferedUarte<T> { 167impl<T: Instance> BufferedUarte<T> {
167 pub fn new(uarte: T, mut pins: Pins, parity: Parity, baudrate: Baudrate) -> Self { 168 pub fn new(
169 uarte: T,
170 irq: T::Interrupt,
171 mut pins: Pins,
172 parity: Parity,
173 baudrate: Baudrate,
174 ) -> Self {
168 // Select pins 175 // Select pins
169 uarte.psel.rxd.write(|w| { 176 uarte.psel.rxd.write(|w| {
170 let w = unsafe { w.pin().bits(pins.rxd.pin()) }; 177 let w = unsafe { w.pin().bits(pins.rxd.pin()) };
@@ -222,6 +229,7 @@ impl<T: Instance> BufferedUarte<T> {
222 started: false, 229 started: false,
223 state: UnsafeCell::new(UarteState { 230 state: UnsafeCell::new(UarteState {
224 inner: uarte, 231 inner: uarte,
232 irq,
225 233
226 rx: RingBuf::new(), 234 rx: RingBuf::new(),
227 rx_state: RxState::Idle, 235 rx_state: RxState::Idle,
@@ -287,9 +295,12 @@ impl<T: Instance> AsyncWrite for BufferedUarte<T> {
287 295
288impl<T: Instance> UarteState<T> { 296impl<T: Instance> UarteState<T> {
289 pub fn start(self: Pin<&mut Self>) { 297 pub fn start(self: Pin<&mut Self>) {
290 interrupt::set_priority(T::interrupt(), interrupt::Priority::Level7); 298 self.irq.set_handler(|| unsafe {
291 interrupt::enable(T::interrupt()); 299 interrupt::free(|cs| T::get_state(cs).as_mut().unwrap().on_interrupt());
292 interrupt::pend(T::interrupt()); 300 });
301
302 self.irq.pend();
303 self.irq.enable();
293 } 304 }
294 305
295 fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> { 306 fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> {
@@ -324,7 +335,7 @@ impl<T: Instance> UarteState<T> {
324 let this = unsafe { self.get_unchecked_mut() }; 335 let this = unsafe { self.get_unchecked_mut() };
325 trace!("consume {:?}", amt); 336 trace!("consume {:?}", amt);
326 this.rx.pop(amt); 337 this.rx.pop(amt);
327 interrupt::pend(T::interrupt()); 338 this.irq.pend();
328 } 339 }
329 340
330 fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> { 341 fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> {
@@ -350,7 +361,7 @@ impl<T: Instance> UarteState<T> {
350 // before any DMA action has started 361 // before any DMA action has started
351 compiler_fence(Ordering::SeqCst); 362 compiler_fence(Ordering::SeqCst);
352 363
353 interrupt::pend(T::interrupt()); 364 this.irq.pend();
354 365
355 Poll::Ready(Ok(n)) 366 Poll::Ready(Ok(n))
356 } 367 }
@@ -509,7 +520,7 @@ mod private {
509} 520}
510 521
511pub trait Instance: Deref<Target = uarte0::RegisterBlock> + Sized + private::Sealed { 522pub trait Instance: Deref<Target = uarte0::RegisterBlock> + Sized + private::Sealed {
512 fn interrupt() -> Interrupt; 523 type Interrupt: OwnedInterrupt;
513 524
514 #[doc(hidden)] 525 #[doc(hidden)]
515 fn get_state(_cs: &CriticalSection) -> *mut UarteState<Self>; 526 fn get_state(_cs: &CriticalSection) -> *mut UarteState<Self>;
@@ -518,25 +529,12 @@ pub trait Instance: Deref<Target = uarte0::RegisterBlock> + Sized + private::Sea
518 fn set_state(_cs: &CriticalSection, state: *mut UarteState<Self>); 529 fn set_state(_cs: &CriticalSection, state: *mut UarteState<Self>);
519} 530}
520 531
521#[interrupt]
522unsafe fn UARTE0_UART0() {
523 interrupt::free(|cs| UARTE0::get_state(cs).as_mut().unwrap().on_interrupt());
524}
525
526#[cfg(any(feature = "52833", feature = "52840", feature = "9160"))]
527#[interrupt]
528unsafe fn UARTE1() {
529 interrupt::free(|cs| UARTE1::get_state(cs).as_mut().unwrap().on_interrupt());
530}
531
532static mut UARTE0_STATE: *mut UarteState<UARTE0> = ptr::null_mut(); 532static mut UARTE0_STATE: *mut UarteState<UARTE0> = ptr::null_mut();
533#[cfg(any(feature = "52833", feature = "52840", feature = "9160"))] 533#[cfg(any(feature = "52833", feature = "52840", feature = "9160"))]
534static mut UARTE1_STATE: *mut UarteState<UARTE1> = ptr::null_mut(); 534static mut UARTE1_STATE: *mut UarteState<UARTE1> = ptr::null_mut();
535 535
536impl Instance for UARTE0 { 536impl Instance for UARTE0 {
537 fn interrupt() -> Interrupt { 537 type Interrupt = interrupt::UARTE0_UART0Interrupt;
538 Interrupt::UARTE0_UART0
539 }
540 538
541 fn get_state(_cs: &CriticalSection) -> *mut UarteState<Self> { 539 fn get_state(_cs: &CriticalSection) -> *mut UarteState<Self> {
542 unsafe { UARTE0_STATE } // Safe because of CriticalSection 540 unsafe { UARTE0_STATE } // Safe because of CriticalSection
@@ -548,9 +546,7 @@ impl Instance for UARTE0 {
548 546
549#[cfg(any(feature = "52833", feature = "52840", feature = "9160"))] 547#[cfg(any(feature = "52833", feature = "52840", feature = "9160"))]
550impl Instance for UARTE1 { 548impl Instance for UARTE1 {
551 fn interrupt() -> Interrupt { 549 type Interrupt = interrupt::UARTE1Interrupt;
552 Interrupt::UARTE1
553 }
554 550
555 fn get_state(_cs: &CriticalSection) -> *mut UarteState<Self> { 551 fn get_state(_cs: &CriticalSection) -> *mut UarteState<Self> {
556 unsafe { UARTE1_STATE } // Safe because of CriticalSection 552 unsafe { UARTE1_STATE } // Safe because of CriticalSection