Skip to content

Commit 74ec6ac

Browse files
committed
Serial flow control
1 parent 5fc93ec commit 74ec6ac

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
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

+44-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ use nb::block;
66
use super::{
77
config, CFlag, Error, Event, Flag, Rx, RxISR, RxListen, Serial, SerialExt, Tx, TxISR, TxListen,
88
};
9-
use crate::dma::{
10-
traits::{DMASet, PeriAddress},
11-
MemoryToPeripheral, PeripheralToMemory,
12-
};
13-
use crate::gpio::{alt::SerialAsync as CommonPins, NoPin, PushPull};
9+
use crate::gpio::{alt::SerialAsync as CommonPins, NoPin, PushPull, SerialFlowControl};
1410
use crate::rcc::{self, Clocks};
11+
use crate::{
12+
dma::{
13+
traits::{DMASet, PeriAddress},
14+
MemoryToPeripheral, PeripheralToMemory,
15+
},
16+
gpio::alt::SerialFlowControl,
17+
};
1518

1619
#[cfg(feature = "uart4")]
1720
pub(crate) use crate::pac::uart4::RegisterBlock as RegisterBlockUart;
@@ -147,6 +150,9 @@ pub trait RegisterBlockImpl: crate::Sealed {
147150
self.listen_event(Some(Event::TxEmpty.into()), None)
148151
}
149152

153+
fn enable_rts(&self, state: bool);
154+
fn enable_cts(&self, state: bool);
155+
150156
// PeriAddress
151157
fn peri_address(&self) -> u32;
152158
}
@@ -260,6 +266,14 @@ macro_rules! uartCommon {
260266
});
261267
}
262268

269+
fn enable_rts(&self, state: bool) {
270+
self.cr3().modify(|_, w| w.rtse().bit(state));
271+
}
272+
273+
fn enable_cts(&self, state: bool) {
274+
self.cr3().modify(|_, w| w.ctse().bit(state));
275+
}
276+
263277
fn peri_address(&self) -> u32 {
264278
self.dr().as_ptr() as u32
265279
}
@@ -513,6 +527,31 @@ where {
513527
uartCommon! {}
514528
}
515529

530+
impl<UART: Instance + SerialFlowControl, WORD> Serial<UART, WORD> {
531+
pub fn with_rts(self, rts: impl Into<UART::Rts>) -> Self {
532+
self.rx.usart.enable_rts(true);
533+
let _rts = rts.into();
534+
self
535+
}
536+
pub fn with_cts(self, cts: impl Into<UART::Cts>) -> Self {
537+
self.tx.usart.enable_cts(true);
538+
let _cts = cts.into();
539+
self
540+
}
541+
pub fn enable_request_to_send(&mut self) {
542+
self.rx.usart.enable_rts(true);
543+
}
544+
pub fn disable_request_to_send(&mut self) {
545+
self.rx.usart.enable_rts(false);
546+
}
547+
pub fn enable_clear_to_send(&mut self) {
548+
self.tx.usart.enable_cts(true);
549+
}
550+
pub fn disable_clear_to_send(&mut self) {
551+
self.tx.usart.enable_cts(false);
552+
}
553+
}
554+
516555
impl<UART: Instance, WORD> RxISR for Serial<UART, WORD>
517556
where
518557
Rx<UART, WORD>: RxISR,

0 commit comments

Comments
 (0)