diff --git a/Marlin/stepper.cpp b/Marlin/stepper.cpp index 7b00da34e..e614f5a89 100644 --- a/Marlin/stepper.cpp +++ b/Marlin/stepper.cpp @@ -46,7 +46,7 @@ block_t *current_block; // A pointer to the block currently being traced //static makes it impossible to be called from outside of this file by extern.! // Variables used by The Stepper Driver Interrupt -static unsigned char out_bits; // The next stepping-bits to be output +static unsigned char out_bits = 0; // The next stepping-bits to be output static unsigned int cleaning_buffer_counter; #ifdef Z_DUAL_ENDSTOPS @@ -364,9 +364,58 @@ FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) { return timer; } +// set the stepper direction of each axis +void set_stepper_direction() { + + // Set the direction bits (X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY) + if (TEST(out_bits, X_AXIS)) { + X_APPLY_DIR(INVERT_X_DIR,0); + count_direction[X_AXIS] = -1; + } + else { + X_APPLY_DIR(!INVERT_X_DIR,0); + count_direction[X_AXIS] = 1; + } + + if (TEST(out_bits, Y_AXIS)) { + Y_APPLY_DIR(INVERT_Y_DIR,0); + count_direction[Y_AXIS] = -1; + } + else { + Y_APPLY_DIR(!INVERT_Y_DIR,0); + count_direction[Y_AXIS] = 1; + } + + if (TEST(out_bits, Z_AXIS)) { + Z_APPLY_DIR(INVERT_Z_DIR,0); + count_direction[Z_AXIS] = -1; + } + else { + Z_APPLY_DIR(!INVERT_Z_DIR,0); + count_direction[Z_AXIS] = 1; + } + + #ifndef ADVANCE + if (TEST(out_bits, E_AXIS)) { + REV_E_DIR(); + count_direction[E_AXIS] = -1; + } + else { + NORM_E_DIR(); + count_direction[E_AXIS] = 1; + } + #endif //!ADVANCE +} + // Initializes the trapezoid generator from the current block. Called whenever a new // block begins. FORCE_INLINE void trapezoid_generator_reset() { + + if (current_block->direction_bits != out_bits) { + out_bits = current_block->direction_bits; + set_stepper_direction(); + } + #ifdef ADVANCE advance = current_block->initial_advance; final_advance = current_block->final_advance; @@ -439,48 +488,27 @@ ISR(TIMER1_COMPA_vect) { } if (current_block != NULL) { - // Set directions TO DO This should be done once during init of trapezoid. Endstops -> interrupt - out_bits = current_block->direction_bits; - - // Set the direction bits (X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY) - if (TEST(out_bits, X_AXIS)) { - X_APPLY_DIR(INVERT_X_DIR,0); - count_direction[X_AXIS] = -1; - } - else { - X_APPLY_DIR(!INVERT_X_DIR,0); - count_direction[X_AXIS] = 1; - } - - if (TEST(out_bits, Y_AXIS)) { - Y_APPLY_DIR(INVERT_Y_DIR,0); - count_direction[Y_AXIS] = -1; - } - else { - Y_APPLY_DIR(!INVERT_Y_DIR,0); - count_direction[Y_AXIS] = 1; - } - - #define _ENDSTOP(axis, minmax) axis ##_## minmax ##_endstop - #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN - #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING - #define _OLD_ENDSTOP(axis, minmax) old_## axis ##_## minmax ##_endstop - #define _AXIS(AXIS) AXIS ##_AXIS - #define _HIT_BIT(AXIS) AXIS ##_MIN - #define _ENDSTOP_HIT(AXIS) endstop_hit_bits |= BIT(_HIT_BIT(AXIS)) - - #define UPDATE_ENDSTOP(axis,AXIS,minmax,MINMAX) \ - bool _ENDSTOP(axis, minmax) = (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)); \ - if (_ENDSTOP(axis, minmax) && _OLD_ENDSTOP(axis, minmax) && (current_block->steps[_AXIS(AXIS)] > 0)) { \ - endstops_trigsteps[_AXIS(AXIS)] = count_position[_AXIS(AXIS)]; \ - _ENDSTOP_HIT(AXIS); \ - step_events_completed = current_block->step_event_count; \ - } \ - _OLD_ENDSTOP(axis, minmax) = _ENDSTOP(axis, minmax); - - // Check X and Y endstops + // Check endstops if (check_endstops) { + + #define _ENDSTOP(axis, minmax) axis ##_## minmax ##_endstop + #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN + #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING + #define _OLD_ENDSTOP(axis, minmax) old_## axis ##_## minmax ##_endstop + #define _AXIS(AXIS) AXIS ##_AXIS + #define _HIT_BIT(AXIS) AXIS ##_MIN + #define _ENDSTOP_HIT(AXIS) endstop_hit_bits |= BIT(_HIT_BIT(AXIS)) + + #define UPDATE_ENDSTOP(axis,AXIS,minmax,MINMAX) \ + bool _ENDSTOP(axis, minmax) = (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)); \ + if (_ENDSTOP(axis, minmax) && _OLD_ENDSTOP(axis, minmax) && (current_block->steps[_AXIS(AXIS)] > 0)) { \ + endstops_trigsteps[_AXIS(AXIS)] = count_position[_AXIS(AXIS)]; \ + _ENDSTOP_HIT(AXIS); \ + step_events_completed = current_block->step_event_count; \ + } \ + _OLD_ENDSTOP(axis, minmax) = _ENDSTOP(axis, minmax); + #ifdef COREXY // Head direction in -X axis for CoreXY bots. // If DeltaX == -DeltaY, the movement is only in Y axis @@ -533,15 +561,7 @@ ISR(TIMER1_COMPA_vect) { #ifdef COREXY } #endif - } - - if (TEST(out_bits, Z_AXIS)) { // -direction - - Z_APPLY_DIR(INVERT_Z_DIR,0); - count_direction[Z_AXIS] = -1; - - if (check_endstops) { - + if (TEST(out_bits, Z_AXIS)) { // z -direction #if HAS_Z_MIN #ifdef Z_DUAL_ENDSTOPS @@ -581,22 +601,12 @@ ISR(TIMER1_COMPA_vect) { { endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS]; endstop_hit_bits |= BIT(Z_PROBE); - -// if (z_probe_endstop && old_z_probe_endstop) SERIAL_ECHOLN("z_probe_endstop = true"); + // if (z_probe_endstop && old_z_probe_endstop) SERIAL_ECHOLN("z_probe_endstop = true"); } old_z_probe_endstop = z_probe_endstop; #endif - - } // check_endstops - - } - else { // +direction - - Z_APPLY_DIR(!INVERT_Z_DIR,0); - count_direction[Z_AXIS] = 1; - - if (check_endstops) { - + } + else { // z +direction #if HAS_Z_MAX #ifdef Z_DUAL_ENDSTOPS @@ -632,7 +642,7 @@ ISR(TIMER1_COMPA_vect) { #endif // !Z_DUAL_ENDSTOPS #endif // Z_MAX_PIN - + #ifdef Z_PROBE_ENDSTOP UPDATE_ENDSTOP(z, Z, probe, PROBE); z_probe_endstop=(READ(Z_PROBE_PIN) != Z_PROBE_ENDSTOP_INVERTING); @@ -640,25 +650,15 @@ ISR(TIMER1_COMPA_vect) { { endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS]; endstop_hit_bits |= BIT(Z_PROBE); -// if (z_probe_endstop && old_z_probe_endstop) SERIAL_ECHOLN("z_probe_endstop = true"); +// if (z_probe_endstop && old_z_probe_endstop) SERIAL_ECHOLN("z_probe_endstop = true"); } old_z_probe_endstop = z_probe_endstop; #endif + } - } // check_endstops + } - } // +direction - #ifndef ADVANCE - if (TEST(out_bits, E_AXIS)) { // -direction - REV_E_DIR(); - count_direction[E_AXIS] = -1; - } - else { // +direction - NORM_E_DIR(); - count_direction[E_AXIS] = 1; - } - #endif //!ADVANCE // Take multiple steps per interrupt (For high speed moves) for (int8_t i = 0; i < step_loops; i++) { @@ -989,12 +989,12 @@ void st_init() { #endif #endif -#if (defined(Z_PROBE_PIN) && Z_PROBE_PIN >= 0) && defined(Z_PROBE_ENDSTOP) // Check for Z_PROBE_ENDSTOP so we don't pull a pin high unless it's to be used. - SET_INPUT(Z_PROBE_PIN); - #ifdef ENDSTOPPULLUP_ZPROBE - WRITE(Z_PROBE_PIN,HIGH); + #if (defined(Z_PROBE_PIN) && Z_PROBE_PIN >= 0) && defined(Z_PROBE_ENDSTOP) // Check for Z_PROBE_ENDSTOP so we don't pull a pin high unless it's to be used. + SET_INPUT(Z_PROBE_PIN); + #ifdef ENDSTOPPULLUP_ZPROBE + WRITE(Z_PROBE_PIN,HIGH); + #endif #endif -#endif #define _STEP_INIT(AXIS) AXIS ##_STEP_INIT #define _WRITE_STEP(AXIS, HIGHLOW) AXIS ##_STEP_WRITE(HIGHLOW) @@ -1073,6 +1073,8 @@ void st_init() { enable_endstops(true); // Start with endstops active. After homing they can be disabled sei(); + + set_stepper_direction(); // Init directions to out_bits = 0 }