123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545 |
- #ifndef STEPPER_H
- #define STEPPER_H
- #include "MarlinConfig.h"
- #ifndef MINIMUM_STEPPER_PULSE
- #define MINIMUM_STEPPER_PULSE 0UL
- #endif
- #ifndef MAXIMUM_STEPPER_RATE
- #if MINIMUM_STEPPER_PULSE
- #define MAXIMUM_STEPPER_RATE (1000000UL / (2UL * (unsigned long)(MINIMUM_STEPPER_PULSE)))
- #else
- #define MAXIMUM_STEPPER_RATE 500000UL
- #endif
- #endif
- #define ISR_BASE_CYCLES 752UL
- #if ENABLED(LIN_ADVANCE)
- #define ISR_LA_BASE_CYCLES 32UL
- #else
- #define ISR_LA_BASE_CYCLES 0UL
- #endif
- #if ENABLED(S_CURVE_ACCELERATION)
- #define ISR_S_CURVE_CYCLES 160UL
- #else
- #define ISR_S_CURVE_CYCLES 0UL
- #endif
- #define ISR_LOOP_BASE_CYCLES 32UL
- #define ISR_START_STEPPER_CYCLES 57UL
- #define ISR_STEPPER_CYCLES 88UL
- #ifdef HAS_X_STEP
- #define ISR_START_X_STEPPER_CYCLES ISR_START_STEPPER_CYCLES
- #define ISR_X_STEPPER_CYCLES ISR_STEPPER_CYCLES
- #else
- #define ISR_START_X_STEPPER_CYCLES 0UL
- #define ISR_X_STEPPER_CYCLES 0UL
- #endif
- #ifdef HAS_Y_STEP
- #define ISR_START_Y_STEPPER_CYCLES ISR_START_STEPPER_CYCLES
- #define ISR_Y_STEPPER_CYCLES ISR_STEPPER_CYCLES
- #else
- #define ISR_START_Y_STEPPER_CYCLES 0UL
- #define ISR_Y_STEPPER_CYCLES 0UL
- #endif
- #ifdef HAS_Z_STEP
- #define ISR_START_Z_STEPPER_CYCLES ISR_START_STEPPER_CYCLES
- #define ISR_Z_STEPPER_CYCLES ISR_STEPPER_CYCLES
- #else
- #define ISR_START_Z_STEPPER_CYCLES 0UL
- #define ISR_Z_STEPPER_CYCLES 0UL
- #endif
- #define ISR_START_E_STEPPER_CYCLES ISR_START_STEPPER_CYCLES
- #define ISR_E_STEPPER_CYCLES ISR_STEPPER_CYCLES
- #if DISABLED(LIN_ADVANCE) && ENABLED(MIXING_EXTRUDER)
- #define ISR_START_MIXING_STEPPER_CYCLES ((MIXING_STEPPERS) * (ISR_START_STEPPER_CYCLES))
- #define ISR_MIXING_STEPPER_CYCLES ((MIXING_STEPPERS) * (ISR_STEPPER_CYCLES))
- #else
- #define ISR_START_MIXING_STEPPER_CYCLES 0UL
- #define ISR_MIXING_STEPPER_CYCLES 0UL
- #endif
- #define MIN_ISR_START_LOOP_CYCLES (ISR_START_X_STEPPER_CYCLES + ISR_START_Y_STEPPER_CYCLES + ISR_START_Z_STEPPER_CYCLES + ISR_START_E_STEPPER_CYCLES + ISR_START_MIXING_STEPPER_CYCLES)
- #define MIN_ISR_LOOP_CYCLES (ISR_X_STEPPER_CYCLES + ISR_Y_STEPPER_CYCLES + ISR_Z_STEPPER_CYCLES + ISR_E_STEPPER_CYCLES + ISR_MIXING_STEPPER_CYCLES)
- #define _MIN_STEPPER_PULSE_CYCLES(N) MAX((unsigned long)((F_CPU) / (MAXIMUM_STEPPER_RATE)), ((F_CPU) / 500000UL) * (N))
- #if MINIMUM_STEPPER_PULSE
- #define MIN_STEPPER_PULSE_CYCLES _MIN_STEPPER_PULSE_CYCLES((unsigned long)(MINIMUM_STEPPER_PULSE))
- #else
- #define MIN_STEPPER_PULSE_CYCLES _MIN_STEPPER_PULSE_CYCLES(1UL)
- #endif
- #define MIN_PULSE_TICKS (((PULSE_TIMER_TICKS_PER_US) * (unsigned long)(MINIMUM_STEPPER_PULSE)) + ((MIN_ISR_START_LOOP_CYCLES) / (unsigned long)(PULSE_TIMER_PRESCALE)))
- #define ADDED_STEP_TICKS (((MIN_STEPPER_PULSE_CYCLES) / (PULSE_TIMER_PRESCALE)) - (MIN_PULSE_TICKS))
- #define ISR_LOOP_CYCLES (ISR_LOOP_BASE_CYCLES + MAX(MIN_STEPPER_PULSE_CYCLES, MIN_ISR_LOOP_CYCLES))
- #if ENABLED(LIN_ADVANCE)
-
- #if ENABLED(MIXING_EXTRUDER)
- #define MIN_ISR_LA_LOOP_CYCLES ((MIXING_STEPPERS) * (ISR_STEPPER_CYCLES))
- #else
- #define MIN_ISR_LA_LOOP_CYCLES ISR_STEPPER_CYCLES
- #endif
-
- #define ISR_LA_LOOP_CYCLES MAX(MIN_STEPPER_PULSE_CYCLES, MIN_ISR_LA_LOOP_CYCLES)
- #else
- #define ISR_LA_LOOP_CYCLES 0UL
- #endif
- #define ISR_EXECUTION_CYCLES(R) (((ISR_BASE_CYCLES + ISR_S_CURVE_CYCLES + (ISR_LOOP_CYCLES) * (R) + ISR_LA_BASE_CYCLES + ISR_LA_LOOP_CYCLES)) / (R))
- #define MAX_STEP_ISR_FREQUENCY_128X ((F_CPU) / ISR_EXECUTION_CYCLES(128))
- #define MAX_STEP_ISR_FREQUENCY_64X ((F_CPU) / ISR_EXECUTION_CYCLES(64))
- #define MAX_STEP_ISR_FREQUENCY_32X ((F_CPU) / ISR_EXECUTION_CYCLES(32))
- #define MAX_STEP_ISR_FREQUENCY_16X ((F_CPU) / ISR_EXECUTION_CYCLES(16))
- #define MAX_STEP_ISR_FREQUENCY_8X ((F_CPU) / ISR_EXECUTION_CYCLES(8))
- #define MAX_STEP_ISR_FREQUENCY_4X ((F_CPU) / ISR_EXECUTION_CYCLES(4))
- #define MAX_STEP_ISR_FREQUENCY_2X ((F_CPU) / ISR_EXECUTION_CYCLES(2))
- #define MAX_STEP_ISR_FREQUENCY_1X ((F_CPU) / ISR_EXECUTION_CYCLES(1))
- #define MIN_STEP_ISR_FREQUENCY MAX_STEP_ISR_FREQUENCY_1X
- #include "planner.h"
- #include "speed_lookuptable.h"
- #include "stepper_indirection.h"
- #include "language.h"
- #include "types.h"
- static FORCE_INLINE uint16_t MultiU16X8toH16(uint8_t charIn1, uint16_t intIn2) {
- register uint8_t tmp;
- register uint16_t intRes;
- __asm__ __volatile__ (
- A("clr %[tmp]")
- A("mul %[charIn1], %B[intIn2]")
- A("movw %A[intRes], r0")
- A("mul %[charIn1], %A[intIn2]")
- A("add %A[intRes], r1")
- A("adc %B[intRes], %[tmp]")
- A("lsr r0")
- A("adc %A[intRes], %[tmp]")
- A("adc %B[intRes], %[tmp]")
- A("clr r1")
- : [intRes] "=&r" (intRes),
- [tmp] "=&r" (tmp)
- : [charIn1] "d" (charIn1),
- [intIn2] "d" (intIn2)
- : "cc"
- );
- return intRes;
- }
- class Stepper {
- public:
- #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
- static bool homing_dual_axis;
- #endif
- #if HAS_MOTOR_CURRENT_PWM
- #ifndef PWM_MOTOR_CURRENT
- #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
- #endif
- static uint32_t motor_current_setting[3];
- #endif
- private:
- static block_t* current_block;
- static uint8_t last_direction_bits,
- axis_did_move;
- static bool abort_current_block;
- #if DISABLED(MIXING_EXTRUDER)
- static uint8_t last_moved_extruder;
- #endif
- #if ENABLED(X_DUAL_ENDSTOPS)
- static bool locked_X_motor, locked_X2_motor;
- #endif
- #if ENABLED(Y_DUAL_ENDSTOPS)
- static bool locked_Y_motor, locked_Y2_motor;
- #endif
- #if ENABLED(Z_DUAL_ENDSTOPS)
- static bool locked_Z_motor, locked_Z2_motor;
- #endif
- static uint32_t acceleration_time, deceleration_time;
- static uint8_t steps_per_isr;
- #if ENABLED(ADAPTIVE_STEP_SMOOTHING)
- static uint8_t oversampling_factor;
- #else
- static constexpr uint8_t oversampling_factor = 0;
- #endif
-
- static int32_t delta_error[NUM_AXIS];
- static uint32_t advance_dividend[NUM_AXIS],
- advance_divisor,
- step_events_completed,
- accelerate_until,
- decelerate_after,
- step_event_count;
-
- #if ENABLED(MIXING_EXTRUDER)
- static int32_t delta_error_m[MIXING_STEPPERS];
- static uint32_t advance_dividend_m[MIXING_STEPPERS],
- advance_divisor_m;
- #define MIXING_STEPPERS_LOOP(VAR) \
- for (uint8_t VAR = 0; VAR < MIXING_STEPPERS; VAR++)
- #else
- static int8_t active_extruder;
- #endif
- #if ENABLED(S_CURVE_ACCELERATION)
- static int32_t bezier_A,
- bezier_B,
- bezier_C;
- static uint32_t bezier_F,
- bezier_AV;
- static bool A_negative,
- bezier_2nd_half;
- #endif
- static uint32_t nextMainISR;
- #if ENABLED(LIN_ADVANCE)
- static uint32_t nextAdvanceISR, LA_isr_rate;
- static uint16_t LA_current_adv_steps, LA_final_adv_steps, LA_max_adv_steps;
- static int8_t LA_steps;
- static bool LA_use_advance_lead;
- #endif // LIN_ADVANCE
- static int32_t ticks_nominal;
- #if DISABLED(S_CURVE_ACCELERATION)
- static uint32_t acc_step_rate;
- #endif
- static volatile int32_t endstops_trigsteps[XYZ];
-
-
-
- static volatile int32_t count_position[NUM_AXIS];
-
-
-
- static int8_t count_direction[NUM_AXIS];
- public:
-
-
-
- Stepper() { };
-
- static void init();
-
-
- static void isr();
-
- static void stepper_pulse_phase_isr();
-
- static uint32_t stepper_block_phase_isr();
- #if ENABLED(LIN_ADVANCE)
-
- static uint32_t advance_isr();
- #endif
-
- static bool is_block_busy(const block_t* const block);
-
- static int32_t position(const AxisEnum axis);
-
- static void report_positions();
-
-
- static void wake_up();
-
- FORCE_INLINE static void quick_stop() { abort_current_block = true; }
-
- FORCE_INLINE static bool motor_direction(const AxisEnum axis) { return TEST(last_direction_bits, axis); }
-
- FORCE_INLINE static bool axis_is_moving(const AxisEnum axis) { return TEST(axis_did_move, axis); }
-
- FORCE_INLINE static uint8_t movement_extruder() {
- return
- #if ENABLED(MIXING_EXTRUDER)
- 0
- #else
- last_moved_extruder
- #endif
- ;
- }
-
- static void endstop_triggered(const AxisEnum axis);
-
- static int32_t triggered_position(const AxisEnum axis);
- #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
- static void digitalPotWrite(const int16_t address, const int16_t value);
- static void digipot_current(const uint8_t driver, const int16_t current);
- #endif
- #if HAS_MICROSTEPS
- static void microstep_ms(const uint8_t driver, const int8_t ms1, const int8_t ms2);
- static void microstep_mode(const uint8_t driver, const uint8_t stepping);
- static void microstep_readings();
- #endif
- #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
- FORCE_INLINE static void set_homing_dual_axis(const bool state) { homing_dual_axis = state; }
- #endif
- #if ENABLED(X_DUAL_ENDSTOPS)
- FORCE_INLINE static void set_x_lock(const bool state) { locked_X_motor = state; }
- FORCE_INLINE static void set_x2_lock(const bool state) { locked_X2_motor = state; }
- #endif
- #if ENABLED(Y_DUAL_ENDSTOPS)
- FORCE_INLINE static void set_y_lock(const bool state) { locked_Y_motor = state; }
- FORCE_INLINE static void set_y2_lock(const bool state) { locked_Y2_motor = state; }
- #endif
- #if ENABLED(Z_DUAL_ENDSTOPS)
- FORCE_INLINE static void set_z_lock(const bool state) { locked_Z_motor = state; }
- FORCE_INLINE static void set_z2_lock(const bool state) { locked_Z2_motor = state; }
- #endif
- #if ENABLED(BABYSTEPPING)
- static void babystep(const AxisEnum axis, const bool direction);
- #endif
- #if HAS_MOTOR_CURRENT_PWM
- static void refresh_motor_power();
- #endif
-
- inline static void set_position(const int32_t &a, const int32_t &b, const int32_t &c
- #if ENABLED(HANGPRINTER)
- , const int32_t &d
- #endif
- , const int32_t &e
- ) {
- planner.synchronize();
- const bool was_enabled = STEPPER_ISR_ENABLED();
- if (was_enabled) DISABLE_STEPPER_DRIVER_INTERRUPT();
- _set_position(a, b, c
- #if ENABLED(HANGPRINTER)
- , d
- #endif
- , e
- );
- if (was_enabled) ENABLE_STEPPER_DRIVER_INTERRUPT();
- }
- inline static void set_position(const AxisEnum a, const int32_t &v) {
- planner.synchronize();
- const bool was_enabled = STEPPER_ISR_ENABLED();
- if (was_enabled) DISABLE_STEPPER_DRIVER_INTERRUPT();
- count_position[a] = v;
- if (was_enabled) ENABLE_STEPPER_DRIVER_INTERRUPT();
- }
- private:
-
- static void _set_position(const int32_t &a, const int32_t &b, const int32_t &c
- #if ENABLED(HANGPRINTER)
- , const int32_t &d
- #endif
- , const int32_t &e
- );
-
- static void set_directions();
-
- friend void reset_stepper_drivers();
- FORCE_INLINE static uint32_t calc_timer_interval(uint32_t step_rate, uint8_t scale, uint8_t* loops) {
- uint32_t timer;
-
- step_rate <<= scale;
- uint8_t multistep = 1;
- #if DISABLED(DISABLE_MULTI_STEPPING)
-
- static const uint32_t limit[] PROGMEM = {
- ( MAX_STEP_ISR_FREQUENCY_1X ),
- ( MAX_STEP_ISR_FREQUENCY_2X >> 1),
- ( MAX_STEP_ISR_FREQUENCY_4X >> 2),
- ( MAX_STEP_ISR_FREQUENCY_8X >> 3),
- ( MAX_STEP_ISR_FREQUENCY_16X >> 4),
- ( MAX_STEP_ISR_FREQUENCY_32X >> 5),
- ( MAX_STEP_ISR_FREQUENCY_64X >> 6),
- (MAX_STEP_ISR_FREQUENCY_128X >> 7)
- };
-
- uint8_t idx = 0;
- while (idx < 7 && step_rate > (uint32_t)pgm_read_dword(&limit[idx])) {
- step_rate >>= 1;
- multistep <<= 1;
- ++idx;
- };
- #else
- NOMORE(step_rate, uint32_t(MAX_STEP_ISR_FREQUENCY_1X));
- #endif
- *loops = multistep;
- constexpr uint32_t min_step_rate = F_CPU / 500000U;
- NOLESS(step_rate, min_step_rate);
- step_rate -= min_step_rate;
- if (step_rate >= (8 * 256)) {
- const uint8_t tmp_step_rate = (step_rate & 0x00FF);
- const uint16_t table_address = (uint16_t)&speed_lookuptable_fast[(uint8_t)(step_rate >> 8)][0],
- gain = (uint16_t)pgm_read_word_near(table_address + 2);
- timer = MultiU16X8toH16(tmp_step_rate, gain);
- timer = (uint16_t)pgm_read_word_near(table_address) - timer;
- }
- else {
- uint16_t table_address = (uint16_t)&speed_lookuptable_slow[0][0];
- table_address += ((step_rate) >> 1) & 0xFFFC;
- timer = (uint16_t)pgm_read_word_near(table_address)
- - (((uint16_t)pgm_read_word_near(table_address + 2) * (uint8_t)(step_rate & 0x0007)) >> 3);
- }
-
-
- return timer;
- }
- #if ENABLED(S_CURVE_ACCELERATION)
- static void _calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t av);
- static int32_t _eval_bezier_curve(const uint32_t curr_step);
- #endif
- #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
- static void digipot_init();
- #endif
- #if HAS_MICROSTEPS
- static void microstep_init();
- #endif
- };
- extern Stepper stepper;
- #endif // STEPPER_H
|