1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//! # Controller Area Network (CAN) Interface
//!

use crate::gpio::{Const, NoPin, PinA, PushPull, SetAlternate};
use crate::pac::{CAN1, CAN2};
use crate::rcc;

pub trait Instance: crate::Sealed + rcc::Enable + rcc::Reset {}

// Implemented by all SPI instances
impl Instance for CAN1 {}
pub type Can1<PINS> = Can<CAN1, PINS>;
impl Instance for CAN2 {}
pub type Can2<PINS> = Can<CAN2, PINS>;
#[cfg(feature = "can3")]
pub type Can3<PINS> = Can<crate::pac::CAN3, PINS>;

pub struct Tx;
impl crate::Sealed for Tx {}
pub struct Rx;
impl crate::Sealed for Rx {}

pub trait Pins<CAN> {
    fn set_alt_mode(&mut self);
    fn restore_mode(&mut self);
}

impl<CAN, TX, RX, const TXA: u8, const RXA: u8> Pins<CAN> for (TX, RX)
where
    TX: PinA<Tx, CAN, A = Const<TXA>> + SetAlternate<TXA, PushPull>,
    RX: PinA<Rx, CAN, A = Const<RXA>> + SetAlternate<RXA, PushPull>,
{
    fn set_alt_mode(&mut self) {
        self.0.set_alt_mode();
        self.1.set_alt_mode();
    }
    fn restore_mode(&mut self) {
        self.0.restore_mode();
        self.1.restore_mode();
    }
}

/// Pins and definitions for models with a third CAN peripheral
#[cfg(feature = "can3")]
mod can3 {
    use super::*;
    use crate::pac::CAN3;

    impl Instance for CAN3 {}

    unsafe impl<PINS> bxcan::Instance for Can<CAN3, PINS> {
        const REGISTERS: *mut bxcan::RegisterBlock = CAN3::ptr() as *mut _;
    }

    unsafe impl<PINS> bxcan::FilterOwner for Can<CAN3, PINS> {
        const NUM_FILTER_BANKS: u8 = 14;
    }
}

pub trait CanExt: Sized + Instance {
    fn can<TX, RX>(self, pins: (TX, RX)) -> Can<Self, (TX, RX)>
    where
        (TX, RX): Pins<Self>;
    fn tx<TX>(self, tx_pin: TX) -> Can<Self, (TX, NoPin)>
    where
        (TX, NoPin): Pins<Self>;
    fn rx<RX>(self, rx_pin: RX) -> Can<Self, (NoPin, RX)>
    where
        (NoPin, RX): Pins<Self>;
}

impl<CAN: Instance> CanExt for CAN {
    fn can<TX, RX>(self, pins: (TX, RX)) -> Can<Self, (TX, RX)>
    where
        (TX, RX): Pins<Self>,
    {
        Can::new(self, pins)
    }
    fn tx<TX>(self, tx_pin: TX) -> Can<Self, (TX, NoPin)>
    where
        (TX, NoPin): Pins<Self>,
    {
        Can::tx(self, tx_pin)
    }
    fn rx<RX>(self, rx_pin: RX) -> Can<Self, (NoPin, RX)>
    where
        (NoPin, RX): Pins<Self>,
    {
        Can::rx(self, rx_pin)
    }
}

/// Interface to the CAN peripheral.
pub struct Can<CAN, PINS> {
    can: CAN,
    pins: PINS,
}

impl<CAN, TX, RX> Can<CAN, (TX, RX)>
where
    CAN: Instance,
    (TX, RX): Pins<CAN>,
{
    /// Creates a CAN interface.
    pub fn new(can: CAN, mut pins: (TX, RX)) -> Self {
        unsafe {
            // NOTE(unsafe) this reference will only be used for atomic writes with no side effects.
            let rcc = &(*crate::pac::RCC::ptr());
            CAN::enable(rcc);
            CAN::reset(rcc);
        }

        pins.set_alt_mode();

        Can { can, pins }
    }

    pub fn release(mut self) -> (CAN, (TX, RX)) {
        self.pins.restore_mode();

        (self.can, (self.pins.0, self.pins.1))
    }
}

impl<CAN, TX> Can<CAN, (TX, NoPin)>
where
    CAN: Instance,
    (TX, NoPin): Pins<CAN>,
{
    pub fn tx(usart: CAN, tx_pin: TX) -> Self {
        Self::new(usart, (tx_pin, NoPin))
    }
}

impl<CAN, RX> Can<CAN, (NoPin, RX)>
where
    CAN: Instance,
    (NoPin, RX): Pins<CAN>,
{
    pub fn rx(usart: CAN, rx_pin: RX) -> Self {
        Self::new(usart, (NoPin, rx_pin))
    }
}

unsafe impl<PINS> bxcan::Instance for Can<CAN1, PINS> {
    const REGISTERS: *mut bxcan::RegisterBlock = CAN1::ptr() as *mut _;
}

unsafe impl<PINS> bxcan::Instance for Can<CAN2, PINS> {
    const REGISTERS: *mut bxcan::RegisterBlock = CAN2::ptr() as *mut _;
}

unsafe impl<PINS> bxcan::FilterOwner for Can<CAN1, PINS> {
    const NUM_FILTER_BANKS: u8 = 28;
}

unsafe impl<PINS> bxcan::MasterInstance for Can<CAN1, PINS> {}