From 6cb2ec72c393f91239ac76c03a0a1cdf9455f656 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 09:51:44 +0300 Subject: [PATCH] added alerts --- main/can.c | 16 +++++++++++++--- main/cmd_can.c | 1 - main/cmd_can.h | 2 +- main/console.c | 2 +- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/main/can.c b/main/can.c index cf6f42d..fb5d9f8 100644 --- a/main/can.c +++ b/main/can.c @@ -73,8 +73,16 @@ void can_task(void* arg) { twai_message_t rx_msg; char data_bytes_str[50]; for (;;) { // A Task shall never return or exit. - curr_can_state = get_can_state(); if (twai_read_alerts(&alerts, 0) == ESP_OK) { + if (alerts & TWAI_ALERT_ERR_ACTIVE) { + is_error_passive = false; + } + if (alerts & TWAI_ALERT_ERR_PASS) { + is_error_passive = true; + } + if (alerts & TWAI_ALERT_BUS_ERROR) { + ESP_LOGE(LOG_TAG, "CAN error!"); + } if (alerts & TWAI_ALERT_BUS_OFF) { ESP_LOGE(LOG_TAG, "CAN went bus-off!"); // ESP_ERROR_CHECK(twai_initiate_recovery()); @@ -84,12 +92,14 @@ void can_task(void* arg) { // ESP_ERROR_CHECK(twai_start()); } } + curr_can_state = get_can_state(); if (xSemaphoreTake(can_mutex, 0) == pdTRUE) { - if (twai_receive(&rx_msg, can_task_timeout) == ESP_OK) { + while (twai_receive(&rx_msg, 0) == ESP_OK) { can_msg_to_str(&rx_msg, data_bytes_str); xprintf(LOG_COLOR(LOG_COLOR_BLUE) "recv %s\n" LOG_RESET_COLOR, data_bytes_str); } xSemaphoreGive(can_mutex); - } else vTaskDelay(can_task_timeout); + } + vTaskDelay(can_task_timeout); } } diff --git a/main/cmd_can.c b/main/cmd_can.c index 34bc261..7814dca 100644 --- a/main/cmd_can.c +++ b/main/cmd_can.c @@ -1,6 +1,5 @@ #include "cmd_can.h" #include "esp_log.h" -#include "freertos/portmacro.h" #include "inttypes.h" #include "freertos/projdefs.h" #include "string.h" diff --git a/main/cmd_can.h b/main/cmd_can.h index 66be1f4..0e1f2e3 100644 --- a/main/cmd_can.h +++ b/main/cmd_can.h @@ -13,7 +13,7 @@ static const twai_general_config_t g_config = { .bus_off_io = TWAI_IO_UNUSED, .tx_queue_len = 5, .rx_queue_len = 5, - .alerts_enabled = TWAI_ALERT_AND_LOG | TWAI_ALERT_ALL, + .alerts_enabled = TWAI_ALERT_ERR_ACTIVE | TWAI_ALERT_BUS_RECOVERED | TWAI_ALERT_BUS_ERROR | TWAI_ALERT_ERR_PASS | TWAI_ALERT_BUS_OFF, .clkout_divider = 0, .intr_flags = ESP_INTR_FLAG_LEVEL1, }; diff --git a/main/console.c b/main/console.c index dfcd36f..edf8515 100644 --- a/main/console.c +++ b/main/console.c @@ -154,7 +154,7 @@ void initialize_console(void) { console_config.max_cmdline_length = 256; if (use_colors) console_config.hint_color = atoi(LOG_COLOR_CYAN); ESP_ERROR_CHECK(esp_console_init(&console_config)); - linenoiseSetMultiLine(1); + // linenoiseSetMultiLine(1); linenoiseSetCompletionCallback(&esp_console_get_completion); linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint); linenoiseHistorySetMaxLen(100);