2016-03-25 07:19:46 +01:00
/**
2016-03-24 19:01:20 +01:00
* Marlin 3 D Printer Firmware
2019-06-28 06:57:50 +02:00
* Copyright ( c ) 2019 MarlinFirmware [ https : //github.com/MarlinFirmware/Marlin]
2016-03-24 19:01:20 +01:00
*
* Based on Sprinter and grbl .
2019-06-28 06:57:50 +02:00
* Copyright ( c ) 2011 Camiel Gubbels / Erik van der Zalm
2016-03-24 19:01:20 +01:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*
*/
2018-11-04 09:25:55 +01:00
# pragma once
2016-03-24 19:01:20 +01:00
2016-03-25 07:19:46 +01:00
/**
2016-04-27 16:15:20 +02:00
* stepper . h - stepper motor driver : executes motion plans of planner . c using the stepper motors
2016-05-08 21:16:26 +02:00
* Derived from Grbl
2016-04-27 16:15:20 +02:00
*
* Copyright ( c ) 2009 - 2011 Simen Svale Skogsrud
*
* Grbl is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* Grbl is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with Grbl . If not , see < http : //www.gnu.org/licenses/>.
*/
2011-11-20 14:50:08 +01:00
2018-06-13 00:28:57 +02:00
# include "../inc/MarlinConfig.h"
2019-09-16 22:01:46 +02:00
# include "planner.h"
# include "stepper/indirection.h"
# ifdef __AVR__
# include "speed_lookuptable.h"
# endif
2018-06-13 00:28:57 +02:00
// Disable multiple steps per ISR
//#define DISABLE_MULTI_STEPPING
//
// Estimate the amount of time the Stepper ISR will take to execute
//
# ifdef CPU_32_BIT
// The base ISR takes 792 cycles
# define ISR_BASE_CYCLES 792UL
// Linear advance base time is 64 cycles
# if ENABLED(LIN_ADVANCE)
# define ISR_LA_BASE_CYCLES 64UL
# else
# define ISR_LA_BASE_CYCLES 0UL
# endif
// S curve interpolation adds 40 cycles
# if ENABLED(S_CURVE_ACCELERATION)
# define ISR_S_CURVE_CYCLES 40UL
# else
# define ISR_S_CURVE_CYCLES 0UL
# endif
// Stepper Loop base cycles
# define ISR_LOOP_BASE_CYCLES 4UL
2018-06-22 16:15:37 +02:00
// To start the step pulse, in the worst case takes
# define ISR_START_STEPPER_CYCLES 13UL
// And each stepper (start + stop pulse) takes in worst case
2018-06-13 00:28:57 +02:00
# define ISR_STEPPER_CYCLES 16UL
# else
// The base ISR takes 752 cycles
# define ISR_BASE_CYCLES 752UL
// Linear advance base time is 32 cycles
# if ENABLED(LIN_ADVANCE)
# define ISR_LA_BASE_CYCLES 32UL
# else
# define ISR_LA_BASE_CYCLES 0UL
# endif
// S curve interpolation adds 160 cycles
# if ENABLED(S_CURVE_ACCELERATION)
# define ISR_S_CURVE_CYCLES 160UL
# else
# define ISR_S_CURVE_CYCLES 0UL
# endif
// Stepper Loop base cycles
# define ISR_LOOP_BASE_CYCLES 32UL
2018-06-22 16:15:37 +02:00
// To start the step pulse, in the worst case takes
# define ISR_START_STEPPER_CYCLES 57UL
// And each stepper (start + stop pulse) takes in worst case
2018-06-13 00:28:57 +02:00
# define ISR_STEPPER_CYCLES 88UL
# endif
// Add time for each stepper
2019-05-30 23:33:40 +02:00
# if HAS_X_STEP
2018-06-22 16:15:37 +02:00
# define ISR_START_X_STEPPER_CYCLES ISR_START_STEPPER_CYCLES
# define ISR_X_STEPPER_CYCLES ISR_STEPPER_CYCLES
2018-06-13 00:28:57 +02:00
# else
2018-06-22 16:15:37 +02:00
# define ISR_START_X_STEPPER_CYCLES 0UL
# define ISR_X_STEPPER_CYCLES 0UL
2018-06-13 00:28:57 +02:00
# endif
2019-05-30 23:33:40 +02:00
# if HAS_Y_STEP
2018-06-22 16:15:37 +02:00
# define ISR_START_Y_STEPPER_CYCLES ISR_START_STEPPER_CYCLES
# define ISR_Y_STEPPER_CYCLES ISR_STEPPER_CYCLES
2018-06-13 00:28:57 +02:00
# else
2018-06-22 16:15:37 +02:00
# define ISR_START_Y_STEPPER_CYCLES 0UL
# define ISR_Y_STEPPER_CYCLES 0UL
2018-06-13 00:28:57 +02:00
# endif
2019-05-30 23:33:40 +02:00
# if HAS_Z_STEP
2018-06-22 16:15:37 +02:00
# define ISR_START_Z_STEPPER_CYCLES ISR_START_STEPPER_CYCLES
# define ISR_Z_STEPPER_CYCLES ISR_STEPPER_CYCLES
2018-06-13 00:28:57 +02:00
# else
2018-06-22 16:15:37 +02:00
# define ISR_START_Z_STEPPER_CYCLES 0UL
# define ISR_Z_STEPPER_CYCLES 0UL
2018-06-13 00:28:57 +02:00
# endif
// E is always interpolated, even for mixing extruders
2018-06-22 16:15:37 +02:00
# define ISR_START_E_STEPPER_CYCLES ISR_START_STEPPER_CYCLES
# define ISR_E_STEPPER_CYCLES ISR_STEPPER_CYCLES
2018-06-13 00:28:57 +02:00
2019-05-31 00:05:06 +02:00
// If linear advance is disabled, the loop also handles them
# if DISABLED(LIN_ADVANCE) && ENABLED(MIXING_EXTRUDER)
2018-06-22 16:15:37 +02:00
# define ISR_START_MIXING_STEPPER_CYCLES ((MIXING_STEPPERS) * (ISR_START_STEPPER_CYCLES))
2018-06-13 00:28:57 +02:00
# define ISR_MIXING_STEPPER_CYCLES ((MIXING_STEPPERS) * (ISR_STEPPER_CYCLES))
# else
2018-06-22 16:15:37 +02:00
# define ISR_START_MIXING_STEPPER_CYCLES 0UL
2018-06-13 00:28:57 +02:00
# define ISR_MIXING_STEPPER_CYCLES 0UL
# endif
2018-06-22 16:15:37 +02:00
// Calculate the minimum time to start all stepper pulses in the ISR loop
# 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)
2018-06-13 00:28:57 +02:00
// And the total minimum loop time, not including the base
# 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)
// Calculate the minimum MPU cycles needed per pulse to enforce, limited to the max stepper rate
2019-07-06 01:01:21 +02:00
# define _MIN_STEPPER_PULSE_CYCLES(N) _MAX(uint32_t((F_CPU) / (MAXIMUM_STEPPER_RATE)), ((F_CPU) / 500000UL) * (N))
2018-06-13 00:28:57 +02:00
# if MINIMUM_STEPPER_PULSE
2019-05-12 02:02:19 +02:00
# define MIN_STEPPER_PULSE_CYCLES _MIN_STEPPER_PULSE_CYCLES(uint32_t(MINIMUM_STEPPER_PULSE))
# elif HAS_DRIVER(LV8729)
# define MIN_STEPPER_PULSE_CYCLES uint32_t((((F_CPU) - 1) / 2000000) + 1) // 0.5µs, aka 500ns
2018-06-13 00:28:57 +02:00
# else
2018-06-22 16:15:37 +02:00
# define MIN_STEPPER_PULSE_CYCLES _MIN_STEPPER_PULSE_CYCLES(1UL)
2018-06-13 00:28:57 +02:00
# endif
2018-06-22 16:15:37 +02:00
// Calculate the minimum ticks of the PULSE timer that must elapse with the step pulse enabled
// adding the "start stepper pulse" code section execution cycles to account for that not all
// pulses start at the beginning of the loop, so an extra time must be added to compensate so
// the last generated pulse (usually the extruder stepper) has the right length
2019-09-04 23:57:13 +02:00
# if HAS_DRIVER(LV8729) && MINIMUM_STEPPER_PULSE == 0
2019-05-12 02:02:19 +02:00
# define MIN_PULSE_TICKS ((((PULSE_TIMER_TICKS_PER_US) + 1) / 2) + ((MIN_ISR_START_LOOP_CYCLES) / uint32_t(PULSE_TIMER_PRESCALE)))
# else
# define MIN_PULSE_TICKS (((PULSE_TIMER_TICKS_PER_US) * uint32_t(MINIMUM_STEPPER_PULSE)) + ((MIN_ISR_START_LOOP_CYCLES) / uint32_t(PULSE_TIMER_PRESCALE)))
# endif
2018-06-22 16:15:37 +02:00
// Calculate the extra ticks of the PULSE timer between step pulses
# define ADDED_STEP_TICKS (((MIN_STEPPER_PULSE_CYCLES) / (PULSE_TIMER_PRESCALE)) - (MIN_PULSE_TICKS))
2018-06-13 00:28:57 +02:00
// But the user could be enforcing a minimum time, so the loop time is
2019-07-06 01:01:21 +02:00
# define ISR_LOOP_CYCLES (ISR_LOOP_BASE_CYCLES + _MAX(MIN_STEPPER_PULSE_CYCLES, MIN_ISR_LOOP_CYCLES))
2018-06-13 00:28:57 +02:00
// If linear advance is enabled, then it is handled separately
# if ENABLED(LIN_ADVANCE)
// Estimate the minimum LA loop time
2018-10-16 10:38:57 +02:00
# if ENABLED(MIXING_EXTRUDER) // ToDo: ???
// HELP ME: What is what?
// Directions are set up for MIXING_STEPPERS - like before.
// Finding the right stepper may last up to MIXING_STEPPERS loops in get_next_stepper().
// These loops are a bit faster than advancing a bresenham counter.
// Always only one e-stepper is stepped.
2018-06-13 00:28:57 +02:00
# define MIN_ISR_LA_LOOP_CYCLES ((MIXING_STEPPERS) * (ISR_STEPPER_CYCLES))
# else
# define MIN_ISR_LA_LOOP_CYCLES ISR_STEPPER_CYCLES
# endif
// And the real loop time
2019-07-06 01:01:21 +02:00
# define ISR_LA_LOOP_CYCLES _MAX(MIN_STEPPER_PULSE_CYCLES, MIN_ISR_LA_LOOP_CYCLES)
2018-06-13 00:28:57 +02:00
# else
# define ISR_LA_LOOP_CYCLES 0UL
# endif
// Now estimate the total ISR execution time in cycles given a step per ISR multiplier
# 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))
// The maximum allowable stepping frequency when doing x128-x1 stepping (in Hz)
# 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))
// The minimum allowable frequency for step smoothing will be 1/10 of the maximum nominal frequency (in Hz)
# define MIN_STEP_ISR_FREQUENCY MAX_STEP_ISR_FREQUENCY_1X
//
// Stepper class definition
//
2016-04-27 16:15:20 +02:00
class Stepper {
2011-11-20 14:50:08 +01:00
2016-04-27 16:15:20 +02:00
public :
2019-02-06 11:59:22 +01:00
# if HAS_EXTRA_ENDSTOPS || ENABLED(Z_STEPPER_AUTO_ALIGN)
2018-06-19 18:55:49 +02:00
static bool separate_multi_axis ;
2016-04-27 16:15:20 +02:00
# endif
2017-06-03 07:38:07 +02:00
# 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 ] ;
2019-03-03 00:29:02 +01:00
static bool initialized ;
2017-06-03 07:38:07 +02:00
# endif
2016-04-27 16:15:20 +02:00
private :
2018-06-28 01:11:16 +02:00
static block_t * current_block ; // A pointer to the block currently being traced
2018-05-23 10:45:25 +02:00
static uint8_t last_direction_bits , // The next stepping-bits to be output
axis_did_move ; // Last Movement in the given direction is not null, as computed when the last movement was fetched from planner
2018-06-03 05:59:21 +02:00
2018-05-23 10:45:25 +02:00
static bool abort_current_block ; // Signals to the stepper that current block should be aborted
2016-04-27 16:15:20 +02:00
2018-09-11 06:09:26 +02:00
// Last-moved extruder, as set when the last movement was fetched from planner
# if EXTRUDERS < 2
static constexpr uint8_t last_moved_extruder = 0 ;
# elif DISABLED(MIXING_EXTRUDER)
static uint8_t last_moved_extruder ;
2018-06-03 05:59:21 +02:00
# endif
2017-10-29 09:43:44 +01:00
# if ENABLED(X_DUAL_ENDSTOPS)
2018-06-02 02:02:22 +02:00
static bool locked_X_motor , locked_X2_motor ;
2017-10-29 09:43:44 +01:00
# endif
# if ENABLED(Y_DUAL_ENDSTOPS)
2018-06-02 02:02:22 +02:00
static bool locked_Y_motor , locked_Y2_motor ;
2017-10-29 09:43:44 +01:00
# endif
2018-10-29 20:01:36 +01:00
# if Z_MULTI_ENDSTOPS || ENABLED(Z_STEPPER_AUTO_ALIGN)
2018-06-02 02:02:22 +02:00
static bool locked_Z_motor , locked_Z2_motor ;
2016-04-27 16:15:20 +02:00
# endif
2019-03-17 05:43:06 +01:00
# if ENABLED(Z_TRIPLE_ENDSTOPS) || BOTH(Z_STEPPER_AUTO_ALIGN, Z_TRIPLE_STEPPER_DRIVERS)
2018-06-19 18:55:49 +02:00
static bool locked_Z3_motor ;
# endif
2016-04-27 16:15:20 +02:00
2018-06-03 05:59:21 +02:00
static uint32_t acceleration_time , deceleration_time ; // time measured in Stepper Timer ticks
static uint8_t steps_per_isr ; // Count of steps to perform per Stepper ISR call
# if ENABLED(ADAPTIVE_STEP_SMOOTHING)
static uint8_t oversampling_factor ; // Oversampling factor (log2(multiplier)) to increase temporal resolution of axis
# else
static constexpr uint8_t oversampling_factor = 0 ;
# endif
// Delta error variables for the Bresenham line tracer
2019-09-29 11:25:39 +02:00
static xyze_long_t delta_error ;
static xyze_ulong_t advance_dividend ;
static uint32_t advance_divisor ,
2018-06-03 05:59:21 +02:00
step_events_completed , // The number of step events executed in the current block
accelerate_until , // The point from where we need to stop acceleration
decelerate_after , // The point from where we need to start decelerating
step_event_count ; // The total event count for the current block
2018-10-16 10:38:57 +02:00
# if EXTRUDERS > 1 || ENABLED(MIXING_EXTRUDER)
static uint8_t stepper_extruder ;
2018-06-03 05:59:21 +02:00
# else
2018-10-16 10:38:57 +02:00
static constexpr uint8_t stepper_extruder = 0 ;
2018-06-03 05:59:21 +02:00
# endif
2016-04-27 16:15:20 +02:00
2018-05-26 09:02:39 +02:00
# if ENABLED(S_CURVE_ACCELERATION)
2018-04-12 01:13:42 +02:00
static int32_t bezier_A , // A coefficient in Bézier speed curve
bezier_B , // B coefficient in Bézier speed curve
bezier_C ; // C coefficient in Bézier speed curve
2018-05-21 05:20:11 +02:00
static uint32_t bezier_F , // F coefficient in Bézier speed curve
bezier_AV ; // AV coefficient in Bézier speed curve
2018-04-12 01:13:42 +02:00
# ifdef __AVR__
static bool A_negative ; // If A coefficient was negative
# endif
static bool bezier_2nd_half ; // If Bézier curve has been initialized or not
2018-04-07 03:48:06 +02:00
# endif
2018-05-09 07:17:53 +02:00
static uint32_t nextMainISR ; // time remaining for the next Step ISR
2017-10-09 11:25:18 +02:00
# if ENABLED(LIN_ADVANCE)
2018-06-03 05:59:21 +02:00
static uint32_t nextAdvanceISR , LA_isr_rate ;
static uint16_t LA_current_adv_steps , LA_final_adv_steps , LA_max_adv_steps ; // Copy from current executed block. Needed because current_block is set to NULL "too early".
static int8_t LA_steps ;
static bool LA_use_advance_lead ;
2018-05-09 07:17:53 +02:00
# endif // LIN_ADVANCE
2018-02-23 07:53:29 +01:00
2018-06-03 05:59:21 +02:00
static int32_t ticks_nominal ;
2018-05-26 09:02:39 +02:00
# if DISABLED(S_CURVE_ACCELERATION)
2018-05-09 07:17:53 +02:00
static uint32_t acc_step_rate ; // needed for deceleration start point
2018-04-07 03:48:06 +02:00
# endif
2016-04-27 16:15:20 +02:00
2019-09-14 10:05:10 +02:00
//
// Exact steps at which an endstop was triggered
//
2019-09-29 11:25:39 +02:00
static xyz_long_t endstops_trigsteps ;
2016-04-27 16:15:20 +02:00
2016-09-15 10:18:10 +02:00
//
// Positions of stepper motors, in step units
//
2019-09-29 11:25:39 +02:00
static xyze_long_t count_position ;
2016-09-15 10:18:10 +02:00
2016-04-27 16:15:20 +02:00
//
// Current direction of stepper motors (+1 or -1)
//
2019-09-29 11:25:39 +02:00
static xyze_int8_t count_direction ;
2016-04-27 16:15:20 +02:00
public :
//
// Constructor / initializer
//
2019-10-01 04:44:07 +02:00
Stepper ( ) { } ;
2016-04-27 16:15:20 +02:00
// Initialize stepper hardware
2016-06-03 02:57:56 +02:00
static void init ( ) ;
2016-04-27 16:15:20 +02:00
// Interrupt Service Routines
2018-05-09 07:17:53 +02:00
// The ISR scheduler
2018-06-02 02:02:22 +02:00
static void isr ( ) ;
2016-04-27 16:15:20 +02:00
2018-05-09 07:17:53 +02:00
// The stepper pulse phase ISR
static void stepper_pulse_phase_isr ( ) ;
2018-05-04 00:45:13 +02:00
2018-05-09 07:17:53 +02:00
// The stepper block processing phase ISR
static uint32_t stepper_block_phase_isr ( ) ;
2018-05-04 00:45:13 +02:00
2018-05-09 07:17:53 +02:00
# if ENABLED(LIN_ADVANCE)
// The Linear advance stepper ISR
static uint32_t advance_isr ( ) ;
# endif
2016-04-27 16:15:20 +02:00
2018-06-28 01:11:16 +02:00
// Check if the given block is busy or not - Must not be called from ISR contexts
static bool is_block_busy ( const block_t * const block ) ;
2016-04-27 16:15:20 +02:00
// Get the position of a stepper, in steps
2018-05-04 03:13:01 +02:00
static int32_t position ( const AxisEnum axis ) ;
2016-04-27 16:15:20 +02:00
// Report the positions of the steppers, in steps
2016-06-03 02:57:56 +02:00
static void report_positions ( ) ;
2016-04-27 16:15:20 +02:00
// The stepper subsystem goes to sleep when it runs out of things to execute. Call this
// to notify the subsystem that it is time to go to work.
2016-06-03 02:57:56 +02:00
static void wake_up ( ) ;
2016-04-27 16:15:20 +02:00
2018-05-09 07:17:53 +02:00
// Quickly stop all steppers
2018-05-16 09:08:43 +02:00
FORCE_INLINE static void quick_stop ( ) { abort_current_block = true ; }
2011-11-20 14:50:08 +01:00
2016-04-27 16:15:20 +02:00
// The direction of a single motor
2017-12-09 09:10:54 +01:00
FORCE_INLINE static bool motor_direction ( const AxisEnum axis ) { return TEST ( last_direction_bits , axis ) ; }
2011-12-12 19:34:37 +01:00
2018-05-16 09:08:43 +02:00
// The last movement direction was not null on the specified axis. Note that motor direction is not necessarily the same.
2018-05-23 10:45:25 +02:00
FORCE_INLINE static bool axis_is_moving ( const AxisEnum axis ) { return TEST ( axis_did_move , axis ) ; }
2018-05-16 09:08:43 +02:00
// The extruder associated to the last movement
2018-06-03 05:59:21 +02:00
FORCE_INLINE static uint8_t movement_extruder ( ) {
2019-09-29 11:25:39 +02:00
return ( 0
# if EXTRUDERS > 1 && DISABLED(MIXING_EXTRUDER)
+ last_moved_extruder
2018-06-03 05:59:21 +02:00
# endif
2019-09-29 11:25:39 +02:00
) ;
2018-06-03 05:59:21 +02:00
}
2018-05-09 07:17:53 +02:00
// Handle a triggered endstop
static void endstop_triggered ( const AxisEnum axis ) ;
// Triggered position of an axis in steps
static int32_t triggered_position ( const AxisEnum axis ) ;
2016-09-25 13:32:58 +02:00
# if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
2017-06-25 05:23:45 +02:00
static void digitalPotWrite ( const int16_t address , const int16_t value ) ;
static void digipot_current ( const uint8_t driver , const int16_t current ) ;
2016-09-25 13:32:58 +02:00
# endif
# if HAS_MICROSTEPS
2018-10-11 07:56:26 +02:00
static void microstep_ms ( const uint8_t driver , const int8_t ms1 , const int8_t ms2 , const int8_t ms3 ) ;
2017-06-25 05:23:45 +02:00
static void microstep_mode ( const uint8_t driver , const uint8_t stepping ) ;
2016-09-25 13:32:58 +02:00
static void microstep_readings ( ) ;
2016-04-27 16:15:20 +02:00
# endif
2011-11-20 14:50:08 +01:00
2019-02-06 11:59:22 +01:00
# if HAS_EXTRA_ENDSTOPS || ENABLED(Z_STEPPER_AUTO_ALIGN)
2018-06-19 18:55:49 +02:00
FORCE_INLINE static void set_separate_multi_axis ( const bool state ) { separate_multi_axis = state ; }
2018-06-03 03:39:00 +02:00
# endif
2017-10-29 09:43:44 +01:00
# if ENABLED(X_DUAL_ENDSTOPS)
2018-06-02 02:02:22 +02:00
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 ; }
2017-10-29 09:43:44 +01:00
# endif
# if ENABLED(Y_DUAL_ENDSTOPS)
2018-06-02 02:02:22 +02:00
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 ; }
2017-10-29 09:43:44 +01:00
# endif
2018-10-29 20:01:36 +01:00
# if Z_MULTI_ENDSTOPS || (ENABLED(Z_STEPPER_AUTO_ALIGN) && Z_MULTI_STEPPER_DRIVERS)
2018-06-02 02:02:22 +02:00
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 ; }
2016-04-27 16:15:20 +02:00
# endif
2019-03-17 05:43:06 +01:00
# if ENABLED(Z_TRIPLE_ENDSTOPS) || BOTH(Z_STEPPER_AUTO_ALIGN, Z_TRIPLE_STEPPER_DRIVERS)
2018-06-19 18:55:49 +02:00
FORCE_INLINE static void set_z3_lock ( const bool state ) { locked_Z3_motor = state ; }
# endif
2011-11-20 14:50:08 +01:00
2016-04-27 16:15:20 +02:00
# if ENABLED(BABYSTEPPING)
2016-11-04 00:23:31 +01:00
static void babystep ( const AxisEnum axis , const bool direction ) ; // perform a short step with a single stepper motor, outside of any convention
2016-04-27 16:15:20 +02:00
# endif
2011-11-20 14:50:08 +01:00
2017-06-03 07:38:07 +02:00
# if HAS_MOTOR_CURRENT_PWM
static void refresh_motor_power ( ) ;
# endif
2018-05-21 22:05:17 +02:00
// Set the current position in steps
2018-08-14 06:55:12 +02:00
static inline void set_position ( const int32_t & a , const int32_t & b , const int32_t & c , const int32_t & e ) {
2018-05-21 22:05:17 +02:00
planner . synchronize ( ) ;
2018-06-02 02:02:22 +02:00
const bool was_enabled = STEPPER_ISR_ENABLED ( ) ;
if ( was_enabled ) DISABLE_STEPPER_DRIVER_INTERRUPT ( ) ;
2018-05-21 22:05:17 +02:00
_set_position ( a , b , c , e ) ;
2018-06-02 02:02:22 +02:00
if ( was_enabled ) ENABLE_STEPPER_DRIVER_INTERRUPT ( ) ;
2018-05-21 22:05:17 +02:00
}
2019-09-29 11:25:39 +02:00
static inline void set_position ( const xyze_long_t & abce ) { set_position ( abce . a , abce . b , abce . c , abce . e ) ; }
2018-05-21 22:05:17 +02:00
2019-09-29 11:25:39 +02:00
static inline void set_axis_position ( const AxisEnum a , const int32_t & v ) {
2018-05-21 22:05:17 +02:00
planner . synchronize ( ) ;
2018-06-02 02:02:22 +02:00
2018-06-12 02:12:56 +02:00
# ifdef __AVR__
// Protect the access to the position. Only required for AVR, as
// any 32bit CPU offers atomic access to 32bit variables
const bool was_enabled = STEPPER_ISR_ENABLED ( ) ;
if ( was_enabled ) DISABLE_STEPPER_DRIVER_INTERRUPT ( ) ;
# endif
2018-06-02 02:02:22 +02:00
2018-05-21 22:05:17 +02:00
count_position [ a ] = v ;
2018-06-02 02:02:22 +02:00
2018-06-12 02:12:56 +02:00
# ifdef __AVR__
// Reenable Stepper ISR
if ( was_enabled ) ENABLE_STEPPER_DRIVER_INTERRUPT ( ) ;
# endif
2018-05-21 22:05:17 +02:00
}
2018-09-16 02:31:51 +02:00
// Set direction bits for all steppers
static void set_directions ( ) ;
2016-05-04 02:00:28 +02:00
2018-09-17 08:06:22 +02:00
private :
2018-09-16 03:11:10 +02:00
2018-05-09 07:17:53 +02:00
// Set the current position in steps
static void _set_position ( const int32_t & a , const int32_t & b , const int32_t & c , const int32_t & e ) ;
2019-09-29 11:25:39 +02:00
FORCE_INLINE static void _set_position ( const abce_long_t & spos ) { _set_position ( spos . a , spos . b , spos . c , spos . e ) ; }
2018-05-09 07:17:53 +02:00
2018-06-03 05:59:21 +02:00
FORCE_INLINE static uint32_t calc_timer_interval ( uint32_t step_rate , uint8_t scale , uint8_t * loops ) {
2018-05-09 07:17:53 +02:00
uint32_t timer ;
2015-10-03 08:08:58 +02:00
2018-06-03 05:59:21 +02:00
// Scale the frequency, as requested by the caller
step_rate < < = scale ;
2011-11-20 14:50:08 +01:00
2018-06-03 05:59:21 +02:00
uint8_t multistep = 1 ;
2018-05-29 00:39:23 +02:00
# if DISABLED(DISABLE_MULTI_STEPPING)
2018-06-03 05:59:21 +02:00
// The stepping frequency limits for each multistepping rate
static const uint32_t limit [ ] PROGMEM = {
2018-06-13 00:28:57 +02:00
( 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 )
2018-06-03 05:59:21 +02:00
} ;
// Select the proper multistepping
uint8_t idx = 0 ;
while ( idx < 7 & & step_rate > ( uint32_t ) pgm_read_dword ( & limit [ idx ] ) ) {
2017-06-18 01:36:10 +02:00
step_rate > > = 1 ;
2018-06-03 05:59:21 +02:00
multistep < < = 1 ;
+ + idx ;
} ;
# else
2018-06-13 00:28:57 +02:00
NOMORE ( step_rate , uint32_t ( MAX_STEP_ISR_FREQUENCY_1X ) ) ;
2017-06-18 01:36:10 +02:00
# endif
2018-06-03 05:59:21 +02:00
* loops = multistep ;
2017-06-18 01:36:10 +02:00
# ifdef CPU_32_BIT
2017-09-27 11:57:14 +02:00
// In case of high-performance processor, it is able to calculate in real-time
2018-06-12 23:32:22 +02:00
timer = uint32_t ( STEPPER_TIMER_RATE ) / step_rate ;
2017-06-18 01:36:10 +02:00
# else
2018-05-29 00:39:23 +02:00
constexpr uint32_t min_step_rate = F_CPU / 500000U ;
NOLESS ( step_rate , min_step_rate ) ;
step_rate - = min_step_rate ; // Correct for minimal speed
2017-06-18 01:36:10 +02:00
if ( step_rate > = ( 8 * 256 ) ) { // higher step rate
2018-05-21 05:20:11 +02:00
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 ] ,
2018-11-02 06:42:16 +01:00
gain = ( uint16_t ) pgm_read_word ( table_address + 2 ) ;
2018-05-13 15:10:08 +02:00
timer = MultiU16X8toH16 ( tmp_step_rate , gain ) ;
2018-11-02 06:42:16 +01:00
timer = ( uint16_t ) pgm_read_word ( table_address ) - timer ;
2017-06-18 01:36:10 +02:00
}
else { // lower step rates
2018-05-13 15:10:08 +02:00
uint16_t table_address = ( uint16_t ) & speed_lookuptable_slow [ 0 ] [ 0 ] ;
2018-05-08 16:17:44 +02:00
table_address + = ( ( step_rate ) > > 1 ) & 0xFFFC ;
2018-11-02 06:42:16 +01:00
timer = ( uint16_t ) pgm_read_word ( table_address )
- ( ( ( uint16_t ) pgm_read_word ( table_address + 2 ) * ( uint8_t ) ( step_rate & 0x0007 ) ) > > 3 ) ;
2017-06-18 01:36:10 +02:00
}
2018-06-03 05:59:21 +02:00
// (there is no need to limit the timer value here. All limits have been
// applied above, and AVR is able to keep up at 30khz Stepping ISR rate)
2017-06-18 01:36:10 +02:00
# endif
2016-04-27 16:15:20 +02:00
return timer ;
}
2016-03-24 11:18:45 +01:00
2018-05-26 09:02:39 +02:00
# if ENABLED(S_CURVE_ACCELERATION)
2018-04-12 01:13:42 +02:00
static void _calc_bezier_curve_coeffs ( const int32_t v0 , const int32_t v1 , const uint32_t av ) ;
2018-04-07 03:48:06 +02:00
static int32_t _eval_bezier_curve ( const uint32_t curr_step ) ;
# endif
2012-08-30 09:16:57 +02:00
2017-06-03 07:38:07 +02:00
# if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
static void digipot_init ( ) ;
# endif
2016-09-25 13:32:58 +02:00
# if HAS_MICROSTEPS
static void microstep_init ( ) ;
# endif
2015-03-24 18:06:44 +01:00
2016-04-27 16:15:20 +02:00
} ;
2015-10-03 08:08:58 +02:00
2018-06-13 00:28:57 +02:00
extern Stepper stepper ;