mirror of
				https://github.com/Klipper3d/klipper.git
				synced 2025-11-03 20:05:49 +01:00 
			
		
		
		
	The recommended TOFF time in "standalone mode" according to the tmc2130 is 4, so use that as the default. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
		
			
				
	
	
		
			71 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			71 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
# 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', 4, 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)
 |