Update embassy-stm32
This commit is contained in:
@ -1,19 +1,17 @@
|
||||
use core::marker::PhantomData;
|
||||
use core::ops::{Deref, DerefMut};
|
||||
|
||||
pub use bxcan;
|
||||
use embassy_hal_common::unborrow;
|
||||
use embassy_hal_common::{unborrow, Unborrowed};
|
||||
|
||||
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>,
|
||||
pub struct Can<'d, T: Instance> {
|
||||
can: bxcan::Can<BxcanInstance<'d, T>>,
|
||||
}
|
||||
|
||||
impl<'d, T: Instance + bxcan::Instance> Can<'d, T> {
|
||||
impl<'d, T: Instance> Can<'d, T> {
|
||||
pub fn new(
|
||||
peri: impl Unborrow<Target = T> + 'd,
|
||||
rx: impl Unborrow<Target = impl RxPin<T>> + 'd,
|
||||
@ -30,32 +28,29 @@ impl<'d, T: Instance + bxcan::Instance> Can<'d, T> {
|
||||
T::reset();
|
||||
|
||||
Self {
|
||||
phantom: PhantomData,
|
||||
can: bxcan::Can::builder(peri).enable(),
|
||||
can: bxcan::Can::builder(BxcanInstance(peri)).enable(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Instance + bxcan::Instance> Drop for Can<'d, T> {
|
||||
impl<'d, T: 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));
|
||||
}
|
||||
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>;
|
||||
impl<'d, T: Instance> Deref for Can<'d, T> {
|
||||
type Target = bxcan::Can<BxcanInstance<'d, T>>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.can
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Instance + bxcan::Instance> DerefMut for Can<'d, T> {
|
||||
impl<'d, T: Instance> DerefMut for Can<'d, T> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.can
|
||||
}
|
||||
@ -63,15 +58,25 @@ impl<'d, T: Instance + bxcan::Instance> DerefMut for Can<'d, T> {
|
||||
|
||||
pub(crate) mod sealed {
|
||||
pub trait Instance {
|
||||
const REGISTERS: *mut bxcan::RegisterBlock;
|
||||
|
||||
fn regs() -> &'static crate::pac::can::Can;
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Instance: sealed::Instance + RccPeripheral {}
|
||||
|
||||
pub struct BxcanInstance<'a, T>(Unborrowed<'a, T>);
|
||||
|
||||
unsafe impl<'d, T: Instance> bxcan::Instance for BxcanInstance<'d, T> {
|
||||
const REGISTERS: *mut bxcan::RegisterBlock = T::REGISTERS;
|
||||
}
|
||||
|
||||
foreach_peripheral!(
|
||||
(can, $inst:ident) => {
|
||||
impl sealed::Instance for peripherals::$inst {
|
||||
const REGISTERS: *mut bxcan::RegisterBlock = crate::pac::$inst.0 as *mut _;
|
||||
|
||||
fn regs() -> &'static crate::pac::can::Can {
|
||||
&crate::pac::$inst
|
||||
}
|
||||
@ -79,15 +84,12 @@ foreach_peripheral!(
|
||||
|
||||
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 {
|
||||
unsafe impl<'d> bxcan::FilterOwner for BxcanInstance<'d, peripherals::CAN> {
|
||||
const NUM_FILTER_BANKS: u8 = 14;
|
||||
}
|
||||
};
|
||||
@ -102,19 +104,19 @@ foreach_peripheral!(
|
||||
))] {
|
||||
// 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 {
|
||||
unsafe impl<'d> bxcan::FilterOwner for BxcanInstance<'d, peripherals::CAN1> {
|
||||
const NUM_FILTER_BANKS: u8 = 14;
|
||||
}
|
||||
} else {
|
||||
unsafe impl bxcan::FilterOwner for peripherals::CAN1 {
|
||||
unsafe impl<'d> bxcan::FilterOwner for BxcanInstance<'d, peripherals::CAN1> {
|
||||
const NUM_FILTER_BANKS: u8 = 28;
|
||||
}
|
||||
unsafe impl bxcan::MasterInstance for peripherals::CAN1 {}
|
||||
unsafe impl<'d> bxcan::MasterInstance for BxcanInstance<'d, peripherals::CAN1> {}
|
||||
}
|
||||
}
|
||||
};
|
||||
(can, CAN3) => {
|
||||
unsafe impl bxcan::FilterOwner for peripherals::CAN3 {
|
||||
unsafe impl<'d> bxcan::FilterOwner for BxcanInstance<'d, peripherals::CAN3> {
|
||||
const NUM_FILTER_BANKS: u8 = 14;
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user