tmc2130: Minor code cleanup

Rework the code to use set_register() with the string name of the
desired register to set.  Add a get_register() method.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-08-24 10:23:39 -04:00
parent e5d95fdd00
commit afd5d55c00
1 changed files with 58 additions and 53 deletions

View File

@ -6,19 +6,23 @@
import math, logging
TMC_FREQUENCY=13200000.
REG_GCONF=0x00
GCONF_EN_PWM_MODE=1<<2
GCONF_DIAG1_STALL=1<<8
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"
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,
}
ReadRegisters = [
"GCONF", "GSTAT", "IOIN", "TSTEP", "XDIRECT", "MSCNT", "MSCURACT",
"CHOPCONF", "DRV_STATUS", "PWM_SCALE", "LOST_STEPS",
]
class TMC2130:
def __init__(self, config):
self.printer = config.get_printer()
@ -30,15 +34,20 @@ class TMC2130:
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, 4000000))
self.spi_send_cmd = self.spi_transfer_cmd = None
self.mcu.add_config_object(self)
# Allow virtual endstop to be created
self.diag1_pin = config.get('diag1_pin', None)
ppins.register_chip("tmc2130_" + self.name, self)
# 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))
# Get config for initial driver settings
run_current = config.getfloat('run_current', above=0., maxval=2.)
hold_current = config.getfloat('hold_current', run_current,
above=0., maxval=2.)
@ -61,11 +70,6 @@ class TMC2130:
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)
# Allow virtual endstop to be created
self.diag1_pin = config.get('diag1_pin', 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
irun = self.current_bits(run_current, sense_resistor, vsense)
@ -74,27 +78,19 @@ class TMC2130:
vsense = True
irun = self.current_bits(run_current, sense_resistor, vsense)
ihold = self.current_bits(hold_current, sense_resistor, vsense)
# configure GCONF
# Configure registers
self.reg_GCONF = (sc_velocity > 0.) << 2
self.add_config_cmd(REG_GCONF, self.reg_GCONF)
# configure CHOPCONF
self.add_config_cmd(
0x6c, toff | (hstrt << 4) | (hend << 7) | (blank_time_select << 15)
| (vsense << 17) | (self.mres << 24) | (interpolate << 28))
# configure IHOLD_IRUN
self.add_config_cmd(0x10, ihold | (irun << 8) | (iholddelay << 16))
# configure TPOWERDOWN
self.add_config_cmd(0x11, tpowerdown)
# configure TPWMTHRS
self.add_config_cmd(0x13, max(0, min(0xfffff, sc_threshold)))
# configure COOLCONF
self.add_config_cmd(REG_COOLCONF, sgt << 16)
# configure PWMCONF
self.add_config_cmd(REG_PWMCONF, pwm_ampl | (pwm_grad << 8)
| (pwm_freq << 16) | (pwm_scale << 18))
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)
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)))
def current_bits(self, current, sense_resistor, vsense_on):
sense_resistor += 0.020
vsense = 0.32
@ -123,23 +119,32 @@ class TMC2130:
"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()):
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'])
val = (pr[1] << 24) | (pr[2] << 16) | (pr[3] << 8) | pr[4]
msg = "%15s: %8x" % (name, val)
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,
(val >> 8) & 0xff, val & 0xff]
self.spi_send_cmd.send([self.oid, data])
cmd_DUMP_TMC_help = "Read and display TMC stepper driver 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_TMC %s", self.name)
for reg_name in ReadRegisters:
val = self.get_register(reg_name)
msg = "%-15s %08x" % (reg_name + ":", val)
logging.info(msg)
gcode.respond_info(msg)
@ -166,12 +171,12 @@ class TMC2130VirtualEndstop:
gconf = self.tmc2130.reg_GCONF
gconf &= ~GCONF_EN_PWM_MODE
gconf |= GCONF_DIAG1_STALL
self.tmc2130.set_register(REG_GCONF, gconf)
self.tmc2130.set_register(REG_TCOOLTHRS, 0xfffff)
self.tmc2130.set_register("GCONF", gconf)
self.tmc2130.set_register("TCOOLTHRS", 0xfffff)
self.mcu_endstop.home_prepare()
def home_finalize(self):
self.tmc2130.set_register(REG_GCONF, self.tmc2130.reg_GCONF)
self.tmc2130.set_register(REG_TCOOLTHRS, 0)
self.tmc2130.set_register("GCONF", self.tmc2130.reg_GCONF)
self.tmc2130.set_register("TCOOLTHRS", 0)
self.mcu_endstop.home_finalize()
def load_config_prefix(config):