chor(twai): improve error logging in _node_queue_tx function

Enhanced error message for unmatched dlc and buffer_len.

Merges https://github.com/espressif/esp-idf/pull/17888
This commit is contained in:
diplfranzhoepfinger
2025-12-23 16:09:39 +08:00
committed by wanckl
parent e0577ae677
commit 4f1cf6a5d2
6 changed files with 34 additions and 13 deletions
+2 -1
View File
@@ -569,6 +569,7 @@ static esp_err_t _node_get_status(twai_node_handle_t node, twai_node_status_t *s
status_ret->state = atomic_load(&twai_ctx->state);
status_ret->tx_error_count = twai_hal_get_tec(twai_ctx->hal);
status_ret->rx_error_count = twai_hal_get_rec(twai_ctx->hal);
status_ret->tx_queue_remaining = uxQueueSpacesAvailable(twai_ctx->tx_mount_queue);
}
if (record_ret) {
*record_ret = twai_ctx->history;
@@ -582,7 +583,7 @@ static esp_err_t _node_queue_tx(twai_node_handle_t node, const twai_frame_t *fra
{
twai_onchip_ctx_t *twai_ctx = __containerof(node, twai_onchip_ctx_t, api_base);
if (frame->header.dlc && frame->buffer_len) {
ESP_RETURN_ON_FALSE_ISR(frame->header.dlc == twaifd_len2dlc(frame->buffer_len), ESP_ERR_INVALID_ARG, TAG, "unmatched dlc and buffer_len");
ESP_RETURN_ON_FALSE_ISR(frame->header.dlc == twaifd_len2dlc(frame->buffer_len), ESP_ERR_INVALID_ARG, TAG, "unmatched dlc(%i) and buffer_len(%i)", frame->header.dlc, twaifd_len2dlc(frame->buffer_len));
}
#if !SOC_TWAI_SUPPORT_FD
ESP_RETURN_ON_FALSE_ISR(!frame->header.fdf || frame->buffer_len <= TWAI_FRAME_MAX_LEN, ESP_ERR_INVALID_ARG, TAG, "fdf flag or buffer_len not supported");
+24 -9
View File
@@ -18,7 +18,8 @@ extern "C" {
* @brief Enable the TWAI node
*
* @param node Handle to the TWAI node
* @return ESP_OK on success, error code otherwise
* @return - ESP_OK: Success
* - ESP_ERR_INVALID_STATE: Node already in enabled state
*/
esp_err_t twai_node_enable(twai_node_handle_t node);
@@ -26,7 +27,8 @@ esp_err_t twai_node_enable(twai_node_handle_t node);
* @brief Disable the TWAI node
*
* @param node Handle to the TWAI node
* @return ESP_OK on success, error code otherwise
* @return - ESP_OK: Success
* - ESP_ERR_INVALID_STATE: Node not in enabled state
*/
esp_err_t twai_node_disable(twai_node_handle_t node);
@@ -35,7 +37,8 @@ esp_err_t twai_node_disable(twai_node_handle_t node);
* @note Follow `on_state_change` callback or `twai_node_get_info` to know recover finish or not
*
* @param node Handle to the TWAI node
* @return ESP_OK on success, error code otherwise
* @return - ESP_OK: Success
* - ESP_ERR_INVALID_STATE: Node not in bus-off state
*/
esp_err_t twai_node_recover(twai_node_handle_t node);
@@ -43,7 +46,8 @@ esp_err_t twai_node_recover(twai_node_handle_t node);
* @brief Delete the TWAI node and release resources
*
* @param node Handle to the TWAI node
* @return ESP_OK on success, error code otherwise
* @return - ESP_OK: Success
* - ESP_ERR_INVALID_STATE: Node not in disabled state
*/
esp_err_t twai_node_delete(twai_node_handle_t node);
@@ -75,7 +79,9 @@ esp_err_t twai_node_reconfig_timing(twai_node_handle_t node, const twai_timing_a
* @param node Handle to the TWAI node
* @param filter_id Index of the filter to configure
* @param mask_cfg Pointer to the mask filter configuration
* @return ESP_OK on success, error code otherwise
* @return - ESP_OK: Success
* - ESP_ERR_INVALID_ARG: Invalid argument
* - ESP_ERR_INVALID_STATE: Node not in disabled state
*/
esp_err_t twai_node_config_mask_filter(twai_node_handle_t node, uint8_t filter_id, const twai_mask_filter_config_t *mask_cfg);
@@ -85,7 +91,9 @@ esp_err_t twai_node_config_mask_filter(twai_node_handle_t node, uint8_t filter_i
* @param node Handle to the TWAI node
* @param filter_id Index of the filter to configure
* @param range_cfg Pointer to the range filter configuration
* @return ESP_OK on success, error code otherwise
* @return - ESP_OK: Success
* - ESP_ERR_INVALID_ARG: Invalid argument
* - ESP_ERR_INVALID_STATE: Node not in disabled state
*/
esp_err_t twai_node_config_range_filter(twai_node_handle_t node, uint8_t filter_id, const twai_range_filter_config_t *range_cfg);
@@ -107,7 +115,11 @@ esp_err_t twai_node_get_info(twai_node_handle_t node, twai_node_status_t *status
* @param[in] node Handle to the TWAI node
* @param[in] frame Pointer to the frame to transmit
* @param[in] timeout_ms Maximum wait time if the transmission queue is full (milliseconds), -1 to wait forever
* @return ESP_OK on success, error code otherwise
* @return - ESP_OK: Success
* - ESP_ERR_INVALID_ARG: Invalid argument
* - ESP_ERR_INVALID_STATE: Node already in bus-off state
* - ESP_ERR_NOT_SUPPORTED: Node is config as listen only
* - ESP_ERR_TIMEOUT: Timeout to wait for queue space
*/
esp_err_t twai_node_transmit(twai_node_handle_t node, const twai_frame_t *frame, int timeout_ms);
@@ -116,7 +128,9 @@ esp_err_t twai_node_transmit(twai_node_handle_t node, const twai_frame_t *frame,
*
* @param[in] node Handle to the TWAI node
* @param[in] timeout_ms Maximum wait time for all pending transfers to finish (milliseconds), -1 to wait forever
* @return ESP_OK on success, error code otherwise
* @return - ESP_OK: Success
* - ESP_ERR_INVALID_STATE: Node already in bus-off state
* - ESP_ERR_TIMEOUT: Timeout
*/
esp_err_t twai_node_transmit_wait_all_done(twai_node_handle_t node, int timeout_ms);
@@ -129,7 +143,8 @@ esp_err_t twai_node_transmit_wait_all_done(twai_node_handle_t node, int timeout_
*
* @param[in] node Handle to the TWAI node
* @param[out] rx_frame Pointer to the frame store rx content
* @return ESP_OK on success, error code otherwise
* @return - ESP_OK: Success
* - ESP_ERR_INVALID_STATE: Called from a task or from other callbacks except `rx_done_cb`
*/
esp_err_t twai_node_receive_from_isr(twai_node_handle_t node, twai_frame_t *rx_frame);
@@ -42,6 +42,7 @@ typedef struct {
twai_error_state_t state; /**< Node's error state */
uint16_t tx_error_count; /**< Node's TX error count */
uint16_t rx_error_count; /**< Node's RX error count */
uint32_t tx_queue_remaining; /**< Node's TX free queue space */
} twai_node_status_t;
/**
@@ -230,7 +230,11 @@ TEST_CASE("twai transmit stop resume (loopback)", "[twai]")
TEST_ESP_OK(twai_node_enable(node_hdl));
//waiting pkg receive finish
TEST_ESP_OK(twai_node_transmit_wait_all_done(node_hdl, -1));
twai_node_status_t status = {};
while (status.tx_queue_remaining < TEST_TWAI_QUEUE_DEPTH) {
TEST_ESP_OK(twai_node_get_info(node_hdl, &status, NULL));
printf("%ld\n", status.tx_queue_remaining);
}
free(tx_msgs);
// check if pkg receive correct