diff --git a/Marlin/stepper.cpp b/Marlin/stepper.cpp index 2d10820823..5533286bd7 100644 --- a/Marlin/stepper.cpp +++ b/Marlin/stepper.cpp @@ -37,7 +37,7 @@ //=========================================================================== //============================= public variables ============================ //=========================================================================== -block_t *current_block; // A pointer to the block currently being traced +block_t* current_block; // A pointer to the block currently being traced //=========================================================================== @@ -81,7 +81,7 @@ static volatile char endstop_hit_bits = 0; // use X_MIN, Y_MIN, Z_MIN and Z_MIN_ #else static uint16_t #endif - old_endstop_bits = 0; // use X_MIN, X_MAX... Z_MAX, Z_MIN_PROBE, Z2_MIN, Z2_MAX + old_endstop_bits = 0; // use X_MIN, X_MAX... Z_MAX, Z_MIN_PROBE, Z2_MIN, Z2_MAX #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) bool abort_on_endstop_hit = false; @@ -163,24 +163,24 @@ volatile signed char count_direction[NUM_AXIS] = { 1, 1, 1, 1 }; // r27 to store the byte 1 of the 24 bit result #define MultiU16X8toH16(intRes, charIn1, intIn2) \ asm volatile ( \ - "clr r26 \n\t" \ - "mul %A1, %B2 \n\t" \ - "movw %A0, r0 \n\t" \ - "mul %A1, %A2 \n\t" \ - "add %A0, r1 \n\t" \ - "adc %B0, r26 \n\t" \ - "lsr r0 \n\t" \ - "adc %A0, r26 \n\t" \ - "adc %B0, r26 \n\t" \ - "clr r1 \n\t" \ - : \ - "=&r" (intRes) \ - : \ - "d" (charIn1), \ - "d" (intIn2) \ - : \ - "r26" \ - ) + "clr r26 \n\t" \ + "mul %A1, %B2 \n\t" \ + "movw %A0, r0 \n\t" \ + "mul %A1, %A2 \n\t" \ + "add %A0, r1 \n\t" \ + "adc %B0, r26 \n\t" \ + "lsr r0 \n\t" \ + "adc %A0, r26 \n\t" \ + "adc %B0, r26 \n\t" \ + "clr r1 \n\t" \ + : \ + "=&r" (intRes) \ + : \ + "d" (charIn1), \ + "d" (intIn2) \ + : \ + "r26" \ + ) // intRes = longIn1 * longIn2 >> 24 // uses: @@ -194,49 +194,49 @@ volatile signed char count_direction[NUM_AXIS] = { 1, 1, 1, 1 }; // #define MultiU24X32toH16(intRes, longIn1, longIn2) \ asm volatile ( \ - "clr r26 \n\t" \ - "mul %A1, %B2 \n\t" \ - "mov r27, r1 \n\t" \ - "mul %B1, %C2 \n\t" \ - "movw %A0, r0 \n\t" \ - "mul %C1, %C2 \n\t" \ - "add %B0, r0 \n\t" \ - "mul %C1, %B2 \n\t" \ - "add %A0, r0 \n\t" \ - "adc %B0, r1 \n\t" \ - "mul %A1, %C2 \n\t" \ - "add r27, r0 \n\t" \ - "adc %A0, r1 \n\t" \ - "adc %B0, r26 \n\t" \ - "mul %B1, %B2 \n\t" \ - "add r27, r0 \n\t" \ - "adc %A0, r1 \n\t" \ - "adc %B0, r26 \n\t" \ - "mul %C1, %A2 \n\t" \ - "add r27, r0 \n\t" \ - "adc %A0, r1 \n\t" \ - "adc %B0, r26 \n\t" \ - "mul %B1, %A2 \n\t" \ - "add r27, r1 \n\t" \ - "adc %A0, r26 \n\t" \ - "adc %B0, r26 \n\t" \ - "lsr r27 \n\t" \ - "adc %A0, r26 \n\t" \ - "adc %B0, r26 \n\t" \ - "mul %D2, %A1 \n\t" \ - "add %A0, r0 \n\t" \ - "adc %B0, r1 \n\t" \ - "mul %D2, %B1 \n\t" \ - "add %B0, r0 \n\t" \ - "clr r1 \n\t" \ - : \ - "=&r" (intRes) \ - : \ - "d" (longIn1), \ - "d" (longIn2) \ - : \ - "r26" , "r27" \ - ) + "clr r26 \n\t" \ + "mul %A1, %B2 \n\t" \ + "mov r27, r1 \n\t" \ + "mul %B1, %C2 \n\t" \ + "movw %A0, r0 \n\t" \ + "mul %C1, %C2 \n\t" \ + "add %B0, r0 \n\t" \ + "mul %C1, %B2 \n\t" \ + "add %A0, r0 \n\t" \ + "adc %B0, r1 \n\t" \ + "mul %A1, %C2 \n\t" \ + "add r27, r0 \n\t" \ + "adc %A0, r1 \n\t" \ + "adc %B0, r26 \n\t" \ + "mul %B1, %B2 \n\t" \ + "add r27, r0 \n\t" \ + "adc %A0, r1 \n\t" \ + "adc %B0, r26 \n\t" \ + "mul %C1, %A2 \n\t" \ + "add r27, r0 \n\t" \ + "adc %A0, r1 \n\t" \ + "adc %B0, r26 \n\t" \ + "mul %B1, %A2 \n\t" \ + "add r27, r1 \n\t" \ + "adc %A0, r26 \n\t" \ + "adc %B0, r26 \n\t" \ + "lsr r27 \n\t" \ + "adc %A0, r26 \n\t" \ + "adc %B0, r26 \n\t" \ + "mul %D2, %A1 \n\t" \ + "add %A0, r0 \n\t" \ + "adc %B0, r1 \n\t" \ + "mul %D2, %B1 \n\t" \ + "add %B0, r0 \n\t" \ + "clr r1 \n\t" \ + : \ + "=&r" (intRes) \ + : \ + "d" (longIn1), \ + "d" (longIn2) \ + : \ + "r26" , "r27" \ + ) // Some useful constants @@ -328,7 +328,7 @@ inline void update_endstops() { if ((current_block->steps[A_AXIS] != current_block->steps[C_AXIS]) || (TEST(out_bits, A_AXIS) == TEST(out_bits, C_AXIS))) { if (TEST(out_bits, X_HEAD)) #else - if (TEST(out_bits, X_AXIS)) // stepping along -X axis (regular Cartesian bot) + if (TEST(out_bits, X_AXIS)) // stepping along -X axis (regular Cartesian bot) #endif { // -direction #if ENABLED(DUAL_X_CARRIAGE) @@ -391,11 +391,11 @@ inline void update_endstops() { #if ENABLED(Z_DUAL_ENDSTOPS) SET_ENDSTOP_BIT(Z, MIN); - #if HAS_Z2_MIN - SET_ENDSTOP_BIT(Z2, MIN); - #else - COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN); - #endif + #if HAS_Z2_MIN + SET_ENDSTOP_BIT(Z2, MIN); + #else + COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN); + #endif byte z_test = TEST_ENDSTOP(Z_MIN) << 0 + TEST_ENDSTOP(Z2_MIN) << 1; // bit 0 for Z, bit 1 for Z2 @@ -408,14 +408,14 @@ inline void update_endstops() { #else // !Z_DUAL_ENDSTOPS UPDATE_ENDSTOP(Z, MIN); + #endif // !Z_DUAL_ENDSTOPS #endif // Z_MIN_PIN #if ENABLED(Z_MIN_PROBE_ENDSTOP) UPDATE_ENDSTOP(Z, MIN_PROBE); - if (TEST_ENDSTOP(Z_MIN_PROBE)) - { + if (TEST_ENDSTOP(Z_MIN_PROBE)) { endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS]; endstop_hit_bits |= BIT(Z_MIN_PROBE); } @@ -427,11 +427,11 @@ inline void update_endstops() { #if ENABLED(Z_DUAL_ENDSTOPS) SET_ENDSTOP_BIT(Z, MAX); - #if HAS_Z2_MAX - SET_ENDSTOP_BIT(Z2, MAX); - #else - COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX); - #endif + #if HAS_Z2_MAX + SET_ENDSTOP_BIT(Z2, MAX); + #else + COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX); + #endif byte z_test = TEST_ENDSTOP(Z_MAX) << 0 + TEST_ENDSTOP(Z2_MAX) << 1; // bit 0 for Z, bit 1 for Z2 @@ -451,7 +451,7 @@ inline void update_endstops() { } #if ENABLED(COREXZ) } - #endif + #endif old_endstop_bits = current_endstop_bits; } @@ -495,17 +495,17 @@ FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) { if (step_rate < (F_CPU / 500000)) step_rate = (F_CPU / 500000); step_rate -= (F_CPU / 500000); // Correct for minimal speed if (step_rate >= (8 * 256)) { // higher step rate - unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate>>8)][0]; + unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate >> 8)][0]; unsigned char tmp_step_rate = (step_rate & 0x00ff); - unsigned short gain = (unsigned short)pgm_read_word_near(table_address+2); + unsigned short gain = (unsigned short)pgm_read_word_near(table_address + 2); MultiU16X8toH16(timer, tmp_step_rate, gain); timer = (unsigned short)pgm_read_word_near(table_address) - timer; } else { // lower step rates unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0]; - table_address += ((step_rate)>>1) & 0xfffc; + table_address += ((step_rate) >> 1) & 0xfffc; timer = (unsigned short)pgm_read_word_near(table_address); - timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3); + timer -= (((unsigned short)pgm_read_word_near(table_address + 2) * (unsigned char)(step_rate & 0x0007)) >> 3); } if (timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TOO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen) return timer; @@ -704,16 +704,18 @@ ISR(TIMER1_COMPA_vect) { timer = calc_timer(acc_step_rate); OCR1A = timer; acceleration_time += timer; + #if ENABLED(ADVANCE) - for(int8_t i=0; i < step_loops; i++) { + + for (int8_t i = 0; i < step_loops; i++) { advance += advance_rate; } //if (advance > current_block->advance) advance = current_block->advance; // Do E steps + advance steps - e_steps[current_block->active_extruder] += ((advance >>8) - old_advance); - old_advance = advance >>8; + e_steps[current_block->active_extruder] += ((advance >> 8) - old_advance); + old_advance = advance >> 8; - #endif + #endif //ADVANCE } else if (step_events_completed > (unsigned long)current_block->decelerate_after) { MultiU24X32toH16(step_rate, deceleration_time, current_block->acceleration_rate); @@ -734,13 +736,13 @@ ISR(TIMER1_COMPA_vect) { OCR1A = timer; deceleration_time += timer; #if ENABLED(ADVANCE) - for(int8_t i=0; i < step_loops; i++) { + for (int8_t i = 0; i < step_loops; i++) { advance -= advance_rate; } if (advance < final_advance) advance = final_advance; // Do E steps + advance steps - e_steps[current_block->active_extruder] += ((advance >>8) - old_advance); - old_advance = advance >>8; + e_steps[current_block->active_extruder] += ((advance >> 8) - old_advance); + old_advance = advance >> 8; #endif //ADVANCE } else { @@ -749,7 +751,7 @@ ISR(TIMER1_COMPA_vect) { step_loops = step_loops_nominal; } - OCR1A = (OCR1A < (TCNT1 +16)) ? (TCNT1 + 16) : OCR1A; + OCR1A = (OCR1A < (TCNT1 + 16)) ? (TCNT1 + 16) : OCR1A; // If current block is finished, reset pointer if (step_events_completed >= current_block->step_event_count) { @@ -763,12 +765,11 @@ ISR(TIMER1_COMPA_vect) { unsigned char old_OCR0A; // Timer interrupt for E. e_steps is set in the main routine; // Timer 0 is shared with millies - ISR(TIMER0_COMPA_vect) - { + ISR(TIMER0_COMPA_vect) { old_OCR0A += 52; // ~10kHz interrupt (250000 / 26 = 9615kHz) OCR0A = old_OCR0A; // Set E direction (Depends on E direction + advance) - for(unsigned char i=0; i<4;i++) { + for (unsigned char i = 0; i < 4; i++) { if (e_steps[0] != 0) { E0_STEP_WRITE(INVERT_E_STEP_PIN); if (e_steps[0] < 0) { @@ -782,52 +783,51 @@ ISR(TIMER1_COMPA_vect) { E0_STEP_WRITE(!INVERT_E_STEP_PIN); } } - #if EXTRUDERS > 1 - if (e_steps[1] != 0) { - E1_STEP_WRITE(INVERT_E_STEP_PIN); - if (e_steps[1] < 0) { - E1_DIR_WRITE(INVERT_E1_DIR); - e_steps[1]++; - E1_STEP_WRITE(!INVERT_E_STEP_PIN); + #if EXTRUDERS > 1 + if (e_steps[1] != 0) { + E1_STEP_WRITE(INVERT_E_STEP_PIN); + if (e_steps[1] < 0) { + E1_DIR_WRITE(INVERT_E1_DIR); + e_steps[1]++; + E1_STEP_WRITE(!INVERT_E_STEP_PIN); + } + else if (e_steps[1] > 0) { + E1_DIR_WRITE(!INVERT_E1_DIR); + e_steps[1]--; + E1_STEP_WRITE(!INVERT_E_STEP_PIN); + } } - else if (e_steps[1] > 0) { - E1_DIR_WRITE(!INVERT_E1_DIR); - e_steps[1]--; - E1_STEP_WRITE(!INVERT_E_STEP_PIN); + #endif + #if EXTRUDERS > 2 + if (e_steps[2] != 0) { + E2_STEP_WRITE(INVERT_E_STEP_PIN); + if (e_steps[2] < 0) { + E2_DIR_WRITE(INVERT_E2_DIR); + e_steps[2]++; + E2_STEP_WRITE(!INVERT_E_STEP_PIN); + } + else if (e_steps[2] > 0) { + E2_DIR_WRITE(!INVERT_E2_DIR); + e_steps[2]--; + E2_STEP_WRITE(!INVERT_E_STEP_PIN); + } } - } - #endif - #if EXTRUDERS > 2 - if (e_steps[2] != 0) { - E2_STEP_WRITE(INVERT_E_STEP_PIN); - if (e_steps[2] < 0) { - E2_DIR_WRITE(INVERT_E2_DIR); - e_steps[2]++; - E2_STEP_WRITE(!INVERT_E_STEP_PIN); + #endif + #if EXTRUDERS > 3 + if (e_steps[3] != 0) { + E3_STEP_WRITE(INVERT_E_STEP_PIN); + if (e_steps[3] < 0) { + E3_DIR_WRITE(INVERT_E3_DIR); + e_steps[3]++; + E3_STEP_WRITE(!INVERT_E_STEP_PIN); + } + else if (e_steps[3] > 0) { + E3_DIR_WRITE(!INVERT_E3_DIR); + e_steps[3]--; + E3_STEP_WRITE(!INVERT_E_STEP_PIN); + } } - else if (e_steps[2] > 0) { - E2_DIR_WRITE(!INVERT_E2_DIR); - e_steps[2]--; - E2_STEP_WRITE(!INVERT_E_STEP_PIN); - } - } - #endif - #if EXTRUDERS > 3 - if (e_steps[3] != 0) { - E3_STEP_WRITE(INVERT_E_STEP_PIN); - if (e_steps[3] < 0) { - E3_DIR_WRITE(INVERT_E3_DIR); - e_steps[3]++; - E3_STEP_WRITE(!INVERT_E_STEP_PIN); - } - else if (e_steps[3] > 0) { - E3_DIR_WRITE(!INVERT_E3_DIR); - e_steps[3]--; - E3_STEP_WRITE(!INVERT_E_STEP_PIN); - } - } - #endif - + #endif } } #endif // ADVANCE @@ -1032,15 +1032,14 @@ void st_init() { TCCR1A &= ~BIT(WGM10); // output mode = 00 (disconnected) - TCCR1A &= ~(3<= 0) switch(driver) { + if (ms1 >= 0) switch (driver) { case 0: digitalWrite(X_MS1_PIN, ms1); break; case 1: digitalWrite(Y_MS1_PIN, ms1); break; case 2: digitalWrite(Z_MS1_PIN, ms1); break; @@ -1264,7 +1263,7 @@ void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2) { case 4: digitalWrite(E1_MS1_PIN, ms1); break; #endif } - if (ms2 >= 0) switch(driver) { + if (ms2 >= 0) switch (driver) { case 0: digitalWrite(X_MS2_PIN, ms2); break; case 1: digitalWrite(Y_MS2_PIN, ms2); break; case 2: digitalWrite(Z_MS2_PIN, ms2); break; @@ -1276,12 +1275,12 @@ void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2) { } void microstep_mode(uint8_t driver, uint8_t stepping_mode) { - switch(stepping_mode) { - case 1: microstep_ms(driver,MICROSTEP1); break; - case 2: microstep_ms(driver,MICROSTEP2); break; - case 4: microstep_ms(driver,MICROSTEP4); break; - case 8: microstep_ms(driver,MICROSTEP8); break; - case 16: microstep_ms(driver,MICROSTEP16); break; + switch (stepping_mode) { + case 1: microstep_ms(driver, MICROSTEP1); break; + case 2: microstep_ms(driver, MICROSTEP2); break; + case 4: microstep_ms(driver, MICROSTEP4); break; + case 8: microstep_ms(driver, MICROSTEP8); break; + case 16: microstep_ms(driver, MICROSTEP16); break; } } diff --git a/Marlin/stepper.h b/Marlin/stepper.h index 54bded4278..46211948e5 100644 --- a/Marlin/stepper.h +++ b/Marlin/stepper.h @@ -19,7 +19,7 @@ */ #ifndef stepper_h -#define stepper_h +#define stepper_h #include "planner.h" #include "stepper_indirection.h" @@ -42,7 +42,7 @@ #define E_STEP_WRITE(v) { if(extruder_duplication_enabled) { E0_STEP_WRITE(v); E1_STEP_WRITE(v); } else if(current_block->active_extruder == 1) { E1_STEP_WRITE(v); } else { E0_STEP_WRITE(v); }} #define NORM_E_DIR() { if(extruder_duplication_enabled) { E0_DIR_WRITE(!INVERT_E0_DIR); E1_DIR_WRITE(!INVERT_E1_DIR); } else if(current_block->active_extruder == 1) { E1_DIR_WRITE(!INVERT_E1_DIR); } else { E0_DIR_WRITE(!INVERT_E0_DIR); }} #define REV_E_DIR() { if(extruder_duplication_enabled) { E0_DIR_WRITE(INVERT_E0_DIR); E1_DIR_WRITE(INVERT_E1_DIR); } else if(current_block->active_extruder == 1) { E1_DIR_WRITE(INVERT_E1_DIR); } else { E0_DIR_WRITE(INVERT_E0_DIR); }} - #endif + #endif #else #define E_STEP_WRITE(v) E0_STEP_WRITE(v) #define NORM_E_DIR() E0_DIR_WRITE(!INVERT_E0_DIR) @@ -60,8 +60,8 @@ void st_init(); void st_synchronize(); // Set current position in steps -void st_set_position(const long &x, const long &y, const long &z, const long &e); -void st_set_e_position(const long &e); +void st_set_position(const long& x, const long& y, const long& z, const long& e); +void st_set_e_position(const long& e); // Get current position in steps long st_get_position(uint8_t axis); @@ -73,7 +73,7 @@ float st_get_position_mm(AxisEnum axis); // to notify the subsystem that it is time to go to work. void st_wake_up(); - + void checkHitEndstops(); //call from somewhere to create an serial error message with the locations the endstops where hit, in case they were triggered void endstops_hit_on_purpose(); //avoid creation of the message, i.e. after homing and before a routine call of checkHitEndstops(); @@ -83,7 +83,7 @@ void checkStepperErrors(); //Print errors detected by the stepper void finishAndDisableSteppers(); -extern block_t *current_block; // A pointer to the block currently being traced +extern block_t* current_block; // A pointer to the block currently being traced void quickStop(); @@ -102,7 +102,7 @@ void microstep_readings(); #endif #if ENABLED(BABYSTEPPING) - void babystep(const uint8_t axis,const bool direction); // perform a short step with a single stepper motor, outside of any convention + void babystep(const uint8_t axis, const bool direction); // perform a short step with a single stepper motor, outside of any convention #endif - + #endif