can_bit_timings_core/lib.rs
1#![no_std]
2/*
3 * Copyright (c) 2021, Pavel Pletenev
4 *
5 * This file is part of can_bit_timings project
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12/// This struct holds parts of bit timing for CAN.
13/// Field descriptions quote descriptions from [this site](http://www.bittiming.can-wiki.info/).
14#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
15pub struct CanBitTiming {
16 /// **bs1** = **PROP_SEG** + **PHASE_SEG1**
17 ///
18 /// **PROP_SEG** is programmable to be 1, 2,... 8 Time Quanta long.
19 /// It is used to compensate for signal delays across the network.
20 ///
21 /// **PHASE_SEG1** is programmable to be 1,2, ... 8 Time Quanta long.
22 /// It is used to compensate for edge phase errors and may be lengthened
23 /// during resynchronization.
24 pub bs1: u8,
25
26 /// **bs2** = **PHASE_SEG2** (Seg 2) is the maximum of PHASE_SEG1 and
27 /// the Information Processing Time long. It is also used to compensate edge
28 /// phase errors and may be shortened during resynchronization.
29 /// For this the minimum value of PHASE_SEG2 is the value of SJW.
30 /// Information Processing Time is less than or equal to 2 Time Quanta long.
31 pub bs2: u8,
32
33 /// Synchronization Jump Width
34 pub sjw: u8,
35
36 /// Prescaler value
37 pub prescaler: u16
38}
39
40
41impl CanBitTiming {
42 /// Converts [CanBitTiming] struct to a `u32` register
43 /// as used by bxcan module.
44 pub fn bxcan(&self) -> u32 {
45 (self.sjw as u32 - 1 ) << 24
46 | (self.bs1 as u32 - 1) << 16
47 | (self.bs2 as u32 - 1 ) << 20
48 | (self.prescaler as u32 -1 )
49 }
50
51 /// Outputs total time quanta used to clock a CAN bus module
52 pub fn total_time_quanta(&self) -> u16 {
53 self.bs1 as u16 + self.bs2 as u16 + self.sjw as u16
54 }
55}