diff options
| -rw-r--r-- | embassy-stm32/src/bxcan.rs | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/embassy-stm32/src/bxcan.rs b/embassy-stm32/src/bxcan.rs index 59dad20c2..8dd7459be 100644 --- a/embassy-stm32/src/bxcan.rs +++ b/embassy-stm32/src/bxcan.rs | |||
| @@ -26,12 +26,12 @@ impl<'d, T: Instance + bxcan::Instance> Can<'d, T> { | |||
| 26 | unsafe { | 26 | unsafe { |
| 27 | rx.set_as_af(rx.af_num()); | 27 | rx.set_as_af(rx.af_num()); |
| 28 | tx.set_as_af(tx.af_num()); | 28 | tx.set_as_af(tx.af_num()); |
| 29 | |||
| 30 | T::enable(); | ||
| 31 | T::reset(); | ||
| 32 | // TODO: CAN2 also required CAN1 clock | ||
| 33 | } | 29 | } |
| 34 | 30 | ||
| 31 | T::enable(); | ||
| 32 | T::reset(); | ||
| 33 | // TODO: CAN2 also required CAN1 clock | ||
| 34 | |||
| 35 | Self { | 35 | Self { |
| 36 | phantom: PhantomData, | 36 | phantom: PhantomData, |
| 37 | can: bxcan::Can::new(peri), | 37 | can: bxcan::Can::new(peri), |
| @@ -39,6 +39,17 @@ impl<'d, T: Instance + bxcan::Instance> Can<'d, T> { | |||
| 39 | } | 39 | } |
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | impl<'d, T: Instance + bxcan::Instance> Drop for Can<'d, T> { | ||
| 43 | fn drop(&mut self) { | ||
| 44 | // Cannot call `free()` because it moves the instance. | ||
| 45 | // Manually reset the peripheral. | ||
| 46 | unsafe { | ||
| 47 | T::regs().mcr().write(|w| w.set_reset(true)); | ||
| 48 | } | ||
| 49 | T::disable(); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 42 | impl<'d, T: Instance + bxcan::Instance> Deref for Can<'d, T> { | 53 | impl<'d, T: Instance + bxcan::Instance> Deref for Can<'d, T> { |
| 43 | type Target = bxcan::Can<T>; | 54 | type Target = bxcan::Can<T>; |
| 44 | 55 | ||
| @@ -56,7 +67,9 @@ impl<'d, T: Instance + bxcan::Instance> DerefMut for Can<'d, T> { | |||
| 56 | pub(crate) mod sealed { | 67 | pub(crate) mod sealed { |
| 57 | use super::*; | 68 | use super::*; |
| 58 | 69 | ||
| 59 | pub trait Instance {} | 70 | pub trait Instance { |
| 71 | fn regs() -> &'static crate::pac::bxcan::Can; | ||
| 72 | } | ||
| 60 | 73 | ||
| 61 | pub trait RxPin<T: Instance>: Pin { | 74 | pub trait RxPin<T: Instance>: Pin { |
| 62 | fn af_num(&self) -> u8; | 75 | fn af_num(&self) -> u8; |
| @@ -73,7 +86,11 @@ pub trait TxPin<T: Instance>: sealed::TxPin<T> {} | |||
| 73 | 86 | ||
| 74 | crate::pac::peripherals!( | 87 | crate::pac::peripherals!( |
| 75 | (bxcan, $inst:ident) => { | 88 | (bxcan, $inst:ident) => { |
| 76 | impl sealed::Instance for peripherals::$inst {} | 89 | impl sealed::Instance for peripherals::$inst { |
| 90 | fn regs() -> &'static crate::pac::bxcan::Can { | ||
| 91 | &crate::pac::$inst | ||
| 92 | } | ||
| 93 | } | ||
| 77 | 94 | ||
| 78 | impl Instance for peripherals::$inst {} | 95 | impl Instance for peripherals::$inst {} |
| 79 | 96 | ||
