feat(driver_twai): support using all 4 hardware tx buffer

This commit is contained in:
wanckl
2026-06-23 20:17:54 +08:00
parent 5223006ec7
commit 671606b9b5
18 changed files with 247 additions and 62 deletions
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -701,6 +701,17 @@ static inline void twai_ll_set_acc_filter(twai_dev_t *hw, uint32_t code, uint32_
/* ------------------------- TX/RX Buffer Registers ------------------------- */
/**
* @brief Get the number of TX buffers that are preset in the hardware.
*
* @param hw Pointer to the TWAI-FD device hardware.
* @return The number of TX buffers available.
*/
static inline uint32_t twai_ll_get_tx_buffer_total(twai_dev_t *hw)
{
return 1; // only one TX buffer
}
/**
* @brief Copy a formatted TWAI frame into TX buffer for transmission
*
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -627,6 +627,17 @@ static inline void twai_ll_set_acc_filter(twai_dev_t *hw, uint32_t code, uint32_
/* ------------------------- TX/RX Buffer Registers ------------------------- */
/**
* @brief Get the number of TX buffers that are preset in the hardware.
*
* @param hw Pointer to the TWAI-FD device hardware.
* @return The number of TX buffers available.
*/
static inline uint32_t twai_ll_get_tx_buffer_total(twai_dev_t *hw)
{
return 1; // only one TX buffer
}
/**
* @brief Copy a formatted TWAI frame into TX buffer for transmission
*
@@ -48,6 +48,10 @@
#define TWAIFD_LL_TX_CMD_READY TWAIFD_TXCR // Set tx buffer to "Ready" state
#define TWAIFD_LL_TX_CMD_ABORT TWAIFD_TXCA // Set tx buffer to "Aborted" state
#define TWAIFD_LL_TX_STATUS_SUCCESS 0x4 // TX buffer transmitted successfully
#define TWAIFD_LL_TX_STATUS_FAILED 0x6 // TX buffer transmission failed
#define TWAIFD_LL_TX_STATUS_ABORTED 0x7 // TX buffer transmission aborted
#define TWAIFD_LL_HW_CMD_RST_ERR_CNT TWAIFD_ERCRST // Error Counters Reset
#define TWAIFD_LL_HW_CMD_RST_RX_CNT TWAIFD_RXFCRST // Clear RX bus traffic counter
#define TWAIFD_LL_HW_CMD_RST_TX_CNT TWAIFD_TXFCRST // Clear TX bus traffic counter
@@ -665,11 +669,10 @@ static inline uint32_t twaifd_ll_get_tx_buffer_total(twaifd_dev_t *hw)
* @param buffer_idx Index of the TX buffer (0-7).
* @return The status of the selected TX buffer.
*/
__attribute__((always_inline))
static inline uint32_t twaifd_ll_get_tx_buffer_status(twaifd_dev_t *hw, uint8_t buffer_idx)
{
HAL_ASSERT(buffer_idx < twaifd_ll_get_tx_buffer_total(hw)); // Ensure buffer index is valid
uint32_t reg_val = hw->tx_status.val;
return reg_val & (TWAIFD_TX2S_V << (TWAIFD_TX2S_S * buffer_idx)); // Get status for buffer
return (hw->tx_status.val >> (TWAIFD_TX2S_S * buffer_idx)) & TWAIFD_TX2S_V;
}
/**
@@ -700,7 +703,6 @@ static inline void twaifd_ll_set_tx_buffer_cmd(twaifd_dev_t *hw, uint8_t buffer_
*/
static inline void twaifd_ll_set_tx_buffer_priority(twaifd_dev_t *hw, uint8_t buffer_idx, uint32_t priority)
{
HAL_ASSERT(buffer_idx < twaifd_ll_get_tx_buffer_total(hw)); // Ensure buffer index is valid
uint32_t reg_val = hw->tx_priority.val;
reg_val &= ~(TWAIFD_TXT1P_V << (TWAIFD_TXT2P_S * buffer_idx)); // Clear old priority
reg_val |= priority << (TWAIFD_TXT2P_S * buffer_idx); // Set new priority
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -637,6 +637,17 @@ static inline void twai_ll_set_acc_filter(twai_dev_t *hw, uint32_t code, uint32_
/* ------------------------- TX/RX Buffer Registers ------------------------- */
/**
* @brief Get the number of TX buffers that are preset in the hardware.
*
* @param hw Pointer to the TWAI-FD device hardware.
* @return The number of TX buffers available.
*/
static inline uint32_t twai_ll_get_tx_buffer_total(twai_dev_t *hw)
{
return 1; // only one TX buffer
}
/**
* @brief Copy a formatted TWAI frame into TX buffer for transmission
*
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -615,6 +615,17 @@ static inline void twai_ll_set_acc_filter(twai_dev_t *hw, uint32_t code, uint32_
/* ------------------------- TX/RX Buffer Registers ------------------------- */
/**
* @brief Get the number of TX buffers that are preset in the hardware.
*
* @param hw Pointer to the TWAI-FD device hardware.
* @return The number of TX buffers available.
*/
static inline uint32_t twai_ll_get_tx_buffer_total(twai_dev_t *hw)
{
return 1; // only one TX buffer
}
/**
* @brief Copy a formatted TWAI frame into TX buffer for transmission
*
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2025-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -615,6 +615,17 @@ static inline void twai_ll_set_acc_filter(twai_dev_t *hw, uint32_t code, uint32_
/* ------------------------- TX/RX Buffer Registers ------------------------- */
/**
* @brief Get the number of TX buffers that are preset in the hardware.
*
* @param hw Pointer to the TWAI-FD device hardware.
* @return The number of TX buffers available.
*/
static inline uint32_t twai_ll_get_tx_buffer_total(twai_dev_t *hw)
{
return 1; // only one TX buffer
}
/**
* @brief Copy a formatted TWAI frame into TX buffer for transmission
*
@@ -48,6 +48,10 @@
#define TWAIFD_LL_TX_CMD_READY TWAIFD_TXCR // Set tx buffer to "Ready" state
#define TWAIFD_LL_TX_CMD_ABORT TWAIFD_TXCA // Set tx buffer to "Aborted" state
#define TWAIFD_LL_TX_STATUS_SUCCESS 0x4 // TX buffer transmitted successfully
#define TWAIFD_LL_TX_STATUS_FAILED 0x6 // TX buffer transmission failed
#define TWAIFD_LL_TX_STATUS_ABORTED 0x7 // TX buffer transmission aborted
#define TWAIFD_LL_HW_CMD_RST_ERR_CNT TWAIFD_ERCRST // Error Counters Reset
#define TWAIFD_LL_HW_CMD_RST_RX_CNT TWAIFD_RXFCRST // Clear RX bus traffic counter
#define TWAIFD_LL_HW_CMD_RST_TX_CNT TWAIFD_TXFCRST // Clear TX bus traffic counter
@@ -664,11 +668,10 @@ static inline uint32_t twaifd_ll_get_tx_buffer_total(twaifd_dev_t *hw)
* @param buffer_idx Index of the TX buffer (0-7).
* @return The status of the selected TX buffer.
*/
__attribute__((always_inline))
static inline uint32_t twaifd_ll_get_tx_buffer_status(twaifd_dev_t *hw, uint8_t buffer_idx)
{
HAL_ASSERT(buffer_idx < twaifd_ll_get_tx_buffer_total(hw)); // Ensure buffer index is valid
uint32_t reg_val = hw->tx_status.val;
return reg_val & (TWAIFD_TX2S_V << (TWAIFD_TX2S_S * buffer_idx)); // Get status for buffer
return (hw->tx_status.val >> (TWAIFD_TX2S_S * buffer_idx)) & TWAIFD_TX2S_V;
}
/**
@@ -699,7 +702,6 @@ static inline void twaifd_ll_set_tx_buffer_cmd(twaifd_dev_t *hw, uint8_t buffer_
*/
static inline void twaifd_ll_set_tx_buffer_priority(twaifd_dev_t *hw, uint8_t buffer_idx, uint32_t priority)
{
HAL_ASSERT(buffer_idx < twaifd_ll_get_tx_buffer_total(hw)); // Ensure buffer index is valid
uint32_t reg_val = hw->tx_priority.val;
reg_val &= ~(TWAIFD_TXT1P_V << (TWAIFD_TXT2P_S * buffer_idx)); // Clear old priority
reg_val |= priority << (TWAIFD_TXT2P_S * buffer_idx); // Set new priority
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -680,6 +680,17 @@ static inline void twai_ll_set_acc_filter(twai_dev_t *hw, uint32_t code, uint32_
/* ------------------------- TX/RX Buffer Registers ------------------------- */
/**
* @brief Get the number of TX buffers that are preset in the hardware.
*
* @param hw Pointer to the TWAI-FD device hardware.
* @return The number of TX buffers available.
*/
static inline uint32_t twai_ll_get_tx_buffer_total(twai_dev_t *hw)
{
return 1; // only one TX buffer
}
/**
* @brief Copy a formatted TWAI frame into TX buffer for transmission
*
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -630,6 +630,17 @@ static inline void twai_ll_set_acc_filter(twai_dev_t *hw, uint32_t code, uint32_
/* ------------------------- TX/RX Buffer Registers ------------------------- */
/**
* @brief Get the number of TX buffers that are preset in the hardware.
*
* @param hw Pointer to the TWAI-FD device hardware.
* @return The number of TX buffers available.
*/
static inline uint32_t twai_ll_get_tx_buffer_total(twai_dev_t *hw)
{
return 1; // only one TX buffer
}
/**
* @brief Copy a formatted TWAI frame into TX buffer for transmission
*
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -627,6 +627,17 @@ static inline void twai_ll_set_acc_filter(twai_dev_t *hw, uint32_t code, uint32_
/* ------------------------- TX/RX Buffer Registers ------------------------- */
/**
* @brief Get the number of TX buffers that are preset in the hardware.
*
* @param hw Pointer to the TWAI-FD device hardware.
* @return The number of TX buffers available.
*/
static inline uint32_t twai_ll_get_tx_buffer_total(twai_dev_t *hw)
{
return 1; // only one TX buffer
}
/**
* @brief Copy a formatted TWAI frame into TX buffer for transmission
*
@@ -46,6 +46,10 @@
#define TWAIFD_LL_TX_CMD_READY TWAIFD_TXCR // Set tx buffer to "Ready" state
#define TWAIFD_LL_TX_CMD_ABORT TWAIFD_TXCA // Set tx buffer to "Aborted" state
#define TWAIFD_LL_TX_STATUS_SUCCESS 0x4 // TX buffer transmitted successfully
#define TWAIFD_LL_TX_STATUS_FAILED 0x6 // TX buffer transmission failed
#define TWAIFD_LL_TX_STATUS_ABORTED 0x7 // TX buffer transmission aborted
#define TWAIFD_LL_HW_CMD_RST_ERR_CNT TWAIFD_ERCRST // Error Counters Reset
#define TWAIFD_LL_HW_CMD_RST_RX_CNT TWAIFD_RXFCRST // Clear RX bus traffic counter
#define TWAIFD_LL_HW_CMD_RST_TX_CNT TWAIFD_TXFCRST // Clear TX bus traffic counter
@@ -662,11 +666,10 @@ static inline uint32_t twaifd_ll_get_tx_buffer_total(twaifd_dev_t *hw)
* @param buffer_idx Index of the TX buffer (0-7).
* @return The status of the selected TX buffer.
*/
__attribute__((always_inline))
static inline uint32_t twaifd_ll_get_tx_buffer_status(twaifd_dev_t *hw, uint8_t buffer_idx)
{
HAL_ASSERT(buffer_idx < twaifd_ll_get_tx_buffer_total(hw)); // Ensure buffer index is valid
uint32_t reg_val = hw->tx_status.val;
return reg_val & (TWAIFD_TX2S_V << (TWAIFD_TX2S_S * buffer_idx)); // Get status for buffer
return (hw->tx_status.val >> (TWAIFD_TX2S_S * buffer_idx)) & TWAIFD_TX2S_V;
}
/**
@@ -697,7 +700,6 @@ static inline void twaifd_ll_set_tx_buffer_cmd(twaifd_dev_t *hw, uint8_t buffer_
*/
static inline void twaifd_ll_set_tx_buffer_priority(twaifd_dev_t *hw, uint8_t buffer_idx, uint32_t priority)
{
HAL_ASSERT(buffer_idx < twaifd_ll_get_tx_buffer_total(hw)); // Ensure buffer index is valid
uint32_t reg_val = hw->tx_priority.val;
reg_val &= ~(TWAIFD_TXT1P_V << (TWAIFD_TXT2P_S * buffer_idx)); // Clear old priority
reg_val |= priority << (TWAIFD_TXT2P_S * buffer_idx); // Set new priority
+32 -3
View File
@@ -50,9 +50,29 @@ typedef union twai_ll_frame_buffer_t twai_hal_frame_t;
#define TWAI_HAL_EVENT_BUS_ERR (1 << 7)
#define TWAI_HAL_EVENT_ARB_LOST (1 << 8)
#define TWAI_HAL_EVENT_RX_BUFF_FRAME (1 << 9)
#define TWAI_HAL_EVENT_TX_BUFF_FREE (1 << 10)
#define TWAI_HAL_EVENT_NEED_PERIPH_RESET (1 << 11)
#define TWAI_HAL_EVENT_TX_SUCCESS (1 << 12)
#define TWAI_HAL_EVENT_NEED_PERIPH_RESET (1 << 10)
#define TWAI_HAL_EVENT_TX0_DONE (1 << 11)
#define TWAI_HAL_EVENT_TX0_SUCCESS (1 << 12)
#define TWAI_HAL_EVENT_TX1_DONE (1 << 13)
#define TWAI_HAL_EVENT_TX1_SUCCESS (1 << 14)
#define TWAI_HAL_EVENT_TX2_DONE (1 << 15)
#define TWAI_HAL_EVENT_TX2_SUCCESS (1 << 16)
#define TWAI_HAL_EVENT_TX3_DONE (1 << 17)
#define TWAI_HAL_EVENT_TX3_SUCCESS (1 << 18)
#define TWAI_HAL_EVENT_TX4_DONE (1 << 19)
#define TWAI_HAL_EVENT_TX4_SUCCESS (1 << 20)
#define TWAI_HAL_EVENT_TX5_DONE (1 << 21)
#define TWAI_HAL_EVENT_TX5_SUCCESS (1 << 22)
#define TWAI_HAL_EVENT_TX6_DONE (1 << 23)
#define TWAI_HAL_EVENT_TX6_SUCCESS (1 << 24)
#define TWAI_HAL_EVENT_TX7_DONE (1 << 25)
#define TWAI_HAL_EVENT_TX7_SUCCESS (1 << 26)
#define TWAI_HAL_TX_BUFFER_SLOT_NUM 8 // support up to 8 TX slots in hal layer
#define TWAI_HAL_EVENT_TX_DONE_MASK (TWAI_HAL_EVENT_TX0_DONE | TWAI_HAL_EVENT_TX1_DONE | TWAI_HAL_EVENT_TX2_DONE | TWAI_HAL_EVENT_TX3_DONE | \
TWAI_HAL_EVENT_TX4_DONE | TWAI_HAL_EVENT_TX5_DONE | TWAI_HAL_EVENT_TX6_DONE | TWAI_HAL_EVENT_TX7_DONE)
#define TWAI_HAL_EVENT_TX_DONE_SLOT(buffer_idx) (TWAI_HAL_EVENT_TX0_DONE << ((buffer_idx) * 2))
#define TWAI_HAL_EVENT_TX_SUCC_SLOT(buffer_idx) (TWAI_HAL_EVENT_TX0_SUCCESS << ((buffer_idx) * 2))
typedef struct {
twai_soc_handle_t dev; // TWAI SOC layer handle (i.e. register base address)
@@ -61,6 +81,7 @@ typedef struct {
uint32_t timer_overflow_cnt;
twai_error_flags_t errors;
uint8_t sja1000_filter_id_type; // hardware don't check id type, check in software, 0:no_filter, 1: std_id_only, 2: ext_id_only
uint8_t tx_buffer_num;
int8_t retry_cnt;
bool enable_self_test;
bool enable_loopback;
@@ -296,6 +317,14 @@ static inline twai_error_flags_t twai_hal_get_err_flags(twai_hal_context_t *hal_
*/
uint32_t twai_hal_get_rx_msg_count(twai_hal_context_t *hal_ctx);
/**
* @brief Get the number of TX buffers supported by the hardware
*
* @param hal_ctx Context of the HAL layer
* @return TX buffer count
*/
#define twai_hal_get_tx_slot_num(hal_ctx) (hal_ctx->tx_buffer_num)
/**
* @brief TWAI hal transaction description type
*/
+3 -2
View File
@@ -50,6 +50,7 @@ bool twai_hal_init(twai_hal_context_t *hal_ctx, const twai_hal_config_t *config)
if (!twai_ll_is_in_reset_mode(hal_ctx->dev)) { //Must enter reset mode to write to config registers
return false;
}
hal_ctx->tx_buffer_num = twai_ll_get_tx_buffer_total(hal_ctx->dev);
#if TWAI_LL_HAS_RX_FRAME_ISSUE || TWAI_LL_HAS_RX_FIFO_ISSUE
hal_ctx->errata_ctx = (twai_hal_errata_ctx_t *)(hal_ctx + 1); //errata context is place at end of hal_ctx
#endif
@@ -238,10 +239,10 @@ static inline uint32_t twai_hal_decode_interrupt(twai_hal_context_t *hal_ctx)
#else
if (interrupts & TWAI_LL_INTR_TI) {
#endif
TWAI_HAL_SET_BITS(events, TWAI_HAL_EVENT_TX_BUFF_FREE);
TWAI_HAL_SET_BITS(events, TWAI_HAL_EVENT_TX0_DONE);
TWAI_HAL_CLEAR_BITS(state_flags, TWAI_HAL_STATE_FLAG_TX_BUFF_OCCUPIED);
if (status & TWAI_LL_STATUS_TCS) {
TWAI_HAL_SET_BITS(events, TWAI_HAL_EVENT_TX_SUCCESS);
TWAI_HAL_SET_BITS(events, TWAI_HAL_EVENT_TX0_SUCCESS);
}
}
//Error Passive Interrupt on transition from error active to passive or vice versa
+15 -3
View File
@@ -22,6 +22,7 @@ bool twai_hal_init(twai_hal_context_t *hal_ctx, const twai_hal_config_t *config)
hal_ctx->enable_listen_only = config->enable_listen_only;
twaifd_ll_reset(hal_ctx->dev);
hal_ctx->tx_buffer_num = twaifd_ll_get_tx_buffer_total(hal_ctx->dev);
twaifd_ll_enable_hw(hal_ctx->dev, false); //mode should be changed under disabled
twaifd_ll_set_mode(hal_ctx->dev, config->enable_listen_only, config->enable_self_test, config->enable_loopback);
twaifd_ll_set_tx_retrans_limit(hal_ctx->dev, config->retry_cnt);
@@ -222,9 +223,20 @@ uint32_t twai_hal_get_events(twai_hal_context_t *hal_ctx)
hal_ctx->timer_overflow_cnt ++;
}
if (int_stat & (TWAIFD_LL_INTR_TX_DONE)) {
hal_events |= TWAI_HAL_EVENT_TX_BUFF_FREE;
if (int_stat & TWAIFD_LL_INTR_TX_FRAME) {
hal_events |= TWAI_HAL_EVENT_TX_SUCCESS;
for (uint32_t i = 0; i < MIN(hal_ctx->tx_buffer_num, TWAI_HAL_TX_BUFFER_SLOT_NUM); i++) {
uint32_t tx_status = twaifd_ll_get_tx_buffer_status(hal_ctx->dev, i);
switch (tx_status) {
case TWAIFD_LL_TX_STATUS_SUCCESS:
hal_events |= TWAI_HAL_EVENT_TX_SUCC_SLOT(i);
__attribute__((fallthrough)); // success event must be a done event, just fallthrough
case TWAIFD_LL_TX_STATUS_FAILED:
case TWAIFD_LL_TX_STATUS_ABORTED:
hal_events |= TWAI_HAL_EVENT_TX_DONE_SLOT(i);
twaifd_ll_set_tx_buffer_cmd(hal_ctx->dev, i, TWAIFD_LL_TX_CMD_EMPTY); // clear buffer to empty state
break;
default:
break;
}
}
}
if (int_stat & TWAIFD_LL_INTR_RX_NOT_EMPTY) {