Skip to main content

ch32_hal/can/filter/
bit32.rs

1use embedded_can::Id;
2
3use super::{Bit16Mode, Bit32Mode, CanFilter, FilterMode, FilterOptions, ListMode, MaskMode};
4
5impl<MODE: FilterMode> CanFilter<Bit32Mode, MODE> {
6    /// Convert filter to 16bit mode
7    /// Register values in selected bank will be reset
8    pub fn use_16bit(self) -> CanFilter<Bit16Mode, MODE> {
9        CanFilter {
10            id_mask: 0,
11            id_value: 0,
12            mode: self.mode,
13            bank: self.bank,
14            bit_mode: Bit16Mode,
15        }
16    }
17}
18
19impl CanFilter<Bit32Mode, ListMode> {
20    /// Create a filter to configure id list
21    pub fn new_id_list() -> Self {
22        Self {
23            bank: 0,
24            bit_mode: Bit32Mode,
25            mode: ListMode,
26
27            id_mask: 0,
28            id_value: 0,
29        }
30    }
31}
32
33impl CanFilter<Bit32Mode, MaskMode> {
34    /// Creates a filter that accepts all frames
35    pub fn accept_all() -> Self {
36        CanFilter {
37            bank: 0,
38            mode: MaskMode,
39            id_value: 0,
40            id_mask: 0,
41            bit_mode: Bit32Mode,
42        }
43    }
44
45    pub fn new_id_mask() -> Self {
46        Self {
47            bank: 0,
48            bit_mode: Bit32Mode,
49            mode: MaskMode,
50
51            id_mask: 0,
52            id_value: 0,
53        }
54    }
55}
56
57pub struct Bit32IdReg<'a>(&'a mut u32);
58
59impl Bit32IdReg<'_> {
60    pub fn set(&mut self, id: Id, opts: FilterOptions) {
61        *self.0 = 0;
62        *self.0 |= (opts.use_rtr as u32) << 1;
63
64        match id {
65            Id::Standard(id) => {
66                let id_bits = id.as_raw() as u32;
67
68                *self.0 |= ((id_bits << 3) << 24) | ((id_bits & 0x7) << 20);
69            }
70
71            Id::Extended(id) => {
72                let std_id = (id.as_raw() as u16) as u32;
73                let ext_id = id.as_raw() >> 11;
74
75                *self.0 |= ((std_id << 3) << 24) | ((std_id & 0x7) << 20) | (ext_id << 3);
76            }
77        }
78    }
79}
80
81impl CanFilter<Bit32Mode, ListMode> {
82    pub fn get(&mut self, index: usize) -> Option<Bit32IdReg<'_>> {
83        match index {
84            0 => Some(Bit32IdReg(&mut self.id_value)),
85            1 => Some(Bit32IdReg(&mut self.id_mask)),
86            _ => None,
87        }
88    }
89}