diff options
author | Kevin O'Connor <kevin@koconnor.net> | 2018-02-17 23:34:41 -0500 |
---|---|---|
committer | Kevin O'Connor <kevin@koconnor.net> | 2018-05-20 18:03:06 -0400 |
commit | 680c92de40dcb37a8ef42ee3a091aa809eb23df9 (patch) | |
tree | ed09a00d50f54a061068757ed7e4afbd8f8191a9 /klippy/extras | |
parent | 61fbd193f91d8429ebdf4615ee43551170d1b520 (diff) | |
download | kutter-680c92de40dcb37a8ef42ee3a091aa809eb23df9.tar.gz kutter-680c92de40dcb37a8ef42ee3a091aa809eb23df9.tar.xz kutter-680c92de40dcb37a8ef42ee3a091aa809eb23df9.zip |
tmc2130: Add initial support for TMC2130 stepper motor drivers
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'klippy/extras')
-rw-r--r-- | klippy/extras/tmc2130.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/klippy/extras/tmc2130.py b/klippy/extras/tmc2130.py new file mode 100644 index 00000000..72d1faf1 --- /dev/null +++ b/klippy/extras/tmc2130.py @@ -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) |