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 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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 <http://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 <http://www.gnu.org/licenses/>.
  41. */
  42. #include "../inc/MarlinConfig.h"
  43. // Disable multiple steps per ISR
  44. //#define DISABLE_MULTI_STEPPING
  45. //
  46. // Estimate the amount of time the Stepper ISR will take to execute
  47. //
  48. #ifndef MINIMUM_STEPPER_PULSE
  49. #define MINIMUM_STEPPER_PULSE 0UL
  50. #endif
  51. #ifndef MAXIMUM_STEPPER_RATE
  52. #if MINIMUM_STEPPER_PULSE
  53. #define MAXIMUM_STEPPER_RATE (1000000UL / (2UL * (unsigned long)(MINIMUM_STEPPER_PULSE)))
  54. #else
  55. #define MAXIMUM_STEPPER_RATE 500000UL
  56. #endif
  57. #endif
  58. #ifdef CPU_32_BIT
  59. // The base ISR takes 792 cycles
  60. #define ISR_BASE_CYCLES 792UL
  61. // Linear advance base time is 64 cycles
  62. #if ENABLED(LIN_ADVANCE)
  63. #define ISR_LA_BASE_CYCLES 64UL
  64. #else
  65. #define ISR_LA_BASE_CYCLES 0UL
  66. #endif
  67. // S curve interpolation adds 40 cycles
  68. #if ENABLED(S_CURVE_ACCELERATION)
  69. #define ISR_S_CURVE_CYCLES 40UL
  70. #else
  71. #define ISR_S_CURVE_CYCLES 0UL
  72. #endif
  73. // Stepper Loop base cycles
  74. #define ISR_LOOP_BASE_CYCLES 4UL
  75. // To start the step pulse, in the worst case takes
  76. #define ISR_START_STEPPER_CYCLES 13UL
  77. // And each stepper (start + stop pulse) takes in worst case
  78. #define ISR_STEPPER_CYCLES 16UL
  79. #else
  80. // The base ISR takes 752 cycles
  81. #define ISR_BASE_CYCLES 752UL
  82. // Linear advance base time is 32 cycles
  83. #if ENABLED(LIN_ADVANCE)
  84. #define ISR_LA_BASE_CYCLES 32UL
  85. #else
  86. #define ISR_LA_BASE_CYCLES 0UL
  87. #endif
  88. // S curve interpolation adds 160 cycles
  89. #if ENABLED(S_CURVE_ACCELERATION)
  90. #define ISR_S_CURVE_CYCLES 160UL
  91. #else
  92. #define ISR_S_CURVE_CYCLES 0UL
  93. #endif
  94. // Stepper Loop base cycles
  95. #define ISR_LOOP_BASE_CYCLES 32UL
  96. // To start the step pulse, in the worst case takes
  97. #define ISR_START_STEPPER_CYCLES 57UL
  98. // And each stepper (start + stop pulse) takes in worst case
  99. #define ISR_STEPPER_CYCLES 88UL
  100. #endif
  101. // Add time for each stepper
  102. #ifdef HAS_X_STEP
  103. #define ISR_START_X_STEPPER_CYCLES ISR_START_STEPPER_CYCLES
  104. #define ISR_X_STEPPER_CYCLES ISR_STEPPER_CYCLES
  105. #else
  106. #define ISR_START_X_STEPPER_CYCLES 0UL
  107. #define ISR_X_STEPPER_CYCLES 0UL
  108. #endif
  109. #ifdef HAS_Y_STEP
  110. #define ISR_START_Y_STEPPER_CYCLES ISR_START_STEPPER_CYCLES
  111. #define ISR_Y_STEPPER_CYCLES ISR_STEPPER_CYCLES
  112. #else
  113. #define ISR_START_Y_STEPPER_CYCLES 0UL
  114. #define ISR_Y_STEPPER_CYCLES 0UL
  115. #endif
  116. #ifdef HAS_Z_STEP
  117. #define ISR_START_Z_STEPPER_CYCLES ISR_START_STEPPER_CYCLES
  118. #define ISR_Z_STEPPER_CYCLES ISR_STEPPER_CYCLES
  119. #else
  120. #define ISR_START_Z_STEPPER_CYCLES 0UL
  121. #define ISR_Z_STEPPER_CYCLES 0UL
  122. #endif
  123. // E is always interpolated, even for mixing extruders
  124. #define ISR_START_E_STEPPER_CYCLES ISR_START_STEPPER_CYCLES
  125. #define ISR_E_STEPPER_CYCLES ISR_STEPPER_CYCLES
  126. // If linear advance is disabled, then the loop also handles them
  127. #if DISABLED(LIN_ADVANCE) && ENABLED(MIXING_EXTRUDER) // ToDo: ???
  128. // HELP ME: What is what?
  129. // Directions are set up for MIXING_STEPPERS - like before.
  130. // Finding the right stepper may last up to MIXING_STEPPERS loops in get_next_stepper().
  131. // These loops are a bit faster than advancing a bresenham counter.
  132. // Always only one e-stepper is stepped.
  133. #define ISR_START_MIXING_STEPPER_CYCLES ((MIXING_STEPPERS) * (ISR_START_STEPPER_CYCLES))
  134. #define ISR_MIXING_STEPPER_CYCLES ((MIXING_STEPPERS) * (ISR_STEPPER_CYCLES))
  135. #else
  136. #define ISR_START_MIXING_STEPPER_CYCLES 0UL
  137. #define ISR_MIXING_STEPPER_CYCLES 0UL
  138. #endif
  139. // Calculate the minimum time to start all stepper pulses in the ISR loop
  140. #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)
  141. // And the total minimum loop time, not including the base
  142. #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)
  143. // Calculate the minimum MPU cycles needed per pulse to enforce, limited to the max stepper rate
  144. #define _MIN_STEPPER_PULSE_CYCLES(N) MAX((unsigned long)((F_CPU) / (MAXIMUM_STEPPER_RATE)), ((F_CPU) / 500000UL) * (N))
  145. #if MINIMUM_STEPPER_PULSE
  146. #define MIN_STEPPER_PULSE_CYCLES _MIN_STEPPER_PULSE_CYCLES((unsigned long)(MINIMUM_STEPPER_PULSE))
  147. #else
  148. #define MIN_STEPPER_PULSE_CYCLES _MIN_STEPPER_PULSE_CYCLES(1UL)
  149. #endif
  150. // Calculate the minimum ticks of the PULSE timer that must elapse with the step pulse enabled
  151. // adding the "start stepper pulse" code section execution cycles to account for that not all
  152. // pulses start at the beginning of the loop, so an extra time must be added to compensate so
  153. // the last generated pulse (usually the extruder stepper) has the right length
  154. #define MIN_PULSE_TICKS (((PULSE_TIMER_TICKS_PER_US) * (unsigned long)(MINIMUM_STEPPER_PULSE)) + ((MIN_ISR_START_LOOP_CYCLES) / (unsigned long)(PULSE_TIMER_PRESCALE)))
  155. // Calculate the extra ticks of the PULSE timer between step pulses
  156. #define ADDED_STEP_TICKS (((MIN_STEPPER_PULSE_CYCLES) / (PULSE_TIMER_PRESCALE)) - (MIN_PULSE_TICKS))
  157. // But the user could be enforcing a minimum time, so the loop time is
  158. #define ISR_LOOP_CYCLES (ISR_LOOP_BASE_CYCLES + MAX(MIN_STEPPER_PULSE_CYCLES, MIN_ISR_LOOP_CYCLES))
  159. // If linear advance is enabled, then it is handled separately
  160. #if ENABLED(LIN_ADVANCE)
  161. // Estimate the minimum LA loop time
  162. #if ENABLED(MIXING_EXTRUDER) // ToDo: ???
  163. // HELP ME: What is what?
  164. // Directions are set up for MIXING_STEPPERS - like before.
  165. // Finding the right stepper may last up to MIXING_STEPPERS loops in get_next_stepper().
  166. // These loops are a bit faster than advancing a bresenham counter.
  167. // Always only one e-stepper is stepped.
  168. #define MIN_ISR_LA_LOOP_CYCLES ((MIXING_STEPPERS) * (ISR_STEPPER_CYCLES))
  169. #else
  170. #define MIN_ISR_LA_LOOP_CYCLES ISR_STEPPER_CYCLES
  171. #endif
  172. // And the real loop time
  173. #define ISR_LA_LOOP_CYCLES MAX(MIN_STEPPER_PULSE_CYCLES, MIN_ISR_LA_LOOP_CYCLES)
  174. #else
  175. #define ISR_LA_LOOP_CYCLES 0UL
  176. #endif
  177. // Now estimate the total ISR execution time in cycles given a step per ISR multiplier
  178. #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))
  179. // The maximum allowable stepping frequency when doing x128-x1 stepping (in Hz)
  180. #define MAX_STEP_ISR_FREQUENCY_128X ((F_CPU) / ISR_EXECUTION_CYCLES(128))
  181. #define MAX_STEP_ISR_FREQUENCY_64X ((F_CPU) / ISR_EXECUTION_CYCLES(64))
  182. #define MAX_STEP_ISR_FREQUENCY_32X ((F_CPU) / ISR_EXECUTION_CYCLES(32))
  183. #define MAX_STEP_ISR_FREQUENCY_16X ((F_CPU) / ISR_EXECUTION_CYCLES(16))
  184. #define MAX_STEP_ISR_FREQUENCY_8X ((F_CPU) / ISR_EXECUTION_CYCLES(8))
  185. #define MAX_STEP_ISR_FREQUENCY_4X ((F_CPU) / ISR_EXECUTION_CYCLES(4))
  186. #define MAX_STEP_ISR_FREQUENCY_2X ((F_CPU) / ISR_EXECUTION_CYCLES(2))
  187. #define MAX_STEP_ISR_FREQUENCY_1X ((F_CPU) / ISR_EXECUTION_CYCLES(1))
  188. // The minimum allowable frequency for step smoothing will be 1/10 of the maximum nominal frequency (in Hz)
  189. #define MIN_STEP_ISR_FREQUENCY MAX_STEP_ISR_FREQUENCY_1X
  190. //
  191. // Stepper class definition
  192. //
  193. #include "stepper_indirection.h"
  194. #ifdef __AVR__
  195. #include "speed_lookuptable.h"
  196. #endif
  197. #include "../module/planner.h"
  198. #include "../core/language.h"
  199. class Stepper {
  200. public:
  201. #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || Z_MULTI_ENDSTOPS || ENABLED(Z_STEPPER_AUTO_ALIGN)
  202. static bool separate_multi_axis;
  203. #endif
  204. #if HAS_MOTOR_CURRENT_PWM
  205. #ifndef PWM_MOTOR_CURRENT
  206. #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
  207. #endif
  208. static uint32_t motor_current_setting[3];
  209. #endif
  210. private:
  211. static block_t* current_block; // A pointer to the block currently being traced
  212. static uint8_t last_direction_bits, // The next stepping-bits to be output
  213. axis_did_move; // Last Movement in the given direction is not null, as computed when the last movement was fetched from planner
  214. static bool abort_current_block; // Signals to the stepper that current block should be aborted
  215. // Last-moved extruder, as set when the last movement was fetched from planner
  216. #if EXTRUDERS < 2
  217. static constexpr uint8_t last_moved_extruder = 0;
  218. #elif DISABLED(MIXING_EXTRUDER)
  219. static uint8_t last_moved_extruder;
  220. #endif
  221. #if ENABLED(X_DUAL_ENDSTOPS)
  222. static bool locked_X_motor, locked_X2_motor;
  223. #endif
  224. #if ENABLED(Y_DUAL_ENDSTOPS)
  225. static bool locked_Y_motor, locked_Y2_motor;
  226. #endif
  227. #if Z_MULTI_ENDSTOPS || ENABLED(Z_STEPPER_AUTO_ALIGN)
  228. static bool locked_Z_motor, locked_Z2_motor;
  229. #endif
  230. #if ENABLED(Z_TRIPLE_ENDSTOPS) || (ENABLED(Z_STEPPER_AUTO_ALIGN) && ENABLED(Z_TRIPLE_STEPPER_DRIVERS))
  231. static bool locked_Z3_motor;
  232. #endif
  233. static uint32_t acceleration_time, deceleration_time; // time measured in Stepper Timer ticks
  234. static uint8_t steps_per_isr; // Count of steps to perform per Stepper ISR call
  235. #if ENABLED(ADAPTIVE_STEP_SMOOTHING)
  236. static uint8_t oversampling_factor; // Oversampling factor (log2(multiplier)) to increase temporal resolution of axis
  237. #else
  238. static constexpr uint8_t oversampling_factor = 0;
  239. #endif
  240. // Delta error variables for the Bresenham line tracer
  241. static int32_t delta_error[XYZE];
  242. static uint32_t advance_dividend[XYZE],
  243. advance_divisor,
  244. step_events_completed, // The number of step events executed in the current block
  245. accelerate_until, // The point from where we need to stop acceleration
  246. decelerate_after, // The point from where we need to start decelerating
  247. step_event_count; // The total event count for the current block
  248. #if EXTRUDERS > 1 || ENABLED(MIXING_EXTRUDER)
  249. static uint8_t stepper_extruder;
  250. #else
  251. static constexpr uint8_t stepper_extruder = 0;
  252. #endif
  253. #if ENABLED(S_CURVE_ACCELERATION)
  254. static int32_t bezier_A, // A coefficient in Bézier speed curve
  255. bezier_B, // B coefficient in Bézier speed curve
  256. bezier_C; // C coefficient in Bézier speed curve
  257. static uint32_t bezier_F, // F coefficient in Bézier speed curve
  258. bezier_AV; // AV coefficient in Bézier speed curve
  259. #ifdef __AVR__
  260. static bool A_negative; // If A coefficient was negative
  261. #endif
  262. static bool bezier_2nd_half; // If Bézier curve has been initialized or not
  263. #endif
  264. static uint32_t nextMainISR; // time remaining for the next Step ISR
  265. #if ENABLED(LIN_ADVANCE)
  266. static uint32_t nextAdvanceISR, LA_isr_rate;
  267. 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".
  268. static int8_t LA_steps;
  269. static bool LA_use_advance_lead;
  270. #endif // LIN_ADVANCE
  271. static int32_t ticks_nominal;
  272. #if DISABLED(S_CURVE_ACCELERATION)
  273. static uint32_t acc_step_rate; // needed for deceleration start point
  274. #endif
  275. static volatile int32_t endstops_trigsteps[XYZ];
  276. //
  277. // Positions of stepper motors, in step units
  278. //
  279. static volatile int32_t count_position[NUM_AXIS];
  280. //
  281. // Current direction of stepper motors (+1 or -1)
  282. //
  283. static int8_t count_direction[NUM_AXIS];
  284. public:
  285. //
  286. // Constructor / initializer
  287. //
  288. Stepper() { };
  289. // Initialize stepper hardware
  290. static void init();
  291. // Interrupt Service Routines
  292. // The ISR scheduler
  293. static void isr();
  294. // The stepper pulse phase ISR
  295. static void stepper_pulse_phase_isr();
  296. // The stepper block processing phase ISR
  297. static uint32_t stepper_block_phase_isr();
  298. #if ENABLED(LIN_ADVANCE)
  299. // The Linear advance stepper ISR
  300. static uint32_t advance_isr();
  301. #endif
  302. // Check if the given block is busy or not - Must not be called from ISR contexts
  303. static bool is_block_busy(const block_t* const block);
  304. // Get the position of a stepper, in steps
  305. static int32_t position(const AxisEnum axis);
  306. // Report the positions of the steppers, in steps
  307. static void report_positions();
  308. // The stepper subsystem goes to sleep when it runs out of things to execute. Call this
  309. // to notify the subsystem that it is time to go to work.
  310. static void wake_up();
  311. // Quickly stop all steppers
  312. FORCE_INLINE static void quick_stop() { abort_current_block = true; }
  313. // The direction of a single motor
  314. FORCE_INLINE static bool motor_direction(const AxisEnum axis) { return TEST(last_direction_bits, axis); }
  315. // The last movement direction was not null on the specified axis. Note that motor direction is not necessarily the same.
  316. FORCE_INLINE static bool axis_is_moving(const AxisEnum axis) { return TEST(axis_did_move, axis); }
  317. // The extruder associated to the last movement
  318. FORCE_INLINE static uint8_t movement_extruder() {
  319. return
  320. #if ENABLED(MIXING_EXTRUDER) || EXTRUDERS < 2
  321. 0
  322. #else
  323. last_moved_extruder
  324. #endif
  325. ;
  326. }
  327. // Handle a triggered endstop
  328. static void endstop_triggered(const AxisEnum axis);
  329. // Triggered position of an axis in steps
  330. static int32_t triggered_position(const AxisEnum axis);
  331. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  332. static void digitalPotWrite(const int16_t address, const int16_t value);
  333. static void digipot_current(const uint8_t driver, const int16_t current);
  334. #endif
  335. #if HAS_MICROSTEPS
  336. static void microstep_ms(const uint8_t driver, const int8_t ms1, const int8_t ms2, const int8_t ms3);
  337. static void microstep_mode(const uint8_t driver, const uint8_t stepping);
  338. static void microstep_readings();
  339. #endif
  340. #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || Z_MULTI_ENDSTOPS || ENABLED(Z_STEPPER_AUTO_ALIGN)
  341. FORCE_INLINE static void set_separate_multi_axis(const bool state) { separate_multi_axis = state; }
  342. #endif
  343. #if ENABLED(X_DUAL_ENDSTOPS)
  344. FORCE_INLINE static void set_x_lock(const bool state) { locked_X_motor = state; }
  345. FORCE_INLINE static void set_x2_lock(const bool state) { locked_X2_motor = state; }
  346. #endif
  347. #if ENABLED(Y_DUAL_ENDSTOPS)
  348. FORCE_INLINE static void set_y_lock(const bool state) { locked_Y_motor = state; }
  349. FORCE_INLINE static void set_y2_lock(const bool state) { locked_Y2_motor = state; }
  350. #endif
  351. #if Z_MULTI_ENDSTOPS || (ENABLED(Z_STEPPER_AUTO_ALIGN) && Z_MULTI_STEPPER_DRIVERS)
  352. FORCE_INLINE static void set_z_lock(const bool state) { locked_Z_motor = state; }
  353. FORCE_INLINE static void set_z2_lock(const bool state) { locked_Z2_motor = state; }
  354. #endif
  355. #if ENABLED(Z_TRIPLE_ENDSTOPS) || (ENABLED(Z_STEPPER_AUTO_ALIGN) && ENABLED(Z_TRIPLE_STEPPER_DRIVERS))
  356. FORCE_INLINE static void set_z3_lock(const bool state) { locked_Z3_motor = state; }
  357. #endif
  358. #if ENABLED(BABYSTEPPING)
  359. static void babystep(const AxisEnum axis, const bool direction); // perform a short step with a single stepper motor, outside of any convention
  360. #endif
  361. #if HAS_MOTOR_CURRENT_PWM
  362. static void refresh_motor_power();
  363. #endif
  364. // Set the current position in steps
  365. static inline void set_position(const int32_t &a, const int32_t &b, const int32_t &c, const int32_t &e) {
  366. planner.synchronize();
  367. const bool was_enabled = STEPPER_ISR_ENABLED();
  368. if (was_enabled) DISABLE_STEPPER_DRIVER_INTERRUPT();
  369. _set_position(a, b, c, e);
  370. if (was_enabled) ENABLE_STEPPER_DRIVER_INTERRUPT();
  371. }
  372. static inline void set_position(const AxisEnum a, const int32_t &v) {
  373. planner.synchronize();
  374. #ifdef __AVR__
  375. // Protect the access to the position. Only required for AVR, as
  376. // any 32bit CPU offers atomic access to 32bit variables
  377. const bool was_enabled = STEPPER_ISR_ENABLED();
  378. if (was_enabled) DISABLE_STEPPER_DRIVER_INTERRUPT();
  379. #endif
  380. count_position[a] = v;
  381. #ifdef __AVR__
  382. // Reenable Stepper ISR
  383. if (was_enabled) ENABLE_STEPPER_DRIVER_INTERRUPT();
  384. #endif
  385. }
  386. // Set direction bits for all steppers
  387. static void set_directions();
  388. private:
  389. // Set the current position in steps
  390. static void _set_position(const int32_t &a, const int32_t &b, const int32_t &c, const int32_t &e);
  391. FORCE_INLINE static uint32_t calc_timer_interval(uint32_t step_rate, uint8_t scale, uint8_t* loops) {
  392. uint32_t timer;
  393. // Scale the frequency, as requested by the caller
  394. step_rate <<= scale;
  395. uint8_t multistep = 1;
  396. #if DISABLED(DISABLE_MULTI_STEPPING)
  397. // The stepping frequency limits for each multistepping rate
  398. static const uint32_t limit[] PROGMEM = {
  399. ( MAX_STEP_ISR_FREQUENCY_1X ),
  400. ( MAX_STEP_ISR_FREQUENCY_2X >> 1),
  401. ( MAX_STEP_ISR_FREQUENCY_4X >> 2),
  402. ( MAX_STEP_ISR_FREQUENCY_8X >> 3),
  403. ( MAX_STEP_ISR_FREQUENCY_16X >> 4),
  404. ( MAX_STEP_ISR_FREQUENCY_32X >> 5),
  405. ( MAX_STEP_ISR_FREQUENCY_64X >> 6),
  406. (MAX_STEP_ISR_FREQUENCY_128X >> 7)
  407. };
  408. // Select the proper multistepping
  409. uint8_t idx = 0;
  410. while (idx < 7 && step_rate > (uint32_t)pgm_read_dword(&limit[idx])) {
  411. step_rate >>= 1;
  412. multistep <<= 1;
  413. ++idx;
  414. };
  415. #else
  416. NOMORE(step_rate, uint32_t(MAX_STEP_ISR_FREQUENCY_1X));
  417. #endif
  418. *loops = multistep;
  419. #ifdef CPU_32_BIT
  420. // In case of high-performance processor, it is able to calculate in real-time
  421. timer = uint32_t(STEPPER_TIMER_RATE) / step_rate;
  422. #else
  423. constexpr uint32_t min_step_rate = F_CPU / 500000U;
  424. NOLESS(step_rate, min_step_rate);
  425. step_rate -= min_step_rate; // Correct for minimal speed
  426. if (step_rate >= (8 * 256)) { // higher step rate
  427. const uint8_t tmp_step_rate = (step_rate & 0x00FF);
  428. const uint16_t table_address = (uint16_t)&speed_lookuptable_fast[(uint8_t)(step_rate >> 8)][0],
  429. gain = (uint16_t)pgm_read_word(table_address + 2);
  430. timer = MultiU16X8toH16(tmp_step_rate, gain);
  431. timer = (uint16_t)pgm_read_word(table_address) - timer;
  432. }
  433. else { // lower step rates
  434. uint16_t table_address = (uint16_t)&speed_lookuptable_slow[0][0];
  435. table_address += ((step_rate) >> 1) & 0xFFFC;
  436. timer = (uint16_t)pgm_read_word(table_address)
  437. - (((uint16_t)pgm_read_word(table_address + 2) * (uint8_t)(step_rate & 0x0007)) >> 3);
  438. }
  439. // (there is no need to limit the timer value here. All limits have been
  440. // applied above, and AVR is able to keep up at 30khz Stepping ISR rate)
  441. #endif
  442. return timer;
  443. }
  444. #if ENABLED(S_CURVE_ACCELERATION)
  445. static void _calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t av);
  446. static int32_t _eval_bezier_curve(const uint32_t curr_step);
  447. #endif
  448. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  449. static void digipot_init();
  450. #endif
  451. #if HAS_MICROSTEPS
  452. static void microstep_init();
  453. #endif
  454. };
  455. extern Stepper stepper;