Subversion Repositories MK-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.cpp - A singleton object to execute motion plans using stepper motors
25
 * Marlin Firmware
26
 *
27
 * Derived from Grbl
28
 * Copyright (c) 2009-2011 Simen Svale Skogsrud
29
 *
30
 * Grbl is free software: you can redistribute it and/or modify
31
 * it under the terms of the GNU General Public License as published by
32
 * the Free Software Foundation, either version 3 of the License, or
33
 * (at your option) any later version.
34
 *
35
 * Grbl is distributed in the hope that it will be useful,
36
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38
 * GNU General Public License for more details.
39
 *
40
 * You should have received a copy of the GNU General Public License
41
 * along with Grbl.  If not, see <http://www.gnu.org/licenses/>.
42
 */
43
 
44
/**
45
 * Timer calculations informed by the 'RepRap cartesian firmware' by Zack Smith
46
 * and Philipp Tiefenbacher.
47
 */
48
 
49
/**
50
 *         __________________________
51
 *        /|                        |\     _________________         ^
52
 *       / |                        | \   /|               |\        |
53
 *      /  |                        |  \ / |               | \       s
54
 *     /   |                        |   |  |               |  \      p
55
 *    /    |                        |   |  |               |   \     e
56
 *   +-----+------------------------+---+--+---------------+----+    e
57
 *   |               BLOCK 1            |      BLOCK 2          |    d
58
 *
59
 *                           time ----->
60
 *
61
 *  The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
62
 *  first block->accelerate_until step_events_completed, then keeps going at constant speed until
63
 *  step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
64
 *  The slope of acceleration is calculated using v = u + at where t is the accumulated timer values of the steps so far.
65
 */
66
 
67
/**
68
 * Marlin uses the Bresenham algorithm. For a detailed explanation of theory and
69
 * method see https://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html
70
 */
71
 
72
/**
73
 * Jerk controlled movements planner added Apr 2018 by Eduardo José Tagle.
74
 * Equations based on Synthethos TinyG2 sources, but the fixed-point
75
 * implementation is new, as we are running the ISR with a variable period.
76
 * Also implemented the Bézier velocity curve evaluation in ARM assembler,
77
 * to avoid impacting ISR speed.
78
 */
79
 
80
#include "Marlin.h"
81
#include "stepper.h"
82
#include "endstops.h"
83
#include "planner.h"
84
#include "temperature.h"
85
#include "ultralcd.h"
86
#include "language.h"
87
#include "cardreader.h"
88
#include "speed_lookuptable.h"
89
#include "delay.h"
90
 
91
#if HAS_DIGIPOTSS
92
  #include <SPI.h>
93
#endif
94
 
95
Stepper stepper; // Singleton
96
 
97
// public:
98
 
99
#if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
100
  bool Stepper::homing_dual_axis = false;
101
#endif
102
 
103
#if HAS_MOTOR_CURRENT_PWM
104
  uint32_t Stepper::motor_current_setting[3]; // Initialized by settings.load()
105
#endif
106
 
107
// private:
108
 
109
block_t* Stepper::current_block = NULL; // A pointer to the block currently being traced
110
 
111
uint8_t Stepper::last_direction_bits = 0,
112
        Stepper::axis_did_move;
113
 
114
bool Stepper::abort_current_block;
115
 
116
#if DISABLED(MIXING_EXTRUDER)
117
  uint8_t Stepper::last_moved_extruder = 0xFF;
118
#endif
119
 
120
#if ENABLED(X_DUAL_ENDSTOPS)
121
  bool Stepper::locked_X_motor = false, Stepper::locked_X2_motor = false;
122
#endif
123
#if ENABLED(Y_DUAL_ENDSTOPS)
124
  bool Stepper::locked_Y_motor = false, Stepper::locked_Y2_motor = false;
125
#endif
126
#if ENABLED(Z_DUAL_ENDSTOPS)
127
  bool Stepper::locked_Z_motor = false, Stepper::locked_Z2_motor = false;
128
#endif
129
 
130
uint32_t Stepper::acceleration_time, Stepper::deceleration_time;
131
uint8_t Stepper::steps_per_isr;
132
 
133
#if DISABLED(ADAPTIVE_STEP_SMOOTHING)
134
  constexpr
135
#endif
136
    uint8_t Stepper::oversampling_factor;
137
 
138
int32_t Stepper::delta_error[NUM_AXIS] = { 0 };
139
uint32_t Stepper::advance_dividend[NUM_AXIS] = { 0 },
140
         Stepper::advance_divisor = 0,
141
         Stepper::step_events_completed = 0, // The number of step events executed in the current block
142
         Stepper::accelerate_until,          // The point from where we need to stop acceleration
143
         Stepper::decelerate_after,          // The point from where we need to start decelerating
144
         Stepper::step_event_count;          // The total event count for the current block
145
 
146
#if ENABLED(MIXING_EXTRUDER)
147
  int32_t Stepper::delta_error_m[MIXING_STEPPERS];
148
  uint32_t Stepper::advance_dividend_m[MIXING_STEPPERS],
149
           Stepper::advance_divisor_m;
150
#else
151
  int8_t Stepper::active_extruder;           // Active extruder
152
#endif
153
 
154
#if ENABLED(S_CURVE_ACCELERATION)
155
  int32_t __attribute__((used)) Stepper::bezier_A __asm__("bezier_A");    // A coefficient in Bézier speed curve with alias for assembler
156
  int32_t __attribute__((used)) Stepper::bezier_B __asm__("bezier_B");    // B coefficient in Bézier speed curve with alias for assembler
157
  int32_t __attribute__((used)) Stepper::bezier_C __asm__("bezier_C");    // C coefficient in Bézier speed curve with alias for assembler
158
  uint32_t __attribute__((used)) Stepper::bezier_F __asm__("bezier_F");   // F coefficient in Bézier speed curve with alias for assembler
159
  uint32_t __attribute__((used)) Stepper::bezier_AV __asm__("bezier_AV"); // AV coefficient in Bézier speed curve with alias for assembler
160
  bool __attribute__((used)) Stepper::A_negative __asm__("A_negative");   // If A coefficient was negative
161
  bool Stepper::bezier_2nd_half;    // =false If Bézier curve has been initialized or not
162
#endif
163
 
164
uint32_t Stepper::nextMainISR = 0;
165
 
166
#if ENABLED(LIN_ADVANCE)
167
 
168
  constexpr uint32_t LA_ADV_NEVER = 0xFFFFFFFF;
169
  uint32_t Stepper::nextAdvanceISR = LA_ADV_NEVER,
170
           Stepper::LA_isr_rate = LA_ADV_NEVER;
171
  uint16_t Stepper::LA_current_adv_steps = 0,
172
           Stepper::LA_final_adv_steps,
173
           Stepper::LA_max_adv_steps;
174
 
175
  int8_t   Stepper::LA_steps = 0;
176
 
177
  bool Stepper::LA_use_advance_lead;
178
 
179
#endif // LIN_ADVANCE
180
 
181
int32_t Stepper::ticks_nominal = -1;
182
 
183
#if DISABLED(S_CURVE_ACCELERATION)
184
  uint32_t Stepper::acc_step_rate; // needed for deceleration start point
185
#endif
186
 
187
volatile int32_t Stepper::endstops_trigsteps[XYZ],
188
                 Stepper::count_position[NUM_AXIS] = { 0 };
189
int8_t Stepper::count_direction[NUM_AXIS] = {
190
  1, 1, 1, 1
191
  #if ENABLED(HANGPRINTER)
192
    , 1
193
  #endif
194
};
195
 
196
#if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
197
  #define DUAL_ENDSTOP_APPLY_STEP(A,V)                                                                                        \
