mcp2515 0.2.2

`#![no_std]` library for interacting with MCP2515 CAN controller chips.
Documentation
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! MCP2515 registers.

use core::{fmt::Debug, ops::BitOr};

use modular_bitfield::prelude::*;

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
pub enum Register {
    RXF0SIDH = 0x00,
    RXF0SIDL = 0x01,
    RXF0EID8 = 0x02,
    RXF0EID0 = 0x03,
    RXF1SIDH = 0x04,
    RXF1SIDL = 0x05,
    RXF1EID8 = 0x06,
    RXF1EID0 = 0x07,
    RXF2SIDH = 0x08,
    RXF2SIDL = 0x09,
    RXF2EID8 = 0x0A,
    RXF2EID0 = 0x0B,
    CANSTAT = 0x0E,
    CANCTRL = 0x0F,
    RXF3SIDH = 0x10,
    RXF3SIDL = 0x11,
    RXF3EID8 = 0x12,
    RXF3EID0 = 0x13,
    RXF4SIDH = 0x14,
    RXF4SIDL = 0x15,
    RXF4EID8 = 0x16,
    RXF4EID0 = 0x17,
    RXF5SIDH = 0x18,
    RXF5SIDL = 0x19,
    RXF5EID8 = 0x1A,
    RXF5EID0 = 0x1B,
    TEC = 0x1C,
    REC = 0x1D,
    RXM0SIDH = 0x20,
    RXM0SIDL = 0x21,
    RXM0EID8 = 0x22,
    RXM0EID0 = 0x23,
    RXM1SIDH = 0x24,
    RXM1SIDL = 0x25,
    RXM1EID8 = 0x26,
    RXM1EID0 = 0x27,
    CNF3 = 0x28,
    CNF2 = 0x29,
    CNF1 = 0x2A,
    CANINTE = 0x2B,
    CANINTF = 0x2C,
    EFLG = 0x2D,
    TXB0CTRL = 0x30,
    TXB0SIDH = 0x31,
    TXB0SIDL = 0x32,
    TXB0EID8 = 0x33,
    TXB0EID0 = 0x34,
    TXB0DLC = 0x35,
    TXB0DATA = 0x36,
    TXB1CTRL = 0x40,
    TXB1SIDH = 0x41,
    TXB1SIDL = 0x42,
    TXB1EID8 = 0x43,
    TXB1EID0 = 0x44,
    TXB1DLC = 0x45,
    TXB1DATA = 0x46,
    TXB2CTRL = 0x50,
    TXB2SIDH = 0x51,
    TXB2SIDL = 0x52,
    TXB2EID8 = 0x53,
    TXB2EID0 = 0x54,
    TXB2DLC = 0x55,
    TXB2DATA = 0x56,
    RXB0CTRL = 0x60,
    RXB0SIDH = 0x61,
    RXB0SIDL = 0x62,
    RXB0EID8 = 0x63,
    RXB0EID0 = 0x64,
    RXB0DLC = 0x65,
    RXB0DATA = 0x66,
    RXB1CTRL = 0x70,
    RXB1SIDH = 0x71,
    RXB1SIDL = 0x72,
    RXB1EID8 = 0x73,
    RXB1EID0 = 0x74,
    RXB1DLC = 0x75,
    RXB1DATA = 0x76,
}

pub trait Reg<const BYTES: usize>: Copy {
    /// List of addresses related to this register (or register set). LSB to
    /// MSB.
    const ADDRESSES: [Register; BYTES];

    /// Read the register into itself from a list of bytes.
    fn read(content: [u8; BYTES]) -> Self;

    /// Write the register to a list of bytes.
    fn write(self) -> [u8; BYTES];
}

/// Marker trait implemented on registers which are modifiable.
pub trait BitModifiable<const BYTES: usize>: Reg<BYTES> {}

#[bitfield]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
pub struct CanCtrl {
    /// CLKOUT Presacalar
    pub clkpre: ClkPre,
    /// CLKOUT Enable
    pub clken: bool,
    /// One-shot Mode
    pub osm: bool,
    /// Abort All Pending Transmisison
    pub abat: bool,
    /// Request Operation Mode
    pub reqop: OpMode,
}

