From c039b33f2a0418469b4502106fa223fd3f54c02a Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Fri, 1 Mar 2019 00:23:19 -0500 Subject: [PATCH] tmc2130: Add support for INIT_TMC command Signed-off-by: Frank Kang Signed-off-by: Kevin O'Connor --- docs/G-Codes.md | 11 +++++++---- klippy/extras/tmc2130.py | 19 +++++++++++++++---- klippy/extras/tmc2208.py | 15 ++++++++++++--- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/docs/G-Codes.md b/docs/G-Codes.md index 8cbd34ba..b9db71c7 100644 --- a/docs/G-Codes.md +++ b/docs/G-Codes.md @@ -287,12 +287,15 @@ section is enabled: carriage. It is typically invoked from the activate_gcode and deactivate_gcode fields in a multiple extruder configuration. -## TMC2130 +## TMC2130 and TMC2208 -The following command is available when the "tmc2130" config section -is enabled: -- `DUMP_TMC STEPPER=`: This command will read the TMC2130 driver +The following command is available when the "tmc2130" or "tmc2208" +config section is enabled: +- `DUMP_TMC STEPPER=`: This command will read the TMC driver registers and report their values. +- `INIT_TMC STEPPER=`: This command will intitialize the TMC + registers. Needed to re-enable the driver if power to the chip is + turned off then back on. ## Endstop adjustments by stepper phase diff --git a/klippy/extras/tmc2130.py b/klippy/extras/tmc2130.py index 02696bda..f8cbc158 100644 --- a/klippy/extras/tmc2130.py +++ b/klippy/extras/tmc2130.py @@ -211,11 +211,14 @@ class TMC2130: self.diag1_pin = config.get('diag1_pin', None) ppins = self.printer.lookup_object("pins") ppins.register_chip("tmc2130_" + self.name, self) - # Add DUMP_TMC command + # Add DUMP_TMC, INIT_TMC command gcode = self.printer.lookup_object("gcode") gcode.register_mux_command( "DUMP_TMC", "STEPPER", self.name, self.cmd_DUMP_TMC, desc=self.cmd_DUMP_TMC_help) + gcode.register_mux_command( + "INIT_TMC", "STEPPER", self.name, + self.cmd_INIT_TMC, desc=self.cmd_INIT_TMC_help) # Setup basic register values self.regs = collections.OrderedDict() self.fields = FieldHelper(Fields, FieldFormatters, self.regs) @@ -242,9 +245,11 @@ class TMC2130: set_config_field(config, "pwm_autoscale", True) sgt = config.getint('driver_SGT', 0, minval=-64, maxval=63) & 0x7f self.fields.set_field("sgt", sgt) + self._init_registers() + def _init_registers(self, min_clock = 0): # Send registers for reg_name, val in self.regs.items(): - self.set_register(reg_name, val) + self.set_register(reg_name, val, min_clock) def setup_pin(self, pin_type, pin_params): if pin_type != 'endstop' or pin_params['pin'] != 'virtual_endstop': raise pins.error("tmc2130 virtual endstop only useful as endstop") @@ -257,11 +262,11 @@ class TMC2130: params = self.spi.spi_transfer([reg, 0x00, 0x00, 0x00, 0x00]) pr = bytearray(params['response']) return (pr[1] << 24) | (pr[2] << 16) | (pr[3] << 8) | pr[4] - def set_register(self, reg_name, val): + def set_register(self, reg_name, val, min_clock = 0): reg = Registers[reg_name] data = [(reg | 0x80) & 0xff, (val >> 24) & 0xff, (val >> 16) & 0xff, (val >> 8) & 0xff, val & 0xff] - self.spi.spi_send(data) + self.spi.spi_send(data, min_clock) def get_microsteps(self): return 256 >> self.fields.get_field("MRES") def get_phase(self): @@ -284,6 +289,12 @@ class TMC2130: msg = self.fields.pretty_format(reg_name, val) logging.info(msg) gcode.respond_info(msg) + cmd_INIT_TMC_help = "Initialize TMC stepper driver registers" + def cmd_INIT_TMC(self, params): + logging.info("INIT_TMC 2130 %s", self.name) + print_time = self.printer.lookup_object('toolhead').get_last_move_time(); + min_clock = self.spi.get_mcu().print_time_to_clock(print_time) + self._init_registers(min_clock) # Endstop wrapper that enables tmc2130 "sensorless homing" class TMC2130VirtualEndstop: diff --git a/klippy/extras/tmc2208.py b/klippy/extras/tmc2208.py index 85806924..f312c205 100644 --- a/klippy/extras/tmc2208.py +++ b/klippy/extras/tmc2208.py @@ -248,7 +248,7 @@ class TMC2208: self.printer = config.get_printer() self.name = config.get_name().split()[-1] self.printer.register_event_handler("klippy:connect", - self.handle_connect) + self._init_registers) # pin setup ppins = self.printer.lookup_object("pins") rx_pin_params = ppins.lookup_pin( @@ -267,11 +267,14 @@ class TMC2208: self.oid = self.mcu.create_oid() self.tmcuart_send_cmd = None self.mcu.register_config_callback(self.build_config) - # Add DUMP_TMC command + # Add DUMP_TMC, INIT_TMC command gcode = self.printer.lookup_object("gcode") gcode.register_mux_command( "DUMP_TMC", "STEPPER", self.name, self.cmd_DUMP_TMC, desc=self.cmd_DUMP_TMC_help) + gcode.register_mux_command( + "INIT_TMC", "STEPPER", self.name, + self.cmd_INIT_TMC, desc=self.cmd_INIT_TMC_help) # Setup basic register values self.ifcnt = None self.regs = collections.OrderedDict() @@ -311,7 +314,8 @@ class TMC2208: cmd_queue = self.mcu.alloc_command_queue() self.tmcuart_send_cmd = self.mcu.lookup_command( "tmcuart_send oid=%c write=%*s read=%c", cq=cmd_queue) - def handle_connect(self): + def _init_registers(self): + # Send registers for reg_name, val in self.regs.items(): self.set_register(reg_name, val) def get_register(self, reg_name): @@ -372,6 +376,11 @@ class TMC2208: msg = self.fields.pretty_format(reg_name, val) logging.info(msg) gcode.respond_info(msg) + cmd_INIT_TMC_help = "Initialize TMC stepper driver registers" + def cmd_INIT_TMC(self, params): + logging.info("INIT_TMC 2208 %s", self.name) + self.printer.lookup_object('toolhead').wait_moves() + self._init_registers() def load_config_prefix(config): return TMC2208(config)