Merge branch 'idfgh-17542' into 'master'

hal/twai: honor prop_seg in legacy timing config

Closes IDFGH-17542

See merge request espressif/esp-idf!47710
This commit is contained in:
Michael (XIAO Xufeng)
2026-04-25 00:49:23 +08:00
2 changed files with 40 additions and 1 deletions

View File

@@ -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();

View File

@@ -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);
}