tmc2208: Move get/set_register functions to new MCU_TMC_uart class

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-06-05 20:06:05 -04:00 committed by KevinOConnor
parent ab710cd4de
commit 6a7ddb6bf0
1 changed files with 124 additions and 112 deletions

View File

@ -1,6 +1,6 @@
# TMC2208 UART communication and configuration
#
# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
# Copyright (C) 2018-2019 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import logging, collections
@ -179,77 +179,17 @@ FieldFormatters.update({
######################################################################
# TMC2208 communication
# TMC2208 uart communication
######################################################################
# Generate a CRC8-ATM value for a bytearray
def calc_crc8(data):
crc = 0
for b in data:
for i in range(8):
if (crc >> 7) ^ (b & 0x01):
crc = (crc << 1) ^ 0x07
else:
crc = (crc << 1)
crc &= 0xff
b >>= 1
return crc
# Add serial start and stop bits to a message in a bytearray
def add_serial_bits(data):
out = 0
pos = 0
for d in data:
b = (d << 1) | 0x200
out |= (b << pos)
pos += 10
res = bytearray()
for i in range((pos+7)//8):
res.append((out >> (i*8)) & 0xff)
return res
# Generate a tmc2208 read register message
def encode_tmc2208_read(sync, addr, reg):
msg = bytearray([sync, addr, reg])
msg.append(calc_crc8(msg))
return add_serial_bits(msg)
# Generate a tmc2208 write register message
def encode_tmc2208_write(sync, addr, reg, val):
msg = bytearray([sync, addr, reg, (val >> 24) & 0xff, (val >> 16) & 0xff,
(val >> 8) & 0xff, val & 0xff])
msg.append(calc_crc8(msg))
return add_serial_bits(msg)
# Extract a tmc2208 read response message
def decode_tmc2208_read(reg, data):
# Convert data into a long integer for easy manipulation
if len(data) != 10:
return None
mval = pos = 0
for d in bytearray(data):
mval |= d << pos
pos += 8
# Extract register value
val = ((((mval >> 31) & 0xff) << 24) | (((mval >> 41) & 0xff) << 16)
| (((mval >> 51) & 0xff) << 8) | ((mval >> 61) & 0xff))
# Verify start/stop bits and crc
encoded_data = encode_tmc2208_write(0x05, 0xff, reg, val)
if data != encoded_data:
return None
return val
######################################################################
# TMC2208 printer object
######################################################################
class TMC2208:
def __init__(self, config):
# Helper code for communicating via TMC uart
class MCU_TMC_uart:
def __init__(self, config, name_to_reg, fields):
self.printer = config.get_printer()
self.name = config.get_name().split()[-1]
self.printer.register_event_handler("klippy:connect",
self._init_registers)
self.name_to_reg = name_to_reg
self.fields = fields
self.ifcnt = None
# pin setup
ppins = self.printer.lookup_object("pins")
rx_pin_params = ppins.lookup_pin(
@ -268,6 +208,116 @@ class TMC2208:
self.oid = self.mcu.create_oid()
self.tmcuart_send_cmd = None
self.mcu.register_config_callback(self.build_config)
def build_config(self):
bit_ticks = int(self.mcu.get_adjusted_freq() / 9000.)
self.mcu.add_config_cmd(
"config_tmcuart oid=%d rx_pin=%s pull_up=%d tx_pin=%s bit_time=%d"
% (self.oid, self.rx_pin, self.pullup, self.tx_pin, bit_ticks))
cmd_queue = self.mcu.alloc_command_queue()
self.tmcuart_send_cmd = self.mcu.lookup_command(
"tmcuart_send oid=%c write=%*s read=%c", cq=cmd_queue)
def get_fields(self):
return self.fields
def _calc_crc8(self, data):
# Generate a CRC8-ATM value for a bytearray
crc = 0
for b in data:
for i in range(8):
if (crc >> 7) ^ (b & 0x01):
crc = (crc << 1) ^ 0x07
else:
crc = (crc << 1)
crc &= 0xff
b >>= 1
return crc
def _add_serial_bits(self, data):
# Add serial start and stop bits to a message in a bytearray
out = 0
pos = 0
for d in data:
b = (d << 1) | 0x200
out |= (b << pos)
pos += 10
res = bytearray()
for i in range((pos+7)//8):
res.append((out >> (i*8)) & 0xff)
return res
def _encode_read(self, sync, addr, reg):
# Generate a tmc2208 read register message
msg = bytearray([sync, addr, reg])
msg.append(self._calc_crc8(msg))
return self._add_serial_bits(msg)
def _encode_write(self, sync, addr, reg, val):
# Generate a tmc2208 write register message
msg = bytearray([sync, addr, reg, (val >> 24) & 0xff,
(val >> 16) & 0xff, (val >> 8) & 0xff, val & 0xff])
msg.append(self._calc_crc8(msg))
return self._add_serial_bits(msg)
def _decode_read(self, reg, data):
# Extract a tmc2208 read response message
if len(data) != 10:
return None
# Convert data into a long integer for easy manipulation
mval = pos = 0
for d in bytearray(data):
mval |= d << pos
pos += 8
# Extract register value
val = ((((mval >> 31) & 0xff) << 24) | (((mval >> 41) & 0xff) << 16)
| (((mval >> 51) & 0xff) << 8) | ((mval >> 61) & 0xff))
# Verify start/stop bits and crc
encoded_data = self._encode_write(0x05, 0xff, reg, val)
if data != encoded_data:
return None
return val
def get_register(self, reg_name):
reg = self.name_to_reg[reg_name]
msg = self._encode_read(0xf5, 0x00, reg)
if self.printer.get_start_args().get('debugoutput') is not None:
return 0
for retry in range(5):
params = self.tmcuart_send_cmd.send_with_response(
[self.oid, msg, 10], 'tmcuart_response', self.oid)
val = self._decode_read(reg, params['read'])
if val is not None:
return val
raise self.printer.command_error(
"Unable to read tmc2208 '%s' register %s" % (self.name, reg_name))
def set_register(self, reg_name, val, print_time=0.):
msg = self._encode_write(0xf5, 0x00,
self.name_to_reg[reg_name] | 0x80, val)
if self.printer.get_start_args().get('debugoutput') is not None:
return
for retry in range(5):
ifcnt = self.ifcnt
if ifcnt is None:
self.ifcnt = ifcnt = self.get_register("IFCNT")
params = self.tmcuart_send_cmd.send_with_response(
[self.oid, msg, 0], 'tmcuart_response', self.oid)
self.ifcnt = self.get_register("IFCNT")
if self.ifcnt == (ifcnt + 1) & 0xff:
return
raise self.printer.command_error(
"Unable to write tmc2208 '%s' register %s" % (self.name, reg_name))
######################################################################
# TMC2208 printer object
######################################################################
class TMC2208:
def __init__(self, config):
self.printer = config.get_printer()
self.name = config.get_name().split()[-1]
self.printer.register_event_handler("klippy:connect",
self._handle_connect)
# Setup mcu communication
self.regs = collections.OrderedDict()
self.fields = tmc2130.FieldHelper(Fields, SignedFields, FieldFormatters,
self.regs)
self.mcu_tmc = MCU_TMC_uart(config, Registers, self.fields)
self.get_register = self.mcu_tmc.get_register
self.set_register = self.mcu_tmc.set_register
# Add DUMP_TMC, INIT_TMC command
gcode = self.printer.lookup_object("gcode")
gcode.register_mux_command(
@ -283,10 +333,6 @@ class TMC2208:
"INIT_TMC", "STEPPER", self.name,
self.cmd_INIT_TMC, desc=self.cmd_INIT_TMC_help)
# Setup basic register values
self.ifcnt = None
self.regs = collections.OrderedDict()
self.fields = tmc2130.FieldHelper(Fields, SignedFields, FieldFormatters,
self.regs)
self.fields.set_field("pdn_disable", True)
self.fields.set_field("mstep_reg_select", True)
self.fields.set_field("multistep_filt", True)
@ -315,46 +361,15 @@ class TMC2208:
set_config_field(config, "pwm_autograd", True)
set_config_field(config, "PWM_REG", 8)
set_config_field(config, "PWM_LIM", 12)
def build_config(self):
bit_ticks = int(self.mcu.get_adjusted_freq() / 9000.)
self.mcu.add_config_cmd(
"config_tmcuart oid=%d rx_pin=%s pull_up=%d tx_pin=%s bit_time=%d"
% (self.oid, self.rx_pin, self.pullup, self.tx_pin, bit_ticks))
cmd_queue = self.mcu.alloc_command_queue()
self.tmcuart_send_cmd = self.mcu.lookup_command(
"tmcuart_send oid=%c write=%*s read=%c", cq=cmd_queue)
def _init_registers(self):
# Send registers
for reg_name, val in self.regs.items():
self.set_register(reg_name, val)
def get_register(self, reg_name):
reg = Registers[reg_name]
msg = encode_tmc2208_read(0xf5, 0x00, reg)
if self.printer.get_start_args().get('debugoutput') is not None:
return 0
for retry in range(5):
params = self.tmcuart_send_cmd.send_with_response(
[self.oid, msg, 10], 'tmcuart_response', self.oid)
val = decode_tmc2208_read(reg, params['read'])
if val is not None:
return val
raise self.printer.config_error(
"Unable to read tmc2208 '%s' register %s" % (self.name, reg_name))
def set_register(self, reg_name, val):
msg = encode_tmc2208_write(0xf5, 0x00, Registers[reg_name] | 0x80, val)
if self.printer.get_start_args().get('debugoutput') is not None:
return
for retry in range(5):
ifcnt = self.ifcnt
if ifcnt is None:
self.ifcnt = ifcnt = self.get_register("IFCNT")
params = self.tmcuart_send_cmd.send_with_response(
[self.oid, msg, 0], 'tmcuart_response', self.oid)
self.ifcnt = self.get_register("IFCNT")
if self.ifcnt == (ifcnt + 1) & 0xff:
return
raise self.printer.config_error(
"Unable to write tmc2208 '%s' register %s" % (self.name, reg_name))
def _handle_connect(self):
try:
self._init_registers()
except self.printer.command_error as e:
raise self.printer.config_error(str(e))
def get_microsteps(self):
return 256 >> self.fields.get_field("MRES")
def get_phase(self):
@ -405,10 +420,7 @@ class TMC2208:
gcode.respond_info(self.fields.pretty_format(reg_name, val))
gcode.respond_info("========== Queried registers ==========")
for reg_name in ReadRegisters:
try:
val = self.get_register(reg_name)
except self.printer.config_error as e:
raise gcode.error(str(e))
val = self.get_register(reg_name)
# IOIN has different mappings depending on the driver type
# (SEL_A field of IOIN reg)
if reg_name == "IOIN":