Skip to content

Commit

Permalink
Update tmc2130.py
Browse files Browse the repository at this point in the history
  • Loading branch information
HonestBrothers committed Dec 19, 2024
1 parent b3d0dac commit 97bb473
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions klippy/extras/tmc2130.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class TMC2130CurrentHelper(tmc.BaseTMCCurrentHelper):
def __init__(self, config, mcu_tmc):
super().__init__(config, mcu_tmc, MAX_CURRENT)

self.cs = config.getint('driver_cs', None, maxval=31, minval=0)
vsense, irun, ihold = self._calc_current(
self.req_run_current, self.req_hold_current
)
Expand All @@ -213,10 +214,13 @@ def _calc_current_bits(self, current, vsense):
vref = 0.32
if vsense:
vref = 0.18
cs = (
int(32.0 * sense_resistor * current * math.sqrt(2.0) / vref + 0.5)
- 1
)
if self.cs is None:
cs = (
int(32.0 * sense_resistor * current * math.sqrt(2.0) / vref + 0.5)
- 1
)
else:
cs = self.cs
return max(0, min(31, cs))

def _calc_current_from_bits(self, cs, vsense):
Expand All @@ -237,7 +241,7 @@ def _calc_current(self, run_current, hold_current):
if abs(run_current - cur2) < abs(run_current - cur):
vsense = False
irun = irun2
ihold = self._calc_current_bits(min(hold_current, run_current), vsense)
ihold = int(min((hold_current / run_current) * irun, irun))
return vsense, irun, ihold

def get_current(self):
Expand Down

0 comments on commit 97bb473

Please sign in to comment.