tmc: Move stepper phase reading code to TMCCommandHelper class

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2021-08-05 23:32:00 -04:00
parent 24586f0c31
commit c2bfeb60aa
6 changed files with 36 additions and 41 deletions

View File

@ -193,6 +193,8 @@ class TMCCommandHelper:
self.toff = None self.toff = None
self.printer.register_event_handler("klippy:connect", self.printer.register_event_handler("klippy:connect",
self._handle_connect) self._handle_connect)
# Set microstep config options
TMCMicrostepHelper(config, mcu_tmc)
# Register commands # Register commands
gcode = self.printer.lookup_object("gcode") gcode = self.printer.lookup_object("gcode")
gcode.register_mux_command("SET_TMC_FIELD", "STEPPER", self.name, gcode.register_mux_command("SET_TMC_FIELD", "STEPPER", self.name,
@ -246,6 +248,17 @@ class TMCCommandHelper:
else: else:
gcmd.respond_info("Run Current: %0.2fA Hold Current: %0.2fA" gcmd.respond_info("Run Current: %0.2fA Hold Current: %0.2fA"
% (prev_run_current, prev_hold_current)) % (prev_run_current, prev_hold_current))
# Stepper phase tracking
def get_microsteps(self):
return 256 >> self.fields.get_field("mres")
def get_phase(self):
field_name = "mscnt"
if self.fields.lookup_register(field_name, None) is None:
# TMC2660 uses MSTEP
field_name = "mstep"
reg = self.mcu_tmc.get_register(self.fields.lookup_register(field_name))
mscnt = self.fields.get_field(field_name, reg)
return 1023 - mscnt, 1024
# Stepper enable/disable tracking # Stepper enable/disable tracking
def _do_enable(self, print_time): def _do_enable(self, print_time):
try: try:
@ -397,31 +410,19 @@ class TMCVirtualPinHelper:
###################################################################### ######################################################################
# Helper to configure and query the microstep settings # Helper to configure and query the microstep settings
class TMCMicrostepHelper: def TMCMicrostepHelper(config, mcu_tmc):
def __init__(self, config, mcu_tmc): fields = mcu_tmc.get_fields()
self.mcu_tmc = mcu_tmc stepper_name = " ".join(config.get_name().split()[1:])
self.fields = mcu_tmc.get_fields() stepper_config = ms_config = config.getsection(stepper_name)
stepper_name = " ".join(config.get_name().split()[1:]) if (stepper_config.get('microsteps', None, note_valid=False) is None
stepper_config = ms_config = config.getsection(stepper_name) and config.get('microsteps', None, note_valid=False) is not None):
if (stepper_config.get('microsteps', None, note_valid=False) is None # Older config format with microsteps in tmc config section
and config.get('microsteps', None, note_valid=False) is not None): ms_config = config
# Older config format with microsteps in tmc config section steps = {'256': 0, '128': 1, '64': 2, '32': 3, '16': 4,
ms_config = config '8': 5, '4': 6, '2': 7, '1': 8}
steps = {'256': 0, '128': 1, '64': 2, '32': 3, '16': 4, mres = ms_config.getchoice('microsteps', steps)
'8': 5, '4': 6, '2': 7, '1': 8} fields.set_field("mres", mres)
mres = ms_config.getchoice('microsteps', steps) fields.set_field("intpol", config.getboolean("interpolate", True))
self.fields.set_field("mres", mres)
self.fields.set_field("intpol", config.getboolean("interpolate", True))
def get_microsteps(self):
return 256 >> self.fields.get_field("mres")
def get_phase(self):
field_name = "mscnt"
if self.fields.lookup_register(field_name, None) is None:
# TMC2660 uses MSTEP
field_name = "mstep"
reg = self.mcu_tmc.get_register(self.fields.lookup_register(field_name))
mscnt = self.fields.get_field(field_name, reg)
return 1023 - mscnt, 1024
# Helper to configure "stealthchop" mode # Helper to configure "stealthchop" mode
def TMCStealthchopHelper(config, mcu_tmc, tmc_freq): def TMCStealthchopHelper(config, mcu_tmc, tmc_freq):

View File

@ -260,10 +260,9 @@ class TMC2130:
current_helper = TMCCurrentHelper(config, self.mcu_tmc) current_helper = TMCCurrentHelper(config, self.mcu_tmc)
cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc, current_helper) cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc, current_helper)
cmdhelper.setup_register_dump(ReadRegisters) cmdhelper.setup_register_dump(ReadRegisters)
self.get_microsteps = cmdhelper.get_microsteps
self.get_phase = cmdhelper.get_phase
# Setup basic register values # Setup basic register values
mh = tmc.TMCMicrostepHelper(config, self.mcu_tmc)
self.get_microsteps = mh.get_microsteps
self.get_phase = mh.get_phase
tmc.TMCStealthchopHelper(config, self.mcu_tmc, TMC_FREQUENCY) tmc.TMCStealthchopHelper(config, self.mcu_tmc, TMC_FREQUENCY)
# Allow other registers to be set from the config # Allow other registers to be set from the config
set_config_field = self.fields.set_config_field set_config_field = self.fields.set_config_field