198
    if (homing_dual_axis) {                                                                                                   \
199
      if (A##_HOME_DIR < 0) {                                                                                                 \
200
        if (!(TEST(endstops.state(), A##_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##_motor) A##_STEP_WRITE(V);    \
201
        if (!(TEST(endstops.state(), A##2_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##2_motor) A##2_STEP_WRITE(V); \
202
      }                                                                                                                       \
203
      else {                                                                                                                  \
204
        if (!(TEST(endstops.state(), A##_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##_motor) A##_STEP_WRITE(V);    \
205
        if (!(TEST(endstops.state(), A##2_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##2_motor) A##2_STEP_WRITE(V); \
206
      }                                                                                                                       \
207
    }                                                                                                                         \
208
    else {                                                                                                                    \
209
      A##_STEP_WRITE(V);                                                                                                      \
210
      A##2_STEP_WRITE(V);                                                                                                     \
211
    }
212
#endif
213
 
214
#if ENABLED(X_DUAL_STEPPER_DRIVERS)
215
  #define X_APPLY_DIR(v,Q) do{ X_DIR_WRITE(v); X2_DIR_WRITE((v) != INVERT_X2_VS_X_DIR); }while(0)
216
  #if ENABLED(X_DUAL_ENDSTOPS)
217
    #define X_APPLY_STEP(v,Q) DUAL_ENDSTOP_APPLY_STEP(X,v)
218
  #else
219
    #define X_APPLY_STEP(v,Q) do{ X_STEP_WRITE(v); X2_STEP_WRITE(v); }while(0)
220
  #endif
221
#elif ENABLED(DUAL_X_CARRIAGE)
222
  #define X_APPLY_DIR(v,ALWAYS) \
223
    if (extruder_duplication_enabled || ALWAYS) { \
224
      X_DIR_WRITE(v); \
225
      X2_DIR_WRITE(v); \
226
    } \
227
    else { \
228
      if (movement_extruder()) X2_DIR_WRITE(v); else X_DIR_WRITE(v); \
229
    }
230
  #define X_APPLY_STEP(v,ALWAYS) \
231
    if (extruder_duplication_enabled || ALWAYS) { \
232
      X_STEP_WRITE(v); \
233
      X2_STEP_WRITE(v); \
234
    } \
235
    else { \
236
      if (movement_extruder()) X2_STEP_WRITE(v); else X_STEP_WRITE(v); \
237
    }
238
#else
239
  #define X_APPLY_DIR(v,Q) X_DIR_WRITE(v)
240
  #define X_APPLY_STEP(v,Q) X_STEP_WRITE(v)
241
#endif
242
 
243
#if ENABLED(Y_DUAL_STEPPER_DRIVERS)
244
  #define Y_APPLY_DIR(v,Q) do{ Y_DIR_WRITE(v); Y2_DIR_WRITE((v) != INVERT_Y2_VS_Y_DIR); }while(0)
245
  #if ENABLED(Y_DUAL_ENDSTOPS)
246
    #define Y_APPLY_STEP(v,Q) DUAL_ENDSTOP_APPLY_STEP(Y,v)
247
  #else
248
    #define Y_APPLY_STEP(v,Q) do{ Y_STEP_WRITE(v); Y2_STEP_WRITE(v); }while(0)
249
  #endif
250
#else
251
  #define Y_APPLY_DIR(v,Q) Y_DIR_WRITE(v)
252
  #define Y_APPLY_STEP(v,Q) Y_STEP_WRITE(v)
253
#endif
254
 
255
#if ENABLED(Z_DUAL_STEPPER_DRIVERS)
256
  #define Z_APPLY_DIR(v,Q) do{ Z_DIR_WRITE(v); Z2_DIR_WRITE(v); }while(0)
257
  #if ENABLED(Z_DUAL_ENDSTOPS)
258
    #define Z_APPLY_STEP(v,Q) DUAL_ENDSTOP_APPLY_STEP(Z,v)
259
  #else
260
    #define Z_APPLY_STEP(v,Q) do{ Z_STEP_WRITE(v); Z2_STEP_WRITE(v); }while(0)
261
  #endif
262
#else
263
  #define Z_APPLY_DIR(v,Q) Z_DIR_WRITE(v)
264
  #define Z_APPLY_STEP(v,Q) Z_STEP_WRITE(v)
265
#endif
266
 
267
/**
268
 * Hangprinter's mapping {A,B,C,D} <-> {X,Y,Z,E1} happens here.
269
 * If you have two extruders: {A,B,C,D} <-> {X,Y,Z,E2}
270
 * ... etc up to max 4 extruders.
271
 * Place D connector on your first "free" extruder output.
272
 */
273
#if ENABLED(HANGPRINTER)
274
  #define A_APPLY_DIR(v,Q)  X_APPLY_DIR(v,Q)
275
  #define A_APPLY_STEP(v,Q) X_APPLY_STEP(v,Q)
276
 
277
  #define B_APPLY_DIR(v,Q)  Y_APPLY_DIR(v,Q)
278
  #define B_APPLY_STEP(v,Q) Y_APPLY_STEP(v,Q)
279
 
280
  #define C_APPLY_DIR(v,Q)  Z_APPLY_DIR(v,Q)
281
  #define C_APPLY_STEP(v,Q) Z_APPLY_STEP(v,Q)
282
 
283
  #define __D_APPLY(I,T,v) E##I##_##T##_WRITE(v)
284
  #define _D_APPLY(I,T,v) __D_APPLY(I,T,v)
285
  #define D_APPLY_DIR(v,Q)  _D_APPLY(EXTRUDERS, DIR, v)
286
  #define D_APPLY_STEP(v,Q) _D_APPLY(EXTRUDERS, STEP, v)
287
#endif
288
 
289
#if DISABLED(MIXING_EXTRUDER)
290
  #define E_APPLY_STEP(v,Q) E_STEP_WRITE(active_extruder, v)
291
#endif
292
 
293
// intRes = longIn1 * longIn2 >> 24
294
// uses:
295
// A[tmp] to store 0
296
// B[tmp] to store bits 16-23 of the 48bit result. The top bit is used to round the two byte result.
297
// note that the lower two bytes and the upper byte of the 48bit result are not calculated.
298
// this can cause the result to be out by one as the lower bytes may cause carries into the upper ones.
299
// B A are bits 24-39 and are the returned value
300
// C B A is longIn1
301
// D C B A is longIn2
302
//
303
static FORCE_INLINE uint16_t MultiU24X32toH16(uint32_t longIn1, uint32_t longIn2) {
304
  register uint8_t tmp1;
305
  register uint8_t tmp2;
306
  register uint16_t intRes;
307
  __asm__ __volatile__(
308
    A("clr %[tmp1]")
309
    A("mul %A[longIn1], %B[longIn2]")
310
    A("mov %[tmp2], r1")
311
    A("mul %B[longIn1], %C[longIn2]")
312
    A("movw %A[intRes], r0")
313
    A("mul %C[longIn1], %C[longIn2]")
314
    A("add %B[intRes], r0")
315
    A("mul %C[longIn1], %B[longIn2]")
316
    A("add %A[intRes], r0")
317
    A("adc %B[intRes], r1")
318
    A("mul %A[longIn1], %C[longIn2]")
319
    A("add %[tmp2], r0")
320
    A("adc %A[intRes], r1")
321
    A("adc %B[intRes], %[tmp1]")
322
    A("mul %B[longIn1], %B[longIn2]")
323
    A("add %[tmp2], r0")
324
    A("adc %A[intRes], r1")
325
    A("adc %B[intRes], %[tmp1]")
326
    A("mul %C[longIn1], %A[longIn2]")
327
    A("add %[tmp2], r0")
328
    A("adc %A[intRes], r1")
329
    A("adc %B[intRes], %[tmp1]")
330
    A("mul %B[longIn1], %A[longIn2]")
331
    A("add %[tmp2], r1")
332
    A("adc %A[intRes], %[tmp1]")
333
    A("adc %B[intRes], %[tmp1]")
334
    A("lsr %[tmp2]")
335
    A("adc %A[intRes], %[tmp1]")
336
    A("adc %B[intRes], %[tmp1]")
337
    A("mul %D[longIn2], %A[longIn1]")
338
    A("add %A[intRes], r0")
339
    A("adc %B[intRes], r1")
340
    A("mul %D[longIn2], %B[longIn1]")
341
    A("add %B[intRes], r0")
342
    A("clr r1")
343
      : [intRes] "=&r" (intRes),
344
        [tmp1] "=&r" (tmp1),
345
        [tmp2] "=&r" (tmp2)
346
      : [longIn1] "d" (longIn1),
347
        [longIn2] "d" (longIn2)
348
      : "cc"
349
  );
350
  return intRes;
351
}
352
 
353
void Stepper::wake_up() {
354
  // TCNT1 = 0;
355
  ENABLE_STEPPER_DRIVER_INTERRUPT();
356
}
357
 
358
/**
359
 * Set the stepper direction of each axis
360
 *
361
 *   COREXY: X_AXIS=A_AXIS and Y_AXIS=B_AXIS
362
 *   COREXZ: X_AXIS=A_AXIS and Z_AXIS=C_AXIS
363
 *   COREYZ: Y_AXIS=B_AXIS and Z_AXIS=C_AXIS
364
 */
365
void Stepper::set_directions() {
366
 
367
  #define SET_STEP_DIR(A) \
368
    if (motor_direction(_AXIS(A))) { \
369
      A##_APPLY_DIR(INVERT_## A##_DIR, false); \
370
      count_direction[_AXIS(A)] = -1; \
371
    } \
372
    else { \
373
      A##_APPLY_DIR(!INVERT_## A##_DIR, false); \
374
      count_direction[_AXIS(A)] = 1; \
375
    }
376
 
377
  #if HAS_X_DIR
378
    SET_STEP_DIR(X); // A
379
  #endif
380
  #if HAS_Y_DIR
381
    SET_STEP_DIR(Y); // B
382
  #endif
383
  #if HAS_Z_DIR
384
    SET_STEP_DIR(Z); // C
385
  #endif
386
  #if ENABLED(HANGPRINTER)
387
    SET_STEP_DIR(D);
388
  #endif
389
 
390
  #if DISABLED(LIN_ADVANCE)
391
    #if ENABLED(MIXING_EXTRUDER)
392
      if (motor_direction(E_AXIS)) {
393
        MIXING_STEPPERS_LOOP(j) REV_E_DIR(j);
394
        count_direction[E_AXIS] = -1;
395
      }
396
      else {
397
        MIXING_STEPPERS_LOOP(j) NORM_E_DIR(j);
398
        count_direction[E_AXIS] = 1;
399
      }
400
    #else
401
      if (motor_direction(E_AXIS)) {
402
        REV_E_DIR(active_extruder);
403
        count_direction[E_AXIS] = -1;
404
      }
405
      else {
406
        NORM_E_DIR(active_extruder);
407
        count_direction[E_AXIS] = 1;
408
      }
409
    #endif
410
  #endif // !LIN_ADVANCE
411
 
412
  // A small delay may be needed after changing direction
413
  #if MINIMUM_STEPPER_DIR_DELAY > 0
414
    DELAY_NS(MINIMUM_STEPPER_DIR_DELAY);
415
  #endif
416
}
417
 
418
#if ENABLED(S_CURVE_ACCELERATION)
419
  /**
420
   *  This uses a quintic (fifth-degree) Bézier polynomial for the velocity curve, giving
421
   *  a "linear pop" velocity curve; with pop being the sixth derivative of position:
422
   *  velocity - 1st, acceleration - 2nd, jerk - 3rd, snap - 4th, crackle - 5th, pop - 6th
423
   *
424
   *  The Bézier curve takes the form:
425
   *
426
   *  V(t) = P_0 * B_0(t) + P_1 * B_1(t) + P_2 * B_2(t) + P_3 * B_3(t) + P_4 * B_4(t) + P_5 * B_5(t)
427
   *
428
   *  Where 0 <= t <= 1, and V(t) is the velocity. P_0 through P_5 are the control points, and B_0(t)
429
   *  through B_5(t) are the Bernstein basis as follows:
430
   *
431
   *        B_0(t) =   (1-t)^5        =   -t^5 +  5t^4 - 10t^3 + 10t^2 -  5t   +   1
432
   *        B_1(t) =  5(1-t)^4 * t    =   5t^5 - 20t^4 + 30t^3 - 20t^2 +  5t
433
   *        B_2(t) = 10(1-t)^3 * t^2  = -10t^5 + 30t^4 - 30t^3 + 10t^2
434
   *        B_3(t) = 10(1-t)^2 * t^3  =  10t^5 - 20t^4 + 10t^3
435
   *        B_4(t) =  5(1-t)   * t^4  =  -5t^5 +  5t^4
436
   *        B_5(t) =             t^5  =    t^5
437
   *                                      ^       ^       ^       ^       ^       ^
438
   *                                      |       |       |       |       |       |
439
   *                                      A       B       C       D       E       F
440
   *
441
   *  Unfortunately, we cannot use forward-differencing to calculate each position through
442
   *  the curve, as Marlin uses variable timer periods. So, we require a formula of the form:
443
   *
444
   *        V_f(t) = A*t^5 + B*t^4 + C*t^3 + D*t^2 + E*t + F
445
   *
446
   *  Looking at the above B_0(t) through B_5(t) expanded forms, if we take the coefficients of t^5
447
   *  through t of the Bézier form of V(t), we can determine that:
448
   *
449
   *        A =    -P_0 +  5*P_1 - 10*P_2 + 10*P_3 -  5*P_4 +  P_5
450
   *        B =   5*P_0 - 20*P_1 + 30*P_2 - 20*P_3 +  5*P_4
451
   *        C = -10*P_0 + 30*P_1 - 30*P_2 + 10*P_3
452
   *        D =  10*P_0 - 20*P_1 + 10*P_2
453
   *        E = - 5*P_0 +  5*P_1
454
   *        F =     P_0
455
   *
456
   *  Now, since we will (currently) *always* want the initial acceleration and jerk values to be 0,
457
   *  We set P_i = P_0 = P_1 = P_2 (initial velocity), and P_t = P_3 = P_4 = P_5 (target velocity),
458
   *  which, after simplification, resolves to:
459
   *
460
   *        A = - 6*P_i +  6*P_t =  6*(P_t - P_i)
461
   *        B =  15*P_i - 15*P_t = 15*(P_i - P_t)
462
   *        C = -10*P_i + 10*P_t = 10*(P_t - P_i)
463
   *        D = 0
464
   *        E = 0
465
   *        F = P_i
466
   *
467
   *  As the t is evaluated in non uniform steps here, there is no other way rather than evaluating
468
   *  the Bézier curve at each point:
469
   *
470
   *        V_f(t) = A*t^5 + B*t^4 + C*t^3 + F          [0 <= t <= 1]
471
   *
472
   * Floating point arithmetic execution time cost is prohibitive, so we will transform the math to
473
   * use fixed point values to be able to evaluate it in realtime. Assuming a maximum of 250000 steps
474
   * per second (driver pulses should at least be 2µS hi/2µS lo), and allocating 2 bits to avoid
475
   * overflows on the evaluation of the Bézier curve, means we can use
476
   *
477
   *   t: unsigned Q0.32 (0 <= t < 1) |range 0 to 0xFFFFFFFF unsigned
478
   *   A:   signed Q24.7 ,            |range = +/- 250000 * 6 * 128 = +/- 192000000 = 0x0B71B000 | 28 bits + sign
479
   *   B:   signed Q24.7 ,            |range = +/- 250000 *15 * 128 = +/- 480000000 = 0x1C9C3800 | 29 bits + sign
480
   *   C:   signed Q24.7 ,            |range = +/- 250000 *10 * 128 = +/- 320000000 = 0x1312D000 | 29 bits + sign
481
   *   F:   signed Q24.7 ,            |range = +/- 250000     * 128 =      32000000 = 0x01E84800 | 25 bits + sign
482
   *
483
   * The trapezoid generator state contains the following information, that we will use to create and evaluate
484
   * the Bézier curve:
485
   *
486
   *  blk->step_event_count [TS] = The total count of steps for this movement. (=distance)
487
   *  blk->initial_rate     [VI] = The initial steps per second (=velocity)
488
   *  blk->final_rate       [VF] = The ending steps per second  (=velocity)
489
   *  and the count of events completed (step_events_completed) [CS] (=distance until now)
490
   *
491
   *  Note the abbreviations we use in the following formulae are between []s
492
   *
493
   *  For Any 32bit CPU:
494
   *
495
   *    At the start of each trapezoid, calculate the coefficients A,B,C,F and Advance [AV], as follows:
496
   *
497
   *      A =  6*128*(VF - VI) =  768*(VF - VI)
498
   *      B = 15*128*(VI - VF) = 1920*(VI - VF)
499
   *      C = 10*128*(VF - VI) = 1280*(VF - VI)
500
   *      F =    128*VI        =  128*VI
501
   *     AV = (1<<32)/TS      ~= 0xFFFFFFFF / TS (To use ARM UDIV, that is 32 bits) (this is computed at the planner, to offload expensive calculations from the ISR)
502
   *
503
   *    And for each point, evaluate the curve with the following sequence:
504
   *
505
   *      void lsrs(uint32_t& d, uint32_t s, int cnt) {
506
   *        d = s >> cnt;
507
   *      }
508
   *      void lsls(uint32_t& d, uint32_t s, int cnt) {
509
   *        d = s << cnt;
510
   *      }
511
   *      void lsrs(int32_t& d, uint32_t s, int cnt) {
512
   *        d = uint32_t(s) >> cnt;
513
   *      }
514
   *      void lsls(int32_t& d, uint32_t s, int cnt) {
515
   *        d = uint32_t(s) << cnt;
516
   *      }
517
   *      void umull(uint32_t& rlo, uint32_t& rhi, uint32_t op1, uint32_t op2) {
518
   *        uint64_t res = uint64_t(op1) * op2;
519
   *        rlo = uint32_t(res & 0xFFFFFFFF);
520
   *        rhi = uint32_t((res >> 32) & 0xFFFFFFFF);
521
   *      }
522
   *      void smlal(int32_t& rlo, int32_t& rhi, int32_t op1, int32_t op2) {
523
   *        int64_t mul = int64_t(op1) * op2;
524
   *        int64_t s = int64_t(uint32_t(rlo) | ((uint64_t(uint32_t(rhi)) << 32U)));
525
   *        mul += s;
526
   *        rlo = int32_t(mul & 0xFFFFFFFF);
527
   *        rhi = int32_t((mul >> 32) & 0xFFFFFFFF);
528
   *      }
529
   *      int32_t _eval_bezier_curve_arm(uint32_t curr_step) {
530
   *        register uint32_t flo = 0;
531
   *        register uint32_t fhi = bezier_AV * curr_step;
532
   *        register uint32_t t = fhi;
533
   *        register int32_t alo = bezier_F;
534
   *        register int32_t ahi = 0;
535
   *        register int32_t A = bezier_A;
536
   *        register int32_t B = bezier_B;
537
   *        register int32_t C = bezier_C;
538
   *
539
   *        lsrs(ahi, alo, 1);          // a  = F << 31
540
   *        lsls(alo, alo, 31);         //
541
   *        umull(flo, fhi, fhi, t);    // f *= t
542
   *        umull(flo, fhi, fhi, t);    // f>>=32; f*=t
543
   *        lsrs(flo, fhi, 1);          //
544
   *        smlal(alo, ahi, flo, C);    // a+=(f>>33)*C
545
   *        umull(flo, fhi, fhi, t);    // f>>=32; f*=t
546
   *        lsrs(flo, fhi, 1);          //
547
   *        smlal(alo, ahi, flo, B);    // a+=(f>>33)*B
548
   *        umull(flo, fhi, fhi, t);    // f>>=32; f*=t
549
   *        lsrs(flo, fhi, 1);          // f>>=33;
550
   *        smlal(alo, ahi, flo, A);    // a+=(f>>33)*A;
551
   *        lsrs(alo, ahi, 6);          // a>>=38
552
   *
553
   *        return alo;
554
   *      }
555
   *
556
   *  This is rewritten in ARM assembly for optimal performance (43 cycles to execute).
557
   *
558
   *  For AVR, the precision of coefficients is scaled so the Bézier curve can be evaluated in real-time:
559
   *  Let's reduce precision as much as possible. After some experimentation we found that:
560
   *
561
   *    Assume t and AV with 24 bits is enough
562
   *       A =  6*(VF - VI)
563
   *       B = 15*(VI - VF)
564
   *       C = 10*(VF - VI)
565
   *       F =     VI
566
   *      AV = (1<<24)/TS   (this is computed at the planner, to offload expensive calculations from the ISR)
567
   *
568
   *    Instead of storing sign for each coefficient, we will store its absolute value,
569
   *    and flag the sign of the A coefficient, so we can save to store the sign bit.
570
   *    It always holds that sign(A) = - sign(B) = sign(C)
571
   *
572
   *     So, the resulting range of the coefficients are:
573
   *
574
   *       t: unsigned (0 <= t < 1) |range 0 to 0xFFFFFF unsigned
575
   *       A:   signed Q24 , range = 250000 * 6 = 1500000 = 0x16E360 | 21 bits
576
   *       B:   signed Q24 , range = 250000 *15 = 3750000 = 0x393870 | 22 bits
577
   *       C:   signed Q24 , range = 250000 *10 = 2500000 = 0x1312D0 | 21 bits
578
   *       F:   signed Q24 , range = 250000     =  250000 = 0x0ED090 | 20 bits
579
   *
580
   *    And for each curve, estimate its coefficients with:
581
   *
582
   *      void _calc_bezier_curve_coeffs(int32_t v0, int32_t v1, uint32_t av) {
583
   *       // Calculate the Bézier coefficients
584
   *       if (v1 < v0) {
585
   *         A_negative = true;
586
   *         bezier_A = 6 * (v0 - v1);
587
   *         bezier_B = 15 * (v0 - v1);
588
   *         bezier_C = 10 * (v0 - v1);
589
   *       }
590
   *       else {
591
   *         A_negative = false;
592
   *         bezier_A = 6 * (v1 - v0);
593
   *         bezier_B = 15 * (v1 - v0);
594
   *         bezier_C = 10 * (v1 - v0);
595
   *       }
596
   *       bezier_F = v0;
597
   *      }
598
   *
599
   *    And for each point, evaluate the curve with the following sequence:
600
   *
601
   *      // unsigned multiplication of 24 bits x 24bits, return upper 16 bits
602
   *      void umul24x24to16hi(uint16_t& r, uint24_t op1, uint24_t op2) {
603
   *        r = (uint64_t(op1) * op2) >> 8;
604
   *      }
605
   *      // unsigned multiplication of 16 bits x 16bits, return upper 16 bits
606
   *      void umul16x16to16hi(uint16_t& r, uint16_t op1, uint16_t op2) {
607
   *        r = (uint32_t(op1) * op2) >> 16;
608
   *      }
609
   *      // unsigned multiplication of 16 bits x 24bits, return upper 24 bits
610
   *      void umul16x24to24hi(uint24_t& r, uint16_t op1, uint24_t op2) {
611
   *        r = uint24_t((uint64_t(op1) * op2) >> 16);
612
   *      }
613
   *
614
   *      int32_t _eval_bezier_curve(uint32_t curr_step) {
615
   *        // To save computing, the first step is always the initial speed
616
   *        if (!curr_step)
617
   *          return bezier_F;
618
   *
619
   *        uint16_t t;
620
   *        umul24x24to16hi(t, bezier_AV, curr_step);   // t: Range 0 - 1^16 = 16 bits
621
   *        uint16_t f = t;
622
   *        umul16x16to16hi(f, f, t);           // Range 16 bits (unsigned)
623
   *        umul16x16to16hi(f, f, t);           // Range 16 bits : f = t^3  (unsigned)
624
   *        uint24_t acc = bezier_F;          // Range 20 bits (unsigned)
625
   *        if (A_negative) {
626
   *          uint24_t v;
627
   *          umul16x24to24hi(v, f, bezier_C);    // Range 21bits
628
   *          acc -= v;
629
   *          umul16x16to16hi(f, f, t);         // Range 16 bits : f = t^4  (unsigned)
630
   *          umul16x24to24hi(v, f, bezier_B);    // Range 22bits
631
   *          acc += v;
632
   *          umul16x16to16hi(f, f, t);         // Range 16 bits : f = t^5  (unsigned)
633
   *          umul16x24to24hi(v, f, bezier_A);    // Range 21bits + 15 = 36bits (plus sign)
634
   *          acc -= v;
635
   *        }
636
   *        else {
637
   *          uint24_t v;
638
   *          umul16x24to24hi(v, f, bezier_C);    // Range 21bits
639
   *          acc += v;
640
   *          umul16x16to16hi(f, f, t);       // Range 16 bits : f = t^4  (unsigned)
641
   *          umul16x24to24hi(v, f, bezier_B);    // Range 22bits
642
   *          acc -= v;
643
   *          umul16x16to16hi(f, f, t);               // Range 16 bits : f = t^5  (unsigned)
644
   *          umul16x24to24hi(v, f, bezier_A);    // Range 21bits + 15 = 36bits (plus sign)
645
   *          acc += v;
646
   *        }
647
   *        return acc;
648
   *      }
649
   *    These functions are translated to assembler for optimal performance.
650
   *    Coefficient calculation takes 70 cycles. Bezier point evaluation takes 150 cycles.
651
   */
652
 
653
  // For AVR we use assembly to maximize speed
654
  void Stepper::_calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t av) {
655
 
656
    // Store advance
657
    bezier_AV = av;
658
 
659
    // Calculate the rest of the coefficients
660
    register uint8_t r2 = v0 & 0xFF;
661
    register uint8_t r3 = (v0 >> 8) & 0xFF;
662
    register uint8_t r12 = (v0 >> 16) & 0xFF;
663
    register uint8_t r5 = v1 & 0xFF;
664
    register uint8_t r6 = (v1 >> 8) & 0xFF;
665
    register uint8_t r7 = (v1 >> 16) & 0xFF;
666
    register uint8_t r4,r8,r9,r10,r11;
667
 
668
    __asm__ __volatile__(
669
      /* Calculate the Bézier coefficients */
670
      /*  %10:%1:%0 = v0*/
671
      /*  %5:%4:%3 = v1*/
672
      /*  %7:%6:%10 = temporary*/
673
      /*  %9 = val (must be high register!)*/
674
      /*  %10 (must be high register!)*/
675
 
676
      /* Store initial velocity*/
677
      A("sts bezier_F, %0")
678
      A("sts bezier_F+1, %1")
679
      A("sts bezier_F+2, %10")    /* bezier_F = %10:%1:%0 = v0 */
680
 
681
      /* Get delta speed */
682
      A("ldi %2,-1")              /* %2 = 0xFF, means A_negative = true */
683
      A("clr %8")                 /* %8 = 0 */
684
      A("sub %0,%3")
685
      A("sbc %1,%4")
686
      A("sbc %10,%5")             /*  v0 -= v1, C=1 if result is negative */
687
      A("brcc 1f")                /* branch if result is positive (C=0), that means v0 >= v1 */
688
 
689
      /*  Result was negative, get the absolute value*/
690
      A("com %10")
691
      A("com %1")
692
      A("neg %0")
693
      A("sbc %1,%2")
694
      A("sbc %10,%2")             /* %10:%1:%0 +1  -> %10:%1:%0 = -(v0 - v1) = (v1 - v0) */
695
      A("clr %2")                 /* %2 = 0, means A_negative = false */
696
 
697
      /*  Store negative flag*/
698
      L("1")
699
      A("sts A_negative, %2")     /* Store negative flag */
700
 
701
      /*  Compute coefficients A,B and C   [20 cycles worst case]*/
702
      A("ldi %9,6")               /* %9 = 6 */
703
      A("mul %0,%9")              /* r1:r0 = 6*LO(v0-v1) */
704
      A("sts bezier_A, r0")
705
      A("mov %6,r1")
706
      A("clr %7")                 /* %7:%6:r0 = 6*LO(v0-v1) */
707
      A("mul %1,%9")              /* r1:r0 = 6*MI(v0-v1) */
708
      A("add %6,r0")
709
      A("adc %7,r1")              /* %7:%6:?? += 6*MI(v0-v1) << 8 */
710
      A("mul %10,%9")             /* r1:r0 = 6*HI(v0-v1) */
711
      A("add %7,r0")              /* %7:%6:?? += 6*HI(v0-v1) << 16 */
712
      A("sts bezier_A+1, %6")
713
      A("sts bezier_A+2, %7")     /* bezier_A = %7:%6:?? = 6*(v0-v1) [35 cycles worst] */
714
 
715
      A("ldi %9,15")              /* %9 = 15 */
716
      A("mul %0,%9")              /* r1:r0 = 5*LO(v0-v1) */
717
      A("sts bezier_B, r0")
718
      A("mov %6,r1")
719
      A("clr %7")                 /* %7:%6:?? = 5*LO(v0-v1) */
720
      A("mul %1,%9")              /* r1:r0 = 5*MI(v0-v1) */
721
      A("add %6,r0")
722
      A("adc %7,r1")              /* %7:%6:?? += 5*MI(v0-v1) << 8 */
723
      A("mul %10,%9")             /* r1:r0 = 5*HI(v0-v1) */
724
      A("add %7,r0")              /* %7:%6:?? += 5*HI(v0-v1) << 16 */
725
      A("sts bezier_B+1, %6")
726
      A("sts bezier_B+2, %7")     /* bezier_B = %7:%6:?? = 5*(v0-v1) [50 cycles worst] */
727
 
728
      A("ldi %9,10")              /* %9 = 10 */
729
      A("mul %0,%9")              /* r1:r0 = 10*LO(v0-v1) */
730
      A("sts bezier_C, r0")
731
      A("mov %6,r1")
732
      A("clr %7")                 /* %7:%6:?? = 10*LO(v0-v1) */
733
      A("mul %1,%9")              /* r1:r0 = 10*MI(v0-v1) */
734
      A("add %6,r0")
735
      A("adc %7,r1")              /* %7:%6:?? += 10*MI(v0-v1) << 8 */
736
      A("mul %10,%9")             /* r1:r0 = 10*HI(v0-v1) */
737
      A("add %7,r0")              /* %7:%6:?? += 10*HI(v0-v1) << 16 */
738
      A("sts bezier_C+1, %6")
739
      " sts bezier_C+2, %7"       /* bezier_C = %7:%6:?? = 10*(v0-v1) [65 cycles worst] */
740
      : "+r" (r2),
741
        "+d" (r3),
742
        "=r" (r4),
743
        "+r" (r5),
744
        "+r" (r6),
745
        "+r" (r7),
746
        "=r" (r8),
747
        "=r" (r9),
748
        "=r" (r10),
749
        "=d" (r11),
750
        "+r" (r12)
751
      :
752
      : "r0", "r1", "cc", "memory"
753
    );
754
  }
755
 
756
  FORCE_INLINE int32_t Stepper::_eval_bezier_curve(const uint32_t curr_step) {
757
 
758
    // If dealing with the first step, save expensive computing and return the initial speed
759
    if (!curr_step)
760
      return bezier_F;
761
 
762
    register uint8_t r0 = 0; /* Zero register */
763
    register uint8_t r2 = (curr_step) & 0xFF;
764
    register uint8_t r3 = (curr_step >> 8) & 0xFF;
765
    register uint8_t r4 = (curr_step >> 16) & 0xFF;
766
    register uint8_t r1,r5,r6,r7,r8,r9,r10,r11; /* Temporary registers */
767
 
768
    __asm__ __volatile(
769
      /* umul24x24to16hi(t, bezier_AV, curr_step);  t: Range 0 - 1^16 = 16 bits*/
770
      A("lds %9,bezier_AV")       /* %9 = LO(AV)*/
771
      A("mul %9,%2")              /* r1:r0 = LO(bezier_AV)*LO(curr_step)*/
772
      A("mov %7,r1")              /* %7 = LO(bezier_AV)*LO(curr_step) >> 8*/
773
      A("clr %8")                 /* %8:%7  = LO(bezier_AV)*LO(curr_step) >> 8*/
774
      A("lds %10,bezier_AV+1")    /* %10 = MI(AV)*/
775
      A("mul %10,%2")             /* r1:r0  = MI(bezier_AV)*LO(curr_step)*/
776
      A("add %7,r0")
777
      A("adc %8,r1")              /* %8:%7 += MI(bezier_AV)*LO(curr_step)*/
778
      A("lds r1,bezier_AV+2")     /* r11 = HI(AV)*/
779
      A("mul r1,%2")              /* r1:r0  = HI(bezier_AV)*LO(curr_step)*/
780
      A("add %8,r0")              /* %8:%7 += HI(bezier_AV)*LO(curr_step) << 8*/
781
      A("mul %9,%3")              /* r1:r0 =  LO(bezier_AV)*MI(curr_step)*/
782
      A("add %7,r0")
783
      A("adc %8,r1")              /* %8:%7 += LO(bezier_AV)*MI(curr_step)*/
784
      A("mul %10,%3")             /* r1:r0 =  MI(bezier_AV)*MI(curr_step)*/
785
      A("add %8,r0")              /* %8:%7 += LO(bezier_AV)*MI(curr_step) << 8*/
786
      A("mul %9,%4")              /* r1:r0 =  LO(bezier_AV)*HI(curr_step)*/
787
      A("add %8,r0")              /* %8:%7 += LO(bezier_AV)*HI(curr_step) << 8*/
788
      /* %8:%7 = t*/
789
 
790
      /* uint16_t f = t;*/
791
      A("mov %5,%7")              /* %6:%5 = f*/
792
      A("mov %6,%8")
793
      /* %6:%5 = f*/
794
 
795
      /* umul16x16to16hi(f, f, t); / Range 16 bits (unsigned) [17] */
796
      A("mul %5,%7")              /* r1:r0 = LO(f) * LO(t)*/
797
      A("mov %9,r1")              /* store MIL(LO(f) * LO(t)) in %9, we need it for rounding*/
798
      A("clr %10")                /* %10 = 0*/
799
      A("clr %11")                /* %11 = 0*/
800
      A("mul %5,%8")              /* r1:r0 = LO(f) * HI(t)*/
801
      A("add %9,r0")              /* %9 += LO(LO(f) * HI(t))*/
802
      A("adc %10,r1")             /* %10 = HI(LO(f) * HI(t))*/
803
      A("adc %11,%0")             /* %11 += carry*/
804
      A("mul %6,%7")              /* r1:r0 = HI(f) * LO(t)*/
805
      A("add %9,r0")              /* %9 += LO(HI(f) * LO(t))*/
806
      A("adc %10,r1")             /* %10 += HI(HI(f) * LO(t)) */
807
      A("adc %11,%0")             /* %11 += carry*/
808
      A("mul %6,%8")              /* r1:r0 = HI(f) * HI(t)*/
809
      A("add %10,r0")             /* %10 += LO(HI(f) * HI(t))*/
810
      A("adc %11,r1")             /* %11 += HI(HI(f) * HI(t))*/
811
      A("mov %5,%10")             /* %6:%5 = */
812
      A("mov %6,%11")             /* f = %10:%11*/
813
 
814
      /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^3  (unsigned) [17]*/
815
      A("mul %5,%7")              /* r1:r0 = LO(f) * LO(t)*/
816
      A("mov %1,r1")              /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
817
      A("clr %10")                /* %10 = 0*/
818
      A("clr %11")                /* %11 = 0*/
819
      A("mul %5,%8")              /* r1:r0 = LO(f) * HI(t)*/
820
      A("add %1,r0")              /* %1 += LO(LO(f) * HI(t))*/
821
      A("adc %10,r1")             /* %10 = HI(LO(f) * HI(t))*/
822
      A("adc %11,%0")             /* %11 += carry*/
823
      A("mul %6,%7")              /* r1:r0 = HI(f) * LO(t)*/
824
      A("add %1,r0")              /* %1 += LO(HI(f) * LO(t))*/
825
      A("adc %10,r1")             /* %10 += HI(HI(f) * LO(t))*/
826
      A("adc %11,%0")             /* %11 += carry*/
827
      A("mul %6,%8")              /* r1:r0 = HI(f) * HI(t)*/
828
      A("add %10,r0")             /* %10 += LO(HI(f) * HI(t))*/
829
      A("adc %11,r1")             /* %11 += HI(HI(f) * HI(t))*/
830
      A("mov %5,%10")             /* %6:%5 =*/
831
      A("mov %6,%11")             /* f = %10:%11*/
832
      /* [15 +17*2] = [49]*/
833
 
834
      /* %4:%3:%2 will be acc from now on*/
835
 
836
      /* uint24_t acc = bezier_F; / Range 20 bits (unsigned)*/
837
      A("clr %9")                 /* "decimal place we get for free"*/
838
      A("lds %2,bezier_F")
839
      A("lds %3,bezier_F+1")
840
      A("lds %4,bezier_F+2")      /* %4:%3:%2 = acc*/
841
 
842
      /* if (A_negative) {*/
843
      A("lds r0,A_negative")
844
      A("or r0,%0")               /* Is flag signalling negative? */
845
      A("brne 3f")                /* If yes, Skip next instruction if A was negative*/
846
      A("rjmp 1f")                /* Otherwise, jump */
847
 
848
      /* uint24_t v; */
849
      /* umul16x24to24hi(v, f, bezier_C); / Range 21bits [29] */
850
      /* acc -= v; */
851
      L("3")
852
      A("lds %10, bezier_C")      /* %10 = LO(bezier_C)*/
853
      A("mul %10,%5")             /* r1:r0 = LO(bezier_C) * LO(f)*/
854
      A("sub %9,r1")
855
      A("sbc %2,%0")
856
      A("sbc %3,%0")
857
      A("sbc %4,%0")              /* %4:%3:%2:%9 -= HI(LO(bezier_C) * LO(f))*/
858
      A("lds %11, bezier_C+1")    /* %11 = MI(bezier_C)*/
859
      A("mul %11,%5")             /* r1:r0 = MI(bezier_C) * LO(f)*/
860
      A("sub %9,r0")
861
      A("sbc %2,r1")
862
      A("sbc %3,%0")
863
      A("sbc %4,%0")              /* %4:%3:%2:%9 -= MI(bezier_C) * LO(f)*/
864
      A("lds %1, bezier_C+2")     /* %1 = HI(bezier_C)*/
865
      A("mul %1,%5")              /* r1:r0 = MI(bezier_C) * LO(f)*/
866
      A("sub %2,r0")
867
      A("sbc %3,r1")
868
      A("sbc %4,%0")              /* %4:%3:%2:%9 -= HI(bezier_C) * LO(f) << 8*/
869
      A("mul %10,%6")             /* r1:r0 = LO(bezier_C) * MI(f)*/
870
      A("sub %9,r0")
871
      A("sbc %2,r1")
872
      A("sbc %3,%0")
873
      A("sbc %4,%0")              /* %4:%3:%2:%9 -= LO(bezier_C) * MI(f)*/
874
      A("mul %11,%6")             /* r1:r0 = MI(bezier_C) * MI(f)*/
875
      A("sub %2,r0")
876
      A("sbc %3,r1")
877
      A("sbc %4,%0")              /* %4:%3:%2:%9 -= MI(bezier_C) * MI(f) << 8*/
878
      A("mul %1,%6")              /* r1:r0 = HI(bezier_C) * LO(f)*/
879
      A("sub %3,r0")
880
      A("sbc %4,r1")              /* %4:%3:%2:%9 -= HI(bezier_C) * LO(f) << 16*/
881
 
882
      /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^3  (unsigned) [17]*/
883
      A("mul %5,%7")              /* r1:r0 = LO(f) * LO(t)*/
884
      A("mov %1,r1")              /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
885
      A("clr %10")                /* %10 = 0*/
886
      A("clr %11")                /* %11 = 0*/
887
      A("mul %5,%8")              /* r1:r0 = LO(f) * HI(t)*/
888
      A("add %1,r0")              /* %1 += LO(LO(f) * HI(t))*/
889
      A("adc %10,r1")             /* %10 = HI(LO(f) * HI(t))*/
890
      A("adc %11,%0")             /* %11 += carry*/
891
      A("mul %6,%7")              /* r1:r0 = HI(f) * LO(t)*/
892
      A("add %1,r0")              /* %1 += LO(HI(f) * LO(t))*/
893
      A("adc %10,r1")             /* %10 += HI(HI(f) * LO(t))*/
894
      A("adc %11,%0")             /* %11 += carry*/
895
      A("mul %6,%8")              /* r1:r0 = HI(f) * HI(t)*/
896
      A("add %10,r0")             /* %10 += LO(HI(f) * HI(t))*/
897
      A("adc %11,r1")             /* %11 += HI(HI(f) * HI(t))*/
898
      A("mov %5,%10")             /* %6:%5 =*/
899
      A("mov %6,%11")             /* f = %10:%11*/
900
 
901
      /* umul16x24to24hi(v, f, bezier_B); / Range 22bits [29]*/
902
      /* acc += v; */
903
      A("lds %10, bezier_B")      /* %10 = LO(bezier_B)*/
904
      A("mul %10,%5")             /* r1:r0 = LO(bezier_B) * LO(f)*/
905
      A("add %9,r1")
906
      A("adc %2,%0")
907
      A("adc %3,%0")
908
      A("adc %4,%0")              /* %4:%3:%2:%9 += HI(LO(bezier_B) * LO(f))*/
909
      A("lds %11, bezier_B+1")    /* %11 = MI(bezier_B)*/
910
      A("mul %11,%5")             /* r1:r0 = MI(bezier_B) * LO(f)*/
911
      A("add %9,r0")
912
      A("adc %2,r1")
913
      A("adc %3,%0")
914
      A("adc %4,%0")              /* %4:%3:%2:%9 += MI(bezier_B) * LO(f)*/
915
      A("lds %1, bezier_B+2")     /* %1 = HI(bezier_B)*/
916
      A("mul %1,%5")              /* r1:r0 = MI(bezier_B) * LO(f)*/
917
      A("add %2,r0")
918
      A("adc %3,r1")
919
      A("adc %4,%0")              /* %4:%3:%2:%9 += HI(bezier_B) * LO(f) << 8*/
920
      A("mul %10,%6")             /* r1:r0 = LO(bezier_B) * MI(f)*/
921
      A("add %9,r0")
922
      A("adc %2,r1")
923
      A("adc %3,%0")
924
      A("adc %4,%0")              /* %4:%3:%2:%9 += LO(bezier_B) * MI(f)*/
925
      A("mul %11,%6")             /* r1:r0 = MI(bezier_B) * MI(f)*/
926
      A("add %2,r0")
927
      A("adc %3,r1")
928
      A("adc %4,%0")              /* %4:%3:%2:%9 += MI(bezier_B) * MI(f) << 8*/
929
      A("mul %1,%6")              /* r1:r0 = HI(bezier_B) * LO(f)*/
930
      A("add %3,r0")
931
      A("adc %4,r1")              /* %4:%3:%2:%9 += HI(bezier_B) * LO(f) << 16*/
932
 
933
      /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^5  (unsigned) [17]*/
934
      A("mul %5,%7")              /* r1:r0 = LO(f) * LO(t)*/
935
      A("mov %1,r1")              /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
936
      A("clr %10")                /* %10 = 0*/
937
      A("clr %11")                /* %11 = 0*/
938
      A("mul %5,%8")              /* r1:r0 = LO(f) * HI(t)*/
939
      A("add %1,r0")              /* %1 += LO(LO(f) * HI(t))*/
940
      A("adc %10,r1")             /* %10 = HI(LO(f) * HI(t))*/
941
      A("adc %11,%0")             /* %11 += carry*/
942
      A("mul %6,%7")              /* r1:r0 = HI(f) * LO(t)*/
943
      A("add %1,r0")              /* %1 += LO(HI(f) * LO(t))*/
944
      A("adc %10,r1")             /* %10 += HI(HI(f) * LO(t))*/
945
      A("adc %11,%0")             /* %11 += carry*/
946
      A("mul %6,%8")              /* r1:r0 = HI(f) * HI(t)*/
947
      A("add %10,r0")             /* %10 += LO(HI(f) * HI(t))*/
948
      A("adc %11,r1")             /* %11 += HI(HI(f) * HI(t))*/
949
      A("mov %5,%10")             /* %6:%5 =*/
950
      A("mov %6,%11")             /* f = %10:%11*/
951
 
952
      /* umul16x24to24hi(v, f, bezier_A); / Range 21bits [29]*/
953
      /* acc -= v; */
954
      A("lds %10, bezier_A")      /* %10 = LO(bezier_A)*/
955
      A("mul %10,%5")             /* r1:r0 = LO(bezier_A) * LO(f)*/
956
      A("sub %9,r1")
957
      A("sbc %2,%0")
958
      A("sbc %3,%0")
959
      A("sbc %4,%0")              /* %4:%3:%2:%9 -= HI(LO(bezier_A) * LO(f))*/
960
      A("lds %11, bezier_A+1")    /* %11 = MI(bezier_A)*/
961
      A("mul %11,%5")             /* r1:r0 = MI(bezier_A) * LO(f)*/
962
      A("sub %9,r0")
963
      A("sbc %2,r1")
964
      A("sbc %3,%0")
965
      A("sbc %4,%0")              /* %4:%3:%2:%9 -= MI(bezier_A) * LO(f)*/
966
      A("lds %1, bezier_A+2")     /* %1 = HI(bezier_A)*/
967
      A("mul %1,%5")              /* r1:r0 = MI(bezier_A) * LO(f)*/
968
      A("sub %2,r0")
969
      A("sbc %3,r1")
970
      A("sbc %4,%0")              /* %4:%3:%2:%9 -= HI(bezier_A) * LO(f) << 8*/
971
      A("mul %10,%6")             /* r1:r0 = LO(bezier_A) * MI(f)*/
972
      A("sub %9,r0")
973
      A("sbc %2,r1")
974
      A("sbc %3,%0")
975
      A("sbc %4,%0")              /* %4:%3:%2:%9 -= LO(bezier_A) * MI(f)*/
976
      A("mul %11,%6")             /* r1:r0 = MI(bezier_A) * MI(f)*/
977
      A("sub %2,r0")
978
      A("sbc %3,r1")
979
      A("sbc %4,%0")              /* %4:%3:%2:%9 -= MI(bezier_A) * MI(f) << 8*/
980
      A("mul %1,%6")              /* r1:r0 = HI(bezier_A) * LO(f)*/
981
      A("sub %3,r0")
982
      A("sbc %4,r1")              /* %4:%3:%2:%9 -= HI(bezier_A) * LO(f) << 16*/
983
      A("jmp 2f")                 /* Done!*/
984
 
985
      L("1")
986
 
987
      /* uint24_t v; */
988
      /* umul16x24to24hi(v, f, bezier_C); / Range 21bits [29]*/
989
      /* acc += v; */
990
      A("lds %10, bezier_C")      /* %10 = LO(bezier_C)*/
991
      A("mul %10,%5")             /* r1:r0 = LO(bezier_C) * LO(f)*/
992
      A("add %9,r1")
993
      A("adc %2,%0")
994
      A("adc %3,%0")
995
      A("adc %4,%0")              /* %4:%3:%2:%9 += HI(LO(bezier_C) * LO(f))*/
996
      A("lds %11, bezier_C+1")    /* %11 = MI(bezier_C)*/
997
      A("mul %11,%5")             /* r1:r0 = MI(bezier_C) * LO(f)*/
998
      A("add %9,r0")
999
      A("adc %2,r1")
1000
      A("adc %3,%0")
1001
      A("adc %4,%0")              /* %4:%3:%2:%9 += MI(bezier_C) * LO(f)*/
1002
      A("lds %1, bezier_C+2")     /* %1 = HI(bezier_C)*/
1003
      A("mul %1,%5")              /* r1:r0 = MI(bezier_C) * LO(f)*/
1004
      A("add %2,r0")
1005
      A("adc %3,r1")
1006
      A("adc %4,%0")              /* %4:%3:%2:%9 += HI(bezier_C) * LO(f) << 8*/
1007
      A("mul %10,%6")             /* r1:r0 = LO(bezier_C) * MI(f)*/
1008
      A("add %9,r0")
1009
      A("adc %2,r1")
1010
      A("adc %3,%0")
1011
      A("adc %4,%0")              /* %4:%3:%2:%9 += LO(bezier_C) * MI(f)*/
1012
      A("mul %11,%6")             /* r1:r0 = MI(bezier_C) * MI(f)*/
1013
      A("add %2,r0")
1014
      A("adc %3,r1")
1015
      A("adc %4,%0")              /* %4:%3:%2:%9 += MI(bezier_C) * MI(f) << 8*/
1016
      A("mul %1,%6")              /* r1:r0 = HI(bezier_C) * LO(f)*/
1017
      A("add %3,r0")
1018
      A("adc %4,r1")              /* %4:%3:%2:%9 += HI(bezier_C) * LO(f) << 16*/
1019
 
1020
      /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^3  (unsigned) [17]*/
1021
      A("mul %5,%7")              /* r1:r0 = LO(f) * LO(t)*/
1022
      A("mov %1,r1")              /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
1023
      A("clr %10")                /* %10 = 0*/
1024
      A("clr %11")                /* %11 = 0*/
1025
      A("mul %5,%8")              /* r1:r0 = LO(f) * HI(t)*/
1026
      A("add %1,r0")              /* %1 += LO(LO(f) * HI(t))*/
1027
      A("adc %10,r1")             /* %10 = HI(LO(f) * HI(t))*/
1028
      A("adc %11,%0")             /* %11 += carry*/
1029
      A("mul %6,%7")              /* r1:r0 = HI(f) * LO(t)*/
1030
      A("add %1,r0")              /* %1 += LO(HI(f) * LO(t))*/
1031
      A("adc %10,r1")             /* %10 += HI(HI(f) * LO(t))*/
1032
      A("adc %11,%0")             /* %11 += carry*/
1033
      A("mul %6,%8")              /* r1:r0 = HI(f) * HI(t)*/
1034
      A("add %10,r0")             /* %10 += LO(HI(f) * HI(t))*/
1035
      A("adc %11,r1")             /* %11 += HI(HI(f) * HI(t))*/
1036
      A("mov %5,%10")             /* %6:%5 =*/
1037
      A("mov %6,%11")             /* f = %10:%11*/
1038
 
1039
      /* umul16x24to24hi(v, f, bezier_B); / Range 22bits [29]*/
1040
      /* acc -= v;*/
1041
      A("lds %10, bezier_B")      /* %10 = LO(bezier_B)*/
1042
      A("mul %10,%5")             /* r1:r0 = LO(bezier_B) * LO(f)*/
1043
      A("sub %9,r1")
1044
      A("sbc %2,%0")
1045
      A("sbc %3,%0")
1046
      A("sbc %4,%0")              /* %4:%3:%2:%9 -= HI(LO(bezier_B) * LO(f))*/
1047
      A("lds %11, bezier_B+1")    /* %11 = MI(bezier_B)*/
1048
      A("mul %11,%5")             /* r1:r0 = MI(bezier_B) * LO(f)*/
1049
      A("sub %9,r0")
1050
      A("sbc %2,r1")
1051
      A("sbc %3,%0")
1052
      A("sbc %4,%0")              /* %4:%3:%2:%9 -= MI(bezier_B) * LO(f)*/
1053
      A("lds %1, bezier_B+2")     /* %1 = HI(bezier_B)*/
1054
      A("mul %1,%5")              /* r1:r0 = MI(bezier_B) * LO(f)*/
1055
      A("sub %2,r0")
1056
      A("sbc %3,r1")
1057
      A("sbc %4,%0")              /* %4:%3:%2:%9 -= HI(bezier_B) * LO(f) << 8*/
1058
      A("mul %10,%6")             /* r1:r0 = LO(bezier_B) * MI(f)*/
1059
      A("sub %9,r0")
1060
      A("sbc %2,r1")
1061
      A("sbc %3,%0")
1062
      A("sbc %4,%0")              /* %4:%3:%2:%9 -= LO(bezier_B) * MI(f)*/
1063
      A("mul %11,%6")             /* r1:r0 = MI(bezier_B) * MI(f)*/
1064
      A("sub %2,r0")
1065
      A("sbc %3,r1")
1066
      A("sbc %4,%0")              /* %4:%3:%2:%9 -= MI(bezier_B) * MI(f) << 8*/
1067
      A("mul %1,%6")              /* r1:r0 = HI(bezier_B) * LO(f)*/
1068
      A("sub %3,r0")
1069
      A("sbc %4,r1")              /* %4:%3:%2:%9 -= HI(bezier_B) * LO(f) << 16*/
1070
 
1071
      /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^5  (unsigned) [17]*/
1072
      A("mul %5,%7")              /* r1:r0 = LO(f) * LO(t)*/
1073
      A("mov %1,r1")              /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
1074
      A("clr %10")                /* %10 = 0*/
1075
      A("clr %11")                /* %11 = 0*/
1076
      A("mul %5,%8")              /* r1:r0 = LO(f) * HI(t)*/
1077
      A("add %1,r0")              /* %1 += LO(LO(f) * HI(t))*/
1078
      A("adc %10,r1")             /* %10 = HI(LO(f) * HI(t))*/
1079
      A("adc %11,%0")             /* %11 += carry*/
1080
      A("mul %6,%7")              /* r1:r0 = HI(f) * LO(t)*/
1081
      A("add %1,r0")              /* %1 += LO(HI(f) * LO(t))*/
1082
      A("adc %10,r1")             /* %10 += HI(HI(f) * LO(t))*/
1083
      A("adc %11,%0")             /* %11 += carry*/
1084
      A("mul %6,%8")              /* r1:r0 = HI(f) * HI(t)*/
1085
      A("add %10,r0")             /* %10 += LO(HI(f) * HI(t))*/
1086
      A("adc %11,r1")             /* %11 += HI(HI(f) * HI(t))*/
1087
      A("mov %5,%10")             /* %6:%5 =*/
1088
      A("mov %6,%11")             /* f = %10:%11*/
1089
 
1090
      /* umul16x24to24hi(v, f, bezier_A); / Range 21bits [29]*/
1091
      /* acc += v; */
1092
      A("lds %10, bezier_A")      /* %10 = LO(bezier_A)*/
1093
      A("mul %10,%5")             /* r1:r0 = LO(bezier_A) * LO(f)*/
1094
      A("add %9,r1")
1095
      A("adc %2,%0")
1096
      A("adc %3,%0")
1097
      A("adc %4,%0")              /* %4:%3:%2:%9 += HI(LO(bezier_A) * LO(f))*/
1098
      A("lds %11, bezier_A+1")    /* %11 = MI(bezier_A)*/
1099
      A("mul %11,%5")             /* r1:r0 = MI(bezier_A) * LO(f)*/
1100
      A("add %9,r0")
1101
      A("adc %2,r1")
1102
      A("adc %3,%0")
1103
      A("adc %4,%0")              /* %4:%3:%2:%9 += MI(bezier_A) * LO(f)*/
1104
      A("lds %1, bezier_A+2")     /* %1 = HI(bezier_A)*/
1105
      A("mul %1,%5")              /* r1:r0 = MI(bezier_A) * LO(f)*/
1106
      A("add %2,r0")
1107
      A("adc %3,r1")
1108
      A("adc %4,%0")              /* %4:%3:%2:%9 += HI(bezier_A) * LO(f) << 8*/
1109
      A("mul %10,%6")             /* r1:r0 = LO(bezier_A) * MI(f)*/
1110
      A("add %9,r0")
1111
      A("adc %2,r1")
1112
      A("adc %3,%0")
1113
      A("adc %4,%0")              /* %4:%3:%2:%9 += LO(bezier_A) * MI(f)*/
1114
      A("mul %11,%6")             /* r1:r0 = MI(bezier_A) * MI(f)*/
1115
      A("add %2,r0")
1116
      A("adc %3,r1")
1117
      A("adc %4,%0")              /* %4:%3:%2:%9 += MI(bezier_A) * MI(f) << 8*/
1118
      A("mul %1,%6")              /* r1:r0 = HI(bezier_A) * LO(f)*/
1119
      A("add %3,r0")
1120
      A("adc %4,r1")              /* %4:%3:%2:%9 += HI(bezier_A) * LO(f) << 16*/
1121
      L("2")
1122
      " clr __zero_reg__"         /* C runtime expects r1 = __zero_reg__ = 0 */
1123
      : "+r"(r0),
1124
        "+r"(r1),
1125
        "+r"(r2),
1126
        "+r"(r3),
1127
        "+r"(r4),
1128
        "+r"(r5),
1129
        "+r"(r6),
1130
        "+r"(r7),
1131
        "+r"(r8),
1132
        "+r"(r9),
1133
        "+r"(r10),
1134
        "+r"(r11)
1135
      :
1136
      :"cc","r0","r1"
1137
    );
1138
    return (r2 | (uint16_t(r3) << 8)) | (uint32_t(r4) << 16);
1139
  }
1140
 
1141
#endif // S_CURVE_ACCELERATION
1142
 
1143
/**
1144
 * Stepper Driver Interrupt
1145
 *
1146
 * Directly pulses the stepper motors at high frequency.
1147
 */
1148
 
1149
HAL_STEP_TIMER_ISR {
1150
  HAL_timer_isr_prologue(STEP_TIMER_NUM);
1151
 
1152
  Stepper::isr();
1153
 
1154
  HAL_timer_isr_epilogue(STEP_TIMER_NUM);
1155
}
1156
 
1157
#define STEP_MULTIPLY(A,B) MultiU24X32toH16(A, B)
1158
 
1159
void Stepper::isr() {
1160
  DISABLE_ISRS();
1161
 
1162
  // Program timer compare for the maximum period, so it does NOT
1163
  // flag an interrupt while this ISR is running - So changes from small
1164
  // periods to big periods are respected and the timer does not reset to 0
1165
  HAL_timer_set_compare(STEP_TIMER_NUM, HAL_TIMER_TYPE_MAX);
1166
 
1167
  // Count of ticks for the next ISR
1168
  hal_timer_t next_isr_ticks = 0;
1169
 
1170
  // Limit the amount of iterations
1171
  uint8_t max_loops = 10;
1172
 
1173
  // We need this variable here to be able to use it in the following loop
1174
  hal_timer_t min_ticks;
1175
  do {
1176
    // Enable ISRs to reduce USART processing latency
1177
    ENABLE_ISRS();
1178
 
1179
    // Run main stepping pulse phase ISR if we have to
1180
    if (!nextMainISR) Stepper::stepper_pulse_phase_isr();
1181
 
1182
    #if ENABLED(LIN_ADVANCE)
1183
      // Run linear advance stepper ISR if we have to
1184
      if (!nextAdvanceISR) nextAdvanceISR = Stepper::advance_isr();
1185
    #endif
1186
 
1187
    // ^== Time critical. NOTHING besides pulse generation should be above here!!!
1188
 
1189
    // Run main stepping block processing ISR if we have to
1190
    if (!nextMainISR) nextMainISR = Stepper::stepper_block_phase_isr();
1191
 
1192
    uint32_t interval =
1193
      #if ENABLED(LIN_ADVANCE)
1194
        MIN(nextAdvanceISR, nextMainISR)  // Nearest time interval
1195
      #else
1196
        nextMainISR                       // Remaining stepper ISR time
1197
      #endif
1198
    ;
1199
 
1200
    // Limit the value to the maximum possible value of the timer
1201
    NOMORE(interval, HAL_TIMER_TYPE_MAX);
1202
 
1203
    // Compute the time remaining for the main isr
1204
    nextMainISR -= interval;
1205
 
1206
    #if ENABLED(LIN_ADVANCE)
1207
      // Compute the time remaining for the advance isr
1208
      if (nextAdvanceISR != LA_ADV_NEVER) nextAdvanceISR -= interval;
1209
    #endif
1210
 
1211
    /**
1212
     * This needs to avoid a race-condition caused by interleaving
1213
     * of interrupts required by both the LA and Stepper algorithms.
1214
     *
1215
     * Assume the following tick times for stepper pulses:
1216
     *   Stepper ISR (S):  1 1000 2000 3000 4000
1217
     *   Linear Adv. (E): 10 1010 2010 3010 4010
1218
     *
1219
     * The current algorithm tries to interleave them, giving:
1220
     *  1:S 10:E 1000:S 1010:E 2000:S 2010:E 3000:S 3010:E 4000:S 4010:E
1221
     *
1222
     * Ideal timing would yield these delta periods:
1223
     *  1:S  9:E  990:S   10:E  990:S   10:E  990:S   10:E  990:S   10:E
1224
     *
1225
     * But, since each event must fire an ISR with a minimum duration, the
1226
     * minimum delta might be 900, so deltas under 900 get rounded up:
1227
     *  900:S d900:E d990:S d900:E d990:S d900:E d990:S d900:E d990:S d900:E
1228
     *
1229
     * It works, but divides the speed of all motors by half, leading to a sudden
1230
     * reduction to 1/2 speed! Such jumps in speed lead to lost steps (not even
1231
     * accounting for double/quad stepping, which makes it even worse).
1232
     */
1233
 
1234
    // Compute the tick count for the next ISR
1235
    next_isr_ticks += interval;
1236
 
1237
    /**
1238
     * The following section must be done with global interrupts disabled.
1239
     * We want nothing to interrupt it, as that could mess the calculations
1240
     * we do for the next value to program in the period register of the
1241
     * stepper timer and lead to skipped ISRs (if the value we happen to program
1242
     * is less than the current count due to something preempting between the
1243
     * read and the write of the new period value).
1244
     */
1245
    DISABLE_ISRS();
1246
 
1247
    /**
1248
     * Get the current tick value + margin
1249
     * Assuming at least 6µs between calls to this ISR...
1250
     * On AVR the ISR epilogue+prologue is estimated at 100 instructions - Give 8µs as margin
1251
     * On ARM the ISR epilogue+prologue is estimated at 20 instructions - Give 1µs as margin
1252
     */
1253
    min_ticks = HAL_timer_get_count(STEP_TIMER_NUM) + hal_timer_t((STEPPER_TIMER_TICKS_PER_US) * 8);
1254
 
1255
    /**
1256
     * NB: If for some reason the stepper monopolizes the MPU, eventually the
1257
     * timer will wrap around (and so will 'next_isr_ticks'). So, limit the
1258
     * loop to 10 iterations. Beyond that, there's no way to ensure correct pulse
1259
     * timing, since the MCU isn't fast enough.
1260
     */
1261
    if (!--max_loops) next_isr_ticks = min_ticks;
1262
 
1263
    // Advance pulses if not enough time to wait for the next ISR
1264
  } while (next_isr_ticks < min_ticks);
1265
 
1266
  // Now 'next_isr_ticks' contains the period to the next Stepper ISR - And we are
1267
  // sure that the time has not arrived yet - Warrantied by the scheduler
1268
 
1269
  // Set the next ISR to fire at the proper time
1270
  HAL_timer_set_compare(STEP_TIMER_NUM, hal_timer_t(next_isr_ticks));
1271
 
1272
  // Don't forget to finally reenable interrupts
1273
  ENABLE_ISRS();
1274
}
1275
 
1276
/**
1277
 * This phase of the ISR should ONLY create the pulses for the steppers.
1278
 * This prevents jitter caused by the interval between the start of the
1279
 * interrupt and the start of the pulses. DON'T add any logic ahead of the
1280
 * call to this method that might cause variation in the timing. The aim
1281
 * is to keep pulse timing as regular as possible.
1282
 */
1283
#if ENABLED(UNREGISTERED_MOVE_SUPPORT)
1284
  #define COUNT_IT current_block->count_it
1285
#else
1286
  #define COUNT_IT true
1287
#endif
1288
 
1289
void Stepper::stepper_pulse_phase_isr() {
1290
 
1291
  // If we must abort the current block, do so!
1292
  if (abort_current_block) {
1293
    abort_current_block = false;
1294
    if (current_block) {
1295
      axis_did_move = 0;
1296
      current_block = NULL;
1297
      planner.discard_current_block();
1298
    }
1299
  }
1300
 
1301
  // If there is no current block, do nothing
1302
  if (!current_block) return;
1303
 
1304
  // Count of pending loops and events for this iteration
1305
  const uint32_t pending_events = step_event_count - step_events_completed;
1306
  uint8_t events_to_do = MIN(pending_events, steps_per_isr);
1307
 
1308
  // Just update the value we will get at the end of the loop
1309
  step_events_completed += events_to_do;
1310
 
1311
  // Get the timer count and estimate the end of the pulse
1312
  hal_timer_t pulse_end = HAL_timer_get_count(PULSE_TIMER_NUM) + hal_timer_t(MIN_PULSE_TICKS);
1313
 
1314
  const hal_timer_t added_step_ticks = hal_timer_t(ADDED_STEP_TICKS);
1315
 
1316
  // Take multiple steps per interrupt (For high speed moves)
1317
  do {
1318
 
1319
    #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
1320
    #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
1321
 
1322
    // Start an active pulse, if Bresenham says so, and update position
1323
    #define PULSE_START(AXIS) do{ \
1324
      delta_error[_AXIS(AXIS)] += advance_dividend[_AXIS(AXIS)]; \
1325
      if (delta_error[_AXIS(AXIS)] >= 0) { \
1326
        _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), 0); \
1327
        if (COUNT_IT) count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
1328
      } \
1329
    }while(0)
1330
 
1331
    // Stop an active pulse, if any, and adjust error term
1332
    #define PULSE_STOP(AXIS) do { \
1333
      if (delta_error[_AXIS(AXIS)] >= 0) { \
1334
        delta_error[_AXIS(AXIS)] -= advance_divisor; \
1335
        _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), 0); \
1336
      } \
1337
    }while(0)
1338
 
1339
    // Pulse start
1340
    #if ENABLED(HANGPRINTER)
1341
      #if HAS_A_STEP
1342
        PULSE_START(A);
1343
      #endif
1344
      #if HAS_B_STEP
1345
        PULSE_START(B);
1346
      #endif
1347
      #if HAS_C_STEP
1348
        PULSE_START(C);
1349
      #endif
1350
      #if HAS_D_STEP
1351
        PULSE_START(D);
1352
      #endif
1353
    #else
1354
      #if HAS_X_STEP
1355
        PULSE_START(X);
1356
      #endif
1357
      #if HAS_Y_STEP
1358
        PULSE_START(Y);
1359
      #endif
1360
      #if HAS_Z_STEP
1361
        PULSE_START(Z);
1362
      #endif
1363
    #endif // HANGPRINTER
1364
 
1365
    // Pulse E/Mixing extruders
1366
    #if ENABLED(LIN_ADVANCE)
1367
      // Tick the E axis, correct error term and update position
1368
      delta_error[E_AXIS] += advance_dividend[E_AXIS];
1369
      if (delta_error[E_AXIS] >= 0) {
1370
        if (COUNT_IT) count_position[E_AXIS] += count_direction[E_AXIS];
1371
        delta_error[E_AXIS] -= advance_divisor;
1372
 
1373
        // Don't step E here - But remember the number of steps to perform
1374
        motor_direction(E_AXIS) ? --LA_steps : ++LA_steps;
1375
      }
1376
    #else // !LIN_ADVANCE - use linear interpolation for E also
1377
      #if ENABLED(MIXING_EXTRUDER)
1378
 
1379
        // Tick the E axis
1380
        delta_error[E_AXIS] += advance_dividend[E_AXIS];
1381
        if (delta_error[E_AXIS] >= 0) {
1382
          if (COUNT_IT) count_position[E_AXIS] += count_direction[E_AXIS];
1383
          delta_error[E_AXIS] -= advance_divisor;
1384
        }
1385
 
1386
        // Tick the counters used for this mix in proper proportion
1387
        MIXING_STEPPERS_LOOP(j) {
1388
          // Step mixing steppers (proportionally)
1389
          delta_error_m[j] += advance_dividend_m[j];
1390
          // Step when the counter goes over zero
1391
          if (delta_error_m[j] >= 0) E_STEP_WRITE(j, !INVERT_E_STEP_PIN);
1392
        }
1393
 
1394
      #else // !MIXING_EXTRUDER
1395
        PULSE_START(E);
1396
      #endif
1397
    #endif // !LIN_ADVANCE
1398
 
1399
    #if MINIMUM_STEPPER_PULSE
1400
      // Just wait for the requested pulse duration
1401
      while (HAL_timer_get_count(PULSE_TIMER_NUM) < pulse_end) { /* nada */ }
1402
    #endif
1403
 
1404
    // Add the delay needed to ensure the maximum driver rate is enforced
1405
    if (signed(added_step_ticks) > 0) pulse_end += hal_timer_t(added_step_ticks);
1406
 
1407
    #if ENABLED(HANGPRINTER)
1408
      #if HAS_A_STEP
1409
        PULSE_STOP(A);
1410
      #endif
1411
      #if HAS_B_STEP
1412
        PULSE_STOP(B);
1413
      #endif
1414
      #if HAS_C_STEP
1415
        PULSE_STOP(C);
1416
      #endif
1417
      #if HAS_D_STEP
1418
        PULSE_STOP(D);
1419
      #endif
1420
    #else
1421
      #if HAS_X_STEP
1422
        PULSE_STOP(X);
1423
      #endif
1424
      #if HAS_Y_STEP
1425
        PULSE_STOP(Y);
1426
      #endif
1427
      #if HAS_Z_STEP
1428
        PULSE_STOP(Z);
1429
      #endif
1430
    #endif
1431
 
1432
    #if DISABLED(LIN_ADVANCE)
1433
      #if ENABLED(MIXING_EXTRUDER)
1434
        MIXING_STEPPERS_LOOP(j) {
1435
          if (delta_error_m[j] >= 0) {
1436
            delta_error_m[j] -= advance_divisor_m;
1437
            E_STEP_WRITE(j, INVERT_E_STEP_PIN);
1438
          }
1439
        }
1440
      #else // !MIXING_EXTRUDER
1441
        PULSE_STOP(E);
1442
      #endif
1443
    #endif // !LIN_ADVANCE
1444
 
1445
    // Decrement the count of pending pulses to do
1446
    --events_to_do;
1447
 
1448
    // For minimum pulse time wait after stopping pulses also
1449
    if (events_to_do) {
1450
      // Just wait for the requested pulse duration
1451
      while (HAL_timer_get_count(PULSE_TIMER_NUM) < pulse_end) { /* nada */ }
1452
      #if MINIMUM_STEPPER_PULSE
1453
        // Add to the value, the time that the pulse must be active (to be used on the next loop)
1454
        pulse_end += hal_timer_t(MIN_PULSE_TICKS);
1455
      #endif
1456
    }
1457
 
1458
  } while (events_to_do);
1459
}
1460
 
1461
// This is the last half of the stepper interrupt: This one processes and
1462
// properly schedules blocks from the planner. This is executed after creating
1463
// the step pulses, so it is not time critical, as pulses are already done.
1464
 
1465
uint32_t Stepper::stepper_block_phase_isr() {
1466
 
1467
  // If no queued movements, just wait 1ms for the next move
1468
  uint32_t interval = (STEPPER_TIMER_RATE / 1000);
1469
 
1470
  // If there is a current block
1471
  if (current_block) {
1472
 
1473
    // If current block is finished, reset pointer
1474
    if (step_events_completed >= step_event_count) {
1475
      axis_did_move = 0;
1476
      current_block = NULL;
1477
      planner.discard_current_block();
1478
    }
1479
    else {
1480
      // Step events not completed yet...
1481
 
1482
      // Are we in acceleration phase ?
1483
      if (step_events_completed <= accelerate_until) { // Calculate new timer value
1484
 
1485
        #if ENABLED(S_CURVE_ACCELERATION)
1486
          // Get the next speed to use (Jerk limited!)
1487
          uint32_t acc_step_rate =
1488
            acceleration_time < current_block->acceleration_time
1489
              ? _eval_bezier_curve(acceleration_time)
1490
              : current_block->cruise_rate;
1491
        #else
1492
          acc_step_rate = STEP_MULTIPLY(acceleration_time, current_block->acceleration_rate) + current_block->initial_rate;
1493
          NOMORE(acc_step_rate, current_block->nominal_rate);
1494
        #endif
1495
 
1496
        // acc_step_rate is in steps/second
1497
 
1498
        // step_rate to timer interval and steps per stepper isr
1499
        interval = calc_timer_interval(acc_step_rate, oversampling_factor, &steps_per_isr);
1500
        acceleration_time += interval;
1501
 
1502
        #if ENABLED(LIN_ADVANCE)
1503
          if (LA_use_advance_lead) {
1504
            // Fire ISR if final adv_rate is reached
1505
            if (LA_steps && LA_isr_rate != current_block->advance_speed) nextAdvanceISR = 0;
1506
          }
1507
          else if (LA_steps) nextAdvanceISR = 0;
1508
        #endif // LIN_ADVANCE
1509
      }
1510
      // Are we in Deceleration phase ?
1511
      else if (step_events_completed > decelerate_after) {
1512
        uint32_t step_rate;
1513
 
1514
        #if ENABLED(S_CURVE_ACCELERATION)
1515
          // If this is the 1st time we process the 2nd half of the trapezoid...
1516
          if (!bezier_2nd_half) {
1517
            // Initialize the Bézier speed curve
1518
            _calc_bezier_curve_coeffs(current_block->cruise_rate, current_block->final_rate, current_block->deceleration_time_inverse);
1519
            bezier_2nd_half = true;
1520
            // The first point starts at cruise rate. Just save evaluation of the Bézier curve
1521
            step_rate = current_block->cruise_rate;
1522
          }
1523
          else {
1524
            // Calculate the next speed to use
1525
            step_rate = deceleration_time < current_block->deceleration_time
1526
              ? _eval_bezier_curve(deceleration_time)
1527
              : current_block->final_rate;
1528
          }
1529
        #else
1530
 
1531
          // Using the old trapezoidal control
1532
          step_rate = STEP_MULTIPLY(deceleration_time, current_block->acceleration_rate);
1533
          if (step_rate < acc_step_rate) { // Still decelerating?
1534
            step_rate = acc_step_rate - step_rate;
1535
            NOLESS(step_rate, current_block->final_rate);
1536
          }
1537
          else
1538
            step_rate = current_block->final_rate;
1539
        #endif
1540
 
1541
        // step_rate is in steps/second
1542
 
1543
        // step_rate to timer interval and steps per stepper isr
1544
        interval = calc_timer_interval(step_rate, oversampling_factor, &steps_per_isr);
1545
        deceleration_time += interval;
1546
 
1547
        #if ENABLED(LIN_ADVANCE)
1548
          if (LA_use_advance_lead) {
1549
            // Wake up eISR on first deceleration loop and fire ISR if final adv_rate is reached
1550
            if (step_events_completed <= decelerate_after + steps_per_isr || (LA_steps && LA_isr_rate != current_block->advance_speed)) {
1551
              nextAdvanceISR = 0;
1552
              LA_isr_rate = current_block->advance_speed;
1553
            }
1554
          }
1555
          else if (LA_steps) nextAdvanceISR = 0;
1556
        #endif // LIN_ADVANCE
1557
      }
1558
      // We must be in cruise phase otherwise
1559
      else {
1560
 
1561
        #if ENABLED(LIN_ADVANCE)
1562
          // If there are any esteps, fire the next advance_isr "now"
1563
          if (LA_steps && LA_isr_rate != current_block->advance_speed) nextAdvanceISR = 0;
1564
        #endif
1565
 
1566
        // Calculate the ticks_nominal for this nominal speed, if not done yet
1567
        if (ticks_nominal < 0) {
1568
          // step_rate to timer interval and loops for the nominal speed
1569
          ticks_nominal = calc_timer_interval(current_block->nominal_rate, oversampling_factor, &steps_per_isr);
1570
        }
1571
 
1572
        // The timer interval is just the nominal value for the nominal speed
1573
        interval = ticks_nominal;
1574
      }
1575
    }
1576
  }
1577
 
1578
  // If there is no current block at this point, attempt to pop one from the buffer
1579
  // and prepare its movement
1580
  if (!current_block) {
1581
 
1582
    // Anything in the buffer?
1583
    if ((current_block = planner.get_current_block())) {
1584
 
1585
      // Sync block? Sync the stepper counts and return
1586
      while (TEST(current_block->flag, BLOCK_BIT_SYNC_POSITION)) {
1587
        _set_position(
1588
          current_block->position[A_AXIS], current_block->position[B_AXIS], current_block->position[C_AXIS],
1589
          #if ENABLED(HANGPRINTER)
1590
            current_block->position[D_AXIS],
1591
          #endif
1592
          current_block->position[E_AXIS]
1593
        );
1594
        planner.discard_current_block();
1595
 
1596
        // Try to get a new block
1597
        if (!(current_block = planner.get_current_block()))
1598
          return interval; // No more queued movements!
1599
      }
1600
 
1601
      // Flag all moving axes for proper endstop handling
1602
 
1603
      #if IS_CORE
1604
        // Define conditions for checking endstops
1605
        #define S_(N) current_block->steps[CORE_AXIS_##N]
1606
        #define D_(N) TEST(current_block->direction_bits, CORE_AXIS_##N)
1607
      #endif
1608
 
1609
      #if CORE_IS_XY || CORE_IS_XZ
1610
        /**
1611
         * Head direction in -X axis for CoreXY and CoreXZ bots.
1612
         *
1613
         * If steps differ, both axes are moving.
1614
         * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Y or Z, handled below)
1615
         * If DeltaA ==  DeltaB, the movement is only in the 1st axis (X)
1616
         */
1617
        #if ENABLED(COREXY) || ENABLED(COREXZ)
1618
          #define X_CMP ==
1619
        #else
1620
          #define X_CMP !=
1621
        #endif
1622
        #define X_MOVE_TEST ( S_(1) != S_(2) || (S_(1) > 0 && D_(1) X_CMP D_(2)) )
1623
      #else
1624
        #define X_MOVE_TEST !!current_block->steps[A_AXIS]
1625
      #endif
1626
 
1627
      #if CORE_IS_XY || CORE_IS_YZ
1628
        /**
1629
         * Head direction in -Y axis for CoreXY / CoreYZ bots.
1630
         *
1631
         * If steps differ, both axes are moving
1632
         * If DeltaA ==  DeltaB, the movement is only in the 1st axis (X or Y)
1633
         * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Y or Z)
1634
         */
1635
        #if ENABLED(COREYX) || ENABLED(COREYZ)
1636
          #define Y_CMP ==
1637
        #else
1638
          #define Y_CMP !=
1639
        #endif
1640
        #define Y_MOVE_TEST ( S_(1) != S_(2) || (S_(1) > 0 && D_(1) Y_CMP D_(2)) )
1641
      #else
1642
        #define Y_MOVE_TEST !!current_block->steps[B_AXIS]
1643
      #endif
1644
 
1645
      #if CORE_IS_XZ || CORE_IS_YZ
1646
        /**
1647
         * Head direction in -Z axis for CoreXZ or CoreYZ bots.
1648
         *
1649
         * If steps differ, both axes are moving
1650
         * If DeltaA ==  DeltaB, the movement is only in the 1st axis (X or Y, already handled above)
1651
         * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Z)
1652
         */
1653
        #if ENABLED(COREZX) || ENABLED(COREZY)
1654
          #define Z_CMP ==
1655
        #else
1656
          #define Z_CMP !=
1657
        #endif
1658
        #define Z_MOVE_TEST ( S_(1) != S_(2) || (S_(1) > 0 && D_(1) Z_CMP D_(2)) )
1659
      #else
1660
        #define Z_MOVE_TEST !!current_block->steps[C_AXIS]
1661
      #endif
1662
 
1663
      uint8_t axis_bits = 0;
1664
      if (X_MOVE_TEST) SBI(axis_bits, A_AXIS);
1665
      if (Y_MOVE_TEST) SBI(axis_bits, B_AXIS);
1666
      if (Z_MOVE_TEST) SBI(axis_bits, C_AXIS);
1667
      //if (!!current_block->steps[E_AXIS]) SBI(axis_bits, E_AXIS);
1668
      //if (!!current_block->steps[A_AXIS]) SBI(axis_bits, X_HEAD);
1669
      //if (!!current_block->steps[B_AXIS]) SBI(axis_bits, Y_HEAD);
1670
      //if (!!current_block->steps[C_AXIS]) SBI(axis_bits, Z_HEAD);
1671
      axis_did_move = axis_bits;
1672
 
1673
      // No acceleration / deceleration time elapsed so far
1674
      acceleration_time = deceleration_time = 0;
1675
 
1676
      uint8_t oversampling = 0;                         // Assume we won't use it
1677
 
1678
      #if ENABLED(ADAPTIVE_STEP_SMOOTHING)
1679
        // At this point, we must decide if we can use Stepper movement axis smoothing.
1680
        uint32_t max_rate = current_block->nominal_rate;  // Get the maximum rate (maximum event speed)
1681
        while (max_rate < MIN_STEP_ISR_FREQUENCY) {
1682
          max_rate <<= 1;
1683
          if (max_rate >= MAX_STEP_ISR_FREQUENCY_1X) break;
1684
          ++oversampling;
1685
        }
1686
        oversampling_factor = oversampling;
1687
      #endif
1688
 
1689
      // Based on the oversampling factor, do the calculations
1690
      step_event_count = current_block->step_event_count << oversampling;
1691
 
1692
      // Initialize Bresenham delta errors to 1/2
1693
      #if ENABLED(HANGPRINTER)
1694
        delta_error[A_AXIS] = delta_error[B_AXIS] = delta_error[C_AXIS] = delta_error[D_AXIS] = delta_error[E_AXIS] = -int32_t(step_event_count);
1695
      #else
1696
        delta_error[X_AXIS] = delta_error[Y_AXIS] = delta_error[Z_AXIS] = delta_error[E_AXIS] = -int32_t(step_event_count);
1697
      #endif
1698
 
1699
      // Calculate Bresenham dividends
1700
      #if ENABLED(HANGPRINTER)
1701
        advance_dividend[A_AXIS] = current_block->steps[A_AXIS] << 1;
1702
        advance_dividend[B_AXIS] = current_block->steps[B_AXIS] << 1;
1703
        advance_dividend[C_AXIS] = current_block->steps[C_AXIS] << 1;
1704
        advance_dividend[D_AXIS] = current_block->steps[D_AXIS] << 1;
1705
      #else
1706
        advance_dividend[X_AXIS] = current_block->steps[X_AXIS] << 1;
1707
        advance_dividend[Y_AXIS] = current_block->steps[Y_AXIS] << 1;
1708
        advance_dividend[Z_AXIS] = current_block->steps[Z_AXIS] << 1;
1709
      #endif
1710
      advance_dividend[E_AXIS] = current_block->steps[E_AXIS] << 1;
1711
 
1712
      // Calculate Bresenham divisor
1713
      advance_divisor = step_event_count << 1;
1714
 
1715
      // No step events completed so far
1716
      step_events_completed = 0;
1717
 
1718
      // Compute the acceleration and deceleration points
1719
      accelerate_until = current_block->accelerate_until << oversampling;
1720
      decelerate_after = current_block->decelerate_after << oversampling;
1721
 
1722
      #if ENABLED(MIXING_EXTRUDER)
1723
        const uint32_t e_steps = (
1724
          #if ENABLED(LIN_ADVANCE)
1725
            current_block->steps[E_AXIS]
1726
          #else
1727
            step_event_count
1728
          #endif
1729
        );
1730
        MIXING_STEPPERS_LOOP(i) {
1731
          delta_error_m[i] = -int32_t(e_steps);
1732
          advance_dividend_m[i] = current_block->mix_steps[i] << 1;
1733
        }
1734
        advance_divisor_m = e_steps << 1;
1735
      #else
1736
        active_extruder = current_block->active_extruder;
1737
      #endif
1738
 
1739
      // Initialize the trapezoid generator from the current block.
1740
      #if ENABLED(LIN_ADVANCE)
1741
        #if DISABLED(MIXING_EXTRUDER) && E_STEPPERS > 1
1742
          // If the now active extruder wasn't in use during the last move, its pressure is most likely gone.
1743
          if (active_extruder != last_moved_extruder) LA_current_adv_steps = 0;
1744
        #endif
1745
 
1746
        if ((LA_use_advance_lead = current_block->use_advance_lead)) {
1747
          LA_final_adv_steps = current_block->final_adv_steps;
1748
          LA_max_adv_steps = current_block->max_adv_steps;
1749
          //Start the ISR
1750
          nextAdvanceISR = 0;
1751
          LA_isr_rate = current_block->advance_speed;
1752
        }
1753
        else LA_isr_rate = LA_ADV_NEVER;
1754
      #endif
1755
 
1756
      if (current_block->direction_bits != last_direction_bits
1757
        #if DISABLED(MIXING_EXTRUDER)
1758
          || active_extruder != last_moved_extruder
1759
        #endif
1760
      ) {
1761
        last_direction_bits = current_block->direction_bits;
1762
        #if DISABLED(MIXING_EXTRUDER)
1763
          last_moved_extruder = active_extruder;
1764
        #endif
1765
        set_directions();
1766
      }
1767
 
1768
      // At this point, we must ensure the movement about to execute isn't
1769
      // trying to force the head against a limit switch. If using interrupt-
1770
      // driven change detection, and already against a limit then no call to
1771
      // the endstop_triggered method will be done and the movement will be
1772
      // done against the endstop. So, check the limits here: If the movement
1773
      // is against the limits, the block will be marked as to be killed, and
1774
      // on the next call to this ISR, will be discarded.
1775
      endstops.update();
1776
 
1777
      #if ENABLED(Z_LATE_ENABLE)
1778
        // If delayed Z enable, enable it now. This option will severely interfere with
1779
        // timing between pulses when chaining motion between blocks, and it could lead
1780
        // to lost steps in both X and Y axis, so avoid using it unless strictly necessary!!
1781
        if (current_block->steps[Z_AXIS]) enable_Z();
1782
      #endif
1783
 
1784
      // Mark the time_nominal as not calculated yet
1785
      ticks_nominal = -1;
1786
 
1787
      #if DISABLED(S_CURVE_ACCELERATION)
1788
        // Set as deceleration point the initial rate of the block
1789
        acc_step_rate = current_block->initial_rate;
1790
      #endif
1791
 
1792
      #if ENABLED(S_CURVE_ACCELERATION)
1793
        // Initialize the Bézier speed curve
1794
        _calc_bezier_curve_coeffs(current_block->initial_rate, current_block->cruise_rate, current_block->acceleration_time_inverse);
1795
        // We haven't started the 2nd half of the trapezoid
1796
        bezier_2nd_half = false;
1797
      #endif
1798
 
1799
      // Calculate the initial timer interval
1800
      interval = calc_timer_interval(current_block->initial_rate, oversampling_factor, &steps_per_isr);
1801
    }
1802
  }
1803
 
1804
  // Return the interval to wait
1805
  return interval;
1806
}
1807
 
1808
#if ENABLED(LIN_ADVANCE)
1809
 
1810
  // Timer interrupt for E. LA_steps is set in the main routine
1811
  uint32_t Stepper::advance_isr() {
1812
    uint32_t interval;
1813
 
1814
    if (LA_use_advance_lead) {
1815
      if (step_events_completed > decelerate_after && LA_current_adv_steps > LA_final_adv_steps) {
1816
        LA_steps--;
1817
        LA_current_adv_steps--;
1818
        interval = LA_isr_rate;
1819
      }
1820
      else if (step_events_completed < decelerate_after && LA_current_adv_steps < LA_max_adv_steps) {
1821
             //step_events_completed <= (uint32_t)accelerate_until) {
1822
        LA_steps++;
1823
        LA_current_adv_steps++;
1824
        interval = LA_isr_rate;
1825
      }
1826
      else
1827
        interval = LA_isr_rate = LA_ADV_NEVER;
1828
    }
1829
    else
1830
      interval = LA_ADV_NEVER;
1831
 
1832
      #if ENABLED(MIXING_EXTRUDER)
1833
        if (LA_steps >= 0)
1834
          MIXING_STEPPERS_LOOP(j) NORM_E_DIR(j);
1835
        else
1836
          MIXING_STEPPERS_LOOP(j) REV_E_DIR(j);
1837
      #else
1838
        if (LA_steps >= 0)
1839
          NORM_E_DIR(active_extruder);
1840
        else
1841
          REV_E_DIR(active_extruder);
1842
      #endif
1843
 
1844
    // Get the timer count and estimate the end of the pulse
1845
    hal_timer_t pulse_end = HAL_timer_get_count(PULSE_TIMER_NUM) + hal_timer_t(MIN_PULSE_TICKS);
1846
 
1847
    const hal_timer_t added_step_ticks = hal_timer_t(ADDED_STEP_TICKS);
1848
 
1849
    // Step E stepper if we have steps
1850
    while (LA_steps) {
1851
 
1852
      // Set the STEP pulse ON
1853
      #if ENABLED(MIXING_EXTRUDER)
1854
        MIXING_STEPPERS_LOOP(j) {
1855
          // Step mixing steppers (proportionally)
1856
          delta_error_m[j] += advance_dividend_m[j];
1857
          // Step when the counter goes over zero
1858
          if (delta_error_m[j] >= 0) E_STEP_WRITE(j, !INVERT_E_STEP_PIN);
1859
        }
1860
      #else
1861
        E_STEP_WRITE(active_extruder, !INVERT_E_STEP_PIN);
1862
      #endif
1863
 
1864
      // Enforce a minimum duration for STEP pulse ON
1865
      #if MINIMUM_STEPPER_PULSE
1866
        // Just wait for the requested pulse duration
1867
        while (HAL_timer_get_count(PULSE_TIMER_NUM) < pulse_end) { /* nada */ }
1868
      #endif
1869
 
1870
      // Add the delay needed to ensure the maximum driver rate is enforced
1871
      if (signed(added_step_ticks) > 0) pulse_end += hal_timer_t(added_step_ticks);
1872
 
1873
      LA_steps < 0 ? ++LA_steps : --LA_steps;
1874
 
1875
      // Set the STEP pulse OFF
1876
      #if ENABLED(MIXING_EXTRUDER)
1877
        MIXING_STEPPERS_LOOP(j) {
1878
          if (delta_error_m[j] >= 0) {
1879
            delta_error_m[j] -= advance_divisor_m;
1880
            E_STEP_WRITE(j, INVERT_E_STEP_PIN);
1881
          }
1882
        }
1883
      #else
1884
        E_STEP_WRITE(active_extruder, INVERT_E_STEP_PIN);
1885
      #endif
1886
 
1887
      // For minimum pulse time wait before looping
1888
      // Just wait for the requested pulse duration
1889
      if (LA_steps) {
1890
        while (HAL_timer_get_count(PULSE_TIMER_NUM) < pulse_end) { /* nada */ }
1891
        #if MINIMUM_STEPPER_PULSE
1892
          // Add to the value, the time that the pulse must be active (to be used on the next loop)
1893
          pulse_end += hal_timer_t(MIN_PULSE_TICKS);
1894
        #endif
1895
      }
1896
    } // LA_steps
1897
 
1898
    return interval;
1899
  }
1900
#endif // LIN_ADVANCE
1901
 
1902
// Check if the given block is busy or not - Must not be called from ISR contexts
1903
// The current_block could change in the middle of the read by an Stepper ISR, so
1904
// we must explicitly prevent that!
1905
bool Stepper::is_block_busy(const block_t* const block) {
1906
  #define sw_barrier() asm volatile("": : :"memory");
1907
 
1908
  // Keep reading until 2 consecutive reads return the same value,
1909
  // meaning there was no update in-between caused by an interrupt.
1910
  // This works because stepper ISRs happen at a slower rate than
1911
  // successive reads of a variable, so 2 consecutive reads with
1912
  // the same value means no interrupt updated it.
1913
  block_t* vold, *vnew = current_block;
1914
  sw_barrier();
1915
  do {
1916
    vold = vnew;
1917
    vnew = current_block;
1918
    sw_barrier();
1919
  } while (vold != vnew);
1920
 
1921
  // Return if the block is busy or not
1922
  return block == vnew;
1923
}
1924
 
1925
void Stepper::init() {
1926
 
1927
  // Init Digipot Motor Current
1928
  #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
1929
    digipot_init();
1930
  #endif
1931
 
1932
  // Init Microstepping Pins
1933
  #if HAS_MICROSTEPS
1934
    microstep_init();
1935
  #endif
1936
 
1937
  // Init Dir Pins
1938
  #if HAS_X_DIR
1939
    X_DIR_INIT;
1940
  #endif
1941
  #if HAS_X2_DIR
1942
    X2_DIR_INIT;
1943
  #endif
1944
  #if HAS_Y_DIR
1945
    Y_DIR_INIT;
1946
    #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_DIR
1947
      Y2_DIR_INIT;
1948
    #endif
1949
  #endif
1950
  #if HAS_Z_DIR
1951
    Z_DIR_INIT;
1952
    #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_DIR
1953
      Z2_DIR_INIT;
1954
    #endif
1955
  #endif
1956
  #if HAS_E0_DIR
1957
    E0_DIR_INIT;
1958
  #endif
1959
  #if HAS_E1_DIR
1960
    E1_DIR_INIT;
1961
  #endif
1962
  #if HAS_E2_DIR
1963
    E2_DIR_INIT;
1964
  #endif
1965
  #if HAS_E3_DIR
1966
    E3_DIR_INIT;
1967
  #endif
1968
  #if HAS_E4_DIR
1969
    E4_DIR_INIT;
1970
  #endif
1971
 
1972
  // Init Enable Pins - steppers default to disabled.
1973
  #if HAS_X_ENABLE
1974
    X_ENABLE_INIT;
1975
    if (!X_ENABLE_ON) X_ENABLE_WRITE(HIGH);
1976
    #if (ENABLED(DUAL_X_CARRIAGE) || ENABLED(X_DUAL_STEPPER_DRIVERS)) && HAS_X2_ENABLE
1977
      X2_ENABLE_INIT;
1978
      if (!X_ENABLE_ON) X2_ENABLE_WRITE(HIGH);
1979
    #endif
1980
  #endif
1981
  #if HAS_Y_ENABLE
1982
    Y_ENABLE_INIT;
1983
    if (!Y_ENABLE_ON) Y_ENABLE_WRITE(HIGH);
1984
    #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_ENABLE
1985
      Y2_ENABLE_INIT;
1986
      if (!Y_ENABLE_ON) Y2_ENABLE_WRITE(HIGH);
1987
    #endif
1988
  #endif
1989
  #if HAS_Z_ENABLE
1990
    Z_ENABLE_INIT;
1991
    if (!Z_ENABLE_ON) Z_ENABLE_WRITE(HIGH);
1992
    #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_ENABLE
1993
      Z2_ENABLE_INIT;
1994
      if (!Z_ENABLE_ON) Z2_ENABLE_WRITE(HIGH);
1995
    #endif
1996
  #endif
1997
  #if HAS_E0_ENABLE
1998
    E0_ENABLE_INIT;
1999
    if (!E_ENABLE_ON) E0_ENABLE_WRITE(HIGH);
2000
  #endif
2001
  #if HAS_E1_ENABLE
2002
    E1_ENABLE_INIT;
2003
    if (!E_ENABLE_ON) E1_ENABLE_WRITE(HIGH);
2004
  #endif
2005
  #if HAS_E2_ENABLE
2006
    E2_ENABLE_INIT;
2007
    if (!E_ENABLE_ON) E2_ENABLE_WRITE(HIGH);
2008
  #endif
2009
  #if HAS_E3_ENABLE
2010
    E3_ENABLE_INIT;
2011
    if (!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH);
2012
  #endif
2013
  #if HAS_E4_ENABLE
2014
    E4_ENABLE_INIT;
2015
    if (!E_ENABLE_ON) E4_ENABLE_WRITE(HIGH);
2016
  #endif
2017
 
2018
  #define _STEP_INIT(AXIS) AXIS ##_STEP_INIT
2019
  #define _WRITE_STEP(AXIS, HIGHLOW) AXIS ##_STEP_WRITE(HIGHLOW)
2020
  #define _DISABLE(AXIS) disable_## AXIS()
2021
 
2022
  #define AXIS_INIT(AXIS, PIN) \
2023
    _STEP_INIT(AXIS); \
2024
    _WRITE_STEP(AXIS, _INVERT_STEP_PIN(PIN)); \
2025
    _DISABLE(AXIS)
2026
 
2027
  #define E_AXIS_INIT(NUM) AXIS_INIT(E## NUM, E)
2028
 
2029
  // Init Step Pins
2030
  #if HAS_X_STEP
2031
    #if ENABLED(X_DUAL_STEPPER_DRIVERS) || ENABLED(DUAL_X_CARRIAGE)
2032
      X2_STEP_INIT;
2033
      X2_STEP_WRITE(INVERT_X_STEP_PIN);
2034
    #endif
2035
    AXIS_INIT(X, X);
2036
  #endif
2037
 
2038
  #if HAS_Y_STEP
2039
    #if ENABLED(Y_DUAL_STEPPER_DRIVERS)
2040
      Y2_STEP_INIT;
2041
      Y2_STEP_WRITE(INVERT_Y_STEP_PIN);
2042
    #endif
2043
    AXIS_INIT(Y, Y);
2044
  #endif
2045
 
2046
  #if HAS_Z_STEP
2047
    #if ENABLED(Z_DUAL_STEPPER_DRIVERS)
2048
      Z2_STEP_INIT;
2049
      Z2_STEP_WRITE(INVERT_Z_STEP_PIN);
2050
    #endif
2051
    AXIS_INIT(Z, Z);
2052
  #endif
2053
 
2054
  #if E_STEPPERS > 0 && HAS_E0_STEP
2055
    E_AXIS_INIT(0);
2056
  #endif
2057
  #if (E_STEPPERS > 1 || (E_STEPPERS == 1 && ENABLED(HANGPRINTER))) && HAS_E1_STEP
2058
    E_AXIS_INIT(1);
2059
  #endif
2060
  #if (E_STEPPERS > 2 || (E_STEPPERS == 2 && ENABLED(HANGPRINTER))) && HAS_E2_STEP
2061
    E_AXIS_INIT(2);
2062
  #endif
2063
  #if (E_STEPPERS > 3 || (E_STEPPERS == 3 && ENABLED(HANGPRINTER))) && HAS_E3_STEP
2064
    E_AXIS_INIT(3);
2065
  #endif
2066
  #if (E_STEPPERS > 4 || (E_STEPPERS == 4 && ENABLED(HANGPRINTER))) && HAS_E4_STEP
2067
    E_AXIS_INIT(4);
2068
  #endif
2069
 
2070
  // Init Stepper ISR to 122 Hz for quick starting
2071
  HAL_timer_start(STEP_TIMER_NUM, 122); // OCR1A = 0x4000
2072
 
2073
  ENABLE_STEPPER_DRIVER_INTERRUPT();
2074
 
2075
  endstops.enable(true); // Start with endstops active. After homing they can be disabled
2076
  sei();
2077
 
2078
  set_directions(); // Init directions to last_direction_bits = 0
2079
}
2080
 
2081
/**
2082
 * Set the stepper positions directly in steps
2083
 *
2084
 * The input is based on the typical per-axis XYZ steps.
2085
 * For CORE machines XYZ needs to be translated to ABC.
2086
 *
2087
 * This allows get_axis_position_mm to correctly
2088
 * derive the current XYZ position later on.
2089
 */
2090
void Stepper::_set_position(const int32_t &a, const int32_t &b, const int32_t &c,
2091
    #if ENABLED(HANGPRINTER)
2092
      const int32_t &d,
2093
    #endif
2094
  const int32_t &e
2095
) {
2096
  #if CORE_IS_XY
2097
    // corexy positioning
2098
    // these equations follow the form of the dA and dB equations on http://www.corexy.com/theory.html
2099
    count_position[A_AXIS] = a + b;
2100
    count_position[B_AXIS] = CORESIGN(a - b);
2101
    count_position[Z_AXIS] = c;
2102
  #elif CORE_IS_XZ
2103
    // corexz planning
2104
    count_position[A_AXIS] = a + c;
2105
    count_position[Y_AXIS] = b;
2106
    count_position[C_AXIS] = CORESIGN(a - c);
2107
  #elif CORE_IS_YZ
2108
    // coreyz planning
2109
    count_position[X_AXIS] = a;
2110
    count_position[B_AXIS] = b + c;
2111
    count_position[C_AXIS] = CORESIGN(b - c);
2112
  #else
2113
    // default non-h-bot planning
2114
    count_position[X_AXIS] = a;
2115
    count_position[Y_AXIS] = b;
2116
    count_position[Z_AXIS] = c;
2117
    #if ENABLED(HANGPRINTER)
2118
      count_position[D_AXIS] = d;
2119
    #endif
2120
  #endif
2121
  count_position[E_AXIS] = e;
2122
}
2123
 
2124
/**
2125
 * Get a stepper's position in steps.
2126
 */
2127
int32_t Stepper::position(const AxisEnum axis) {
2128
  const bool was_enabled = STEPPER_ISR_ENABLED();
2129
  if (was_enabled) DISABLE_STEPPER_DRIVER_INTERRUPT();
2130
 
2131
  const int32_t v = count_position[axis];
2132
 
2133
  if (was_enabled) ENABLE_STEPPER_DRIVER_INTERRUPT();
2134
  return v;
2135
}
2136
 
2137
// Signal endstops were triggered - This function can be called from
2138
// an ISR context  (Temperature, Stepper or limits ISR), so we must
2139
// be very careful here. If the interrupt being preempted was the
2140
// Stepper ISR (this CAN happen with the endstop limits ISR) then
2141
// when the stepper ISR resumes, we must be very sure that the movement
2142
// is properly cancelled
2143
void Stepper::endstop_triggered(const AxisEnum axis) {
2144
 
2145
  const bool was_enabled = STEPPER_ISR_ENABLED();
2146
  if (was_enabled) DISABLE_STEPPER_DRIVER_INTERRUPT();
2147
 
2148
  #if IS_CORE
2149
 
2150
    endstops_trigsteps[axis] = 0.5f * (
2151
      axis == CORE_AXIS_2 ? CORESIGN(count_position[CORE_AXIS_1] - count_position[CORE_AXIS_2])
2152
                          : count_position[CORE_AXIS_1] + count_position[CORE_AXIS_2]
2153
    );
2154
 
2155
  #else // !COREXY && !COREXZ && !COREYZ
2156
 
2157
    endstops_trigsteps[axis] = count_position[axis];
2158
 
2159
  #endif // !COREXY && !COREXZ && !COREYZ
2160
 
2161
  // Discard the rest of the move if there is a current block
2162
  quick_stop();
2163
 
2164
  if (was_enabled) ENABLE_STEPPER_DRIVER_INTERRUPT();
2165
}
2166
 
2167
int32_t Stepper::triggered_position(const AxisEnum axis) {
2168
  const bool was_enabled = STEPPER_ISR_ENABLED();
2169
  if (was_enabled) DISABLE_STEPPER_DRIVER_INTERRUPT();
2170
 
2171
  const int32_t v = endstops_trigsteps[axis];
2172
 
2173
  if (was_enabled) ENABLE_STEPPER_DRIVER_INTERRUPT();
2174
 
2175
  return v;
2176
}
2177
 
2178
void Stepper::report_positions() {
2179
 
2180
  // Protect the access to the position.
2181
  const bool was_enabled = STEPPER_ISR_ENABLED();
2182
  if (was_enabled) DISABLE_STEPPER_DRIVER_INTERRUPT();
2183
 
2184
  const int32_t xpos = count_position[X_AXIS],
2185
                ypos = count_position[Y_AXIS],
2186
                #if ENABLED(HANGPRINTER)
2187
                  dpos = count_position[D_AXIS],
2188
                #endif
2189
                zpos = count_position[Z_AXIS];
2190
 
2191
  if (was_enabled) ENABLE_STEPPER_DRIVER_INTERRUPT();
2192
 
2193
  #if CORE_IS_XY || CORE_IS_XZ || IS_DELTA || IS_SCARA || ENABLED(HANGPRINTER)
2194
    SERIAL_PROTOCOLPGM(MSG_COUNT_A);
2195
  #else
2196
    SERIAL_PROTOCOLPGM(MSG_COUNT_X);
2197
  #endif
2198
  SERIAL_PROTOCOL(xpos);
2199
 
2200
  #if CORE_IS_XY || CORE_IS_YZ || IS_DELTA || IS_SCARA || ENABLED(HANGPRINTER)
2201
    SERIAL_PROTOCOLPGM(" B:");
2202
  #else
2203
    SERIAL_PROTOCOLPGM(" Y:");
2204
  #endif
2205
  SERIAL_PROTOCOL(ypos);
2206
 
2207
  #if CORE_IS_XZ || CORE_IS_YZ || IS_DELTA || ENABLED(HANGPRINTER)
2208
    SERIAL_PROTOCOLPGM(" C:");
2209
  #else
2210
    SERIAL_PROTOCOLPGM(" Z:");
2211
  #endif
2212
  SERIAL_PROTOCOL(zpos);
2213
 
2214
  #if ENABLED(HANGPRINTER)
2215
    SERIAL_PROTOCOLPAIR(" D:", dpos);
2216
  #endif
2217
 
2218
  SERIAL_EOL();
2219
}
2220
 
2221
#if ENABLED(BABYSTEPPING)
2222
 
2223
  #if MINIMUM_STEPPER_PULSE
2224
    #define STEP_PULSE_CYCLES ((MINIMUM_STEPPER_PULSE) * CYCLES_PER_MICROSECOND)
2225
  #else
2226
    #define STEP_PULSE_CYCLES 0
2227
  #endif
2228
 
2229
  #if ENABLED(DELTA)
2230
    #define CYCLES_EATEN_BABYSTEP (2 * 15)
2231
  #else
2232
    #define CYCLES_EATEN_BABYSTEP 0
2233
  #endif
2234
  #define EXTRA_CYCLES_BABYSTEP (STEP_PULSE_CYCLES - (CYCLES_EATEN_BABYSTEP))
2235
 
2236
  #define _ENABLE(AXIS) enable_## AXIS()
2237
  #define _READ_DIR(AXIS) AXIS ##_DIR_READ
2238
  #define _INVERT_DIR(AXIS) INVERT_## AXIS ##_DIR
2239
  #define _APPLY_DIR(AXIS, INVERT) AXIS ##_APPLY_DIR(INVERT, true)
2240
 
2241
  #if EXTRA_CYCLES_BABYSTEP > 20
2242
    #define _SAVE_START const hal_timer_t pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM)
2243
    #define _PULSE_WAIT while (EXTRA_CYCLES_BABYSTEP > (uint32_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
2244
  #else
2245
    #define _SAVE_START NOOP
2246
    #if EXTRA_CYCLES_BABYSTEP > 0
2247
      #define _PULSE_WAIT DELAY_NS(EXTRA_CYCLES_BABYSTEP * NANOSECONDS_PER_CYCLE)
2248
    #elif STEP_PULSE_CYCLES > 0
2249
      #define _PULSE_WAIT NOOP
2250
    #elif ENABLED(DELTA)
2251
      #define _PULSE_WAIT DELAY_US(2);
2252
    #else
2253
      #define _PULSE_WAIT DELAY_US(4);
2254
    #endif
2255
  #endif
2256
 
2257
  #define BABYSTEP_AXIS(AXIS, INVERT, DIR) {            \
2258
      const uint8_t old_dir = _READ_DIR(AXIS);          \
2259
      _ENABLE(AXIS);                                    \
2260
      _APPLY_DIR(AXIS, _INVERT_DIR(AXIS)^DIR^INVERT);   \
2261
      DELAY_NS(MINIMUM_STEPPER_DIR_DELAY);              \
2262
      _SAVE_START;                                      \
2263
      _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), true); \
2264
      _PULSE_WAIT;                                      \
2265
      _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), true);  \
2266
      _APPLY_DIR(AXIS, old_dir);                        \
2267
    }