impl CanCtrl {
    /// Mask to modify the `reqop` bits.
    pub const MASK_REQOP: Self = Self::from_bytes([0b1110_0000]);
    /// Mask to modify the `clken` bit.
    pub const MASK_CLKEN: Self = Self::from_bytes([0b0000_0100]);
}

impl BitModifiable<1> for CanCtrl {}

#[bitfield]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
pub struct CanStat {
    #[skip]
    __: B1,
    #[skip(setters)]
    pub icod: IntFlagCode,
    #[skip]
    __: B1,
    #[skip(setters)]
    pub opmod: OpMode,
}

#[bitfield]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
pub struct CanIntf {
    pub rx0if: bool,
    pub rx1if: bool,
    pub tx0if: bool,
    pub tx1if: bool,
    pub tx2if: bool,
    pub errif: bool,
    pub wakif: bool,
    pub merrf: bool,
}

impl CanIntf {
    pub const MASK_RX0IF: Self = Self::from_bytes([0b0000_0001]);
    pub const MASK_RX1IF: Self = Self::from_bytes([0b0000_0010]);
    pub const MASK_TX0IF: Self = Self::from_bytes([0b0000_0100]);
    pub const MASK_TX1IF: Self = Self::from_bytes([0b0000_1000]);
    pub const MASK_TX2IF: Self = Self::from_bytes([0b0001_0000]);
    pub const MASK_ERRIF: Self = Self::from_bytes([0b0010_0000]);
    pub const MASK_WAKIF: Self = Self::from_bytes([0b0100_0000]);
    pub const MASK_MERRF: Self = Self::from_bytes([0b1000_0000]);
}

impl BitModifiable<1> for CanIntf {}

#[bitfield]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
pub struct CanInte {
    pub rx0ie: bool,
    pub rx1ie: bool,
    pub tx0ie: bool,
    pub tx1ie: bool,
    pub tx2ie: bool,
    pub errie: bool,
    pub wakie: bool,
    pub merre: bool,
}

impl CanInte {
    pub const MASK_WAKIE: Self = Self::from_bytes([0b0100_0000]);
}

impl BitModifiable<1> for CanInte {}

#[bitfield]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
pub struct Cnf1 {
    pub brp: B6,
    pub sjw: SyncJumpWidth,
}

#[bitfield]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
pub struct Cnf2 {
    pub prseg: B3,
    pub phseg1: B3,
    pub sam: bool,
    pub btlmode: bool,
}

#[bitfield]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
pub struct Cnf3 {
    pub phseg2: B3,
    #[skip]
    __: B3,
    pub wakfil: bool,
    pub sof: bool,
}

#[bitfield]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
pub struct Rxb0Ctrl {
    /// Filter hit.
    // TODO(david): Data sheet says this is read-only however other libraries write to it.
    pub filhit0: bool,
    /// Read-Only copy of BUKT bit (used internally by MCP2515).
    #[skip(setters)]
    pub bukt1: bool,
    /// Rollover enable.
    pub bukt: bool,
    /// Received remote transfer request.
    #[skip(setters)]
    pub rxrtr: bool,
    #[skip]
    __: B1,
    /// Receive buffer operating mode.
    pub rxm: RecvBufOpMode,
    #[skip]
    __: B1,
}

impl Rxb0Ctrl {
    pub const MASK_RXM: Self = Self::from_bytes([0b0110_0000]);
    pub const MASK_BUKT: Self = Self::from_bytes([0b0000_0100]);
    pub const MASK_FILTHIT0: Self = Self::from_bytes([0b0000_0001]);
}

impl BitModifiable<1> for Rxb0Ctrl {}

#[bitfield]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
pub struct Rxb1Ctrl {
    /// Filter hit.
    // TODO(david): Data sheet says this is read-only however other libraries write to it.
    pub filthit: FilterHit,
    /// Received remote transfer request.
    #[skip(setters)]
    pub rxrtr: bool,
    #[skip]
    __: B1,
    /// Received buffer operating mode.
    pub rxm: RecvBufOpMode,
    #[skip]
    __: B1,
}

