@@ -440,9 +440,14 @@ pub trait SerialExt<USART>: Sized {
440
440
}
441
441
}
442
442
443
+ macro_rules! replace_expr {
444
+ ( $_t: tt $sub: expr) => {
445
+ $sub
446
+ } ;
447
+ }
443
448
macro_rules! usart {
444
449
( $(
445
- $USARTX: ident: ( $usartX: ident, $Rec: ident, $pclkX: ident) ,
450
+ $USARTX: ident: ( $usartX: ident, $Rec: ident, $pclkX: ident $ ( , $synchronous : ident ) ? ) ,
446
451
) +) => {
447
452
$(
448
453
/// Configures a USART peripheral to provide serial
@@ -452,8 +457,8 @@ macro_rules! usart {
452
457
usart: $USARTX,
453
458
config: impl Into <config:: Config >,
454
459
prec: rec:: $Rec,
455
- clocks: & CoreClocks ,
456
- synchronous: bool
460
+ clocks: & CoreClocks
461
+ $ ( , $ synchronous: bool ) ?
457
462
) -> Result <Self , config:: InvalidConfig >
458
463
{
459
464
use crate :: stm32:: usart1:: cr2:: STOP_A as STOP ;
@@ -504,28 +509,33 @@ macro_rules! usart {
504
509
BitOrder :: MsbFirst => MSBFIRST_A :: MSB ,
505
510
} ) ;
506
511
507
- w. lbcl( ) . variant( if config. lastbitclockpulse {
508
- LBCL_A :: OUTPUT
509
- } else {
510
- LBCL_A :: NOTOUTPUT
511
- } ) ;
512
-
513
- w. clken( ) . variant( if synchronous {
514
- CLKEN_A :: ENABLED
515
- } else {
516
- CLKEN_A :: DISABLED
517
- } ) ;
518
-
519
- w. cpol( ) . variant( match config. clockpolarity {
520
- ClockPolarity :: IdleHigh =>CPOL_A :: HIGH ,
521
- ClockPolarity :: IdleLow =>CPOL_A :: LOW
522
- } ) ;
523
-
524
- w. cpha( ) . variant( match config. clockphase {
525
- ClockPhase :: First => CPHA_A :: FIRST ,
526
- ClockPhase :: Second => CPHA_A :: SECOND
527
- } )
528
-
512
+ // If synchronous mode is not supported, these bits are
513
+ // reserved and must be kept at reset value
514
+ $(
515
+ w. lbcl( ) . variant( if config. lastbitclockpulse {
516
+ LBCL_A :: OUTPUT
517
+ } else {
518
+ LBCL_A :: NOTOUTPUT
519
+ } ) ;
520
+
521
+ w. clken( ) . variant( if $synchronous {
522
+ CLKEN_A :: ENABLED
523
+ } else {
524
+ CLKEN_A :: DISABLED
525
+ } ) ;
526
+
527
+ w. cpol( ) . variant( match config. clockpolarity {
528
+ ClockPolarity :: IdleHigh =>CPOL_A :: HIGH ,
529
+ ClockPolarity :: IdleLow =>CPOL_A :: LOW
530
+ } ) ;
531
+
532
+ w. cpha( ) . variant( match config. clockphase {
533
+ ClockPhase :: First => CPHA_A :: FIRST ,
534
+ ClockPhase :: Second => CPHA_A :: SECOND
535
+ } ) ;
536
+ ) ?
537
+
538
+ w
529
539
} ) ;
530
540
531
541
// Enable transmission and receiving
@@ -675,17 +685,24 @@ macro_rules! usart {
675
685
clocks: & CoreClocks
676
686
) -> Result <Serial <$USARTX>, config:: InvalidConfig >
677
687
{
678
- Serial :: $usartX( self , config, prec, clocks, P :: SYNCHRONOUS )
688
+ Serial :: $usartX(
689
+ self , config, prec, clocks
690
+ $( , replace_expr!( $synchronous P :: SYNCHRONOUS ) ) ?
691
+ )
679
692
}
680
693
681
694
fn serial_unchecked( self ,
682
695
config: impl Into <config:: Config >,
683
696
prec: rec:: $Rec,
684
697
clocks: & CoreClocks ,
698
+ #[ allow( unused) ]
685
699
synchronous: bool ,
686
700
) -> Result <Serial <$USARTX>, config:: InvalidConfig >
687
701
{
688
- Serial :: $usartX( self , config, prec, clocks, synchronous)
702
+ Serial :: $usartX(
703
+ self , config, prec, clocks
704
+ $( , replace_expr!( $synchronous synchronous) ) ?
705
+ )
689
706
}
690
707
}
691
708
@@ -913,10 +930,10 @@ macro_rules! usart_sel {
913
930
}
914
931
915
932
usart ! {
916
- USART1 : ( usart1, Usart1 , pclk2) ,
917
- USART2 : ( usart2, Usart2 , pclk1) ,
918
- USART3 : ( usart3, Usart3 , pclk1) ,
919
- USART6 : ( usart6, Usart6 , pclk2) ,
933
+ USART1 : ( usart1, Usart1 , pclk2, synchronous ) ,
934
+ USART2 : ( usart2, Usart2 , pclk1, synchronous ) ,
935
+ USART3 : ( usart3, Usart3 , pclk1, synchronous ) ,
936
+ USART6 : ( usart6, Usart6 , pclk2, synchronous ) ,
920
937
921
938
UART4 : ( uart4, Uart4 , pclk1) ,
922
939
UART5 : ( uart5, Uart5 , pclk1) ,
0 commit comments