|
| 1 | +//! # Controller Area Network (CAN) Interface |
| 2 | +//! |
| 3 | +
|
| 4 | +use crate::fdcan; |
| 5 | +use crate::fdcan::message_ram; |
| 6 | +use crate::rcc::Rcc; |
| 7 | +use crate::stm32::{self, FDCAN1, FDCAN2, FDCAN3}; |
| 8 | + |
| 9 | +mod sealed { |
| 10 | + pub trait Sealed {} |
| 11 | +} |
| 12 | + |
| 13 | +/// A pair of (TX, RX) pins configured for CAN communication |
| 14 | +pub trait Pins: sealed::Sealed { |
| 15 | + /// The CAN peripheral that uses these pins |
| 16 | + type Instance; |
| 17 | +} |
| 18 | + |
| 19 | +/// Implements sealed::Sealed and Pins for a (TX, RX) pair of pins associated with a CAN peripheral |
| 20 | +/// The alternate function number can be specified after each pin name. If not specified, both |
| 21 | +/// default to AF9. |
| 22 | +macro_rules! pins { |
| 23 | + ($($PER:ident => ($tx:ident<$txaf:ident>, $rx:ident<$rxaf:ident>),)+) => { |
| 24 | + $( |
| 25 | + impl crate::can::sealed::Sealed for ($tx<crate::gpio::Alternate<$txaf>>, $rx<crate::gpio::Alternate<$rxaf>>) {} |
| 26 | + impl crate::can::Pins for ($tx<crate::gpio::Alternate<$txaf>>, $rx<crate::gpio::Alternate<$rxaf>>) { |
| 27 | + type Instance = $PER; |
| 28 | + } |
| 29 | + )+ |
| 30 | + }; |
| 31 | + ($($PER:ident => ($tx:ident, $rx:ident),)+) => { |
| 32 | + pins! { $($PER => ($tx<crate::gpio::AF9>, $rx<crate::gpio::AF9>),)+ } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +//TODO: verify correct pins |
| 37 | +mod common_pins { |
| 38 | + use crate::gpio::{ |
| 39 | + gpioa::{PA11, PA12}, |
| 40 | + gpiob::{PB12, PB13, PB5, PB6, PB8, PB9}, |
| 41 | + gpiod::{PD0, PD1}, |
| 42 | + AF7, |
| 43 | + }; |
| 44 | + use crate::stm32::{FDCAN1, FDCAN2}; |
| 45 | + // All STM32F4 models with CAN support these pins |
| 46 | + pins! { |
| 47 | + FDCAN1 => (PA12<AF7>, PA11<AF7>), |
| 48 | + FDCAN1 => (PD1<AF7>, PD0<AF7>), |
| 49 | + FDCAN1 => (PB9<AF7>, PB8<AF7>), |
| 50 | + FDCAN2 => (PB13<AF7>, PB12<AF7>), |
| 51 | + FDCAN2 => (PB6<AF7>, PB5<AF7>), |
| 52 | + } |
| 53 | +} |
| 54 | +/* |
| 55 | +//TODO: add other types |
| 56 | +//TODO: verify correct pins |
| 57 | +#[cfg(any(feature = "stm32g474"))] |
| 58 | +mod pb9_pb8_af8 { |
| 59 | + use crate::gpio::{ |
| 60 | + gpiob::{PB8, PB9}, |
| 61 | + AF8, |
| 62 | + }; |
| 63 | + use crate::stm32::FDCAN1; |
| 64 | + pins! { FDCAN1 => (PB9<AF8>, PB8<AF8>), } |
| 65 | +} |
| 66 | +*/ |
| 67 | +/* |
| 68 | +//TODO: add other types |
| 69 | +//TODO: verify correct pins |
| 70 | +#[cfg(any(feature = "stm32g474"))] |
| 71 | +mod pb9_pb8_af9 { |
| 72 | + use crate::gpio::{ |
| 73 | + gpiob::{PB8, PB9}, |
| 74 | + AF9, |
| 75 | + }; |
| 76 | + use crate::stm32::FDCAN1; |
| 77 | + pins! { FDCAN1 => (PB9<AF9>, PB8<AF9>), } |
| 78 | +} |
| 79 | +
|
| 80 | +//TODO: add other types |
| 81 | +//TODO: verify correct pins |
| 82 | +#[cfg(any(feature = "stm32g474"))] |
| 83 | +mod pg1_pg0 { |
| 84 | + use crate::gpio::{ |
| 85 | + gpiog::{PG0, PG1}, |
| 86 | + AF9, |
| 87 | + }; |
| 88 | + use crate::stm32::FDCAN1; |
| 89 | + pins! { FDCAN1 => (PG1<AF9>, PG0<AF9>), } |
| 90 | +} |
| 91 | +
|
| 92 | +//TODO: add other types |
| 93 | +//TODO: verify correct pins |
| 94 | +#[cfg(any(feature = "stm32g474"))] |
| 95 | +mod pg12_pg11 { |
| 96 | + use crate::gpio::{ |
| 97 | + gpiog::{PG11, PG12}, |
| 98 | + AF9, |
| 99 | + }; |
| 100 | + use crate::stm32::CAN2; |
| 101 | + pins! { CAN2 => (PG12<AF9>, PG11<AF9>), } |
| 102 | +} |
| 103 | +
|
| 104 | +//TODO: add other types |
| 105 | +//TODO: verify correct pins |
| 106 | +#[cfg(any(feature = "stm32g474"))] |
| 107 | +mod ph13_pi9 { |
| 108 | + use crate::gpio::{gpioh::PH13, gpioi::PI9, AF9}; |
| 109 | + use crate::stm32::CAN1; |
| 110 | + pins! { CAN1 => (PH13<AF9>, PI9<AF9>), } |
| 111 | +} |
| 112 | +*/ |
| 113 | +/// Enable/disable peripheral |
| 114 | +pub trait Enable: sealed::Sealed { |
| 115 | + /// Enables this peripheral by setting the associated enable bit in an RCC enable register |
| 116 | + fn enable(rcc: &Rcc); |
| 117 | +} |
| 118 | + |
| 119 | +/// Implements sealed::Sealed and Enable for a CAN peripheral (e.g. CAN1) |
| 120 | +/// |
| 121 | +/// $peren is the index in RCC_APB1ENR of the enable bit for the CAN peripheral, and the |
| 122 | +/// index in RCC_APB1RSTR of the reset bit for the CAN peripheral. |
| 123 | +impl crate::can::sealed::Sealed for crate::stm32::FDCAN1 {} |
| 124 | +impl crate::can::Enable for crate::stm32::FDCAN1 { |
| 125 | + #[inline(always)] |
| 126 | + fn enable(rcc: &Rcc) { |
| 127 | + // Enable peripheral |
| 128 | + rcc.rb.apb1enr1.modify(|_, w| w.fdcanen().set_bit()); |
| 129 | + } |
| 130 | +} |
| 131 | +impl crate::can::sealed::Sealed for crate::stm32::FDCAN2 {} |
| 132 | +impl crate::can::Enable for crate::stm32::FDCAN2 { |
| 133 | + #[inline(always)] |
| 134 | + fn enable(rcc: &Rcc) { |
| 135 | + // Enable peripheral |
| 136 | + rcc.rb.apb1enr1.modify(|_, w| w.fdcanen().set_bit()); |
| 137 | + } |
| 138 | +} |
| 139 | +/* |
| 140 | +/// Pins and definitions for models with a third CAN peripheral |
| 141 | +#[cfg(any(feature = "stm32f413", feature = "stm32f423"))] |
| 142 | +mod can3 { |
| 143 | + use super::Can; |
| 144 | + use crate::gpio::{ |
| 145 | + gpioa::{PA15, PA8}, |
| 146 | + gpiob::{PB3, PB4}, |
| 147 | + AF11, |
| 148 | + }; |
| 149 | + use crate::stm32::CAN3; |
| 150 | + pins! { |
| 151 | + CAN3 => (PA15<AF11>, PA8<AF11>), |
| 152 | + CAN3 => (PB4<AF11>, PB3<AF11>), |
| 153 | + } |
| 154 | + bus! { CAN3 => (27), } |
| 155 | +
|
| 156 | + unsafe impl bxcan::Instance for Can<CAN3> { |
| 157 | + const REGISTERS: *mut bxcan::RegisterBlock = CAN3::ptr() as *mut _; |
| 158 | + } |
| 159 | +
|
| 160 | + unsafe impl bxcan::FilterOwner for Can<CAN3> { |
| 161 | + const NUM_FILTER_BANKS: u8 = 14; |
| 162 | + } |
| 163 | +} |
| 164 | +*/ |
| 165 | +/// Interface to the CAN peripheral. |
| 166 | +pub struct FdCan<Instance> { |
| 167 | + _peripheral: Instance, |
| 168 | +} |
| 169 | + |
| 170 | +impl<Instance> FdCan<Instance> |
| 171 | +where |
| 172 | + Instance: Enable, |
| 173 | +{ |
| 174 | + /// Creates a CAN interface. |
| 175 | + pub fn new<P>(can: Instance, _pins: P, rcc: &Rcc) -> FdCan<Instance> |
| 176 | + where |
| 177 | + P: Pins<Instance = Instance>, |
| 178 | + { |
| 179 | + Instance::enable(rcc); |
| 180 | + FdCan { _peripheral: can } |
| 181 | + } |
| 182 | + |
| 183 | + pub fn new_unchecked(can: Instance, rcc: &Rcc) -> FdCan<Instance> { |
| 184 | + Instance::enable(rcc); |
| 185 | + FdCan { _peripheral: can } |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +unsafe impl fdcan::Instance for FdCan<FDCAN1> { |
| 190 | + const REGISTERS: *mut stm32::fdcan::RegisterBlock = FDCAN1::ptr() as *mut _; |
| 191 | +} |
| 192 | + |
| 193 | +unsafe impl fdcan::Instance for FdCan<FDCAN2> { |
| 194 | + const REGISTERS: *mut stm32::fdcan::RegisterBlock = FDCAN2::ptr() as *mut _; |
| 195 | +} |
| 196 | + |
| 197 | +unsafe impl fdcan::Instance for FdCan<FDCAN3> { |
| 198 | + const REGISTERS: *mut stm32::fdcan::RegisterBlock = FDCAN3::ptr() as *mut _; |
| 199 | +} |
| 200 | + |
| 201 | +unsafe impl message_ram::MsgRamExt for FdCan<FDCAN1> { |
| 202 | + const MSG_RAM: *mut message_ram::RegisterBlock = (0x4000_ac00 as *mut _); |
| 203 | +} |
| 204 | + |
| 205 | +unsafe impl message_ram::MsgRamExt for FdCan<FDCAN2> { |
| 206 | + const MSG_RAM: *mut message_ram::RegisterBlock = (0x4000_af54 as *mut _); |
| 207 | +} |
| 208 | + |
| 209 | +unsafe impl message_ram::MsgRamExt for FdCan<FDCAN3> { |
| 210 | + const MSG_RAM: *mut message_ram::RegisterBlock = (0x4000_b2a4 as *mut _); |
| 211 | +} |
0 commit comments