2019-06-10 21:29:35 +03:00
|
|
|
# Common helper code for TMC stepper drivers
|
|
|
|
#
|
2020-01-06 04:19:43 +03:00
|
|
|
# Copyright (C) 2018-2020 Kevin O'Connor <kevin@koconnor.net>
|
2019-06-10 21:29:35 +03:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
import logging, collections
|
2020-01-06 04:19:43 +03:00
|
|
|
import stepper
|
2019-06-10 21:29:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Field helpers
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
# Return the position of the first bit set in a mask
|
|
|
|
def ffs(mask):
|
|
|
|
return (mask & -mask).bit_length() - 1
|
|
|
|
|
|
|
|
class FieldHelper:
|
|
|
|
def __init__(self, all_fields, signed_fields=[], field_formatters={},
|
|
|
|
registers=None):
|
|
|
|
self.all_fields = all_fields
|
|
|
|
self.signed_fields = {sf: 1 for sf in signed_fields}
|
|
|
|
self.field_formatters = field_formatters
|
|
|
|
self.registers = registers
|
|
|
|
if self.registers is None:
|
|
|
|
self.registers = collections.OrderedDict()
|
|
|
|
self.field_to_register = { f: r for r, fields in self.all_fields.items()
|
|
|
|
for f in fields }
|
|
|
|
def lookup_register(self, field_name, default=None):
|
|
|
|
return self.field_to_register.get(field_name, default)
|
|
|
|
def get_field(self, field_name, reg_value=None, reg_name=None):
|
|
|
|
# Returns value of the register field
|
|
|
|
if reg_name is None:
|
|
|
|
reg_name = self.field_to_register[field_name]
|
|
|
|
if reg_value is None:
|
2019-08-18 04:05:30 +03:00
|
|
|
reg_value = self.registers.get(reg_name, 0)
|
2019-06-10 21:29:35 +03:00
|
|
|
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:
|
|
|
|
field_value -= (1 << field_value.bit_length())
|
|
|
|
return field_value
|
|
|
|
def set_field(self, field_name, field_value, reg_value=None, reg_name=None):
|
|
|
|
# Returns register value with field bits filled with supplied value
|
|
|
|
if reg_name is None:
|
|
|
|
reg_name = self.field_to_register[field_name]
|
|
|
|
if reg_value is None:
|
|
|
|
reg_value = self.registers.get(reg_name, 0)
|
|
|
|
mask = self.all_fields[reg_name][field_name]
|
|
|
|
new_value = (reg_value & ~mask) | ((field_value << ffs(mask)) & mask)
|
|
|
|
self.registers[reg_name] = new_value
|
|
|
|
return new_value
|
2019-06-25 19:01:47 +03:00
|
|
|
def set_config_field(self, config, field_name, default):
|
2019-06-10 21:29:35 +03:00
|
|
|
# Allow a field to be set from the config file
|
2019-06-25 19:01:47 +03:00
|
|
|
config_name = "driver_" + field_name.upper()
|
2019-06-10 21:29:35 +03:00
|
|
|
reg_name = self.field_to_register[field_name]
|
|
|
|
mask = self.all_fields[reg_name][field_name]
|
|
|
|
maxval = mask >> ffs(mask)
|
|
|
|
if maxval == 1:
|
|
|
|
val = config.getboolean(config_name, default)
|
|
|
|
elif field_name in self.signed_fields:
|
|
|
|
val = config.getint(config_name, default,
|
|
|
|
minval=-(maxval//2 + 1), maxval=maxval//2)
|
|
|
|
else:
|
|
|
|
val = config.getint(config_name, default, minval=0, maxval=maxval)
|
|
|
|
return self.set_field(field_name, val)
|
|
|
|
def pretty_format(self, reg_name, reg_value):
|
|
|
|
# Provide a string description of a register
|
|
|
|
reg_fields = self.all_fields.get(reg_name, {})
|
|
|
|
reg_fields = sorted([(mask, name) for name, mask in reg_fields.items()])
|
|
|
|
fields = []
|
|
|
|
for mask, field_name in reg_fields:
|
|
|
|
field_value = self.get_field(field_name, reg_value, reg_name)
|
|
|
|
sval = self.field_formatters.get(field_name, str)(field_value)
|
|
|
|
if sval and sval != "0":
|
|
|
|
fields.append(" %s=%s" % (field_name, sval))
|
|
|
|
return "%-11s %08x%s" % (reg_name + ":", reg_value, "".join(fields))
|
2021-08-28 02:24:14 +03:00
|
|
|
def get_reg_fields(self, reg_name, reg_value):
|
|
|
|
# Provide fields found in a register
|
|
|
|
reg_fields = self.all_fields.get(reg_name, {})
|
|
|
|
return {field_name: self.get_field(field_name, reg_value, reg_name)
|
|
|
|
for field_name, mask in reg_fields.items()}
|
2019-06-10 21:29:35 +03:00
|
|
|
|
|
|
|
|
2021-02-21 04:18:40 +03:00
|
|
|
######################################################################
|
|
|
|
# Periodic error checking
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
class TMCErrorCheck:
|
2021-03-15 07:19:57 +03:00
|
|
|
def __init__(self, config, mcu_tmc):
|
2021-02-21 04:18:40 +03:00
|
|
|
self.printer = config.get_printer()
|
2021-03-15 07:19:57 +03:00
|
|
|
name_parts = config.get_name().split()
|
|
|
|
self.stepper_name = ' '.join(name_parts[1:])
|
2021-02-21 04:18:40 +03:00
|
|
|
self.mcu_tmc = mcu_tmc
|
|
|
|
self.fields = mcu_tmc.get_fields()
|
|
|
|
self.check_timer = None
|
2021-08-28 02:24:14 +03:00
|
|
|
self.last_drv_status = self.last_status = None
|
2021-02-21 04:18:40 +03:00
|
|
|
# Setup for GSTAT query
|
|
|
|
reg_name = self.fields.lookup_register("drv_err")
|
|
|
|
if reg_name is not None:
|
2021-03-15 18:16:15 +03:00
|
|
|
self.gstat_reg_info = [0, reg_name, 0xffffffff, 0xffffffff, 0]
|
2021-02-21 04:18:40 +03:00
|
|
|
else:
|
|
|
|
self.gstat_reg_info = None
|
2021-03-15 18:16:15 +03:00
|
|
|
self.clear_gstat = True
|
2021-02-21 04:18:40 +03:00
|
|
|
# Setup for DRV_STATUS query
|
2021-08-05 21:25:53 +03:00
|
|
|
self.irun_field = "irun"
|
2021-03-15 18:16:15 +03:00
|
|
|
reg_name = "DRV_STATUS"
|
|
|
|
mask = err_mask = cs_actual_mask = 0
|
|
|
|
if name_parts[0] == 'tmc2130':
|
|
|
|
# TMC2130 driver quirks
|
|
|
|
self.clear_gstat = False
|
2021-08-05 21:25:53 +03:00
|
|
|
cs_actual_mask = self.fields.all_fields[reg_name]["cs_actual"]
|
2021-03-15 18:16:15 +03:00
|
|
|
elif name_parts[0] == 'tmc2660':
|
|
|
|
# TMC2660 driver quirks
|
2021-08-05 21:25:53 +03:00
|
|
|
self.irun_field = "cs"
|
2021-03-15 18:16:15 +03:00
|
|
|
reg_name = "READRSP@RDSEL2"
|
2021-08-05 21:25:53 +03:00
|
|
|
cs_actual_mask = self.fields.all_fields[reg_name]["se"]
|
2021-02-21 04:18:40 +03:00
|
|
|
err_fields = ["ot", "s2ga", "s2gb", "s2vsa", "s2vsb"]
|
|
|
|
warn_fields = ["otpw", "t120", "t143", "t150", "t157"]
|
|
|
|
for f in err_fields + warn_fields:
|
|
|
|
if f in self.fields.all_fields[reg_name]:
|
|
|
|
mask |= self.fields.all_fields[reg_name][f]
|
|
|
|
if f in err_fields:
|
|
|
|
err_mask |= self.fields.all_fields[reg_name][f]
|
2021-03-15 18:16:15 +03:00
|
|
|
self.drv_status_reg_info = [0, reg_name, mask, err_mask, cs_actual_mask]
|
2021-02-21 04:18:40 +03:00
|
|
|
def _query_register(self, reg_info, try_clear=False):
|
2021-03-15 18:16:15 +03:00
|
|
|
last_value, reg_name, mask, err_mask, cs_actual_mask = reg_info
|
2021-08-06 06:21:31 +03:00
|
|
|
cleared_flags = 0
|
2021-02-21 04:18:40 +03:00
|
|
|
count = 0
|
|
|
|
while 1:
|
2021-03-01 00:44:03 +03:00
|
|
|
try:
|
|
|
|
val = self.mcu_tmc.get_register(reg_name)
|
|
|
|
except self.printer.command_error as e:
|
|
|
|
count += 1
|
|
|
|
if count < 3 and str(e).startswith("Unable to read tmc uart"):
|
|
|
|
# Allow more retries on a TMC UART read error
|
|
|
|
reactor = self.printer.get_reactor()
|
|
|
|
reactor.pause(reactor.monotonic() + 0.050)
|
|
|
|
continue
|
|
|
|
raise
|
2021-02-21 04:18:40 +03:00
|
|
|
if val & mask != last_value & mask:
|
|
|
|
fmt = self.fields.pretty_format(reg_name, val)
|
|
|
|
logging.info("TMC '%s' reports %s", self.stepper_name, fmt)
|
2021-08-28 02:24:14 +03:00
|
|
|
reg_info[0] = last_value = val
|
2021-02-21 04:18:40 +03:00
|
|
|
if not val & err_mask:
|
2021-03-15 18:16:15 +03:00
|
|
|
if not cs_actual_mask or val & cs_actual_mask:
|
|
|
|
break
|
|
|
|
irun = self.fields.get_field(self.irun_field)
|
|
|
|
if self.check_timer is None or irun < 4:
|
|
|
|
break
|
2021-08-05 21:25:53 +03:00
|
|
|
if (self.irun_field == "irun"
|
|
|
|
and not self.fields.get_field("ihold")):
|
2021-03-15 20:48:58 +03:00
|
|
|
break
|
2021-03-15 18:16:15 +03:00
|
|
|
# CS_ACTUAL field of zero - indicates a driver reset
|
2021-02-21 04:18:40 +03:00
|
|
|
count += 1
|
|
|
|
if count >= 3:
|
|
|
|
fmt = self.fields.pretty_format(reg_name, val)
|
|
|
|
raise self.printer.command_error("TMC '%s' reports error: %s"
|
|
|
|
% (self.stepper_name, fmt))
|
2021-03-15 18:16:15 +03:00
|
|
|
if try_clear and val & err_mask:
|
2021-02-21 04:18:40 +03:00
|
|
|
try_clear = False
|
2021-08-06 06:21:31 +03:00
|
|
|
cleared_flags |= val & err_mask
|
2021-02-21 04:18:40 +03:00
|
|
|
self.mcu_tmc.set_register(reg_name, val & err_mask)
|
2021-08-06 06:21:31 +03:00
|
|
|
return cleared_flags
|
|
|
|
def _do_periodic_check(self, eventtime):
|
2021-02-21 04:18:40 +03:00
|
|
|
try:
|
|
|
|
self._query_register(self.drv_status_reg_info)
|
|
|
|
if self.gstat_reg_info is not None:
|
2021-08-06 06:21:31 +03:00
|
|
|
self._query_register(self.gstat_reg_info)
|
2021-02-21 04:18:40 +03:00
|
|
|
except self.printer.command_error as e:
|
|
|
|
self.printer.invoke_shutdown(str(e))
|
|
|
|
return self.printer.get_reactor().NEVER
|
|
|
|
return eventtime + 1.
|
|
|
|
def stop_checks(self):
|
|
|
|
if self.check_timer is None:
|
|
|
|
return
|
|
|
|
self.printer.get_reactor().unregister_timer(self.check_timer)
|
|
|
|
self.check_timer = None
|
|
|
|
def start_checks(self):
|
|
|
|
if self.check_timer is not None:
|
|
|
|
self.stop_checks()
|
2021-08-06 06:21:31 +03:00
|
|
|
cleared_flags = 0
|
|
|
|
self._query_register(self.drv_status_reg_info)
|
|
|
|
if self.gstat_reg_info is not None:
|
|
|
|
cleared_flags = self._query_register(self.gstat_reg_info,
|
|
|
|
try_clear=self.clear_gstat)
|
2021-02-21 04:18:40 +03:00
|
|
|
reactor = self.printer.get_reactor()
|
|
|
|
curtime = reactor.monotonic()
|
|
|
|
self.check_timer = reactor.register_timer(self._do_periodic_check,
|
|
|
|
curtime + 1.)
|
2021-08-06 06:21:31 +03:00
|
|
|
if cleared_flags:
|
|
|
|
reset_mask = self.fields.all_fields["GSTAT"]["reset"]
|
|
|
|
if cleared_flags & reset_mask:
|
|
|
|
return True
|
|
|
|
return False
|
2021-08-28 02:24:14 +03:00
|
|
|
def get_status(self, eventtime=None):
|
|
|
|
if self.check_timer is None:
|
|
|
|
return {'drv_status': None}
|
|
|
|
last_value, reg_name = self.drv_status_reg_info[:2]
|
|
|
|
if last_value != self.last_drv_status:
|
|
|
|
self.last_drv_status = last_value
|
|
|
|
fields = self.fields.get_reg_fields(reg_name, last_value)
|
|
|
|
fields = {n: v for n, v in fields.items() if v}
|
|
|
|
self.last_status = {'drv_status': fields}
|
|
|
|
return self.last_status
|
2021-02-21 04:18:40 +03:00
|
|
|
|
|
|
|
|
2019-06-10 21:29:35 +03:00
|
|
|
######################################################################
|
|
|
|
# G-Code command helpers
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
class TMCCommandHelper:
|
2021-03-15 07:19:57 +03:00
|
|
|
def __init__(self, config, mcu_tmc, current_helper):
|
2019-06-10 21:29:35 +03:00
|
|
|
self.printer = config.get_printer()
|
2019-11-13 02:18:32 +03:00
|
|
|
self.stepper_name = ' '.join(config.get_name().split()[1:])
|
2019-06-10 21:29:35 +03:00
|
|
|
self.name = config.get_name().split()[-1]
|
|
|
|
self.mcu_tmc = mcu_tmc
|
2021-02-20 21:42:16 +03:00
|
|
|
self.current_helper = current_helper
|
2021-03-15 07:19:57 +03:00
|
|
|
self.echeck_helper = TMCErrorCheck(config, mcu_tmc)
|
2019-06-10 21:29:35 +03:00
|
|
|
self.fields = mcu_tmc.get_fields()
|
2019-06-23 19:37:27 +03:00
|
|
|
self.read_registers = self.read_translate = None
|
2019-11-13 02:18:32 +03:00
|
|
|
self.toff = None
|
2021-08-06 06:21:31 +03:00
|
|
|
self.mcu_phase_offset = None
|
|
|
|
self.stepper = None
|
|
|
|
self.stepper_enable = self.printer.load_object(config, "stepper_enable")
|
|
|
|
self.printer.register_event_handler("stepper:sync_mcu_position",
|
|
|
|
self._handle_sync_mcu_pos)
|
2022-01-30 03:13:31 +03:00
|
|
|
self.printer.register_event_handler("stepper:set_sdir_inverted",
|
|
|
|
self._handle_sync_mcu_pos)
|
2021-10-29 00:10:10 +03:00
|
|
|
self.printer.register_event_handler("klippy:mcu_identify",
|
|
|
|
self._handle_mcu_identify)
|
2019-11-13 02:18:32 +03:00
|
|
|
self.printer.register_event_handler("klippy:connect",
|
|
|
|
self._handle_connect)
|
2021-08-06 06:32:00 +03:00
|
|
|
# Set microstep config options
|
|
|
|
TMCMicrostepHelper(config, mcu_tmc)
|
2019-11-13 02:18:32 +03:00
|
|
|
# Register commands
|
2020-04-25 06:57:26 +03:00
|
|
|
gcode = self.printer.lookup_object("gcode")
|
|
|
|
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)
|
2021-02-20 21:42:16 +03:00
|
|
|
gcode.register_mux_command("SET_TMC_CURRENT", "STEPPER", self.name,
|
|
|
|
self.cmd_SET_TMC_CURRENT,
|
|
|
|
desc=self.cmd_SET_TMC_CURRENT_help)
|
2019-06-26 01:15:17 +03:00
|
|
|
def _init_registers(self, print_time=None):
|
2019-06-10 21:29:35 +03:00
|
|
|
# Send registers
|
|
|
|
for reg_name, val in self.fields.registers.items():
|
|
|
|
self.mcu_tmc.set_register(reg_name, val, print_time)
|
|
|
|
cmd_INIT_TMC_help = "Initialize TMC stepper driver registers"
|
2020-04-25 06:57:26 +03:00
|
|
|
def cmd_INIT_TMC(self, gcmd):
|
2019-06-10 21:29:35 +03:00
|
|
|
logging.info("INIT_TMC %s", self.name)
|
|
|
|
print_time = self.printer.lookup_object('toolhead').get_last_move_time()
|
|
|
|
self._init_registers(print_time)
|
|
|
|
cmd_SET_TMC_FIELD_help = "Set a register field of a TMC driver"
|
2020-04-25 06:57:26 +03:00
|
|
|
def cmd_SET_TMC_FIELD(self, gcmd):
|
2021-08-05 21:25:53 +03:00
|
|
|
field_name = gcmd.get('FIELD').lower()
|
2019-06-10 21:29:35 +03:00
|
|
|
reg_name = self.fields.lookup_register(field_name, None)
|
|
|
|
if reg_name is None:
|
2020-04-25 06:57:26 +03:00
|
|
|
raise gcmd.error("Unknown field name '%s'" % (field_name,))
|
|
|
|
value = gcmd.get_int('VALUE')
|
2019-06-10 21:29:35 +03:00
|
|
|
reg_val = self.fields.set_field(field_name, value)
|
|
|
|
print_time = self.printer.lookup_object('toolhead').get_last_move_time()
|
|
|
|
self.mcu_tmc.set_register(reg_name, reg_val, print_time)
|
2021-02-20 21:42:16 +03:00
|
|
|
cmd_SET_TMC_CURRENT_help = "Set the current of a TMC driver"
|
|
|
|
def cmd_SET_TMC_CURRENT(self, gcmd):
|
|
|
|
ch = self.current_helper
|
2021-10-25 03:03:42 +03:00
|
|
|
prev_cur, prev_hold_cur, req_hold_cur, max_cur = ch.get_current()
|
|
|
|
run_current = gcmd.get_float('CURRENT', None, minval=0., maxval=max_cur)
|
2021-02-20 21:42:16 +03:00
|
|
|
hold_current = gcmd.get_float('HOLDCURRENT', None,
|
2021-10-25 03:03:42 +03:00
|
|
|
above=0., maxval=max_cur)
|
2021-02-20 22:08:24 +03:00
|
|
|
if run_current is not None or hold_current is not None:
|
|
|
|
if run_current is None:
|
2021-10-25 03:03:42 +03:00
|
|
|
run_current = prev_cur
|
2021-02-20 22:08:24 +03:00
|
|
|
if hold_current is None:
|
2021-10-25 03:03:42 +03:00
|
|
|
hold_current = req_hold_cur
|
2021-02-20 22:08:24 +03:00
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
|
|
|
print_time = toolhead.get_last_move_time()
|
|
|
|
ch.set_current(run_current, hold_current, print_time)
|
2021-10-25 03:03:42 +03:00
|
|
|
prev_cur, prev_hold_cur, req_hold_cur, max_cur = ch.get_current()
|
2021-02-20 22:08:24 +03:00
|
|
|
# Report values
|
2021-10-25 03:03:42 +03:00
|
|
|
if prev_hold_cur is None:
|
|
|
|
gcmd.respond_info("Run Current: %0.2fA" % (prev_cur,))
|
2021-02-20 22:08:24 +03:00
|
|
|
else:
|
|
|
|
gcmd.respond_info("Run Current: %0.2fA Hold Current: %0.2fA"
|
2021-10-25 03:03:42 +03:00
|
|
|
% (prev_cur, prev_hold_cur))
|
2021-08-06 06:32:00 +03:00
|
|
|
# Stepper phase tracking
|
2021-08-06 07:38:49 +03:00
|
|
|
def _get_phases(self):
|
|
|
|
return (256 >> self.fields.get_field("mres")) * 4
|
|
|
|
def get_phase_offset(self):
|
|
|
|
return self.mcu_phase_offset, self._get_phases()
|
|
|
|
def _query_phase(self):
|
2021-08-06 06:32:00 +03:00
|
|
|
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))
|
2021-08-06 07:38:49 +03:00
|
|
|
return self.fields.get_field(field_name, reg)
|
2021-08-06 06:21:31 +03:00
|
|
|
def _handle_sync_mcu_pos(self, stepper):
|
|
|
|
if stepper.get_name() != self.stepper_name:
|
|
|
|
return
|
|
|
|
try:
|
2021-08-06 07:38:49 +03:00
|
|
|
driver_phase = self._query_phase()
|
2021-08-06 06:21:31 +03:00
|
|
|
except self.printer.command_error as e:
|
|
|
|
logging.info("Unable to obtain tmc %s phase", self.stepper_name)
|
|
|
|
self.mcu_phase_offset = None
|
|
|
|
enable_line = self.stepper_enable.lookup_enable(self.stepper_name)
|
|
|
|
if enable_line.is_motor_enabled():
|
|
|
|
raise
|
|
|
|
return
|
2022-01-30 03:13:31 +03:00
|
|
|
if not stepper.get_dir_inverted()[0]:
|
2021-08-06 07:38:49 +03:00
|
|
|
driver_phase = 1023 - driver_phase
|
|
|
|
phases = self._get_phases()
|
|
|
|
phase = int(float(driver_phase) / 1024 * phases + .5) % phases
|
2021-08-06 06:21:31 +03:00
|
|
|
moff = (phase - stepper.get_mcu_position()) % phases
|
|
|
|
if self.mcu_phase_offset is not None and self.mcu_phase_offset != moff:
|
|
|
|
logging.warning("Stepper %s phase change (was %d now %d)",
|
2022-01-23 04:41:44 +03:00
|
|
|
self.stepper_name, self.mcu_phase_offset, moff)
|
2021-08-06 06:21:31 +03:00
|
|
|
self.mcu_phase_offset = moff
|
2021-02-21 04:14:45 +03:00
|
|
|
# Stepper enable/disable tracking
|
|
|
|
def _do_enable(self, print_time):
|
|
|
|
try:
|
|
|
|
if self.toff is not None:
|
|
|
|
# Shared enable via comms handling
|
2021-03-01 02:04:20 +03:00
|
|
|
self.fields.set_field("toff", self.toff)
|
|
|
|
self._init_registers()
|
2021-08-06 06:21:31 +03:00
|
|
|
did_reset = self.echeck_helper.start_checks()
|
|
|
|
if did_reset:
|
|
|
|
self.mcu_phase_offset = None
|
|
|
|
# Calculate phase offset
|
|
|
|
if self.mcu_phase_offset is not None:
|
|
|
|
return
|
|
|
|
gcode = self.printer.lookup_object("gcode")
|
|
|
|
with gcode.get_mutex():
|
|
|
|
if self.mcu_phase_offset is not None:
|
|
|
|
return
|
|
|
|
logging.info("Pausing toolhead to calculate %s phase offset",
|
|
|
|
self.stepper_name)
|
|
|
|
self.printer.lookup_object('toolhead').wait_moves()
|
|
|
|
self._handle_sync_mcu_pos(self.stepper)
|
2021-02-21 04:14:45 +03:00
|
|
|
except self.printer.command_error as e:
|
|
|
|
self.printer.invoke_shutdown(str(e))
|
|
|
|
def _do_disable(self, print_time):
|
|
|
|
try:
|
|
|
|
if self.toff is not None:
|
|
|
|
val = self.fields.set_field("toff", 0)
|
|
|
|
reg_name = self.fields.lookup_register("toff")
|
|
|
|
self.mcu_tmc.set_register(reg_name, val, print_time)
|
2021-02-21 04:18:40 +03:00
|
|
|
self.echeck_helper.stop_checks()
|
2021-02-21 04:14:45 +03:00
|
|
|
except self.printer.command_error as e:
|
|
|
|
self.printer.invoke_shutdown(str(e))
|
2021-10-29 00:10:10 +03:00
|
|
|
def _handle_mcu_identify(self):
|
|
|
|
# Lookup stepper object
|
|
|
|
force_move = self.printer.lookup_object("force_move")
|
|
|
|
self.stepper = force_move.lookup_stepper(self.stepper_name)
|
|
|
|
# Note pulse duration and step_both_edge optimizations available
|
|
|
|
self.stepper.setup_default_pulse_duration(.000000100, True)
|
2021-10-29 18:45:39 +03:00
|
|
|
def _handle_stepper_enable(self, print_time, is_enable):
|
2021-02-21 04:14:45 +03:00
|
|
|
if is_enable:
|
|
|
|
cb = (lambda ev: self._do_enable(print_time))
|
|
|
|
else:
|
|
|
|
cb = (lambda ev: self._do_disable(print_time))
|
2019-11-13 02:18:32 +03:00
|
|
|
self.printer.get_reactor().register_callback(cb)
|
2021-02-21 04:14:45 +03:00
|
|
|
def _handle_connect(self):
|
2021-10-29 00:10:10 +03:00
|
|
|
# Check if using step on both edges optimization
|
|
|
|
pulse_duration, step_both_edge = self.stepper.get_pulse_duration()
|
|
|
|
if step_both_edge:
|
|
|
|
self.fields.set_field("dedge", 1)
|
2021-02-21 04:14:45 +03:00
|
|
|
# Check for soft stepper enable/disable
|
2021-08-06 06:21:31 +03:00
|
|
|
enable_line = self.stepper_enable.lookup_enable(self.stepper_name)
|
2021-10-29 18:45:39 +03:00
|
|
|
enable_line.register_state_callback(self._handle_stepper_enable)
|
2021-02-21 04:14:45 +03:00
|
|
|
if not enable_line.has_dedicated_enable():
|
|
|
|
self.toff = self.fields.get_field("toff")
|
|
|
|
self.fields.set_field("toff", 0)
|
|
|
|
logging.info("Enabling TMC virtual enable for '%s'",
|
|
|
|
self.stepper_name)
|
|
|
|
# Send init
|
|
|
|
try:
|
|
|
|
self._init_registers()
|
|
|
|
except self.printer.command_error as e:
|
|
|
|
logging.info("TMC %s failed to init: %s", self.name, str(e))
|
2021-08-28 02:24:14 +03:00
|
|
|
# get_status information export
|
|
|
|
def get_status(self, eventtime=None):
|
2021-08-30 21:17:42 +03:00
|
|
|
cpos = None
|
2021-08-28 02:24:14 +03:00
|
|
|
if self.stepper is not None and self.mcu_phase_offset is not None:
|
|
|
|
cpos = self.stepper.mcu_to_commanded_position(self.mcu_phase_offset)
|
2021-10-11 19:03:08 +03:00
|
|
|
current = self.current_helper.get_current()
|
2021-08-28 02:24:14 +03:00
|
|
|
res = {'mcu_phase_offset': self.mcu_phase_offset,
|
2021-10-11 19:03:08 +03:00
|
|
|
'phase_offset_position': cpos,
|
|
|
|
'run_current': current[0],
|
|
|
|
'hold_current': current[1]}
|
2021-08-28 02:24:14 +03:00
|
|
|
res.update(self.echeck_helper.get_status(eventtime))
|
|
|
|
return res
|
2019-06-10 21:29:35 +03:00
|
|
|
# DUMP_TMC support
|
2019-06-23 19:37:27 +03:00
|
|
|
def setup_register_dump(self, read_registers, read_translate=None):
|
|
|
|
self.read_registers = read_registers
|
|
|
|
self.read_translate = read_translate
|
2020-04-25 06:57:26 +03:00
|
|
|
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)
|
2019-06-10 21:29:35 +03:00
|
|
|
cmd_DUMP_TMC_help = "Read and display TMC stepper driver registers"
|
2020-04-25 06:57:26 +03:00
|
|
|
def cmd_DUMP_TMC(self, gcmd):
|
2019-06-10 21:29:35 +03:00
|
|
|
logging.info("DUMP_TMC %s", self.name)
|
|
|
|
print_time = self.printer.lookup_object('toolhead').get_last_move_time()
|
2020-04-25 06:57:26 +03:00
|
|
|
gcmd.respond_info("========== Write-only registers ==========")
|
2019-06-10 21:29:35 +03:00
|
|
|
for reg_name, val in self.fields.registers.items():
|
2019-06-23 19:37:27 +03:00
|
|
|
if reg_name not in self.read_registers:
|
2020-04-25 06:57:26 +03:00
|
|
|
gcmd.respond_info(self.fields.pretty_format(reg_name, val))
|
|
|
|
gcmd.respond_info("========== Queried registers ==========")
|
2019-06-23 19:37:27 +03:00
|
|
|
for reg_name in self.read_registers:
|
|
|
|
val = self.mcu_tmc.get_register(reg_name)
|
|
|
|
if self.read_translate is not None:
|
|
|
|
reg_name, val = self.read_translate(reg_name, val)
|
2020-04-25 06:57:26 +03:00
|
|
|
gcmd.respond_info(self.fields.pretty_format(reg_name, val))
|
2019-06-10 21:29:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
######################################################################
|
2019-06-13 06:28:22 +03:00
|
|
|
# TMC virtual pins
|
2019-06-10 21:29:35 +03:00
|
|
|
######################################################################
|
|
|
|
|
2020-02-12 21:03:42 +03:00
|
|
|
# Helper class for "sensorless homing"
|
|
|
|
class TMCVirtualPinHelper:
|
2020-08-09 15:17:51 +03:00
|
|
|
def __init__(self, config, mcu_tmc):
|
2020-02-12 21:03:42 +03:00
|
|
|
self.printer = config.get_printer()
|
2019-06-10 21:29:35 +03:00
|
|
|
self.mcu_tmc = mcu_tmc
|
|
|
|
self.fields = mcu_tmc.get_fields()
|
2020-08-09 15:17:51 +03:00
|
|
|
if self.fields.lookup_register('diag0_stall') is not None:
|
|
|
|
if config.get('diag0_pin', None) is not None:
|
|
|
|
self.diag_pin = config.get('diag0_pin')
|
|
|
|
self.diag_pin_field = 'diag0_stall'
|
|
|
|
else:
|
|
|
|
self.diag_pin = config.get('diag1_pin', None)
|
|
|
|
self.diag_pin_field = 'diag1_stall'
|
|
|
|
else:
|
|
|
|
self.diag_pin = config.get('diag_pin', None)
|
|
|
|
self.diag_pin_field = None
|
2020-02-12 21:03:42 +03:00
|
|
|
self.mcu_endstop = None
|
|
|
|
self.en_pwm = False
|
|
|
|
self.pwmthrs = 0
|
|
|
|
# Register virtual_endstop pin
|
|
|
|
name_parts = config.get_name().split()
|
|
|
|
ppins = self.printer.lookup_object("pins")
|
|
|
|
ppins.register_chip("%s_%s" % (name_parts[0], name_parts[-1]), self)
|
|
|
|
def setup_pin(self, pin_type, pin_params):
|
|
|
|
# Validate pin
|
|
|
|
ppins = self.printer.lookup_object('pins')
|
|
|
|
if pin_type != 'endstop' or pin_params['pin'] != 'virtual_endstop':
|
|
|
|
raise ppins.error("tmc virtual endstop only useful as endstop")
|
|
|
|
if pin_params['invert'] or pin_params['pullup']:
|
|
|
|
raise ppins.error("Can not pullup/invert tmc virtual pin")
|
|
|
|
if self.diag_pin is None:
|
|
|
|
raise ppins.error("tmc virtual endstop requires diag pin config")
|
|
|
|
# Setup for sensorless homing
|
2019-08-18 04:05:30 +03:00
|
|
|
reg = self.fields.lookup_register("en_pwm_mode", None)
|
|
|
|
if reg is None:
|
2021-08-05 21:25:53 +03:00
|
|
|
self.en_pwm = not self.fields.get_field("en_spreadcycle")
|
|
|
|
self.pwmthrs = self.fields.get_field("tpwmthrs")
|
2019-08-18 04:05:30 +03:00
|
|
|
else:
|
|
|
|
self.en_pwm = self.fields.get_field("en_pwm_mode")
|
|
|
|
self.pwmthrs = 0
|
2020-02-12 21:03:42 +03:00
|
|
|
self.printer.register_event_handler("homing:homing_move_begin",
|
|
|
|
self.handle_homing_move_begin)
|
|
|
|
self.printer.register_event_handler("homing:homing_move_end",
|
|
|
|
self.handle_homing_move_end)
|
|
|
|
self.mcu_endstop = ppins.setup_pin('endstop', self.diag_pin)
|
|
|
|
return self.mcu_endstop
|
2021-03-29 21:23:46 +03:00
|
|
|
def handle_homing_move_begin(self, hmove):
|
|
|
|
if self.mcu_endstop not in hmove.get_mcu_endstops():
|
2020-02-12 21:03:42 +03:00
|
|
|
return
|
2019-08-18 04:05:30 +03:00
|
|
|
reg = self.fields.lookup_register("en_pwm_mode", None)
|
|
|
|
if reg is None:
|
|
|
|
# On "stallguard4" drivers, "stealthchop" must be enabled
|
2021-08-05 21:25:53 +03:00
|
|
|
tp_val = self.fields.set_field("tpwmthrs", 0)
|
2021-03-01 02:30:25 +03:00
|
|
|
self.mcu_tmc.set_register("TPWMTHRS", tp_val)
|
2021-08-05 21:25:53 +03:00
|
|
|
val = self.fields.set_field("en_spreadcycle", 0)
|
2019-08-18 04:05:30 +03:00
|
|
|
else:
|
|
|
|
# On earlier drivers, "stealthchop" must be disabled
|
|
|
|
self.fields.set_field("en_pwm_mode", 0)
|
2020-08-09 15:17:51 +03:00
|
|
|
val = self.fields.set_field(self.diag_pin_field, 1)
|
2019-06-10 21:29:35 +03:00
|
|
|
self.mcu_tmc.set_register("GCONF", val)
|
2021-08-05 21:25:53 +03:00
|
|
|
tc_val = self.fields.set_field("tcoolthrs", 0xfffff)
|
2021-03-01 02:30:25 +03:00
|
|
|
self.mcu_tmc.set_register("TCOOLTHRS", tc_val)
|
2021-03-29 21:23:46 +03:00
|
|
|
def handle_homing_move_end(self, hmove):
|
|
|
|
if self.mcu_endstop not in hmove.get_mcu_endstops():
|
2020-02-12 21:03:42 +03:00
|
|
|
return
|
2019-08-18 04:05:30 +03:00
|
|
|
reg = self.fields.lookup_register("en_pwm_mode", None)
|
|
|
|
if reg is None:
|
2021-08-05 21:25:53 +03:00
|
|
|
tp_val = self.fields.set_field("tpwmthrs", self.pwmthrs)
|
2021-03-01 02:30:25 +03:00
|
|
|
self.mcu_tmc.set_register("TPWMTHRS", tp_val)
|
2021-08-05 21:25:53 +03:00
|
|
|
val = self.fields.set_field("en_spreadcycle", not self.en_pwm)
|
2019-08-18 04:05:30 +03:00
|
|
|
else:
|
|
|
|
self.fields.set_field("en_pwm_mode", self.en_pwm)
|
2020-08-09 15:17:51 +03:00
|
|
|
val = self.fields.set_field(self.diag_pin_field, 0)
|
2019-06-10 21:29:35 +03:00
|
|
|
self.mcu_tmc.set_register("GCONF", val)
|
2021-08-05 21:25:53 +03:00
|
|
|
tc_val = self.fields.set_field("tcoolthrs", 0)
|
2021-03-01 02:30:25 +03:00
|
|
|
self.mcu_tmc.set_register("TCOOLTHRS", tc_val)
|
2019-06-10 21:29:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Config reading helpers
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
# Helper to configure and query the microstep settings
|
2021-08-06 06:32:00 +03:00
|
|
|
def TMCMicrostepHelper(config, mcu_tmc):
|
|
|
|
fields = mcu_tmc.get_fields()
|
|
|
|
stepper_name = " ".join(config.get_name().split()[1:])
|
2022-09-05 20:52:17 +03:00
|
|
|
if not config.has_section(stepper_name):
|
|
|
|
raise config.error(
|
|
|
|
"Could not find config section '[%s]' required by tmc driver"
|
|
|
|
% (stepper_name,))
|
2021-08-06 06:32:00 +03:00
|
|
|
stepper_config = ms_config = config.getsection(stepper_name)
|
|
|
|
if (stepper_config.get('microsteps', None, note_valid=False) is None
|
|
|
|
and config.get('microsteps', None, note_valid=False) is not None):
|
|
|
|
# Older config format with microsteps in tmc config section
|
|
|
|
ms_config = config
|
2021-08-25 17:36:45 +03:00
|
|
|
steps = {256: 0, 128: 1, 64: 2, 32: 3, 16: 4, 8: 5, 4: 6, 2: 7, 1: 8}
|
|
|
|
mres = ms_config.getchoice('microsteps', steps)
|
2021-08-06 06:32:00 +03:00
|
|
|
fields.set_field("mres", mres)
|
|
|
|
fields.set_field("intpol", config.getboolean("interpolate", True))
|
2019-06-10 21:29:35 +03:00
|
|
|
|
|
|
|
# Helper to configure "stealthchop" mode
|
2020-02-20 19:32:38 +03:00
|
|
|
def TMCStealthchopHelper(config, mcu_tmc, tmc_freq):
|
|
|
|
fields = mcu_tmc.get_fields()
|
|
|
|
en_pwm_mode = False
|
|
|
|
velocity = config.getfloat('stealthchop_threshold', 0., minval=0.)
|
|
|
|
if velocity:
|
|
|
|
stepper_name = " ".join(config.get_name().split()[1:])
|
2022-01-29 22:13:26 +03:00
|
|
|
sconfig = config.getsection(stepper_name)
|
|
|
|
rotation_dist, steps_per_rotation = stepper.parse_step_distance(sconfig)
|
|
|
|
step_dist = rotation_dist / steps_per_rotation
|
2021-08-05 21:25:53 +03:00
|
|
|
step_dist_256 = step_dist / (1 << fields.get_field("mres"))
|
2020-02-20 19:32:38 +03:00
|
|
|
threshold = int(tmc_freq * step_dist_256 / velocity + .5)
|
2021-08-05 21:25:53 +03:00
|
|
|
fields.set_field("tpwmthrs", max(0, min(0xfffff, threshold)))
|
2020-02-20 19:32:38 +03:00
|
|
|
en_pwm_mode = True
|
|
|
|
reg = fields.lookup_register("en_pwm_mode", None)
|
|
|
|
if reg is not None:
|
|
|
|
fields.set_field("en_pwm_mode", en_pwm_mode)
|
|
|
|
else:
|
|
|
|
# TMC2208 uses en_spreadCycle
|
2021-08-05 21:25:53 +03:00
|
|
|
fields.set_field("en_spreadcycle", not en_pwm_mode)
|