tmc5160: Use TMCCommandHelper

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-06-05 13:44:16 -04:00 committed by KevinOConnor
parent 1bdd51d575
commit c76428bd01
1 changed files with 7 additions and 46 deletions

View File

@ -265,20 +265,13 @@ class TMC5160:
self.diag1_pin = config.get('diag1_pin', None) self.diag1_pin = config.get('diag1_pin', None)
ppins = self.printer.lookup_object("pins") ppins = self.printer.lookup_object("pins")
ppins.register_chip("tmc5160_" + self.name, self) ppins.register_chip("tmc5160_" + self.name, self)
# Add DUMP_TMC, INIT_TMC command # Register commands
cmdhelper = tmc2130.TMCCommandHelper(config, self.mcu_tmc)
cmdhelper.setup_register_dump(self.query_registers)
gcode = self.printer.lookup_object("gcode") gcode = self.printer.lookup_object("gcode")
gcode.register_mux_command( gcode.register_mux_command(
"SET_TMC_CURRENT", "STEPPER", self.name, "SET_TMC_CURRENT", "STEPPER", self.name,
self.cmd_SET_TMC_CURRENT, desc=self.cmd_SET_TMC_CURRENT_help) self.cmd_SET_TMC_CURRENT, desc=self.cmd_SET_TMC_CURRENT_help)
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 basic register values # Setup basic register values
irun, ihold, self.sense_resistor = get_config_current(config) irun, ihold, self.sense_resistor = get_config_current(config)
msteps, en_pwm, thresh = \ msteps, en_pwm, thresh = \
@ -328,16 +321,16 @@ class TMC5160:
# TPOWERDOWN # TPOWERDOWN
set_config_field(config, "TPOWERDOWN", 10) set_config_field(config, "TPOWERDOWN", 10)
self._init_registers() cmdhelper.init_registers()
def _init_registers(self, print_time=0.):
for reg_name, val in self.regs.items():
self.set_register(reg_name, val, print_time)
def setup_pin(self, pin_type, pin_params): def setup_pin(self, pin_type, pin_params):
if pin_type != 'endstop' or pin_params['pin'] != 'virtual_endstop': if pin_type != 'endstop' or pin_params['pin'] != 'virtual_endstop':
raise pins.error("tmc5160 virtual endstop only useful as endstop") raise pins.error("tmc5160 virtual endstop only useful as endstop")
if pin_params['invert'] or pin_params['pullup']: if pin_params['invert'] or pin_params['pullup']:
raise pins.error("Can not pullup/invert tmc5160 virtual endstop") raise pins.error("Can not pullup/invert tmc5160 virtual endstop")
return tmc2130.TMC2130VirtualEndstop(self) return tmc2130.TMC2130VirtualEndstop(self)
def query_registers(self, print_time=0.):
return [(reg_name, self.get_register(reg_name))
for reg_name in ReadRegisters]
def get_microsteps(self): def get_microsteps(self):
return 256 >> self.fields.get_field("MRES") return 256 >> self.fields.get_field("MRES")
def get_phase(self): def get_phase(self):
@ -376,38 +369,6 @@ class TMC5160:
gcode.respond_info( gcode.respond_info(
"Run Current: %0.2fA Hold Current: %0.2fA" "Run Current: %0.2fA Hold Current: %0.2fA"
% (run_current, hold_current)) % (run_current, hold_current))
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')
logging.info("DUMP_TMC %s", self.name)
gcode.respond_info("========== Write-only registers ==========")
for reg_name, val in self.regs.items():
if reg_name not in ReadRegisters:
gcode.respond_info(self.fields.pretty_format(reg_name, val))
gcode.respond_info("========== Queried registers ==========")
for reg_name in ReadRegisters:
val = self.get_register(reg_name)
gcode.respond_info(self.fields.pretty_format(reg_name, val))
cmd_INIT_TMC_help = "Initialize TMC stepper driver registers"
def cmd_INIT_TMC(self, params):
logging.info("INIT_TMC 5160 %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 TMC5160 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)
reg = self.fields.field_to_register.get(field)
if reg is None:
raise gcode.error("Unknown field name '%s'" % field)
value = gcode.get_int('VALUE', params)
self.fields.set_field(field, value)
print_time = self.printer.lookup_object('toolhead').get_last_move_time()
self.set_register(reg, self.regs[reg], print_time)
def load_config_prefix(config): def load_config_prefix(config):
return TMC5160(config) return TMC5160(config)