-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved canbus diagnostics (#6784) (#568)
* stm32: Add support for reporting canbus state from can.c Signed-off-by: Kevin O'Connor <kevin@koconnor.net> * stm32: Add support for reporting canbus state from fdcan.c Signed-off-by: Kevin O'Connor <kevin@koconnor.net> * rp2040: Add support for reporting canbus state Signed-off-by: Kevin O'Connor <kevin@koconnor.net> * atsamd: Add support for reporting canbus state Signed-off-by: Kevin O'Connor <kevin@koconnor.net> * atsam: Add support for reporting canbus state Signed-off-by: Kevin O'Connor <kevin@koconnor.net> * canbus_stats: Periodically report canbus interface statistics Add support for a new get_canbus_status command to canserial.c . Add new canbus_stats.py module that will periodically query canbus mcus for connection status information. Signed-off-by: Kevin O'Connor <kevin@koconnor.net> * 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 <kevin@koconnor.net> --------- Signed-off-by: Kevin O'Connor <kevin@koconnor.net> Co-authored-by: Kevin O'Connor <kevin@koconnor.net>
- Loading branch information
1 parent
38052fc
commit b107647
Showing
11 changed files
with
426 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Report canbus connection status | ||
# | ||
# Copyright (C) 2025 Kevin O'Connor <kevin@koconnor.net> | ||
# | ||
# This file may be distributed under the terms of the GNU GPLv3 license. | ||
import logging | ||
|
||
|
||
class PrinterCANBusStats: | ||
def __init__(self, config): | ||
self.printer = config.get_printer() | ||
self.reactor = self.printer.get_reactor() | ||
self.name = config.get_name().split()[-1] | ||
self.mcu = None | ||
self.get_canbus_status_cmd = None | ||
self.status = { | ||
"rx_error": None, | ||
"tx_error": None, | ||
"tx_retries": None, | ||
"bus_state": None, | ||
} | ||
self.printer.register_event_handler( | ||
"klippy:connect", self.handle_connect | ||
) | ||
self.printer.register_event_handler( | ||
"klippy:shutdown", self.handle_shutdown | ||
) | ||
|
||
def handle_shutdown(self): | ||
status = self.status.copy() | ||
if status["bus_state"] is not None: | ||
# Clear bus_state on shutdown to note that the values may be stale | ||
status["bus_state"] = "unknown" | ||
self.status = status | ||
|
||
def handle_connect(self): | ||
# Lookup mcu | ||
mcu_name = self.name | ||
if mcu_name != "mcu": | ||
mcu_name = "mcu " + mcu_name | ||
self.mcu = self.printer.lookup_object(mcu_name) | ||
# Lookup status query command | ||
if self.mcu.try_lookup_command("get_canbus_status") is None: | ||
return | ||
self.get_canbus_status_cmd = self.mcu.lookup_query_command( | ||
"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"] | ||
prev_retries = self.status["tx_retries"] | ||
if prev_rx is None: | ||
prev_rx = prev_tx = prev_retries = 0 | ||
params = self.get_canbus_status_cmd.send() | ||
rx = prev_rx + ((params["rx_error"] - prev_rx) & 0xFFFFFFFF) | ||
tx = prev_tx + ((params["tx_error"] - prev_tx) & 0xFFFFFFFF) | ||
retries = prev_retries + ( | ||
(params["tx_retries"] - prev_retries) & 0xFFFFFFFF | ||
) | ||
state = params["canbus_bus_state"] | ||
self.status = { | ||
"rx_error": rx, | ||
"tx_error": tx, | ||
"tx_retries": retries, | ||
"bus_state": state, | ||
} | ||
return self.reactor.monotonic() + 1.0 | ||
|
||
def stats(self, eventtime): | ||
status = self.status | ||
if status["rx_error"] is None: | ||
return (False, "") | ||
return ( | ||
False, | ||
"canstat_%s: bus_state=%s rx_error=%d" | ||
" tx_error=%d tx_retries=%d" | ||
% ( | ||
self.name, | ||
status["bus_state"], | ||
status["rx_error"], | ||
status["tx_error"], | ||
status["tx_retries"], | ||
), | ||
) | ||
|
||
def get_status(self, eventtime): | ||
return self.status | ||
|
||
|
||
def load_config_prefix(config): | ||
return PrinterCANBusStats(config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.