ch32_hal/can/util.rs
1//! CAN utility functions from embassy-stm32
2
3use core::num::{NonZeroU16, NonZeroU8};
4
5/// Shared struct to represent bit timings used by calc_can_timings.
6#[derive(Clone, Copy, Debug)]
7pub struct NominalBitTiming {
8 /// Value by which the oscillator frequency is divided for generating the bit time quanta. The bit
9 /// time is built up from a multiple of this quanta. Valid values are 1 to 512.
10 pub prescaler: NonZeroU16,
11 /// Valid values are 1 to 128.
12 pub seg1: NonZeroU8,
13 /// Valid values are 1 to 255.
14 pub seg2: NonZeroU8,
15 /// Valid values are 1 to 128.
16 pub sync_jump_width: NonZeroU8,
17}
18
19/// Calculate nominal CAN bit timing based on CAN bitrate and periphial clock frequency
20pub fn calc_can_timings(periph_clock: u32, can_bitrate: u32) -> Option<NominalBitTiming> {
21 const BS1_MAX: u8 = 16;
22 const BS2_MAX: u8 = 8;
23 const MAX_SAMPLE_POINT_PERMILL: u16 = 900;
24
25 if can_bitrate < 1000 {
26 return None;
27 }
28
29 // Ref. "Automatic Baudrate Detection in CANopen Networks", U. Koppe, MicroControl GmbH & Co. KG
30 // CAN in Automation, 2003
31 //
32 // According to the source, optimal quanta per bit are:
33 // Bitrate Optimal Maximum
34 // 1000 kbps 8 10
35 // 500 kbps 16 17
36 // 250 kbps 16 17
37 // 125 kbps 16 17
38 let max_quanta_per_bit: u8 = if can_bitrate >= 1_000_000 { 10 } else { 17 };
39
40 // Computing (prescaler * BS):
41 // BITRATE = 1 / (PRESCALER * (1 / PCLK) * (1 + BS1 + BS2)) -- See the Reference Manual
42 // BITRATE = PCLK / (PRESCALER * (1 + BS1 + BS2)) -- Simplified
43 // let:
44 // BS = 1 + BS1 + BS2 -- Number of time quanta per bit
45 // PRESCALER_BS = PRESCALER * BS
46 // ==>
47 // PRESCALER_BS = PCLK / BITRATE
48 let prescaler_bs = periph_clock / can_bitrate;
49
50 // Searching for such prescaler value so that the number of quanta per bit is highest.
51 let mut bs1_bs2_sum = max_quanta_per_bit - 1;
52 while (prescaler_bs % (1 + bs1_bs2_sum) as u32) != 0 {
53 if bs1_bs2_sum <= 2 {
54 return None; // No solution
55 }
56 bs1_bs2_sum -= 1;
57 }
58
59 let prescaler = prescaler_bs / (1 + bs1_bs2_sum) as u32;
60 if (prescaler < 1) || (prescaler > 1024) {
61 return None; // No solution
62 }
63
64 // Now we have a constraint: (BS1 + BS2) == bs1_bs2_sum.
65 // We need to find such values so that the sample point is as close as possible to the optimal value,
66 // which is 87.5%, which is 7/8.
67 //
68 // Solve[(1 + bs1)/(1 + bs1 + bs2) == 7/8, bs2] (* Where 7/8 is 0.875, the recommended sample point location *)
69 // {{bs2 -> (1 + bs1)/7}}
70 //
71 // Hence:
72 // bs2 = (1 + bs1) / 7
73 // bs1 = (7 * bs1_bs2_sum - 1) / 8
74 //
75 // Sample point location can be computed as follows:
76 // Sample point location = (1 + bs1) / (1 + bs1 + bs2)
77 //
78 // Since the optimal solution is so close to the maximum, we prepare two solutions, and then pick the best one:
79 // - With rounding to nearest
80 // - With rounding to zero
81 let mut bs1 = ((7 * bs1_bs2_sum - 1) + 4) / 8; // Trying rounding to nearest first
82 let mut bs2 = bs1_bs2_sum - bs1;
83
84 if bs1_bs2_sum <= bs1 {
85 return None;
86 }
87 // core::assert!(bs1_bs2_sum > bs1);
88
89 let sample_point_permill = 1000 * ((1 + bs1) / (1 + bs1 + bs2)) as u16;
90 if sample_point_permill > MAX_SAMPLE_POINT_PERMILL {
91 // Nope, too far; now rounding to zero
92 bs1 = (7 * bs1_bs2_sum - 1) / 8;
93 bs2 = bs1_bs2_sum - bs1;
94 }
95
96 // Check is BS1 and BS2 are in range
97 if (bs1 < 1) || (bs1 > BS1_MAX) || (bs2 < 1) || (bs2 > BS2_MAX) {
98 return None;
99 }
100
101 // Check if final bitrate matches the requested
102 if can_bitrate != (periph_clock / (prescaler * (1 + bs1 + bs2) as u32)) {
103 return None;
104 }
105
106 // One is recommended by DS-015, CANOpen, and DeviceNet
107 let sync_jump_width = core::num::NonZeroU8::new(1)?;
108
109 let seg1 = core::num::NonZeroU8::new(bs1)?;
110 let seg2 = core::num::NonZeroU8::new(bs2)?;
111 let nz_prescaler = core::num::NonZeroU16::new(prescaler as u16)?;
112
113 Some(NominalBitTiming {
114 sync_jump_width,
115 prescaler: nz_prescaler,
116 seg1,
117 seg2,
118 })
119}