From 9ba94ded9e9c48c81ddd10e9712619882e046fa1 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sun, 18 Nov 2018 18:18:25 -0500 Subject: [PATCH] tmc2660: Use bus.py helper code for spi Signed-off-by: Kevin O'Connor --- config/example-extras.cfg | 14 +++++++------- config/generic-duet2.cfg | 20 ++++++++++---------- klippy/extras/tmc2660.py | 39 ++++++++------------------------------- 3 files changed, 25 insertions(+), 48 deletions(-) diff --git a/config/example-extras.cfg b/config/example-extras.cfg index a493c951..a86759f0 100644 --- a/config/example-extras.cfg +++ b/config/example-extras.cfg @@ -745,14 +745,14 @@ # The pin corresponding to the TMC2660 chip select line. This pin # will be set to low at the start of SPI messages and set to high # after the message transfer completes. This parameter must be provided. -#bus: -# Select the SPI bus the TMC2660 stepper driver is connected to. This -# depends on the physical connections on your board, as well as the -# SPI implementation of your particular microcontroller. This parameter -# must be provided. -#freq: 2000000 +#spi_bus: +# Select the SPI bus the TMC2660 stepper driver is connected to. +# This depends on the physical connections on your board, as well as +# the SPI implementation of your particular micro-controller. The +# default is bus 0. +#spi_speed: 2000000 # SPI bus frequency used to communicate with the TMC2660 stepper -# driver. the default is 2000000. +# driver. The default is 2000000. #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 diff --git a/config/generic-duet2.cfg b/config/generic-duet2.cfg index 0193cae1..39603417 100644 --- a/config/generic-duet2.cfg +++ b/config/generic-duet2.cfg @@ -94,7 +94,7 @@ position_max: 250 [tmc2660 stepper_x] cs_pin: PD14 # X_SPI_EN Required for communication -bus: 1 # All TMC2660 drivers are connected to USART1, which is bus 1 on the sam4e port +spi_bus: 1 # All TMC2660 drivers are connected to USART1, which is bus 1 on the sam4e port microsteps: 16 interpolate: True # 1/16 micro-steps interpolated to 1/256 run_current: 1.000 @@ -111,7 +111,7 @@ position_max: 210 [tmc2660 stepper_y] cs_pin: PC9 -bus: 1 +spi_bus: 1 microsteps: 16 interpolate: True run_current: 1.000 @@ -128,7 +128,7 @@ position_max: 200 [tmc2660 stepper_z] cs_pin: PC10 -bus: 1 +spi_bus: 1 microsteps: 16 interpolate: True run_current: 1.000 @@ -142,7 +142,7 @@ step_distance: .0025 [tmc2660 stepper_z1] cs_pin: PD25 -bus: 1 +spi_bus: 1 microsteps: 16 interpolate: True run_current: 1.000 @@ -156,7 +156,7 @@ step_distance: .0025 [tmc2660 stepper_z2] cs_pin: PD26 -bus: 1 +spi_bus: 1 microsteps: 16 interpolate: True run_current: 1.000 @@ -170,7 +170,7 @@ step_distance: .0025 [tmc2660 stepper_z3] cs_pin: PC28 -bus: 1 +spi_bus: 1 microsteps: 16 interpolate: True run_current: 1.000 @@ -195,7 +195,7 @@ max_temp: 250 [tmc2660 extruder0] cs_pin: PC17 -bus: 1 +spi_bus: 1 microsteps: 16 interpolate: True run_current: 1.000 @@ -220,7 +220,7 @@ max_temp: 250 [tmc2660 extruder1] cs_pin: PC25 -bus: 1 +spi_bus: 1 microsteps: 16 interpolate: True run_current: 1.000 @@ -245,7 +245,7 @@ max_temp: 250 [tmc2660 extruder2] cs_pin: PD23 -bus: 1 +spi_bus: 1 microsteps: 16 interpolate: True run_current: 1.000 @@ -270,7 +270,7 @@ max_temp: 250 [tmc2660 extruder3] cs_pin: PD24 -bus: 1 +spi_bus: 1 microsteps: 16 interpolate: True run_current: 1.000 diff --git a/klippy/extras/tmc2660.py b/klippy/extras/tmc2660.py index d561ae1d..3cd41687 100644 --- a/klippy/extras/tmc2660.py +++ b/klippy/extras/tmc2660.py @@ -3,6 +3,7 @@ # Copyright (C) 2018 Florian Heilmann # # This file may be distributed under the terms of the GNU GPLv3 license. +import bus CURRENT_MAX = 2.4 CURRENT_MIN = 0.1 @@ -80,24 +81,7 @@ class TMC2660: def __init__(self, config): self.printer = config.get_printer() self.name = config.get_name().split()[1] - self.toolhead = None - - # Generic setup - ppins = self.printer.lookup_object("pins") - cs_pin = config.get('cs_pin') - cs_pin_params = ppins.lookup_pin(cs_pin) - self.mcu = cs_pin_params['chip'] - if cs_pin_params['invert']: - raise pins.error("tmc2660 can not invert pin") - pin = cs_pin_params['pin'] - self.oid = self.mcu.create_oid() - self.bus = config.getint('bus', minval=0, maxval=3) - self.freq = config.getint('freq', default=2000000, minval=1000000, maxval=4000000) - self.mcu.add_config_cmd( - "config_spi oid=%d bus=%d pin=%s mode=%d rate=%d shutdown_msg=" % ( - self.oid, self.bus, cs_pin_params['pin'], 0, self.freq)) - self.spi_send_cmd = self.spi_transfer_cmd = None - self.mcu.register_config_callback(self.build_config) + self.spi = bus.MCU_SPI_from_config(config, 0, default_speed=2000000) # Add SET_CURRENT and DUMP_TMC commands gcode = self.printer.lookup_object("gcode") gcode.register_mux_command( @@ -213,15 +197,8 @@ class TMC2660: self.handle_ready) def add_config_cmd(self, val): - self.mcu.add_config_cmd("spi_send oid=%d data=%06x" % ( - self.oid, val & 0xffffff)) - - 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) - self.spi_transfer_cmd = self.mcu.lookup_command( - "spi_transfer oid=%c data=%*s", cq=cmd_queue) + data = [(val >> 16) & 0xff, (val >> 8) & 0xff, val & 0xff] + self.spi.spi_send(data) def get_microsteps(self): return 256 >> self.driver_mres @@ -229,7 +206,7 @@ class TMC2660: def get_phase(self): # Send DRVCTRL to get a response reg_data = [(self.reg_drvctrl >> 16) & 0xff, (self.reg_drvctrl >> 8) & 0xff, self.reg_drvctrl & 0xff] - params = self.spi_transfer_cmd.send_with_response([self.oid, reg_data], 'spi_transfer_response', self.oid) + params = self.spi.spi_transfer(reg_data) pr = bytearray(params['response']) steps = (((pr[0] << 16) | (pr[1] << 8) | pr[2]) & READRSP['MSTEP'][1]) >> READRSP['MSTEP'][0] return steps >> self.driver_mres @@ -246,8 +223,8 @@ class TMC2660: reg &= ~(SGCSCONF["CS"][1]) reg |= get_bits(SGCSCONF, "CS", self.driver_cs) reg_data = [(reg >> 16) & 0xff, (reg >> 8) & 0xff, reg & 0xff] - clock = self.mcu.print_time_to_clock(print_time) - params = self.spi_send_cmd.send([self.oid, reg_data], minclock=clock, reqclock=clock) + clock = self.spi.get_mcu().print_time_to_clock(print_time) + self.spi.spi_send(reg_data, minclock=clock, reqclock=clock) cmd_SET_TMC_CURRENT_help = "Set the current of a TMC2660 driver (between %d and %d)" % (CURRENT_MIN, CURRENT_MAX) def cmd_SET_TMC_CURRENT(self, params): @@ -266,7 +243,7 @@ class TMC2660: gcode.respond_info(msg) # Send one register to get the return data reg_data = [(self.reg_drvctrl >> 16) & 0xff, (self.reg_drvctrl >> 8) & 0xff, self.reg_drvctrl & 0xff] - params = self.spi_transfer_cmd.send_with_response([self.oid, reg_data], 'spi_transfer_response', self.oid) + params = self.spi.spi_transfer(reg_data) pr = bytearray(params['response']) msg = "%-15s %08x" % ("RESPONSE:", ((pr[0] << 16) | (pr[1] << 8) | pr[2])) gcode.respond_info(msg)