1use crate::gpio::{gpioa, gpiob};
15use crate::gpio::{PushPull, AF9};
16use crate::pac;
17
18use crate::rcc::{Enable, Reset, APB1};
19
20use bxcan::RegisterBlock;
21
22use cfg_if::cfg_if;
23
24pub trait RxPin: crate::private::Sealed {}
26
27pub trait TxPin: crate::private::Sealed {}
29
30impl RxPin for gpioa::PA11<AF9<PushPull>> {}
31impl TxPin for gpioa::PA12<AF9<PushPull>> {}
32impl RxPin for gpiob::PB8<AF9<PushPull>> {}
33impl TxPin for gpiob::PB9<AF9<PushPull>> {}
34
35cfg_if! {
36 if #[cfg(any(feature = "gpio-f303", feature = "gpio-f303e", feature = "gpio-f373"))] {
37 use crate::gpio::{gpiod, AF7};
38 impl RxPin for gpiod::PD0<AF7<PushPull>> {}
39 impl TxPin for gpiod::PD1<AF7<PushPull>> {}
40 }
41}
42
43pub struct Can<Tx, Rx> {
47 can: pac::CAN,
48 tx: Tx,
49 rx: Rx,
50}
51
52impl<Tx, Rx> Can<Tx, Rx>
53where
54 Tx: TxPin,
55 Rx: RxPin,
56{
57 pub fn new(can: pac::CAN, tx: Tx, rx: Rx, apb1: &mut APB1) -> Self {
59 pac::CAN::enable(apb1);
60 pac::CAN::reset(apb1);
61
62 Can { can, tx, rx }
63 }
64
65 pub fn free(self) -> (pac::CAN, Tx, Rx) {
67 (self.can, self.tx, self.rx)
68 }
69}
70
71unsafe impl<Tx, Rx> bxcan::Instance for Can<Tx, Rx> {
73 const REGISTERS: *mut RegisterBlock = pac::CAN::ptr() as *mut _;
74}
75
76unsafe impl<Tx, Rx> bxcan::FilterOwner for Can<Tx, Rx> {
78 const NUM_FILTER_BANKS: u8 = 28;
79}