aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf/src
diff options
context:
space:
mode:
authorKezi <[email protected]>2025-10-10 01:36:09 +0200
committerKezi <[email protected]>2025-10-14 17:54:28 +0200
commitdad58cf915c753602f6c6bcdc4db7123c31b2877 (patch)
treedbd2d2b98183257c732ece46c2cc5be468de43d0 /embassy-nrf/src
parenta83cf2480671cee67e8edaa27565203aaaf6d8bc (diff)
return error on read when uarte buffer overrun
Diffstat (limited to 'embassy-nrf/src')
-rw-r--r--embassy-nrf/src/buffered_uarte.rs21
1 files changed, 17 insertions, 4 deletions
diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs
index ec104788f..b1eb5c81a 100644
--- a/embassy-nrf/src/buffered_uarte.rs
+++ b/embassy-nrf/src/buffered_uarte.rs
@@ -40,6 +40,7 @@ pub(crate) struct State {
40 rx_started_count: AtomicU8, 40 rx_started_count: AtomicU8,
41 rx_ended_count: AtomicU8, 41 rx_ended_count: AtomicU8,
42 rx_ppi_ch: AtomicU8, 42 rx_ppi_ch: AtomicU8,
43 rx_overrun: AtomicBool,
43} 44}
44 45
45/// UART error. 46/// UART error.
@@ -47,7 +48,8 @@ pub(crate) struct State {
47#[cfg_attr(feature = "defmt", derive(defmt::Format))] 48#[cfg_attr(feature = "defmt", derive(defmt::Format))]
48#[non_exhaustive] 49#[non_exhaustive]
49pub enum Error { 50pub enum Error {
50 // No errors for now 51 /// Buffer Overrun
52 Overrun,
51} 53}
52 54
53impl State { 55impl State {
@@ -61,6 +63,7 @@ impl State {
61 rx_started_count: AtomicU8::new(0), 63 rx_started_count: AtomicU8::new(0),
62 rx_ended_count: AtomicU8::new(0), 64 rx_ended_count: AtomicU8::new(0),
63 rx_ppi_ch: AtomicU8::new(0), 65 rx_ppi_ch: AtomicU8::new(0),
66 rx_overrun: AtomicBool::new(false),
64 } 67 }
65 } 68 }
66} 69}
@@ -87,8 +90,8 @@ impl<U: UarteInstance> interrupt::typelevel::Handler<U::Interrupt> for Interrupt
87 r.errorsrc().write_value(errs); 90 r.errorsrc().write_value(errs);
88 91
89 if errs.overrun() { 92 if errs.overrun() {
90 #[cfg(feature = "defmt")] 93 s.rx_overrun.store(true, Ordering::Release);
91 defmt::warn!("BufferedUarte overrun"); 94 ss.rx_waker.wake();
92 } 95 }
93 } 96 }
94 97
@@ -690,6 +693,7 @@ impl<'d> BufferedUarteRx<'d> {
690 buffered_state.rx_started_count.store(0, Ordering::Relaxed); 693 buffered_state.rx_started_count.store(0, Ordering::Relaxed);
691 buffered_state.rx_ended_count.store(0, Ordering::Relaxed); 694 buffered_state.rx_ended_count.store(0, Ordering::Relaxed);
692 buffered_state.rx_started.store(false, Ordering::Relaxed); 695 buffered_state.rx_started.store(false, Ordering::Relaxed);
696 buffered_state.rx_overrun.store(false, Ordering::Relaxed);
693 let rx_len = rx_buffer.len().min(EASY_DMA_SIZE * 2); 697 let rx_len = rx_buffer.len().min(EASY_DMA_SIZE * 2);
694 unsafe { buffered_state.rx_buf.init(rx_buffer.as_mut_ptr(), rx_len) }; 698 unsafe { buffered_state.rx_buf.init(rx_buffer.as_mut_ptr(), rx_len) };
695 699
@@ -763,6 +767,10 @@ impl<'d> BufferedUarteRx<'d> {
763 compiler_fence(Ordering::SeqCst); 767 compiler_fence(Ordering::SeqCst);
764 //trace!("poll_read"); 768 //trace!("poll_read");
765 769
770 if s.rx_overrun.swap(false, Ordering::Acquire) {
771 return Poll::Ready(Err(Error::Overrun));
772 }
773
766 // Read the RXDRDY counter. 774 // Read the RXDRDY counter.
767 timer.cc(0).capture(); 775 timer.cc(0).capture();
768 let mut end = timer.cc(0).read() as usize; 776 let mut end = timer.cc(0).read() as usize;
@@ -821,6 +829,9 @@ impl<'d> BufferedUarteRx<'d> {
821 /// we are ready to read if there is data in the buffer 829 /// we are ready to read if there is data in the buffer
822 fn read_ready(&self) -> Result<bool, Error> { 830 fn read_ready(&self) -> Result<bool, Error> {
823 let state = self.buffered_state; 831 let state = self.buffered_state;
832 if state.rx_overrun.swap(false, Ordering::Acquire) {
833 return Err(Error::Overrun);
834 }
824 Ok(!state.rx_buf.is_empty()) 835 Ok(!state.rx_buf.is_empty())
825 } 836 }
826} 837}
@@ -855,7 +866,9 @@ mod _embedded_io {
855 866
856 impl embedded_io_async::Error for Error { 867 impl embedded_io_async::Error for Error {
857 fn kind(&self) -> embedded_io_async::ErrorKind { 868 fn kind(&self) -> embedded_io_async::ErrorKind {
858 match *self {} 869 match *self {
870 Error::Overrun => embedded_io_async::ErrorKind::OutOfMemory,
871 }
859 } 872 }
860 } 873 }
861 874