18
18
19
19
#include < IRQManager.h>
20
20
21
+ #include " CanUtil.h"
22
+
21
23
/* *************************************************************************************
22
24
* DEFINE
23
25
**************************************************************************************/
@@ -141,7 +143,7 @@ R7FA4M1_CAN::R7FA4M1_CAN(int const can_tx_pin, int const can_rx_pin)
141
143
* PUBLIC MEMBER FUNCTIONS
142
144
**************************************************************************************/
143
145
144
- bool R7FA4M1_CAN::begin (CanBitRate const /* can_bitrate */ )
146
+ bool R7FA4M1_CAN::begin (CanBitRate const can_bitrate)
145
147
{
146
148
bool init_ok = true ;
147
149
@@ -159,6 +161,23 @@ bool R7FA4M1_CAN::begin(CanBitRate const /* can_bitrate */)
159
161
};
160
162
init_ok &= IRQManager::getInstance ().addPeripheral (IRQ_CAN, &irq_req);
161
163
164
+ /* Calculate the CAN bitrate based on the value of this functions parameter.
165
+ */
166
+ static uint32_t const F_CAN_CLK_Hz = 20 *1000 *1000UL ; /* CANMCLK */
167
+ static uint32_t const CAN_TIME_QUANTA_per_Bit = 32 ; /* TQ */
168
+
169
+ auto [is_valid_baudrate, baud_rate_prescaler, time_segment_1, time_segment_2, synchronization_jump_width] =
170
+ util::calc_can_bit_timing (can_bitrate, F_CAN_CLK_Hz, CAN_TIME_QUANTA_per_Bit);
171
+ init_ok &= is_valid_baudrate;
172
+
173
+ if (is_valid_baudrate) {
174
+ _can_bit_timing_cfg.baud_rate_prescaler = baud_rate_prescaler;
175
+ _can_bit_timing_cfg.time_segment_1 = time_segment_1;
176
+ _can_bit_timing_cfg.time_segment_2 = time_segment_2;
177
+ _can_bit_timing_cfg.synchronization_jump_width = synchronization_jump_width;
178
+ }
179
+
180
+ /* Initialize the peripheral's FSP driver. */
162
181
if (R_CAN_Open (&_can_ctrl, &_can_cfg) != FSP_SUCCESS)
163
182
init_ok = false ;
164
183
0 commit comments