aboutsummaryrefslogtreecommitdiffstats
path: root/klippy
diff options
context:
space:
mode:
authorAlex Voinea <voinea.dragos.alexandru@gmail.com>2023-03-04 10:25:49 +0100
committerKevinOConnor <kevin@koconnor.net>2023-03-07 10:55:44 -0500
commitbee1c67416c0f05eda06078bf160edb8b6ce092e (patch)
tree4c89a91f28960940e4bb47b93c456459a449e13f /klippy
parente6307ca9fe8d6e63037fa22ec9a2200f7921537c (diff)
downloadkutter-bee1c67416c0f05eda06078bf160edb8b6ce092e.tar.gz
kutter-bee1c67416c0f05eda06078bf160edb8b6ce092e.tar.xz
kutter-bee1c67416c0f05eda06078bf160edb8b6ce092e.zip
tmc5160: allow changing the globalscaler at runtime
Previously, the globalscaler was calculated during the config parsing and set to a fixed value. If the current was changed for any reason after the initialization, only IRUN and IHOLD would be changed. This however caused issues: - If the new current was lower, then the resolution of the possible current values would be low since there are only 32 IRUN/IHOLD steps. - If the new current was higher, it wouldn't actually work since IRUN and IHOLD are capped at 31, so it wouldn't be possible to increase the current without increasing globalscaler. With this commit, the globalscaler is recalculated whenever necessary in order to ensure the correct range of IRUN/IHOLD is used. Signed-off-by: Alex Voinea <voinea.dragos.alexandru@gmail.com>
Diffstat (limited to 'klippy')
-rw-r--r--klippy/extras/tmc5160.py22
1 files changed, 12 insertions, 10 deletions
diff --git a/klippy/extras/tmc5160.py b/klippy/extras/tmc5160.py
index f63d17ee..e0db1434 100644
--- a/klippy/extras/tmc5160.py
+++ b/klippy/extras/tmc5160.py
@@ -264,19 +264,18 @@ class TMC5160CurrentHelper:
above=0., maxval=MAX_CURRENT)
self.req_hold_current = hold_current
self.sense_resistor = config.getfloat('sense_resistor', 0.075, above=0.)
- self._set_globalscaler(run_current)
- irun, ihold = self._calc_current(run_current, hold_current)
+ gscaler, irun, ihold = self._calc_current(run_current, hold_current)
+ self.fields.set_field("globalscaler", gscaler)
self.fields.set_field("ihold", ihold)
self.fields.set_field("irun", irun)
- def _set_globalscaler(self, current):
+ def _calc_globalscaler(self, current):
globalscaler = int((current * 256. * math.sqrt(2.)
* self.sense_resistor / VREF) + .5)
globalscaler = max(32, globalscaler)
if globalscaler >= 256:
globalscaler = 0
- self.fields.set_field("globalscaler", globalscaler)
- def _calc_current_bits(self, current):
- globalscaler = self.fields.get_field("globalscaler")
+ return globalscaler
+ def _calc_current_bits(self, current, globalscaler):
if not globalscaler:
globalscaler = 256
cs = int((current * 256. * 32. * math.sqrt(2.) * self.sense_resistor)
@@ -284,9 +283,10 @@ class TMC5160CurrentHelper:
- 1. + .5)
return max(0, min(31, cs))
def _calc_current(self, run_current, hold_current):
- irun = self._calc_current_bits(run_current)
- ihold = self._calc_current_bits(min(hold_current, run_current))
- return irun, ihold
+ gscaler = self._calc_globalscaler(run_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):
globalscaler = self.fields.get_field("globalscaler")
if not globalscaler:
@@ -300,7 +300,9 @@ class TMC5160CurrentHelper:
return run_current, hold_current, self.req_hold_current, MAX_CURRENT
def set_current(self, run_current, hold_current, print_time):
self.req_hold_current = hold_current
- irun, ihold = self._calc_current(run_current, hold_current)
+ gscaler, irun, ihold = self._calc_current(run_current, hold_current)
+ val = self.fields.set_field("globalscaler", gscaler)
+ self.mcu_tmc.set_register("GLOBALSCALER", val, print_time)
self.fields.set_field("ihold", ihold)
val = self.fields.set_field("irun", irun)
self.mcu_tmc.set_register("IHOLD_IRUN", val, print_time)