Subversion Repositories Tronxy-X3A-Marlin

Rev

Details | Last modification | View Log | RSS feed

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