@@ -40,14 +40,16 @@ pub(crate) struct State {
4040 rx_started_count : AtomicU8 ,
4141 rx_ended_count : AtomicU8 ,
4242 rx_ppi_ch : AtomicU8 ,
43+ rx_overrun : AtomicBool ,
4344}
4445
4546/// UART error.
4647#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
4748#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
4849#[ non_exhaustive]
4950pub enum Error {
50- // No errors for now
51+ /// Buffer Overrun
52+ Overrun ,
5153}
5254
5355impl State {
@@ -61,6 +63,7 @@ impl State {
6163 rx_started_count : AtomicU8 :: new ( 0 ) ,
6264 rx_ended_count : AtomicU8 :: new ( 0 ) ,
6365 rx_ppi_ch : AtomicU8 :: new ( 0 ) ,
66+ rx_overrun : AtomicBool :: new ( false ) ,
6467 }
6568 }
6669}
@@ -87,8 +90,8 @@ impl<U: UarteInstance> interrupt::typelevel::Handler<U::Interrupt> for Interrupt
8790 r. errorsrc ( ) . write_value ( errs) ;
8891
8992 if errs. overrun ( ) {
90- # [ cfg ( feature = "defmt" ) ]
91- defmt :: warn! ( "BufferedUarte overrun" ) ;
93+ s . rx_overrun . store ( true , Ordering :: Relaxed ) ;
94+ ss . rx_waker . wake ( ) ;
9295 }
9396 }
9497
@@ -690,6 +693,7 @@ impl<'d> BufferedUarteRx<'d> {
690693 buffered_state. rx_started_count . store ( 0 , Ordering :: Relaxed ) ;
691694 buffered_state. rx_ended_count . store ( 0 , Ordering :: Relaxed ) ;
692695 buffered_state. rx_started . store ( false , Ordering :: Relaxed ) ;
696+ buffered_state. rx_overrun . store ( false , Ordering :: Relaxed ) ;
693697 let rx_len = rx_buffer. len ( ) . min ( EASY_DMA_SIZE * 2 ) ;
694698 unsafe { buffered_state. rx_buf . init ( rx_buffer. as_mut_ptr ( ) , rx_len) } ;
695699
@@ -763,6 +767,11 @@ impl<'d> BufferedUarteRx<'d> {
763767 compiler_fence ( Ordering :: SeqCst ) ;
764768 //trace!("poll_read");
765769
770+ if s. rx_overrun . load ( Ordering :: Relaxed ) {
771+ s. rx_overrun . store ( false , Ordering :: Relaxed ) ;
772+ return Poll :: Ready ( Err ( Error :: Overrun ) ) ;
773+ }
774+
766775 // Read the RXDRDY counter.
767776 timer. cc ( 0 ) . capture ( ) ;
768777 let mut end = timer. cc ( 0 ) . read ( ) as usize ;
@@ -821,6 +830,10 @@ impl<'d> BufferedUarteRx<'d> {
821830 /// we are ready to read if there is data in the buffer
822831 fn read_ready ( & self ) -> Result < bool , Error > {
823832 let state = self . buffered_state ;
833+ if state. rx_overrun . load ( Ordering :: Relaxed ) {
834+ state. rx_overrun . store ( false , Ordering :: Relaxed ) ;
835+ return Err ( Error :: Overrun ) ;
836+ }
824837 Ok ( !state. rx_buf . is_empty ( ) )
825838 }
826839}
@@ -855,7 +868,9 @@ mod _embedded_io {
855868
856869 impl embedded_io_async:: Error for Error {
857870 fn kind ( & self ) -> embedded_io_async:: ErrorKind {
858- match * self { }
871+ match * self {
872+ Error :: Overrun => embedded_io_async:: ErrorKind :: OutOfMemory ,
873+ }
859874 }
860875 }
861876
0 commit comments