From 191a589820159a69fbc72f996c4be91093dc2b44 Mon Sep 17 00:00:00 2001 From: Timo Kröger Date: Fri, 6 Aug 2021 11:59:16 +0200 Subject: bxcan: namechange "bxcan_v1" -> "can_bxcan" --- embassy-stm32/src/bxcan.rs | 145 ---------------------------------------- embassy-stm32/src/can/bxcan.rs | 140 ++++++++++++++++++++++++++++++++++++++ embassy-stm32/src/can/mod.rs | 5 ++ embassy-stm32/src/lib.rs | 4 +- examples/stm32f4/src/bin/can.rs | 2 +- 5 files changed, 148 insertions(+), 148 deletions(-) delete mode 100644 embassy-stm32/src/bxcan.rs create mode 100644 embassy-stm32/src/can/bxcan.rs create mode 100644 embassy-stm32/src/can/mod.rs diff --git a/embassy-stm32/src/bxcan.rs b/embassy-stm32/src/bxcan.rs deleted file mode 100644 index 8dd7459be..000000000 --- a/embassy-stm32/src/bxcan.rs +++ /dev/null @@ -1,145 +0,0 @@ -use core::marker::PhantomData; -use core::ops::{Deref, DerefMut}; - -use embassy::util::Unborrow; -use embassy_hal_common::unborrow; - -use crate::gpio::Pin; -use crate::{peripherals, rcc::RccPeripheral}; - -pub use bxcan::*; - -pub struct Can<'d, T: Instance + bxcan::Instance> { - phantom: PhantomData<&'d mut T>, - can: bxcan::Can, -} - -impl<'d, T: Instance + bxcan::Instance> Can<'d, T> { - pub fn new( - peri: impl Unborrow + 'd, - // irq: impl Unborrow + 'd, - rx: impl Unborrow> + 'd, - tx: impl Unborrow> + 'd, - ) -> Self { - unborrow!(peri, rx, tx); - - unsafe { - rx.set_as_af(rx.af_num()); - tx.set_as_af(tx.af_num()); - } - - T::enable(); - T::reset(); - // TODO: CAN2 also required CAN1 clock - - Self { - phantom: PhantomData, - can: bxcan::Can::new(peri), - } - } -} - -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; - - 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 { - use super::*; - - pub trait Instance { - fn regs() -> &'static crate::pac::bxcan::Can; - } - - pub trait RxPin: Pin { - fn af_num(&self) -> u8; - } - - pub trait TxPin: Pin { - fn af_num(&self) -> u8; - } -} - -pub trait Instance: sealed::Instance + RccPeripheral {} -pub trait RxPin: sealed::RxPin {} -pub trait TxPin: sealed::TxPin {} - -crate::pac::peripherals!( - (bxcan, $inst:ident) => { - impl sealed::Instance for peripherals::$inst { - fn regs() -> &'static crate::pac::bxcan::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 _; - } - }; - // (bxcan, CAN) => { - // unsafe impl bxcan::FilterOwner for Can { - // const NUM_FILTER_BANKS: u8 = 14; - // } - // }; -); - -crate::pac::peripherals!( - // TODO: rename CAN to CAN1 on yaml level?? - (bxcan, CAN) => { - unsafe impl bxcan::FilterOwner for peripherals::CAN { - const NUM_FILTER_BANKS: u8 = 14; - } - }; - (bxcan, CAN1) => { - unsafe impl bxcan::FilterOwner for peripherals::CAN1 { - const NUM_FILTER_BANKS: u8 = 14; - } - }; - (bxcan, CAN2) => { - // TODO: when CAN2 existis, we have 28 filter banks - unsafe impl bxcan::MasterInstance for peripherals::CAN1 {} - }; -); - -macro_rules! impl_pin { - ($inst:ident, $pin:ident, $signal:ident, $af:expr) => { - impl $signal for peripherals::$pin {} - - impl sealed::$signal for peripherals::$pin { - fn af_num(&self) -> u8 { - $af - } - } - }; -} - -crate::pac::peripheral_pins!( - ($inst:ident, bxcan, CAN, $pin:ident, TX, $af:expr) => { - impl_pin!($inst, $pin, TxPin, $af); - }; - ($inst:ident, bxcan, CAN, $pin:ident, RX, $af:expr) => { - impl_pin!($inst, $pin, RxPin, $af); - }; -); diff --git a/embassy-stm32/src/can/bxcan.rs b/embassy-stm32/src/can/bxcan.rs new file mode 100644 index 000000000..33f3cebff --- /dev/null +++ b/embassy-stm32/src/can/bxcan.rs @@ -0,0 +1,140 @@ +use core::marker::PhantomData; +use core::ops::{Deref, DerefMut}; + +use embassy::util::Unborrow; +use embassy_hal_common::unborrow; + +use crate::gpio::Pin; +use crate::{peripherals, rcc::RccPeripheral}; + +pub use bxcan::*; + +pub struct Can<'d, T: Instance + bxcan::Instance> { + phantom: PhantomData<&'d mut T>, + can: bxcan::Can, +} + +impl<'d, T: Instance + bxcan::Instance> Can<'d, T> { + pub fn new( + peri: impl Unborrow + 'd, + // irq: impl Unborrow + 'd, + rx: impl Unborrow> + 'd, + tx: impl Unborrow> + 'd, + ) -> Self { + unborrow!(peri, rx, tx); + + unsafe { + rx.set_as_af(rx.af_num()); + tx.set_as_af(tx.af_num()); + } + + T::enable(); + T::reset(); + // TODO: CAN2 also required CAN1 clock + + Self { + phantom: PhantomData, + can: bxcan::Can::new(peri), + } + } +} + +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; + + 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 { + use super::*; + + pub trait Instance { + fn regs() -> &'static crate::pac::can::Can; + } + + pub trait RxPin: Pin { + fn af_num(&self) -> u8; + } + + pub trait TxPin: Pin { + fn af_num(&self) -> u8; + } +} + +pub trait Instance: sealed::Instance + RccPeripheral {} +pub trait RxPin: sealed::RxPin {} +pub trait TxPin: sealed::TxPin {} + +crate::pac::peripherals!( + (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 _; + } + }; +); + +crate::pac::peripherals!( + // TODO: rename CAN to CAN1 on yaml level?? + (can, CAN) => { + unsafe impl bxcan::FilterOwner for peripherals::CAN { + const NUM_FILTER_BANKS: u8 = 14; + } + }; + (can, CAN1) => { + unsafe impl bxcan::FilterOwner for peripherals::CAN1 { + const NUM_FILTER_BANKS: u8 = 14; + } + }; + (can, CAN2) => { + // TODO: when CAN2 existis, we have 28 filter banks + unsafe impl bxcan::MasterInstance for peripherals::CAN1 {} + }; +); + +macro_rules! impl_pin { + ($inst:ident, $pin:ident, $signal:ident, $af:expr) => { + impl $signal for peripherals::$pin {} + + impl sealed::$signal for peripherals::$pin { + fn af_num(&self) -> u8 { + $af + } + } + }; +} + +crate::pac::peripheral_pins!( + ($inst:ident, can, CAN, $pin:ident, TX, $af:expr) => { + impl_pin!($inst, $pin, TxPin, $af); + }; + ($inst:ident, can, CAN, $pin:ident, RX, $af:expr) => { + impl_pin!($inst, $pin, RxPin, $af); + }; +); diff --git a/embassy-stm32/src/can/mod.rs b/embassy-stm32/src/can/mod.rs new file mode 100644 index 000000000..c7e2e620a --- /dev/null +++ b/embassy-stm32/src/can/mod.rs @@ -0,0 +1,5 @@ +#![macro_use] + +#[cfg_attr(can_bxcan, path = "bxcan.rs")] +mod _version; +pub use _version::*; diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index ca5c4d77a..af4ab95fd 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs @@ -27,8 +27,8 @@ mod time_driver; #[cfg(adc)] pub mod adc; -#[cfg(bxcan)] -pub mod bxcan; +#[cfg(can)] +pub mod can; #[cfg(dac)] pub mod dac; #[cfg(dbgmcu)] diff --git a/examples/stm32f4/src/bin/can.rs b/examples/stm32f4/src/bin/can.rs index d35a81d1d..04f3417c6 100644 --- a/examples/stm32f4/src/bin/can.rs +++ b/examples/stm32f4/src/bin/can.rs @@ -8,7 +8,7 @@ mod example_common; use cortex_m_rt::entry; -use embassy_stm32::bxcan::{Can, Frame, StandardId}; +use embassy_stm32::can::{Can, Frame, StandardId}; use embassy_stm32::dbgmcu::Dbgmcu; use example_common::*; -- cgit