2268
 
2269
  // MUST ONLY BE CALLED BY AN ISR,
2270
  // No other ISR should ever interrupt this!
2271
  void Stepper::babystep(const AxisEnum axis, const bool direction) {
2272
    cli();
2273
 
2274
    switch (axis) {
2275
 
2276
      #if ENABLED(BABYSTEP_XY)
2277
 
2278
        case X_AXIS:
2279
          #if CORE_IS_XY
2280
            BABYSTEP_AXIS(X, false, direction);
2281
            BABYSTEP_AXIS(Y, false, direction);
2282
          #elif CORE_IS_XZ
2283
            BABYSTEP_AXIS(X, false, direction);
2284
            BABYSTEP_AXIS(Z, false, direction);
2285
          #else
2286
            BABYSTEP_AXIS(X, false, direction);
2287
          #endif
2288
          break;
2289
 
2290
        case Y_AXIS:
2291
          #if CORE_IS_XY
2292
            BABYSTEP_AXIS(X, false, direction);
2293
            BABYSTEP_AXIS(Y, false, direction^(CORESIGN(1)<0));
2294
          #elif CORE_IS_YZ
2295
            BABYSTEP_AXIS(Y, false, direction);
2296
            BABYSTEP_AXIS(Z, false, direction^(CORESIGN(1)<0));
2297
          #else
2298
            BABYSTEP_AXIS(Y, false, direction);
2299
          #endif
2300
          break;
2301
 
2302
      #endif
2303
 
2304
      case Z_AXIS: {
2305
 
2306
        #if CORE_IS_XZ
2307
          BABYSTEP_AXIS(X, BABYSTEP_INVERT_Z, direction);
2308
          BABYSTEP_AXIS(Z, BABYSTEP_INVERT_Z, direction^(CORESIGN(1)<0));
2309
 
2310
        #elif CORE_IS_YZ
2311
          BABYSTEP_AXIS(Y, BABYSTEP_INVERT_Z, direction);
2312
          BABYSTEP_AXIS(Z, BABYSTEP_INVERT_Z, direction^(CORESIGN(1)<0));
2313
 
2314
        #elif DISABLED(DELTA)
2315
          BABYSTEP_AXIS(Z, BABYSTEP_INVERT_Z, direction);
2316
 
2317
        #else // DELTA
2318
 
2319
          const bool z_direction = direction ^ BABYSTEP_INVERT_Z;
2320
 
2321
          enable_X();
2322
          enable_Y();
2323
          enable_Z();
2324
 
2325
          const uint8_t old_x_dir_pin = X_DIR_READ,
2326
                        old_y_dir_pin = Y_DIR_READ,
2327
                        old_z_dir_pin = Z_DIR_READ;
2328
 
2329
          X_DIR_WRITE(INVERT_X_DIR ^ z_direction);
2330
          Y_DIR_WRITE(INVERT_Y_DIR ^ z_direction);
2331
          Z_DIR_WRITE(INVERT_Z_DIR ^ z_direction);
2332
 
2333
          #if MINIMUM_STEPPER_DIR_DELAY > 0
2334
            DELAY_NS(MINIMUM_STEPPER_DIR_DELAY);
2335
          #endif
2336
 
2337
          _SAVE_START;
2338
 
2339
          X_STEP_WRITE(!INVERT_X_STEP_PIN);
2340
          Y_STEP_WRITE(!INVERT_Y_STEP_PIN);
2341
          Z_STEP_WRITE(!INVERT_Z_STEP_PIN);
2342
 
2343
          _PULSE_WAIT;
2344
 
2345
          X_STEP_WRITE(INVERT_X_STEP_PIN);
2346
          Y_STEP_WRITE(INVERT_Y_STEP_PIN);
2347
          Z_STEP_WRITE(INVERT_Z_STEP_PIN);
2348
 
2349
          // Restore direction bits
2350
          X_DIR_WRITE(old_x_dir_pin);
2351
          Y_DIR_WRITE(old_y_dir_pin);
2352
          Z_DIR_WRITE(old_z_dir_pin);
2353
 
2354
        #endif
2355
 
2356
      } break;
2357
 
2358
      default: break;
2359
    }
2360
    sei();
2361
  }
