tmc2208: Add register field formatters

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Signed-off-by: Dmitry Frolov <dmitry.frolov@gmail.com>
This commit is contained in:
Dmitry Frolov 2019-02-03 14:49:03 +07:00 committed by Kevin O'Connor
parent 955fdcaa0c
commit 51f14b9c8c
2 changed files with 40 additions and 7 deletions

View File

@ -35,9 +35,16 @@ Fields = {}
def ffs(mask):
return (mask & -mask).bit_length() - 1
# Decode two's complement signed integer
def decode_signed_int(val, bits):
if ((val >> (bits - 1)) & 1):
return val - (1 << bits)
return val
class FieldHelper:
def __init__(self, all_fields):
def __init__(self, all_fields, field_formatters={}):
self.all_fields = all_fields
self.field_formatters = field_formatters
def get_field(self, reg_name, field_name, reg_value):
# Returns value of the register field
mask = self.all_fields.get(reg_name, {})[field_name]
@ -48,11 +55,15 @@ class FieldHelper:
return (reg_value & ~mask) | ((field_value << ffs(mask)) & mask)
def pretty_format(self, reg_name, value):
# Provide a string description of a register
fields = [ " %s=%d" % (field_name, (value & mask) >> ffs(mask))
for field_name, mask in sorted(self.all_fields.get(
reg_name, {}).items(), key = lambda f: f[1])
if value & mask ]
return "%-15s %08x%s" % (reg_name + ":", value, "".join(fields))
reg_fields = self.all_fields.get(reg_name, {})
reg_fields = sorted([(mask, name) for name, mask in reg_fields.items()])
fields = []
for mask, field_name in reg_fields:
fval = (value & mask) >> ffs(mask)
sval = self.field_formatters.get(field_name, str)(fval)
if sval and sval != "0":
fields.append(" %s=%s" % (field_name, sval))
return "%-11s %08x%s" % (reg_name + ":", value, "".join(fields))
######################################################################

View File

@ -171,6 +171,28 @@ Fields["PWM_AUTO"] = {
"PWM_GRAD_AUTO": 0xff << 16
}
FieldFormatters = {
"I_scale_analog": (lambda v: "1(ExtVREF)" if v else ""),
"shaft": (lambda v: "1(Reverse)" if v else ""),
"drv_err": (lambda v: "1(ErrorShutdown!)" if v else ""),
"uv_cp": (lambda v: "1(Undervoltage!)" if v else ""),
"SEL_A": (lambda v: "%d(%s)" % (v, ["TMC222x", "TMC220x"][v])),
"VERSION": (lambda v: "%#x" % v),
"CUR_A": (lambda v: str(tmc2130.decode_signed_int(v, 9))),
"CUR_B": (lambda v: str(tmc2130.decode_signed_int(v, 9))),
"MRES": (lambda v: "%d(%dusteps)" % (v, 0x100 >> v)),
"otpw": (lambda v: "1(OvertempWarning!)" if v else ""),
"ot": (lambda v: "1(OvertempError!)" if v else ""),
"s2ga": (lambda v: "1(ShortToGND_A!)" if v else ""),
"s2gb": (lambda v: "1(ShortToGND_B!)" if v else ""),
"s2vsa": (lambda v: "1(LowSideShort_A!)" if v else ""),
"s2vsb": (lambda v: "1(LowSideShort_B!)" if v else ""),
"ola": (lambda v: "1(OpenLoad_A!)" if v else ""),
"olb": (lambda v: "1(OpenLoad_B!)" if v else ""),
"PWM_SCALE_AUTO": (lambda v: str(tmc2130.decode_signed_int(v, 9)))
}
######################################################################
# TMC2208 communication
######################################################################
@ -267,7 +289,7 @@ class TMC2208:
"DUMP_TMC", "STEPPER", self.name,
self.cmd_DUMP_TMC, desc=self.cmd_DUMP_TMC_help)
# Get config for initial driver settings
self.field_helper = tmc2130.FieldHelper(Fields)
self.field_helper = tmc2130.FieldHelper(Fields, FieldFormatters)
run_current = config.getfloat('run_current', above=0., maxval=2.)
hold_current = config.getfloat('hold_current', run_current,
above=0., maxval=2.)