Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tmc2240: add current formatters #6503

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions klippy/extras/tmc2240.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,15 @@ def _calc_current(self, run_current, hold_current):
irun = self._calc_current_bits(run_current, gscaler)
ihold = self._calc_current_bits(min(hold_current, run_current), gscaler)
return gscaler, irun, ihold
def _calc_current_from_field(self, field_name):
def _calc_current_from_field_value(self, value):
ifs_rms = self._get_ifs_rms()
globalscaler = self.fields.get_field("globalscaler")
if not globalscaler:
globalscaler = 256
return globalscaler * (value + 1) * ifs_rms / (256. * 32.)
def _calc_current_from_field(self, field_name):
bits = self.fields.get_field(field_name)
return globalscaler * (bits + 1) * ifs_rms / (256. * 32.)
return self._calc_current_from_field_value(bits)
def get_current(self):
ifs_rms = self._get_ifs_rms()
run_current = self._calc_current_from_field("irun")
Expand All @@ -342,7 +344,8 @@ def set_current(self, run_current, hold_current, print_time):
class TMC2240:
def __init__(self, config):
# Setup mcu communication
self.fields = tmc.FieldHelper(Fields, SignedFields, FieldFormatters)
self.fields = tmc.FieldHelper(Fields, SignedFields,
FieldFormatters.copy())
if config.get("uart_pin", None) is not None:
# use UART for communication
self.mcu_tmc = tmc_uart.MCU_TMC_uart(config, Registers, self.fields,
Expand All @@ -355,6 +358,14 @@ def __init__(self, config):
tmc.TMCVirtualPinHelper(config, self.mcu_tmc)
# Register commands
current_helper = TMC2240CurrentHelper(config, self.mcu_tmc)
self.fields.field_formatters.update({
"current_range": (lambda v: "%d (%.2fA RMS)"
% (v, current_helper._get_ifs_rms(v))),
"ihold": (lambda v: "%d (%.2fA RMS)"
% (v, current_helper._calc_current_from_field_value(v))),
"irun": (lambda v: "%d (%.2fA RMS)"
% (v, current_helper._calc_current_from_field_value(v))),
})
cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc, current_helper)
cmdhelper.setup_register_dump(ReadRegisters)
self.get_phase_offset = cmdhelper.get_phase_offset
Expand Down
Loading