2362
 
2363
#endif // BABYSTEPPING
2364
 
2365
/**
2366
 * Software-controlled Stepper Motor Current
2367
 */
2368
 
2369
#if HAS_DIGIPOTSS
2370
 
2371
  // From Arduino DigitalPotControl example
2372
  void Stepper::digitalPotWrite(const int16_t address, const int16_t value) {
2373
    WRITE(DIGIPOTSS_PIN, LOW);  // Take the SS pin low to select the chip
2374
    SPI.transfer(address);      // Send the address and value via SPI
2375
    SPI.transfer(value);
2376
    WRITE(DIGIPOTSS_PIN, HIGH); // Take the SS pin high to de-select the chip
2377
    //delay(10);
2378
  }
2379
 
2380
#endif // HAS_DIGIPOTSS
2381
 
2382
#if HAS_MOTOR_CURRENT_PWM
2383
 
2384
  void Stepper::refresh_motor_power() {
2385
    for (uint8_t i = 0; i < COUNT(motor_current_setting); ++i) {
2386
      switch (i) {
2387
        #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
2388
          case 0:
2389
        #endif
2390
        #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
2391
          case 1:
2392
        #endif
2393
        #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
2394
          case 2:
2395
        #endif
2396
            digipot_current(i, motor_current_setting[i]);
2397
        default: break;
2398
      }
2399
    }
2400
  }
