stm32f7xx_hal/
can.rs

1//! # Controller Area Network (CAN) Interface
2//!
3//! ## Alternate function remapping
4//!
5//! TX: Alternate Push-Pull Output
6//! RX: Alternate AF9 Alternate
7//!
8//! ### CAN1
9//!
10//! | Function | NoRemap | Remap |
11//! |----------|---------|-------|
12//! | TX       | PA12    | PB9   |
13//! | RX       | PA11    | PB8   |
14//!
15//! ### CAN2
16//!
17//! | Function | NoRemap | Remap |
18//! |----------|---------|-------|
19//! | TX       | PB6     | PB13  |
20//! | RX       | PB5     | PB12  |
21
22use crate::gpio::gpiob::{PB12, PB13, PB5, PB6, PB8, PB9};
23use crate::gpio::{
24    gpioa::{PA11, PA12},
25    Alternate,
26};
27use crate::pac::CAN1;
28use crate::pac::CAN2;
29use crate::rcc::APB1;
30
31mod sealed {
32    pub trait Sealed {}
33}
34
35pub trait Pins: sealed::Sealed {
36    type Instance;
37}
38
39impl sealed::Sealed for (PA12<Alternate<9>>, PA11<Alternate<9>>) {}
40impl Pins for (PA12<Alternate<9>>, PA11<Alternate<9>>) {
41    type Instance = CAN1;
42}
43
44impl sealed::Sealed for (PB9<Alternate<9>>, PB8<Alternate<9>>) {}
45impl Pins for (PB9<Alternate<9>>, PB8<Alternate<9>>) {
46    type Instance = CAN1;
47}
48
49impl sealed::Sealed for (PB6<Alternate<9>>, PB5<Alternate<9>>) {}
50impl Pins for (PB6<Alternate<9>>, PB5<Alternate<9>>) {
51    type Instance = CAN2;
52}
53
54impl sealed::Sealed for (PB13<Alternate<9>>, PB12<Alternate<9>>) {}
55impl Pins for (PB13<Alternate<9>>, PB12<Alternate<9>>) {
56    type Instance = CAN2;
57}
58
59/// Interface to the CAN peripheral.
60pub struct Can<Instance> {
61    _peripheral: Instance,
62}
63
64impl<Instance> Can<Instance>
65where
66    Instance: crate::rcc::Enable<Bus = APB1>,
67{
68    /// Creates a CAN interaface.
69    pub fn new<P>(can: Instance, apb: &mut APB1, _pins: P) -> Can<Instance>
70    where
71        P: Pins<Instance = Instance>,
72    {
73        Instance::enable(apb);
74        Can { _peripheral: can }
75    }
76}
77
78unsafe impl bxcan::Instance for Can<CAN1> {
79    const REGISTERS: *mut bxcan::RegisterBlock = CAN1::ptr() as *mut _;
80}
81
82unsafe impl bxcan::Instance for Can<CAN2> {
83    const REGISTERS: *mut bxcan::RegisterBlock = CAN2::ptr() as *mut _;
84}
85
86unsafe impl bxcan::FilterOwner for Can<CAN1> {
87    const NUM_FILTER_BANKS: u8 = 28;
88}
89
90unsafe impl bxcan::MasterInstance for Can<CAN1> {}