embassy_stm32_plus/builder/can/
can2.rs

1use embassy_stm32::{bind_interrupts, can, Peripheral};
2use embassy_stm32::can::{Can, TxPin};
3use embassy_stm32::peripherals::{CAN2, PB12, PB13, PB5, PB6};
4
5bind_interrupts!(struct Irqs {
6    CAN2_RX0 => can::Rx0InterruptHandler<CAN2>;
7    CAN2_RX1 => can::Rx1InterruptHandler<CAN2>;
8    CAN2_SCE => can::SceInterruptHandler<CAN2>;
9    CAN2_TX => can::TxInterruptHandler<CAN2>;
10});
11
12/// can2 rx pin
13pub enum Can2Rx {
14    PB5(PB5),
15    PB12(PB12),
16}
17
18/// can2 tx pin
19pub enum Can2Tx {
20    PB6(PB6),
21    PB13(PB13),
22}
23
24/// can2 builder
25pub struct Can2Builder {
26    /// can2 device
27    pub can: CAN2,
28    /// can2 tx pin
29    pub tx: Can2Tx,
30    /// can2 rx pin
31    pub rx: Can2Rx,
32}
33
34/// custom method
35impl Can2Builder {
36    /// create builder
37    #[inline]
38    pub fn new(can: CAN2, tx: Can2Tx, rx: Can2Rx) -> Self {
39        Self { can, tx, rx }
40    }
41
42    /// build bx_can instance, more see [Can::new]
43    pub fn build(self) -> Can<'static> {
44        match self.tx {
45            Can2Tx::PB6(pb6) => { Self::build_rx(self.can, pb6, self.rx) }
46            Can2Tx::PB13(pb13) => { Self::build_rx(self.can, pb13, self.rx) }
47        }
48    }
49
50    /// build by rx
51    fn build_rx(can: CAN2, tx: impl Peripheral<P=impl TxPin<CAN2>> + 'static, rx: Can2Rx) -> Can<'static> {
52        match rx {
53            Can2Rx::PB5(pb5) => { Can::new(can, pb5, tx, Irqs) }
54            Can2Rx::PB12(pb12) => { Can::new(can, pb12, tx, Irqs) }
55        }
56    }
57}