From 041692828c66cd291435020f240b243c862177bf Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sun, 7 Feb 2021 17:56:39 -0500 Subject: [PATCH] mcu: Add support for connecting to devices on a CAN bus Signed-off-by: Kevin O'Connor --- docs/Config_Reference.md | 9 ++++++++- klippy/extras/canbus_ids.py | 24 ++++++++++++++++++++++++ klippy/mcu.py | 25 +++++++++++++++++++------ 3 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 klippy/extras/canbus_ids.py diff --git a/docs/Config_Reference.md b/docs/Config_Reference.md index 7259d998..c1c09cb7 100644 --- a/docs/Config_Reference.md +++ b/docs/Config_Reference.md @@ -39,9 +39,16 @@ Configuration of the primary micro-controller. serial: # The serial port to connect to the MCU. If unsure (or if it # changes) see the "Where's my serial port?" section of the FAQ. -# This parameter must be provided. +# This parameter must be provided when using a serial port. #baud: 250000 # The baud rate to use. The default is 250000. +#canbus_uuid: +# If using a device connected to a CAN bus then this sets the unique +# chip identifier to connect to. This value must be provided when using +# CAN bus for communication. +#canbus_interface: +# If using a device connected to a CAN bus then this sets the CAN +# network interface to use. The default is 'can0'. #pin_map: # This option may be used to enable Arduino pin name aliases. The # default is to not enable the aliases. diff --git a/klippy/extras/canbus_ids.py b/klippy/extras/canbus_ids.py new file mode 100644 index 00000000..5e70f8b9 --- /dev/null +++ b/klippy/extras/canbus_ids.py @@ -0,0 +1,24 @@ +# Support for tracking canbus node ids +# +# Copyright (C) 2021 Kevin O'Connor +# +# This file may be distributed under the terms of the GNU GPLv3 license. + +class PrinterCANBus: + def __init__(self, config): + self.printer = config.get_printer() + self.ids = {} + def add_uuid(self, config, canbus_uuid, canbus_iface): + if canbus_uuid in self.ids: + raise config.error("Duplicate canbus_uuid") + new_id = len(self.ids) + self.ids[canbus_uuid] = new_id + return new_id + def get_nodeid(self, canbus_uuid): + if canbus_uuid not in self.ids: + raise self.printer.config_error("Unknown canbus_uuid %s" + % (canbus_uuid,)) + return self.ids[canbus_uuid] + +def load_config(config): + return PrinterCANBus(config) diff --git a/klippy/mcu.py b/klippy/mcu.py index 93e71572..ec2cce7a 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -415,12 +415,20 @@ class MCU: if self._name.startswith('mcu '): self._name = self._name[4:] # Serial port - self._serialport = config.get('serial') - self._baud = 0 - if not (self._serialport.startswith("/dev/rpmsg_") - or self._serialport.startswith("/tmp/klipper_host_")): - self._baud = config.getint('baud', 250000, minval=2400) self._serial = serialhdl.SerialReader(self._reactor) + self._baud = 0 + self._canbus_iface = None + canbus_uuid = config.get('canbus_uuid', None) + if canbus_uuid is not None: + self._serialport = canbus_uuid + self._canbus_iface = config.get('canbus_interface', 'can0') + cbid = self._printer.load_object(config, 'canbus_ids') + cbid.add_uuid(config, canbus_uuid, self._canbus_iface) + else: + self._serialport = config.get('serial') + if not (self._serialport.startswith("/dev/rpmsg_") + or self._serialport.startswith("/tmp/klipper_host_")): + self._baud = config.getint('baud', 250000, minval=2400) # Restarts restart_methods = [None, 'arduino', 'cheetah', 'command', 'rpi_usb'] self._restart_method = 'command' @@ -617,7 +625,12 @@ class MCU: # Try toggling usb power self._check_restart("enable power") try: - if self._baud: + if self._canbus_iface is not None: + cbid = self._printer.lookup_object('canbus_ids') + nodeid = cbid.get_nodeid(self._serialport) + self._serial.connect_canbus(self._serialport, nodeid, + self._canbus_iface) + elif self._baud: # Cheetah boards require RTS to be deasserted # else a reset will trigger the built-in bootloader. rts = (resmeth != "cheetah")