My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stepper.h 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * stepper.h - stepper motor driver: executes motion plans of planner.c using the stepper motors
  25. * Derived from Grbl
  26. *
  27. * Copyright (c) 2009-2011 Simen Svale Skogsrud
  28. *
  29. * Grbl is free software: you can redistribute it and/or modify
  30. * it under the terms of the GNU General Public License as published by
  31. * the Free Software Foundation, either version 3 of the License, or
  32. * (at your option) any later version.
  33. *
  34. * Grbl is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License
  40. * along with Grbl. If not, see <https://www.gnu.org/licenses/>.
  41. */
  42. #include "../inc/MarlinConfig.h"
  43. #include "planner.h"
  44. #include "stepper/indirection.h"
  45. #ifdef __AVR__
  46. #include "speed_lookuptable.h"
  47. #endif
  48. // Disable multiple steps per ISR
  49. //#define DISABLE_MULTI_STEPPING
  50. //
  51. // Estimate the amount of time the Stepper ISR will take to execute
  52. //
  53. /**
  54. * The method of calculating these cycle-constants is unclear.
  55. * Most of them are no longer used directly for pulse timing, and exist
  56. * only to estimate a maximum step rate based on the user's configuration.
  57. * As 32-bit processors continue to diverge, maintaining cycle counts
  58. * will become increasingly difficult and error-prone.
  59. */
  60. #ifdef CPU_32_BIT
  61. /**
  62. * Duration of START_TIMED_PULSE
  63. *
  64. * ...as measured on an LPC1768 with a scope and converted to cycles.
  65. * Not applicable to other 32-bit processors, but as long as others
  66. * take longer, pulses will be longer. For example the SKR Pro
  67. * (stm32f407zgt6) requires ~60 cyles.
  68. */
  69. #define TIMER_READ_ADD_AND_STORE_CYCLES 34UL
  70. // The base ISR takes 792 cycles
  71. #define ISR_BASE_CYCLES 792UL
  72. // Linear advance base time is 64 cycles
  73. #if ENABLED(LIN_ADVANCE)
  74. #define ISR_LA_BASE_CYCLES 64UL
  75. #else
  76. #define ISR_LA_BASE_CYCLES 0UL
  77. #endif
  78. // S curve interpolation adds 40 cycles
  79. #if ENABLED(S_CURVE_ACCELERATION)
  80. #define ISR_S_CURVE_CYCLES 40UL
  81. #else
  82. #define ISR_S_CURVE_CYCLES 0UL
  83. #endif
  84. // Stepper Loop base cycles
  85. #define ISR_LOOP_BASE_CYCLES 4UL
  86. // To start the step pulse, in the worst case takes
  87. #define ISR_START_STEPPER_CYCLES 13UL
  88. // And each stepper (start + stop pulse) takes in worst case
  89. #define ISR_STEPPER_CYCLES 16UL
  90. #else
  91. // Cycles to perform actions in START_TIMED_PULSE
  92. #define TIMER_READ_ADD_AND_STORE_CYCLES 13UL
  93. // The base ISR takes 752 cycles
  94. #define ISR_BASE_CYCLES 752UL
  95. // Linear advance base time is 32 cycles
  96. #if ENABLED(LIN_ADVANCE)
  97. #define ISR_LA_BASE_CYCLES 32UL
  98. #else
  99. #define ISR_LA_BASE_CYCLES 0UL
  100. #endif
  101. // S curve interpolation adds 160 cycles
  102. #if ENABLED(S_CURVE_ACCELERATION)
  103. #define ISR_S_CURVE_CYCLES 160UL
  104. #else
  105. #define ISR_S_CURVE_CYCLES 0UL
  106. #endif
  107. // Stepper Loop base cycles
  108. #define ISR_LOOP_BASE_CYCLES 32UL
  109. // To start the step pulse, in the worst case takes
  110. #define ISR_START_STEPPER_CYCLES 57UL
  111. // And each stepper (start + stop pulse) takes in worst case
  112. #define ISR_STEPPER_CYCLES 88UL
  113. #endif
  114. // If linear advance is disabled, the loop also handles them
  115. #if DISABLED(LIN_ADVANCE) && ENABLED(MIXING_EXTRUDER)
  116. #define ISR_MIXING_STEPPER_CYCLES ((MIXING_STEPPERS) * (ISR_STEPPER_CYCLES))
  117. #else
  118. #define ISR_MIXING_STEPPER_CYCLES 0UL
  119. #endif
  120. // Add time for each stepper
  121. #if HAS_X_STEP
  122. #define ISR_X_STEPPER_CYCLES ISR_STEPPER_CYCLES
  123. #endif
  124. #if HAS_Y_STEP
  125. #define ISR_Y_STEPPER_CYCLES ISR_STEPPER_CYCLES
  126. #endif
  127. #if HAS_Z_STEP
  128. #define ISR_Z_STEPPER_CYCLES ISR_STEPPER_CYCLES
  129. #endif
  130. #if HAS_I_STEP
  131. #define ISR_I_STEPPER_CYCLES ISR_STEPPER_CYCLES
  132. #endif
  133. #if HAS_J_STEP
  134. #define ISR_J_STEPPER_CYCLES ISR_STEPPER_CYCLES
  135. #endif
  136. #if HAS_K_STEP
  137. #define ISR_K_STEPPER_CYCLES ISR_STEPPER_CYCLES
  138. #endif
  139. #if HAS_EXTRUDERS
  140. #define ISR_E_STEPPER_CYCLES ISR_STEPPER_CYCLES // E is always interpolated, even for mixing extruders
  141. #endif
  142. // And the total minimum loop time, not including the base
  143. #define MIN_ISR_LOOP_CYCLES (ISR_MIXING_STEPPER_CYCLES LOGICAL_AXIS_GANG(+ ISR_E_STEPPER_CYCLES, + ISR_X_STEPPER_CYCLES, + ISR_Y_STEPPER_CYCLES, + ISR_Z_STEPPER_CYCLES, + ISR_I_STEPPER_CYCLES, + ISR_J_STEPPER_CYCLES, + ISR_K_STEPPER_CYCLES))
  144. // Calculate the minimum MPU cycles needed per pulse to enforce, limited to the max stepper rate
  145. #define _MIN_STEPPER_PULSE_CYCLES(N) _MAX(uint32_t((F_CPU) / (MAXIMUM_STEPPER_RATE)), ((F_CPU) / 500000UL) * (N))
  146. #if MINIMUM_STEPPER_PULSE
  147. #define MIN_STEPPER_PULSE_CYCLES _MIN_STEPPER_PULSE_CYCLES(uint32_t(MINIMUM_STEPPER_PULSE))
  148. #elif HAS_DRIVER(LV8729)
  149. #define MIN_STEPPER_PULSE_CYCLES uint32_t((((F_CPU) - 1) / 2000000) + 1) // 0.5µs, aka 500ns
  150. #else
  151. #define MIN_STEPPER_PULSE_CYCLES _MIN_STEPPER_PULSE_CYCLES(1UL)
  152. #endif
  153. // Calculate the minimum pulse times (high and low)
  154. #if MINIMUM_STEPPER_PULSE && MAXIMUM_STEPPER_RATE
  155. constexpr uint32_t _MIN_STEP_PERIOD_NS = 1000000000UL / MAXIMUM_STEPPER_RATE;
  156. constexpr uint32_t _MIN_PULSE_HIGH_NS = 1000UL * MINIMUM_STEPPER_PULSE;
  157. constexpr uint32_t _MIN_PULSE_LOW_NS = _MAX((_MIN_STEP_PERIOD_NS - _MIN(_MIN_STEP_PERIOD_NS, _MIN_PULSE_HIGH_NS)), _MIN_PULSE_HIGH_NS);
  158. #elif MINIMUM_STEPPER_PULSE
  159. // Assume 50% duty cycle
  160. constexpr uint32_t _MIN_PULSE_HIGH_NS = 1000UL * MINIMUM_STEPPER_PULSE;
  161. constexpr uint32_t _MIN_PULSE_LOW_NS = _MIN_PULSE_HIGH_NS;
  162. #elif MAXIMUM_STEPPER_RATE
  163. // Assume 50% duty cycle
  164. constexpr uint32_t _MIN_PULSE_HIGH_NS = 500000000UL / MAXIMUM_STEPPER_RATE;
  165. constexpr uint32_t _MIN_PULSE_LOW_NS = _MIN_PULSE_HIGH_NS;
  166. #else
  167. #error "Expected at least one of MINIMUM_STEPPER_PULSE or MAXIMUM_STEPPER_RATE to be defined"
  168. #endif
  169. // But the user could be enforcing a minimum time, so the loop time is
  170. #define ISR_LOOP_CYCLES (ISR_LOOP_BASE_CYCLES + _MAX(MIN_STEPPER_PULSE_CYCLES, MIN_ISR_LOOP_CYCLES))
  171. // If linear advance is enabled, then it is handled separately
  172. #if ENABLED(LIN_ADVANCE)
  173. // Estimate the minimum LA loop time
  174. #if ENABLED(MIXING_EXTRUDER) // ToDo: ???
  175. // HELP ME: What is what?
  176. // Directions are set up for MIXING_STEPPERS - like before.
  177. // Finding the right stepper may last up to MIXING_STEPPERS loops in get_next_stepper().
  178. // These loops are a bit faster than advancing a bresenham counter.
  179. // Always only one e-stepper is stepped.
  180. #define MIN_ISR_LA_LOOP_CYCLES ((MIXING_STEPPERS) * (ISR_STEPPER_CYCLES))
  181. #else
  182. #define MIN_ISR_LA_LOOP_CYCLES ISR_STEPPER_CYCLES
  183. #endif
  184. // And the real loop time
  185. #define ISR_LA_LOOP_CYCLES _MAX(MIN_STEPPER_PULSE_CYCLES, MIN_ISR_LA_LOOP_CYCLES)
  186. #else
  187. #define ISR_LA_LOOP_CYCLES 0UL
  188. #endif
  189. // Now estimate the total ISR execution time in cycles given a step per ISR multiplier
  190. #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))
  191. // The maximum allowable stepping frequency when doing x128-x1 stepping (in Hz)
  192. #define MAX_STEP_ISR_FREQUENCY_128X ((F_CPU) / ISR_EXECUTION_CYCLES(128))
  193. #define MAX_STEP_ISR_FREQUENCY_64X ((F_CPU) / ISR_EXECUTION_CYCLES(64))
  194. #define MAX_STEP_ISR_FREQUENCY_32X ((F_CPU) / ISR_EXECUTION_CYCLES(32))
  195. #define MAX_STEP_ISR_FREQUENCY_16X ((F_CPU) / ISR_EXECUTION_CYCLES(16))
  196. #define MAX_STEP_ISR_FREQUENCY_8X ((F_CPU) / ISR_EXECUTION_CYCLES(8))
  197. #define MAX_STEP_ISR_FREQUENCY_4X ((F_CPU) / ISR_EXECUTION_CYCLES(4))
  198. #define MAX_STEP_ISR_FREQUENCY_2X ((F_CPU) / ISR_EXECUTION_CYCLES(2))
  199. #define MAX_STEP_ISR_FREQUENCY_1X ((F_CPU) / ISR_EXECUTION_CYCLES(1))
  200. // The minimum step ISR rate used by ADAPTIVE_STEP_SMOOTHING to target 50% CPU usage
  201. // This does not account for the possibility of multi-stepping.
  202. // Perhaps DISABLE_MULTI_STEPPING should be required with ADAPTIVE_STEP_SMOOTHING.
  203. #define MIN_STEP_ISR_FREQUENCY (MAX_STEP_ISR_FREQUENCY_1X / 2)
  204. //
  205. // Stepper class definition
  206. //
  207. class Stepper {
  208. public:
  209. #if EITHER(HAS_EXTRA_ENDSTOPS, Z_STEPPER_AUTO_ALIGN)
  210. static bool separate_multi_axis;
  211. #endif
  212. #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
  213. #if HAS_MOTOR_CURRENT_PWM
  214. #ifndef PWM_MOTOR_CURRENT
  215. #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
  216. #endif
  217. #define MOTOR_CURRENT_COUNT LINEAR_AXES
  218. #elif HAS_MOTOR_CURRENT_SPI
  219. static constexpr uint32_t digipot_count[] = DIGIPOT_MOTOR_CURRENT;
  220. #define MOTOR_CURRENT_COUNT COUNT(Stepper::digipot_count)
  221. #endif
  222. static bool initialized;
  223. static uint32_t motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load()
  224. #endif
  225. // Last-moved extruder, as set when the last movement was fetched from planner
  226. #if HAS_MULTI_EXTRUDER
  227. static uint8_t last_moved_extruder;
  228. #else
  229. static constexpr uint8_t last_moved_extruder = 0;
  230. #endif
  231. #if HAS_FREEZE_PIN
  232. static bool frozen; // Set this flag to instantly freeze motion
  233. #endif
  234. private:
  235. static block_t* current_block; // A pointer to the block currently being traced
  236. static uint8_t last_direction_bits, // The next stepping-bits to be output
  237. axis_did_move; // Last Movement in the given direction is not null, as computed when the last movement was fetched from planner
  238. static bool abort_current_block; // Signals to the stepper that current block should be aborted
  239. #if ENABLED(X_DUAL_ENDSTOPS)
  240. static bool locked_X_motor, locked_X2_motor;
  241. #endif
  242. #if ENABLED(Y_DUAL_ENDSTOPS)
  243. static bool locked_Y_motor, locked_Y2_motor;
  244. #endif
  245. #if EITHER(Z_MULTI_ENDSTOPS, Z_STEPPER_AUTO_ALIGN)
  246. static bool locked_Z_motor, locked_Z2_motor
  247. #if NUM_Z_STEPPER_DRIVERS >= 3
  248. , locked_Z3_motor
  249. #if NUM_Z_STEPPER_DRIVERS >= 4
  250. , locked_Z4_motor
  251. #endif
  252. #endif
  253. ;
  254. #endif
  255. static uint32_t acceleration_time, deceleration_time; // time measured in Stepper Timer ticks
  256. static uint8_t steps_per_isr; // Count of steps to perform per Stepper ISR call
  257. #if ENABLED(ADAPTIVE_STEP_SMOOTHING)
  258. static uint8_t oversampling_factor; // Oversampling factor (log2(multiplier)) to increase temporal resolution of axis
  259. #else
  260. static constexpr uint8_t oversampling_factor = 0;
  261. #endif
  262. // Delta error variables for the Bresenham line tracer
  263. static xyze_long_t delta_error;
  264. static xyze_ulong_t advance_dividend;
  265. static uint32_t advance_divisor,
  266. step_events_completed, // The number of step events executed in the current block
  267. accelerate_until, // The point from where we need to stop acceleration
  268. decelerate_after, // The point from where we need to start decelerating
  269. step_event_count; // The total event count for the current block
  270. #if EITHER(HAS_MULTI_EXTRUDER, MIXING_EXTRUDER)
  271. static uint8_t stepper_extruder;
  272. #else
  273. static constexpr uint8_t stepper_extruder = 0;
  274. #endif
  275. #if ENABLED(S_CURVE_ACCELERATION)
  276. static int32_t bezier_A, // A coefficient in Bézier speed curve
  277. bezier_B, // B coefficient in Bézier speed curve
  278. bezier_C; // C coefficient in Bézier speed curve
  279. static uint32_t bezier_F, // F coefficient in Bézier speed curve
  280. bezier_AV; // AV coefficient in Bézier speed curve
  281. #ifdef __AVR__
  282. static bool A_negative; // If A coefficient was negative
  283. #endif
  284. static bool bezier_2nd_half; // If Bézier curve has been initialized or not
  285. #endif
  286. #if ENABLED(LIN_ADVANCE)
  287. static constexpr uint32_t LA_ADV_NEVER = 0xFFFFFFFF;
  288. static uint32_t nextAdvanceISR, LA_isr_rate;
  289. 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".
  290. static int8_t LA_steps;
  291. static bool LA_use_advance_lead;
  292. #endif
  293. #if ENABLED(INTEGRATED_BABYSTEPPING)
  294. static constexpr uint32_t BABYSTEP_NEVER = 0xFFFFFFFF;
  295. static uint32_t nextBabystepISR;
  296. #endif
  297. #if ENABLED(DIRECT_STEPPING)
  298. static page_step_state_t page_step_state;
  299. #endif
  300. static int32_t ticks_nominal;
  301. #if DISABLED(S_CURVE_ACCELERATION)
  302. static uint32_t acc_step_rate; // needed for deceleration start point
  303. #endif
  304. // Exact steps at which an endstop was triggered
  305. static xyz_long_t endstops_trigsteps;
  306. // Positions of stepper motors, in step units
  307. static xyze_long_t count_position;
  308. // Current stepper motor directions (+1 or -1)
  309. static xyze_int8_t count_direction;
  310. #if ENABLED(LASER_POWER_INLINE_TRAPEZOID)
  311. typedef struct {
  312. bool enabled; // Trapezoid needed flag (i.e., laser on, planner in control)
  313. uint8_t cur_power; // Current laser power
  314. bool cruise_set; // Power set up for cruising?
  315. #if DISABLED(LASER_POWER_INLINE_TRAPEZOID_CONT)
  316. uint32_t last_step_count, // Step count from the last update
  317. acc_step_count; // Bresenham counter for laser accel/decel
  318. #else
  319. uint16_t till_update; // Countdown to the next update
  320. #endif
  321. } stepper_laser_t;
  322. static stepper_laser_t laser_trap;
  323. #endif
  324. public:
  325. // Initialize stepper hardware
  326. static void init();
  327. // Interrupt Service Routine and phases
  328. // The stepper subsystem goes to sleep when it runs out of things to execute.
  329. // Call this to notify the subsystem that it is time to go to work.
  330. static inline void wake_up() { ENABLE_STEPPER_DRIVER_INTERRUPT(); }
  331. static inline bool is_awake() { return STEPPER_ISR_ENABLED(); }
  332. static inline bool suspend() {
  333. const bool awake = is_awake();
  334. if (awake) DISABLE_STEPPER_DRIVER_INTERRUPT();
  335. return awake;
  336. }
  337. // The ISR scheduler
  338. static void isr();
  339. // The stepper pulse ISR phase
  340. static void pulse_phase_isr();
  341. // The stepper block processing ISR phase
  342. static uint32_t block_phase_isr();
  343. #if ENABLED(LIN_ADVANCE)
  344. // The Linear advance ISR phase
  345. static uint32_t advance_isr();
  346. FORCE_INLINE static void initiateLA() { nextAdvanceISR = 0; }
  347. #endif
  348. #if ENABLED(INTEGRATED_BABYSTEPPING)
  349. // The Babystepping ISR phase
  350. static uint32_t babystepping_isr();
  351. FORCE_INLINE static void initiateBabystepping() {
  352. if (nextBabystepISR == BABYSTEP_NEVER) {
  353. nextBabystepISR = 0;
  354. wake_up();
  355. }
  356. }
  357. #endif
  358. // Check if the given block is busy or not - Must not be called from ISR contexts
  359. static bool is_block_busy(const block_t * const block);
  360. // Get the position of a stepper, in steps
  361. static int32_t position(const AxisEnum axis);
  362. // Set the current position in steps
  363. static void set_position(const xyze_long_t &spos);
  364. static void set_axis_position(const AxisEnum a, const int32_t &v);
  365. // Report the positions of the steppers, in steps
  366. static void report_a_position(const xyz_long_t &pos);
  367. static void report_positions();
  368. // Discard current block and free any resources
  369. FORCE_INLINE static void discard_current_block() {
  370. #if ENABLED(DIRECT_STEPPING)
  371. if (IS_PAGE(current_block))
  372. page_manager.free_page(current_block->page_idx);
  373. #endif
  374. current_block = nullptr;
  375. axis_did_move = 0;
  376. planner.release_current_block();
  377. }
  378. // Quickly stop all steppers
  379. FORCE_INLINE static void quick_stop() { abort_current_block = true; }
  380. // The direction of a single motor
  381. FORCE_INLINE static bool motor_direction(const AxisEnum axis) { return TEST(last_direction_bits, axis); }
  382. // The last movement direction was not null on the specified axis. Note that motor direction is not necessarily the same.
  383. FORCE_INLINE static bool axis_is_moving(const AxisEnum axis) { return TEST(axis_did_move, axis); }
  384. // Handle a triggered endstop
  385. static void endstop_triggered(const AxisEnum axis);
  386. // Triggered position of an axis in steps
  387. static int32_t triggered_position(const AxisEnum axis);
  388. #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
  389. static void set_digipot_value_spi(const int16_t address, const int16_t value);
  390. static void set_digipot_current(const uint8_t driver, const int16_t current);
  391. #endif
  392. #if HAS_MICROSTEPS
  393. static void microstep_ms(const uint8_t driver, const int8_t ms1, const int8_t ms2, const int8_t ms3);
  394. static void microstep_mode(const uint8_t driver, const uint8_t stepping);
  395. static void microstep_readings();
  396. #endif
  397. #if EITHER(HAS_EXTRA_ENDSTOPS, Z_STEPPER_AUTO_ALIGN)
  398. FORCE_INLINE static void set_separate_multi_axis(const bool state) { separate_multi_axis = state; }
  399. #endif
  400. #if ENABLED(X_DUAL_ENDSTOPS)
  401. FORCE_INLINE static void set_x_lock(const bool state) { locked_X_motor = state; }
  402. FORCE_INLINE static void set_x2_lock(const bool state) { locked_X2_motor = state; }
  403. #endif
  404. #if ENABLED(Y_DUAL_ENDSTOPS)
  405. FORCE_INLINE static void set_y_lock(const bool state) { locked_Y_motor = state; }
  406. FORCE_INLINE static void set_y2_lock(const bool state) { locked_Y2_motor = state; }
  407. #endif
  408. #if EITHER(Z_MULTI_ENDSTOPS, Z_STEPPER_AUTO_ALIGN)
  409. FORCE_INLINE static void set_z1_lock(const bool state) { locked_Z_motor = state; }
  410. FORCE_INLINE static void set_z2_lock(const bool state) { locked_Z2_motor = state; }
  411. #if NUM_Z_STEPPER_DRIVERS >= 3
  412. FORCE_INLINE static void set_z3_lock(const bool state) { locked_Z3_motor = state; }
  413. #if NUM_Z_STEPPER_DRIVERS >= 4
  414. FORCE_INLINE static void set_z4_lock(const bool state) { locked_Z4_motor = state; }
  415. #endif
  416. #endif
  417. static inline void set_all_z_lock(const bool lock, const int8_t except=-1) {
  418. set_z1_lock(lock ^ (except == 0));
  419. set_z2_lock(lock ^ (except == 1));
  420. #if NUM_Z_STEPPER_DRIVERS >= 3
  421. set_z3_lock(lock ^ (except == 2));
  422. #if NUM_Z_STEPPER_DRIVERS >= 4
  423. set_z4_lock(lock ^ (except == 3));
  424. #endif
  425. #endif
  426. }
  427. #endif
  428. #if ENABLED(BABYSTEPPING)
  429. static void do_babystep(const AxisEnum axis, const bool direction); // perform a short step with a single stepper motor, outside of any convention
  430. #endif
  431. #if HAS_MOTOR_CURRENT_PWM
  432. static void refresh_motor_power();
  433. #endif
  434. // Update direction states for all steppers
  435. static void set_directions();
  436. // Set direction bits and update all stepper DIR states
  437. static void set_directions(const uint8_t bits) {
  438. last_direction_bits = bits;
  439. set_directions();
  440. }
  441. private:
  442. // Set the current position in steps
  443. static void _set_position(const abce_long_t &spos);
  444. FORCE_INLINE static uint32_t calc_timer_interval(uint32_t step_rate, uint8_t *loops) {
  445. uint32_t timer;
  446. // Scale the frequency, as requested by the caller
  447. step_rate <<= oversampling_factor;
  448. uint8_t multistep = 1;
  449. #if DISABLED(DISABLE_MULTI_STEPPING)
  450. // The stepping frequency limits for each multistepping rate
  451. static const uint32_t limit[] PROGMEM = {
  452. ( MAX_STEP_ISR_FREQUENCY_1X ),
  453. ( MAX_STEP_ISR_FREQUENCY_2X >> 1),
  454. ( MAX_STEP_ISR_FREQUENCY_4X >> 2),
  455. ( MAX_STEP_ISR_FREQUENCY_8X >> 3),
  456. ( MAX_STEP_ISR_FREQUENCY_16X >> 4),
  457. ( MAX_STEP_ISR_FREQUENCY_32X >> 5),
  458. ( MAX_STEP_ISR_FREQUENCY_64X >> 6),
  459. (MAX_STEP_ISR_FREQUENCY_128X >> 7)
  460. };
  461. // Select the proper multistepping
  462. uint8_t idx = 0;
  463. while (idx < 7 && step_rate > (uint32_t)pgm_read_dword(&limit[idx])) {
  464. step_rate >>= 1;
  465. multistep <<= 1;
  466. ++idx;
  467. };
  468. #else
  469. NOMORE(step_rate, uint32_t(MAX_STEP_ISR_FREQUENCY_1X));
  470. #endif
  471. *loops = multistep;
  472. #ifdef CPU_32_BIT
  473. // In case of high-performance processor, it is able to calculate in real-time
  474. timer = uint32_t(STEPPER_TIMER_RATE) / step_rate;
  475. #else
  476. constexpr uint32_t min_step_rate = (F_CPU) / 500000U;
  477. NOLESS(step_rate, min_step_rate);
  478. step_rate -= min_step_rate; // Correct for minimal speed
  479. if (step_rate >= (8 * 256)) { // higher step rate
  480. const uint8_t tmp_step_rate = (step_rate & 0x00FF);
  481. const uint16_t table_address = (uint16_t)&speed_lookuptable_fast[(uint8_t)(step_rate >> 8)][0],
  482. gain = (uint16_t)pgm_read_word(table_address + 2);
  483. timer = MultiU16X8toH16(tmp_step_rate, gain);
  484. timer = (uint16_t)pgm_read_word(table_address) - timer;
  485. }
  486. else { // lower step rates
  487. uint16_t table_address = (uint16_t)&speed_lookuptable_slow[0][0];
  488. table_address += ((step_rate) >> 1) & 0xFFFC;
  489. timer = (uint16_t)pgm_read_word(table_address)
  490. - (((uint16_t)pgm_read_word(table_address + 2) * (uint8_t)(step_rate & 0x0007)) >> 3);
  491. }
  492. // (there is no need to limit the timer value here. All limits have been
  493. // applied above, and AVR is able to keep up at 30khz Stepping ISR rate)
  494. #endif
  495. return timer;
  496. }
  497. #if ENABLED(S_CURVE_ACCELERATION)
  498. static void _calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t av);
  499. static int32_t _eval_bezier_curve(const uint32_t curr_step);
  500. #endif
  501. #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
  502. static void digipot_init();
  503. #endif
  504. #if HAS_MICROSTEPS
  505. static void microstep_init();
  506. #endif
  507. };
  508. extern Stepper stepper;