@@ -14,16 +14,7 @@ use embedded_hal::prelude::*;
14
14
use embedded_hal:: serial;
15
15
use nb:: block;
16
16
17
- use crate :: stm32;
18
- use crate :: stm32:: usart1:: cr1:: { M0_A as M0 , PCE_A as PCE , PS_A as PS } ;
19
-
20
- #[ cfg( feature = "rm0455" ) ]
21
- use crate :: stm32:: rcc:: cdccip2r:: { USART16910SEL_A , USART234578SEL_A } ;
22
- #[ cfg( not( feature = "rm0455" ) ) ]
23
- use crate :: stm32:: rcc:: d2ccip2r:: { USART16SEL_A , USART234578SEL_A } ;
24
-
25
- use crate :: stm32:: { UART4 , UART5 , UART7 , UART8 } ;
26
- use crate :: stm32:: { USART1 , USART2 , USART3 , USART6 } ;
17
+ use stm32:: usart1:: cr2:: { CLKEN_A , CPHA_A , CPOL_A , LBCL_A , MSBFIRST_A } ;
27
18
28
19
use crate :: gpio:: gpioa:: {
29
20
PA0 , PA1 , PA10 , PA11 , PA12 , PA15 , PA2 , PA3 , PA4 , PA8 , PA9 ,
@@ -40,11 +31,17 @@ use crate::gpio::gpioh::{PH13, PH14};
40
31
use crate :: gpio:: gpioi:: PI9 ;
41
32
#[ cfg( not( feature = "stm32h7b0" ) ) ]
42
33
use crate :: gpio:: gpioj:: { PJ8 , PJ9 } ;
43
-
44
34
use crate :: gpio:: { Alternate , AF11 , AF14 , AF4 , AF6 , AF7 , AF8 } ;
45
35
use crate :: rcc:: { rec, CoreClocks , ResetEnable } ;
36
+ use crate :: stm32;
37
+ #[ cfg( feature = "rm0455" ) ]
38
+ use crate :: stm32:: rcc:: cdccip2r:: { USART16910SEL_A , USART234578SEL_A } ;
39
+ #[ cfg( not( feature = "rm0455" ) ) ]
40
+ use crate :: stm32:: rcc:: d2ccip2r:: { USART16SEL_A , USART234578SEL_A } ;
41
+ use crate :: stm32:: usart1:: cr1:: { M0_A as M0 , PCE_A as PCE , PS_A as PS } ;
42
+ use crate :: stm32:: { UART4 , UART5 , UART7 , UART8 } ;
43
+ use crate :: stm32:: { USART1 , USART2 , USART3 , USART6 } ;
46
44
use crate :: time:: Hertz ;
47
-
48
45
use crate :: Never ;
49
46
50
47
/// Serial error
@@ -100,11 +97,30 @@ pub mod config {
100
97
STOP1P5 ,
101
98
}
102
99
100
+ pub enum BitOrder {
101
+ LsbFirst ,
102
+ MsbFirst ,
103
+ }
104
+
105
+ pub enum ClockPhase {
106
+ First ,
107
+ Second ,
108
+ }
109
+
110
+ pub enum ClockPolarity {
111
+ IdleHigh ,
112
+ IdleLow ,
113
+ }
114
+
103
115
pub struct Config {
104
116
pub baudrate : Hertz ,
105
117
pub wordlength : WordLength ,
106
118
pub parity : Parity ,
107
119
pub stopbits : StopBits ,
120
+ pub clockphase : ClockPhase ,
121
+ pub bitorder : BitOrder ,
122
+ pub clockpolarity : ClockPolarity ,
123
+ pub lastbitclockpulse : bool ,
108
124
}
109
125
110
126
impl Config {
@@ -154,6 +170,10 @@ pub mod config {
154
170
wordlength : WordLength :: DataBits8 ,
155
171
parity : Parity :: ParityNone ,
156
172
stopbits : StopBits :: STOP1 ,
173
+ clockphase : ClockPhase :: First ,
174
+ bitorder : BitOrder :: LsbFirst ,
175
+ clockpolarity : ClockPolarity :: IdleLow ,
176
+ lastbitclockpulse : false ,
157
177
}
158
178
}
159
179
}
@@ -168,7 +188,9 @@ pub mod config {
168
188
}
169
189
}
170
190
171
- pub trait Pins < USART > { }
191
+ pub trait Pins < USART > {
192
+ const SYNCHRONOUS : bool = false ;
193
+ }
172
194
pub trait PinTx < USART > { }
173
195
pub trait PinRx < USART > { }
174
196
pub trait PinCk < USART > { }
@@ -180,6 +202,15 @@ where
180
202
{
181
203
}
182
204
205
+ impl < USART , TX , RX , CK > Pins < USART > for ( TX , RX , CK )
206
+ where
207
+ TX : PinTx < USART > ,
208
+ RX : PinRx < USART > ,
209
+ CK : PinCk < USART > ,
210
+ {
211
+ const SYNCHRONOUS : bool = true ;
212
+ }
213
+
183
214
/// A filler type for when the Tx pin is unnecessary
184
215
pub struct NoTx ;
185
216
/// A filler type for when the Rx pin is unnecessary
@@ -372,9 +403,9 @@ pub struct Tx<USART> {
372
403
pub trait SerialExt < USART > : Sized {
373
404
type Rec : ResetEnable ;
374
405
375
- fn serial (
406
+ fn serial < P : Pins < USART > > (
376
407
self ,
377
- _pins : impl Pins < USART > ,
408
+ _pins : P ,
378
409
config : impl Into < config:: Config > ,
379
410
prec : Self :: Rec ,
380
411
clocks : & CoreClocks ,
@@ -424,7 +455,8 @@ macro_rules! usart {
424
455
usart: $USARTX,
425
456
config: impl Into <config:: Config >,
426
457
prec: rec:: $Rec,
427
- clocks: & CoreClocks
458
+ clocks: & CoreClocks ,
459
+ synchronous: bool
428
460
) -> Result <Self , config:: InvalidConfig >
429
461
{
430
462
use crate :: stm32:: usart1:: cr2:: STOP_A as STOP ;
@@ -468,7 +500,35 @@ macro_rules! usart {
468
500
StopBits :: STOP1 => STOP :: STOP1 ,
469
501
StopBits :: STOP1P5 => STOP :: STOP1P5 ,
470
502
StopBits :: STOP2 => STOP :: STOP2 ,
503
+ } ) ;
504
+
505
+ w. msbfirst( ) . variant( match config. bitorder {
506
+ BitOrder :: LsbFirst => MSBFIRST_A :: LSB ,
507
+ BitOrder :: MsbFirst => MSBFIRST_A :: MSB ,
508
+ } ) ;
509
+
510
+ w. lbcl( ) . variant( if config. lastbitclockpulse {
511
+ LBCL_A :: OUTPUT
512
+ } else {
513
+ LBCL_A :: NOTOUTPUT
514
+ } ) ;
515
+
516
+ w. clken( ) . variant( if synchronous {
517
+ CLKEN_A :: ENABLED
518
+ } else {
519
+ CLKEN_A :: DISABLED
520
+ } ) ;
521
+
522
+ w. cpol( ) . variant( match config. clockpolarity {
523
+ ClockPolarity :: IdleHigh =>CPOL_A :: HIGH ,
524
+ ClockPolarity :: IdleLow =>CPOL_A :: LOW
525
+ } ) ;
526
+
527
+ w. cpha( ) . variant( match config. clockphase {
528
+ ClockPhase :: First => CPHA_A :: FIRST ,
529
+ ClockPhase :: Second => CPHA_A :: SECOND
471
530
} )
531
+
472
532
} ) ;
473
533
474
534
// Enable transmission and receiving
@@ -611,14 +671,14 @@ macro_rules! usart {
611
671
impl SerialExt <$USARTX> for $USARTX {
612
672
type Rec = rec:: $Rec;
613
673
614
- fn serial( self ,
615
- _pins: impl Pins <$USARTX> ,
674
+ fn serial< P : Pins <$USARTX>> ( self ,
675
+ _pins: P ,
616
676
config: impl Into <config:: Config >,
617
677
prec: rec:: $Rec,
618
678
clocks: & CoreClocks
619
679
) -> Result <Serial <$USARTX>, config:: InvalidConfig >
620
680
{
621
- Serial :: $usartX( self , config, prec, clocks)
681
+ Serial :: $usartX( self , config, prec, clocks, P :: SYNCHRONOUS )
622
682
}
623
683
624
684
fn serial_unchecked( self ,
@@ -627,7 +687,7 @@ macro_rules! usart {
627
687
clocks: & CoreClocks
628
688
) -> Result <Serial <$USARTX>, config:: InvalidConfig >
629
689
{
630
- Serial :: $usartX( self , config, prec, clocks)
690
+ Serial :: $usartX( self , config, prec, clocks, false )
631
691
}
632
692
}
633
693
0 commit comments