tmc2130: Rework remaining config helpers to use mcu_tmc interface
Introduce TMCMicrostepHelper and TMCStealthchopHelper helper code that uses the mcu_tmc interface. Update the callers to use these new helpers. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
7f34264101
commit
10c0da9a6c
|
@ -360,22 +360,45 @@ class TMCCurrentHelper:
|
|||
# Config reading helpers
|
||||
######################################################################
|
||||
|
||||
def get_config_microsteps(config):
|
||||
steps = {'256': 0, '128': 1, '64': 2, '32': 3, '16': 4,
|
||||
'8': 5, '4': 6, '2': 7, '1': 8}
|
||||
return config.getchoice('microsteps', steps)
|
||||
# Helper to configure and query the microstep settings
|
||||
class TMCMicrostepHelper:
|
||||
def __init__(self, config, mcu_tmc):
|
||||
self.mcu_tmc = mcu_tmc
|
||||
self.fields = mcu_tmc.get_fields()
|
||||
steps = {'256': 0, '128': 1, '64': 2, '32': 3, '16': 4,
|
||||
'8': 5, '4': 6, '2': 7, '1': 8}
|
||||
mres = config.getchoice('microsteps', steps)
|
||||
self.fields.set_field("MRES", mres)
|
||||
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 mscnt >> self.fields.get_field("MRES")
|
||||
|
||||
def get_config_stealthchop(config, tmc_freq):
|
||||
mres = get_config_microsteps(config)
|
||||
# Helper to configure "stealthchop" mode
|
||||
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 not velocity:
|
||||
return mres, False, 0
|
||||
stepper_name = " ".join(config.get_name().split()[1:])
|
||||
stepper_config = config.getsection(stepper_name)
|
||||
step_dist = stepper_config.getfloat('step_distance')
|
||||
step_dist_256 = step_dist / (1 << mres)
|
||||
threshold = int(tmc_freq * step_dist_256 / velocity + .5)
|
||||
return mres, True, max(0, min(0xfffff, threshold))
|
||||
if velocity:
|
||||
stepper_name = " ".join(config.get_name().split()[1:])
|
||||
stepper_config = config.getsection(stepper_name)
|
||||
step_dist = stepper_config.getfloat('step_distance')
|
||||
step_dist_256 = step_dist / (1 << fields.get_field("MRES"))
|
||||
threshold = int(tmc_freq * step_dist_256 / velocity + .5)
|
||||
fields.set_field("TPWMTHRS", max(0, min(0xfffff, threshold)))
|
||||
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
|
||||
fields.set_field("en_spreadCycle", not en_pwm_mode)
|
||||
|
||||
|
||||
######################################################################
|
||||
|
@ -413,13 +436,9 @@ class MCU_TMC_SPI:
|
|||
|
||||
class TMC2130:
|
||||
def __init__(self, config):
|
||||
self.printer = config.get_printer()
|
||||
self.name = config.get_name().split()[-1]
|
||||
# Setup mcu communication
|
||||
self.fields = FieldHelper(Fields, SignedFields, FieldFormatters)
|
||||
self.mcu_tmc = MCU_TMC_SPI(config, Registers, self.fields)
|
||||
self.get_register = self.mcu_tmc.get_register
|
||||
self.set_register = self.mcu_tmc.set_register
|
||||
# Allow virtual endstop to be created
|
||||
diag1_pin = config.get('diag1_pin', None)
|
||||
TMCEndstopHelper(config, self.mcu_tmc, diag1_pin)
|
||||
|
@ -428,10 +447,10 @@ class TMC2130:
|
|||
cmdhelper.setup_register_dump(self.query_registers)
|
||||
# Setup basic register values
|
||||
TMCCurrentHelper(config, self.mcu_tmc)
|
||||
mres, en_pwm, thresh = get_config_stealthchop(config, TMC_FREQUENCY)
|
||||
self.fields.set_field("MRES", mres)
|
||||
self.fields.set_field("en_pwm_mode", en_pwm)
|
||||
self.fields.set_field("TPWMTHRS", thresh)
|
||||
mh = TMCMicrostepHelper(config, self.mcu_tmc)
|
||||
self.get_microsteps = mh.get_microsteps
|
||||
self.get_phase = mh.get_phase
|
||||
TMCStealthchopHelper(config, self.mcu_tmc, TMC_FREQUENCY)
|
||||
# Allow other registers to be set from the config
|
||||
set_config_field = self.fields.set_config_field
|
||||
set_config_field(config, "toff", 4)
|
||||
|
@ -447,13 +466,8 @@ class TMC2130:
|
|||
set_config_field(config, "pwm_autoscale", True)
|
||||
set_config_field(config, "sgt", 0)
|
||||
def query_registers(self, print_time=0.):
|
||||
return [(reg_name, self.get_register(reg_name))
|
||||
return [(reg_name, self.mcu_tmc.get_register(reg_name))
|
||||
for reg_name in ReadRegisters]
|
||||
def get_microsteps(self):
|
||||
return 256 >> self.fields.get_field("MRES")
|
||||
def get_phase(self):
|
||||
mscnt = self.fields.get_field("MSCNT", self.get_register("MSCNT"))
|
||||
return mscnt >> self.fields.get_field("MRES")
|
||||
|
||||
def load_config_prefix(config):
|
||||
return TMC2130(config)
|
||||
|
|
|
@ -307,13 +307,9 @@ class MCU_TMC_uart:
|
|||
|
||||
class TMC2208:
|
||||
def __init__(self, config):
|
||||
self.printer = config.get_printer()
|
||||
self.name = config.get_name().split()[-1]
|
||||
# Setup mcu communication
|
||||
self.fields = tmc2130.FieldHelper(Fields, SignedFields, FieldFormatters)
|
||||
self.mcu_tmc = MCU_TMC_uart(config, Registers, self.fields)
|
||||
self.get_register = self.mcu_tmc.get_register
|
||||
self.set_register = self.mcu_tmc.set_register
|
||||
# Register commands
|
||||
cmdhelper = tmc2130.TMCCommandHelper(config, self.mcu_tmc)
|
||||
cmdhelper.setup_register_dump(self.query_registers)
|
||||
|
@ -322,10 +318,10 @@ class TMC2208:
|
|||
self.fields.set_field("mstep_reg_select", True)
|
||||
self.fields.set_field("multistep_filt", True)
|
||||
tmc2130.TMCCurrentHelper(config, self.mcu_tmc)
|
||||
mres, en, thresh = tmc2130.get_config_stealthchop(config, TMC_FREQUENCY)
|
||||
self.fields.set_field("MRES", mres)
|
||||
self.fields.set_field("en_spreadCycle", not en)
|
||||
self.fields.set_field("TPWMTHRS", thresh)
|
||||
mh = tmc2130.TMCMicrostepHelper(config, self.mcu_tmc)
|
||||
self.get_microsteps = mh.get_microsteps
|
||||
self.get_phase = mh.get_phase
|
||||
tmc2130.TMCStealthchopHelper(config, self.mcu_tmc, TMC_FREQUENCY)
|
||||
# Allow other registers to be set from the config
|
||||
set_config_field = self.fields.set_config_field
|
||||
set_config_field(config, "toff", 3)
|
||||
|
@ -345,7 +341,7 @@ class TMC2208:
|
|||
def query_registers(self, print_time=0.):
|
||||
out = []
|
||||
for reg_name in ReadRegisters:
|
||||
val = self.get_register(reg_name)
|
||||
val = self.mcu_tmc.get_register(reg_name)
|
||||
# IOIN has different mappings depending on the driver type
|
||||
# (SEL_A field of IOIN reg)
|
||||
if reg_name == "IOIN":
|
||||
|
@ -353,11 +349,6 @@ class TMC2208:
|
|||
reg_name = "IOIN@TMC220x" if drv_type else "IOIN@TMC222x"
|
||||
out.append((reg_name, val))
|
||||
return out
|
||||
def get_microsteps(self):
|
||||
return 256 >> self.fields.get_field("MRES")
|
||||
def get_phase(self):
|
||||
mscnt = self.fields.get_field("MSCNT", self.get_register("MSCNT"))
|
||||
return mscnt >> self.fields.get_field("MRES")
|
||||
|
||||
def load_config_prefix(config):
|
||||
return TMC2208(config)
|
||||
|
|
|
@ -230,20 +230,17 @@ class MCU_TMC2660_SPI:
|
|||
|
||||
class TMC2660:
|
||||
def __init__(self, config):
|
||||
self.printer = config.get_printer()
|
||||
self.name = config.get_name().split()[1]
|
||||
# Setup mcu communication
|
||||
self.fields = tmc2130.FieldHelper(Fields, SignedFields, FieldFormatters)
|
||||
self.mcu_tmc = MCU_TMC2660_SPI(config, Registers, self.fields)
|
||||
self.get_register = self.mcu_tmc.get_register
|
||||
self.set_register = self.mcu_tmc.set_register
|
||||
# Register commands
|
||||
cmdhelper = tmc2130.TMCCommandHelper(config, self.mcu_tmc)
|
||||
cmdhelper.setup_register_dump(self.query_registers)
|
||||
|
||||
# DRVCTRL
|
||||
mres = tmc2130.get_config_microsteps(config)
|
||||
self.fields.set_field("MRES", mres)
|
||||
mh = tmc2130.TMCMicrostepHelper(config, self.mcu_tmc)
|
||||
self.get_microsteps = mh.get_microsteps
|
||||
self.get_phase = mh.get_phase
|
||||
set_config_field = self.fields.set_config_field
|
||||
set_config_field(config, "DEDGE", 0)
|
||||
set_config_field(config, "INTPOL", True, 'interpolate')
|
||||
|
@ -280,16 +277,8 @@ class TMC2660:
|
|||
self.fields.set_field("SDOFF", 0) # only step/dir mode supported
|
||||
|
||||
def query_registers(self, print_time=0.):
|
||||
return [(reg_name, self.get_register(reg_name))
|
||||
return [(reg_name, self.mcu_tmc.get_register(reg_name))
|
||||
for reg_name in ReadRegisters]
|
||||
|
||||
def get_microsteps(self):
|
||||
return 256 >> self.fields.get_field("MRES")
|
||||
|
||||
def get_phase(self):
|
||||
reg = self.get_register("READRSP@RDSEL0")
|
||||
mscnt = self.fields.get_field("MSTEP", reg)
|
||||
return mscnt >> self.fields.get_field("MRES")
|
||||
|
||||
def load_config_prefix(config):
|
||||
return TMC2660(config)
|
||||
|
|
|
@ -99,7 +99,7 @@ Fields["CHOPCONF"] = {
|
|||
"vhighfs": 0x01 << 18,
|
||||
"vhighchm": 0x01 << 19,
|
||||
"tpfd": 0x0F << 20, # midrange resonances
|
||||
"mres": 0x0F << 24,
|
||||
"MRES": 0x0F << 24,
|
||||
"intpol": 0x01 << 28,
|
||||
"dedge": 0x01 << 29,
|
||||
"diss2g": 0x01 << 30,
|
||||
|
@ -288,13 +288,9 @@ class TMC5160CurrentHelper:
|
|||
|
||||
class TMC5160:
|
||||
def __init__(self, config):
|
||||
self.printer = config.get_printer()
|
||||
self.name = config.get_name().split()[-1]
|
||||
# Setup mcu communication
|
||||
self.fields = tmc2130.FieldHelper(Fields, SignedFields, FieldFormatters)
|
||||
self.mcu_tmc = tmc2130.MCU_TMC_SPI(config, Registers, self.fields)
|
||||
self.get_register = self.mcu_tmc.get_register
|
||||
self.set_register = self.mcu_tmc.set_register
|
||||
# Allow virtual endstop to be created
|
||||
diag1_pin = config.get('diag1_pin', None)
|
||||
tmc2130.TMCEndstopHelper(config, self.mcu_tmc, diag1_pin)
|
||||
|
@ -302,10 +298,12 @@ class TMC5160:
|
|||
cmdhelper = tmc2130.TMCCommandHelper(config, self.mcu_tmc)
|
||||
cmdhelper.setup_register_dump(self.query_registers)
|
||||
# Setup basic register values
|
||||
msteps, en_pwm, thresh = \
|
||||
tmc2130.get_config_stealthchop(config, TMC_FREQUENCY)
|
||||
set_config_field = self.fields.set_config_field
|
||||
mh = tmc2130.TMCMicrostepHelper(config, self.mcu_tmc)
|
||||
self.get_microsteps = mh.get_microsteps
|
||||
self.get_phase = mh.get_phase
|
||||
tmc2130.TMCStealthchopHelper(config, self.mcu_tmc, TMC_FREQUENCY)
|
||||
# CHOPCONF
|
||||
set_config_field = self.fields.set_config_field
|
||||
set_config_field(config, "toff", 3)
|
||||
set_config_field(config, "hstrt", 5)
|
||||
set_config_field(config, "hend", 2)
|
||||
|
@ -316,7 +314,6 @@ class TMC5160:
|
|||
set_config_field(config, "vhighfs", 0)
|
||||
set_config_field(config, "vhighchm", 0)
|
||||
set_config_field(config, "tpfd", 4)
|
||||
self.fields.set_field("mres", msteps) # microsteps
|
||||
set_config_field(config, "intpol", True, "interpolate")
|
||||
set_config_field(config, "dedge", 0)
|
||||
set_config_field(config, "diss2g", 0)
|
||||
|
@ -329,8 +326,6 @@ class TMC5160:
|
|||
set_config_field(config, "seimin", 0)
|
||||
set_config_field(config, "sgt", 0)
|
||||
set_config_field(config, "sfilt", 0)
|
||||
# GCONF
|
||||
self.fields.set_field("en_pwm_mode", en_pwm)
|
||||
# IHOLDIRUN
|
||||
TMC5160CurrentHelper(config, self.mcu_tmc)
|
||||
set_config_field(config, "IHOLDDELAY", 6)
|
||||
|
@ -343,18 +338,11 @@ class TMC5160:
|
|||
set_config_field(config, "freewheel", 0)
|
||||
set_config_field(config, "PWM_REG", 4)
|
||||
set_config_field(config, "PWM_LIM", 12)
|
||||
# TPWMTHRS
|
||||
self.fields.set_field("TPWMTHRS", thresh)
|
||||
# TPOWERDOWN
|
||||
set_config_field(config, "TPOWERDOWN", 10)
|
||||
def query_registers(self, print_time=0.):
|
||||
return [(reg_name, self.get_register(reg_name))
|
||||
return [(reg_name, self.mcu_tmc.get_register(reg_name))
|
||||
for reg_name in ReadRegisters]
|
||||
def get_microsteps(self):
|
||||
return 256 >> self.fields.get_field("MRES")
|
||||
def get_phase(self):
|
||||
mscnt = self.fields.get_field("MSCNT", self.get_register("MSCNT"))
|
||||
return mscnt >> self.fields.get_field("MRES")
|
||||
|
||||
def load_config_prefix(config):
|
||||
return TMC5160(config)
|
||||
|
|
Loading…
Reference in New Issue