2401
 
2402
#endif // HAS_MOTOR_CURRENT_PWM
2403
 
2404
#if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
2405
 
2406
  void Stepper::digipot_current(const uint8_t driver, const int current) {
2407
 
2408
    #if HAS_DIGIPOTSS
2409
 
2410
      const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
2411
      digitalPotWrite(digipot_ch[driver], current);
2412
 
2413
    #elif HAS_MOTOR_CURRENT_PWM
2414
 
2415
      if (WITHIN(driver, 0, 2))
2416
        motor_current_setting[driver] = current; // update motor_current_setting
2417
 
2418
      #define _WRITE_CURRENT_PWM(P) analogWrite(MOTOR_CURRENT_PWM_## P ##_PIN, 255L * current / (MOTOR_CURRENT_PWM_RANGE))
2419
      switch (driver) {
2420
        #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
2421
          case 0: _WRITE_CURRENT_PWM(XY); break;
2422
        #endif
2423
        #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
2424
          case 1: _WRITE_CURRENT_PWM(Z); break;
2425
        #endif
2426
        #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
2427
          case 2: _WRITE_CURRENT_PWM(E); break;
2428
        #endif
2429
      }
2430
    #endif
2431
  }
2432
 
2433
  void Stepper::digipot_init() {
2434
 
2435
    #if HAS_DIGIPOTSS
2436
 
2437
      static const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
2438
 
2439
      SPI.begin();
2440
      SET_OUTPUT(DIGIPOTSS_PIN);
2441
 
2442
      for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++) {
2443
        //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
2444
        digipot_current(i, digipot_motor_current[i]);
2445
      }
