From 64ecf56190ffeeb42a4d06efede3cd29172fbfbc Mon Sep 17 00:00:00 2001 From: Matti Airas Date: Fri, 1 Apr 2022 16:17:02 +0300 Subject: [PATCH] Inspect and output the CAN bus state If the bus goes to bus-off state, a reboot is performed to clear the issue. --- src/main.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 6e1ebe8..3dcfe19 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,6 +13,8 @@ #include #include +#include +#include using namespace reactesp; ReactESP app; @@ -52,6 +54,30 @@ void HandleStreamActisenseMsg(const tN2kMsg &message) { nmea2000->SendMsg(message); } +String can_state; + +void PollCANStatus() { + // CAN controller registers are SJA1000 compatible. + // Bus status value 0 indicates bus-on; value 1 indicates bus-off. + unsigned int bus_status = MODULE_CAN->SR.B.BS; + + switch (bus_status) { + case 0: + can_state = "RUNNING"; + break; + case 1: + can_state = "BUS-OFF"; + // try to automatically recover by rebooting + app.onDelay(2000, []() { + esp_task_wdt_init(1, true); + esp_task_wdt_add(NULL); + while (true) + ; + }); + break; + } +} + void setup() { // setup serial output Serial.begin(115200); @@ -109,6 +135,9 @@ void setup() { actisense_reader.ParseMessages(); }); + // enable CAN status polling + app.onRepeat(100, []() { PollCANStatus(); }); + // initialize the display i2c = new TwoWire(0); i2c->begin(SDA_PIN, SCL_PIN); @@ -129,6 +158,7 @@ void setup() { display->setCursor(0, 0); display->setTextColor(SSD1306_WHITE); display->printf("SH-ESP32 N2K USB GW\n"); + display->printf("CAN: %s\n", can_state.c_str()); display->printf("Uptime: %lu\n", millis() / 1000); display->printf("RX: %d\n", num_n2k_messages); display->printf("TX: %d\n", num_actisense_messages);