From 51a539608cafafb41fe8ee248c41a91c70f7b145 Mon Sep 17 00:00:00 2001
From: Yuri D'Elia <wavexx@thregr.org>
Date: Mon, 22 Jun 2020 00:19:47 +0200
Subject: [PATCH] Reset LA_phase at each trapezoid stage

There used to be a single stage where an extruder reversal could occur,
but since PR #2591 reversals can happen up to two times per trapezoid.

Reset LA_phase when ADV_INIT is set, since it is re-inizialized only
when needed a few lines afterward. This improves performance by avoiding
to check the phase continuosly to the end of the trapezoid.

Likewise, always set ADV_INIT during the first cruising step, also to
force a LA_phase reset.
---
 Firmware/stepper.cpp | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/Firmware/stepper.cpp b/Firmware/stepper.cpp
index de250ec9..57520070 100644
--- a/Firmware/stepper.cpp
+++ b/Firmware/stepper.cpp
@@ -848,12 +848,10 @@ FORCE_INLINE void isr() {
 
 #ifdef LIN_ADVANCE
           if(current_block->use_advance_lead) {
-              if (!nextAdvanceISR) {
-                  // Due to E-jerk, there can be discontinuities in pressure state where an
-                  // acceleration or deceleration can be skipped or joined with the previous block.
-                  // If LA was not previously active, re-check the pressure level
-                  la_state = ADV_INIT;
-              }
+              // Due to E-jerk, there can be discontinuities in pressure state where an
+              // acceleration or deceleration can be skipped or joined with the previous block.
+              // If LA was not previously active, re-check the pressure level
+              la_state = ADV_INIT;
           }
 #endif
         }
@@ -865,6 +863,7 @@ FORCE_INLINE void isr() {
 #ifdef LIN_ADVANCE
     // avoid multiple instances or function calls to advance_spread
     if (la_state & ADV_INIT) {
+        LA_phase = -1;
         if (current_adv_steps == target_adv_steps) {
             // nothing to be done in this phase
             la_state = 0;