|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 by Alexander Entinger <a.entinger@arduino.cc> |
| 3 | + * CAN library for Arduino. |
| 4 | + * |
| 5 | + * This file is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of either the GNU General Public License version 2 |
| 7 | + * or the GNU Lesser General Public License version 2.1, both as |
| 8 | + * published by the Free Software Foundation. |
| 9 | + */ |
| 10 | + |
| 11 | +/************************************************************************************** |
| 12 | + INCLUDE |
| 13 | + **************************************************************************************/ |
| 14 | + |
| 15 | +#include <catch2/catch.hpp> |
| 16 | + |
| 17 | +#include "CanUtil.h" |
| 18 | + |
| 19 | +/************************************************************************************** |
| 20 | + TEST CODE |
| 21 | + **************************************************************************************/ |
| 22 | + |
| 23 | +SCENARIO("calc_can_bit_timing", "[calc_can_bit_timing]") |
| 24 | +{ |
| 25 | + GIVEN("Santiago") |
| 26 | + { |
| 27 | + static uint32_t const F_CAN_CLK_Hz = 20*1000*1000UL; |
| 28 | + static uint32_t const CAN_TIME_QUANTA_per_Bit = 32; /* TQ */ |
| 29 | + |
| 30 | + WHEN("CanBitRate::BR_125k") |
| 31 | + { |
| 32 | + auto [is_valid_baudrate, baud_rate_prescaler, time_segment_1, time_segment_2, synchronization_jump_width] = |
| 33 | + util::calc_can_bit_timing(CanBitRate::BR_125k, F_CAN_CLK_Hz, CAN_TIME_QUANTA_per_Bit); |
| 34 | + THEN("") |
| 35 | + { |
| 36 | + REQUIRE(is_valid_baudrate == true); |
| 37 | + REQUIRE(baud_rate_prescaler == 5); |
| 38 | + REQUIRE(time_segment_1 == 23); |
| 39 | + REQUIRE(time_segment_2 == 8); |
| 40 | + REQUIRE(synchronization_jump_width == 1); |
| 41 | + }; |
| 42 | + } |
| 43 | + WHEN("CanBitRate::BR_250k") |
| 44 | + { |
| 45 | + auto [is_valid_baudrate, baud_rate_prescaler, time_segment_1, time_segment_2, synchronization_jump_width] = |
| 46 | + util::calc_can_bit_timing(CanBitRate::BR_250k, F_CAN_CLK_Hz, CAN_TIME_QUANTA_per_Bit); |
| 47 | + THEN("") |
| 48 | + { |
| 49 | + REQUIRE(is_valid_baudrate == true); |
| 50 | + REQUIRE(baud_rate_prescaler == 4); |
| 51 | + REQUIRE(time_segment_1 == 14); |
| 52 | + REQUIRE(time_segment_2 == 5); |
| 53 | + REQUIRE(synchronization_jump_width == 1); |
| 54 | + }; |
| 55 | + } |
| 56 | + WHEN("CanBitRate::BR_500k") |
| 57 | + { |
| 58 | + auto [is_valid_baudrate, baud_rate_prescaler, time_segment_1, time_segment_2, synchronization_jump_width] = |
| 59 | + util::calc_can_bit_timing(CanBitRate::BR_500k, F_CAN_CLK_Hz, CAN_TIME_QUANTA_per_Bit); |
| 60 | + THEN("") |
| 61 | + { |
| 62 | + REQUIRE(is_valid_baudrate == true); |
| 63 | + REQUIRE(baud_rate_prescaler == 2); |
| 64 | + REQUIRE(time_segment_1 == 14); |
| 65 | + REQUIRE(time_segment_2 == 5); |
| 66 | + REQUIRE(synchronization_jump_width == 1); |
| 67 | + }; |
| 68 | + } |
| 69 | + WHEN("CanBitRate::BR_1000k") |
| 70 | + { |
| 71 | + auto [is_valid_baudrate, baud_rate_prescaler, time_segment_1, time_segment_2, synchronization_jump_width] = |
| 72 | + util::calc_can_bit_timing(CanBitRate::BR_1000k, F_CAN_CLK_Hz, CAN_TIME_QUANTA_per_Bit); |
| 73 | + THEN("") |
| 74 | + { |
| 75 | + REQUIRE(is_valid_baudrate == true); |
| 76 | + REQUIRE(baud_rate_prescaler == 1); |
| 77 | + REQUIRE(time_segment_1 == 14); |
| 78 | + REQUIRE(time_segment_2 == 5); |
| 79 | + REQUIRE(synchronization_jump_width == 1); |
| 80 | + }; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + GIVEN("Portenta H33") |
| 85 | + { |
| 86 | + static uint32_t const F_CAN_CLK_Hz = 24*1000*1000UL; |
| 87 | + static uint32_t const CAN_TIME_QUANTA_per_Bit = 32; /* TQ */ |
| 88 | + |
| 89 | + WHEN("CanBitRate::BR_125k") |
| 90 | + { |
| 91 | + auto [is_valid_baudrate, baud_rate_prescaler, time_segment_1, time_segment_2, synchronization_jump_width] = |
| 92 | + util::calc_can_bit_timing(CanBitRate::BR_125k, F_CAN_CLK_Hz, CAN_TIME_QUANTA_per_Bit); |
| 93 | + THEN("") |
| 94 | + { |
| 95 | + REQUIRE(is_valid_baudrate == true); |
| 96 | + REQUIRE(baud_rate_prescaler == 6); |
| 97 | + REQUIRE(time_segment_1 == 23); |
| 98 | + REQUIRE(time_segment_2 == 8); |
| 99 | + REQUIRE(synchronization_jump_width == 1); |
| 100 | + }; |
| 101 | + } |
| 102 | + WHEN("CanBitRate::BR_250k") |
| 103 | + { |
| 104 | + auto [is_valid_baudrate, baud_rate_prescaler, time_segment_1, time_segment_2, synchronization_jump_width] = |
| 105 | + util::calc_can_bit_timing(CanBitRate::BR_250k, F_CAN_CLK_Hz, CAN_TIME_QUANTA_per_Bit); |
| 106 | + THEN("") |
| 107 | + { |
| 108 | + REQUIRE(is_valid_baudrate == true); |
| 109 | + REQUIRE(baud_rate_prescaler == 3); |
| 110 | + REQUIRE(time_segment_1 == 23); |
| 111 | + REQUIRE(time_segment_2 == 8); |
| 112 | + REQUIRE(synchronization_jump_width == 1); |
| 113 | + }; |
| 114 | + } |
| 115 | + WHEN("CanBitRate::BR_500k") |
| 116 | + { |
| 117 | + auto [is_valid_baudrate, baud_rate_prescaler, time_segment_1, time_segment_2, synchronization_jump_width] = |
| 118 | + util::calc_can_bit_timing(CanBitRate::BR_500k, F_CAN_CLK_Hz, CAN_TIME_QUANTA_per_Bit); |
| 119 | + THEN("") |
| 120 | + { |
| 121 | + REQUIRE(is_valid_baudrate == true); |
| 122 | + REQUIRE(baud_rate_prescaler == 2); |
| 123 | + REQUIRE(time_segment_1 == 17); |
| 124 | + REQUIRE(time_segment_2 == 6); |
| 125 | + REQUIRE(synchronization_jump_width == 1); |
| 126 | + }; |
| 127 | + } |
| 128 | + WHEN("CanBitRate::BR_1000k") |
| 129 | + { |
| 130 | + auto [is_valid_baudrate, baud_rate_prescaler, time_segment_1, time_segment_2, synchronization_jump_width] = |
| 131 | + util::calc_can_bit_timing(CanBitRate::BR_1000k, F_CAN_CLK_Hz, CAN_TIME_QUANTA_per_Bit); |
| 132 | + THEN("") |
| 133 | + { |
| 134 | + REQUIRE(is_valid_baudrate == true); |
| 135 | + REQUIRE(baud_rate_prescaler == 1); |
| 136 | + REQUIRE(time_segment_1 == 17); |
| 137 | + REQUIRE(time_segment_2 == 6); |
| 138 | + REQUIRE(synchronization_jump_width == 1); |
| 139 | + }; |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments