stm32f4xx_hal/
uart.rs

1//!
2//! Asynchronous serial communication using UART peripherals
3//!
4//! # Word length
5//!
6//! By default, the UART/UART uses 8 data bits. The `Serial`, `Rx`, and `Tx` structs implement
7//! the embedded-hal read and write traits with `u8` as the word type.
8//!
9//! You can also configure the hardware to use 9 data bits with the `Config` `wordlength_9()`
10//! function. After creating a `Serial` with this option, use the `with_u16_data()` function to
11//! convert the `Serial<_, u8>` object into a `Serial<_, u16>` that can send and receive `u16`s.
12//!
13//! In this mode, the `Serial<_, u16>`, `Rx<_, u16>`, and `Tx<_, u16>` structs instead implement
14//! the embedded-hal read and write traits with `u16` as the word type. You can use these
15//! implementations for 9-bit words.
16
17use crate::pac;
18
19use crate::serial::uart_impls::RegisterBlockUart;
20
21pub use crate::serial::{config, Error, Event, Instance, NoRx, NoTx, Rx, RxISR, Serial, Tx, TxISR};
22pub use config::Config;
23
24macro_rules! halUart {
25    ($UART:ty, $Serial:ident, $Rx:ident, $Tx:ident) => {
26        pub type $Serial<WORD = u8> = Serial<$UART, WORD>;
27        pub type $Tx<WORD = u8> = Tx<$UART, WORD>;
28        pub type $Rx<WORD = u8> = Rx<$UART, WORD>;
29
30        impl Instance for $UART {
31            fn set_stopbits(&self, bits: config::StopBits) {
32                use crate::pac::uart4::cr2::STOP;
33                use config::StopBits;
34
35                /*
36                    StopBits::STOP0P5 and StopBits::STOP1P5 aren't supported when using UART
37                    STOP_A::STOP1 and STOP_A::STOP2 will be used, respectively
38                */
39                self.cr2().write(|w| {
40                    w.stop().variant(match bits {
41                        StopBits::STOP0P5 => STOP::Stop1,
42                        StopBits::STOP1 => STOP::Stop1,
43                        StopBits::STOP1P5 => STOP::Stop2,
44                        StopBits::STOP2 => STOP::Stop2,
45                    })
46                });
47            }
48        }
49
50        impl crate::Ptr for $UART {
51            type RB = RegisterBlockUart;
52
53            fn ptr() -> *const Self::RB {
54                Self::ptr()
55            }
56        }
57
58        impl crate::Steal for $UART {
59            unsafe fn steal() -> Self {
60                Self::steal()
61            }
62        }
63    };
64}
65
66#[cfg(feature = "uart4")]
67halUart! { pac::UART4, Serial4, Rx4, Tx4 }
68#[cfg(feature = "uart5")]
69halUart! { pac::UART5, Serial5, Rx5, Tx5 }
70#[cfg(feature = "uart7")]
71halUart! { pac::UART7, Serial7, Rx7, Tx7 }
72#[cfg(feature = "uart8")]
73halUart! { pac::UART8, Serial8, Rx8, Tx8 }
74#[cfg(feature = "uart9")]
75halUart! { pac::UART9, Serial9, Rx9, Tx9 }
76#[cfg(feature = "uart10")]
77halUart! { pac::UART10, Serial10, Rx10, Tx10 }