From b95a972949ee47671d2f10d09842dc65b3bc1fa9 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 14:40:41 +0300 Subject: [PATCH] better alarms and can recovery --- README.md | 1 - main/can.c | 11 +++++++++-- main/can.h | 1 + main/cmd_can.c | 7 ++++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4cb9f31..ecf48ce 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ TODO: add caninstall and canstart canup/caninstall parameters (without filtering) add filtering (software and hardware) - better alarms handling, can recovering better history clear history and clear screen commands code refactoring diff --git a/main/can.c b/main/can.c index 1b954f6..c39efa4 100644 --- a/main/can.c +++ b/main/can.c @@ -10,6 +10,7 @@ #include "xvprintf.h" bool is_error_passive = false; +bool auto_recovery = false; SemaphoreHandle_t can_mutex; volatile can_status_t curr_can_state = { 0 }; @@ -86,11 +87,17 @@ void can_task(void* arg) { } if (alerts & TWAI_ALERT_BUS_OFF) { print_w_clr_time("CAN went bus-off!", LOG_COLOR_RED, false); - // ESP_ERROR_CHECK(twai_initiate_recovery()); + if (auto_recovery) { + print_w_clr_time("Initiating auto-recovery...", LOG_COLOR_GREEN, false); + twai_initiate_recovery(); + } } if (alerts & TWAI_ALERT_BUS_RECOVERED) { print_w_clr_time("CAN recovered!", LOG_COLOR_BLUE, false); - // ESP_ERROR_CHECK(twai_start()); + if (auto_recovery) { + print_w_clr_time("Starting CAN...", LOG_COLOR_GREEN, false); + twai_start(); + } } } curr_can_state = get_can_state(); diff --git a/main/can.h b/main/can.h index 597090f..82775b7 100644 --- a/main/can.h +++ b/main/can.h @@ -40,6 +40,7 @@ typedef struct { extern SemaphoreHandle_t can_mutex; extern volatile can_status_t curr_can_state; extern bool timestamp_enabled; +extern bool auto_recovery; // functions diff --git a/main/cmd_can.c b/main/cmd_can.c index 4c2a10b..acb50e9 100644 --- a/main/cmd_can.c +++ b/main/cmd_can.c @@ -1,4 +1,5 @@ #include "cmd_can.h" +#include "can.h" #include "esp_log.h" #include "inttypes.h" #include "freertos/projdefs.h" @@ -83,6 +84,10 @@ invalid_args: static int canrecover(int argc, char **argv) { + esp_err_t res = twai_initiate_recovery(); + if (res == ESP_OK) print_w_clr_time("Started CAN recovery.", LOG_COLOR_GREEN, false); + else if (curr_can_state.state == CAN_NOT_INSTALLED) print_w_clr_time("CAN driver is not installed!", LOG_COLOR_RED, false); + else print_w_clr_time("Can't start recovery - not in bus-off state!", LOG_COLOR_RED, false); return 0; } @@ -169,7 +174,7 @@ static void register_canup(void) { canup_args.speed = arg_str1(NULL, NULL, "<25|50|100|125|250|500|800|1000>", "CAN bus speed, in kbits."); canup_args.mode = arg_str0("m", "mode", "", "CAN filters to receive only selected frames."); - canup_args.autorecover = arg_str0("r", "auto-recover", "<1|0>", "Set 1 to enable auto-recovery of CAN bus if case of bus-off event."); + canup_args.autorecover = arg_str0("r", "auto-recovery", "<1|0>", "Set 1 to enable auto-recovery of CAN bus if case of bus-off event."); canup_args.end = arg_end(2); const esp_console_cmd_t cmd = { .command = "canup",