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>
This commit is contained in:
Alex Voinea 2023-03-04 10:25:49 +01:00 committed by KevinOConnor
parent e6307ca9fe
commit bee1c67416
3 changed files with 24 additions and 11 deletions

View file

@ -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)