impl Rxb1Ctrl {
    pub const MASK_RXM: Self = Self::from_bytes([0b011_0000]);
    pub const MASK_FILTHIT: Self = Self::from_bytes([0b0000_0111]);
}

impl BitModifiable<1> for Rxb1Ctrl {}

#[bitfield]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
pub struct TxbCtrl {
    pub txp: TxBufPriority,
    #[skip]
    __: B1,
    pub txreq: bool,
    #[skip(setters)]
    pub txerr: bool,
    #[skip(setters)]
    pub mloa: bool,
    #[skip(setters)]
    pub abtf: bool,
    #[skip]
    __: B1,
}

impl TxbCtrl {
    pub const MASK_TXB: Self = Self::from_bytes([0b0000_0011]);
    pub const MASK_TXREQ: Self = Self::from_bytes([0b0000_1000]);
}

///////////////////
/// Enums
///////////////////

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, BitfieldSpecifier)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
#[bits = 2]
pub enum TxBufPriority {
    Low,
    LowIntermediate,
    HighIntermediate,
    High,
}

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, BitfieldSpecifier)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
#[bits = 3]
pub enum FilterHit {
    /// Acceptance filter 0 (only if the BUKT bit is set in RXB0CTRL).
    Filter0,
    /// Acceptance filter 1 (only if the BUKT bit is set in RXB0CTRL).
    Filter1,
    /// Acceptance filter 2.
    Filter2,
    /// Acceptance filter 3.
    Fitler3,
    /// Acceptance filter 4.
    Filter4,
    /// Acceptance filter 5.
    Filter5,
}

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, BitfieldSpecifier)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
#[bits = 2]
pub enum RecvBufOpMode {
    /// Receives all valid messages using either Standard or Extended
    /// Identifiers that meet filter criteria; Extended ID Filter registers,
    /// RXFnEID8:RXFnEID0, are applied to the first two bytes of data in the
    /// messages with standard IDs.
    FilterOn = 0x0,
    /// Turns masks/filters off; receives any message.
    FilterOff = 0x3,
}

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, BitfieldSpecifier)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
#[bits = 2]
pub enum SyncJumpWidth {
    Tq1,
    Tq2,
    Tq3,
    Tq4,
}

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, BitfieldSpecifier)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
#[bits = 3]
pub enum OpMode {
    Normal,
    Sleep,
    Loopback,
    ListenOnly,
    Configuration,
}

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, BitfieldSpecifier)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
#[bits = 2]
pub enum ClkPre {
    Div1,
    Div2,
    Div4,
    Div8,
}

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, BitfieldSpecifier)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "ufmt", derive(ufmt::derive::uDebug))]
#[bits = 3]
pub enum IntFlagCode {
    None,
    Error,
    WakeUp,
    TXB0,
    TXB1,
    TXB2,
    RXB0,
    RXB1,
}

macro_rules! reg {
    ($($s:ty => $reg:expr),*) => {
        $(
            impl Reg<1> for $s {
                const ADDRESSES: [Register; 1] = [$reg];

                #[inline]
                fn read(content: [u8; 1]) -> Self {
                    Self::from_bytes(content)
                }

                #[inline]
                fn write(self) -> [u8; 1] {
                    self.into_bytes()
                }
            }

            impl BitOr for $s {
                type Output = Self;

                fn bitor(self, rhs: Self) -> Self::Output {
                    Self::from_bytes([
                        self.into_bytes()[0] | rhs.into_bytes()[0]
                    ])
                }
            }
        )*
    };
}

reg! {
    CanCtrl => Register::CANCTRL,
    CanStat => Register::CANSTAT,
    CanIntf => Register::CANINTF,
    CanInte => Register::CANINTE,
    Cnf1 => Register::CNF1,
    Cnf2 => Register::CNF2,
    Cnf3 => Register::CNF3,
    Rxb0Ctrl => Register::RXB0CTRL,
    Rxb1Ctrl => Register::RXB1CTRL
}