diff --git a/components/driver/test_apps/legacy_twai/main/test_twai_loop_back.cpp b/components/driver/test_apps/legacy_twai/main/test_twai_loop_back.cpp index 4a80486bdb7..4fa7f87bf50 100644 --- a/components/driver/test_apps/legacy_twai/main/test_twai_loop_back.cpp +++ b/components/driver/test_apps/legacy_twai/main/test_twai_loop_back.cpp @@ -11,6 +11,7 @@ #include "freertos/task.h" #include "unity.h" #include "driver/twai.h" +#include "hal/twai_ll.h" #include "soc/soc_caps.h" #include "esp_attr.h" #include "esp_private/sleep_cpu.h" @@ -57,6 +58,44 @@ TEST_CASE("twai_bit_timing", "[twai-loop-back]") TEST_ESP_OK(twai_driver_uninstall()); } +TEST_CASE("twai_bit_timing_uses_prop_seg", "[twai-loop-back]") +{ + twai_timing_config_t t_config = { + .clk_src = TWAI_CLK_SRC_DEFAULT, + .quanta_resolution_hz = 0, + .brp = 16, + .prop_seg = 8, + .tseg_1 = 7, + .tseg_2 = 4, + .sjw = 3, + .ssp_offset = 0, + .triple_sampling = false, + }; + twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL(); + twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(GPIO_NUM_0, GPIO_NUM_0, TWAI_MODE_NO_ACK); + + TEST_ESP_OK(twai_driver_install(&g_config, &t_config, &f_config)); + + const twai_dev_t *twai_dev = TWAI_LL_GET_HW(g_config.controller_id); + TEST_ASSERT_NOT_NULL(twai_dev); + + // twai_dev_t uses different register struct names across chip families: + // - older (esp32/s2/s3/c3): bus_timing_1_reg.{tseg1, tseg2, sam} + // - newer (esp32p4/c6/h2/h21): bus_timing_1.{time_segment1, time_segment2, time_sampling} +#if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S2) || \ + defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C3) + TEST_ASSERT_EQUAL_UINT32(t_config.tseg_1 + t_config.prop_seg - 1, twai_dev->bus_timing_1_reg.tseg1); + TEST_ASSERT_EQUAL_UINT32(t_config.tseg_2 - 1, twai_dev->bus_timing_1_reg.tseg2); + TEST_ASSERT_EQUAL((int)t_config.triple_sampling, (int)twai_dev->bus_timing_1_reg.sam); +#else + TEST_ASSERT_EQUAL_UINT32(t_config.tseg_1 + t_config.prop_seg - 1, twai_dev->bus_timing_1.time_segment1); + TEST_ASSERT_EQUAL_UINT32(t_config.tseg_2 - 1, twai_dev->bus_timing_1.time_segment2); + TEST_ASSERT_EQUAL((int)t_config.triple_sampling, (int)twai_dev->bus_timing_1.time_sampling); +#endif + + TEST_ESP_OK(twai_driver_uninstall()); +} + TEST_CASE("twai_mode_std_no_ack_25kbps", "[twai-loop-back]") { twai_timing_config_t t_config = TWAI_TIMING_CONFIG_25KBITS(); diff --git a/components/esp_hal_twai/twai_hal_v1.c b/components/esp_hal_twai/twai_hal_v1.c index cf01ef1a6b4..bf13115bda5 100644 --- a/components/esp_hal_twai/twai_hal_v1.c +++ b/components/esp_hal_twai/twai_hal_v1.c @@ -86,7 +86,7 @@ void twai_hal_configure(twai_hal_context_t *hal_ctx, const twai_timing_config_t } //Configure bus timing, acceptance filter, CLKOUT, and interrupts - twai_ll_set_bus_timing(hal_ctx->dev, brp, t_config->sjw, t_config->tseg_1, t_config->tseg_2, t_config->triple_sampling); + twai_ll_set_bus_timing(hal_ctx->dev, brp, t_config->sjw, t_config->tseg_1 + t_config->prop_seg, t_config->tseg_2, t_config->triple_sampling); twai_ll_set_acc_filter(hal_ctx->dev, f_config->acceptance_code, f_config->acceptance_mask, f_config->single_filter); twai_ll_set_clkout(hal_ctx->dev, clkout_divider); }