Skip to content

Commit 1378382

Browse files
committed
further improve usart capabilities
- added proper sync mode - added usart config options - clockphase: clockphase, - bitorder: bitorder, - clockpolarity: clockpolarity, - lastbitclockpulse: bool,
1 parent fc623b7 commit 1378382

File tree

1 file changed

+80
-20
lines changed

1 file changed

+80
-20
lines changed

src/serial.rs

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,7 @@ use embedded_hal::prelude::*;
99
use embedded_hal::serial;
1010
use nb::block;
1111

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};
2213

2314
use crate::gpio::gpioa::{
2415
PA0, PA1, PA10, PA11, PA12, PA15, PA2, PA3, PA4, PA8, PA9,
@@ -35,11 +26,17 @@ use crate::gpio::gpioh::{PH13, PH14};
3526
use crate::gpio::gpioi::PI9;
3627
#[cfg(not(feature = "stm32h7b0"))]
3728
use crate::gpio::gpioj::{PJ8, PJ9};
38-
3929
use crate::gpio::{Alternate, AF11, AF14, AF4, AF6, AF7, AF8};
4030
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};
4139
use crate::time::Hertz;
42-
4340
use crate::Never;
4441

4542
/// Serial error
@@ -95,11 +92,30 @@ pub mod config {
9592
STOP1P5,
9693
}
9794

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+
98110
pub struct Config {
99111
pub baudrate: Hertz,
100112
pub wordlength: WordLength,
101113
pub parity: Parity,
102114
pub stopbits: StopBits,
115+
pub clockphase: ClockPhase,
116+
pub bitorder: BitOrder,
117+
pub clockpolarity: ClockPolarity,
118+
pub lastbitclockpulse: bool,
103119
}
104120

105121
impl Config {
@@ -149,6 +165,10 @@ pub mod config {
149165
wordlength: WordLength::DataBits8,
150166
parity: Parity::ParityNone,
151167
stopbits: StopBits::STOP1,
168+
clockphase: ClockPhase::First,
169+
bitorder: BitOrder::LsbFirst,
170+
clockpolarity: ClockPolarity::IdleLow,
171+
lastbitclockpulse: false,
152172
}
153173
}
154174
}
@@ -163,7 +183,9 @@ pub mod config {
163183
}
164184
}
165185

166-
pub trait Pins<USART> {}
186+
pub trait Pins<USART> {
187+
const SYNCHRONOUS: bool = false;
188+
}
167189
pub trait PinTx<USART> {}
168190
pub trait PinRx<USART> {}
169191
pub trait PinCk<USART> {}
@@ -175,6 +197,15 @@ where
175197
{
176198
}
177199

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+
178209
/// A filler type for when the Tx pin is unnecessary
179210
pub struct NoTx;
180211
/// A filler type for when the Rx pin is unnecessary
@@ -367,9 +398,9 @@ pub struct Tx<USART> {
367398
pub trait SerialExt<USART>: Sized {
368399
type Rec: ResetEnable;
369400

370-
fn serial(
401+
fn serial<P: Pins<USART>>(
371402
self,
372-
_pins: impl Pins<USART>,
403+
_pins: P,
373404
config: impl Into<config::Config>,
374405
prec: Self::Rec,
375406
clocks: &CoreClocks,
@@ -419,7 +450,8 @@ macro_rules! usart {
419450
usart: $USARTX,
420451
config: impl Into<config::Config>,
421452
prec: rec::$Rec,
422-
clocks: &CoreClocks
453+
clocks: &CoreClocks,
454+
synchronous: bool
423455
) -> Result<Self, config::InvalidConfig>
424456
{
425457
use crate::stm32::usart1::cr2::STOP_A as STOP;
@@ -463,7 +495,35 @@ macro_rules! usart {
463495
StopBits::STOP1 => STOP::STOP1,
464496
StopBits::STOP1P5 => STOP::STOP1P5,
465497
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
466525
})
526+
467527
});
468528

469529
// Enable transmission and receiving
@@ -606,14 +666,14 @@ macro_rules! usart {
606666
impl SerialExt<$USARTX> for $USARTX {
607667
type Rec = rec::$Rec;
608668

609-
fn serial(self,
610-
_pins: impl Pins<$USARTX>,
669+
fn serial<P: Pins<$USARTX>>(self,
670+
_pins: P,
611671
config: impl Into<config::Config>,
612672
prec: rec::$Rec,
613673
clocks: &CoreClocks
614674
) -> Result<Serial<$USARTX>, config::InvalidConfig>
615675
{
616-
Serial::$usartX(self, config, prec, clocks)
676+
Serial::$usartX(self, config, prec, clocks, P::SYNCHRONOUS)
617677
}
618678

619679
fn serial_unchecked(self,
@@ -622,7 +682,7 @@ macro_rules! usart {
622682
clocks: &CoreClocks
623683
) -> Result<Serial<$USARTX>, config::InvalidConfig>
624684
{
625-
Serial::$usartX(self, config, prec, clocks)
685+
Serial::$usartX(self, config, prec, clocks, false)
626686
}
627687
}
628688

0 commit comments

Comments
 (0)