Skip to content

Commit

Permalink
tmc2240: add current formatters
Browse files Browse the repository at this point in the history
Run current, hold current and current range will be shown
in Amperes. Each 2240 driver now has a separate instance of
FieldFormatters, since the current formatters need to
access a few fields in order to calculate the current.

Signed-off-by: Kamil Domański <[email protected]>
  • Loading branch information
kdomanski committed Feb 20, 2024
1 parent a77d079 commit 6086ca1
Showing 1 changed file with 14 additions and 3 deletions.
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

0 comments on commit 6086ca1

Please sign in to comment.