diff --git a/config/example-extras.cfg b/config/example-extras.cfg index c9efe854..ffe0caa8 100644 --- a/config/example-extras.cfg +++ b/config/example-extras.cfg @@ -461,10 +461,18 @@ #driver_TOFF: 4 #driver_HEND: 7 #driver_HSTRT: 0 +#driver_SGT: 0 # Set the given register during the configuration of the TMC2130 # 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. +#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. # Homing override. One may use this mechanism to run a series of diff --git a/config/generic-einsy-rambo.cfg b/config/generic-einsy-rambo.cfg index bb2e3387..7c7cb5c5 100644 --- a/config/generic-einsy-rambo.cfg +++ b/config/generic-einsy-rambo.cfg @@ -9,6 +9,7 @@ dir_pin: PL0 enable_pin: !PA7 step_distance: .005 endstop_pin: ^PB6 +#endstop_pin: tmc2130_stepper_x:virtual_endstop position_endstop: 0 position_max: 250 @@ -17,6 +18,7 @@ cs_pin: PG0 microsteps: 16 run_current: .5 sense_resistor: 0.220 +diag1_pin: !PK2 [stepper_y] step_pin: PC1 @@ -24,6 +26,7 @@ dir_pin: !PL1 enable_pin: !PA6 step_distance: .005 endstop_pin: ^PB5 +#endstop_pin: tmc2130_stepper_y:virtual_endstop position_endstop: 0 position_max: 210 @@ -32,6 +35,7 @@ cs_pin: PG2 microsteps: 16 run_current: .5 sense_resistor: 0.220 +diag1_pin: !PK7 [stepper_z] step_pin: PC2 @@ -39,6 +43,7 @@ dir_pin: PL2 enable_pin: !PA5 step_distance: .0025 endstop_pin: ^PB4 +#endstop_pin: tmc2130_stepper_z:virtual_endstop position_endstop: 0.5 position_max: 200 @@ -47,6 +52,7 @@ cs_pin: PK5 microsteps: 16 run_current: .5 sense_resistor: 0.220 +diag1_pin: !PK6 [extruder] step_pin: PC3 @@ -70,6 +76,7 @@ cs_pin: PK4 microsteps: 16 run_current: .5 sense_resistor: 0.220 +diag1_pin: !PK3 [heater_bed] heater_pin: PG5 diff --git a/klippy/extras/tmc2130.py b/klippy/extras/tmc2130.py index 5974438d..6f6bbed5 100644 --- a/klippy/extras/tmc2130.py +++ b/klippy/extras/tmc2130.py @@ -6,12 +6,17 @@ import math TMC_FREQUENCY=13200000. +REG_GCONF=0x00 +GCONF_EN_PWM_MODE=1<<2 +GCONF_DIAG1_STALL=1<<8 +REG_TCOOLTHRS=0x14 +REG_COOLCONF=0x6d -class tmc2130: +class TMC2130: def __init__(self, config): - printer = config.get_printer() + self.printer = config.get_printer() # pin setup - ppins = printer.lookup_object("pins") + ppins = self.printer.lookup_object("pins") cs_pin = config.get('cs_pin') cs_pin_params = ppins.lookup_pin('digital_out', cs_pin) if cs_pin_params['invert']: @@ -39,6 +44,12 @@ class tmc2130: toff = config.getint('driver_TOFF', 4, minval=1, maxval=15) hend = config.getint('driver_HEND', 7, minval=0, maxval=15) hstrt = config.getint('driver_HSTRT', 0, minval=0, maxval=7) + sgt = config.getint('driver_SGT', 0, minval=-64, maxval=63) & 0x7f + # Allow virtual endstop to be created + self.diag1_pin = config.get('diag1_pin', None) + ppins.register_chip("_".join(config.get_name().split()[:2]), self) + self.send_spi_cmd = None + self.mcu.add_config_object(self) # calculate current vsense = False irun = self.current_bits(run_current, sense_resistor, vsense) @@ -48,7 +59,8 @@ class tmc2130: irun = self.current_bits(run_current, sense_resistor, vsense) ihold = self.current_bits(hold_current, sense_resistor, vsense) # configure GCONF - self.add_config_cmd(0x00, (sc_velocity > 0.) << 2) + self.reg_GCONF = (sc_velocity > 0.) << 2 + self.add_config_cmd(REG_GCONF, self.reg_GCONF) # configure CHOPCONF self.add_config_cmd( 0x6c, toff | (hstrt << 4) | (hend << 7) | (blank_time_select << 15) @@ -59,6 +71,8 @@ class tmc2130: self.add_config_cmd(0x11, tpowerdown) # configure TPWMTHRS self.add_config_cmd(0x13, max(0, min(0xfffff, sc_threshold))) + # configure COOLCONF + self.add_config_cmd(REG_COOLCONF, sgt << 16) def add_config_cmd(self, addr, val): self.mcu.add_config_cmd("spi_send oid=%d data=%02x%08x" % ( self.oid, (addr | 0x80) & 0xff, val & 0xffffffff), is_init=True) @@ -78,6 +92,52 @@ class tmc2130: step_dist = stepper_config.getfloat('step_distance') step_dist_256 = step_dist / (1 << self.mres) return int(TMC_FREQUENCY * step_dist_256 / velocity + .5) + def setup_pin(self, pin_params): + if (pin_params['pin'] != 'virtual_endstop' + or pin_params['type'] != 'endstop'): + raise pins.error("tmc2130 virtual endstop only useful as endstop") + if pin_params['invert'] or pin_params['pullup']: + raise pins.error("Can not pullup/invert tmc2130 virtual endstop") + return TMC2130VirtualEndstop(self) + def build_config(self): + cmd_queue = self.mcu.alloc_command_queue() + self.spi_send_cmd = self.mcu.lookup_command( + "spi_send oid=%c data=%*s", cq=cmd_queue) + def set_register(self, addr, val): + data = [(addr | 0x80) & 0xff, (val >> 24) & 0xff, (val >> 16) & 0xff, + (val >> 8) & 0xff, val & 0xff] + self.spi_send_cmd.send([self.oid, data]) + +# Endstop wrapper that enables tmc2130 "sensorless homing" +class TMC2130VirtualEndstop: + def __init__(self, tmc2130): + self.tmc2130 = tmc2130 + if tmc2130.diag1_pin is None: + raise pins.error("tmc2130 virtual endstop requires diag1_pin") + ppins = tmc2130.printer.lookup_object('pins') + self.mcu_endstop = ppins.setup_pin('endstop', tmc2130.diag1_pin) + if self.mcu_endstop.get_mcu() is not tmc2130.mcu: + raise pins.error("tmc2130 virtual endstop must be on same mcu") + # Wrappers + self.get_mcu = self.mcu_endstop.get_mcu + self.add_stepper = self.mcu_endstop.add_stepper + self.get_steppers = self.mcu_endstop.get_steppers + self.home_start = self.mcu_endstop.home_start + self.home_wait = self.mcu_endstop.home_wait + self.query_endstop = self.mcu_endstop.query_endstop + self.query_endstop_wait = self.mcu_endstop.query_endstop_wait + self.TimeoutError = self.mcu_endstop.TimeoutError + def home_prepare(self): + gconf = self.tmc2130.reg_GCONF + gconf &= ~GCONF_EN_PWM_MODE + gconf |= GCONF_DIAG1_STALL + self.tmc2130.set_register(REG_GCONF, gconf) + self.tmc2130.set_register(REG_TCOOLTHRS, 0xfffff) + self.mcu_endstop.home_prepare() + def home_finalize(self): + self.tmc2130.set_register(REG_GCONF, self.tmc2130.reg_GCONF) + self.tmc2130.set_register(REG_TCOOLTHRS, 0) + self.mcu_endstop.home_finalize() def load_config_prefix(config): - return tmc2130(config) + return TMC2130(config)