49
49
//!
50
50
//! // Write data (9 bits) to the USART.
51
51
//! // Depending on the configuration, only the lower 7, 8, or 9 bits are used.
52
- //! block!(tx.write_u16(0x1FF)).unwrap_infallible ();
52
+ //! block!(tx.write_u16(0x1FF)).unwrap ();
53
53
//!
54
54
//! // Write 'R' (8 bits) to the USART
55
- //! block!(tx.write (b'R')).unwrap_infallible ();
55
+ //! block!(tx.write_u8 (b'R')).unwrap ();
56
56
//!
57
57
//! // Receive a data (9 bits) from the USART and store it in "received"
58
58
//! let received = block!(rx.read_u16()).unwrap();
61
61
//! let received = block!(rx.read()).unwrap();
62
62
//! ```
63
63
64
- use core:: convert:: Infallible ;
65
64
use core:: marker:: PhantomData ;
66
65
use core:: ops:: Deref ;
67
66
use core:: sync:: atomic:: { self , Ordering } ;
@@ -134,8 +133,26 @@ inst! {
134
133
USART3
135
134
}
136
135
137
- /// Serial error
138
- pub use embedded_hal_nb:: serial:: ErrorKind as Error ;
136
+ /// Serial error kind
137
+ ///
138
+ /// This represents a common set of serial operation errors. HAL implementations are
139
+ /// free to define more specific or additional error types. However, by providing
140
+ /// a mapping to these common serial errors, generic code can still react to them.
141
+ #[ derive( Debug , Copy , Clone , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
142
+ #[ non_exhaustive]
143
+ pub enum Error {
144
+ /// The peripheral receive buffer was overrun.
145
+ Overrun ,
146
+ /// Received data does not conform to the peripheral configuration.
147
+ /// Can be caused by a misconfigured device on either end of the serial line.
148
+ FrameFormat ,
149
+ /// Parity check failed.
150
+ Parity ,
151
+ /// Serial line is too noisy to read valid data.
152
+ Noise ,
153
+ /// A different error occurred. The original error may contain more information.
154
+ Other ,
155
+ }
139
156
140
157
pub enum WordLength {
141
158
/// When parity is enabled, a word has 7 data bits + 1 parity bit,
@@ -321,7 +338,7 @@ impl<USART: Instance, PINS> Serial<USART, PINS> {
321
338
& mut self ,
322
339
config : impl Into < Config > ,
323
340
clocks : & Clocks ,
324
- ) -> nb:: Result < ( ) , Infallible > {
341
+ ) -> nb:: Result < ( ) , Error > {
325
342
reconfigure ( & mut self . tx , & mut self . rx , config, clocks)
326
343
}
327
344
@@ -403,7 +420,7 @@ pub fn reconfigure<USART: Instance>(
403
420
#[ allow( unused_variables) ] rx : & mut Rx < USART > ,
404
421
config : impl Into < Config > ,
405
422
clocks : & Clocks ,
406
- ) -> nb:: Result < ( ) , Infallible > {
423
+ ) -> nb:: Result < ( ) , Error > {
407
424
// if we're currently busy transmitting, we have to wait until that is
408
425
// over -- regarding reception, we assume that the caller -- with
409
426
// exclusive access to the Serial instance due to &mut self -- knows
@@ -419,7 +436,7 @@ impl<USART: Instance> Tx<USART> {
419
436
/// If the UART/USART was configured with `WordLength::Bits9`, the 9 least significant bits will
420
437
/// be transmitted and the other 7 bits will be ignored. Otherwise, the 8 least significant bits
421
438
/// will be transmitted and the other 8 bits will be ignored.
422
- pub fn write_u16 ( & mut self , word : u16 ) -> nb:: Result < ( ) , Infallible > {
439
+ pub fn write_u16 ( & mut self , word : u16 ) -> nb:: Result < ( ) , Error > {
423
440
let usart = unsafe { & * USART :: ptr ( ) } ;
424
441
425
442
if usart. sr . read ( ) . txe ( ) . bit_is_set ( ) {
@@ -430,25 +447,25 @@ impl<USART: Instance> Tx<USART> {
430
447
}
431
448
}
432
449
433
- pub fn write ( & mut self , word : u8 ) -> nb:: Result < ( ) , Infallible > {
450
+ pub fn write_u8 ( & mut self , word : u8 ) -> nb:: Result < ( ) , Error > {
434
451
self . write_u16 ( word as u16 )
435
452
}
436
453
437
- pub fn bwrite_all_u16 ( & mut self , buffer : & [ u16 ] ) -> Result < ( ) , Infallible > {
454
+ pub fn bwrite_all_u16 ( & mut self , buffer : & [ u16 ] ) -> Result < ( ) , Error > {
438
455
for & w in buffer {
439
456
nb:: block!( self . write_u16( w) ) ?;
440
457
}
441
458
Ok ( ( ) )
442
459
}
443
460
444
- pub fn bwrite_all ( & mut self , buffer : & [ u8 ] ) -> Result < ( ) , Infallible > {
461
+ pub fn bwrite_all_u8 ( & mut self , buffer : & [ u8 ] ) -> Result < ( ) , Error > {
445
462
for & w in buffer {
446
- nb:: block!( self . write ( w) ) ?;
463
+ nb:: block!( self . write_u8 ( w) ) ?;
447
464
}
448
465
Ok ( ( ) )
449
466
}
450
467
451
- pub fn flush ( & mut self ) -> nb:: Result < ( ) , Infallible > {
468
+ pub fn flush ( & mut self ) -> nb:: Result < ( ) , Error > {
452
469
let usart = unsafe { & * USART :: ptr ( ) } ;
453
470
454
471
if usart. sr . read ( ) . tc ( ) . bit_is_set ( ) {
@@ -458,7 +475,7 @@ impl<USART: Instance> Tx<USART> {
458
475
}
459
476
}
460
477
461
- pub fn bflush ( & mut self ) -> Result < ( ) , Infallible > {
478
+ pub fn bflush ( & mut self ) -> Result < ( ) , Error > {
462
479
nb:: block!( self . flush( ) )
463
480
}
464
481
@@ -485,7 +502,7 @@ impl<USART: Instance> Tx<USART> {
485
502
impl < USART : Instance > core:: fmt:: Write for Tx < USART > {
486
503
fn write_str ( & mut self , s : & str ) -> core:: fmt:: Result {
487
504
s. bytes ( )
488
- . try_for_each ( |c| nb:: block!( self . write ( c) ) )
505
+ . try_for_each ( |c| nb:: block!( self . write_u8 ( c) ) )
489
506
. map_err ( |_| core:: fmt:: Error )
490
507
}
491
508
}
0 commit comments