From 3284e36401f03031ffdcf5d61959f96e71f525d1 Mon Sep 17 00:00:00 2001 From: fastbike Date: Mon, 29 Apr 2024 20:33:36 +1200 Subject: [PATCH 1/2] Fix for https://github.com/arduino/ArduinoCore-renesas/issues/295 to allow flexible bit rate either one of the 4 predefined rates or a custom rate --- libraries/Arduino_CAN/src/CanUtil.cpp | 2 +- libraries/Arduino_CAN/src/CanUtil.h | 2 +- libraries/Arduino_CAN/src/R7FA4M1_CAN.cpp | 10 ++++++++-- libraries/Arduino_CAN/src/R7FA4M1_CAN.h | 1 + 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/libraries/Arduino_CAN/src/CanUtil.cpp b/libraries/Arduino_CAN/src/CanUtil.cpp index d3250ae4..9861e15e 100644 --- a/libraries/Arduino_CAN/src/CanUtil.cpp +++ b/libraries/Arduino_CAN/src/CanUtil.cpp @@ -28,7 +28,7 @@ namespace util **************************************************************************************/ std::tuple - calc_can_bit_timing(CanBitRate const can_bitrate, + calc_can_bit_timing(uint32_t const can_bitrate, uint32_t const can_clock_Hz, uint32_t const tq_min, uint32_t const tq_max, diff --git a/libraries/Arduino_CAN/src/CanUtil.h b/libraries/Arduino_CAN/src/CanUtil.h index ec5a89ae..ae1a488a 100644 --- a/libraries/Arduino_CAN/src/CanUtil.h +++ b/libraries/Arduino_CAN/src/CanUtil.h @@ -36,7 +36,7 @@ std::tuple /* time_segment_2 */ -calc_can_bit_timing(CanBitRate const can_bitrate, uint32_t const can_clock_Hz, uint32_t const tq_min, uint32_t const tq_max, +calc_can_bit_timing(uint32_t const can_bitrate, uint32_t const can_clock_Hz, uint32_t const tq_min, uint32_t const tq_max, uint32_t const tseg1_min, uint32_t const tseg1_max, uint32_t const tseg2_min, uint32_t const tseg2_max); /************************************************************************************** diff --git a/libraries/Arduino_CAN/src/R7FA4M1_CAN.cpp b/libraries/Arduino_CAN/src/R7FA4M1_CAN.cpp index 95938e80..95241ec5 100644 --- a/libraries/Arduino_CAN/src/R7FA4M1_CAN.cpp +++ b/libraries/Arduino_CAN/src/R7FA4M1_CAN.cpp @@ -137,6 +137,11 @@ R7FA4M1_CAN::R7FA4M1_CAN(int const can_tx_pin, int const can_rx_pin) **************************************************************************************/ bool R7FA4M1_CAN::begin(CanBitRate const can_bitrate) +{ + return begin(static_cast(can_bitrate)); +} + +bool R7FA4M1_CAN::begin(uint32_t const can_bitrate) { bool init_ok = true; @@ -156,7 +161,7 @@ bool R7FA4M1_CAN::begin(CanBitRate const can_bitrate) /* Calculate the CAN bitrate based on the value of this functions parameter. */ - static uint32_t const F_CAN_CLK_Hz = 24*1000*1000UL; + static uint32_t const F_CAN_CLK_Hz = 24*1000*1000UL; static uint32_t const TQ_MIN = 8; static uint32_t const TQ_MAX = 25; static uint32_t const TSEG_1_MIN = 4; @@ -167,7 +172,7 @@ bool R7FA4M1_CAN::begin(CanBitRate const can_bitrate) auto [is_valid_baudrate, baud_rate_prescaler, time_segment_1, time_segment_2] = util::calc_can_bit_timing(can_bitrate, F_CAN_CLK_Hz, TQ_MIN, TQ_MAX, TSEG_1_MIN, TSEG_1_MAX, TSEG_2_MIN, TSEG_2_MAX); init_ok &= is_valid_baudrate; - + if (is_valid_baudrate) { _can_bit_timing_cfg.baud_rate_prescaler = baud_rate_prescaler; _can_bit_timing_cfg.time_segment_1 = time_segment_1; @@ -182,6 +187,7 @@ bool R7FA4M1_CAN::begin(CanBitRate const can_bitrate) return init_ok; } +//////////////////////// void R7FA4M1_CAN::end() { R_CAN_Close(&_can_ctrl); diff --git a/libraries/Arduino_CAN/src/R7FA4M1_CAN.h b/libraries/Arduino_CAN/src/R7FA4M1_CAN.h index f60252f6..235df871 100644 --- a/libraries/Arduino_CAN/src/R7FA4M1_CAN.h +++ b/libraries/Arduino_CAN/src/R7FA4M1_CAN.h @@ -52,6 +52,7 @@ class R7FA4M1_CAN final : public HardwareCAN bool begin(CanBitRate const can_bitrate) override; + bool begin(uint32_t const can_bitrate); void end() override; void setFilterMask_Standard(uint32_t const mask); From 0bbe53319990b350516c6cb142b321fb6241929b Mon Sep 17 00:00:00 2001 From: fastbike Date: Mon, 29 Apr 2024 20:51:41 +1200 Subject: [PATCH 2/2] Formatting fix --- libraries/Arduino_CAN/src/R7FA4M1_CAN.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libraries/Arduino_CAN/src/R7FA4M1_CAN.cpp b/libraries/Arduino_CAN/src/R7FA4M1_CAN.cpp index 95241ec5..c53f4494 100644 --- a/libraries/Arduino_CAN/src/R7FA4M1_CAN.cpp +++ b/libraries/Arduino_CAN/src/R7FA4M1_CAN.cpp @@ -161,7 +161,7 @@ bool R7FA4M1_CAN::begin(uint32_t const can_bitrate) /* Calculate the CAN bitrate based on the value of this functions parameter. */ - static uint32_t const F_CAN_CLK_Hz = 24*1000*1000UL; + static uint32_t const F_CAN_CLK_Hz = 24*1000*1000UL; static uint32_t const TQ_MIN = 8; static uint32_t const TQ_MAX = 25; static uint32_t const TSEG_1_MIN = 4; @@ -172,7 +172,7 @@ bool R7FA4M1_CAN::begin(uint32_t const can_bitrate) auto [is_valid_baudrate, baud_rate_prescaler, time_segment_1, time_segment_2] = util::calc_can_bit_timing(can_bitrate, F_CAN_CLK_Hz, TQ_MIN, TQ_MAX, TSEG_1_MIN, TSEG_1_MAX, TSEG_2_MIN, TSEG_2_MAX); init_ok &= is_valid_baudrate; - + if (is_valid_baudrate) { _can_bit_timing_cfg.baud_rate_prescaler = baud_rate_prescaler; _can_bit_timing_cfg.time_segment_1 = time_segment_1; @@ -187,7 +187,6 @@ bool R7FA4M1_CAN::begin(uint32_t const can_bitrate) return init_ok; } -//////////////////////// void R7FA4M1_CAN::end() { R_CAN_Close(&_can_ctrl);