@@ -9,16 +9,7 @@ use embedded_hal::prelude::*;
9
9
use embedded_hal:: serial;
10
10
use nb:: block;
11
11
12
- use crate :: stm32;
13
- use crate :: stm32:: usart1:: cr1:: { M0_A as M0 , PCE_A as PCE , PS_A as PS } ;
14
-
15
- #[ cfg( feature = "rm0455" ) ]
16
- use crate :: stm32:: rcc:: cdccip2r:: { USART16910SEL_A , USART234578SEL_A } ;
17
- #[ cfg( not( feature = "rm0455" ) ) ]
18
- use crate :: stm32:: rcc:: d2ccip2r:: { USART16SEL_A , USART234578SEL_A } ;
19
-
20
- use crate :: stm32:: { UART4 , UART5 , UART7 , UART8 } ;
21
- use crate :: stm32:: { USART1 , USART2 , USART3 , USART6 } ;
12
+ use stm32:: usart1:: cr2:: { CLKEN_A , CPHA_A , CPOL_A , LBCL_A , MSBFIRST_A } ;
22
13
23
14
use crate :: gpio:: gpioa:: {
24
15
PA0 , PA1 , PA10 , PA11 , PA12 , PA15 , PA2 , PA3 , PA4 , PA8 , PA9 ,
@@ -35,11 +26,17 @@ use crate::gpio::gpioh::{PH13, PH14};
35
26
use crate :: gpio:: gpioi:: PI9 ;
36
27
#[ cfg( not( feature = "stm32h7b0" ) ) ]
37
28
use crate :: gpio:: gpioj:: { PJ8 , PJ9 } ;
38
-
39
29
use crate :: gpio:: { Alternate , AF11 , AF14 , AF4 , AF6 , AF7 , AF8 } ;
40
30
use crate :: rcc:: { rec, CoreClocks , ResetEnable } ;
31
+ use crate :: stm32;
32
+ #[ cfg( feature = "rm0455" ) ]
33
+ use crate :: stm32:: rcc:: cdccip2r:: { USART16910SEL_A , USART234578SEL_A } ;
34
+ #[ cfg( not( feature = "rm0455" ) ) ]
35
+ use crate :: stm32:: rcc:: d2ccip2r:: { USART16SEL_A , USART234578SEL_A } ;
36
+ use crate :: stm32:: usart1:: cr1:: { M0_A as M0 , PCE_A as PCE , PS_A as PS } ;
37
+ use crate :: stm32:: { UART4 , UART5 , UART7 , UART8 } ;
38
+ use crate :: stm32:: { USART1 , USART2 , USART3 , USART6 } ;
41
39
use crate :: time:: Hertz ;
42
-
43
40
use crate :: Never ;
44
41
45
42
/// Serial error
@@ -95,11 +92,30 @@ pub mod config {
95
92
STOP1P5 ,
96
93
}
97
94
95
+ pub enum BitOrder {
96
+ LsbFirst ,
97
+ MsbFirst ,
98
+ }
99
+
100
+ pub enum ClockPhase {
101
+ First ,
102
+ Second ,
103
+ }
104
+
105
+ pub enum ClockPolarity {
106
+ IdleHigh ,
107
+ IdleLow ,
108
+ }
109
+
98
110
pub struct Config {
99
111
pub baudrate : Hertz ,
100
112
pub wordlength : WordLength ,
101
113
pub parity : Parity ,
102
114
pub stopbits : StopBits ,
115
+ pub clockphase : ClockPhase ,
116
+ pub bitorder : BitOrder ,
117
+ pub clockpolarity : ClockPolarity ,
118
+ pub lastbitclockpulse : bool ,
103
119
}
104
120
105
121
impl Config {
@@ -149,6 +165,10 @@ pub mod config {
149
165
wordlength : WordLength :: DataBits8 ,
150
166
parity : Parity :: ParityNone ,
151
167
stopbits : StopBits :: STOP1 ,
168
+ clockphase : ClockPhase :: First ,
169
+ bitorder : BitOrder :: LsbFirst ,
170
+ clockpolarity : ClockPolarity :: IdleLow ,
171
+ lastbitclockpulse : false ,
152
172
}
153
173
}
154
174
}
@@ -163,7 +183,9 @@ pub mod config {
163
183
}
164
184
}
165
185
166
- pub trait Pins < USART > { }
186
+ pub trait Pins < USART > {
187
+ const SYNCHRONOUS : bool = false ;
188
+ }
167
189
pub trait PinTx < USART > { }
168
190
pub trait PinRx < USART > { }
169
191
pub trait PinCk < USART > { }
@@ -175,6 +197,15 @@ where
175
197
{
176
198
}
177
199
200
+ impl < USART , TX , RX , CK > Pins < USART > for ( TX , RX , CK )
201
+ where
202
+ TX : PinTx < USART > ,
203
+ RX : PinRx < USART > ,
204
+ CK : PinCk < USART > ,
205
+ {
206
+ const SYNCHRONOUS : bool = true ;
207
+ }
208
+
178
209
/// A filler type for when the Tx pin is unnecessary
179
210
pub struct NoTx ;
180
211
/// A filler type for when the Rx pin is unnecessary
@@ -367,9 +398,9 @@ pub struct Tx<USART> {
367
398
pub trait SerialExt < USART > : Sized {
368
399
type Rec : ResetEnable ;
369
400
370
- fn serial (
401
+ fn serial < P : Pins < USART > > (
371
402
self ,
372
- _pins : impl Pins < USART > ,
403
+ _pins : P ,
373
404
config : impl Into < config:: Config > ,
374
405
prec : Self :: Rec ,
375
406
clocks : & CoreClocks ,
@@ -419,7 +450,8 @@ macro_rules! usart {
419
450
usart: $USARTX,
420
451
config: impl Into <config:: Config >,
421
452
prec: rec:: $Rec,
422
- clocks: & CoreClocks
453
+ clocks: & CoreClocks ,
454
+ synchronous: bool
423
455
) -> Result <Self , config:: InvalidConfig >
424
456
{
425
457
use crate :: stm32:: usart1:: cr2:: STOP_A as STOP ;
@@ -463,7 +495,35 @@ macro_rules! usart {
463
495
StopBits :: STOP1 => STOP :: STOP1 ,
464
496
StopBits :: STOP1P5 => STOP :: STOP1P5 ,
465
497
StopBits :: STOP2 => STOP :: STOP2 ,
498
+ } ) ;
499
+
500
+ w. msbfirst( ) . variant( match config. bitorder {
501
+ BitOrder :: LsbFirst => MSBFIRST_A :: LSB ,
502
+ BitOrder :: MsbFirst => MSBFIRST_A :: MSB ,
503
+ } ) ;
504
+
505
+ w. lbcl( ) . variant( if config. lastbitclockpulse {
506
+ LBCL_A :: OUTPUT
507
+ } else {
508
+ LBCL_A :: NOTOUTPUT
509
+ } ) ;
510
+
511
+ w. clken( ) . variant( if synchronous {
512
+ CLKEN_A :: ENABLED
513
+ } else {
514
+ CLKEN_A :: DISABLED
515
+ } ) ;
516
+
517
+ w. cpol( ) . variant( match config. clockpolarity {
518
+ ClockPolarity :: IdleHigh =>CPOL_A :: HIGH ,
519
+ ClockPolarity :: IdleLow =>CPOL_A :: LOW
520
+ } ) ;
521
+
522
+ w. cpha( ) . variant( match config. clockphase {
523
+ ClockPhase :: First => CPHA_A :: FIRST ,
524
+ ClockPhase :: Second => CPHA_A :: SECOND
466
525
} )
526
+
467
527
} ) ;
468
528
469
529
// Enable transmission and receiving
@@ -606,14 +666,14 @@ macro_rules! usart {
606
666
impl SerialExt <$USARTX> for $USARTX {
607
667
type Rec = rec:: $Rec;
608
668
609
- fn serial( self ,
610
- _pins: impl Pins <$USARTX> ,
669
+ fn serial< P : Pins <$USARTX>> ( self ,
670
+ _pins: P ,
611
671
config: impl Into <config:: Config >,
612
672
prec: rec:: $Rec,
613
673
clocks: & CoreClocks
614
674
) -> Result <Serial <$USARTX>, config:: InvalidConfig >
615
675
{
616
- Serial :: $usartX( self , config, prec, clocks)
676
+ Serial :: $usartX( self , config, prec, clocks, P :: SYNCHRONOUS )
617
677
}
618
678
619
679
fn serial_unchecked( self ,
@@ -622,7 +682,7 @@ macro_rules! usart {
622
682
clocks: & CoreClocks
623
683
) -> Result <Serial <$USARTX>, config:: InvalidConfig >
624
684
{
625
- Serial :: $usartX( self , config, prec, clocks)
685
+ Serial :: $usartX( self , config, prec, clocks, false )
626
686
}
627
687
}
628
688
0 commit comments