tmc2130: Add initial support for TMC2130 stepper motor drivers

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-02-17 23:34:41 -05:00
parent 61fbd193f9
commit 680c92de40
2 changed files with 108 additions and 0 deletions

View File

@ -402,6 +402,44 @@
# default is to not scale the 'channel_x' parameters. # default is to not scale the 'channel_x' parameters.
# Configure a TMC2130 stepper motor driver (one may define any number
# of sections with a "tmc2130" prefix).
#[tmc2130 my_name]
#cs_pin:
# The pin corresponding to the TMC2130 chip select line. This pin
# will be set to low at the start of SPI messages and raised to high
# after the message completes. This parameter must be provided.
#microsteps:
# The number of microsteps to configure the driver to use. Valid
# values are 1, 2, 4, 8, 16, 32, 64, 128, 256. This parameter must
# be provided.
#interpolate: True
# If true, enable TMC2130 step interpolation (the driver will
# interally step at a rate of 256 micro-steps). The default is True.
#run_current:
# The amount of current (in amps) to configure the driver to use
# during stepper movement. This parameter must be provided.
#hold_current:
# The amount of current (in amps) to configure the driver to use
# when the stepper is not moving. The default is to use the same
# value as run_current.
#sense_resistor: 0.110
# The resistance (in ohms) of the motor sense resistor. The default
# is 0.110 ohms.
#stealthchop: False
# Enable "stealthChop" mode. The default is False.
#driver_IHOLDDELAY: 8
#driver_TPOWERDOWN: 0
#driver_BLANK_TIME_SELECT: 1
#driver_TOFF: 1
#driver_HEND: 7
#driver_HSTRT: 0
# Set the given register during the configuration of the TMC2130
# chip. This may be used to set custom motor parameters. The
# defaults for each parameter are next to the parameter name in the
# above list.
# Homing override. One may use this mechanism to run a series of # Homing override. One may use this mechanism to run a series of
# g-code commands in place of a G28 found in the normal g-code input. # g-code commands in place of a G28 found in the normal g-code input.
# This may be useful on printers that require a specific procedure to # This may be useful on printers that require a specific procedure to

70
klippy/extras/tmc2130.py Normal file
View File

@ -0,0 +1,70 @@
# TMC2130 configuration
#
# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import math
class tmc2130:
def __init__(self, config):
printer = config.get_printer()
# pin setup
ppins = printer.lookup_object("pins")
cs_pin = config.get('cs_pin')
cs_pin_params = ppins.lookup_pin('digital_out', cs_pin)
if cs_pin_params['invert']:
raise pins.error("tmc2130 can not invert pin")
self.mcu = cs_pin_params['chip']
pin = cs_pin_params['pin']
self.oid = self.mcu.create_oid()
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, 3600000))
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}
microsteps = config.getchoice('microsteps', steps)
interpolate = config.getboolean('interpolate', True)
stealthchop = config.getboolean('stealthchop', False)
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)
toff = config.getint('driver_TOFF', 1, minval=1, maxval=15)
hend = config.getint('driver_HEND', 7, minval=0, maxval=15)
hstrt = config.getint('driver_HSTRT', 0, minval=0, maxval=7)
# 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)
# configure GCONF
self.add_config_cmd(0x00, stealthchop << 2)
# configure CHOPCONF
self.add_config_cmd(
0x6c, toff | (hstrt << 4) | (hend << 7) | (blank_time_select << 15)
| (vsense << 17) | (microsteps << 24) | (interpolate << 28))
# configure IHOLD_IRUN
self.add_config_cmd(0x10, ihold | (irun << 8) | (iholddelay << 16))
# configure TPOWERDOWN
self.add_config_cmd(0x11, tpowerdown)
def add_config_cmd(self, addr, val):
self.mcu.add_config_cmd("spi_send oid=%d data=%02x%08x" % (
self.oid, (addr | 0x80) & 0xff, val & 0xffffffff), is_init=True)
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))
def load_config_prefix(config):
return tmc2130(config)