diff --git a/docs/G-Codes.md b/docs/G-Codes.md index 6a004cf2..a3d41cd3 100644 --- a/docs/G-Codes.md +++ b/docs/G-Codes.md @@ -290,15 +290,26 @@ section is enabled: carriage. It is typically invoked from the activate_gcode and deactivate_gcode fields in a multiple extruder configuration. -## TMC2130 and TMC2208 +## TMC2130, TMC2660 and TMC2208 -The following command is available when the "tmc2130" or "tmc2208" -config section is enabled: +The following command is available when the "tmc2130", "tmc2660" +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. +The following commands are additionally available when the "tmc2660" +config section is enabled: +- `SET_TMC_CURRENT STEPPER= CURRENT=`: This will adjust + the run_current of the TMC driver. +- `SET_TMC_FIELD STEPPER= FIELD= VALUE=`: This will + alter the value of the specified register field of the TMC driver. + This command is intended for low-level diagnostics and debugging only because + changing the fields during run-time can lead to undesired and potentially + dangerous behavior of your printer. Permanent changes should be made using + the printer configuration file instead. No sanity checks are performed for the + given values. ## Endstop adjustments by stepper phase diff --git a/klippy/extras/tmc2660.py b/klippy/extras/tmc2660.py index 2aea8ac2..2135f356 100644 --- a/klippy/extras/tmc2660.py +++ b/klippy/extras/tmc2660.py @@ -3,7 +3,7 @@ # Copyright (C) 2018-2019 Florian Heilmann # # This file may be distributed under the terms of the GNU GPLv3 license. -import math, collections +import math, collections, logging import bus, tmc2130 def current_to_reg(current, sense_resistor, vsense_on): @@ -134,6 +134,12 @@ class TMC2660: gcode.register_mux_command( "DUMP_TMC", "STEPPER", self.name, self.cmd_DUMP_TMC, desc=self.cmd_DUMP_TMC_help) + gcode.register_mux_command( + "SET_TMC_FIELD", "STEPPER", self.name, + self.cmd_SET_TMC_FIELD, desc=self.cmd_SET_TMC_FIELD_help) + gcode.register_mux_command( + "INIT_TMC", "STEPPER", self.name, + self.cmd_INIT_TMC, desc=self.cmd_INIT_TMC_help) # Setup driver registers self.regs = collections.OrderedDict() self.fields = tmc2130.FieldHelper(Fields, FieldFormatters, self.regs) @@ -178,7 +184,7 @@ class TMC2660: # SGSCONF set_config_field(config, "SFILT", 1) set_config_field(config, "SGT", 0) - self.current = config.getfloat('run_current', minval=0.1, + self.current = config.getfloat('run_current', minval=0.1, maxval=2.4) self.driver_cs = current_to_reg(self.current, self.sense_resistor, self.fields.get_field("VSENSE")) @@ -259,5 +265,28 @@ class TMC2660: msg = self.fields.pretty_format(return_format, self.get_response()) gcode.respond_info(msg) + cmd_INIT_TMC_help = "Initialize TMC stepper driver registers" + def cmd_INIT_TMC(self, params): + logging.info("INIT_TMC 2660 %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) + + cmd_SET_TMC_FIELD_help = "Set a register field of a TMC2660 driver" + def cmd_SET_TMC_FIELD(self, params): + gcode = self.printer.lookup_object('gcode') + if ('FIELD' not in params or + 'VALUE' not in params): + raise gcode.error("Invalid command format") + field = gcode.get_str('FIELD', params) + if field == "CS": + raise gcode.error("Use SET_TMC_CURRENT to set CS") + reg = self.fields.field_to_register[field] + value = gcode.get_int('VALUE', params) + self.fields.set_field(field, value) + print_time = self.printer.lookup_object('toolhead').get_last_move_time() + clock = self.spi.get_mcu().print_time_to_clock(print_time) + self.set_register(reg, self.regs[reg], min_clock=clock) + def load_config_prefix(config): return TMC2660(config)