diff --git a/components/esp_driver_twai/esp_twai.c b/components/esp_driver_twai/esp_twai.c index e79b666c39..0cbc86e7ad 100644 --- a/components/esp_driver_twai/esp_twai.c +++ b/components/esp_driver_twai/esp_twai.c @@ -145,6 +145,7 @@ esp_err_t twai_node_transmit(twai_node_handle_t node, const twai_frame_t *frame, esp_err_t twai_node_receive_from_isr(twai_node_handle_t node, twai_frame_t *rx_frame) { ESP_RETURN_ON_FALSE_ISR(node && rx_frame, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null"); + ESP_RETURN_ON_FALSE_ISR((rx_frame->buffer_len == 0) || esp_ptr_in_dram(rx_frame->buffer) || esp_ptr_external_ram(rx_frame->buffer), ESP_ERR_INVALID_ARG, TAG, "invalid 'rx_frame->buffer' pointer or buffer_len"); ESP_RETURN_ON_FALSE_ISR(node->receive_isr, ESP_ERR_NOT_SUPPORTED, TAG, "receive func null"); return node->receive_isr(node, rx_frame); diff --git a/components/esp_driver_twai/esp_twai_onchip.c b/components/esp_driver_twai/esp_twai_onchip.c index 1a2ea9006a..1a56595e5f 100644 --- a/components/esp_driver_twai/esp_twai_onchip.c +++ b/components/esp_driver_twai/esp_twai_onchip.c @@ -496,7 +496,7 @@ static esp_err_t _node_config_mask_filter(twai_node_handle_t node, uint8_t filte // FD targets don't support Dual filter ESP_RETURN_ON_FALSE(!mask_cfg->dual_filter, ESP_ERR_NOT_SUPPORTED, TAG, "The target don't support Dual Filter"); #endif - ESP_RETURN_ON_FALSE(atomic_load(&twai_ctx->state) == TWAI_ERROR_BUS_OFF, ESP_ERR_INVALID_STATE, TAG, "config filter must when node stopped"); + ESP_RETURN_ON_FALSE(atomic_load(&twai_ctx->state) == TWAI_ERROR_BUS_OFF, ESP_ERR_INVALID_STATE, TAG, "filter config must do when node stopped"); twai_hal_configure_filter(twai_ctx->hal, filter_id, mask_cfg); return ESP_OK; @@ -509,7 +509,7 @@ static esp_err_t _node_config_range_filter(twai_node_handle_t node, uint8_t filt ESP_RETURN_ON_FALSE(filter_id < SOC_TWAI_RANGE_FILTER_NUM, ESP_ERR_INVALID_ARG, TAG, "Invalid range filter id %d", filter_id); ESP_RETURN_ON_FALSE((range_cfg->range_low > range_cfg->range_high) || range_cfg->is_ext || !(range_cfg->range_high & ~TWAI_STD_ID_MASK), \ ESP_ERR_INVALID_ARG, TAG, "std_id only (is_ext=0) but valid low/high id larger than 11 bits"); - ESP_RETURN_ON_FALSE(atomic_load(&twai_ctx->state) == TWAI_ERROR_BUS_OFF, ESP_ERR_INVALID_STATE, TAG, "config filter must when node stopped"); + ESP_RETURN_ON_FALSE(atomic_load(&twai_ctx->state) == TWAI_ERROR_BUS_OFF, ESP_ERR_INVALID_STATE, TAG, "filter config must do when node stopped"); twai_hal_configure_range_filter(twai_ctx->hal, filter_id, range_cfg); return ESP_OK; @@ -626,6 +626,13 @@ esp_err_t twai_new_node_onchip(const twai_onchip_node_config_t *node_config, twa .enable_self_test = node_config->flags.enable_self_test, .enable_loopback = node_config->flags.enable_loopback, }; +#if CONFIG_IDF_TARGET_ESP32C5 + // ESP32C5 has a bug that the listen only mode don't work when there are other nodes sending ACK each other + // See IDF-13059 for more details + if (node_config->flags.enable_listen_only) { + ESP_LOGW(TAG, "Listen only mode for ESP32C5 may not work properly when there are more than 2 nodes on the bus that are sending ACKs to each other"); + } +#endif ESP_GOTO_ON_FALSE(twai_hal_init(node->hal, &hal_config), ESP_ERR_INVALID_STATE, err, TAG, "hardware not in reset state"); // Configure bus timing ESP_GOTO_ON_ERROR(_node_calc_set_bit_timing(&node->api_base, node_config->clk_src, &node_config->bit_timing, &node_config->data_timing), err, TAG, "bitrate error"); diff --git a/components/esp_driver_twai/test_apps/test_twai/main/test_twai_common.c b/components/esp_driver_twai/test_apps/test_twai/main/test_twai_common.c index 4fa3d8db80..6039900085 100644 --- a/components/esp_driver_twai/test_apps/test_twai/main/test_twai_common.c +++ b/components/esp_driver_twai/test_apps/test_twai/main/test_twai_common.c @@ -26,7 +26,7 @@ static IRAM_ATTR bool test_driver_install_rx_cb(twai_node_handle_t handle, const twai_rx_done_event_data_t *edata, void *user_ctx) { - twai_frame_t rx_frame; + twai_frame_t rx_frame = {0}; if (ESP_OK == twai_node_receive_from_isr(handle, &rx_frame)) { ESP_EARLY_LOGI("Recv ", "id 0x%lx rtr %d", rx_frame.header.id, rx_frame.header.rtr); } @@ -89,7 +89,7 @@ TEST_CASE("twai install uninstall (loopback)", "[twai]") TEST_ESP_OK(twai_node_enable(node_hdl[SOC_TWAI_CONTROLLER_NUM])); tx_frame.header.id = 0x100; TEST_ESP_OK(twai_node_transmit(node_hdl[SOC_TWAI_CONTROLLER_NUM], &tx_frame, 0)); - twai_frame_t rx_frame; + twai_frame_t rx_frame = {0}; printf("Test receive from task\n"); TEST_ESP_ERR(ESP_ERR_INVALID_STATE, twai_node_receive_from_isr(node_hdl[SOC_TWAI_CONTROLLER_NUM], &rx_frame)); diff --git a/components/hal/twai_hal_ctufd.c b/components/hal/twai_hal_ctufd.c index ecd12e58ba..e71cb923b5 100644 --- a/components/hal/twai_hal_ctufd.c +++ b/components/hal/twai_hal_ctufd.c @@ -4,6 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include #include "hal/twai_hal.h" #include "hal/twaifd_ll.h" @@ -118,17 +119,24 @@ void twai_hal_format_frame(const twai_hal_trans_desc_t *trans_desc, twai_hal_fra { const twai_frame_header_t *header = trans_desc->frame.header; int final_dlc = (header->dlc) ? header->dlc : twaifd_len2dlc(trans_desc->frame.buffer_len); - int data_len = (header->dlc) ? twaifd_dlc2len(header->dlc) : trans_desc->frame.buffer_len; twaifd_ll_format_frame_header(header, final_dlc, frame); - twaifd_ll_format_frame_data(trans_desc->frame.buffer, data_len, frame); + if (!header->rtr) { + int data_len = (header->dlc) ? twaifd_dlc2len(header->dlc) : trans_desc->frame.buffer_len; + data_len = (header->fdf) ? MIN(data_len, TWAIFD_FRAME_MAX_LEN) : MIN(data_len, TWAI_FRAME_MAX_LEN); + twaifd_ll_format_frame_data(trans_desc->frame.buffer, data_len, frame); + } } void twai_hal_parse_frame(const twai_hal_frame_t *frame, twai_frame_header_t *header, uint8_t *buffer, uint8_t buffer_len) { twaifd_ll_parse_frame_header(frame, header); - int frame_data_len = twaifd_dlc2len(header->dlc); - uint8_t final_len = (frame_data_len < buffer_len) ? frame_data_len : buffer_len; - twaifd_ll_parse_frame_data(frame, buffer, final_len); + if (!header->rtr) { + int frame_data_len = twaifd_dlc2len(header->dlc); + // limit data_len for twai classic non-iso mode. + frame_data_len = (header->fdf) ? MIN(frame_data_len, TWAIFD_FRAME_MAX_LEN) : MIN(frame_data_len, TWAI_FRAME_MAX_LEN); + uint8_t final_len = MIN(frame_data_len, buffer_len); + twaifd_ll_parse_frame_data(frame, buffer, final_len); + } } void twai_hal_set_tx_buffer_and_transmit(twai_hal_context_t *hal_ctx, twai_hal_frame_t *tx_frame, uint8_t buffer_idx) diff --git a/components/soc/esp32/twai_periph.c b/components/soc/esp32/twai_periph.c index cf763269fe..ae4d056e34 100644 --- a/components/soc/esp32/twai_periph.c +++ b/components/soc/esp32/twai_periph.c @@ -11,6 +11,7 @@ const twai_controller_signal_conn_t twai_controller_periph_signals = { .controllers = { [0] = { + .module_name = "TWAI0", .module = PERIPH_TWAI_MODULE, .irq_id = ETS_TWAI_INTR_SOURCE, .tx_sig = TWAI_TX_IDX, diff --git a/components/soc/esp32c3/twai_periph.c b/components/soc/esp32c3/twai_periph.c index bae7c6ec5d..1984188271 100644 --- a/components/soc/esp32c3/twai_periph.c +++ b/components/soc/esp32c3/twai_periph.c @@ -10,6 +10,7 @@ const twai_controller_signal_conn_t twai_controller_periph_signals = { .controllers = { [0] = { + .module_name = "TWAI0", .module = PERIPH_TWAI_MODULE, .irq_id = ETS_TWAI_INTR_SOURCE, .tx_sig = TWAI_TX_IDX, diff --git a/components/soc/esp32c6/twai_periph.c b/components/soc/esp32c6/twai_periph.c index 3066be7bf8..c184b2bac6 100644 --- a/components/soc/esp32c6/twai_periph.c +++ b/components/soc/esp32c6/twai_periph.c @@ -11,6 +11,7 @@ const twai_controller_signal_conn_t twai_controller_periph_signals = { .controllers = { [0] = { + .module_name = "TWAI0", .module = PERIPH_TWAI0_MODULE, .irq_id = ETS_TWAI0_INTR_SOURCE, .tx_sig = TWAI0_TX_IDX, @@ -20,6 +21,7 @@ const twai_controller_signal_conn_t twai_controller_periph_signals = { .stand_by_sig = TWAI0_STANDBY_IDX, }, [1] = { + .module_name = "TWAI1", .module = PERIPH_TWAI1_MODULE, .irq_id = ETS_TWAI1_INTR_SOURCE, .tx_sig = TWAI1_TX_IDX, diff --git a/components/soc/esp32h2/twai_periph.c b/components/soc/esp32h2/twai_periph.c index b81202a741..f8934a14f2 100644 --- a/components/soc/esp32h2/twai_periph.c +++ b/components/soc/esp32h2/twai_periph.c @@ -11,6 +11,7 @@ const twai_controller_signal_conn_t twai_controller_periph_signals = { .controllers = { [0] = { + .module_name = "TWAI0", .module = PERIPH_TWAI0_MODULE, .irq_id = ETS_TWAI0_INTR_SOURCE, .tx_sig = TWAI_TX_IDX, diff --git a/components/soc/esp32p4/twai_periph.c b/components/soc/esp32p4/twai_periph.c index b9fde3d047..9e6711c55f 100644 --- a/components/soc/esp32p4/twai_periph.c +++ b/components/soc/esp32p4/twai_periph.c @@ -11,6 +11,7 @@ const twai_controller_signal_conn_t twai_controller_periph_signals = { .controllers = { [0] = { + .module_name = "TWAI0", .module = PERIPH_TWAI0_MODULE, .irq_id = ETS_TWAI0_INTR_SOURCE, .tx_sig = TWAI0_TX_PAD_OUT_IDX, @@ -20,6 +21,7 @@ const twai_controller_signal_conn_t twai_controller_periph_signals = { .stand_by_sig = TWAI0_STANDBY_PAD_OUT_IDX, }, [1] = { + .module_name = "TWAI1", .module = PERIPH_TWAI1_MODULE, .irq_id = ETS_TWAI1_INTR_SOURCE, .tx_sig = TWAI1_TX_PAD_OUT_IDX, @@ -29,6 +31,7 @@ const twai_controller_signal_conn_t twai_controller_periph_signals = { .stand_by_sig = TWAI1_STANDBY_PAD_OUT_IDX, }, [2] = { + .module_name = "TWAI2", .module = PERIPH_TWAI2_MODULE, .irq_id = ETS_TWAI2_INTR_SOURCE, .tx_sig = TWAI2_TX_PAD_OUT_IDX, diff --git a/components/soc/esp32s2/twai_periph.c b/components/soc/esp32s2/twai_periph.c index bae7c6ec5d..1984188271 100644 --- a/components/soc/esp32s2/twai_periph.c +++ b/components/soc/esp32s2/twai_periph.c @@ -10,6 +10,7 @@ const twai_controller_signal_conn_t twai_controller_periph_signals = { .controllers = { [0] = { + .module_name = "TWAI0", .module = PERIPH_TWAI_MODULE, .irq_id = ETS_TWAI_INTR_SOURCE, .tx_sig = TWAI_TX_IDX, diff --git a/components/soc/esp32s3/twai_periph.c b/components/soc/esp32s3/twai_periph.c index bae7c6ec5d..1984188271 100644 --- a/components/soc/esp32s3/twai_periph.c +++ b/components/soc/esp32s3/twai_periph.c @@ -10,6 +10,7 @@ const twai_controller_signal_conn_t twai_controller_periph_signals = { .controllers = { [0] = { + .module_name = "TWAI0", .module = PERIPH_TWAI_MODULE, .irq_id = ETS_TWAI_INTR_SOURCE, .tx_sig = TWAI_TX_IDX, diff --git a/docs/en/api-reference/peripherals/twai.rst b/docs/en/api-reference/peripherals/twai.rst index 7e4160fad2..0a8c4070b1 100644 --- a/docs/en/api-reference/peripherals/twai.rst +++ b/docs/en/api-reference/peripherals/twai.rst @@ -90,6 +90,12 @@ Below are additional configuration fields of the :cpp:type:`twai_onchip_node_con - :cpp:member:`twai_onchip_node_config_t::flags::enable_listen_only`: Configures the node in listen-only mode. In this mode, the node only receives and does not transmit any dominant bits, including ACK and error frames. - :cpp:member:`twai_onchip_node_config_t::flags::no_receive_rtr`: When using filters, determines whether remote frames matching the ID pattern should be filtered out. +.. only:: esp32c5 + + .. note:: + + Note: The listen-only mode on ESP32C5 can't work properly when there are multiple nodes on the bus that are sending ACKs to each other. An alternative is to use transceiver which supports listen-only mode itself (e.g. TJA1145), and combine it with self-test mode enabled. + The :cpp:func:`twai_node_enable` function starts the TWAI controller. Once enabled, the controller is connected to the bus and can transmit messages. It also generates events upon receiving messages from other nodes on the bus or when bus errors are detected. The corresponding function, :cpp:func:`twai_node_disable`, immediately stops the node and disconnects it from the bus. Any ongoing transmissions will be aborted. When the node is re-enabled later, if there are pending transmissions in the queue, the driver will immediately initiate a new transmission attempt. @@ -202,7 +208,7 @@ The following code demonstrates how to configure a baud rate of 500 Kbit/s with .. code:: c twai_timing_advanced_config_t timing_cfg = { - .brp = 8, // Prescaler set to 8, time quantum = 80M / 8 = 10 MHz (1M Tq) + .brp = 8, // Prescaler set to 8, time quantum = 80M / 8 = 10 MHz (10M Tq) .prop_seg = 10, // Propagation segment .tseg_1 = 4, // Phase segment 1 .tseg_2 = 5, // Phase segment 2 @@ -254,7 +260,7 @@ The following code demonstrates how to calculate the MASK and configure a filter Dual Filter Mode """""""""""""""" - {IDF_TARGET_NAME} supports dual filter mode, which allows the hardware to be configured as two parallel independent 16-bit mask filters. By enabling this, more IDs can be received. Note that when filtering 29-bit extended IDs, each filter can only filter the upper 16 bits of the ID, while the remaining 13 bits are not filtered. The following code demonstrates how to configure dual filter mode using the function :cpp:func:`twai_make_dual_filter`: + {IDF_TARGET_NAME} supports dual filter mode, which allows the hardware to be configured as two parallel independent 16-bit mask filters. By enabling this, more IDs can be received. Note that using dual filter mode to filter 29-bit extended IDs, each filter can only filter the upper 16 bits of the ID, while the remaining 13 bits are not filtered. The following code demonstrates how to configure dual filter mode using the function :cpp:func:`twai_make_dual_filter`: .. code:: c diff --git a/docs/zh_CN/api-reference/peripherals/twai.rst b/docs/zh_CN/api-reference/peripherals/twai.rst index f37633995f..6547c08e53 100644 --- a/docs/zh_CN/api-reference/peripherals/twai.rst +++ b/docs/zh_CN/api-reference/peripherals/twai.rst @@ -90,6 +90,12 @@ TWAI 是一种适用于汽车和工业应用的高可靠性的多主机实时串 - :cpp:member:`twai_onchip_node_config_t::flags::enable_listen_only` 配置为监听模式,节点只接收,不发送任何显性位,包括 ACK 和错误帧。 - :cpp:member:`twai_onchip_node_config_t::flags::no_receive_rtr` 使用过滤器时是否同时过滤掉符合 ID 规则的远程帧。 +.. only:: esp32c5 + + .. note:: + + 注意: ESP32C5 的监听模式在总线上有多个节点相互发送 ACK 信号时无法正常工作。一种替代方案是使用本身支持监听模式的收发器(例如 TJA1145),并结合启用自测模式。 + 函数 :cpp:func:`twai_node_enable` 将启动 TWAI 控制器,此时 TWAI 控制器就连接到了总线,可以向总线发送报文。如果收到了总线上其他节点发送的报文,或者检测到了总线错误,也将产生相应事件。 与之对应的函数是 :cpp:func:`twai_node_disable`,该函数将立即停止节点工作并与总线断开,正在进行的传输将被中止。当下次重新启动时,如果发送队列中有未完成的任务,驱动将立即发起新的传输。 @@ -202,7 +208,7 @@ TWAI 报文有多种类型,由报头指定。一个典型的数据帧报文主 .. code:: c twai_timing_advanced_config_t timing_cfg = { - .brp = 8, // 预分频为 8,时间量子 80M/8=1M + .brp = 8, // 预分频为 8,时间量子 80M/8=10M .prop_seg = 10, .tseg_1 = 4, .tseg_2 = 5, @@ -227,7 +233,7 @@ TWAI 报文有多种类型,由报头指定。一个典型的数据帧报文主 TWAI 控制器硬件可以根据 ID 对报文进行过滤,从而减少软硬件开销使节点更加高效。过滤掉报文的节点 **不会接收到该报文,但仍会应答**。 -{IDF_TARGET_NAME} 包含 {IDF_TARGET_CONFIG_SOC_TWAI_MASK_FILTER_NUM} 个掩码过滤器,报文通过任意一个过滤器即能收到改报文。典型的 TWAI 掩码过滤器通过 ID 和 MASK 配置,其中: +{IDF_TARGET_NAME} 包含 {IDF_TARGET_CONFIG_SOC_TWAI_MASK_FILTER_NUM} 个掩码过滤器,报文通过任意一个过滤器即能收到该报文。典型的 TWAI 掩码过滤器通过 ID 和 MASK 配置,其中: - ID 表示期望接收的报文的标准11位或扩展29位ID。 - MASK 表示对ID的过滤规则: @@ -254,7 +260,7 @@ TWAI 控制器硬件可以根据 ID 对报文进行过滤,从而减少软硬 双过滤器模式 """""""""""" - {IDF_TARGET_NAME} 支持双过滤器模式,可将硬件配置为并列的两个独立的 16 位掩码过滤器,支持接收更多 ID,但当配置为过滤 29 位扩展ID时,每个过滤器只能过滤其ID的高 16 位,剩余13位不做过滤。以下代码展示了如何借助 :cpp:func:`twai_make_dual_filter` 配置双过滤器模式。 + {IDF_TARGET_NAME} 支持双过滤器模式,可将硬件配置为并列的两个独立的 16 位掩码过滤器,支持接收更多 ID。但注意,使用双过滤器模式过滤 29 位扩展ID时,每个过滤器只能过滤其ID的高 16 位,剩余13位不做过滤。以下代码展示了如何借助 :cpp:func:`twai_make_dual_filter` 配置双过滤器模式。 .. code:: c