mcu: Add support for connecting to devices on a CAN bus

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2021-02-07 17:56:39 -05:00
parent babde17c4c
commit 041692828c
3 changed files with 51 additions and 7 deletions

View File

@ -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.

View File

@ -0,0 +1,24 @@
# Support for tracking canbus node ids
#
# Copyright (C) 2021 Kevin O'Connor <kevin@koconnor.net>
#
# 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)

View File

@ -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")