2018-09-11 19:34:05 +03:00
|
|
|
# TMC2660 configuration
|
|
|
|
#
|
2019-03-01 22:44:35 +03:00
|
|
|
# Copyright (C) 2018-2019 Florian Heilmann <Florian.Heilmann@gmx.net>
|
2018-09-11 19:34:05 +03:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2019-03-05 22:04:59 +03:00
|
|
|
import math, collections, logging
|
2019-03-02 03:41:00 +03:00
|
|
|
import bus, tmc2130
|
2018-09-11 19:34:05 +03:00
|
|
|
|
2019-03-22 20:26:40 +03:00
|
|
|
def current_bits(current, sense_resistor, vsense_on):
|
2019-03-01 22:44:35 +03:00
|
|
|
vsense = 0.165 if vsense_on else 0.310
|
|
|
|
cs = int(32 * current * sense_resistor * math.sqrt(2.) / vsense - 1. + .5)
|
2019-03-02 03:41:00 +03:00
|
|
|
return max(0, min(cs, 31))
|
|
|
|
|
2019-03-22 20:26:40 +03:00
|
|
|
def get_config_current(run_current, sense_resistor):
|
|
|
|
vsense = False
|
|
|
|
cs = current_bits(run_current, sense_resistor, vsense)
|
|
|
|
if cs < 16:
|
|
|
|
vsense = True
|
|
|
|
cs = current_bits(run_current, sense_resistor, vsense)
|
|
|
|
return cs, vsense
|
|
|
|
|
2019-03-02 03:41:00 +03:00
|
|
|
Registers = {
|
|
|
|
"DRVCONF": 0xE, "SGCSCONF": 0xC, "SMARTEN": 0xA,
|
|
|
|
"CHOPCONF": 0x8, "DRVCTRL": 0x0
|
|
|
|
}
|
|
|
|
|
|
|
|
Fields = {}
|
|
|
|
|
|
|
|
Fields["DRVCTRL"] = {
|
|
|
|
"MRES": 0x0f,
|
|
|
|
"DEDGE": 0x01 << 8,
|
|
|
|
"INTPOL": 0x01 << 9,
|
|
|
|
}
|
2018-09-11 19:34:05 +03:00
|
|
|
|
2019-03-02 03:41:00 +03:00
|
|
|
Fields["CHOPCONF"] = {
|
|
|
|
"TOFF": 0x0f,
|
|
|
|
"HSTRT": 0x7 << 4,
|
|
|
|
"HEND": 0x0f << 7,
|
|
|
|
"HDEC": 0x03 << 11,
|
|
|
|
"RNDTF": 0x01 << 13,
|
|
|
|
"CHM": 0x01 << 14,
|
|
|
|
"TBL": 0x03 << 15
|
2018-09-11 19:34:05 +03:00
|
|
|
}
|
|
|
|
|
2019-03-02 03:41:00 +03:00
|
|
|
Fields["SMARTEN"] = {
|
|
|
|
"SEMIN" : 0x0f,
|
|
|
|
"SEUP": 0x03 << 5,
|
|
|
|
"SEMAX": 0x0f << 8,
|
|
|
|
"SEDN": 0x03 << 13,
|
|
|
|
"SEIMIN": 0x01 << 15
|
2018-09-11 19:34:05 +03:00
|
|
|
}
|
|
|
|
|
2019-03-02 03:41:00 +03:00
|
|
|
Fields["SGCSCONF"] = {
|
|
|
|
"CS": 0x1f,
|
|
|
|
"SGT": 0x7F << 8,
|
|
|
|
"SFILT": 0x01 << 16
|
2018-09-11 19:34:05 +03:00
|
|
|
}
|
|
|
|
|
2019-03-02 03:41:00 +03:00
|
|
|
Fields["DRVCONF"] = {
|
|
|
|
"RDSEL": 0x03 << 4,
|
|
|
|
"VSENSE": 0x01 << 6,
|
|
|
|
"SDOFF": 0x01 << 7,
|
|
|
|
"TS2G": 0x03 << 8,
|
|
|
|
"DISS2G": 0x01 << 10,
|
|
|
|
"SLPL": 0x03 << 12,
|
|
|
|
"SLPH": 0x03 << 14,
|
|
|
|
"TST": 0x01 << 16
|
2018-09-11 19:34:05 +03:00
|
|
|
}
|
|
|
|
|
2019-03-02 03:41:00 +03:00
|
|
|
Fields["READRSP@RDSEL0"] = {
|
|
|
|
"SG": 0x01,
|
|
|
|
"OT": 0x01 << 1,
|
|
|
|
"OTPW": 0x01 << 2,
|
|
|
|
"S2GA": 0x01 << 3,
|
|
|
|
"S2GB": 0x01 << 4,
|
|
|
|
"OLA": 0x01 << 5,
|
|
|
|
"OLB": 0x01 << 6,
|
|
|
|
"STST": 0x01 << 7,
|
|
|
|
"MSTEP": 0x3ff << 10
|
2018-09-11 19:34:05 +03:00
|
|
|
}
|
|
|
|
|
2019-03-02 03:41:00 +03:00
|
|
|
Fields["READRSP@RDSEL1"] = {
|
|
|
|
"SG": 0x01,
|
|
|
|
"OT": 0x01 << 1,
|
|
|
|
"OTPW": 0x01 << 2,
|
|
|
|
"S2GA": 0x01 << 3,
|
|
|
|
"S2GB": 0x01 << 4,
|
|
|
|
"OLA": 0x01 << 5,
|
|
|
|
"OLB": 0x01 << 6,
|
|
|
|
"STST": 0x01 << 7,
|
|
|
|
"SG@RDSEL1": 0x3ff << 10
|
|
|
|
}
|
|
|
|
|
|
|
|
Fields["READRSP@RDSEL2"] = {
|
|
|
|
"SG": 0x01,
|
|
|
|
"OT": 0x01 << 1,
|
|
|
|
"OTPW": 0x01 << 2,
|
|
|
|
"S2GA": 0x01 << 3,
|
|
|
|
"S2GB": 0x01 << 4,
|
|
|
|
"OLA": 0x01 << 5,
|
|
|
|
"OLB": 0x01 << 6,
|
|
|
|
"STST": 0x01 << 7,
|
|
|
|
"SG@RDSEL2": 0x1f << 15,
|
|
|
|
"SE": 0x1f << 10
|
|
|
|
}
|
|
|
|
|
2019-05-29 16:14:21 +03:00
|
|
|
SignedFields = ["SGT"]
|
|
|
|
|
2019-03-02 03:41:00 +03:00
|
|
|
FieldFormatters = {
|
|
|
|
"MRES": (lambda v: "%d(%dusteps)" % (v, 0x100 >> v)),
|
|
|
|
"DEDGE": (lambda v:
|
|
|
|
"1(Both Edges Active)" if v else "0(Only Rising Edge active)"),
|
|
|
|
"INTPOL": (lambda v: "1(On)" if v else "0(Off)"),
|
|
|
|
"TOFF": (lambda v: ("%d" % v) if v else "0(Driver Disabled!)"),
|
|
|
|
"CHM": (lambda v: "1(constant toff)" if v else "0(spreadCycle)"),
|
|
|
|
"SFILT": (lambda v: "1(Filtered mode)" if v else "0(Standard mode)"),
|
|
|
|
"VSENSE": (lambda v: "%d(%dmV)" % (v, 165 if v else 305)),
|
|
|
|
"SDOFF": (lambda v: "1(Step/Dir disabled" if v else "0(Step/dir enabled)"),
|
|
|
|
"DISS2G": (lambda v: "%d(Short to GND protection %s)" % (v,
|
|
|
|
"disabled" if v else "enabled")),
|
|
|
|
"MSTEP": (lambda v: "%d(%d, OA1 %s OA2)" % (v, v & 0xff,
|
|
|
|
"<=" if v & 0x100 else "=>")),
|
|
|
|
"SG": (lambda v: "%d(%s)" % (v, "Stall!" if v else "No Stall!")),
|
|
|
|
"OT": (lambda v: "1(Overtemp shutdown!)" if v else ""),
|
|
|
|
"OTPW": (lambda v: "1(Overtemp warning!)" if v else ""),
|
|
|
|
"S2GA": (lambda v: "1(Short to GND Coil A!)" if v else ""),
|
|
|
|
"S2GB": (lambda v: "1(Short to GND Coil B!)" if v else ""),
|
|
|
|
"OLA": (lambda v: "1(Open Load Coil A at slow speed!)" if v else ""),
|
|
|
|
"OLB": (lambda v: "1(Open Load Coil B at slow speed!)" if v else ""),
|
|
|
|
"STST": (lambda v: "1(Standstill detected!)" if v else ""),
|
2018-09-11 19:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class TMC2660:
|
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
|
|
|
self.name = config.get_name().split()[1]
|
2019-03-05 21:45:50 +03:00
|
|
|
self.spi = bus.MCU_SPI_from_config(config, 0, default_speed=4000000)
|
2018-10-24 19:23:20 +03:00
|
|
|
# Add SET_CURRENT and DUMP_TMC commands
|
2018-10-24 19:17:49 +03:00
|
|
|
gcode = self.printer.lookup_object("gcode")
|
|
|
|
gcode.register_mux_command(
|
|
|
|
"SET_TMC_CURRENT", "STEPPER", self.name,
|
|
|
|
self.cmd_SET_TMC_CURRENT, desc=self.cmd_SET_TMC_CURRENT_help)
|
2018-10-24 19:23:20 +03:00
|
|
|
gcode.register_mux_command(
|
|
|
|
"DUMP_TMC", "STEPPER", self.name,
|
|
|
|
self.cmd_DUMP_TMC, desc=self.cmd_DUMP_TMC_help)
|
2019-03-05 22:04:59 +03:00
|
|
|
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)
|
2018-10-24 19:17:49 +03:00
|
|
|
# Setup driver registers
|
2019-03-02 03:41:00 +03:00
|
|
|
self.regs = collections.OrderedDict()
|
2019-05-29 16:14:21 +03:00
|
|
|
self.fields = tmc2130.FieldHelper(Fields, SignedFields, FieldFormatters,
|
|
|
|
self.regs)
|
2019-03-02 03:41:00 +03:00
|
|
|
set_config_field = self.fields.set_config_field
|
|
|
|
|
2018-09-11 19:34:05 +03:00
|
|
|
# DRVCTRL
|
|
|
|
steps = {'256': 0, '128': 1, '64': 2, '32': 3, '16': 4,
|
|
|
|
'8': 5, '4': 6, '2': 7, '1': 8}
|
|
|
|
self.driver_mres = config.getchoice('microsteps', steps)
|
2019-03-02 03:41:00 +03:00
|
|
|
self.fields.set_field("MRES", self.driver_mres)
|
|
|
|
set_config_field(config, "DEDGE", 0)
|
|
|
|
set_config_field(config, "INTPOL", True, 'interpolate')
|
2018-09-11 19:34:05 +03:00
|
|
|
# CHOPCONF
|
2019-03-02 03:41:00 +03:00
|
|
|
set_config_field(config, "TBL", 2)
|
|
|
|
set_config_field(config, "RNDTF", 0)
|
|
|
|
set_config_field(config, "HDEC", 0)
|
|
|
|
set_config_field(config, "CHM", 0)
|
2019-03-22 20:26:40 +03:00
|
|
|
set_config_field(config, "HEND", 3)
|
2019-03-02 03:41:00 +03:00
|
|
|
set_config_field(config, "HSTRT", 3)
|
|
|
|
set_config_field(config, "TOFF", 4)
|
|
|
|
if not self.fields.get_field("CHM"):
|
|
|
|
if (self.fields.get_field("HSTRT") +
|
|
|
|
self.fields.get_field("HEND")) > 15:
|
|
|
|
raise config.error("driver_HEND + driver_HSTRT must be <= 15")
|
2018-09-11 19:34:05 +03:00
|
|
|
# SMARTEN
|
2019-03-02 03:41:00 +03:00
|
|
|
set_config_field(config, "SEIMIN", 0)
|
|
|
|
set_config_field(config, "SEDN", 0)
|
|
|
|
set_config_field(config, "SEMAX", 0)
|
|
|
|
set_config_field(config, "SEUP", 0)
|
|
|
|
set_config_field(config, "SEMIN", 0)
|
2018-09-11 19:34:05 +03:00
|
|
|
|
2019-03-22 20:26:40 +03:00
|
|
|
# SGSCONF
|
|
|
|
set_config_field(config, "SFILT", 1)
|
|
|
|
set_config_field(config, "SGT", 0)
|
|
|
|
self.current = config.getfloat('run_current', minval=0.1,
|
|
|
|
maxval=2.4)
|
|
|
|
self.sense_resistor = config.getfloat('sense_resistor')
|
|
|
|
(self.driver_cs,
|
|
|
|
self.driver_vsense) = get_config_current(self.current,
|
|
|
|
self.sense_resistor)
|
|
|
|
self.fields.set_field("CS", self.driver_cs)
|
|
|
|
|
2018-09-11 19:34:05 +03:00
|
|
|
# DRVCONF
|
2019-03-02 03:41:00 +03:00
|
|
|
set_config_field(config, "SLPH", 0)
|
|
|
|
set_config_field(config, "SLPL", 0)
|
|
|
|
set_config_field(config, "DISS2G", 0)
|
|
|
|
set_config_field(config, "TS2G", 3)
|
2019-03-22 20:26:40 +03:00
|
|
|
self.fields.set_field("VSENSE", self.driver_vsense)
|
2019-03-02 03:41:00 +03:00
|
|
|
self.fields.set_field("RDSEL", 0) # needed for phase calculations
|
|
|
|
self.fields.set_field("SDOFF", 0) # only step/dir mode supported
|
2018-09-11 19:34:05 +03:00
|
|
|
|
2019-03-02 03:41:00 +03:00
|
|
|
# Init Registers
|
|
|
|
self._init_registers(self)
|
2018-09-11 19:34:05 +03:00
|
|
|
|
2019-03-02 03:41:00 +03:00
|
|
|
# Register ready/printing handlers
|
2019-02-27 22:32:46 +03:00
|
|
|
self.idle_current_percentage = config.getint(
|
|
|
|
'idle_current_percent', default=100, minval=0, maxval=100)
|
2018-10-24 19:17:49 +03:00
|
|
|
if self.idle_current_percentage < 100:
|
|
|
|
self.printer.register_event_handler("idle_timeout:printing",
|
|
|
|
self.handle_printing)
|
|
|
|
self.printer.register_event_handler("idle_timeout:ready",
|
|
|
|
self.handle_ready)
|
|
|
|
|
2019-03-02 03:41:00 +03:00
|
|
|
def _init_registers(self, min_clock=0):
|
|
|
|
for reg_name in Registers:
|
|
|
|
self.set_register(reg_name, self.regs[reg_name])
|
|
|
|
|
|
|
|
def set_register(self, reg_name, val, min_clock=0):
|
|
|
|
reg = Registers[reg_name]
|
|
|
|
self.spi.spi_send([((val >> 16) | reg) & 0xff,
|
|
|
|
(val >> 8) & 0xff, val & 0xff], min_clock)
|
|
|
|
|
|
|
|
def get_response(self):
|
|
|
|
reg = Registers["DRVCTRL"]
|
|
|
|
val = self.regs["DRVCTRL"]
|
|
|
|
params = self.spi.spi_transfer([((val >> 16) | reg) & 0xff,
|
|
|
|
(val >> 8) & 0xff, val & 0xff])
|
|
|
|
pr = bytearray(params['response'])
|
|
|
|
return (pr[0] << 16) | (pr[1] << 8) | pr[2]
|
2018-09-11 19:34:05 +03:00
|
|
|
|
2018-10-24 23:01:12 +03:00
|
|
|
def get_microsteps(self):
|
2019-03-02 03:41:00 +03:00
|
|
|
return 256 >> self.fields.get_field("MRES")
|
2018-10-24 23:01:12 +03:00
|
|
|
|
|
|
|
def get_phase(self):
|
2019-03-02 03:41:00 +03:00
|
|
|
mscnt = self.fields.get_field("MSTEP", self.get_response())
|
|
|
|
return mscnt >> self.driver_mres
|
2018-10-24 23:01:12 +03:00
|
|
|
|
2018-10-24 19:17:49 +03:00
|
|
|
def handle_printing(self, print_time):
|
2019-03-02 03:41:00 +03:00
|
|
|
self.set_current(0., self.current) # workaround
|
2018-10-24 19:17:49 +03:00
|
|
|
|
|
|
|
def handle_ready(self, print_time):
|
2019-02-27 22:32:46 +03:00
|
|
|
self.set_current(print_time, (float(self.idle_current_percentage)
|
|
|
|
* self.current / 100))
|
2018-10-24 19:17:49 +03:00
|
|
|
|
|
|
|
def set_current(self, print_time, current):
|
2019-03-22 20:26:40 +03:00
|
|
|
(self.driver_cs,
|
|
|
|
self.driver_vsense) = get_config_current(current, self.sense_resistor)
|
2019-03-02 03:41:00 +03:00
|
|
|
self.fields.set_field("CS", self.driver_cs)
|
2018-11-19 02:18:25 +03:00
|
|
|
clock = self.spi.get_mcu().print_time_to_clock(print_time)
|
2019-03-02 03:41:00 +03:00
|
|
|
self.set_register("SGCSCONF", self.regs["SGCSCONF"], min_clock=clock)
|
2019-03-22 20:26:40 +03:00
|
|
|
# Only update VSENSE if we need to
|
|
|
|
if self.driver_vsense != self.fields.get_field("VSENSE"):
|
|
|
|
self.fields.set_field("VSENSE", self.driver_vsense)
|
|
|
|
self.set_register("DRVCONF", self.regs["DRVCONF"], min_clock=clock)
|
2018-09-11 19:34:05 +03:00
|
|
|
|
2019-03-02 03:41:00 +03:00
|
|
|
cmd_SET_TMC_CURRENT_help = "Set the current of a TMC2660 driver"
|
2018-09-11 19:34:05 +03:00
|
|
|
def cmd_SET_TMC_CURRENT(self, params):
|
|
|
|
gcode = self.printer.lookup_object('gcode')
|
|
|
|
if 'CURRENT' in params:
|
2019-02-27 22:32:46 +03:00
|
|
|
self.current = gcode.get_float(
|
2019-03-01 22:44:35 +03:00
|
|
|
'CURRENT', params, minval=0.1, maxval=2.4)
|
2019-02-27 22:32:46 +03:00
|
|
|
self.set_current(
|
|
|
|
self.printer.lookup_object('toolhead').get_last_move_time(),
|
|
|
|
self.current)
|
2018-09-11 19:34:05 +03:00
|
|
|
|
2018-10-24 19:23:20 +03:00
|
|
|
cmd_DUMP_TMC_help = "Read and display TMC stepper driver registers"
|
|
|
|
def cmd_DUMP_TMC(self, params):
|
|
|
|
self.printer.lookup_object('toolhead').get_last_move_time()
|
|
|
|
gcode = self.printer.lookup_object('gcode')
|
2019-03-02 03:41:00 +03:00
|
|
|
gcode.respond_info("========== Write-only registers ==========")
|
|
|
|
for reg_name in Registers:
|
|
|
|
msg = self.fields.pretty_format(reg_name, self.regs[reg_name])
|
2018-10-24 19:23:20 +03:00
|
|
|
gcode.respond_info(msg)
|
2019-03-02 03:41:00 +03:00
|
|
|
|
2018-10-24 19:23:20 +03:00
|
|
|
# Send one register to get the return data
|
2019-03-02 03:41:00 +03:00
|
|
|
gcode.respond_info("========== Queried registers ==========")
|
|
|
|
return_format = "READRSP@RDSEL" + str(self.fields.get_field("RDSEL"))
|
|
|
|
msg = self.fields.pretty_format(return_format, self.get_response())
|
2018-10-24 19:23:20 +03:00
|
|
|
gcode.respond_info(msg)
|
|
|
|
|
2019-03-05 22:04:59 +03:00
|
|
|
cmd_INIT_TMC_help = "Initialize TMC stepper driver registers"
|
|
|
|
def cmd_INIT_TMC(self, params):
|
|
|
|
logging.info("INIT_TMC 2660 %s", self.name)
|
2019-03-22 20:26:40 +03:00
|
|
|
pt = self.printer.lookup_object('toolhead').get_last_move_time()
|
|
|
|
min_clock = self.spi.get_mcu().print_time_to_clock(pt)
|
2019-03-05 22:04:59 +03:00
|
|
|
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)
|
2019-06-02 18:41:15 +03:00
|
|
|
reg = self.fields.field_to_register.get(field)
|
|
|
|
if reg is None:
|
|
|
|
raise gcode.error("Unknown field name '%s'" % field)
|
2019-03-05 22:04:59 +03:00
|
|
|
value = gcode.get_int('VALUE', params)
|
|
|
|
self.fields.set_field(field, value)
|
2019-03-22 20:26:40 +03:00
|
|
|
pt = self.printer.lookup_object('toolhead').get_last_move_time()
|
|
|
|
clock = self.spi.get_mcu().print_time_to_clock(pt)
|
2019-03-05 22:04:59 +03:00
|
|
|
self.set_register(reg, self.regs[reg], min_clock=clock)
|
|
|
|
|
2018-09-11 19:34:05 +03:00
|
|
|
def load_config_prefix(config):
|
|
|
|
return TMC2660(config)
|