@@ -321,17 +321,52 @@ cfg_if! {
321
321
322
322
/// Types for configuring a serial interface.
323
323
pub mod config {
324
+ use crate :: pac:: usart1:: cr2:: STOP_A ;
324
325
use crate :: time:: rate:: { Baud , Extensions } ;
325
326
326
- // Reexport stop bit enum from PAC. In case there is a breaking change,
327
- // provide a compatible enum from this HAL.
328
- pub use crate :: pac:: usart1:: cr2:: STOP_A as StopBits ;
327
+ /// Stop Bit configuration parameter for serial.
328
+ ///
329
+ /// Wrapper around [`STOP_A`]
330
+ #[ derive( Clone , Copy , Debug , PartialEq ) ]
331
+ #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
332
+ pub enum StopBits {
333
+ /// 0.5 stop bit
334
+ Stop0P5 ,
335
+ /// 1 stop bit
336
+ Stop1 ,
337
+ /// 1.5 stop bit
338
+ Stop1P5 ,
339
+ /// 2 stop bit
340
+ Stop2 ,
341
+ }
342
+
343
+ impl From < StopBits > for STOP_A {
344
+ fn from ( stopbit : StopBits ) -> Self {
345
+ match stopbit {
346
+ StopBits :: Stop0P5 => STOP_A :: STOP0P5 ,
347
+ StopBits :: Stop1 => STOP_A :: STOP1 ,
348
+ StopBits :: Stop1P5 => STOP_A :: STOP1P5 ,
349
+ StopBits :: Stop2 => STOP_A :: STOP2 ,
350
+ }
351
+ }
352
+ }
353
+
354
+ impl From < STOP_A > for StopBits {
355
+ fn from ( stopbit : STOP_A ) -> Self {
356
+ match stopbit {
357
+ STOP_A :: STOP0P5 => StopBits :: Stop0P5 ,
358
+ STOP_A :: STOP1 => StopBits :: Stop1 ,
359
+ STOP_A :: STOP1P5 => StopBits :: Stop1P5 ,
360
+ STOP_A :: STOP2 => StopBits :: Stop2 ,
361
+ }
362
+ }
363
+ }
329
364
330
365
/// Parity generation and checking. If odd or even parity is selected, the
331
366
/// underlying USART will be configured to send/receive the parity bit in
332
367
/// addtion to the data bits.
333
368
#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
334
- #[ derive( Clone , Copy , PartialEq ) ]
369
+ #[ derive( Debug , Clone , Copy , PartialEq ) ]
335
370
pub enum Parity {
336
371
/// No parity bit will be added/checked.
337
372
None ,
@@ -359,7 +394,7 @@ pub mod config {
359
394
/// assert!(config.parity == Parity::None);
360
395
/// assert!(config.stopbits == StopBits::STOP1);
361
396
/// ```
362
- #[ derive( Clone , Copy , PartialEq ) ]
397
+ #[ derive( Debug , Clone , Copy , PartialEq ) ]
363
398
#[ non_exhaustive]
364
399
pub struct Config {
365
400
/// Serial interface baud rate
@@ -398,7 +433,7 @@ pub mod config {
398
433
Config {
399
434
baudrate : 115_200 . Bd ( ) ,
400
435
parity : Parity :: None ,
401
- stopbits : StopBits :: STOP1 ,
436
+ stopbits : StopBits :: Stop1 ,
402
437
}
403
438
}
404
439
}
@@ -411,6 +446,24 @@ pub mod config {
411
446
}
412
447
}
413
448
}
449
+
450
+ #[ cfg( feature = "defmt" ) ]
451
+ impl defmt:: Format for Config {
452
+ fn format ( & self , f : defmt:: Formatter ) {
453
+ // Omitting pins makes it:
454
+ // 1. Easier.
455
+ // 2. Not to specialized to use it ergonimically for users
456
+ // even in a generic context.
457
+ // 3. Not require specialization.
458
+ defmt:: write!(
459
+ f,
460
+ "Serial {{ baudrate: {} Bd , parity: {} , stopbits: {} }}" ,
461
+ self . baudrate. 0 ,
462
+ self . parity,
463
+ self . stopbits,
464
+ ) ;
465
+ }
466
+ }
414
467
}
415
468
416
469
/// Serial abstraction
@@ -586,7 +639,9 @@ where
586
639
Parity :: Odd => ( M_A :: BIT9 , PS_A :: ODD , PCE_A :: ENABLED ) ,
587
640
} ;
588
641
589
- usart. cr2 . modify ( |_, w| w. stop ( ) . variant ( config. stopbits ) ) ;
642
+ usart
643
+ . cr2
644
+ . modify ( |_, w| w. stop ( ) . variant ( config. stopbits . into ( ) ) ) ;
590
645
usart. cr1 . modify ( |_, w| {
591
646
w. ps ( ) . variant ( ps) ; // set parity mode
592
647
w. pce ( ) . variant ( pce) ; // enable parity checking/generation
0 commit comments