From ebca79b6761a9759643aa54d99094eb4daaa984d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=BB=D0=B0=20=D0=93=D0=BE=D1=80?= =?UTF-8?q?=D0=BD=D1=83=D1=88=D0=BA=D0=BE?= Date: Tue, 28 Nov 2023 13:59:41 +0300 Subject: [PATCH] minimal can receive delay --- main/can.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/main/can.c b/main/can.c index 20cc220..4166494 100644 --- a/main/can.c +++ b/main/can.c @@ -1,5 +1,6 @@ #include "can.h" #include "esp_log.h" +#include "freertos/portmacro.h" #include "freertos/projdefs.h" #include "sdkconfig.h" #include @@ -65,8 +66,10 @@ void can_msg_to_str(twai_message_t *can_msg, char *start_str, char *out_str) { // TODO: add software filtering void can_task(void* arg) { - static const TickType_t can_task_timeout = pdMS_TO_TICKS(10); + static const TickType_t can_task_timeout = pdMS_TO_TICKS(100); uint32_t alerts = 0; + esp_err_t ret = ESP_OK; + BaseType_t sem_res; can_mutex = xSemaphoreCreateMutex(); twai_message_t rx_msg; char data_bytes_str[70]; @@ -91,13 +94,16 @@ void can_task(void* arg) { } } curr_can_state = get_can_state(); - if (xSemaphoreTake(can_mutex, 0) == pdTRUE) { - while (twai_receive(&rx_msg, 0) == ESP_OK) { + sem_res = xSemaphoreTake(can_mutex, 0); + if (sem_res == pdTRUE) { + while ((ret = twai_receive(&rx_msg, can_task_timeout)) == ESP_OK) { can_msg_to_str(&rx_msg, "recv ", data_bytes_str); print_w_clr_time(data_bytes_str, LOG_COLOR_BLUE, false); } xSemaphoreGive(can_mutex); } - vTaskDelay(can_task_timeout); + if (sem_res != pdTRUE || ret == ESP_ERR_INVALID_STATE || ret == ESP_ERR_NOT_SUPPORTED) { + vTaskDelay(can_task_timeout); + } } }