embassy_stm32_plus/builder/can/
can1.rs

1use embassy_stm32::Peripheral;
2use embassy_stm32::can::{Can, TxPin};
3#[cfg(CAN)]
4use embassy_stm32::peripherals::CAN;
5#[cfg(CAN1)]
6use embassy_stm32::peripherals::CAN1;
7use embassy_stm32::peripherals::{PA11, PA12};
8#[cfg(PB8)]
9use embassy_stm32::peripherals::PB8;
10#[cfg(PB9)]
11use embassy_stm32::peripherals::PB9;
12#[cfg(any(CAN_PD0, CAN1_PD0))]
13use embassy_stm32::peripherals::PD0;
14#[cfg(any(CAN_PD1, CAN1_PD1))]
15use embassy_stm32::peripherals::PD1;
16use crate::irq_s::usb_can1::Irqs;
17
18/// can1 rx pin
19pub enum Can1Rx {
20    PA11(PA11),
21    #[cfg(PB8)]
22    PB8(PB8),
23    #[cfg(any(CAN_PD0, CAN1_PD0))]
24    PD0(PD0),
25}
26
27/// can1 tx pin
28pub enum Can1Tx {
29    PA12(PA12),
30    #[cfg(PB9)]
31    PB9(PB9),
32    #[cfg(any(CAN_PD1, CAN1_PD1))]
33    PD1(PD1),
34}
35
36/// can1 builder
37pub struct Can1Builder {
38    /// can device
39    #[cfg(CAN)]
40    pub can: CAN,
41    /// can1 device
42    #[cfg(CAN1)]
43    pub can: CAN1,
44    /// can1 tx pin
45    pub tx: Can1Tx,
46    /// can1 rx pin
47    pub rx: Can1Rx,
48}
49
50macro_rules! build_rx_fn {
51    ($can:ty) => {
52        /// build by rx
53        fn build_rx(can: $can, tx: impl Peripheral<P=impl TxPin<$can>> + 'static, rx: Can1Rx) -> Can<'static> {
54            match rx {
55                Can1Rx::PA11(pa11) => { Can::new(can, pa11, tx, Irqs) }
56                #[cfg(PB8)]
57                Can1Rx::PB8(pb8) => { Can::new(can, pb8, tx, Irqs) }
58                #[cfg(any(CAN_PD0, CAN1_PD0))]
59                Can1Rx::PD0(pd0) => { Can::new(can, pd0, tx, Irqs) }
60            }
61        }
62    };
63}
64
65/// custom method
66impl Can1Builder {
67    /// create builder
68    #[cfg(CAN)]
69    #[inline]
70    pub fn new(can: CAN, tx: Can1Tx, rx: Can1Rx) -> Self {
71        Self { can, tx, rx }
72    }
73
74    /// create builder
75    #[cfg(CAN1)]
76    #[inline]
77    pub fn new(can: CAN1, tx: Can1Tx, rx: Can1Rx) -> Self {
78        Self { can, tx, rx }
79    }
80
81    /// build bx_can instance, more see [Can::new]
82    pub fn build(self) -> Can<'static> {
83        match self.tx {
84            Can1Tx::PA12(pa12) => { Self::build_rx(self.can, pa12, self.rx) }
85            #[cfg(PB9)]
86            Can1Tx::PB9(pb9) => { Self::build_rx(self.can, pb9, self.rx) }
87            #[cfg(any(CAN_PD1, CAN1_PD1))]
88            Can1Tx::PD1(pd1) => { Self::build_rx(self.can, pd1, self.rx) }
89        }
90    }
91
92    #[cfg(CAN)]
93    build_rx_fn!(CAN);
94    #[cfg(CAN1)]
95    build_rx_fn!(CAN1);
96}