From efebbb9a2f7e43eee1d4a68b9d3821a2f3ca0b76 Mon Sep 17 00:00:00 2001 From: Trevor Jones Date: Sun, 9 Aug 2020 06:17:51 -0600 Subject: [PATCH] tmc5160: diag0 support (#3159) Allow for diag0 only hardware to use sensorless homing. Signed-off-by: Trevor Jones --- config/example-extras.cfg | 32 ++++++++++++++++++-------------- docs/Sensorless_Homing.md | 2 ++ klippy/extras/tmc.py | 17 +++++++++++++---- klippy/extras/tmc2130.py | 3 +-- klippy/extras/tmc2209.py | 3 +-- klippy/extras/tmc5160.py | 3 +-- 6 files changed, 36 insertions(+), 24 deletions(-) diff --git a/config/example-extras.cfg b/config/example-extras.cfg index 3e4b728d..ac833938 100644 --- a/config/example-extras.cfg +++ b/config/example-extras.cfg @@ -1436,14 +1436,16 @@ # chip. This may be used to set custom motor parameters. The # defaults for each parameter are next to the parameter name in the # above list. +#diag0_pin: #diag1_pin: -# The micro-controller pin attached to the DIAG1 line of the TMC2130 -# chip. Setting this creates a "tmc2130_stepper_x:virtual_endstop" -# virtual pin which may be used as the stepper's endstop_pin. Doing -# this enables "sensorless homing". (Be sure to also set driver_SGT -# to an appropriate sensitivity value.) The default is to not enable -# sensorless homing. See docs/Sensorless_Homing.md for details on how -# to configure this. +# The micro-controller pin attached to one of the DIAG lines of the +# TMC2130 chip. Only a single diag pin should be specified. +# Setting this creates a "tmc2130_stepper_x:virtual_endstop" virtual +# pin which may be used as the stepper's endstop_pin. Doing this +# enables "sensorless homing". (Be sure to also set driver_SGT to an +# appropriate sensitivity value.) The default is to not enable +# sensorless homing. See docs/Sensorless_Homing.md for details on +# how to configure this. # Configure a TMC2208 (or TMC2224) stepper motor driver via single # wire UART. To use this feature, define a config section with a @@ -1691,14 +1693,16 @@ # chip. This may be used to set custom motor parameters. The # defaults for each parameter are next to the parameter name in the # above list. +#diag0_pin: #diag1_pin: -# The micro-controller pin attached to the DIAG1 line of the TMC5160 -# chip. Setting this creates a "tmc5160_stepper_x:virtual_endstop" -# virtual pin which may be used as the stepper's endstop_pin. Doing -# this enables "sensorless homing". (Be sure to also set driver_SGT -# to an appropriate sensitivity value.) The default is to not enable -# sensorless homing. See docs/Sensorless_Homing.md for details on how -# to configure this. +# The micro-controller pin attached to one of the DIAG lines of the +# TMC5160 chip. Only a single diag pin should be specified. +# Setting this creates a "tmc5160_stepper_x:virtual_endstop" virtual +# pin which may be used as the stepper's endstop_pin. Doing this +# enables "sensorless homing". (Be sure to also set driver_SGT to an +# appropriate sensitivity value.) The default is to not enable +# sensorless homing. See docs/Sensorless_Homing.md for details on +# how to configure this. ###################################################################### diff --git a/docs/Sensorless_Homing.md b/docs/Sensorless_Homing.md index 18195bec..d8f95fdb 100644 --- a/docs/Sensorless_Homing.md +++ b/docs/Sensorless_Homing.md @@ -59,6 +59,8 @@ homing_retract_dist: 0 The name of the virtual end stop pin is derived from the name of the TMC2130 section. The `homing_retract_dist` setting should be set to zero to disable the second homing move as a second pass is not needed, and attempts to do so are error prone. +The TMC2130 and TMC5160 have both a `diag0_pin` and `diag1_pin` in most known hardware the `diag1_pin` is appropriate. In order for klipper to correctly configure the driver for sensorless homing, the correct configuration property name `diag0_pin` or `diag1_pin` must be used. Which is used is determined by which driver pin is connected to the MCU pin. + ATTENTION: This guide only mentions the mandatory parameters and the ones needed to set up sensorless homing. There are many other options to configure on a TMC2130, make sure to take a look at `config/example-extras.cfg` for all the available options. ## Testing of SPI/UART communication diff --git a/klippy/extras/tmc.py b/klippy/extras/tmc.py index d87d5a3f..acc0b607 100644 --- a/klippy/extras/tmc.py +++ b/klippy/extras/tmc.py @@ -182,11 +182,20 @@ class TMCCommandHelper: # Helper class for "sensorless homing" class TMCVirtualPinHelper: - def __init__(self, config, mcu_tmc, diag_pin): + def __init__(self, config, mcu_tmc): self.printer = config.get_printer() self.mcu_tmc = mcu_tmc self.fields = mcu_tmc.get_fields() - self.diag_pin = diag_pin + if self.fields.lookup_register('diag0_stall') is not None: + if config.get('diag0_pin', None) is not None: + self.diag_pin = config.get('diag0_pin') + self.diag_pin_field = 'diag0_stall' + else: + self.diag_pin = config.get('diag1_pin', None) + self.diag_pin_field = 'diag1_stall' + else: + self.diag_pin = config.get('diag_pin', None) + self.diag_pin_field = None self.mcu_endstop = None self.en_pwm = False self.pwmthrs = 0 @@ -228,7 +237,7 @@ class TMCVirtualPinHelper: else: # On earlier drivers, "stealthchop" must be disabled self.fields.set_field("en_pwm_mode", 0) - val = self.fields.set_field("diag1_stall", 1) + val = self.fields.set_field(self.diag_pin_field, 1) self.mcu_tmc.set_register("GCONF", val) self.mcu_tmc.set_register("TCOOLTHRS", 0xfffff) def handle_homing_move_end(self, endstops): @@ -240,7 +249,7 @@ class TMCVirtualPinHelper: val = self.fields.set_field("en_spreadCycle", not self.en_pwm) else: self.fields.set_field("en_pwm_mode", self.en_pwm) - val = self.fields.set_field("diag1_stall", 0) + val = self.fields.set_field(self.diag_pin_field, 0) self.mcu_tmc.set_register("GCONF", val) self.mcu_tmc.set_register("TCOOLTHRS", 0) diff --git a/klippy/extras/tmc2130.py b/klippy/extras/tmc2130.py index 50d2ce84..891de858 100644 --- a/klippy/extras/tmc2130.py +++ b/klippy/extras/tmc2130.py @@ -210,8 +210,7 @@ class TMC2130: self.fields = tmc.FieldHelper(Fields, SignedFields, FieldFormatters) self.mcu_tmc = MCU_TMC_SPI(config, Registers, self.fields) # Allow virtual pins to be created - diag1_pin = config.get('diag1_pin', None) - tmc.TMCVirtualPinHelper(config, self.mcu_tmc, diag1_pin) + 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/tmc2209.py b/klippy/extras/tmc2209.py index 9533488d..666bba63 100644 --- a/klippy/extras/tmc2209.py +++ b/klippy/extras/tmc2209.py @@ -60,8 +60,7 @@ class TMC2209: FieldFormatters) self.mcu_tmc = tmc_uart.MCU_TMC_uart(config, Registers, self.fields, 3) # Allow virtual pins to be created - diag_pin = config.get('diag_pin', None) - tmc.TMCVirtualPinHelper(config, self.mcu_tmc, diag_pin) + 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/tmc5160.py b/klippy/extras/tmc5160.py index e02ae7a1..b4ef6a68 100644 --- a/klippy/extras/tmc5160.py +++ b/klippy/extras/tmc5160.py @@ -309,8 +309,7 @@ class TMC5160: self.fields = tmc.FieldHelper(Fields, SignedFields, FieldFormatters) self.mcu_tmc = tmc2130.MCU_TMC_SPI(config, Registers, self.fields) # Allow virtual pins to be created - diag1_pin = config.get('diag1_pin', None) - tmc.TMCVirtualPinHelper(config, self.mcu_tmc, diag1_pin) + tmc.TMCVirtualPinHelper(config, self.mcu_tmc) # Register commands cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc) cmdhelper.setup_register_dump(ReadRegisters)