tmc2130: Convert field helper into a class
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
cdcc320710
commit
955fdcaa0c
|
@ -35,23 +35,24 @@ Fields = {}
|
||||||
def ffs(mask):
|
def ffs(mask):
|
||||||
return (mask & -mask).bit_length() - 1
|
return (mask & -mask).bit_length() - 1
|
||||||
|
|
||||||
# Provide a string description of a register
|
class FieldHelper:
|
||||||
def pretty_format(all_fields, reg_name, value):
|
def __init__(self, all_fields):
|
||||||
fields = [ " %s=%d" % (field_name, (value & mask) >> ffs(mask))
|
self.all_fields = all_fields
|
||||||
for field_name, mask in sorted(all_fields.get(
|
def get_field(self, reg_name, field_name, reg_value):
|
||||||
reg_name, {}).items(), key = lambda f: f[1])
|
# Returns value of the register field
|
||||||
if value & mask ]
|
mask = self.all_fields.get(reg_name, {})[field_name]
|
||||||
return "%-15s %08x%s" % (reg_name + ":", value, "".join(fields))
|
return (reg_value & mask) >> ffs(mask)
|
||||||
|
def set_field(self, reg_name, field_name, reg_value, field_value):
|
||||||
# Returns value of the register field
|
# Returns register value with field bits filled with supplied value
|
||||||
def get_field(all_fields, reg_name, field_name, reg_value):
|
mask = self.all_fields.get(reg_name, {})[field_name]
|
||||||
mask = all_fields.get(reg_name, {})[field_name]
|
return (reg_value & ~mask) | ((field_value << ffs(mask)) & mask)
|
||||||
return (reg_value & mask) >> ffs(mask)
|
def pretty_format(self, reg_name, value):
|
||||||
|
# Provide a string description of a register
|
||||||
# Returns register value with field bits filled with supplied field value
|
fields = [ " %s=%d" % (field_name, (value & mask) >> ffs(mask))
|
||||||
def set_field(all_fields, reg_name, field_name, reg_value, field_value):
|
for field_name, mask in sorted(self.all_fields.get(
|
||||||
mask = all_fields.get(reg_name, {})[field_name]
|
reg_name, {}).items(), key = lambda f: f[1])
|
||||||
return (reg_value & ~mask) | ((field_value << ffs(mask)) & mask)
|
if value & mask ]
|
||||||
|
return "%-15s %08x%s" % (reg_name + ":", value, "".join(fields))
|
||||||
|
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
|
@ -73,6 +74,7 @@ class TMC2130:
|
||||||
"DUMP_TMC", "STEPPER", self.name,
|
"DUMP_TMC", "STEPPER", self.name,
|
||||||
self.cmd_DUMP_TMC, desc=self.cmd_DUMP_TMC_help)
|
self.cmd_DUMP_TMC, desc=self.cmd_DUMP_TMC_help)
|
||||||
# Get config for initial driver settings
|
# Get config for initial driver settings
|
||||||
|
self.field_helper = FieldHelper(Fields)
|
||||||
run_current = config.getfloat('run_current', above=0., maxval=2.)
|
run_current = config.getfloat('run_current', above=0., maxval=2.)
|
||||||
hold_current = config.getfloat('hold_current', run_current,
|
hold_current = config.getfloat('hold_current', run_current,
|
||||||
above=0., maxval=2.)
|
above=0., maxval=2.)
|
||||||
|
@ -160,7 +162,7 @@ class TMC2130:
|
||||||
logging.info("DUMP_TMC %s", self.name)
|
logging.info("DUMP_TMC %s", self.name)
|
||||||
for reg_name in ReadRegisters:
|
for reg_name in ReadRegisters:
|
||||||
val = self.get_register(reg_name)
|
val = self.get_register(reg_name)
|
||||||
msg = pretty_format(Fields, reg_name, val)
|
msg = self.field_helper.pretty_format(reg_name, val)
|
||||||
logging.info(msg)
|
logging.info(msg)
|
||||||
gcode.respond_info(msg)
|
gcode.respond_info(msg)
|
||||||
|
|
||||||
|
|
|
@ -267,6 +267,7 @@ class TMC2208:
|
||||||
"DUMP_TMC", "STEPPER", self.name,
|
"DUMP_TMC", "STEPPER", self.name,
|
||||||
self.cmd_DUMP_TMC, desc=self.cmd_DUMP_TMC_help)
|
self.cmd_DUMP_TMC, desc=self.cmd_DUMP_TMC_help)
|
||||||
# Get config for initial driver settings
|
# Get config for initial driver settings
|
||||||
|
self.field_helper = tmc2130.FieldHelper(Fields)
|
||||||
run_current = config.getfloat('run_current', above=0., maxval=2.)
|
run_current = config.getfloat('run_current', above=0., maxval=2.)
|
||||||
hold_current = config.getfloat('hold_current', run_current,
|
hold_current = config.getfloat('hold_current', run_current,
|
||||||
above=0., maxval=2.)
|
above=0., maxval=2.)
|
||||||
|
@ -386,10 +387,11 @@ class TMC2208:
|
||||||
raise gcode.error(str(e))
|
raise gcode.error(str(e))
|
||||||
# IOIN has different mappings depending on the driver type
|
# IOIN has different mappings depending on the driver type
|
||||||
# (SEL_A field of IOIN reg)
|
# (SEL_A field of IOIN reg)
|
||||||
if reg_name is "IOIN":
|
if reg_name == "IOIN":
|
||||||
drv_type = tmc2130.get_field(Fields, "IOIN@TMC222x", "SEL_A", val)
|
drv_type = self.field_helper.get_field("IOIN@TMC222x", "SEL_A",
|
||||||
|
val)
|
||||||
reg_name = "IOIN@TMC220x" if drv_type else "IOIN@TMC222x"
|
reg_name = "IOIN@TMC220x" if drv_type else "IOIN@TMC222x"
|
||||||
msg = tmc2130.pretty_format(Fields, reg_name, val)
|
msg = self.field_helper.pretty_format(reg_name, val)
|
||||||
logging.info(msg)
|
logging.info(msg)
|
||||||
gcode.respond_info(msg)
|
gcode.respond_info(msg)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue