From 2c90c97ccd5ceb9b4071f95866160344f013a86f Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Tue, 14 Jan 2025 22:38:08 -0500 Subject: usb_canbus: Detect canbus stalls when in usb to canbus bridge mode If the low-level canbus stops working then it could become impossible to send messages to and from the canbus bridge node itself. This can make it difficult to diagnose canbus problems. Change the canbus bridge code to detect if message transmits become stalled for 50+ milliseconds and go into a "discarding" state. In this discarding state, messages destined for the canbus will be discarded until the canbus becomes active again. In this discarding state it will therefore be possible to transmit messages to and from the canbus bridge node. Signed-off-by: Kevin O'Connor --- klippy/extras/canbus_stats.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'klippy/extras') diff --git a/klippy/extras/canbus_stats.py b/klippy/extras/canbus_stats.py index b9ee3102..0ddafaf3 100644 --- a/klippy/extras/canbus_stats.py +++ b/klippy/extras/canbus_stats.py @@ -3,6 +3,7 @@ # Copyright (C) 2025 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. +import logging class PrinterCANBusStats: def __init__(self, config): @@ -36,8 +37,19 @@ class PrinterCANBusStats: "get_canbus_status", "canbus_status rx_error=%u tx_error=%u tx_retries=%u" " canbus_bus_state=%u") + # Register usb_canbus_state message handling (for usb to canbus bridge) + self.mcu.register_response(self.handle_usb_canbus_state, + "usb_canbus_state") # Register periodic query timer self.reactor.register_timer(self.query_event, self.reactor.NOW) + def handle_usb_canbus_state(self, params): + discard = params['discard'] + if discard: + logging.warning("USB CANBUS bridge '%s' is discarding!" + % (self.name,)) + else: + logging.warning("USB CANBUS bridge '%s' is no longer discarding." + % (self.name,)) def query_event(self, eventtime): prev_rx = self.status['rx_error'] prev_tx = self.status['tx_error'] -- cgit v1.2.3-70-g09d2