From a482ddf582862103fdf4ef465cbad81ea46af90b Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sat, 17 Aug 2019 21:05:30 -0400 Subject: [PATCH] tmc2209: Add support for sensorless homing Signed-off-by: Kevin O'Connor --- config/example-extras.cfg | 7 +++++++ klippy/extras/tmc.py | 30 ++++++++++++++++++++++++------ klippy/extras/tmc2209.py | 3 ++- test/klippy/tmc.cfg | 2 ++ 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/config/example-extras.cfg b/config/example-extras.cfg index 8567ead7..184f690a 100644 --- a/config/example-extras.cfg +++ b/config/example-extras.cfg @@ -1130,6 +1130,13 @@ # 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. +#diag_pin: +# The micro-controller pin attached to the DIAG line of the TMC2209 +# chip. Setting this creates a "tmc2209_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_SGTHRS to an appropriate sensitivity value.) The default is +# to not enable sensorless homing. # Configure a TMC2660 stepper motor driver via SPI bus. To use this diff --git a/klippy/extras/tmc.py b/klippy/extras/tmc.py index 16b5c708..8d0bf9f9 100644 --- a/klippy/extras/tmc.py +++ b/klippy/extras/tmc.py @@ -32,7 +32,7 @@ class FieldHelper: if reg_name is None: reg_name = self.field_to_register[field_name] if reg_value is None: - reg_value = self.registers[reg_name] + reg_value = self.registers.get(reg_name, 0) mask = self.all_fields[reg_name][field_name] field_value = (reg_value & mask) >> ffs(mask) if field_name in self.signed_fields and ((reg_value & mask)<<1) > mask: @@ -163,7 +163,13 @@ class TMCVirtualEndstop: self.mcu_tmc = mcu_tmc self.fields = mcu_tmc.get_fields() self.mcu_endstop = mcu_endstop - self.en_pwm = self.fields.get_field("en_pwm_mode") + reg = self.fields.lookup_register("en_pwm_mode", None) + if reg is None: + self.en_pwm = not self.fields.get_field("en_spreadCycle") + self.pwmthrs = self.fields.get_field("TPWMTHRS") + else: + self.en_pwm = self.fields.get_field("en_pwm_mode") + self.pwmthrs = 0 # Wrappers self.get_mcu = self.mcu_endstop.get_mcu self.add_stepper = self.mcu_endstop.add_stepper @@ -173,14 +179,26 @@ class TMCVirtualEndstop: self.query_endstop = self.mcu_endstop.query_endstop self.TimeoutError = self.mcu_endstop.TimeoutError def home_prepare(self): - self.fields.set_field("en_pwm_mode", 0) - val = self.fields.set_field("diag1_stall", 1) + reg = self.fields.lookup_register("en_pwm_mode", None) + if reg is None: + # On "stallguard4" drivers, "stealthchop" must be enabled + self.mcu_tmc.set_register("TPWMTHRS", 0) + val = self.fields.set_field("en_spreadCycle", 0) + else: + # On earlier drivers, "stealthchop" must be disabled + self.fields.set_field("en_pwm_mode", 0) + val = self.fields.set_field("diag1_stall", 1) self.mcu_tmc.set_register("GCONF", val) self.mcu_tmc.set_register("TCOOLTHRS", 0xfffff) self.mcu_endstop.home_prepare() def home_finalize(self): - self.fields.set_field("en_pwm_mode", self.en_pwm) - val = self.fields.set_field("diag1_stall", 0) + reg = self.fields.lookup_register("en_pwm_mode", None) + if reg is None: + self.mcu_tmc.set_register("TPWMTHRS", self.pwmthrs) + 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) self.mcu_tmc.set_register("GCONF", val) self.mcu_tmc.set_register("TCOOLTHRS", 0) self.mcu_endstop.home_finalize() diff --git a/klippy/extras/tmc2209.py b/klippy/extras/tmc2209.py index 5f487122..5b97d906 100644 --- a/klippy/extras/tmc2209.py +++ b/klippy/extras/tmc2209.py @@ -60,7 +60,8 @@ class TMC2209: FieldFormatters) self.mcu_tmc = tmc_uart.MCU_TMC_uart(config, Registers, self.fields, 3) # Allow virtual pins to be created - tmc.TMCVirtualPinHelper(config, self.mcu_tmc) + diag_pin = config.get('diag_pin', None) + tmc.TMCVirtualPinHelper(config, self.mcu_tmc, diag_pin) # Register commands cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc) cmdhelper.setup_register_dump(ReadRegisters) diff --git a/test/klippy/tmc.cfg b/test/klippy/tmc.cfg index 443f1435..603538a3 100644 --- a/test/klippy/tmc.cfg +++ b/test/klippy/tmc.cfg @@ -48,9 +48,11 @@ diag1_pin: !PK7 step_pin: PA4 dir_pin: PA6 step_distance: .005 +endstop_pin: tmc2209_stepper_y1:virtual_endstop [tmc2209 stepper_y1] uart_pin: PA5 +diag_pin: PH6 microsteps: 16 run_current: .5 sense_resistor: 0.075