summaryrefslogtreecommitdiff
path: root/embassy-stm32/src/can/bxcan.rs
blob: b54fd3ff3631d2eb059ea7f7fddea80a0165e3f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};

pub use bxcan;
use embassy_hal_common::unborrow;

use crate::gpio::sealed::AFType;
use crate::rcc::RccPeripheral;
use crate::{peripherals, Unborrow};

pub struct Can<'d, T: Instance + bxcan::Instance> {
    phantom: PhantomData<&'d mut T>,
    can: bxcan::Can<T>,
}

impl<'d, T: Instance + bxcan::Instance> Can<'d, T> {
    pub fn new(
        peri: impl Unborrow<Target = T> + 'd,
        rx: impl Unborrow<Target = impl RxPin<T>> + 'd,
        tx: impl Unborrow<Target = impl TxPin<T>> + 'd,
    ) -> Self {
        unborrow!(peri, rx, tx);

        unsafe {
            rx.set_as_af(rx.af_num(), AFType::Input);
            tx.set_as_af(tx.af_num(), AFType::OutputPushPull);
        }

        T::enable();
        T::reset();

        Self {
            phantom: PhantomData,
            can: bxcan::Can::builder(peri).enable(),
        }
    }
}

impl<'d, T: Instance + bxcan::Instance> Drop for Can<'d, T> {
    fn drop(&mut self) {
        // Cannot call `free()` because it moves the instance.
        // Manually reset the peripheral.
        unsafe {
            T::regs().mcr().write(|w| w.set_reset(true));
        }
        T::disable();
    }
}

impl<'d, T: Instance + bxcan::Instance> Deref for Can<'d, T> {
    type Target = bxcan::Can<T>;

    fn deref(&self) -> &Self::Target {
        &self.can
    }
}

impl<'d, T: Instance + bxcan::Instance> DerefMut for Can<'d, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.can
    }
}

pub(crate) mod sealed {
    pub trait Instance {
        fn regs() -> &'static crate::pac::can::Can;
    }
}

pub trait Instance: sealed::Instance + RccPeripheral {}

foreach_peripheral!(
    (can, $inst:ident) => {
        impl sealed::Instance for peripherals::$inst {
            fn regs() -> &'static crate::pac::can::Can {
                &crate::pac::$inst
            }
        }

        impl Instance for peripherals::$inst {}

        unsafe impl bxcan::Instance for peripherals::$inst {
            const REGISTERS: *mut bxcan::RegisterBlock = crate::pac::$inst.0 as *mut _;
        }
    };
);

foreach_peripheral!(
    (can, CAN) => {
        unsafe impl bxcan::FilterOwner for peripherals::CAN {
            const NUM_FILTER_BANKS: u8 = 14;
        }
    };
    // CAN1 and CAN2 is a combination of master and slave instance.
    // CAN1 owns the filter bank and needs to be enabled in order
    // for CAN2 to receive messages.
    (can, CAN1) => {
        cfg_if::cfg_if! {
            if #[cfg(all(
                any(stm32l4, stm32f72, stm32f73),
                not(any(stm32l49, stm32l4a))
            ))] {
                // Most L4 devices and some F7 devices use the name "CAN1"
                // even if there is no "CAN2" peripheral.
                unsafe impl bxcan::FilterOwner for peripherals::CAN1 {
                    const NUM_FILTER_BANKS: u8 = 14;
                }
            } else {
                unsafe impl bxcan::FilterOwner for peripherals::CAN1 {
                    const NUM_FILTER_BANKS: u8 = 28;
                }
                unsafe impl bxcan::MasterInstance for peripherals::CAN1 {}
            }
        }
    };
    (can, CAN3) => {
        unsafe impl bxcan::FilterOwner for peripherals::CAN3 {
            const NUM_FILTER_BANKS: u8 = 14;
        }
    };
);

pin_trait!(RxPin, Instance);
pin_trait!(TxPin, Instance);