Skip to content

Commit a7f06c1

Browse files
Merge #261
261: further improve usart capabilities r=richardeoin a=andrewgazelka - added proper sync mode to usarts - added usart config options - clockphase: clockphase, - bitorder: bitorder, - clockpolarity: clockpolarity, - lastbitclockpulse: bool, Co-authored-by: Andrew Gazelka <andrew.gazelka@gmail.com>
2 parents be57a02 + 1378382 commit a7f06c1

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
@@ -14,16 +14,7 @@ use embedded_hal::prelude::*;
1414
use embedded_hal::serial;
1515
use nb::block;
1616

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

2819
use crate::gpio::gpioa::{
2920
PA0, PA1, PA10, PA11, PA12, PA15, PA2, PA3, PA4, PA8, PA9,
@@ -40,11 +31,17 @@ use crate::gpio::gpioh::{PH13, PH14};
4031
use crate::gpio::gpioi::PI9;
4132
#[cfg(not(feature = "stm32h7b0"))]
4233
use crate::gpio::gpioj::{PJ8, PJ9};
43-
4434
use crate::gpio::{Alternate, AF11, AF14, AF4, AF6, AF7, AF8};
4535
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};
4644
use crate::time::Hertz;
47-
4845
use crate::Never;
4946

5047
/// Serial error
@@ -100,11 +97,30 @@ pub mod config {
10097
STOP1P5,
10198
}
10299

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+
103115
pub struct Config {
104116
pub baudrate: Hertz,
105117
pub wordlength: WordLength,
106118
pub parity: Parity,
107119
pub stopbits: StopBits,
120+
pub clockphase: ClockPhase,
121+
pub bitorder: BitOrder,
122+
pub clockpolarity: ClockPolarity,
123+
pub lastbitclockpulse: bool,
108124
}
109125

110126
impl Config {
@@ -154,6 +170,10 @@ pub mod config {
154170
wordlength: WordLength::DataBits8,
155171
parity: Parity::ParityNone,
156172
stopbits: StopBits::STOP1,
173+
clockphase: ClockPhase::First,
174+
bitorder: BitOrder::LsbFirst,
175+
clockpolarity: ClockPolarity::IdleLow,
176+
lastbitclockpulse: false,
157177
}
158178
}
159179
}
@@ -168,7 +188,9 @@ pub mod config {
168188
}
169189
}
170190

171-
pub trait Pins<USART> {}
191+
pub trait Pins<USART> {
192+
const SYNCHRONOUS: bool = false;
193+
}
172194
pub trait PinTx<USART> {}
173195
pub trait PinRx<USART> {}
174196
pub trait PinCk<USART> {}
@@ -180,6 +202,15 @@ where
180202
{
181203
}
182204

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+
183214
/// A filler type for when the Tx pin is unnecessary
184215
pub struct NoTx;
185216
/// A filler type for when the Rx pin is unnecessary
@@ -372,9 +403,9 @@ pub struct Tx<USART> {
372403
pub trait SerialExt<USART>: Sized {
373404
type Rec: ResetEnable;
374405

375-
fn serial(
406+
fn serial<P: Pins<USART>>(
376407
self,
377-
_pins: impl Pins<USART>,
408+
_pins: P,
378409
config: impl Into<config::Config>,
379410
prec: Self::Rec,
380411
clocks: &CoreClocks,
@@ -424,7 +455,8 @@ macro_rules! usart {
424455
usart: $USARTX,
425456
config: impl Into<config::Config>,
426457
prec: rec::$Rec,
427-
clocks: &CoreClocks
458+
clocks: &CoreClocks,
459+
synchronous: bool
428460
) -> Result<Self, config::InvalidConfig>
429461
{
430462
use crate::stm32::usart1::cr2::STOP_A as STOP;
@@ -468,7 +500,35 @@ macro_rules! usart {
468500
StopBits::STOP1 => STOP::STOP1,
469501
StopBits::STOP1P5 => STOP::STOP1P5,
470502
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
471530
})
531+
472532
});
473533

474534
// Enable transmission and receiving
@@ -611,14 +671,14 @@ macro_rules! usart {
611671
impl SerialExt<$USARTX> for $USARTX {
612672
type Rec = rec::$Rec;
613673

614-
fn serial(self,
615-
_pins: impl Pins<$USARTX>,
674+
fn serial<P: Pins<$USARTX>>(self,
675+
_pins: P,
616676
config: impl Into<config::Config>,
617677
prec: rec::$Rec,
618678
clocks: &CoreClocks
619679
) -> Result<Serial<$USARTX>, config::InvalidConfig>
620680
{
621-
Serial::$usartX(self, config, prec, clocks)
681+
Serial::$usartX(self, config, prec, clocks, P::SYNCHRONOUS)
622682
}
623683

624684
fn serial_unchecked(self,
@@ -627,7 +687,7 @@ macro_rules! usart {
627687
clocks: &CoreClocks
628688
) -> Result<Serial<$USARTX>, config::InvalidConfig>
629689
{
630-
Serial::$usartX(self, config, prec, clocks)
690+
Serial::$usartX(self, config, prec, clocks, false)
631691
}
632692
}
633693

0 commit comments

Comments
 (0)