2446
 
2447
    #elif HAS_MOTOR_CURRENT_PWM
2448
 
2449
      #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
2450
        SET_OUTPUT(MOTOR_CURRENT_PWM_XY_PIN);
2451
      #endif
2452
      #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
2453
        SET_OUTPUT(MOTOR_CURRENT_PWM_Z_PIN);
2454
      #endif
2455
      #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
2456
        SET_OUTPUT(MOTOR_CURRENT_PWM_E_PIN);
2457
      #endif
2458
 
2459
      refresh_motor_power();
2460
 
2461
      // Set Timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
2462
      SET_CS5(PRESCALER_1);
2463
 
2464
    #endif
2465
  }
2466
 
2467
#endif
2468
 
2469
#if HAS_MICROSTEPS
2470
 
2471
  /**
2472
   * Software-controlled Microstepping
2473
   */
2474
 
2475
  void Stepper::microstep_init() {
2476
    SET_OUTPUT(X_MS1_PIN);
2477
    SET_OUTPUT(X_MS2_PIN);
2478
    #if HAS_Y_MICROSTEPS
2479
      SET_OUTPUT(Y_MS1_PIN);
2480
      SET_OUTPUT(Y_MS2_PIN);
2481
    #endif
2482
    #if HAS_Z_MICROSTEPS
2483
      SET_OUTPUT(Z_MS1_PIN);
2484
      SET_OUTPUT(Z_MS2_PIN);
2485
    #endif
2486
    #if HAS_E0_MICROSTEPS
2487
      SET_OUTPUT(E0_MS1_PIN);
2488
      SET_OUTPUT(E0_MS2_PIN);
2489
    #endif
2490
    #if HAS_E1_MICROSTEPS
2491
      SET_OUTPUT(E1_MS1_PIN);
2492
      SET_OUTPUT(E1_MS2_PIN);
2493
    #endif
2494
    #if HAS_E2_MICROSTEPS
2495
      SET_OUTPUT(E2_MS1_PIN);
2496
      SET_OUTPUT(E2_MS2_PIN);
2497
    #endif
2498
    #if HAS_E3_MICROSTEPS
2499
      SET_OUTPUT(E3_MS1_PIN);
2500
      SET_OUTPUT(E3_MS2_PIN);
2501
    #endif
2502
    #if HAS_E4_MICROSTEPS
2503
      SET_OUTPUT(E4_MS1_PIN);
2504
      SET_OUTPUT(E4_MS2_PIN);
2505
    #endif
2506
    static const uint8_t microstep_modes[] = MICROSTEP_MODES;
2507
    for (uint16_t i = 0; i < COUNT(microstep_modes); i++)
2508
      microstep_mode(i, microstep_modes[i]);
2509
  }
