Skip to content

Commit 4a09898

Browse files
committed
Serial flow control
1 parent 5fc93ec commit 4a09898

File tree

5 files changed

+74
-11
lines changed

5 files changed

+74
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Added
1111

12+
- Serial flow control enable
1213
- `i2c_scanner` example [#758]
1314
- Enable `sdio` for stm32f446
1415
- port LTDC implementation and example from stm32f7xx-hal [#731]

src/gpio/alt.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,10 @@ pub trait SerialSync {
335335
type Ck;
336336
}
337337
/// Hardware flow control (RS232)
338-
pub trait SerialRs232 {
339-
/// Receive
338+
pub trait SerialFlowControl {
339+
/// "Clear To Send" blocks the data transmission at the end of the current transfer when high
340340
type Cts;
341-
/// Transmit
341+
/// "Request to send" indicates that the USART is ready to receive a data (when low)
342342
type Rts;
343343
}
344344

src/gpio/alt/f4.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -4047,7 +4047,7 @@ pub mod usart1 {
40474047
impl SerialSync for USART {
40484048
type Ck = Ck;
40494049
}
4050-
impl SerialRs232 for USART {
4050+
impl SerialFlowControl for USART {
40514051
type Cts = Cts;
40524052
type Rts = Rts;
40534053
}
@@ -4103,7 +4103,7 @@ pub mod usart2 {
41034103
impl SerialSync for USART {
41044104
type Ck = Ck;
41054105
}
4106-
impl SerialRs232 for USART {
4106+
impl SerialFlowControl for USART {
41074107
type Cts = Cts;
41084108
type Rts = Rts;
41094109
}
@@ -4182,7 +4182,7 @@ pub mod usart3 {
41824182
impl SerialSync for USART {
41834183
type Ck = Ck;
41844184
}
4185-
impl SerialRs232 for USART {
4185+
impl SerialFlowControl for USART {
41864186
type Cts = Cts;
41874187
type Rts = Rts;
41884188
}
@@ -4293,7 +4293,7 @@ pub mod usart6 {
42934293
feature = "gpio-f446",
42944294
feature = "gpio-f469"
42954295
))]
4296-
impl SerialRs232 for USART {
4296+
impl SerialFlowControl for USART {
42974297
type Cts = Cts;
42984298
type Rts = Rts;
42994299
}
@@ -4355,7 +4355,7 @@ pub mod uart4 {
43554355
type Tx<Otype> = Tx<Otype>;
43564356
}
43574357
#[cfg(feature = "gpio-f446")]
4358-
impl SerialRs232 for UART {
4358+
impl SerialFlowControl for UART {
43594359
type Cts = Cts;
43604360
type Rts = Rts;
43614361
}
@@ -4416,7 +4416,7 @@ pub mod uart5 {
44164416
type Tx<Otype> = Tx<Otype>;
44174417
}
44184418
#[cfg(feature = "gpio-f446")]
4419-
impl SerialRs232 for UART {
4419+
impl SerialFlowControl for UART {
44204420
type Cts = Cts;
44214421
type Rts = Rts;
44224422
}

src/serial/uart_impls.rs

+63-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use crate::dma::{
1010
traits::{DMASet, PeriAddress},
1111
MemoryToPeripheral, PeripheralToMemory,
1212
};
13-
use crate::gpio::{alt::SerialAsync as CommonPins, NoPin, PushPull};
13+
use crate::gpio::{
14+
alt::{SerialAsync as CommonPins, SerialFlowControl},
15+
NoPin, PushPull,
16+
};
1417
use crate::rcc::{self, Clocks};
1518

1619
#[cfg(feature = "uart4")]
@@ -266,6 +269,20 @@ macro_rules! uartCommon {
266269
};
267270
}
268271

272+
pub trait RBFlowControlImpl {
273+
fn enable_rts(&self, state: bool);
274+
fn enable_cts(&self, state: bool);
275+
}
276+
277+
impl RBFlowControlImpl for RegisterBlockUsart {
278+
fn enable_rts(&self, state: bool) {
279+
self.cr3().modify(|_, w| w.rtse().bit(state));
280+
}
281+
fn enable_cts(&self, state: bool) {
282+
self.cr3().modify(|_, w| w.ctse().bit(state));
283+
}
284+
}
285+
269286
impl RegisterBlockImpl for RegisterBlockUsart {
270287
fn new<UART: Instance<RegisterBlock = Self>, WORD>(
271288
uart: UART,
@@ -406,6 +423,23 @@ where {
406423
uartCommon! {}
407424
}
408425

426+
#[cfg(feature = "uart4")]
427+
#[cfg(not(any(
428+
feature = "gpio-f413",
429+
feature = "gpio-f417",
430+
feature = "gpio-f427",
431+
feature = "gpio-f446",
432+
feature = "gpio-f469"
433+
)))]
434+
impl RBFlowControlImpl for RegisterBlockUart {
435+
fn enable_rts(&self, state: bool) {
436+
self.cr3().modify(|_, w| w.rtse().bit(state));
437+
}
438+
fn enable_cts(&self, state: bool) {
439+
self.cr3().modify(|_, w| w.ctse().bit(state));
440+
}
441+
}
442+
409443
#[cfg(feature = "uart4")]
410444
impl RegisterBlockImpl for RegisterBlockUart {
411445
fn new<UART: Instance<RegisterBlock = Self>, WORD>(
@@ -513,6 +547,34 @@ where {
513547
uartCommon! {}
514548
}
515549

550+
impl<UART: Instance + SerialFlowControl, WORD> Serial<UART, WORD>
551+
where
552+
UART::RegisterBlock: RBFlowControlImpl,
553+
{
554+
pub fn with_rts(self, rts: impl Into<UART::Rts>) -> Self {
555+
self.rx.usart.enable_rts(true);
556+
let _rts = rts.into();
557+
self
558+
}
559+
pub fn with_cts(self, cts: impl Into<UART::Cts>) -> Self {
560+
self.tx.usart.enable_cts(true);
561+
let _cts = cts.into();
562+
self
563+
}
564+
pub fn enable_request_to_send(&mut self) {
565+
self.rx.usart.enable_rts(true);
566+
}
567+
pub fn disable_request_to_send(&mut self) {
568+
self.rx.usart.enable_rts(false);
569+
}
570+
pub fn enable_clear_to_send(&mut self) {
571+
self.tx.usart.enable_cts(true);
572+
}
573+
pub fn disable_clear_to_send(&mut self) {
574+
self.tx.usart.enable_cts(false);
575+
}
576+
}
577+
516578
impl<UART: Instance, WORD> RxISR for Serial<UART, WORD>
517579
where
518580
Rx<UART, WORD>: RxISR,

src/uart.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
1717
use crate::pac;
1818

19-
use crate::serial::uart_impls::{RegisterBlockImpl, RegisterBlockUart};
19+
use crate::serial::uart_impls::RegisterBlockUart;
2020

2121
pub use crate::serial::{config, Error, Event, Instance, NoRx, NoTx, Rx, RxISR, Serial, Tx, TxISR};
2222
pub use config::Config;

0 commit comments

Comments
 (0)