diff options
| -rw-r--r-- | embassy-stm32/src/can/fdcan.rs | 66 | ||||
| -rw-r--r-- | embassy-stm32/src/can/mod.rs | 1 |
2 files changed, 67 insertions, 0 deletions
diff --git a/embassy-stm32/src/can/fdcan.rs b/embassy-stm32/src/can/fdcan.rs new file mode 100644 index 000000000..c31a7fc63 --- /dev/null +++ b/embassy-stm32/src/can/fdcan.rs | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | pub use bxcan; | ||
| 2 | use embassy_hal_common::PeripheralRef; | ||
| 3 | |||
| 4 | use crate::peripherals; | ||
| 5 | |||
| 6 | pub(crate) mod sealed { | ||
| 7 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; | ||
| 8 | use embassy_sync::channel::Channel; | ||
| 9 | use embassy_sync::waitqueue::AtomicWaker; | ||
| 10 | |||
| 11 | pub struct State { | ||
| 12 | pub tx_waker: AtomicWaker, | ||
| 13 | pub err_waker: AtomicWaker, | ||
| 14 | pub rx_queue: Channel<CriticalSectionRawMutex, (u16, bxcan::Frame), 32>, | ||
| 15 | } | ||
| 16 | |||
| 17 | impl State { | ||
| 18 | pub const fn new() -> Self { | ||
| 19 | Self { | ||
| 20 | tx_waker: AtomicWaker::new(), | ||
| 21 | err_waker: AtomicWaker::new(), | ||
| 22 | rx_queue: Channel::new(), | ||
| 23 | } | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | pub trait Instance { | ||
| 28 | const REGISTERS: *mut bxcan::RegisterBlock; | ||
| 29 | |||
| 30 | fn regs() -> &'static crate::pac::can::Fdcan; | ||
| 31 | fn state() -> &'static State; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | pub trait InterruptableInstance {} | ||
| 36 | pub trait Instance: sealed::Instance + InterruptableInstance + 'static {} | ||
| 37 | |||
| 38 | pub struct BxcanInstance<'a, T>(PeripheralRef<'a, T>); | ||
| 39 | |||
| 40 | unsafe impl<'d, T: Instance> bxcan::Instance for BxcanInstance<'d, T> { | ||
| 41 | const REGISTERS: *mut bxcan::RegisterBlock = T::REGISTERS; | ||
| 42 | } | ||
| 43 | |||
| 44 | foreach_peripheral!( | ||
| 45 | (can, $inst:ident) => { | ||
| 46 | impl sealed::Instance for peripherals::$inst { | ||
| 47 | const REGISTERS: *mut bxcan::RegisterBlock = crate::pac::$inst.as_ptr() as *mut _; | ||
| 48 | |||
| 49 | fn regs() -> &'static crate::pac::can::Fdcan { | ||
| 50 | &crate::pac::$inst | ||
| 51 | } | ||
| 52 | |||
| 53 | fn state() -> &'static sealed::State { | ||
| 54 | static STATE: sealed::State = sealed::State::new(); | ||
| 55 | &STATE | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | impl Instance for peripherals::$inst {} | ||
| 60 | |||
| 61 | impl InterruptableInstance for peripherals::$inst {} | ||
| 62 | }; | ||
| 63 | ); | ||
| 64 | |||
| 65 | pin_trait!(RxPin, Instance); | ||
| 66 | pin_trait!(TxPin, Instance); | ||
diff --git a/embassy-stm32/src/can/mod.rs b/embassy-stm32/src/can/mod.rs index c7e2e620a..4ff5aa0de 100644 --- a/embassy-stm32/src/can/mod.rs +++ b/embassy-stm32/src/can/mod.rs | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | #![macro_use] | 1 | #![macro_use] |
| 2 | 2 | ||
| 3 | #[cfg_attr(can_bxcan, path = "bxcan.rs")] | 3 | #[cfg_attr(can_bxcan, path = "bxcan.rs")] |
| 4 | #[cfg_attr(can_fdcan, path = "fdcan.rs")] | ||
| 4 | mod _version; | 5 | mod _version; |
| 5 | pub use _version::*; | 6 | pub use _version::*; |