View File

@ -192,12 +192,11 @@ class TMC2208:
current_helper = tmc2130.TMCCurrentHelper(config, self.mcu_tmc) current_helper = tmc2130.TMCCurrentHelper(config, self.mcu_tmc)
cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc, current_helper) cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc, current_helper)
cmdhelper.setup_register_dump(ReadRegisters, self.read_translate) cmdhelper.setup_register_dump(ReadRegisters, self.read_translate)
self.get_microsteps = cmdhelper.get_microsteps
self.get_phase = cmdhelper.get_phase
# Setup basic register values # Setup basic register values
self.fields.set_field("mstep_reg_select", True) self.fields.set_field("mstep_reg_select", True)
self.fields.set_field("multistep_filt", True) self.fields.set_field("multistep_filt", True)
mh = tmc.TMCMicrostepHelper(config, self.mcu_tmc)
self.get_microsteps = mh.get_microsteps
self.get_phase = mh.get_phase
tmc.TMCStealthchopHelper(config, self.mcu_tmc, TMC_FREQUENCY) tmc.TMCStealthchopHelper(config, self.mcu_tmc, TMC_FREQUENCY)
# Allow other registers to be set from the config # Allow other registers to be set from the config
set_config_field = self.fields.set_config_field set_config_field = self.fields.set_config_field

View File

@ -68,13 +68,12 @@ class TMC2209:
current_helper = tmc2130.TMCCurrentHelper(config, self.mcu_tmc) current_helper = tmc2130.TMCCurrentHelper(config, self.mcu_tmc)
cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc, current_helper) cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc, current_helper)
cmdhelper.setup_register_dump(ReadRegisters) cmdhelper.setup_register_dump(ReadRegisters)
self.get_microsteps = cmdhelper.get_microsteps
self.get_phase = cmdhelper.get_phase
# Setup basic register values # Setup basic register values
self.fields.set_field("pdn_disable", True) self.fields.set_field("pdn_disable", True)
self.fields.set_field("mstep_reg_select", True) self.fields.set_field("mstep_reg_select", True)
self.fields.set_field("multistep_filt", True) self.fields.set_field("multistep_filt", True)
mh = tmc.TMCMicrostepHelper(config, self.mcu_tmc)
self.get_microsteps = mh.get_microsteps
self.get_phase = mh.get_phase
tmc.TMCStealthchopHelper(config, self.mcu_tmc, TMC_FREQUENCY) tmc.TMCStealthchopHelper(config, self.mcu_tmc, TMC_FREQUENCY)
# Allow other registers to be set from the config # Allow other registers to be set from the config
set_config_field = self.fields.set_config_field set_config_field = self.fields.set_config_field

View File

@ -229,11 +229,9 @@ class TMC2660:
current_helper = TMC2660CurrentHelper(config, self.mcu_tmc) current_helper = TMC2660CurrentHelper(config, self.mcu_tmc)
cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc, current_helper) cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc, current_helper)
cmdhelper.setup_register_dump(ReadRegisters) cmdhelper.setup_register_dump(ReadRegisters)
self.get_microsteps = cmdhelper.get_microsteps
self.get_phase = cmdhelper.get_phase
# DRVCTRL
mh = tmc.TMCMicrostepHelper(config, self.mcu_tmc)
self.get_microsteps = mh.get_microsteps
self.get_phase = mh.get_phase
# CHOPCONF # CHOPCONF
set_config_field = self.fields.set_config_field set_config_field = self.fields.set_config_field
set_config_field(config, "tbl", 2) set_config_field(config, "tbl", 2)

View File

@ -294,10 +294,9 @@ class TMC5160:
current_helper = TMC5160CurrentHelper(config, self.mcu_tmc) current_helper = TMC5160CurrentHelper(config, self.mcu_tmc)
cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc, current_helper) cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc, current_helper)
cmdhelper.setup_register_dump(ReadRegisters) cmdhelper.setup_register_dump(ReadRegisters)
self.get_microsteps = cmdhelper.get_microsteps
self.get_phase = cmdhelper.get_phase
# Setup basic register values # Setup basic register values
mh = tmc.TMCMicrostepHelper(config, self.mcu_tmc)
self.get_microsteps = mh.get_microsteps
self.get_phase = mh.get_phase
tmc.TMCStealthchopHelper(config, self.mcu_tmc, TMC_FREQUENCY) tmc.TMCStealthchopHelper(config, self.mcu_tmc, TMC_FREQUENCY)
# CHOPCONF # CHOPCONF
set_config_field = self.fields.set_config_field set_config_field = self.fields.set_config_field