From 21ee7b1c862230e58d68bd8a9a81e561fc5df1e2 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Fri, 4 Dec 2020 21:58:39 -0800 Subject: [PATCH] Fix TMC_HOME_PHASE divide by zero (#20368) --- Marlin/src/module/motion.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Marlin/src/module/motion.cpp b/Marlin/src/module/motion.cpp index 3a01cda5c1..7b4c89e759 100644 --- a/Marlin/src/module/motion.cpp +++ b/Marlin/src/module/motion.cpp @@ -1490,10 +1490,12 @@ void set_axis_never_homed(const AxisEnum axis) { effectorBackoutDir, // Direction in which the effector mm coordinates move away from endstop. stepperBackoutDir; // Direction in which the TMC µstep count(phase) move away from endstop. + #define PHASE_PER_MICROSTEP(N) (256 / _MAX(1, N##_MICROSTEPS)) + switch (axis) { #ifdef X_MICROSTEPS case X_AXIS: - phasePerUStep = 256 / (X_MICROSTEPS); + phasePerUStep = PHASE_PER_MICROSTEP(X); phaseCurrent = stepperX.get_microstep_counter(); effectorBackoutDir = -X_HOME_DIR; stepperBackoutDir = INVERT_X_DIR ? effectorBackoutDir : -effectorBackoutDir; @@ -1501,7 +1503,7 @@ void set_axis_never_homed(const AxisEnum axis) { #endif #ifdef Y_MICROSTEPS case Y_AXIS: - phasePerUStep = 256 / (Y_MICROSTEPS); + phasePerUStep = PHASE_PER_MICROSTEP(Y); phaseCurrent = stepperY.get_microstep_counter(); effectorBackoutDir = -Y_HOME_DIR; stepperBackoutDir = INVERT_Y_DIR ? effectorBackoutDir : -effectorBackoutDir; @@ -1509,7 +1511,7 @@ void set_axis_never_homed(const AxisEnum axis) { #endif #ifdef Z_MICROSTEPS case Z_AXIS: - phasePerUStep = 256 / (Z_MICROSTEPS); + phasePerUStep = PHASE_PER_MICROSTEP(Z); phaseCurrent = stepperZ.get_microstep_counter(); effectorBackoutDir = -Z_HOME_DIR; stepperBackoutDir = INVERT_Z_DIR ? effectorBackoutDir : -effectorBackoutDir;