2510
 
2511
  void Stepper::microstep_ms(const uint8_t driver, const int8_t ms1, const int8_t ms2) {
2512
    if (ms1 >= 0) switch (driver) {
2513
      case 0: WRITE(X_MS1_PIN, ms1); break;
2514
      #if HAS_Y_MICROSTEPS
2515
        case 1: WRITE(Y_MS1_PIN, ms1); break;
2516
      #endif
2517
      #if HAS_Z_MICROSTEPS
2518
        case 2: WRITE(Z_MS1_PIN, ms1); break;
2519
      #endif
2520
      #if HAS_E0_MICROSTEPS
2521
        case 3: WRITE(E0_MS1_PIN, ms1); break;
2522
      #endif
2523
      #if HAS_E1_MICROSTEPS
2524
        case 4: WRITE(E1_MS1_PIN, ms1); break;
2525
      #endif
2526
      #if HAS_E2_MICROSTEPS
2527
        case 5: WRITE(E2_MS1_PIN, ms1); break;
2528
      #endif
2529
      #if HAS_E3_MICROSTEPS
2530
        case 6: WRITE(E3_MS1_PIN, ms1); break;
2531
      #endif
2532
      #if HAS_E4_MICROSTEPS
2533
        case 7: WRITE(E4_MS1_PIN, ms1); break;
2534
      #endif
2535
    }
2536
    if (ms2 >= 0) switch (driver) {
2537
      case 0: WRITE(X_MS2_PIN, ms2); break;
2538
      #if HAS_Y_MICROSTEPS
2539
        case 1: WRITE(Y_MS2_PIN, ms2); break;
2540
      #endif
2541
      #if HAS_Z_MICROSTEPS
2542
        case 2: WRITE(Z_MS2_PIN, ms2); break;
2543
      #endif
2544
      #if HAS_E0_MICROSTEPS
2545
        case 3: WRITE(E0_MS2_PIN, ms2); break;
2546
      #endif
2547
      #if HAS_E1_MICROSTEPS
2548
        case 4: WRITE(E1_MS2_PIN, ms2); break;
2549
      #endif
2550
      #if HAS_E2_MICROSTEPS
2551
        case 5: WRITE(E2_MS2_PIN, ms2); break;
2552
      #endif
2553
      #if HAS_E3_MICROSTEPS
2554
        case 6: WRITE(E3_MS2_PIN, ms2); break;
2555
      #endif
2556
      #if HAS_E4_MICROSTEPS
2557
        case 7: WRITE(E4_MS2_PIN, ms2); break;
2558
      #endif
2559
    }
2560
  }
