tmc2130: Add support for INIT_TMC command

Signed-off-by: Frank Kang <amblidex@outlook.com>
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-03-01 00:23:19 -05:00
parent 8446f28850
commit c039b33f2a
3 changed files with 34 additions and 11 deletions

View File

@ -287,12 +287,15 @@ section is enabled:
carriage. It is typically invoked from the activate_gcode and carriage. It is typically invoked from the activate_gcode and
deactivate_gcode fields in a multiple extruder configuration. deactivate_gcode fields in a multiple extruder configuration.
## TMC2130 ## TMC2130 and TMC2208
The following command is available when the "tmc2130" config section The following command is available when the "tmc2130" or "tmc2208"
is enabled: config section is enabled:
- `DUMP_TMC STEPPER=<name>`: This command will read the TMC2130 driver - `DUMP_TMC STEPPER=<name>`: This command will read the TMC driver
registers and report their values. registers and report their values.
- `INIT_TMC STEPPER=<name>`: This command will intitialize the TMC
registers. Needed to re-enable the driver if power to the chip is
turned off then back on.
## Endstop adjustments by stepper phase ## Endstop adjustments by stepper phase

View File

@ -211,11 +211,14 @@ class TMC2130:
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("tmc2130_" + self.name, self) ppins.register_chip("tmc2130_" + self.name, self)
# Add DUMP_TMC command # Add DUMP_TMC, INIT_TMC command
gcode = self.printer.lookup_object("gcode") gcode = self.printer.lookup_object("gcode")
gcode.register_mux_command( gcode.register_mux_command(
"DUMP_TMC", "STEPPER", self.name, "DUMP_TMC", "STEPPER", self.name,
self.cmd_DUMP_TMC, desc=self.cmd_DUMP_TMC_help) self.cmd_DUMP_TMC, desc=self.cmd_DUMP_TMC_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
self.regs = collections.OrderedDict() self.regs = collections.OrderedDict()
self.fields = FieldHelper(Fields, FieldFormatters, self.regs) self.fields = FieldHelper(Fields, FieldFormatters, self.regs)
@ -242,9 +245,11 @@ class TMC2130:
set_config_field(config, "pwm_autoscale", True) set_config_field(config, "pwm_autoscale", True)
sgt = config.getint('driver_SGT', 0, minval=-64, maxval=63) & 0x7f sgt = config.getint('driver_SGT', 0, minval=-64, maxval=63) & 0x7f
self.fields.set_field("sgt", sgt) self.fields.set_field("sgt", sgt)
self._init_registers()
def _init_registers(self, min_clock = 0):
# Send registers # Send registers
for reg_name, val in self.regs.items(): for reg_name, val in self.regs.items():
self.set_register(reg_name, val) self.set_register(reg_name, val, min_clock)
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("tmc2130 virtual endstop only useful as endstop") raise pins.error("tmc2130 virtual endstop only useful as endstop")
@ -257,11 +262,11 @@ class TMC2130:
params = self.spi.spi_transfer([reg, 0x00, 0x00, 0x00, 0x00]) params = self.spi.spi_transfer([reg, 0x00, 0x00, 0x00, 0x00])
pr = bytearray(params['response']) pr = bytearray(params['response'])
return (pr[1] << 24) | (pr[2] << 16) | (pr[3] << 8) | pr[4] return (pr[1] << 24) | (pr[2] << 16) | (pr[3] << 8) | pr[4]
def set_register(self, reg_name, val): def set_register(self, reg_name, val, min_clock = 0):
reg = Registers[reg_name] reg = Registers[reg_name]
data = [(reg | 0x80) & 0xff, (val >> 24) & 0xff, (val >> 16) & 0xff, data = [(reg | 0x80) & 0xff, (val >> 24) & 0xff, (val >> 16) & 0xff,
(val >> 8) & 0xff, val & 0xff] (val >> 8) & 0xff, val & 0xff]
self.spi.spi_send(data) self.spi.spi_send(data, min_clock)
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):
@ -284,6 +289,12 @@ class TMC2130:
msg = self.fields.pretty_format(reg_name, val) msg = self.fields.pretty_format(reg_name, val)
logging.info(msg) logging.info(msg)
gcode.respond_info(msg) gcode.respond_info(msg)
cmd_INIT_TMC_help = "Initialize TMC stepper driver registers"
def cmd_INIT_TMC(self, params):
logging.info("INIT_TMC 2130 %s", self.name)
print_time = self.printer.lookup_object('toolhead').get_last_move_time();
min_clock = self.spi.get_mcu().print_time_to_clock(print_time)
self._init_registers(min_clock)
# Endstop wrapper that enables tmc2130 "sensorless homing" # Endstop wrapper that enables tmc2130 "sensorless homing"
class TMC2130VirtualEndstop: class TMC2130VirtualEndstop:

