From 7702f0a027686ccba4d45417c79c353c00e8c712 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 12 Jun 2019 23:28:22 -0400 Subject: [PATCH] tmc: Add support for virtual enable pins Add support for enabling the stepper via the communication channel. This improves support for boards with a shared enable line. Signed-off-by: Kevin O'Connor --- config/example-extras.cfg | 25 +++++++++++++++++++----- klippy/extras/tmc.py | 40 +++++++++++++++++++++++++++++++++------ klippy/extras/tmc2130.py | 4 ++-- klippy/extras/tmc2208.py | 2 ++ klippy/extras/tmc2209.py | 2 ++ klippy/extras/tmc2660.py | 8 +++++--- klippy/extras/tmc5160.py | 4 ++-- 7 files changed, 67 insertions(+), 18 deletions(-) diff --git a/config/example-extras.cfg b/config/example-extras.cfg index 256fa886..6664fec6 100644 --- a/config/example-extras.cfg +++ b/config/example-extras.cfg @@ -975,7 +975,10 @@ # Configure a TMC2130 stepper motor driver via SPI bus. To use this # feature, define a config section with a "tmc2130" prefix followed by # the name of the corresponding stepper config section (for example, -# "[tmc2130 stepper_x]"). +# "[tmc2130 stepper_x]"). This also creates a +# "tmc2130_stepper_x:virtual_enable" virtual pin which may be used as +# the stepper's enable_pin (for enabling the driver via an SPI +# message). #[tmc2130 stepper_x] #cs_pin: # The pin corresponding to the TMC2130 chip select line. This pin @@ -1038,7 +1041,10 @@ # Configure a TMC2208 (or TMC2224) stepper motor driver via single # wire UART. To use this feature, define a config section with a # "tmc2208" prefix followed by the name of the corresponding stepper -# config section (for example, "[tmc2208 stepper_x]"). +# config section (for example, "[tmc2208 stepper_x]"). This also +# creates a "tmc2208_stepper_x:virtual_enable" virtual pin which may +# be used as the stepper's enable_pin (for enabling the driver via a +# UART message). #[tmc2208 stepper_x] #uart_pin: # The pin connected to the TMC2208 PDN_UART line. This parameter @@ -1096,7 +1102,10 @@ # Configure a TMC2209 stepper motor driver via single wire UART. To # use this feature, define a config section with a "tmc2209" prefix # followed by the name of the corresponding stepper config section -# (for example, "[tmc2209 stepper_x]"). +# (for example, "[tmc2209 stepper_x]"). This also creates a +# "tmc2209_stepper_x:virtual_enable" virtual pin which may be used as +# the stepper's enable_pin (for enabling the driver via a UART +# message). #[tmc2209 stepper_x] #uart_pin: #tx_pin: @@ -1132,7 +1141,10 @@ # Configure a TMC2660 stepper motor driver via SPI bus. To use this # feature, define a config section with a tmc2660 prefix followed by # the name of the corresponding stepper config section (for example, -# "[tmc2660 stepper_x]"). +# "[tmc2660 stepper_x]"). This also creates a +# "tmc2660_stepper_x:virtual_enable" virtual pin which may be used as +# the stepper's enable_pin (for enabling the driver via an SPI +# message). #[tmc2660 stepper_x] #cs_pin: # The pin corresponding to the TMC2660 chip select line. This pin @@ -1204,7 +1216,10 @@ # Configure a TMC5160 stepper motor driver via SPI bus. To use this # feature, define a config section with a "tmc5160" prefix followed by # the name of the corresponding stepper config section (for example, -# "[tmc5160 stepper_x]"). +# "[tmc5160 stepper_x]"). This also creates a +# "tmc5160_stepper_x:virtual_enable" virtual pin which may be used as +# the stepper's enable_pin (for enabling the driver via an SPI +# message). #[tmc5160 stepper_x] #cs_pin: # The pin corresponding to the TMC5160 chip select line. This pin diff --git a/klippy/extras/tmc.py b/klippy/extras/tmc.py index f0a9ad38..5b606e26 100644 --- a/klippy/extras/tmc.py +++ b/klippy/extras/tmc.py @@ -155,7 +155,7 @@ class TMCCommandHelper: ###################################################################### -# TMC virtual endstops +# TMC virtual pins ###################################################################### # Endstop wrapper that enables "sensorless homing" @@ -187,8 +187,30 @@ class TMCVirtualEndstop: self.mcu_tmc.set_register("TCOOLTHRS", 0) self.mcu_endstop.home_finalize() -class TMCEndstopHelper: - def __init__(self, config, mcu_tmc, diag_pin): +# Digital output wrapper for virtual enable +class TMCVirtualEnable: + def __init__(self, printer, mcu_tmc): + self.reactor = printer.get_reactor() + self.mcu_tmc = mcu_tmc + self.fields = mcu_tmc.get_fields() + self.toff = self.fields.get_field("toff") + self.fields.set_field("toff", 0) + def setup_max_duration(self, max_duration): + pass + def _do_set_digital(self, print_time, value): + toff_val = 0 + if value: + toff_val = self.toff + print_time -= 0.100 # Schedule slightly before deadline + val = self.fields.set_field("toff", toff_val) + reg_name = self.fields.lookup_register("toff") + self.mcu_tmc.set_register(reg_name, val, print_time) + def set_digital(self, print_time, value): + self.reactor.register_callback( + (lambda ev: self._do_set_digital(print_time, value))) + +class TMCVirtualPinHelper: + def __init__(self, config, mcu_tmc, diag_pin=None): self.printer = config.get_printer() self.mcu_tmc = mcu_tmc self.diag_pin = diag_pin @@ -197,12 +219,18 @@ class TMCEndstopHelper: ppins.register_chip("%s_%s" % (name_parts[0], name_parts[-1]), self) def setup_pin(self, pin_type, pin_params): ppins = self.printer.lookup_object('pins') + if pin_params['pin'] not in ('virtual_endstop', 'virtual_enable'): + raise ppins.error("Unknown tmc virtual pin") + if pin_params['invert'] or pin_params['pullup']: + raise ppins.error("Can not pullup/invert tmc virtual pin") + if pin_params['pin'] == 'virtual_enable': + if pin_type != 'digital_out': + raise ppins.error("tmc virtual enable only useful for enable") + return TMCVirtualEnable(self.printer, self.mcu_tmc) if self.diag_pin is None: raise ppins.error("tmc virtual endstop requires diag pin config") - if pin_type != 'endstop' or pin_params['pin'] != 'virtual_endstop': + if pin_type != 'endstop': raise ppins.error("tmc virtual endstop only useful as endstop") - if pin_params['invert'] or pin_params['pullup']: - raise ppins.error("Can not pullup/invert tmc virtual endstop") mcu_endstop = ppins.setup_pin('endstop', self.diag_pin) return TMCVirtualEndstop(self.mcu_tmc, mcu_endstop) diff --git a/klippy/extras/tmc2130.py b/klippy/extras/tmc2130.py index e9f36f1e..aa31f90b 100644 --- a/klippy/extras/tmc2130.py +++ b/klippy/extras/tmc2130.py @@ -208,9 +208,9 @@ class TMC2130: # Setup mcu communication self.fields = tmc.FieldHelper(Fields, SignedFields, FieldFormatters) self.mcu_tmc = MCU_TMC_SPI(config, Registers, self.fields) - # Allow virtual endstop to be created + # Allow virtual pins to be created diag1_pin = config.get('diag1_pin', None) - tmc.TMCEndstopHelper(config, self.mcu_tmc, diag1_pin) + tmc.TMCVirtualPinHelper(config, self.mcu_tmc, diag1_pin) # Register commands cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc) cmdhelper.setup_register_dump(ReadRegisters) diff --git a/klippy/extras/tmc2208.py b/klippy/extras/tmc2208.py index 4bcdabd8..6b004fb1 100644 --- a/klippy/extras/tmc2208.py +++ b/klippy/extras/tmc2208.py @@ -187,6 +187,8 @@ class TMC2208: # Setup mcu communication self.fields = tmc.FieldHelper(Fields, SignedFields, FieldFormatters) self.mcu_tmc = tmc_uart.MCU_TMC_uart(config, Registers, self.fields) + # Allow virtual pins to be created + tmc.TMCVirtualPinHelper(config, self.mcu_tmc) # Register commands cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc) cmdhelper.setup_register_dump(ReadRegisters, self.read_translate) diff --git a/klippy/extras/tmc2209.py b/klippy/extras/tmc2209.py index 22a973ff..02ff3c7e 100644 --- a/klippy/extras/tmc2209.py +++ b/klippy/extras/tmc2209.py @@ -59,6 +59,8 @@ class TMC2209: self.fields = tmc.FieldHelper(Fields, tmc2208.SignedFields, FieldFormatters) self.mcu_tmc = tmc_uart.MCU_TMC_uart(config, Registers, self.fields) + # Allow virtual pins to be created + tmc.TMCVirtualPinHelper(config, self.mcu_tmc) # Register commands cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc) cmdhelper.setup_register_dump(ReadRegisters) diff --git a/klippy/extras/tmc2660.py b/klippy/extras/tmc2660.py index 87e63ec0..e1dd7531 100644 --- a/klippy/extras/tmc2660.py +++ b/klippy/extras/tmc2660.py @@ -23,7 +23,7 @@ Fields["DRVCTRL"] = { } Fields["CHOPCONF"] = { - "TOFF": 0x0f, + "toff": 0x0f, "HSTRT": 0x7 << 4, "HEND": 0x0f << 7, "HDEC": 0x03 << 11, @@ -101,7 +101,7 @@ FieldFormatters = { "DEDGE": (lambda v: "1(Both Edges Active)" if v else "0(Only Rising Edge active)"), "INTPOL": (lambda v: "1(On)" if v else "0(Off)"), - "TOFF": (lambda v: ("%d" % v) if v else "0(Driver Disabled!)"), + "toff": (lambda v: ("%d" % v) if v else "0(Driver Disabled!)"), "CHM": (lambda v: "1(constant toff)" if v else "0(spreadCycle)"), "SFILT": (lambda v: "1(Filtered mode)" if v else "0(Standard mode)"), "VSENSE": (lambda v: "%d(%dmV)" % (v, 165 if v else 305)), @@ -242,6 +242,8 @@ class TMC2660: self.fields = tmc.FieldHelper(Fields, SignedFields, FieldFormatters) self.fields.set_field("SDOFF", 0) # Access DRVCTRL in step/dir mode self.mcu_tmc = MCU_TMC2660_SPI(config, Registers, self.fields) + # Allow virtual pins to be created + tmc.TMCVirtualPinHelper(config, self.mcu_tmc) # Register commands cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc) cmdhelper.setup_register_dump(ReadRegisters) @@ -259,7 +261,7 @@ class TMC2660: set_config_field(config, "CHM", 0) set_config_field(config, "HEND", 3) set_config_field(config, "HSTRT", 3) - set_config_field(config, "TOFF", 4) + set_config_field(config, "toff", 4) if not self.fields.get_field("CHM"): if (self.fields.get_field("HSTRT") + self.fields.get_field("HEND")) > 15: diff --git a/klippy/extras/tmc5160.py b/klippy/extras/tmc5160.py index 637f8d39..48158e2a 100644 --- a/klippy/extras/tmc5160.py +++ b/klippy/extras/tmc5160.py @@ -291,9 +291,9 @@ class TMC5160: # Setup mcu communication self.fields = tmc.FieldHelper(Fields, SignedFields, FieldFormatters) self.mcu_tmc = tmc2130.MCU_TMC_SPI(config, Registers, self.fields) - # Allow virtual endstop to be created + # Allow virtual pins to be created diag1_pin = config.get('diag1_pin', None) - tmc.TMCEndstopHelper(config, self.mcu_tmc, diag1_pin) + tmc.TMCVirtualPinHelper(config, self.mcu_tmc, diag1_pin) # Register commands cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc) cmdhelper.setup_register_dump(ReadRegisters)