2021-08-06 00:08:24 +02:00
|
|
|
use core::marker::PhantomData;
|
|
|
|
use core::ops::{Deref, DerefMut};
|
|
|
|
|
|
|
|
use embassy::util::Unborrow;
|
|
|
|
use embassy_hal_common::unborrow;
|
|
|
|
|
2022-02-10 21:38:03 +01:00
|
|
|
use crate::gpio::sealed::AFType;
|
2021-08-06 00:08:24 +02:00
|
|
|
use crate::{peripherals, rcc::RccPeripheral};
|
|
|
|
|
|
|
|
pub use bxcan::*;
|
|
|
|
|
|
|
|
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 {
|
2022-02-24 00:20:43 +01:00
|
|
|
rx.set_as_af(rx.af_num(), AFType::Input);
|
2022-02-10 21:38:03 +01:00
|
|
|
tx.set_as_af(tx.af_num(), AFType::OutputPushPull);
|
2021-08-06 00:08:24 +02:00
|
|
|
}
|
|
|
|
|
2021-08-06 10:37:32 +02:00
|
|
|
T::enable();
|
|
|
|
T::reset();
|
|
|
|
|
2021-08-06 00:08:24 +02:00
|
|
|
Self {
|
|
|
|
phantom: PhantomData,
|
2021-11-15 17:08:51 +01:00
|
|
|
can: bxcan::Can::builder(peri).enable(),
|
2021-08-06 00:08:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 10:37:32 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 00:08:24 +02:00
|
|
|
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 {
|
2021-08-06 10:37:32 +02:00
|
|
|
pub trait Instance {
|
2021-08-06 11:59:16 +02:00
|
|
|
fn regs() -> &'static crate::pac::can::Can;
|
2021-08-06 10:37:32 +02:00
|
|
|
}
|
2021-08-06 00:08:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Instance: sealed::Instance + RccPeripheral {}
|
|
|
|
|
2022-02-26 01:40:43 +01:00
|
|
|
foreach_peripheral!(
|
2021-08-06 11:59:16 +02:00
|
|
|
(can, $inst:ident) => {
|
2021-08-06 10:37:32 +02:00
|
|
|
impl sealed::Instance for peripherals::$inst {
|
2021-08-06 11:59:16 +02:00
|
|
|
fn regs() -> &'static crate::pac::can::Can {
|
2021-08-06 10:37:32 +02:00
|
|
|
&crate::pac::$inst
|
|
|
|
}
|
|
|
|
}
|
2021-08-06 00:08:24 +02:00
|
|
|
|
|
|
|
impl Instance for peripherals::$inst {}
|
|
|
|
|
|
|
|
unsafe impl bxcan::Instance for peripherals::$inst {
|
|
|
|
const REGISTERS: *mut bxcan::RegisterBlock = crate::pac::$inst.0 as *mut _;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
);
|
|
|
|
|
2022-02-26 01:40:43 +01:00
|
|
|
foreach_peripheral!(
|
2021-08-06 11:59:16 +02:00
|
|
|
(can, CAN) => {
|
2021-11-29 02:10:06 +01:00
|
|
|
unsafe impl bxcan::FilterOwner for peripherals::CAN {
|
2021-08-06 00:08:24 +02:00
|
|
|
const NUM_FILTER_BANKS: u8 = 14;
|
|
|
|
}
|
|
|
|
};
|
2021-08-14 23:13:50 +02:00
|
|
|
// Only correct when CAN2 also exists… Fix on yaml level?
|
|
|
|
// There are only 14 filter banks when CAN2 is not available.
|
2021-08-06 11:59:16 +02:00
|
|
|
(can, CAN1) => {
|
2021-08-06 00:08:24 +02:00
|
|
|
unsafe impl bxcan::FilterOwner for peripherals::CAN1 {
|
2021-08-14 23:13:50 +02:00
|
|
|
const NUM_FILTER_BANKS: u8 = 28;
|
2021-08-06 00:08:24 +02:00
|
|
|
}
|
|
|
|
};
|
2021-08-06 11:59:16 +02:00
|
|
|
(can, CAN2) => {
|
2021-08-14 23:13:50 +02:00
|
|
|
// CAN2 is always a slave instance where CAN1 is the master instance
|
2021-08-06 00:08:24 +02:00
|
|
|
unsafe impl bxcan::MasterInstance for peripherals::CAN1 {}
|
|
|
|
};
|
2021-08-14 23:13:50 +02:00
|
|
|
(can, CAN3) => {
|
2021-10-19 15:36:41 +02:00
|
|
|
unsafe impl bxcan::FilterOwner for peripherals::CAN3 {
|
2021-08-14 23:13:50 +02:00
|
|
|
const NUM_FILTER_BANKS: u8 = 14;
|
|
|
|
}
|
|
|
|
};
|
2021-08-06 00:08:24 +02:00
|
|
|
);
|
|
|
|
|
2022-02-10 21:38:03 +01:00
|
|
|
pin_trait!(RxPin, Instance);
|
|
|
|
pin_trait!(TxPin, Instance);
|