Skip to content

Commit 9eacbc1

Browse files
committed
Merge branch 'contrib/github_pr_17888' into 'master'
feat(driver_twai): support get queue remaining and improve logs and docs (GitHub PR) Closes IDFGH-16811, IDFGH-16807, and IDFGH-16805 See merge request espressif/esp-idf!43623
2 parents 9a21040 + 1437506 commit 9eacbc1

File tree

6 files changed

+34
-13
lines changed

6 files changed

+34
-13
lines changed

components/esp_driver_twai/esp_twai_onchip.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ static esp_err_t _node_get_status(twai_node_handle_t node, twai_node_status_t *s
572572
status_ret->state = atomic_load(&twai_ctx->state);
573573
status_ret->tx_error_count = twai_hal_get_tec(twai_ctx->hal);
574574
status_ret->rx_error_count = twai_hal_get_rec(twai_ctx->hal);
575+
status_ret->tx_queue_remaining = uxQueueSpacesAvailable(twai_ctx->tx_mount_queue);
575576
}
576577
if (record_ret) {
577578
*record_ret = twai_ctx->history;
@@ -585,7 +586,7 @@ static esp_err_t _node_queue_tx(twai_node_handle_t node, const twai_frame_t *fra
585586
{
586587
twai_onchip_ctx_t *twai_ctx = __containerof(node, twai_onchip_ctx_t, api_base);
587588
if (frame->header.dlc && frame->buffer_len) {
588-
ESP_RETURN_ON_FALSE_ISR(frame->header.dlc == twaifd_len2dlc(frame->buffer_len), ESP_ERR_INVALID_ARG, TAG, "unmatched dlc and buffer_len");
589+
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));
589590
}
590591
#if !SOC_TWAI_SUPPORT_FD
591592
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");

components/esp_driver_twai/include/esp_twai.h

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ extern "C" {
1818
* @brief Enable the TWAI node
1919
*
2020
* @param node Handle to the TWAI node
21-
* @return ESP_OK on success, error code otherwise
21+
* @return - ESP_OK: Success
22+
* - ESP_ERR_INVALID_STATE: Node already in enabled state
2223
*/
2324
esp_err_t twai_node_enable(twai_node_handle_t node);
2425

2526
/**
2627
* @brief Disable the TWAI node
2728
*
2829
* @param node Handle to the TWAI node
29-
* @return ESP_OK on success, error code otherwise
30+
* @return - ESP_OK: Success
31+
* - ESP_ERR_INVALID_STATE: Node not in enabled state
3032
*/
3133
esp_err_t twai_node_disable(twai_node_handle_t node);
3234

@@ -35,15 +37,17 @@ esp_err_t twai_node_disable(twai_node_handle_t node);
3537
* @note Follow `on_state_change` callback or `twai_node_get_info` to know recover finish or not
3638
*
3739
* @param node Handle to the TWAI node
38-
* @return ESP_OK on success, error code otherwise
40+
* @return - ESP_OK: Success
41+
* - ESP_ERR_INVALID_STATE: Node not in bus-off state
3942
*/
4043
esp_err_t twai_node_recover(twai_node_handle_t node);
4144

4245
/**
4346
* @brief Delete the TWAI node and release resources
4447
*
4548
* @param node Handle to the TWAI node
46-
* @return ESP_OK on success, error code otherwise
49+
* @return - ESP_OK: Success
50+
* - ESP_ERR_INVALID_STATE: Node not in disabled state
4751
*/
4852
esp_err_t twai_node_delete(twai_node_handle_t node);
4953

@@ -75,7 +79,9 @@ esp_err_t twai_node_reconfig_timing(twai_node_handle_t node, const twai_timing_a
7579
* @param node Handle to the TWAI node
7680
* @param filter_id Index of the filter to configure
7781
* @param mask_cfg Pointer to the mask filter configuration
78-
* @return ESP_OK on success, error code otherwise
82+
* @return - ESP_OK: Success
83+
* - ESP_ERR_INVALID_ARG: Invalid argument
84+
* - ESP_ERR_INVALID_STATE: Node not in disabled state
7985
*/
8086
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);
8187

@@ -85,7 +91,9 @@ esp_err_t twai_node_config_mask_filter(twai_node_handle_t node, uint8_t filter_i
8591
* @param node Handle to the TWAI node
8692
* @param filter_id Index of the filter to configure
8793
* @param range_cfg Pointer to the range filter configuration
88-
* @return ESP_OK on success, error code otherwise
94+
* @return - ESP_OK: Success
95+
* - ESP_ERR_INVALID_ARG: Invalid argument
96+
* - ESP_ERR_INVALID_STATE: Node not in disabled state
8997
*/
9098
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);
9199

@@ -107,7 +115,11 @@ esp_err_t twai_node_get_info(twai_node_handle_t node, twai_node_status_t *status
107115
* @param[in] node Handle to the TWAI node
108116
* @param[in] frame Pointer to the frame to transmit
109117
* @param[in] timeout_ms Maximum wait time if the transmission queue is full (milliseconds), -1 to wait forever
110-
* @return ESP_OK on success, error code otherwise
118+
* @return - ESP_OK: Success
119+
* - ESP_ERR_INVALID_ARG: Invalid argument
120+
* - ESP_ERR_INVALID_STATE: Node already in bus-off state
121+
* - ESP_ERR_NOT_SUPPORTED: Node is config as listen only
122+
* - ESP_ERR_TIMEOUT: Timeout to wait for queue space
111123
*/
112124
esp_err_t twai_node_transmit(twai_node_handle_t node, const twai_frame_t *frame, int timeout_ms);
113125

@@ -116,7 +128,9 @@ esp_err_t twai_node_transmit(twai_node_handle_t node, const twai_frame_t *frame,
116128
*
117129
* @param[in] node Handle to the TWAI node
118130
* @param[in] timeout_ms Maximum wait time for all pending transfers to finish (milliseconds), -1 to wait forever
119-
* @return ESP_OK on success, error code otherwise
131+
* @return - ESP_OK: Success
132+
* - ESP_ERR_INVALID_STATE: Node already in bus-off state
133+
* - ESP_ERR_TIMEOUT: Timeout
120134
*/
121135
esp_err_t twai_node_transmit_wait_all_done(twai_node_handle_t node, int timeout_ms);
122136

@@ -129,7 +143,8 @@ esp_err_t twai_node_transmit_wait_all_done(twai_node_handle_t node, int timeout_
129143
*
130144
* @param[in] node Handle to the TWAI node
131145
* @param[out] rx_frame Pointer to the frame store rx content
132-
* @return ESP_OK on success, error code otherwise
146+
* @return - ESP_OK: Success
147+
* - ESP_ERR_INVALID_STATE: Called from a task or from other callbacks except `rx_done_cb`
133148
*/
134149
esp_err_t twai_node_receive_from_isr(twai_node_handle_t node, twai_frame_t *rx_frame);
135150

components/esp_driver_twai/include/esp_twai_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ typedef struct {
4242
twai_error_state_t state; /**< Node's error state */
4343
uint16_t tx_error_count; /**< Node's TX error count */
4444
uint16_t rx_error_count; /**< Node's RX error count */
45+
uint32_t tx_queue_remaining; /**< Node's TX free queue space */
4546
} twai_node_status_t;
4647

4748
/**

components/esp_driver_twai/test_apps/test_twai/main/test_twai_common.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,11 @@ TEST_CASE("twai transmit stop resume (loopback)", "[twai]")
225225
TEST_ESP_OK(twai_node_enable(node_hdl));
226226

227227
//waiting pkg receive finish
228-
TEST_ESP_OK(twai_node_transmit_wait_all_done(node_hdl, -1));
228+
twai_node_status_t status = {};
229+
while (status.tx_queue_remaining < TEST_TWAI_QUEUE_DEPTH) {
230+
TEST_ESP_OK(twai_node_get_info(node_hdl, &status, NULL));
231+
printf("%ld\n", status.tx_queue_remaining);
232+
}
229233
free(tx_msgs);
230234

231235
// check if pkg receive correct

docs/en/api-reference/peripherals/twai.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ The TWAI controller can detect errors caused by bus interference or corrupted fr
311311
- **Error Passive**: When either TEC or REC is greater than or equal to 128, the node enters the passive error state. It can still communicate on the bus but sends only one **passive error flag** when detecting errors.
312312
- **Bus Off**: When **TEC** is greater than or equal to 256, the node enters the bus off (offline) state. The node is effectively disconnected and does not affect the bus. It remains offline until recovery is triggered by software.
313313

314-
Software can retrieve the node status anytime via the function :cpp:func:`twai_node_get_info`. When the controller detects errors, it triggers the :cpp:member:`twai_event_callbacks_t::on_error` callback, where the error data provides detailed information.
314+
Software can retrieve the node status from tasks via the function :cpp:func:`twai_node_get_info`. When the controller detects errors, it triggers the :cpp:member:`twai_event_callbacks_t::on_error` callback, where the error data provides detailed information.
315315

316316
When the node’s error state changes, the :cpp:member:`twai_event_callbacks_t::on_state_change` callback is triggered, allowing the application to respond to the state transition. If the node is offline and needs recovery, call :cpp:func:`twai_node_recover` from a task context. **Note that recovery is not immediate; the controller will automatically reconnect to the bus only after detecting 129 consecutive recessive bits (11 bits each).**
317317

docs/zh_CN/api-reference/peripherals/twai.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ TWAI控制器能够检测由于总线干扰产生的/损坏的不符合帧格式
311311
- **被动错误:** 当 TEC 或 REC 中的一个大于或等于 128 时,节点处于被动错误状态。仍可以参与总线通信,但在检测到错误时,只能发送一次 **被动错误标志**。
312312
- **离线:** 当 **TEC** 大于或等于 256 时,节点进入离线状态。离线的节点相当于断开连接,不会对总线产生任何影响。节点将保持离线状态,直到软件触发恢复操作。
313313

314-
软件可随时使用函数 :cpp:func:`twai_node_get_info` 获取节点状态。或当控制器检测到错误时,会产生 :cpp:member:`twai_event_callbacks_t::on_error` 回调,可通过传参中的错误数据查看错误原因。
314+
软件可随时在 task 中使用函数 :cpp:func:`twai_node_get_info` 获取节点状态。或当控制器检测到错误时,会产生 :cpp:member:`twai_event_callbacks_t::on_error` 回调,可通过传参中的错误数据查看错误原因。
315315

316316
当错误导致节点状态变化时,会进入 :cpp:member:`twai_event_callbacks_t::on_state_change` 回调,可在回调中查看节点的状态变化。若节点已经离线且需要恢复,需要在task中调用 :cpp:func:`twai_node_recover`。 **但注意,控制器不会立即恢复** ,需要在检测到 129 次连续 11 个隐性位后才会自动重新连接到总线。
317317

0 commit comments

Comments
 (0)