diff --git a/Marlin/stepper.cpp b/Marlin/stepper.cpp index e085206ef..a74c317e8 100644 --- a/Marlin/stepper.cpp +++ b/Marlin/stepper.cpp @@ -83,7 +83,7 @@ block_t* Stepper::current_block = NULL; // A pointer to the block currently bei // private: uint8_t Stepper::last_direction_bits = 0; // The next stepping-bits to be output -uint16_t Stepper::cleaning_buffer_counter = 0; +int16_t Stepper::cleaning_buffer_counter = 0; #if ENABLED(X_DUAL_ENDSTOPS) bool Stepper::locked_x_motor = false, Stepper::locked_x2_motor = false; @@ -381,8 +381,8 @@ void Stepper::isr() { uint16_t ocr_val; - #define ENDSTOP_NOMINAL_OCR_VAL 3000 // check endstops every 1.5ms to guarantee two stepper ISRs within 5ms for BLTouch - #define OCR_VAL_TOLERANCE 1000 // First max delay is 2.0ms, last min delay is 0.5ms, all others 1.5ms + #define ENDSTOP_NOMINAL_OCR_VAL 3000 // Check endstops every 1.5ms to guarantee two stepper ISRs within 5ms for BLTouch + #define OCR_VAL_TOLERANCE 1000 // First max delay is 2.0ms, last min delay is 0.5ms, all others 1.5ms #if DISABLED(LIN_ADVANCE) // Disable Timer0 ISRs and enable global ISR again to capture UART events (incoming chars) @@ -393,9 +393,13 @@ void Stepper::isr() { #define _SPLIT(L) (ocr_val = (uint16_t)L) #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE) + #define SPLIT(L) _SPLIT(L) - #else // sample endstops in between step pulses + + #else // !ENDSTOP_INTERRUPTS_FEATURE : Sample endstops between stepping ISRs + static uint32_t step_remaining = 0; + #define SPLIT(L) do { \ _SPLIT(L); \ if (ENDSTOPS_ENABLED && L > ENDSTOP_NOMINAL_OCR_VAL) { \ @@ -407,33 +411,34 @@ void Stepper::isr() { if (step_remaining && ENDSTOPS_ENABLED) { // Just check endstops - not yet time for a step endstops.update(); - if (step_remaining > ENDSTOP_NOMINAL_OCR_VAL) { - step_remaining -= ENDSTOP_NOMINAL_OCR_VAL; - ocr_val = ENDSTOP_NOMINAL_OCR_VAL; - } - else { - ocr_val = step_remaining; - step_remaining = 0; // last one before the ISR that does the step - } + // Next ISR either for endstops or stepping + ocr_val = step_remaining <= ENDSTOP_NOMINAL_OCR_VAL ? step_remaining : ENDSTOP_NOMINAL_OCR_VAL; + step_remaining -= ocr_val; _NEXT_ISR(ocr_val); - NOLESS(OCR1A, TCNT1 + 16); - _ENABLE_ISRs(); // re-enable ISRs return; } - #endif + #endif // !ENDSTOP_INTERRUPTS_FEATURE + + // + // When cleaning, discard the current block and run fast + // if (cleaning_buffer_counter) { - --cleaning_buffer_counter; current_block = NULL; planner.discard_current_block(); - #ifdef SD_FINISHED_RELEASECOMMAND - if (!cleaning_buffer_counter && (SD_FINISHED_STEPPERRELEASE)) enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND)); - #endif - _NEXT_ISR(200); // Run at max speed - 10 KHz - _ENABLE_ISRs(); // re-enable ISRs + if (cleaning_buffer_counter < 0) + ++cleaning_buffer_counter; // Count up for endstop hit + else { + --cleaning_buffer_counter; // Count down for abort print + #ifdef SD_FINISHED_RELEASECOMMAND + if (!cleaning_buffer_counter && (SD_FINISHED_STEPPERRELEASE)) enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND)); + #endif + } + _NEXT_ISR(200); // Run at max speed - 10 KHz + _ENABLE_ISRs(); return; } diff --git a/Marlin/stepper.h b/Marlin/stepper.h index 7724eed14..a19810376 100644 --- a/Marlin/stepper.h +++ b/Marlin/stepper.h @@ -104,7 +104,7 @@ class Stepper { private: static uint8_t last_direction_bits; // The next stepping-bits to be output - static uint16_t cleaning_buffer_counter; + static int16_t cleaning_buffer_counter; #if ENABLED(X_DUAL_ENDSTOPS) static bool locked_x_motor, locked_x2_motor;