View File

@ -248,7 +248,7 @@ class TMC2208:
self.printer = config.get_printer() self.printer = config.get_printer()
self.name = config.get_name().split()[-1] self.name = config.get_name().split()[-1]
self.printer.register_event_handler("klippy:connect", self.printer.register_event_handler("klippy:connect",
self.handle_connect) self._init_registers)
# pin setup # pin setup
ppins = self.printer.lookup_object("pins") ppins = self.printer.lookup_object("pins")
rx_pin_params = ppins.lookup_pin( rx_pin_params = ppins.lookup_pin(
@ -267,11 +267,14 @@ class TMC2208:
self.oid = self.mcu.create_oid() self.oid = self.mcu.create_oid()
self.tmcuart_send_cmd = None self.tmcuart_send_cmd = None
self.mcu.register_config_callback(self.build_config) self.mcu.register_config_callback(self.build_config)
# Add DUMP_TMC command # Add DUMP_TMC, INIT_TMC command
gcode = self.printer.lookup_object("gcode") gcode = self.printer.lookup_object("gcode")
gcode.register_mux_command( gcode.register_mux_command(
"DUMP_TMC", "STEPPER", self.name, "DUMP_TMC", "STEPPER", self.name,
self.cmd_DUMP_TMC, desc=self.cmd_DUMP_TMC_help) self.cmd_DUMP_TMC, desc=self.cmd_DUMP_TMC_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
self.ifcnt = None self.ifcnt = None
self.regs = collections.OrderedDict() self.regs = collections.OrderedDict()
@ -311,7 +314,8 @@ class TMC2208:
cmd_queue = self.mcu.alloc_command_queue() cmd_queue = self.mcu.alloc_command_queue()
self.tmcuart_send_cmd = self.mcu.lookup_command( self.tmcuart_send_cmd = self.mcu.lookup_command(
"tmcuart_send oid=%c write=%*s read=%c", cq=cmd_queue) "tmcuart_send oid=%c write=%*s read=%c", cq=cmd_queue)
def handle_connect(self): def _init_registers(self):
# Send registers
for reg_name, val in self.regs.items(): for reg_name, val in self.regs.items():
self.set_register(reg_name, val) self.set_register(reg_name, val)
def get_register(self, reg_name): def get_register(self, reg_name):
@ -372,6 +376,11 @@ class TMC2208:
msg = self.fields.pretty_format(reg_name, val) msg = self.fields.pretty_format(reg_name, val)
logging.info(msg) logging.info(msg)
gcode.respond_info(msg) gcode.respond_info(msg)
cmd_INIT_TMC_help = "Initialize TMC stepper driver registers"
def cmd_INIT_TMC(self, params):
logging.info("INIT_TMC 2208 %s", self.name)
self.printer.lookup_object('toolhead').wait_moves()
self._init_registers()
def load_config_prefix(config): def load_config_prefix(config):
return TMC2208(config) return TMC2208(config)