tmc_uart: Hold a mutex during MCU_TMC_uart get/set_register()

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-06-10 09:55:52 -04:00 committed by KevinOConnor
parent 844dca9c6e
commit 041831f93a
1 changed files with 16 additions and 10 deletions

View File

@ -57,6 +57,7 @@ class MCU_analog_mux:
class MCU_TMC_uart_bitbang: class MCU_TMC_uart_bitbang:
def __init__(self, rx_pin_params, tx_pin_params, select_pins_desc): def __init__(self, rx_pin_params, tx_pin_params, select_pins_desc):
self.mcu = rx_pin_params['chip'] self.mcu = rx_pin_params['chip']
self.mutex = self.mcu.get_printer().get_reactor().mutex()
self.pullup = rx_pin_params['pullup'] self.pullup = rx_pin_params['pullup']
self.rx_pin = rx_pin_params['pin'] self.rx_pin = rx_pin_params['pin']
self.tx_pin = tx_pin_params['pin'] self.tx_pin = tx_pin_params['pin']
@ -192,9 +193,10 @@ class MCU_TMC_uart:
self.ifcnt = None self.ifcnt = None
self.addr = 0 self.addr = 0
self.instance_id, self.mcu_uart = lookup_tmc_uart_bitbang(config) self.instance_id, self.mcu_uart = lookup_tmc_uart_bitbang(config)
self.mutex = self.mcu_uart.mutex
def get_fields(self): def get_fields(self):
return self.fields return self.fields
def get_register(self, reg_name): def _do_get_register(self, reg_name):
reg = self.name_to_reg[reg_name] reg = self.name_to_reg[reg_name]
if self.printer.get_start_args().get('debugoutput') is not None: if self.printer.get_start_args().get('debugoutput') is not None:
return 0 return 0
@ -204,17 +206,21 @@ class MCU_TMC_uart:
return val return val
raise self.printer.command_error( raise self.printer.command_error(
"Unable to read tmc2208 '%s' register %s" % (self.name, reg_name)) "Unable to read tmc2208 '%s' register %s" % (self.name, reg_name))
def get_register(self, reg_name):
with self.mutex:
return self._do_get_register(reg_name)
def set_register(self, reg_name, val, print_time=None): def set_register(self, reg_name, val, print_time=None):
reg = self.name_to_reg[reg_name] reg = self.name_to_reg[reg_name]
if self.printer.get_start_args().get('debugoutput') is not None: if self.printer.get_start_args().get('debugoutput') is not None:
return return
with self.mutex:
for retry in range(5): for retry in range(5):
ifcnt = self.ifcnt ifcnt = self.ifcnt
if ifcnt is None: if ifcnt is None:
self.ifcnt = ifcnt = self.get_register("IFCNT") self.ifcnt = ifcnt = self._do_get_register("IFCNT")
self.mcu_uart.reg_write(self.instance_id, self.addr, reg, val, self.mcu_uart.reg_write(self.instance_id, self.addr, reg, val,
print_time) print_time)
self.ifcnt = self.get_register("IFCNT") self.ifcnt = self._do_get_register("IFCNT")
if self.ifcnt == (ifcnt + 1) & 0xff: if self.ifcnt == (ifcnt + 1) & 0xff:
return return
raise self.printer.command_error( raise self.printer.command_error(