2021-08-06 00:08:24 +02:00
|
|
|
use core::ops::{Deref, DerefMut};
|
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
pub use bxcan;
|
2022-07-23 14:00:19 +02:00
|
|
|
use embassy_hal_common::{into_ref, PeripheralRef};
|
2021-08-06 00:08:24 +02:00
|
|
|
|
2022-02-10 21:38:03 +01:00
|
|
|
use crate::gpio::sealed::AFType;
|
2022-06-12 22:15:44 +02:00
|
|
|
use crate::rcc::RccPeripheral;
|
2022-07-23 14:00:19 +02:00
|
|
|
use crate::{peripherals, Peripheral};
|
2021-08-06 00:08:24 +02:00
|
|
|
|
2022-07-23 01:29:35 +02:00
|
|
|
pub struct Can<'d, T: Instance> {
|
|
|
|
can: bxcan::Can<BxcanInstance<'d, T>>,
|
2021-08-06 00:08:24 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 01:29:35 +02:00
|
|
|
impl<'d, T: Instance> Can<'d, T> {
|
2022-09-22 04:29:57 +02:00
|
|
|
/// Creates a new Bxcan instance, blocking for 11 recessive bits to sync with the CAN bus.
|
2021-08-06 00:08:24 +02:00
|
|
|
pub fn new(
|
2022-07-23 14:00:19 +02:00
|
|
|
peri: impl Peripheral<P = T> + 'd,
|
|
|
|
rx: impl Peripheral<P = impl RxPin<T>> + 'd,
|
|
|
|
tx: impl Peripheral<P = impl TxPin<T>> + 'd,
|
2021-08-06 00:08:24 +02:00
|
|
|
) -> Self {
|
2022-07-23 14:00:19 +02:00
|
|
|
into_ref!(peri, rx, tx);
|
2021-08-06 00:08:24 +02:00
|
|
|
|
|
|
|
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 {
|
2022-07-23 01:29:35 +02:00
|
|
|
can: bxcan::Can::builder(BxcanInstance(peri)).enable(),
|
2021-08-06 00:08:24 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-22 04:29:57 +02:00
|
|
|
|
|
|
|
/// Creates a new Bxcan instance, keeping the peripheral in sleep mode.
|
|
|
|
/// You must call [Can::enable_non_blocking] to use the peripheral.
|
|
|
|
pub fn new_disabled(
|
|
|
|
peri: impl Peripheral<P = T> + 'd,
|
|
|
|
rx: impl Peripheral<P = impl RxPin<T>> + 'd,
|
|
|
|
tx: impl Peripheral<P = impl TxPin<T>> + 'd,
|
|
|
|
) -> Self {
|
|
|
|
into_ref!(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 {
|
|
|
|
can: bxcan::Can::builder(BxcanInstance(peri)).leave_disabled(),
|
|
|
|
}
|
|
|
|
}
|
2021-08-06 00:08:24 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 01:29:35 +02:00
|
|
|
impl<'d, T: Instance> Drop for Can<'d, T> {
|
2021-08-06 10:37:32 +02:00
|
|
|
fn drop(&mut self) {
|
|
|
|
// Cannot call `free()` because it moves the instance.
|
|
|
|
// Manually reset the peripheral.
|
2022-07-23 01:29:35 +02:00
|
|
|
unsafe { T::regs().mcr().write(|w| w.set_reset(true)) }
|
2021-08-06 10:37:32 +02:00
|
|
|
T::disable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-23 01:29:35 +02:00
|
|
|
impl<'d, T: Instance> Deref for Can<'d, T> {
|
|
|
|
type Target = bxcan::Can<BxcanInstance<'d, T>>;
|
2021-08-06 00:08:24 +02:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.can
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-23 01:29:35 +02:00
|
|
|
impl<'d, T: Instance> DerefMut for Can<'d, T> {
|
2021-08-06 00:08:24 +02:00
|
|
|
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 {
|
2022-07-23 01:29:35 +02:00
|
|
|
const REGISTERS: *mut bxcan::RegisterBlock;
|
|
|
|
|
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-07-23 14:00:19 +02:00
|
|
|
pub struct BxcanInstance<'a, T>(PeripheralRef<'a, T>);
|
2022-07-23 01:29:35 +02:00
|
|
|
|
|
|
|
unsafe impl<'d, T: Instance> bxcan::Instance for BxcanInstance<'d, T> {
|
|
|
|
const REGISTERS: *mut bxcan::RegisterBlock = T::REGISTERS;
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-07-23 01:29:35 +02:00
|
|
|
const REGISTERS: *mut bxcan::RegisterBlock = crate::pac::$inst.0 as *mut _;
|
|
|
|
|
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 {}
|
|
|
|
|
|
|
|
};
|
|
|
|
);
|
|
|
|
|
2022-02-26 01:40:43 +01:00
|
|
|
foreach_peripheral!(
|
2021-08-06 11:59:16 +02:00
|
|
|
(can, CAN) => {
|
2022-07-23 01:29:35 +02:00
|
|
|
unsafe impl<'d> bxcan::FilterOwner for BxcanInstance<'d, peripherals::CAN> {
|
2021-08-06 00:08:24 +02:00
|
|
|
const NUM_FILTER_BANKS: u8 = 14;
|
|
|
|
}
|
|
|
|
};
|
2022-02-28 22:43:21 +01:00
|
|
|
// 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.
|
2021-08-06 11:59:16 +02:00
|
|
|
(can, CAN1) => {
|
2022-03-03 20:51:34 +01:00
|
|
|
cfg_if::cfg_if! {
|
|
|
|
if #[cfg(all(
|
2022-03-05 09:43:47 +01:00
|
|
|
any(stm32l4, stm32f72, stm32f73),
|
2022-03-03 20:51:34 +01:00
|
|
|
not(any(stm32l49, stm32l4a))
|
|
|
|
))] {
|
|
|
|
// Most L4 devices and some F7 devices use the name "CAN1"
|
|
|
|
// even if there is no "CAN2" peripheral.
|
2022-07-23 01:29:35 +02:00
|
|
|
unsafe impl<'d> bxcan::FilterOwner for BxcanInstance<'d, peripherals::CAN1> {
|
2022-03-03 20:51:34 +01:00
|
|
|
const NUM_FILTER_BANKS: u8 = 14;
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-23 01:29:35 +02:00
|
|
|
unsafe impl<'d> bxcan::FilterOwner for BxcanInstance<'d, peripherals::CAN1> {
|
2022-03-03 20:51:34 +01:00
|
|
|
const NUM_FILTER_BANKS: u8 = 28;
|
|
|
|
}
|
2022-07-23 01:29:35 +02:00
|
|
|
unsafe impl<'d> bxcan::MasterInstance for BxcanInstance<'d, peripherals::CAN1> {}
|
2022-03-03 20:51:34 +01:00
|
|
|
}
|
2021-08-06 00:08:24 +02:00
|
|
|
}
|
|
|
|
};
|
2021-08-14 23:13:50 +02:00
|
|
|
(can, CAN3) => {
|
2022-07-23 01:29:35 +02:00
|
|
|
unsafe impl<'d> bxcan::FilterOwner for BxcanInstance<'d, 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);
|