stm32f1_hal/uart/
mod.rs

1#[cfg(any(all(feature = "stm32f103", feature = "high"), feature = "connectivity"))]
2pub mod uart4;
3#[cfg(any(all(feature = "stm32f103", feature = "high"), feature = "connectivity"))]
4pub mod uart5;
5pub mod usart1;
6pub mod usart2;
7pub mod usart3;
8pub use crate::common::uart::*;
9
10use crate::{
11    Steal,
12    afio::{RemapMode, uart_remap::*},
13    rcc::{BusClock, Enable, Reset},
14};
15
16use crate::Mcu;
17
18pub trait UartInit<U> {
19    fn constrain(self, mcu: &mut Mcu) -> Uart<U>;
20}
21
22pub trait UartPeriphExt: UartPeriph + BusClock + Enable + Reset {
23    fn config(&mut self, config: Config, mcu: &mut Mcu);
24    fn enable_comm(&mut self, tx: bool, rx: bool);
25    fn set_stop_bits(&mut self, bits: StopBits);
26}
27
28// wrapper
29pub struct Uart<U> {
30    uart: U,
31}
32
33impl<U: UartPeriphExt + Steal> Uart<U> {
34    pub fn into_tx_rx<REMAP: RemapMode<U>>(
35        mut self,
36        pins: (Option<impl UartTxPin<REMAP>>, Option<impl UartRxPin<REMAP>>),
37        config: Config,
38        mcu: &mut Mcu,
39    ) -> (Option<Tx<U>>, Option<Rx<U>>) {
40        REMAP::remap(&mut mcu.afio);
41        self.uart.config(config, mcu);
42        self.uart.enable_comm(pins.0.is_some(), pins.1.is_some());
43        unsafe {
44            (
45                pins.0
46                    .map(|_| Tx::new([self.uart.steal(), self.uart.steal()])),
47                pins.1
48                    .map(|_| Rx::new([self.uart.steal(), self.uart.steal()])),
49            )
50        }
51    }
52
53    pub fn get_idle_interrupt_handler(&self) -> UartIdleInterrupt<U> {
54        UartIdleInterrupt::new(unsafe { self.uart.steal() })
55    }
56}