From b41770caa657bc8a3eb1e41331aa3603c97dcaca Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sat, 30 Jun 2018 12:17:32 -0400 Subject: [PATCH] tmc2130: Add a DUMP_TMC helper command Signed-off-by: Kevin O'Connor --- docs/G-Codes.md | 7 +++++++ klippy/extras/tmc2130.py | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/docs/G-Codes.md b/docs/G-Codes.md index 844e62de..0a5fa7e0 100644 --- a/docs/G-Codes.md +++ b/docs/G-Codes.md @@ -158,3 +158,10 @@ section is enabled: - `SET_DUAL_CARRIAGE CARRIAGE=[0|1]`: This command will set the active carriage. It is typically invoked from the activate_gcode and deactivate_gcode fields in a multiple extruder configuration. + +## TMC2130 + +The following command is available when the "tmc2130" config section +is enabled: +- `DUMP_TMC STEPPER=`: This command will read the TMC2130 driver + registers and report their values. diff --git a/klippy/extras/tmc2130.py b/klippy/extras/tmc2130.py index 236050af..001e6229 100644 --- a/klippy/extras/tmc2130.py +++ b/klippy/extras/tmc2130.py @@ -3,7 +3,7 @@ # Copyright (C) 2018 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. -import math +import math, logging TMC_FREQUENCY=13200000. REG_GCONF=0x00 @@ -13,9 +13,16 @@ REG_TCOOLTHRS=0x14 REG_COOLCONF=0x6d REG_PWMCONF=0x70 +ReadRegisters = { + 0x00: "GCONF", 0x01: "GSTAT", 0x04: "IOIN", 0x12: "TSTEP", 0x2d: "XDIRECT", + 0x6a: "MSCNT", 0x6b: "MSCURACT", 0x6c: "CHOPCONF", 0x6f: "DRV_STATUS", + 0x71: "PWM_SCALE", 0x73: "LOST_STEPS" +} + class TMC2130: def __init__(self, config): self.printer = config.get_printer() + self.name = config.get_name().split()[1] # pin setup ppins = self.printer.lookup_object("pins") cs_pin = config.get('cs_pin') @@ -25,6 +32,12 @@ class TMC2130: self.mcu = cs_pin_params['chip'] pin = cs_pin_params['pin'] self.oid = self.mcu.create_oid() + # Add DUMP_TMC command + 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) + # Setup driver registers self.mcu.add_config_cmd( "config_spi oid=%d bus=%d pin=%s mode=%d rate=%d shutdown_msg=" % ( self.oid, 0, cs_pin_params['pin'], 3, 4000000)) @@ -52,8 +65,8 @@ class TMC2130: pwm_ampl = config.getint('driver_PWM_AMPL', 128, minval=0, maxval=255) # Allow virtual endstop to be created self.diag1_pin = config.get('diag1_pin', None) - ppins.register_chip("_".join(config.get_name().split()[:2]), self) - self.send_spi_cmd = None + ppins.register_chip("tmc2130_" + self.name, self) + self.spi_send_cmd = self.spi_transfer_cmd = None self.mcu.add_config_object(self) # calculate current vsense = False @@ -111,10 +124,27 @@ class TMC2130: cmd_queue = self.mcu.alloc_command_queue() self.spi_send_cmd = self.mcu.lookup_command( "spi_send oid=%c data=%*s", cq=cmd_queue) + self.spi_transfer_cmd = self.mcu.lookup_command( + "spi_transfer oid=%c data=%*s", cq=cmd_queue) def set_register(self, addr, val): data = [(addr | 0x80) & 0xff, (val >> 24) & 0xff, (val >> 16) & 0xff, (val >> 8) & 0xff, val & 0xff] self.spi_send_cmd.send([self.oid, data]) + cmd_DUMP_TMC_help = "Read and display TMC2130 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_TMC2130 %s", self.name) + for reg, name in sorted(ReadRegisters.items()): + self.spi_send_cmd.send([self.oid, [reg, 0x00, 0x00, 0x00, 0x00]]) + params = self.spi_transfer_cmd.send_with_response( + [self.oid, [reg, 0x00, 0x00, 0x00, 0x00]], + 'spi_transfer_response', self.oid) + pr = bytearray(params['response']) + val = (pr[1] << 24) | (pr[2] << 16) | (pr[3] << 8) | pr[4] + msg = "%15s: %8x" % (name, val) + logging.info(msg) + gcode.respond_info(msg) # Endstop wrapper that enables tmc2130 "sensorless homing" class TMC2130VirtualEndstop: