@@ -21,6 +21,7 @@ pub struct UartPeripheral<S: State, D: UartDevice, P: ValidUartPinout<D>> {
21
21
device : D ,
22
22
_state : S ,
23
23
pins : P ,
24
+ read_error : Option < ReadErrorType > ,
24
25
}
25
26
26
27
impl < S : State , D : UartDevice , P : ValidUartPinout < D > > UartPeripheral < S , D , P > {
@@ -29,6 +30,7 @@ impl<S: State, D: UartDevice, P: ValidUartPinout<D>> UartPeripheral<S, D, P> {
29
30
device : self . device ,
30
31
pins : self . pins ,
31
32
_state : state,
33
+ read_error : None ,
32
34
}
33
35
}
34
36
@@ -48,6 +50,7 @@ impl<D: UartDevice, P: ValidUartPinout<D>> UartPeripheral<Disabled, D, P> {
48
50
device,
49
51
_state : Disabled ,
50
52
pins,
53
+ read_error : None ,
51
54
}
52
55
}
53
56
@@ -88,6 +91,7 @@ impl<D: UartDevice, P: ValidUartPinout<D>> UartPeripheral<Disabled, D, P> {
88
91
device,
89
92
pins,
90
93
_state : Enabled ,
94
+ read_error : None ,
91
95
} )
92
96
}
93
97
}
@@ -247,6 +251,7 @@ impl<D: UartDevice, P: ValidUartPinout<D>> UartPeripheral<Enabled, D, P> {
247
251
device : reader. device ,
248
252
_state : Enabled ,
249
253
pins : reader. pins ,
254
+ read_error : reader. read_error ,
250
255
}
251
256
}
252
257
}
@@ -257,6 +262,7 @@ impl<P: ValidUartPinout<UART0>> UartPeripheral<Enabled, UART0, P> {
257
262
let reader = Reader {
258
263
device : self . device ,
259
264
pins : self . pins ,
265
+ read_error : self . read_error ,
260
266
} ;
261
267
// Safety: reader and writer will never write to the same address
262
268
let device_copy = unsafe { Peripherals :: steal ( ) . UART0 } ;
@@ -275,6 +281,7 @@ impl<P: ValidUartPinout<UART1>> UartPeripheral<Enabled, UART1, P> {
275
281
let reader = Reader {
276
282
device : self . device ,
277
283
pins : self . pins ,
284
+ read_error : self . read_error ,
278
285
} ;
279
286
// Safety: reader and writer will never write to the same address
280
287
let device_copy = unsafe { Peripherals :: steal ( ) . UART1 } ;
@@ -466,7 +473,21 @@ impl<D: UartDevice, P: ValidUartPinout<D>> embedded_io::ErrorType
466
473
}
467
474
impl < D : UartDevice , P : ValidUartPinout < D > > embedded_io:: Read for UartPeripheral < Enabled , D , P > {
468
475
fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , Self :: Error > {
469
- nb:: block!( self . read_raw( buf) ) . map_err ( |e| e. err_type )
476
+ // If the last read stored an error, report it now
477
+ if let Some ( err) = self . read_error . take ( ) {
478
+ return Err ( err) ;
479
+ }
480
+ match nb:: block!( self . read_raw( buf) ) {
481
+ Ok ( bytes_read) => Ok ( bytes_read) ,
482
+ Err ( err) if !err. discarded . is_empty ( ) => {
483
+ // If an error was reported but some bytes were already read,
484
+ // return the data now and store the error for the next
485
+ // invocation.
486
+ self . read_error = Some ( err. err_type ) ;
487
+ Ok ( err. discarded . len ( ) )
488
+ }
489
+ Err ( err) => Err ( err. err_type ) ,
490
+ }
470
491
}
471
492
}
472
493
impl < D : UartDevice , P : ValidUartPinout < D > > embedded_io:: Write for UartPeripheral < Enabled , D , P > {
0 commit comments