Marlin-RC_Deltaprintr/Marlin/stepper.cpp
// "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
// It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
"The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
" The Stepper Driver Interrupt " - This timer interrupt is the workhorse . It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately .
ISR(TIMER1_COMPA_vect) {
  if (cleaning_buffer_counter) {
    current_block = NULL;
    plan_discard_current_block();
    #ifdef SD_FINISHED_RELEASECOMMAND
      if ((cleaning_buffer_counter == 1) && (SD_FINISHED_STEPPERRELEASE)) enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
    #endif
    cleaning_buffer_counter--;
    OCR1A = 200;
    return;
  }
  if (!current_block) {
    current_block = plan_get_current_block();
    if (current_block) {
      current_block->busy = true;
      trapezoid_generator_reset();
      counter_x = -(current_block->step_event_count >> 1);
      counter_y = counter_z = counter_e = counter_x;
      step_events_completed = 0;
      #if ENABLED(Z_LATE_ENABLE)
        if (current_block->steps[Z_AXIS] > 0) {
          enable_z();
          OCR1A = 2000; 
          return;
        }
      #endif
    }
    else {
      OCR1A = 2000; 
    }
  }
  if (current_block != NULL) {
    #if ENABLED(HAS_Z_MIN_PROBE)
      if (check_endstops || z_probe_is_active) update_endstops();
    #else
      if (check_endstops) update_endstops();
    #endif
    for (int8_t i = 0; i < step_loops; i++) {
      #ifndef USBCON
        customizedSerial.checkRx(); 
      #endif
      #if ENABLED(ADVANCE)
        counter_e += current_block->steps[E_AXIS];
        if (counter_e > 0) {
          counter_e -= current_block->step_event_count;
          e_steps[current_block->active_extruder] += TEST(out_bits, E_AXIS) ? -1 : 1;
        }
      #endif 
      #define _COUNTER(axis) counter_## axis
      #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
      #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
      #define STEP_ADD(axis, AXIS) \
        _COUNTER(axis) += current_block->steps[_AXIS(AXIS)]; \
        if (_COUNTER(axis) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS),0); }
      STEP_ADD(x,X);
      STEP_ADD(y,Y);
      STEP_ADD(z,Z);
      #if DISABLED(ADVANCE)
        STEP_ADD(e,E);
      #endif
      #define STEP_IF_COUNTER(axis, AXIS) \
        if (_COUNTER(axis) > 0) { \
          _COUNTER(axis) -= current_block->step_event_count; \
          count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
          _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS),0); \
        }
      STEP_IF_COUNTER(x, X);
      STEP_IF_COUNTER(y, Y);
      STEP_IF_COUNTER(z, Z);
      #if DISABLED(ADVANCE)
        STEP_IF_COUNTER(e, E);
      #endif
      step_events_completed++;
      if (step_events_completed >= current_block->step_event_count) break;
    }
    unsigned short timer;
    unsigned short step_rate;
    if (step_events_completed <= (unsigned long)current_block->accelerate_until) {
      MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
      acc_step_rate += current_block->initial_rate;
      NOMORE(acc_step_rate, current_block->nominal_rate);
      timer = calc_timer(acc_step_rate);
      OCR1A = timer;
      acceleration_time += timer;
      #if ENABLED(ADVANCE)
        advance += advance_rate * step_loops;
        e_steps[current_block->active_extruder] += ((advance >> 8) - old_advance);
        old_advance = advance >> 8;
      #endif 
    }
    else if (step_events_completed > (unsigned long)current_block->decelerate_after) {
      MultiU24X32toH16(step_rate, deceleration_time, current_block->acceleration_rate);
      if (step_rate <= acc_step_rate) { 
        step_rate = acc_step_rate - step_rate;
        NOLESS(step_rate, current_block->final_rate);
      }
      else
        step_rate = current_block->final_rate;
      timer = calc_timer(step_rate);
      OCR1A = timer;
      deceleration_time += timer;
      #if ENABLED(ADVANCE)
        advance -= advance_rate * step_loops;
        NOLESS(advance, final_advance);
        uint32_t advance_whole = advance >> 8;
        e_steps[current_block->active_extruder] += advance_whole - old_advance;
        old_advance = advance_whole;
      #endif 
    }
    else {
      OCR1A = OCR1A_nominal;
      step_loops = step_loops_nominal;
    }
    OCR1A = (OCR1A < (TCNT1 + 16)) ? (TCNT1 + 16) : OCR1A;
    if (step_events_completed >= current_block->step_event_count) {
      current_block = NULL;
      plan_discard_current_block();
    }
  }
}
ISR ( TIMER1_COMPA_vect ) { if ( cleaning_buffer_counter ) { current_block = NULL ; plan_discard_current_block ( ) ; #ifdef SD_FINISHED_RELEASECOMMAND if ( ( cleaning_buffer_counter == 1 ) && ( SD_FINISHED_STEPPERRELEASE ) ) enqueue_and_echo_commands_P ( PSTR ( SD_FINISHED_RELEASECOMMAND ) ) ; #endif cleaning_buffer_counter -- ; OCR1A = 200 ; return ; } if ( ! current_block ) { current_block = plan_get_current_block ( ) ; if ( current_block ) { current_block -> busy = true ; trapezoid_generator_reset ( ) ; counter_x = - ( current_block -> step_event_count >> 1 ) ; counter_y = counter_z = counter_e = counter_x ; step_events_completed = 0 ; #if ENABLED ( Z_LATE_ENABLE ) if ( current_block -> steps [ Z_AXIS ] > 0 ) { enable_z ( ) ; OCR1A = 2000 ; return ; } #endif } else { OCR1A = 2000 ; } } if ( current_block != NULL ) { #if ENABLED ( HAS_Z_MIN_PROBE ) if ( check_endstops || z_probe_is_active ) update_endstops ( ) ; #else if ( check_endstops ) update_endstops ( ) ; #endif for ( int8_t i = 0 ; i < step_loops ; i ++ ) { #ifndef USBCON customizedSerial . checkRx ( ) ; #endif #if ENABLED ( ADVANCE ) counter_e += current_block -> steps [ E_AXIS ] ; if ( counter_e > 0 ) { counter_e -= current_block -> step_event_count ; e_steps [ current_block -> active_extruder ] += TEST ( out_bits , E_AXIS ) ? -1 : 1 ; } #endif #define _COUNTER ( axis )  counter_## axis #define _APPLY_STEP ( AXIS )  AXIS ##_APPLY_STEP #define _INVERT_STEP_PIN ( AXIS )  INVERT_## AXIS ##_STEP_PIN #define STEP_ADD ( axis , AXIS )  \
        _COUNTER(axis) += current_block->steps[_AXIS(AXIS)]; \
        if (_COUNTER(axis) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS),0); } STEP_ADD ( x , X ) ; STEP_ADD ( y , Y ) ; STEP_ADD ( z , Z ) ; #if DISABLED ( ADVANCE ) STEP_ADD ( e , E ) ; #endif #define STEP_IF_COUNTER ( axis , AXIS )  \
        if (_COUNTER(axis) > 0) { \
          _COUNTER(axis) -= current_block->step_event_count; \
          count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
          _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS),0); \
        } STEP_IF_COUNTER ( x , X ) ; STEP_IF_COUNTER ( y , Y ) ; STEP_IF_COUNTER ( z , Z ) ; #if DISABLED ( ADVANCE ) STEP_IF_COUNTER ( e , E ) ; #endif step_events_completed ++ ; if ( step_events_completed >= current_block -> step_event_count ) break ; } unsigned short timer ; unsigned short step_rate ; if ( step_events_completed <= ( unsigned long ) current_block -> accelerate_until ) { MultiU24X32toH16 ( acc_step_rate , acceleration_time , current_block -> acceleration_rate ) ; acc_step_rate += current_block -> initial_rate ; NOMORE ( acc_step_rate , current_block -> nominal_rate ) ; timer = calc_timer ( acc_step_rate ) ; OCR1A = timer ; acceleration_time += timer ; #if ENABLED ( ADVANCE ) advance += advance_rate * step_loops ; e_steps [ current_block -> active_extruder ] += ( ( advance >> 8 ) - old_advance ) ; old_advance = advance >> 8 ; #endif } else if ( step_events_completed > ( unsigned long ) current_block -> decelerate_after ) { MultiU24X32toH16 ( step_rate , deceleration_time , current_block -> acceleration_rate ) ; if ( step_rate <= acc_step_rate ) { step_rate = acc_step_rate - step_rate ; NOLESS ( step_rate , current_block -> final_rate ) ; } else step_rate = current_block -> final_rate ; timer = calc_timer ( step_rate ) ; OCR1A = timer ; deceleration_time += timer ; #if ENABLED ( ADVANCE ) advance -= advance_rate * step_loops ; NOLESS ( advance , final_advance ) ; uint32_t advance_whole = advance >> 8 ; e_steps [ current_block -> active_extruder ] += advance_whole - old_advance ; old_advance = advance_whole ; #endif } else { OCR1A = OCR1A_nominal ; step_loops = step_loops_nominal ; } OCR1A = ( OCR1A < ( TCNT1 + 16 ) ) ? ( TCNT1 + 16 ) : OCR1A ; if ( step_events_completed >= current_block -> step_event_count ) { current_block = NULL ; plan_discard_current_block ( ) ; } } }
"The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
" The Stepper Driver Interrupt " - This timer interrupt is the workhorse .
// If there is no current block, attempt to pop one from the buffer // Anything in the buffer? //1ms wait // #if ENABLED(ADVANCE) //   e_steps[current_block->active_extruder] = 0; // #endif // 1kHz. // Update endstops state, if enabled // Take multiple steps per interrupt (For high speed moves) // Check for serial chars. //ADVANCE // Calculate new timer value // upper limit // step_rate to timer interval //NOLESS(advance, current_block->advance); // Do E steps + advance steps //ADVANCE // Still decelerating? // step_rate to timer interval // Do E steps + advance steps //ADVANCE // ensure we're running at the correct step rate, even if we just came off an acceleration // If current block is finished, reset pointer