2561
 
2562
  void Stepper::microstep_mode(const uint8_t driver, const uint8_t stepping_mode) {
2563
    switch (stepping_mode) {
2564
      case 1: microstep_ms(driver, MICROSTEP1); break;
2565
      #if ENABLED(HEROIC_STEPPER_DRIVERS)
2566
        case 128: microstep_ms(driver, MICROSTEP128); break;
2567
      #else
2568
        case 2: microstep_ms(driver, MICROSTEP2); break;
2569
        case 4: microstep_ms(driver, MICROSTEP4); break;
2570
      #endif
2571
      case 8: microstep_ms(driver, MICROSTEP8); break;
2572
      case 16: microstep_ms(driver, MICROSTEP16); break;
2573
      default: SERIAL_ERROR_START(); SERIAL_ERRORLNPGM("Microsteps unavailable"); break;
2574
    }
2575
  }
2576
 
2577
  void Stepper::microstep_readings() {
2578
    SERIAL_PROTOCOLLNPGM("MS1,MS2 Pins");
2579
    SERIAL_PROTOCOLPGM("X: ");
2580
    SERIAL_PROTOCOL(READ(X_MS1_PIN));
2581
    SERIAL_PROTOCOLLN(READ(X_MS2_PIN));
2582
    #if HAS_Y_MICROSTEPS
2583
      SERIAL_PROTOCOLPGM("Y: ");
2584
      SERIAL_PROTOCOL(READ(Y_MS1_PIN));
2585
      SERIAL_PROTOCOLLN(READ(Y_MS2_PIN));
2586
    #endif
2587
    #if HAS_Z_MICROSTEPS
2588
      SERIAL_PROTOCOLPGM("Z: ");
2589
      SERIAL_PROTOCOL(READ(Z_MS1_PIN));
2590
      SERIAL_PROTOCOLLN(READ(Z_MS2_PIN));
2591
    #endif
2592
    #if HAS_E0_MICROSTEPS
2593
      SERIAL_PROTOCOLPGM("E0: ");
2594
      SERIAL_PROTOCOL(READ(E0_MS1_PIN));
2595
      SERIAL_PROTOCOLLN(READ(E0_MS2_PIN));
2596
    #endif
2597
    #if HAS_E1_MICROSTEPS
2598
      SERIAL_PROTOCOLPGM("E1: ");
2599
      SERIAL_PROTOCOL(READ(E1_MS1_PIN));
2600
      SERIAL_PROTOCOLLN(READ(E1_MS2_PIN));
2601
    #endif
2602
    #if HAS_E2_MICROSTEPS
2603
      SERIAL_PROTOCOLPGM("E2: ");
2604
      SERIAL_PROTOCOL(READ(E2_MS1_PIN));
2605
      SERIAL_PROTOCOLLN(READ(E2_MS2_PIN));
2606
    #endif
2607
    #if HAS_E3_MICROSTEPS
2608
      SERIAL_PROTOCOLPGM("E3: ");
2609
      SERIAL_PROTOCOL(READ(E3_MS1_PIN));
2610
      SERIAL_PROTOCOLLN(READ(E3_MS2_PIN));
2611
    #endif
2612
    #if HAS_E4_MICROSTEPS
2613
      SERIAL_PROTOCOLPGM("E4: ");
2614
      SERIAL_PROTOCOL(READ(E4_MS1_PIN));
2615
      SERIAL_PROTOCOLLN(READ(E4_MS2_PIN));
2616
    #endif
2617
  }
2618
 
2619
#endif // HAS_MICROSTEPS