2018-02-18 05:34:41 +01:00
|
|
|
# TMC2130 configuration
|
|
|
|
#
|
|
|
|
# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2018-06-30 18:17:32 +02:00
|
|
|
import math, logging
|
2018-02-18 05:34:41 +01:00
|
|
|
|
2018-05-23 03:40:32 +02:00
|
|
|
TMC_FREQUENCY=13200000.
|
2018-05-23 05:39:06 +02:00
|
|
|
GCONF_EN_PWM_MODE=1<<2
|
|
|
|
GCONF_DIAG1_STALL=1<<8
|
2018-05-23 03:40:32 +02:00
|
|
|
|
2018-08-24 16:23:39 +02:00
|
|
|
Registers = {
|
|
|
|
"GCONF": 0x00, "GSTAT": 0x01, "IOIN": 0x04, "IHOLD_IRUN": 0x10,
|
|
|
|
"TPOWERDOWN": 0x11, "TSTEP": 0x12, "TPWMTHRS": 0x13, "TCOOLTHRS": 0x14,
|
|
|
|
"THIGH": 0x15, "XDIRECT": 0x2d, "MSLUT0": 0x60, "MSLUTSEL": 0x68,
|
|
|
|
"MSLUTSTART": 0x69, "MSCNT": 0x6a, "MSCURACT": 0x6b, "CHOPCONF": 0x6c,
|
|
|
|
"COOLCONF": 0x6d, "DCCTRL": 0x6e, "DRV_STATUS": 0x6f, "PWMCONF": 0x70,
|
|
|
|
"PWM_SCALE": 0x71, "ENCM_CTRL": 0x72, "LOST_STEPS": 0x73,
|
2018-06-30 18:17:32 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:23:39 +02:00
|
|
|
ReadRegisters = [
|
|
|
|
"GCONF", "GSTAT", "IOIN", "TSTEP", "XDIRECT", "MSCNT", "MSCURACT",
|
|
|
|
"CHOPCONF", "DRV_STATUS", "PWM_SCALE", "LOST_STEPS",
|
|
|
|
]
|
|
|
|
|
2018-05-23 05:39:06 +02:00
|
|
|
class TMC2130:
|
2018-02-18 05:34:41 +01:00
|
|
|
def __init__(self, config):
|
2018-05-23 05:39:06 +02:00
|
|
|
self.printer = config.get_printer()
|
2018-06-30 18:17:32 +02:00
|
|
|
self.name = config.get_name().split()[1]
|
2018-02-18 05:34:41 +01:00
|
|
|
# pin setup
|
2018-05-23 05:39:06 +02:00
|
|
|
ppins = self.printer.lookup_object("pins")
|
2018-02-18 05:34:41 +01:00
|
|
|
cs_pin = config.get('cs_pin')
|
2018-07-26 15:44:45 +02:00
|
|
|
cs_pin_params = ppins.lookup_pin(cs_pin)
|
2018-02-18 05:34:41 +01:00
|
|
|
self.mcu = cs_pin_params['chip']
|
|
|
|
pin = cs_pin_params['pin']
|
|
|
|
self.oid = self.mcu.create_oid()
|
2018-08-24 16:23:39 +02:00
|
|
|
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))
|
|
|
|
self.spi_send_cmd = self.spi_transfer_cmd = None
|
2018-09-03 17:48:22 +02:00
|
|
|
self.mcu.register_config_callback(self.build_config)
|
2018-08-24 16:23:39 +02:00
|
|
|
# Allow virtual endstop to be created
|
|
|
|
self.diag1_pin = config.get('diag1_pin', None)
|
|
|
|
ppins.register_chip("tmc2130_" + self.name, self)
|
2018-06-30 18:17:32 +02:00
|
|
|
# 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)
|
2018-08-24 16:23:39 +02:00
|
|
|
# Get config for initial driver settings
|
2018-02-18 05:34:41 +01:00
|
|
|
run_current = config.getfloat('run_current', above=0., maxval=2.)
|
|
|
|
hold_current = config.getfloat('hold_current', run_current,
|
|
|
|
above=0., maxval=2.)
|
|
|
|
sense_resistor = config.getfloat('sense_resistor', 0.110, above=0.)
|
|
|
|
steps = {'256': 0, '128': 1, '64': 2, '32': 3, '16': 4,
|
|
|
|
'8': 5, '4': 6, '2': 7, '1': 8}
|
2018-05-23 03:40:32 +02:00
|
|
|
self.mres = config.getchoice('microsteps', steps)
|
2018-02-18 05:34:41 +01:00
|
|
|
interpolate = config.getboolean('interpolate', True)
|
2018-05-23 03:40:32 +02:00
|
|
|
sc_velocity = config.getfloat('stealthchop_threshold', 0., minval=0.)
|
|
|
|
sc_threshold = self.velocity_to_clock(config, sc_velocity)
|
2018-02-18 05:34:41 +01:00
|
|
|
iholddelay = config.getint('driver_IHOLDDELAY', 8, minval=0, maxval=15)
|
|
|
|
tpowerdown = config.getint('driver_TPOWERDOWN', 0, minval=0, maxval=255)
|
|
|
|
blank_time_select = config.getint('driver_BLANK_TIME_SELECT', 1,
|
|
|
|
minval=0, maxval=3)
|
2018-05-24 16:44:45 +02:00
|
|
|
toff = config.getint('driver_TOFF', 4, minval=1, maxval=15)
|
2018-02-18 05:34:41 +01:00
|
|
|
hend = config.getint('driver_HEND', 7, minval=0, maxval=15)
|
|
|
|
hstrt = config.getint('driver_HSTRT', 0, minval=0, maxval=7)
|
2018-05-23 05:39:06 +02:00
|
|
|
sgt = config.getint('driver_SGT', 0, minval=-64, maxval=63) & 0x7f
|
2018-05-24 17:03:29 +02:00
|
|
|
pwm_scale = config.getboolean('driver_PWM_AUTOSCALE', True)
|
|
|
|
pwm_freq = config.getint('driver_PWM_FREQ', 1, minval=0, maxval=3)
|
|
|
|
pwm_grad = config.getint('driver_PWM_GRAD', 4, minval=0, maxval=255)
|
|
|
|
pwm_ampl = config.getint('driver_PWM_AMPL', 128, minval=0, maxval=255)
|
2018-02-18 05:34:41 +01:00
|
|
|
# calculate current
|
|
|
|
vsense = False
|
|
|
|
irun = self.current_bits(run_current, sense_resistor, vsense)
|
|
|
|
ihold = self.current_bits(hold_current, sense_resistor, vsense)
|
|
|
|
if irun < 16 and ihold < 16:
|
|
|
|
vsense = True
|
|
|
|
irun = self.current_bits(run_current, sense_resistor, vsense)
|
|
|
|
ihold = self.current_bits(hold_current, sense_resistor, vsense)
|
2018-08-24 16:23:39 +02:00
|
|
|
# Configure registers
|
2018-05-23 05:39:06 +02:00
|
|
|
self.reg_GCONF = (sc_velocity > 0.) << 2
|
2018-08-24 16:23:39 +02:00
|
|
|
self.set_register("GCONF", self.reg_GCONF)
|
|
|
|
self.set_register("CHOPCONF", (
|
|
|
|
toff | (hstrt << 4) | (hend << 7) | (blank_time_select << 15)
|
|
|
|
| (vsense << 17) | (self.mres << 24) | (interpolate << 28)))
|
|
|
|
self.set_register("IHOLD_IRUN",
|
|
|
|
ihold | (irun << 8) | (iholddelay << 16))
|
|
|
|
self.set_register("TPOWERDOWN", tpowerdown)
|
|
|
|
self.set_register("TPWMTHRS", max(0, min(0xfffff, sc_threshold)))
|
|
|
|
self.set_register("COOLCONF", sgt << 16)
|
|
|
|
self.set_register("PWMCONF", (
|
|
|
|
pwm_ampl | (pwm_grad << 8) | (pwm_freq << 16) | (pwm_scale << 18)))
|
2018-02-18 05:34:41 +01:00
|
|
|
def current_bits(self, current, sense_resistor, vsense_on):
|
|
|
|
sense_resistor += 0.020
|
|
|
|
vsense = 0.32
|
|
|
|
if vsense_on:
|
|
|
|
vsense = 0.18
|
|
|
|
cs = int(32. * current * sense_resistor * math.sqrt(2.) / vsense
|
|
|
|
- 1. + .5)
|
|
|
|
return max(0, min(31, cs))
|
2018-05-23 03:40:32 +02:00
|
|
|
def velocity_to_clock(self, config, velocity):
|
|
|
|
if not velocity:
|
|
|
|
return 0
|
|
|
|
stepper_name = config.get_name().split()[1]
|
|
|
|
stepper_config = config.getsection(stepper_name)
|
|
|
|
step_dist = stepper_config.getfloat('step_distance')
|
|
|
|
step_dist_256 = step_dist / (1 << self.mres)
|
|
|
|
return int(TMC_FREQUENCY * step_dist_256 / velocity + .5)
|
2018-07-26 15:44:45 +02:00
|
|
|
def setup_pin(self, pin_type, pin_params):
|
|
|
|
if pin_type != 'endstop' or pin_params['pin'] != 'virtual_endstop':
|
2018-05-23 05:39:06 +02:00
|
|
|
raise pins.error("tmc2130 virtual endstop only useful as endstop")
|
|
|
|
if pin_params['invert'] or pin_params['pullup']:
|
|
|
|
raise pins.error("Can not pullup/invert tmc2130 virtual endstop")
|
|
|
|
return TMC2130VirtualEndstop(self)
|
|
|
|
def build_config(self):
|
|
|
|
cmd_queue = self.mcu.alloc_command_queue()
|
|
|
|
self.spi_send_cmd = self.mcu.lookup_command(
|
|
|
|
"spi_send oid=%c data=%*s", cq=cmd_queue)
|
2018-06-30 18:17:32 +02:00
|
|
|
self.spi_transfer_cmd = self.mcu.lookup_command(
|
|
|
|
"spi_transfer oid=%c data=%*s", cq=cmd_queue)
|
2018-08-24 16:23:39 +02:00
|
|
|
def get_register(self, reg_name):
|
|
|
|
reg = Registers[reg_name]
|
|
|
|
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'])
|
|
|
|
return (pr[1] << 24) | (pr[2] << 16) | (pr[3] << 8) | pr[4]
|
|
|
|
def set_register(self, reg_name, val):
|
|
|
|
reg = Registers[reg_name]
|
|
|
|
if self.spi_send_cmd is None:
|
|
|
|
# Setup register via chip initialization
|
|
|
|
self.mcu.add_config_cmd("spi_send oid=%d data=%02x%08x" % (
|
|
|
|
self.oid, (reg | 0x80) & 0xff, val & 0xffffffff), is_init=True)
|
|
|
|
return
|
|
|
|
data = [(reg | 0x80) & 0xff, (val >> 24) & 0xff, (val >> 16) & 0xff,
|
2018-05-23 05:39:06 +02:00
|
|
|
(val >> 8) & 0xff, val & 0xff]
|
|
|
|
self.spi_send_cmd.send([self.oid, data])
|
2018-10-11 01:48:35 +02:00
|
|
|
def get_microsteps(self):
|
|
|
|
return 256 >> self.mres
|
|
|
|
def get_phase(self):
|
|
|
|
return (self.get_register("MSCNT") & 0x3ff) >> self.mres
|
2018-08-24 16:23:39 +02:00
|
|
|
cmd_DUMP_TMC_help = "Read and display TMC stepper driver registers"
|
2018-06-30 18:17:32 +02:00
|
|
|
def cmd_DUMP_TMC(self, params):
|
|
|
|
self.printer.lookup_object('toolhead').get_last_move_time()
|
|
|
|
gcode = self.printer.lookup_object('gcode')
|
2018-08-24 16:23:39 +02:00
|
|
|
logging.info("DUMP_TMC %s", self.name)
|
|
|
|
for reg_name in ReadRegisters:
|
|
|
|
val = self.get_register(reg_name)
|
|
|
|
msg = "%-15s %08x" % (reg_name + ":", val)
|
2018-06-30 18:17:32 +02:00
|
|
|
logging.info(msg)
|
|
|
|
gcode.respond_info(msg)
|
2018-05-23 05:39:06 +02:00
|
|
|
|
|
|
|
# Endstop wrapper that enables tmc2130 "sensorless homing"
|
|
|
|
class TMC2130VirtualEndstop:
|
|
|
|
def __init__(self, tmc2130):
|
|
|
|
self.tmc2130 = tmc2130
|
|
|
|
if tmc2130.diag1_pin is None:
|
|
|
|
raise pins.error("tmc2130 virtual endstop requires diag1_pin")
|
|
|
|
ppins = tmc2130.printer.lookup_object('pins')
|
|
|
|
self.mcu_endstop = ppins.setup_pin('endstop', tmc2130.diag1_pin)
|
|
|
|
if self.mcu_endstop.get_mcu() is not tmc2130.mcu:
|
|
|
|
raise pins.error("tmc2130 virtual endstop must be on same mcu")
|
|
|
|
# Wrappers
|
|
|
|
self.get_mcu = self.mcu_endstop.get_mcu
|
|
|
|
self.add_stepper = self.mcu_endstop.add_stepper
|
|
|
|
self.get_steppers = self.mcu_endstop.get_steppers
|
|
|
|
self.home_start = self.mcu_endstop.home_start
|
|
|
|
self.home_wait = self.mcu_endstop.home_wait
|
|
|
|
self.query_endstop = self.mcu_endstop.query_endstop
|
|
|
|
self.query_endstop_wait = self.mcu_endstop.query_endstop_wait
|
|
|
|
self.TimeoutError = self.mcu_endstop.TimeoutError
|
|
|
|
def home_prepare(self):
|
|
|
|
gconf = self.tmc2130.reg_GCONF
|
|
|
|
gconf &= ~GCONF_EN_PWM_MODE
|
|
|
|
gconf |= GCONF_DIAG1_STALL
|
2018-08-24 16:23:39 +02:00
|
|
|
self.tmc2130.set_register("GCONF", gconf)
|
|
|
|
self.tmc2130.set_register("TCOOLTHRS", 0xfffff)
|
2018-05-23 05:39:06 +02:00
|
|
|
self.mcu_endstop.home_prepare()
|
|
|
|
def home_finalize(self):
|
2018-08-24 16:23:39 +02:00
|
|
|
self.tmc2130.set_register("GCONF", self.tmc2130.reg_GCONF)
|
|
|
|
self.tmc2130.set_register("TCOOLTHRS", 0)
|
2018-05-23 05:39:06 +02:00
|
|
|
self.mcu_endstop.home_finalize()
|
2018-02-18 05:34:41 +01:00
|
|
|
|
|
|
|
def load_config_prefix(config):
|
2018-05-23 05:39:06 +02:00
|
|
|
return TMC2130(config)
|