Skip to content

Commit 2c71ff6

Browse files
committed
Serial flow control
1 parent 5fc93ec commit 2c71ff6

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 6 additions & 6 deletions
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.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(crate) mod uart_impls;
2323
pub use uart_impls::Instance;
2424
use uart_impls::RegisterBlockImpl;
2525

26-
use crate::gpio::{self, PushPull};
26+
use crate::gpio::{self, alt::SerialFlowControl, PushPull};
2727

2828
use crate::pac;
2929

@@ -250,6 +250,41 @@ impl<UART: CommonPins, WORD> Serial<UART, WORD> {
250250
}
251251
}
252252

253+
impl<UART: Instance<RegisterBlock = pac::usart1::RegisterBlock> + SerialFlowControl, WORD>
254+
Serial<UART, WORD>
255+
{
256+
fn enable_rts(&self, state: bool) {
257+
self.rx.usart.cr3().modify(|_, w| w.rtse().bit(state));
258+
}
259+
260+
fn enable_cts(&self, state: bool) {
261+
self.tx.usart.cr3().modify(|_, w| w.ctse().bit(state));
262+
}
263+
264+
pub fn with_rts(self, rts: impl Into<UART::Rts>) -> Self {
265+
self.enable_rts(true);
266+
let _rts = rts.into();
267+
self
268+
}
269+
pub fn with_cts(self, cts: impl Into<UART::Cts>) -> Self {
270+
self.enable_cts(true);
271+
let _cts = cts.into();
272+
self
273+
}
274+
pub fn enable_request_to_send(&mut self) {
275+
self.enable_rts(true);
276+
}
277+
pub fn disable_request_to_send(&mut self) {
278+
self.enable_rts(false);
279+
}
280+
pub fn enable_clear_to_send(&mut self) {
281+
self.enable_cts(true);
282+
}
283+
pub fn disable_clear_to_send(&mut self) {
284+
self.enable_cts(false);
285+
}
286+
}
287+
253288
macro_rules! halUsart {
254289
($USART:ty, $Serial:ident, $Rx:ident, $Tx:ident) => {
255290
pub type $Serial<WORD = u8> = Serial<$USART, WORD>;

src/uart.rs

Lines changed: 1 addition & 1 deletion
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)