tmc5160: Use tmc2130.MCU_TMC_SPI for low-level get/set_register access

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-06-05 18:49:13 -04:00 committed by KevinOConnor
parent 3becc6cf54
commit a122d113e8
1 changed files with 13 additions and 43 deletions

View File

@ -254,7 +254,13 @@ class TMC5160:
def __init__(self, config): def __init__(self, config):
self.printer = config.get_printer() self.printer = config.get_printer()
self.name = config.get_name().split()[-1] self.name = config.get_name().split()[-1]
self.spi = bus.MCU_SPI_from_config(config, 3, default_speed=100000) # Setup mcu communication
self.regs = collections.OrderedDict()
self.fields = tmc2130.FieldHelper(Fields, SignedFields, FieldFormatters,
self.regs)
self.mcu_tmc = tmc2130.MCU_TMC_SPI(config, Registers, self.fields)
self.get_register = self.mcu_tmc.get_register
self.set_register = self.mcu_tmc.set_register
# Allow virtual endstop to be created # Allow virtual endstop to be created
self.diag1_pin = config.get('diag1_pin', None) self.diag1_pin = config.get('diag1_pin', None)
ppins = self.printer.lookup_object("pins") ppins = self.printer.lookup_object("pins")
@ -274,9 +280,6 @@ class TMC5160:
"INIT_TMC", "STEPPER", self.name, "INIT_TMC", "STEPPER", self.name,
self.cmd_INIT_TMC, desc=self.cmd_INIT_TMC_help) self.cmd_INIT_TMC, desc=self.cmd_INIT_TMC_help)
# Setup basic register values # Setup basic register values
self.regs = collections.OrderedDict()
self.fields = tmc2130.FieldHelper(Fields, SignedFields, FieldFormatters,
self.regs)
irun, ihold, self.sense_resistor = get_config_current(config) irun, ihold, self.sense_resistor = get_config_current(config)
msteps, en_pwm, thresh = \ msteps, en_pwm, thresh = \
tmc2130.get_config_stealthchop(config, TMC_FREQUENCY) tmc2130.get_config_stealthchop(config, TMC_FREQUENCY)
@ -326,45 +329,15 @@ class TMC5160:
set_config_field(config, "TPOWERDOWN", 10) set_config_field(config, "TPOWERDOWN", 10)
self._init_registers() self._init_registers()
def decode_hex(self, hex_, reg_name=False): def _init_registers(self, print_time=0.):
reg = int( (hex_ >> 32) & 0xff - long(0x80) )
if reg not in Registers.values():
reg = int( (hex_ >> 32) & 0xff )
if reg not in Registers.values():
reg_name = "UFO"
if not reg_name:
for name, adr_ in Registers.items():
if adr_ == reg:
reg_name = name
val = hex_ & 0xFFFFFFFF
logging.debug(self.fields.pretty_format(reg_name, val))
def _init_registers(self, min_clock = 0):
for reg_name, val in self.regs.items(): for reg_name, val in self.regs.items():
self.set_register(reg_name, val, min_clock) self.set_register(reg_name, val, print_time)
def setup_pin(self, pin_type, pin_params): def setup_pin(self, pin_type, pin_params):
if pin_type != 'endstop' or pin_params['pin'] != 'virtual_endstop': if pin_type != 'endstop' or pin_params['pin'] != 'virtual_endstop':
raise pins.error("tmc5160 virtual endstop only useful as endstop") raise pins.error("tmc5160 virtual endstop only useful as endstop")
if pin_params['invert'] or pin_params['pullup']: if pin_params['invert'] or pin_params['pullup']:
raise pins.error("Can not pullup/invert tmc5160 virtual endstop") raise pins.error("Can not pullup/invert tmc5160 virtual endstop")
return tmc2130.TMC2130VirtualEndstop(self) return tmc2130.TMC2130VirtualEndstop(self)
def get_register(self, reg_name):
reg = Registers[reg_name]
self.spi.spi_send([reg, 0x00, 0x00, 0x00, 0x00])
if self.printer.get_start_args().get('debugoutput') is not None:
return 0
params = self.spi.spi_transfer([reg, 0x00, 0x00, 0x00, 0x00])
pr = bytearray(params['response'])
return (pr[1] << 24) | (pr[2] << 16) | (pr[3] << 8) | pr[4]
def set_register(self, reg_name, val, min_clock = 0):
reg = Registers[reg_name]
data = [(reg | 0x80) & 0xff, (val >> 24) & 0xff, (val >> 16) & 0xff,
(val >> 8) & 0xff, val & 0xff]
self.decode_hex( val, reg_name=reg_name )
self.spi.spi_send(data, min_clock)
def set_adress(self, val, min_clock=0):
data = [(val >> 32) & 0xff, (val >> 24) & 0xff,
(val >> 16) & 0xff, (val >> 8) & 0xff, val & 0xff]
self.spi.spi_send(data, min_clock)
def get_microsteps(self): def get_microsteps(self):
return 256 >> self.fields.get_field("MRES") return 256 >> self.fields.get_field("MRES")
def get_phase(self): def get_phase(self):
@ -393,13 +366,12 @@ class TMC5160:
if 'HOLDCURRENT' in params or 'CURRENT' in params: if 'HOLDCURRENT' in params or 'CURRENT' in params:
print_time = self.printer.lookup_object('toolhead')\ print_time = self.printer.lookup_object('toolhead')\
.get_last_move_time() .get_last_move_time()
min_clock = self.spi.get_mcu().print_time_to_clock(print_time)
irun, ihold = calc_current_config(run_current, irun, ihold = calc_current_config(run_current,
hold_current, self.sense_resistor) hold_current, self.sense_resistor)
self.set_register("CHOPCONF", self.regs["CHOPCONF"], min_clock) self.set_register("CHOPCONF", self.regs["CHOPCONF"], print_time)
self.fields.set_field("IHOLD", ihold) self.fields.set_field("IHOLD", ihold)
self.fields.set_field("IRUN", irun) self.fields.set_field("IRUN", irun)
self.set_register("IHOLD_IRUN", self.regs["IHOLD_IRUN"], min_clock) self.set_register("IHOLD_IRUN", self.regs["IHOLD_IRUN"], print_time)
else: else:
gcode.respond_info( gcode.respond_info(
"Run Current: %0.2fA Hold Current: %0.2fA" "Run Current: %0.2fA Hold Current: %0.2fA"
@ -421,8 +393,7 @@ class TMC5160:
def cmd_INIT_TMC(self, params): def cmd_INIT_TMC(self, params):
logging.info("INIT_TMC 5160 %s", self.name) logging.info("INIT_TMC 5160 %s", self.name)
print_time = self.printer.lookup_object('toolhead').get_last_move_time() print_time = self.printer.lookup_object('toolhead').get_last_move_time()
min_clock = self.spi.get_mcu().print_time_to_clock(print_time) self._init_registers(print_time)
self._init_registers(min_clock)
cmd_SET_TMC_FIELD_help = "Set a register field of a TMC5160 driver" cmd_SET_TMC_FIELD_help = "Set a register field of a TMC5160 driver"
def cmd_SET_TMC_FIELD(self, params): def cmd_SET_TMC_FIELD(self, params):
gcode = self.printer.lookup_object('gcode') gcode = self.printer.lookup_object('gcode')
@ -436,8 +407,7 @@ class TMC5160:
value = gcode.get_int('VALUE', params) value = gcode.get_int('VALUE', params)
self.fields.set_field(field, value) self.fields.set_field(field, value)
print_time = self.printer.lookup_object('toolhead').get_last_move_time() print_time = self.printer.lookup_object('toolhead').get_last_move_time()
min_clock = self.spi.get_mcu().print_time_to_clock(print_time) self.set_register(reg, self.regs[reg], print_time)
self.set_register(reg, self.regs[reg], min_clock)
def load_config_prefix(config): def load_config_prefix(config):
return TMC5160(config) return TMC5160(config)