@@ -64,55 +64,6 @@ pub struct NoInterruptIdWasSet;
64
64
#[ error( "transer is pending" ) ]
65
65
pub struct TransferPendingError ;
66
66
67
- #[ derive( Debug , PartialEq , Eq , thiserror:: Error ) ]
68
- #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
69
- pub enum RxError {
70
- #[ error( "overrun error" ) ]
71
- Overrun ,
72
- #[ error( "framing error" ) ]
73
- Framing ,
74
- #[ error( "parity error" ) ]
75
- Parity ,
76
- }
77
- #[ derive( Debug , PartialEq , Eq , thiserror:: Error ) ]
78
- #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
79
- pub enum Error {
80
- #[ error( "rx error: {0}" ) ]
81
- Rx ( #[ from] RxError ) ,
82
- #[ error( "break condition" ) ]
83
- BreakCondition ,
84
- }
85
-
86
- impl embedded_io:: Error for Error {
87
- fn kind ( & self ) -> embedded_io:: ErrorKind {
88
- embedded_io:: ErrorKind :: Other
89
- }
90
- }
91
-
92
- impl embedded_io:: Error for RxError {
93
- fn kind ( & self ) -> embedded_io:: ErrorKind {
94
- embedded_io:: ErrorKind :: Other
95
- }
96
- }
97
- impl embedded_hal_nb:: serial:: Error for RxError {
98
- fn kind ( & self ) -> embedded_hal_nb:: serial:: ErrorKind {
99
- match self {
100
- RxError :: Overrun => embedded_hal_nb:: serial:: ErrorKind :: Overrun ,
101
- RxError :: Framing => embedded_hal_nb:: serial:: ErrorKind :: FrameFormat ,
102
- RxError :: Parity => embedded_hal_nb:: serial:: ErrorKind :: Parity ,
103
- }
104
- }
105
- }
106
-
107
- impl embedded_hal_nb:: serial:: Error for Error {
108
- fn kind ( & self ) -> embedded_hal_nb:: serial:: ErrorKind {
109
- match self {
110
- Error :: Rx ( rx_error) => embedded_hal_nb:: serial:: Error :: kind ( rx_error) ,
111
- Error :: BreakCondition => embedded_hal_nb:: serial:: ErrorKind :: Other ,
112
- }
113
- }
114
- }
115
-
116
67
#[ derive( Debug , PartialEq , Eq , Copy , Clone ) ]
117
68
#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
118
69
pub enum Event {
@@ -363,7 +314,7 @@ impl UartErrors {
363
314
impl UartErrors {
364
315
#[ inline( always) ]
365
316
pub fn error ( & self ) -> bool {
366
- self . overflow || self . framing || self . parity
317
+ self . overflow || self . framing || self . parity || self . other
367
318
}
368
319
}
369
320
@@ -585,22 +536,27 @@ impl<Uart: Instance> UartBase<Uart> {
585
536
self . uart
586
537
}
587
538
539
+ /// Poll receiver errors.
540
+ pub fn poll_rx_errors ( & self ) -> Option < UartErrors > {
541
+ self . rx . poll_errors ( )
542
+ }
543
+
588
544
pub fn split ( self ) -> ( Tx < Uart > , Rx < Uart > ) {
589
545
( self . tx , self . rx )
590
546
}
591
547
}
592
548
593
549
impl < UartInstance > embedded_io:: ErrorType for UartBase < UartInstance > {
594
- type Error = Error ;
550
+ type Error = Infallible ;
595
551
}
596
552
597
553
impl < UartInstance > embedded_hal_nb:: serial:: ErrorType for UartBase < UartInstance > {
598
- type Error = Error ;
554
+ type Error = Infallible ;
599
555
}
600
556
601
557
impl < Uart : Instance > embedded_hal_nb:: serial:: Read < u8 > for UartBase < Uart > {
602
558
fn read ( & mut self ) -> nb:: Result < u8 , Self :: Error > {
603
- self . rx . read ( ) . map_err ( |e| e . map ( Error :: Rx ) )
559
+ self . rx . read ( )
604
560
}
605
561
}
606
562
@@ -711,6 +667,11 @@ where
711
667
self
712
668
}
713
669
670
+ /// Poll receiver errors.
671
+ pub fn poll_rx_errors ( & self ) -> Option < UartErrors > {
672
+ self . inner . poll_rx_errors ( )
673
+ }
674
+
714
675
#[ inline]
715
676
pub fn enable_rx ( & mut self ) {
716
677
self . inner . enable_rx ( ) ;
@@ -825,6 +786,23 @@ impl<Uart: Instance> Rx<Uart> {
825
786
& self . 0
826
787
}
827
788
789
+ pub fn poll_errors ( & self ) -> Option < UartErrors > {
790
+ let mut errors = UartErrors :: default ( ) ;
791
+
792
+ let uart = unsafe { & ( * Uart :: ptr ( ) ) } ;
793
+ let status_reader = uart. rxstatus ( ) . read ( ) ;
794
+ if status_reader. rxovr ( ) . bit_is_set ( ) {
795
+ errors. overflow = true ;
796
+ } else if status_reader. rxfrm ( ) . bit_is_set ( ) {
797
+ errors. framing = true ;
798
+ } else if status_reader. rxpar ( ) . bit_is_set ( ) {
799
+ errors. parity = true ;
800
+ } else {
801
+ return None ;
802
+ } ;
803
+ Some ( errors)
804
+ }
805
+
828
806
#[ inline]
829
807
pub fn clear_fifo ( & self ) {
830
808
self . 0 . fifo_clr ( ) . write ( |w| w. rxfifo ( ) . set_bit ( ) ) ;
@@ -887,34 +865,15 @@ impl<Uart: Instance> Rx<Uart> {
887
865
}
888
866
889
867
impl < Uart > embedded_io:: ErrorType for Rx < Uart > {
890
- type Error = RxError ;
868
+ type Error = Infallible ;
891
869
}
892
870
893
871
impl < Uart > embedded_hal_nb:: serial:: ErrorType for Rx < Uart > {
894
- type Error = RxError ;
872
+ type Error = Infallible ;
895
873
}
896
874
897
875
impl < Uart : Instance > embedded_hal_nb:: serial:: Read < u8 > for Rx < Uart > {
898
876
fn read ( & mut self ) -> nb:: Result < u8 , Self :: Error > {
899
- let uart = unsafe { & ( * Uart :: ptr ( ) ) } ;
900
- let status_reader = uart. rxstatus ( ) . read ( ) ;
901
- let err = if status_reader. rxovr ( ) . bit_is_set ( ) {
902
- Some ( RxError :: Overrun )
903
- } else if status_reader. rxfrm ( ) . bit_is_set ( ) {
904
- Some ( RxError :: Framing )
905
- } else if status_reader. rxpar ( ) . bit_is_set ( ) {
906
- Some ( RxError :: Parity )
907
- } else {
908
- None
909
- } ;
910
- if let Some ( err) = err {
911
- // The status code is always related to the next bit for the framing
912
- // and parity status bits. We have to read the DATA register
913
- // so that the next status reflects the next DATA word
914
- // For overrun error, we read as well to clear the peripheral
915
- self . read_fifo_unchecked ( ) ;
916
- return Err ( err. into ( ) ) ;
917
- }
918
877
self . read_fifo ( ) . map ( |val| ( val & 0xff ) as u8 ) . map_err ( |e| {
919
878
if let nb:: Error :: Other ( _) = e {
920
879
unreachable ! ( )
@@ -926,16 +885,18 @@ impl<Uart: Instance> embedded_hal_nb::serial::Read<u8> for Rx<Uart> {
926
885
927
886
impl < Uart : Instance > embedded_io:: Read for Rx < Uart > {
928
887
fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , Self :: Error > {
929
- if buf. is_empty ( ) {
930
- return Ok ( 0 ) ;
931
- }
932
-
888
+ let mut read = 0 ;
933
889
for byte in buf. iter_mut ( ) {
934
- let w = nb:: block!( <Self as embedded_hal_nb:: serial:: Read <u8 >>:: read( self ) ) ?;
935
- * byte = w;
890
+ match <Self as embedded_hal_nb:: serial:: Read < u8 > >:: read ( self ) {
891
+ Ok ( w) => {
892
+ * byte = w;
893
+ read += 1 ;
894
+ }
895
+ Err ( nb:: Error :: WouldBlock ) => break ,
896
+ }
936
897
}
937
898
938
- Ok ( buf . len ( ) )
899
+ Ok ( read )
939
900
}
940
901
}
941
902
@@ -1090,14 +1051,12 @@ impl<Uart: Instance> embedded_hal_nb::serial::Write<u8> for Tx<Uart> {
1090
1051
1091
1052
impl < Uart : Instance > embedded_io:: Write for Tx < Uart > {
1092
1053
fn write ( & mut self , buf : & [ u8 ] ) -> Result < usize , Self :: Error > {
1093
- if buf. is_empty ( ) {
1094
- return Ok ( 0 ) ;
1095
- }
1096
-
1054
+ let mut written = 0 ;
1097
1055
for byte in buf. iter ( ) {
1098
- nb:: block!( <Self as embedded_hal_nb:: serial:: Write <u8 >>:: write(
1099
- self , * byte
1100
- ) ) ?;
1056
+ match <Self as embedded_hal_nb:: serial:: Write < u8 > >:: write ( self , * byte) {
1057
+ Ok ( _) => written += 1 ,
1058
+ Err ( nb:: Error :: WouldBlock ) => return Ok ( written) ,
1059
+ }
1101
1060
}
1102
1061
1103
1062
Ok ( buf. len ( ) )
@@ -1218,15 +1177,10 @@ impl<Uart: Instance> RxWithInterrupt<Uart> {
1218
1177
1219
1178
// Timeout, empty the FIFO completely.
1220
1179
if irq_end. irq_rx_to ( ) . bit_is_set ( ) {
1221
- loop {
1222
- // While there is data in the FIFO, write it into the reception buffer
1223
- let read_result = self . 0 . read ( ) ;
1224
- if let Some ( byte) = self . read_handler ( & mut result. errors , & read_result) {
1225
- buf[ result. bytes_read ] = byte;
1226
- result. bytes_read += 1 ;
1227
- } else {
1228
- break ;
1229
- }
1180
+ // While there is data in the FIFO, write it into the reception buffer
1181
+ while let Ok ( byte) = self . 0 . read_fifo ( ) {
1182
+ buf[ result. bytes_read ] = byte as u8 ;
1183
+ result. bytes_read += 1 ;
1230
1184
}
1231
1185
}
1232
1186
@@ -1303,12 +1257,13 @@ impl<Uart: Instance> RxWithInterrupt<Uart> {
1303
1257
if context. rx_idx == context. max_len {
1304
1258
break ;
1305
1259
}
1306
- let read_result = self . 0 . read ( ) ;
1307
- if let Some ( byte) = self . read_handler ( & mut result. errors , & read_result) {
1308
- buf[ context. rx_idx ] = byte;
1309
- context. rx_idx += 1 ;
1310
- } else {
1311
- break ;
1260
+ // While there is data in the FIFO, write it into the reception buffer
1261
+ match self . 0 . read ( ) {
1262
+ Ok ( byte) => {
1263
+ buf[ result. bytes_read ] = byte;
1264
+ result. bytes_read += 1 ;
1265
+ }
1266
+ Err ( _) => break ,
1312
1267
}
1313
1268
}
1314
1269
self . irq_completion_handler_max_size_timeout ( & mut result, context) ;
@@ -1327,29 +1282,6 @@ impl<Uart: Instance> RxWithInterrupt<Uart> {
1327
1282
Ok ( result)
1328
1283
}
1329
1284
1330
- fn read_handler (
1331
- & self ,
1332
- errors : & mut Option < UartErrors > ,
1333
- read_res : & nb:: Result < u8 , RxError > ,
1334
- ) -> Option < u8 > {
1335
- match read_res {
1336
- Ok ( byte) => Some ( * byte) ,
1337
- Err ( nb:: Error :: WouldBlock ) => None ,
1338
- Err ( nb:: Error :: Other ( e) ) => {
1339
- // Ensure `errors` is Some(IrqUartError), initializing if it's None
1340
- let err = errors. get_or_insert ( UartErrors :: default ( ) ) ;
1341
-
1342
- // Now we can safely modify fields inside `err`
1343
- match e {
1344
- RxError :: Overrun => err. overflow = true ,
1345
- RxError :: Framing => err. framing = true ,
1346
- RxError :: Parity => err. parity = true ,
1347
- }
1348
- None
1349
- }
1350
- }
1351
- }
1352
-
1353
1285
fn check_for_errors ( & self , errors : & mut Option < UartErrors > ) {
1354
1286
let rx_status = self . uart ( ) . rxstatus ( ) . read ( ) ;
1355
1287
0 commit comments