embassy_stm32_plus/builder/uart/uart3/
rx.rs

1use embassy_stm32::mode::Async;
2use embassy_stm32::Peripheral;
3use embassy_stm32::peripherals::{DMA1_CH3, PB11, PB14, USART3};
4#[cfg(PC11)]
5use embassy_stm32::peripherals::PC11;
6#[cfg(PD12)]
7use embassy_stm32::peripherals::PD12;
8#[cfg(PD9)]
9use embassy_stm32::peripherals::PD9;
10use embassy_stm32::usart::{Config, ConfigError, RxPin, UartRx};
11use crate::builder::uart::base::UartBase;
12use crate::builder::uart::uart3::Irqs;
13
14/// uart3 rx pin
15pub enum Uart3Rx {
16    PB11(PB11),
17    #[cfg(PC11)]
18    PC11(PC11),
19    #[cfg(PD9)]
20    PD9(PD9),
21}
22
23/// uart3 rtx pin
24pub enum Uart3Rts {
25    PB14(PB14),
26    #[cfg(PD12)]
27    PD12(PD12),
28}
29
30/// uart3 rx builder
31pub struct Uart3RxBuilder {
32    /// uart3 base device
33    pub base: UartBase<USART3>,
34    /// rx pin
35    pub rx: Uart3Rx,
36    /// use rts
37    pub rts: Option<Uart3Rts>,
38}
39
40/// custom method
41impl Uart3RxBuilder {
42    /// create builder
43    #[inline]
44    pub fn new(uart: USART3, rx: Uart3Rx) -> Self {
45        Self { base: UartBase::new(uart), rx, rts: None }
46    }
47
48    /// set uart config
49    #[inline]
50    pub fn config(mut self, config: Config) -> Self {
51        self.base.set_config(config);
52        self
53    }
54
55    /// set rts
56    #[inline]
57    pub fn rts(mut self, rts: Uart3Rts) -> Self {
58        self.rts = Some(rts);
59        self
60    }
61
62    /// build uart rx that supports read data
63    pub fn build(self, rx_dma: DMA1_CH3) -> Result<UartRx<'static, Async>, ConfigError> {
64        match self.rx {
65            Uart3Rx::PB11(pb11) => { Self::build_rts(pb11, rx_dma, self.base, self.rts) }
66            #[cfg(PC11)]
67            Uart3Rx::PC11(pc11) => { Self::build_rts(pc11, rx_dma, self.base, self.rts) }
68            #[cfg(PD9)]
69            Uart3Rx::PD9(pd9) => { Self::build_rts(pd9, rx_dma, self.base, self.rts) }
70        }
71    }
72
73    /// build by rts
74    fn build_rts(
75        rx: impl Peripheral<P=impl RxPin<USART3>> + 'static,
76        rx_dma: DMA1_CH3,
77        base: UartBase<USART3>,
78        rts: Option<Uart3Rts>)
79        -> Result<UartRx<'static, Async>, ConfigError> {
80        let rts = crate::match_some_return!(rts,
81            UartRx::new(base.uart, Irqs, rx, rx_dma, base.config.unwrap_or_default()));
82
83        match rts {
84            Uart3Rts::PB14(pb14) => { UartRx::new_with_rts(base.uart, Irqs, rx, pb14, rx_dma, base.config.unwrap_or_default()) }
85            #[cfg(PD12)]
86            Uart3Rts::PD12(pd12) => { UartRx::new_with_rts(base.uart, Irqs, rx, pd12, rx_dma, base.config.unwrap_or_default()) }
87        }
88    }
89}