Преглед изворни кода

[2.0.x] 6th-order jerk-controlled motion planning in real-time for AVR (#10373)

Eduardo José Tagle пре 6 година
родитељ
комит
57a899a412

+ 0
- 2
Marlin/src/inc/SanityCheck.h Прегледај датотеку

@@ -99,8 +99,6 @@
99 99
   #error "Z_ENDSTOP_SERVO_NR is now Z_PROBE_SERVO_NR. Please update your configuration."
100 100
 #elif defined(DEFAULT_XYJERK)
101 101
   #error "DEFAULT_XYJERK is deprecated. Use DEFAULT_XJERK and DEFAULT_YJERK instead."
102
-#elif ENABLED(BEZIER_JERK_CONTROL) && !defined(CPU_32_BIT)
103
-  #error "BEZIER_JERK_CONTROL is computationally intensive and requires a 32-bit board."
104 102
 #elif defined(XY_TRAVEL_SPEED)
105 103
   #error "XY_TRAVEL_SPEED is deprecated. Use XY_PROBE_SPEED instead."
106 104
 #elif defined(PROBE_SERVO_DEACTIVATION_DELAY)

+ 530
- 2
Marlin/src/module/planner.cpp Прегледај датотеку

@@ -56,6 +56,10 @@
56 56
  *
57 57
  * IntersectionDistance[s1_, s2_, a_, d_] := (2 a d - s1^2 + s2^2)/(4 a)
58 58
  *
59
+ * --
60
+ *
61
+ * The fast inverse function needed for Bézier interpolation for AVR
62
+ * was designed, written and tested by Eduardo José Tagle on April/2018
59 63
  */
60 64
 
61 65
 #include "planner.h"
@@ -215,6 +219,523 @@ void Planner::init() {
215 219
   #endif
216 220
 }
217 221
 
222
+#if ENABLED(BEZIER_JERK_CONTROL)
223
+
224
+  #ifdef __AVR__
225
+    // This routine, for AVR, returns 0x1000000 / d, but trying to get the inverse as
226
+    //  fast as possible. A fast converging iterative Newton-Raphson method is able to
227
+    //  reach full precision in just 1 iteration, and takes 211 cycles (worst case, mean
228
+    //  case is less, up to 30 cycles for small divisors), instead of the 500 cycles a
229
+    //  normal division would take.
230
+    //
231
+    // Inspired by the following page,
232
+    //  https://stackoverflow.com/questions/27801397/newton-raphson-division-with-big-integers
233
+    //
234
+    // Suppose we want to calculate
235
+    //  floor(2 ^ k / B)    where B is a positive integer
236
+    // Then
237
+    //  B must be <= 2^k, otherwise, the quotient is 0.
238
+    //
239
+    // The Newton - Raphson iteration for x = B / 2 ^ k yields:
240
+    //  q[n + 1] = q[n] * (2 - q[n] * B / 2 ^ k)
241
+    //
242
+    // We can rearrange it as:
243
+    //  q[n + 1] = q[n] * (2 ^ (k + 1) - q[n] * B) >> k
244
+    //
245
+    //  Each iteration of this kind requires only integer multiplications
246
+    // and bit shifts.
247
+    //  Does it converge to floor(2 ^ k / B) ?:  Not necessarily, but, in
248
+    // the worst case, it eventually alternates between floor(2 ^ k / B)
249
+    // and ceiling(2 ^ k / B)).
250
+    //  So we can use some not-so-clever test to see if we are in this
251
+    // case, and extract floor(2 ^ k / B).
252
+    //  Lastly, a simple but important optimization for this approach is to
253
+    // truncate multiplications (i.e.calculate only the higher bits of the
254
+    // product) in the early iterations of the Newton - Raphson method.The
255
+    // reason to do so, is that the results of the early iterations are far
256
+    // from the quotient, and it doesn't matter to perform them inaccurately.
257
+    //  Finally, we should pick a good starting value for x. Knowing how many
258
+    // digits the divisor has, we can estimate it:
259
+    //
260
+    // 2^k / x = 2 ^ log2(2^k / x)
261
+    // 2^k / x = 2 ^(log2(2^k)-log2(x))
262
+    // 2^k / x = 2 ^(k*log2(2)-log2(x))
263
+    // 2^k / x = 2 ^ (k-log2(x))
264
+    // 2^k / x >= 2 ^ (k-floor(log2(x)))
265
+    // floor(log2(x)) simply is the index of the most significant bit set.
266
+    //
267
+    //  If we could improve this estimation even further, then the number of
268
+    // iterations can be dropped quite a bit, thus saving valuable execution time.
269
+    //  The paper "Software Integer Division" by Thomas L.Rodeheffer, Microsoft
270
+    // Research, Silicon Valley,August 26, 2008, that is available at
271
+    // https://www.microsoft.com/en-us/research/wp-content/uploads/2008/08/tr-2008-141.pdf
272
+    // suggests , for its integer division algorithm, that using a table to supply the
273
+    // first 8 bits of precision, and due to the quadratic convergence nature of the
274
+    // Newton-Raphon iteration, then just 2 iterations should be enough to get
275
+    // maximum precision of the division.
276
+    //  If we precompute values of inverses for small denominator values, then
277
+    // just one Newton-Raphson iteration is enough to reach full precision
278
+    //  We will use the top 9 bits of the denominator as index.
279
+    //
280
+    //  The AVR assembly function is implementing the following C code, included
281
+    // here as reference:
282
+    //
283
+    // uint32_t get_period_inverse(uint32_t d) {
284
+    //  static const uint8_t inv_tab[256] = {
285
+    //    255,253,252,250,248,246,244,242,240,238,236,234,233,231,229,227,
286
+    //    225,224,222,220,218,217,215,213,212,210,208,207,205,203,202,200,
287
+    //    199,197,195,194,192,191,189,188,186,185,183,182,180,179,178,176,
288
+    //    175,173,172,170,169,168,166,165,164,162,161,160,158,157,156,154,
289
+    //    153,152,151,149,148,147,146,144,143,142,141,139,138,137,136,135,
290
+    //    134,132,131,130,129,128,127,126,125,123,122,121,120,119,118,117,
291
+    //    116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,
292
+    //    100,99,98,97,96,95,94,93,92,91,90,89,88,88,87,86,
293
+    //    85,84,83,82,81,80,80,79,78,77,76,75,74,74,73,72,
294
+    //    71,70,70,69,68,67,66,66,65,64,63,62,62,61,60,59,
295
+    //    59,58,57,56,56,55,54,53,53,52,51,50,50,49,48,48,
296
+    //    47,46,46,45,44,43,43,42,41,41,40,39,39,38,37,37,
297
+    //    36,35,35,34,33,33,32,32,31,30,30,29,28,28,27,27,
298
+    //    26,25,25,24,24,23,22,22,21,21,20,19,19,18,18,17,
299
+    //    17,16,15,15,14,14,13,13,12,12,11,10,10,9,9,8,
300
+    //    8,7,7,6,6,5,5,4,4,3,3,2,2,1,0,0
301
+    //  };
302
+    //
303
+    //  // For small denominators, it is cheaper to directly store the result,
304
+    //  //  because those denominators would require 2 Newton-Raphson iterations
305
+    //  //  to converge to the required result precision. For bigger ones, just
306
+    //  //  ONE Newton-Raphson iteration is enough to get maximum precision!
307
+    //  static const uint32_t small_inv_tab[111] PROGMEM = {
308
+    //    16777216,16777216,8388608,5592405,4194304,3355443,2796202,2396745,2097152,1864135,1677721,1525201,1398101,1290555,1198372,1118481,
309
+    //    1048576,986895,932067,883011,838860,798915,762600,729444,699050,671088,645277,621378,599186,578524,559240,541200,
310
+    //    524288,508400,493447,479349,466033,453438,441505,430185,419430,409200,399457,390167,381300,372827,364722,356962,
311
+    //    349525,342392,335544,328965,322638,316551,310689,305040,299593,294337,289262,284359,279620,275036,270600,266305,
312
+    //    262144,258111,254200,250406,246723,243148,239674,236298,233016,229824,226719,223696,220752,217885,215092,212369,
313
+    //    209715,207126,204600,202135,199728,197379,195083,192841,190650,188508,186413,184365,182361,180400,178481,176602,
314
+    //    174762,172960,171196,169466,167772,166111,164482,162885,161319,159783,158275,156796,155344,153919,152520
315
+    //  };
316
+    //
317
+    //  // For small divisors, it is best to directly retrieve the results
318
+    //  if (d <= 110)
319
+    //    return pgm_read_dword(&small_inv_tab[d]);
320
+    //
321
+    //  // Compute initial estimation of 0x1000000/x -
322
+    //  // Get most significant bit set on divider
323
+    //  uint8_t idx = 0;
324
+    //  uint32_t nr = d;
325
+    //  if (!(nr & 0xff0000)) {
326
+    //    nr <<= 8;
327
+    //    idx += 8;
328
+    //    if (!(nr & 0xff0000)) {
329
+    //      nr <<= 8;
330
+    //      idx += 8;
331
+    //    }
332
+    //  }
333
+    //  if (!(nr & 0xf00000)) {
334
+    //    nr <<= 4;
335
+    //    idx += 4;
336
+    //  }
337
+    //  if (!(nr & 0xc00000)) {
338
+    //    nr <<= 2;
339
+    //    idx += 2;
340
+    //  }
341
+    //  if (!(nr & 0x800000)) {
342
+    //    nr <<= 1;
343
+    //    idx += 1;
344
+    //  }
345
+    //
346
+    //  // Isolate top 9 bits of the denominator, to be used as index into the initial estimation table
347
+    //  uint32_t tidx = nr >> 15;         // top 9 bits. bit8 is always set
348
+    //  uint32_t ie = inv_tab[tidx & 0xFF] + 256; // Get the table value. bit9 is always set
349
+    //  uint32_t x = idx <= 8 ? (ie >> (8 - idx)) : (ie << (idx - 8)); // Position the estimation at the proper place
350
+    //
351
+    //  // Now, refine estimation by newton-raphson. 1 iteration is enough
352
+    //  x = uint32_t((x * uint64_t((1 << 25) - x * d)) >> 24);
353
+    //
354
+    //  // Estimate remainder
355
+    //  uint32_t r = (1 << 24) - x * d;
356
+    //
357
+    //  // Check if we must adjust result
358
+    //  if (r >= d) x++;
359
+    //
360
+    //  // x holds the proper estimation
361
+    //  return uint32_t(x);
362
+    // }
363
+    //
364
+    static uint32_t get_period_inverse(uint32_t d) {
365
+
366
+       static const uint8_t inv_tab[256] PROGMEM = {
367
+        255,253,252,250,248,246,244,242,240,238,236,234,233,231,229,227,
368
+        225,224,222,220,218,217,215,213,212,210,208,207,205,203,202,200,
369
+        199,197,195,194,192,191,189,188,186,185,183,182,180,179,178,176,
370
+        175,173,172,170,169,168,166,165,164,162,161,160,158,157,156,154,
371
+        153,152,151,149,148,147,146,144,143,142,141,139,138,137,136,135,
372
+        134,132,131,130,129,128,127,126,125,123,122,121,120,119,118,117,
373
+        116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,
374
+        100,99,98,97,96,95,94,93,92,91,90,89,88,88,87,86,
375
+        85,84,83,82,81,80,80,79,78,77,76,75,74,74,73,72,
376
+        71,70,70,69,68,67,66,66,65,64,63,62,62,61,60,59,
377
+        59,58,57,56,56,55,54,53,53,52,51,50,50,49,48,48,
378
+        47,46,46,45,44,43,43,42,41,41,40,39,39,38,37,37,
379
+        36,35,35,34,33,33,32,32,31,30,30,29,28,28,27,27,
380
+        26,25,25,24,24,23,22,22,21,21,20,19,19,18,18,17,
381
+        17,16,15,15,14,14,13,13,12,12,11,10,10,9,9,8,
382
+        8,7,7,6,6,5,5,4,4,3,3,2,2,1,0,0
383
+      };
384
+
385
+      // For small denominators, it is cheaper to directly store the result.
386
+      //  For bigger ones, just ONE Newton-Raphson iteration is enough to get
387
+      //  maximum precision we need
388
+      static const uint32_t small_inv_tab[111] PROGMEM = {
389
+        16777216,16777216,8388608,5592405,4194304,3355443,2796202,2396745,2097152,1864135,1677721,1525201,1398101,1290555,1198372,1118481,
390
+        1048576,986895,932067,883011,838860,798915,762600,729444,699050,671088,645277,621378,599186,578524,559240,541200,
391
+        524288,508400,493447,479349,466033,453438,441505,430185,419430,409200,399457,390167,381300,372827,364722,356962,
392
+        349525,342392,335544,328965,322638,316551,310689,305040,299593,294337,289262,284359,279620,275036,270600,266305,
393
+        262144,258111,254200,250406,246723,243148,239674,236298,233016,229824,226719,223696,220752,217885,215092,212369,
394
+        209715,207126,204600,202135,199728,197379,195083,192841,190650,188508,186413,184365,182361,180400,178481,176602,
395
+        174762,172960,171196,169466,167772,166111,164482,162885,161319,159783,158275,156796,155344,153919,152520
396
+      };
397
+
398
+      // For small divisors, it is best to directly retrieve the results
399
+      if (d <= 110)
400
+        return pgm_read_dword(&small_inv_tab[d]);
401
+
402
+      register uint8_t r8 = d & 0xFF;
403
+      register uint8_t r9 = (d >> 8) & 0xFF;
404
+      register uint8_t r10 = (d >> 16) & 0xFF;
405
+      register uint8_t r2,r3,r4,r5,r6,r7,r11,r12,r13,r14,r15,r16,r17,r18;
406
+      register const uint8_t* ptab = inv_tab;
407
+
408
+      __asm__ __volatile__(
409
+        /*  %8:%7:%6 = interval*/
410
+        /*  r31:r30: MUST be those registers, and they must point to the inv_tab */
411
+
412
+        " clr %13" "\n\t"                 /* %13 = 0 */
413
+
414
+        /*  Now we must compute */
415
+        /*   result = 0xFFFFFF / d */
416
+        /*  %8:%7:%6 = interval*/
417
+        /*  %16:%15:%14 = nr */
418
+        /*  %13 = 0*/
419
+
420
+        /*  A plain division of 24x24 bits should take 388 cycles to complete. We will */
421
+        /*  use Newton-Raphson for the calculation, and will strive to get way less cycles*/
422
+        /*  for the same result - Using C division, it takes 500cycles to complete .*/
423
+
424
+        " clr %3" "\n\t"                  /* idx = 0 */
425
+        " mov %14,%6" "\n\t"
426
+        " mov %15,%7" "\n\t"
427
+        " mov %16,%8" "\n\t"              /* nr = interval */
428
+        " tst %16" "\n\t"                 /* nr & 0xFF0000 == 0 ? */
429
+        " brne 2f" "\n\t"                 /* No, skip this */
430
+        " mov %16,%15" "\n\t"
431
+        " mov %15,%14" "\n\t"             /* nr <<= 8, %14 not needed */
432
+        " subi %3,-8" "\n\t"              /* idx += 8 */
433
+        " tst %16" "\n\t"                 /* nr & 0xFF0000 == 0 ? */
434
+        " brne 2f" "\n\t"                 /* No, skip this */
435
+        " mov %16,%15" "\n\t"             /* nr <<= 8, %14 not needed */
436
+        " clr %15" "\n\t"                 /* We clear %14 */
437
+        " subi %3,-8" "\n\t"              /* idx += 8 */
438
+
439
+        /*  here %16 != 0 and %16:%15 contains at least 9 MSBits, or both %16:%15 are 0 */
440
+        "2:" "\n\t"
441
+        " cpi %16,0x10" "\n\t"            /* (nr & 0xf00000) == 0 ? */
442
+        " brcc 3f" "\n\t"                 /* No, skip this */
443
+        " swap %15" "\n\t"                /* Swap nibbles */
444
+        " swap %16" "\n\t"                /* Swap nibbles. Low nibble is 0 */
445
+        " mov %14, %15" "\n\t"
446
+        " andi %14,0x0f" "\n\t"           /* Isolate low nibble */
447
+        " andi %15,0xf0" "\n\t"           /* Keep proper nibble in %15 */
448
+        " or %16, %14" "\n\t"             /* %16:%15 <<= 4 */
449
+        " subi %3,-4" "\n\t"              /* idx += 4 */
450
+
451
+        "3:" "\n\t"
452
+        " cpi %16,0x40" "\n\t"            /* (nr & 0xc00000) == 0 ? */
453
+        " brcc 4f" "\n\t"                 /* No, skip this*/
454
+        " add %15,%15" "\n\t"
455
+        " adc %16,%16" "\n\t"
456
+        " add %15,%15" "\n\t"
457
+        " adc %16,%16" "\n\t"             /* %16:%15 <<= 2 */
458
+        " subi %3,-2" "\n\t"              /* idx += 2 */
459
+
460
+        "4:" "\n\t"
461
+        " cpi %16,0x80" "\n\t"            /* (nr & 0x800000) == 0 ? */
462
+        " brcc 5f" "\n\t"                 /* No, skip this */
463
+        " add %15,%15" "\n\t"
464
+        " adc %16,%16" "\n\t"             /* %16:%15 <<= 1 */
465
+        " inc %3" "\n\t"                  /* idx += 1 */
466
+
467
+        /*  Now %16:%15 contains its MSBit set to 1, or %16:%15 is == 0. We are now absolutely sure*/
468
+        /*  we have at least 9 MSBits available to enter the initial estimation table*/
469
+        "5:" "\n\t"
470
+        " add %15,%15" "\n\t"
471
+        " adc %16,%16" "\n\t"             /* %16:%15 = tidx = (nr <<= 1), we lose the top MSBit (always set to 1, %16 is the index into the inverse table)*/
472
+        " add r30,%16" "\n\t"             /* Only use top 8 bits */
473
+        " adc r31,%13" "\n\t"             /* r31:r30 = inv_tab + (tidx) */
474
+        " lpm %14, Z" "\n\t"              /* %14 = inv_tab[tidx] */
475
+        " ldi %15, 1" "\n\t"              /* %15 = 1  %15:%14 = inv_tab[tidx] + 256 */
476
+
477
+        /*  We must scale the approximation to the proper place*/
478
+        " clr %16" "\n\t"                 /* %16 will always be 0 here */
479
+        " subi %3,8" "\n\t"               /* idx == 8 ? */
480
+        " breq 6f" "\n\t"                 /* yes, no need to scale*/
481
+        " brcs 7f" "\n\t"                 /* If C=1, means idx < 8, result was negative!*/
482
+
483
+        /*  idx > 8, now %3 = idx - 8. We must perform a left shift. idx range:[1-8]*/
484
+        " sbrs %3,0" "\n\t"               /* shift by 1bit position?*/
485
+        " rjmp 8f" "\n\t"                 /* No*/
486
+        " add %14,%14" "\n\t"
487
+        " adc %15,%15" "\n\t"             /* %15:16 <<= 1*/
488
+        "8:" "\n\t"
489
+        " sbrs %3,1" "\n\t"               /* shift by 2bit position?*/
490
+        " rjmp 9f" "\n\t"                 /* No*/
491
+        " add %14,%14" "\n\t"
492
+        " adc %15,%15" "\n\t"
493
+        " add %14,%14" "\n\t"
494
+        " adc %15,%15" "\n\t"             /* %15:16 <<= 1*/
495
+        "9:" "\n\t"
496
+        " sbrs %3,2" "\n\t"               /* shift by 4bits position?*/
497
+        " rjmp 16f" "\n\t"                /* No*/
498
+        " swap %15" "\n\t"                /* Swap nibbles. lo nibble of %15 will always be 0*/
499
+        " swap %14" "\n\t"                /* Swap nibbles*/
500
+        " mov %12,%14" "\n\t"
501
+        " andi %12,0x0f" "\n\t"           /* isolate low nibble*/
502
+        " andi %14,0xf0" "\n\t"           /* and clear it*/
503
+        " or %15,%12" "\n\t"              /* %15:%16 <<= 4*/
504
+        "16:" "\n\t"
505
+        " sbrs %3,3" "\n\t"               /* shift by 8bits position?*/
506
+        " rjmp 6f" "\n\t"                 /* No, we are done */
507
+        " mov %16,%15" "\n\t"
508
+        " mov %15,%14" "\n\t"
509
+        " clr %14" "\n\t"
510
+        " jmp 6f" "\n\t"
511
+
512
+        /*  idx < 8, now %3 = idx - 8. Get the count of bits */
513
+        "7:" "\n\t"
514
+        " neg %3" "\n\t"                  /* %3 = -idx = count of bits to move right. idx range:[1...8]*/
515
+        " sbrs %3,0" "\n\t"               /* shift by 1 bit position ?*/
516
+        " rjmp 10f" "\n\t"                /* No, skip it*/
517
+        " asr %15" "\n\t"                 /* (bit7 is always 0 here)*/
518
+        " ror %14" "\n\t"
519
+        "10:" "\n\t"
520
+        " sbrs %3,1" "\n\t"               /* shift by 2 bit position ?*/
521
+        " rjmp 11f" "\n\t"                /* No, skip it*/
522
+        " asr %15" "\n\t"                 /* (bit7 is always 0 here)*/
523
+        " ror %14" "\n\t"
524
+        " asr %15" "\n\t"                 /* (bit7 is always 0 here)*/
525
+        " ror %14" "\n\t"
526
+        "11:" "\n\t"
527
+        " sbrs %3,2" "\n\t"               /* shift by 4 bit position ?*/
528
+        " rjmp 12f" "\n\t"                /* No, skip it*/
529
+        " swap %15" "\n\t"                /* Swap nibbles*/
530
+        " andi %14, 0xf0" "\n\t"          /* Lose the lowest nibble*/
531
+        " swap %14" "\n\t"                /* Swap nibbles. Upper nibble is 0*/
532
+        " or %14,%15" "\n\t"              /* Pass nibble from upper byte*/
533
+        " andi %15, 0x0f" "\n\t"          /* And get rid of that nibble*/
534
+        "12:" "\n\t"
535
+        " sbrs %3,3" "\n\t"               /* shift by 8 bit position ?*/
536
+        " rjmp 6f" "\n\t"                 /* No, skip it*/
537
+        " mov %14,%15" "\n\t"
538
+        " clr %15" "\n\t"
539
+        "6:" "\n\t"                       /* %16:%15:%14 = initial estimation of 0x1000000 / d*/
540
+
541
+        /*  Now, we must refine the estimation present on %16:%15:%14 using 1 iteration*/
542
+        /*   of Newton-Raphson. As it has a quadratic convergence, 1 iteration is enough*/
543
+        /*   to get more than 18bits of precision (the initial table lookup gives 9 bits of*/
544
+        /*   precision to start from). 18bits of precision is all what is needed here for result */
545
+
546
+        /*  %8:%7:%6 = d = interval*/
547
+        /*  %16:%15:%14 = x = initial estimation of 0x1000000 / d*/
548
+        /*  %13 = 0*/
549
+        /*  %3:%2:%1:%0 = working accumulator*/
550
+
551
+        /*  Compute 1<<25 - x*d. Result should never exceed 25 bits and should always be positive*/
552
+        " clr %0" "\n\t"
553
+        " clr %1" "\n\t"
554
+        " clr %2" "\n\t"
555
+        " ldi %3,2" "\n\t"                /* %3:%2:%1:%0 = 0x2000000*/
556
+        " mul %6,%14" "\n\t"              /* r1:r0 = LO(d) * LO(x)*/
557
+        " sub %0,r0" "\n\t"
558
+        " sbc %1,r1" "\n\t"
559
+        " sbc %2,%13" "\n\t"
560
+        " sbc %3,%13" "\n\t"              /* %3:%2:%1:%0 -= LO(d) * LO(x)*/
561
+        " mul %7,%14" "\n\t"              /* r1:r0 = MI(d) * LO(x)*/
562
+        " sub %1,r0" "\n\t"
563
+        " sbc %2,r1"  "\n\t"
564
+        " sbc %3,%13" "\n\t"              /* %3:%2:%1:%0 -= MI(d) * LO(x) << 8*/
565
+        " mul %8,%14" "\n\t"              /* r1:r0 = HI(d) * LO(x)*/
566
+        " sub %2,r0" "\n\t"
567
+        " sbc %3,r1" "\n\t"               /* %3:%2:%1:%0 -= MIL(d) * LO(x) << 16*/
568
+        " mul %6,%15" "\n\t"              /* r1:r0 = LO(d) * MI(x)*/
569
+        " sub %1,r0" "\n\t"
570
+        " sbc %2,r1" "\n\t"
571
+        " sbc %3,%13" "\n\t"              /* %3:%2:%1:%0 -= LO(d) * MI(x) << 8*/
572
+        " mul %7,%15" "\n\t"              /* r1:r0 = MI(d) * MI(x)*/
573
+        " sub %2,r0" "\n\t"
574
+        " sbc %3,r1" "\n\t"               /* %3:%2:%1:%0 -= MI(d) * MI(x) << 16*/
575
+        " mul %8,%15" "\n\t"              /* r1:r0 = HI(d) * MI(x)*/
576
+        " sub %3,r0" "\n\t"               /* %3:%2:%1:%0 -= MIL(d) * MI(x) << 24*/
577
+        " mul %6,%16" "\n\t"              /* r1:r0 = LO(d) * HI(x)*/
578
+        " sub %2,r0" "\n\t"
579
+        " sbc %3,r1" "\n\t"               /* %3:%2:%1:%0 -= LO(d) * HI(x) << 16*/
580
+        " mul %7,%16" "\n\t"              /* r1:r0 = MI(d) * HI(x)*/
581
+        " sub %3,r0" "\n\t"               /* %3:%2:%1:%0 -= MI(d) * HI(x) << 24*/
582
+        /*  %3:%2:%1:%0 = (1<<25) - x*d     [169]*/
583
+
584
+        /*  We need to multiply that result by x, and we are only interested in the top 24bits of that multiply*/
585
+
586
+        /*  %16:%15:%14 = x = initial estimation of 0x1000000 / d*/
587
+        /*  %3:%2:%1:%0 = (1<<25) - x*d = acc*/
588
+        /*  %13 = 0 */
589
+
590
+        /*  result = %11:%10:%9:%5:%4*/
591
+        " mul %14,%0" "\n\t"              /* r1:r0 = LO(x) * LO(acc)*/
592
+        " mov %4,r1" "\n\t"
593
+        " clr %5" "\n\t"
594
+        " clr %9" "\n\t"
595
+        " clr %10" "\n\t"
596
+        " clr %11" "\n\t"                 /* %11:%10:%9:%5:%4 = LO(x) * LO(acc) >> 8*/
597
+        " mul %15,%0" "\n\t"              /* r1:r0 = MI(x) * LO(acc)*/
598
+        " add %4,r0" "\n\t"
599
+        " adc %5,r1" "\n\t"
600
+        " adc %9,%13" "\n\t"
601
+        " adc %10,%13" "\n\t"
602
+        " adc %11,%13" "\n\t"             /* %11:%10:%9:%5:%4 += MI(x) * LO(acc) */
603
+        " mul %16,%0" "\n\t"              /* r1:r0 = HI(x) * LO(acc)*/
604
+        " add %5,r0" "\n\t"
605
+        " adc %9,r1" "\n\t"
606
+        " adc %10,%13" "\n\t"
607
+        " adc %11,%13" "\n\t"             /* %11:%10:%9:%5:%4 += MI(x) * LO(acc) << 8*/
608
+
609
+        " mul %14,%1" "\n\t"              /* r1:r0 = LO(x) * MIL(acc)*/
610
+        " add %4,r0" "\n\t"
611
+        " adc %5,r1" "\n\t"
612
+        " adc %9,%13" "\n\t"
613
+        " adc %10,%13" "\n\t"
614
+        " adc %11,%13" "\n\t"             /* %11:%10:%9:%5:%4 = LO(x) * MIL(acc)*/
615
+        " mul %15,%1" "\n\t"              /* r1:r0 = MI(x) * MIL(acc)*/
616
+        " add %5,r0" "\n\t"
617
+        " adc %9,r1" "\n\t"
618
+        " adc %10,%13" "\n\t"
619
+        " adc %11,%13" "\n\t"             /* %11:%10:%9:%5:%4 += MI(x) * MIL(acc) << 8*/
620
+        " mul %16,%1" "\n\t"              /* r1:r0 = HI(x) * MIL(acc)*/
621
+        " add %9,r0" "\n\t"
622
+        " adc %10,r1" "\n\t"
623
+        " adc %11,%13" "\n\t"             /* %11:%10:%9:%5:%4 += MI(x) * MIL(acc) << 16*/
624
+
625
+        " mul %14,%2" "\n\t"              /* r1:r0 = LO(x) * MIH(acc)*/
626
+        " add %5,r0" "\n\t"
627
+        " adc %9,r1" "\n\t"
628
+        " adc %10,%13" "\n\t"
629
+        " adc %11,%13" "\n\t"             /* %11:%10:%9:%5:%4 = LO(x) * MIH(acc) << 8*/
630
+        " mul %15,%2" "\n\t"              /* r1:r0 = MI(x) * MIH(acc)*/
631
+        " add %9,r0" "\n\t"
632
+        " adc %10,r1" "\n\t"
633
+        " adc %11,%13" "\n\t"             /* %11:%10:%9:%5:%4 += MI(x) * MIH(acc) << 16*/
634
+        " mul %16,%2" "\n\t"              /* r1:r0 = HI(x) * MIH(acc)*/
635
+        " add %10,r0" "\n\t"
636
+        " adc %11,r1" "\n\t"              /* %11:%10:%9:%5:%4 += MI(x) * MIH(acc) << 24*/
637
+
638
+        " mul %14,%3" "\n\t"              /* r1:r0 = LO(x) * HI(acc)*/
639
+        " add %9,r0" "\n\t"
640
+        " adc %10,r1" "\n\t"
641
+        " adc %11,%13" "\n\t"             /* %11:%10:%9:%5:%4 = LO(x) * HI(acc) << 16*/
642
+        " mul %15,%3" "\n\t"              /* r1:r0 = MI(x) * HI(acc)*/
643
+        " add %10,r0" "\n\t"
644
+        " adc %11,r1" "\n\t"              /* %11:%10:%9:%5:%4 += MI(x) * HI(acc) << 24*/
645
+        " mul %16,%3" "\n\t"              /* r1:r0 = HI(x) * HI(acc)*/
646
+        " add %11,r0" "\n\t"              /* %11:%10:%9:%5:%4 += MI(x) * HI(acc) << 32*/
647
+
648
+        /*  At this point, %11:%10:%9 contains the new estimation of x. */
649
+
650
+        /*  Finally, we must correct the result. Estimate remainder as*/
651
+        /*  (1<<24) - x*d*/
652
+        /*  %11:%10:%9 = x*/
653
+        /*  %8:%7:%6 = d = interval" "\n\t" /*  */
654
+        " ldi %3,1" "\n\t"
655
+        " clr %2" "\n\t"
656
+        " clr %1" "\n\t"
657
+        " clr %0" "\n\t"                  /* %3:%2:%1:%0 = 0x1000000*/
658
+        " mul %6,%9" "\n\t"              /* r1:r0 = LO(d) * LO(x)*/
659
+        " sub %0,r0" "\n\t"
660
+        " sbc %1,r1" "\n\t"
661
+        " sbc %2,%13" "\n\t"
662
+        " sbc %3,%13" "\n\t"              /* %3:%2:%1:%0 -= LO(d) * LO(x)*/
663
+        " mul %7,%9" "\n\t"              /* r1:r0 = MI(d) * LO(x)*/
664
+        " sub %1,r0" "\n\t"
665
+        " sbc %2,r1" "\n\t"
666
+        " sbc %3,%13" "\n\t"              /* %3:%2:%1:%0 -= MI(d) * LO(x) << 8*/
667
+        " mul %8,%9" "\n\t"              /* r1:r0 = HI(d) * LO(x)*/
668
+        " sub %2,r0" "\n\t"
669
+        " sbc %3,r1" "\n\t"               /* %3:%2:%1:%0 -= MIL(d) * LO(x) << 16*/
670
+        " mul %6,%10" "\n\t"              /* r1:r0 = LO(d) * MI(x)*/
671
+        " sub %1,r0" "\n\t"
672
+        " sbc %2,r1" "\n\t"
673
+        " sbc %3,%13" "\n\t"              /* %3:%2:%1:%0 -= LO(d) * MI(x) << 8*/
674
+        " mul %7,%10" "\n\t"              /* r1:r0 = MI(d) * MI(x)*/
675
+        " sub %2,r0" "\n\t"
676
+        " sbc %3,r1" "\n\t"               /* %3:%2:%1:%0 -= MI(d) * MI(x) << 16*/
677
+        " mul %8,%10" "\n\t"              /* r1:r0 = HI(d) * MI(x)*/
678
+        " sub %3,r0" "\n\t"               /* %3:%2:%1:%0 -= MIL(d) * MI(x) << 24*/
679
+        " mul %6,%11" "\n\t"              /* r1:r0 = LO(d) * HI(x)*/
680
+        " sub %2,r0" "\n\t"
681
+        " sbc %3,r1" "\n\t"               /* %3:%2:%1:%0 -= LO(d) * HI(x) << 16*/
682
+        " mul %7,%11" "\n\t"              /* r1:r0 = MI(d) * HI(x)*/
683
+        " sub %3,r0" "\n\t"               /* %3:%2:%1:%0 -= MI(d) * HI(x) << 24*/
684
+        /*  %3:%2:%1:%0 = r = (1<<24) - x*d*/
685
+        /*  %8:%7:%6 = d = interval */
686
+
687
+        /*  Perform the final correction*/
688
+        " sub %0,%6" "\n\t"
689
+        " sbc %1,%7" "\n\t"
690
+        " sbc %2,%8" "\n\t"              /* r -= d*/
691
+        " brcs 14f" "\n\t"                /* if ( r >= d) */
692
+
693
+        /*  %11:%10:%9 = x */
694
+        " ldi %3,1" "\n\t"
695
+        " add %9,%3" "\n\t"
696
+        " adc %10,%13" "\n\t"
697
+        " adc %11,%13" "\n\t"             /* x++*/
698
+        "14:" "\n\t"
699
+
700
+        /*  Estimation is done. %11:%10:%9 = x */
701
+        " clr __zero_reg__" "\n\t"        /* Make C runtime happy */
702
+        /*  [211 cycles total]*/
703
+        : "=r" (r2),
704
+          "=r" (r3),
705
+          "=r" (r4),
706
+          "=d" (r5),
707
+          "=r" (r6),
708
+          "=r" (r7),
709
+          "+r" (r8),
710
+          "+r" (r9),
711
+          "+r" (r10),
712
+          "=d" (r11),
713
+          "=r" (r12),
714
+          "=r" (r13),
715
+          "=d" (r14),
716
+          "=d" (r15),
717
+          "=d" (r16),
718
+          "=d" (r17),
719
+          "=d" (r18),
720
+          "+z" (ptab)
721
+        :
722
+        : "r0", "r1", "cc"
723
+      );
724
+
725
+      // Return the result
726
+      return r11 | (uint16_t(r12) << 8) | (uint32_t(r13) << 16);
727
+    }
728
+  #else
729
+    // All the other 32 CPUs can easily perform the inverse using hardware division,
730
+    // so we don´t need to reduce precision or to use assembly language at all.
731
+
732
+    // This routine, for all the other archs, returns 0x100000000 / d ~= 0xFFFFFFFF / d
733
+    static FORCE_INLINE uint32_t get_period_inverse(uint32_t d) {
734
+      return 0xFFFFFFFF / d;
735
+    }
736
+  #endif
737
+#endif
738
+
218 739
 #define MINIMAL_STEP_RATE 120
219 740
 
220 741
 /**
@@ -266,8 +787,13 @@ void Planner::calculate_trapezoid_for_block(block_t* const block, const float &e
266 787
 
267 788
   #if ENABLED(BEZIER_JERK_CONTROL)
268 789
     // Jerk controlled speed requires to express speed versus time, NOT steps
269
-    int32_t acceleration_time = ((float)(cruise_rate - initial_rate) / accel) * HAL_STEPPER_TIMER_RATE,
270
-            deceleration_time = ((float)(cruise_rate - final_rate) / accel) * HAL_STEPPER_TIMER_RATE;
790
+    uint32_t acceleration_time = ((float)(cruise_rate - initial_rate) / accel) * HAL_STEPPER_TIMER_RATE,
791
+             deceleration_time = ((float)(cruise_rate - final_rate) / accel) * HAL_STEPPER_TIMER_RATE;
792
+
793
+    // And to offload calculations from the ISR, we also calculate the inverse of those times here
794
+    uint32_t acceleration_time_inverse = get_period_inverse(acceleration_time);
795
+    uint32_t deceleration_time_inverse = get_period_inverse(deceleration_time);
796
+
271 797
   #endif
272 798
 
273 799
   CRITICAL_SECTION_START;  // Fill variables used by the stepper in a critical section
@@ -278,6 +804,8 @@ void Planner::calculate_trapezoid_for_block(block_t* const block, const float &e
278 804
     #if ENABLED(BEZIER_JERK_CONTROL)
279 805
       block->acceleration_time = acceleration_time;
280 806
       block->deceleration_time = deceleration_time;
807
+      block->acceleration_time_inverse = acceleration_time_inverse;
808
+      block->deceleration_time_inverse = deceleration_time_inverse;
281 809
       block->cruise_rate = cruise_rate;
282 810
     #endif
283 811
     block->final_rate = final_rate;

+ 4
- 2
Marlin/src/module/planner.h Прегледај датотеку

@@ -96,8 +96,10 @@ typedef struct {
96 96
 
97 97
   #if ENABLED(BEZIER_JERK_CONTROL)
98 98
     uint32_t cruise_rate;                   // The actual cruise rate to use, between end of the acceleration phase and start of deceleration phase
99
-    int32_t acceleration_time,              // Acceleration time and deceleration time in STEP timer counts
100
-            deceleration_time;
99
+    uint32_t acceleration_time,             // Acceleration time and deceleration time in STEP timer counts
100
+             deceleration_time;
101
+    uint32_t acceleration_time_inverse,     // Inverse of acceleration and deceleration periods, expressed as integer. Scale depends on CPU being used
102
+             deceleration_time_inverse;
101 103
   #else
102 104
     int32_t acceleration_rate;              // The acceleration rate used for acceleration calculation
103 105
   #endif

+ 724
- 116
Marlin/src/module/stepper.cpp Прегледај датотеку

@@ -117,11 +117,14 @@ long Stepper::counter_X = 0,
117 117
 volatile uint32_t Stepper::step_events_completed = 0; // The number of step events executed in the current block
118 118
 
119 119
 #if ENABLED(BEZIER_JERK_CONTROL)
120
-  int32_t Stepper::bezier_A,        // A coefficient in Bézier speed curve
121
-          Stepper::bezier_B,        // B coefficient in Bézier speed curve
122
-          Stepper::bezier_C,        // C coefficient in Bézier speed curve
123
-          Stepper::bezier_F;        // F coefficient in Bézier speed curve
124
-  uint32_t Stepper::bezier_AV;      // AV coefficient in Bézier speed curve
120
+  int32_t __attribute__((used)) Stepper::bezier_A __asm__("bezier_A");    // A coefficient in Bézier speed curve with alias for assembler
121
+  int32_t __attribute__((used)) Stepper::bezier_B __asm__("bezier_B");    // B coefficient in Bézier speed curve with alias for assembler
122
+  int32_t __attribute__((used)) Stepper::bezier_C __asm__("bezier_C");    // C coefficient in Bézier speed curve with alias for assembler
123
+  uint32_t __attribute__((used)) Stepper::bezier_F __asm__("bezier_F");   // F coefficient in Bézier speed curve with alias for assembler
124
+  uint32_t __attribute__((used)) Stepper::bezier_AV __asm__("bezier_AV"); // AV coefficient in Bézier speed curve with alias for assembler
125
+  #ifdef __AVR__
126
+    bool __attribute__((used)) Stepper::A_negative __asm__("A_negative");   // If A coefficient was negative
127
+  #endif
125 128
   bool Stepper::bezier_2nd_half;    // =false If Bézier curve has been initialized or not
126 129
 #endif
127 130
 
@@ -391,130 +394,735 @@ void Stepper::set_directions() {
391 394
    *
392 395
    *  Note the abbreviations we use in the following formulae are between []s
393 396
    *
394
-   *  At the start of each trapezoid, we calculate the coefficients A,B,C,F and Advance [AV], as follows:
397
+   *  For Any 32bit CPU:
398
+   *
399
+   *    At the start of each trapezoid, we calculate the coefficients A,B,C,F and Advance [AV], as follows:
400
+   *
401
+   *      A =  6*128*(VF - VI) =  768*(VF - VI)
402
+   *      B = 15*128*(VI - VF) = 1920*(VI - VF)
403
+   *      C = 10*128*(VF - VI) = 1280*(VF - VI)
404
+   *      F =    128*VI        =  128*VI
405
+   *     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)
406
+   *
407
+   *   And for each point, we will evaluate the curve with the following sequence:
408
+   *
409
+   *      void lsrs(uint32_t& d, uint32_t s, int cnt) {
410
+   *        d = s >> cnt;
411
+   *      }
412
+   *      void lsls(uint32_t& d, uint32_t s, int cnt) {
413
+   *        d = s << cnt;
414
+   *      }
415
+   *      void lsrs(int32_t& d, uint32_t s, int cnt) {
416
+   *        d = uint32_t(s) >> cnt;
417
+   *      }
418
+   *      void lsls(int32_t& d, uint32_t s, int cnt) {
419
+   *        d = uint32_t(s) << cnt;
420
+   *      }
421
+   *      void umull(uint32_t& rlo, uint32_t& rhi, uint32_t op1, uint32_t op2) {
422
+   *        uint64_t res = uint64_t(op1) * op2;
423
+   *        rlo = uint32_t(res & 0xFFFFFFFF);
424
+   *        rhi = uint32_t((res >> 32) & 0xFFFFFFFF);
425
+   *      }
426
+   *      void smlal(int32_t& rlo, int32_t& rhi, int32_t op1, int32_t op2) {
427
+   *        int64_t mul = int64_t(op1) * op2;
428
+   *        int64_t s = int64_t(uint32_t(rlo) | ((uint64_t(uint32_t(rhi)) << 32U)));
429
+   *        mul += s;
430
+   *        rlo = int32_t(mul & 0xFFFFFFFF);
431
+   *        rhi = int32_t((mul >> 32) & 0xFFFFFFFF);
432
+   *      }
433
+   *      int32_t _eval_bezier_curve_arm(uint32_t curr_step) {
434
+   *        register uint32_t flo = 0;
435
+   *        register uint32_t fhi = bezier_AV * curr_step;
436
+   *        register uint32_t t = fhi;
437
+   *        register int32_t alo = bezier_F;
438
+   *        register int32_t ahi = 0;
439
+   *        register int32_t A = bezier_A;
440
+   *        register int32_t B = bezier_B;
441
+   *        register int32_t C = bezier_C;
442
+   *
443
+   *        lsrs(ahi, alo, 1);          // a  = F << 31
444
+   *        lsls(alo, alo, 31);         //
445
+   *        umull(flo, fhi, fhi, t);    // f *= t
446
+   *        umull(flo, fhi, fhi, t);    // f>>=32; f*=t
447
+   *        lsrs(flo, fhi, 1);          //
448
+   *        smlal(alo, ahi, flo, C);    // a+=(f>>33)*C
449
+   *        umull(flo, fhi, fhi, t);    // f>>=32; f*=t
450
+   *        lsrs(flo, fhi, 1);          //
451
+   *        smlal(alo, ahi, flo, B);    // a+=(f>>33)*B
452
+   *        umull(flo, fhi, fhi, t);    // f>>=32; f*=t
453
+   *        lsrs(flo, fhi, 1);          // f>>=33;
454
+   *        smlal(alo, ahi, flo, A);    // a+=(f>>33)*A;
455
+   *        lsrs(alo, ahi, 6);          // a>>=38
456
+   *
457
+   *        return alo;
458
+   *      }
459
+   *
460
+   *    This will be rewritten in ARM assembly to get peak performance and will take 43 cycles to execute
395 461
    *
396
-   *   A =  6*128*(VF - VI) =  768*(VF - VI)
397
-   *   B = 15*128*(VI - VF) = 1920*(VI - VF)
398
-   *   C = 10*128*(VF - VI) = 1280*(VF - VI)
399
-   *   F =    128*VI        =  128*VI
400
-   *  AV = (1<<32)/TS      ~= 0xFFFFFFFF / TS (To use ARM UDIV, that is 32 bits)
462
+   *  For AVR, we scale precision of coefficients to make it possible to evaluate the Bézier curve in
463
+   *    realtime: Let's reduce precision as much as possible. After some experimentation we found that:
401 464
    *
402
-   *  And for each point, we will evaluate the curve with the following sequence:
465
+   *    Assume t and AV with 24 bits is enough
466
+   *       A =  6*(VF - VI)
467
+   *       B = 15*(VI - VF)
468
+   *       C = 10*(VF - VI)
469
+   *       F =     VI
470
+   *      AV = (1<<24)/TS   (this is computed at the planner, to offload expensive calculations from the ISR)
403 471
    *
404
-   *    uint32_t t = bezier_AV * curr_step;               // t: Range 0 - 1^32 = 32 bits
405
-   *    uint64_t f = t;
406
-   *    f *= t;                                           // Range 32*2 = 64 bits (unsigned)
407
-   *    f >>= 32;                                         // Range 32 bits  (unsigned)
408
-   *    f *= t;                                           // Range 32*2 = 64 bits  (unsigned)
409
-   *    f >>= 32;                                         // Range 32 bits : f = t^3  (unsigned)
410
-   *    int64_t acc = (int64_t) bezier_F << 31;           // Range 63 bits (signed)
411
-   *    acc += ((uint32_t) f >> 1) * (int64_t) bezier_C;  // Range 29bits + 31 = 60bits (plus sign)
412
-   *    f *= t;                                           // Range 32*2 = 64 bits
413
-   *    f >>= 32;                                         // Range 32 bits : f = t^3  (unsigned)
414
-   *    acc += ((uint32_t) f >> 1) * (int64_t) bezier_B;  // Range 29bits + 31 = 60bits (plus sign)
415
-   *    f *= t;                                           // Range 32*2 = 64 bits
416
-   *    f >>= 32;                                         // Range 32 bits : f = t^3  (unsigned)
417
-   *    acc += ((uint32_t) f >> 1) * (int64_t) bezier_A;  // Range 28bits + 31 = 59bits (plus sign)
418
-   *    acc >>= (31 + 7);                                 // Range 24bits (plus sign)
472
+   *     Instead of storing sign for each coefficient, we will store its absolute value,
473
+   *    and flag the sign of the A coefficient, so we can save to store the sign bit.
474
+   *     It always holds that sign(A) = - sign(B) = sign(C)
419 475
    *
420
-   * This can be translated to the following ARM assembly sequence:
476
+   *     So, the resulting range of the coefficients are:
421 477
    *
422
-   * At start:
423
-   *  fhi = AV, flo = CS, alo = F
478
+   *       t: unsigned (0 <= t < 1) |range 0 to 0xFFFFFF unsigned
479
+   *       A:   signed Q24 , range = 250000 * 6 = 1500000 = 0x16E360 | 21 bits
480
+   *       B:   signed Q24 , range = 250000 *15 = 3750000 = 0x393870 | 22 bits
481
+   *       C:   signed Q24 , range = 250000 *10 = 2500000 = 0x1312D0 | 21 bits
482
+   *       F:   signed Q24 , range = 250000     =  250000 = 0x0ED090 | 20 bits
483
+   *
484
+   *    And for each curve, we estimate its coefficients with:
485
+   *
486
+   *      void _calc_bezier_curve_coeffs(int32_t v0, int32_t v1, uint32_t av) {
487
+   *       // Calculate the Bézier coefficients
488
+   *       if (v1 < v0) {
489
+   *         A_negative = true;
490
+   *         bezier_A = 6 * (v0 - v1);
491
+   *         bezier_B = 15 * (v0 - v1);
492
+   *         bezier_C = 10 * (v0 - v1);
493
+   *       }
494
+   *       else {
495
+   *         A_negative = false;
496
+   *         bezier_A = 6 * (v1 - v0);
497
+   *         bezier_B = 15 * (v1 - v0);
498
+   *         bezier_C = 10 * (v1 - v0);
499
+   *       }
500
+   *       bezier_F = v0;
501
+   *      }
502
+   *
503
+   *    And for each point, we will evaluate the curve with the following sequence:
504
+   *
505
+   *      // unsigned multiplication of 24 bits x 24bits, return upper 16 bits
506
+   *      void umul24x24to16hi(uint16_t& r, uint24_t op1, uint24_t op2) {
507
+   *        r = (uint64_t(op1) * op2) >> 8;
508
+   *      }
509
+   *      // unsigned multiplication of 16 bits x 16bits, return upper 16 bits
510
+   *      void umul16x16to16hi(uint16_t& r, uint16_t op1, uint16_t op2) {
511
+   *        r = (uint32_t(op1) * op2) >> 16;
512
+   *      }
513
+   *      // unsigned multiplication of 16 bits x 24bits, return upper 24 bits
514
+   *      void umul16x24to24hi(uint24_t& r, uint16_t op1, uint24_t op2) {
515
+   *        r = uint24_t((uint64_t(op1) * op2) >> 16);
516
+   *      }
517
+   *
518
+   *      int32_t _eval_bezier_curve(uint32_t curr_step) {
519
+   *        // To save computing, the first step is always the initial speed
520
+   *        if (!curr_step)
521
+   *          return bezier_F;
522
+   *
523
+   *        uint16_t t;
524
+   *        umul24x24to16hi(t, bezier_AV, curr_step);   // t: Range 0 - 1^16 = 16 bits
525
+   *        uint16_t f = t;
526
+   *        umul16x16to16hi(f, f, t);           // Range 16 bits (unsigned)
527
+   *        umul16x16to16hi(f, f, t);           // Range 16 bits : f = t^3  (unsigned)
528
+   *        uint24_t acc = bezier_F;          // Range 20 bits (unsigned)
529
+   *        if (A_negative) {
530
+   *          uint24_t v;
531
+   *          umul16x24to24hi(v, f, bezier_C);    // Range 21bits
532
+   *          acc -= v;
533
+   *          umul16x16to16hi(f, f, t);         // Range 16 bits : f = t^4  (unsigned)
534
+   *          umul16x24to24hi(v, f, bezier_B);    // Range 22bits
535
+   *          acc += v;
536
+   *          umul16x16to16hi(f, f, t);         // Range 16 bits : f = t^5  (unsigned)
537
+   *          umul16x24to24hi(v, f, bezier_A);    // Range 21bits + 15 = 36bits (plus sign)
538
+   *          acc -= v;
539
+   *        }
540
+   *        else {
541
+   *          uint24_t v;
542
+   *          umul16x24to24hi(v, f, bezier_C);    // Range 21bits
543
+   *          acc += v;
544
+   *          umul16x16to16hi(f, f, t);       // Range 16 bits : f = t^4  (unsigned)
545
+   *          umul16x24to24hi(v, f, bezier_B);    // Range 22bits
546
+   *          acc -= v;
547
+   *          umul16x16to16hi(f, f, t);               // Range 16 bits : f = t^5  (unsigned)
548
+   *          umul16x24to24hi(v, f, bezier_A);    // Range 21bits + 15 = 36bits (plus sign)
549
+   *          acc += v;
550
+   *        }
551
+   *        return acc;
552
+   *      }
553
+   *    Those functions will be translated into assembler to get peak performance. coefficient calculations takes 70 cycles,
554
+   *    Bezier point evaluation takes 150 cycles
424 555
    *
425
-   *  muls  fhi,flo               | f = AV * CS       1 cycles
426
-   *  mov   t,fhi                 | t = AV * CS       1 cycles
427
-   *  lsrs  ahi,alo,#1            | a  = F << 31      1 cycles
428
-   *  lsls  alo,alo,#31           |                   1 cycles
429
-   *  umull flo,fhi,fhi,t         | f *= t            5 cycles [fhi:flo=64bits
430
-   *  umull flo,fhi,fhi,t         | f>>=32; f*=t      5 cycles [fhi:flo=64bits
431
-   *  lsrs  flo,fhi,#1            |                   1 cycles [31bits
432
-   *  smlal alo,ahi,flo,C         | a+=(f>>33)*C;     5 cycles
433
-   *  umull flo,fhi,fhi,t         | f>>=32; f*=t      5 cycles [fhi:flo=64bits
434
-   *  lsrs  flo,fhi,#1            |                   1 cycles [31bits
435
-   *  smlal alo,ahi,flo,B         | a+=(f>>33)*B;     5 cycles
436
-   *  umull flo,fhi,fhi,t         | f>>=32; f*=t      5 cycles [fhi:flo=64bits
437
-   *  lsrs  flo,fhi,#1            | f>>=33;           1 cycles [31bits
438
-   *  smlal alo,ahi,flo,A         | a+=(f>>33)*A;     5 cycles
439
-   *  lsrs  alo,ahi,#6            | a>>=38            1 cycles
440
-   *  43 cycles total
441 556
    */
442 557
 
443
-  FORCE_INLINE void Stepper::_calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t interval) {
444
-    // Calculate the Bézier coefficients
445
-    bezier_A =  768 * (v1 - v0);
446
-    bezier_B = 1920 * (v0 - v1);
447
-    bezier_C = 1280 * (v1 - v0);
448
-    bezier_F =  128 * v0;
449
-    bezier_AV = 0xFFFFFFFF / interval;
450
-  }
558
+  #ifdef __AVR__
451 559
 
452
-  FORCE_INLINE int32_t Stepper::_eval_bezier_curve(const uint32_t curr_step) {
453
-    #if defined(__ARM__) || defined(__thumb__)
454
-
455
-      // For ARM CORTEX M3/M4 CPUs, we have the optimized assembler version, that takes 43 cycles to execute
456
-      register uint32_t flo = 0;
457
-      register uint32_t fhi = bezier_AV * curr_step;
458
-      register uint32_t t = fhi;
459
-      register int32_t alo = bezier_F;
460
-      register int32_t ahi = 0;
461
-      register int32_t A = bezier_A;
462
-      register int32_t B = bezier_B;
463
-      register int32_t C = bezier_C;
464
-
465
-       __asm__ __volatile__(
466
-        ".syntax unified"                   "\n\t"  // is to prevent CM0,CM1 non-unified syntax
467
-        " lsrs  %[ahi],%[alo],#1"           "\n\t"  // a  = F << 31      1 cycles
468
-        " lsls  %[alo],%[alo],#31"          "\n\t"  //                   1 cycles
469
-        " umull %[flo],%[fhi],%[fhi],%[t]"  "\n\t"  // f *= t            5 cycles [fhi:flo=64bits]
470
-        " umull %[flo],%[fhi],%[fhi],%[t]"  "\n\t"  // f>>=32; f*=t      5 cycles [fhi:flo=64bits]
471
-        " lsrs  %[flo],%[fhi],#1"           "\n\t"  //                   1 cycles [31bits]
472
-        " smlal %[alo],%[ahi],%[flo],%[C]"  "\n\t"  // a+=(f>>33)*C;     5 cycles
473
-        " umull %[flo],%[fhi],%[fhi],%[t]"  "\n\t"  // f>>=32; f*=t      5 cycles [fhi:flo=64bits]
474
-        " lsrs  %[flo],%[fhi],#1"           "\n\t"  //                   1 cycles [31bits]
475
-        " smlal %[alo],%[ahi],%[flo],%[B]"  "\n\t"  // a+=(f>>33)*B;     5 cycles
476
-        " umull %[flo],%[fhi],%[fhi],%[t]"  "\n\t"  // f>>=32; f*=t      5 cycles [fhi:flo=64bits]
477
-        " lsrs  %[flo],%[fhi],#1"           "\n\t"  // f>>=33;           1 cycles [31bits]
478
-        " smlal %[alo],%[ahi],%[flo],%[A]"  "\n\t"  // a+=(f>>33)*A;     5 cycles
479
-        " lsrs  %[alo],%[ahi],#6"           "\n\t"  // a>>=38            1 cycles
480
-        : [alo]"+r"( alo ) ,
481
-          [flo]"+r"( flo ) ,
482
-          [fhi]"+r"( fhi ) ,
483
-          [ahi]"+r"( ahi ) ,
484
-          [A]"+r"( A ) ,  // <== Note: Even if A, B, C, and t registers are INPUT ONLY
485
-          [B]"+r"( B ) ,  //  GCC does bad optimizations on the code if we list them as
486
-          [C]"+r"( C ) ,  //  such, breaking this function. So, to avoid that problem,
487
-          [t]"+r"( t )    //  we list all registers as input-outputs.
560
+    // For AVR we use assembly to maximize speed
561
+    void Stepper::_calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t av) {
562
+
563
+      // Store advance
564
+      bezier_AV = av;
565
+
566
+      // Calculate the rest of the coefficients
567
+      register uint8_t r2 = v0 & 0xFF;
568
+      register uint8_t r3 = (v0 >> 8) & 0xFF;
569
+      register uint8_t r12 = (v0 >> 16) & 0xFF;
570
+      register uint8_t r5 = v1 & 0xFF;
571
+      register uint8_t r6 = (v1 >> 8) & 0xFF;
572
+      register uint8_t r7 = (v1 >> 16) & 0xFF;
573
+      register uint8_t r4,r8,r9,r10,r11;
574
+
575
+      __asm__ __volatile__(
576
+        /* Calculate the Bézier coefficients */
577
+        /*  %10:%1:%0 = v0*/
578
+        /*  %5:%4:%3 = v1*/
579
+        /*  %7:%6:%10 = temporary*/
580
+        /*  %9 = val (must be high register!)*/
581
+        /*  %10 (must be high register!)*/
582
+
583
+        /* Store initial velocity*/
584
+        " sts bezier_F, %0" "\n\t"
585
+        " sts bezier_F+1, %1" "\n\t"
586
+        " sts bezier_F+2, %10" "\n\t"    /* bezier_F = %10:%1:%0 = v0 */
587
+
588
+        /* Get delta speed */
589
+        " ldi %2,-1" "\n\t"              /* %2 = 0xff, means A_negative = true */
590
+        " clr %8" "\n\t"                 /* %8 = 0 */
591
+        " sub %0,%3" "\n\t"
592
+        " sbc %1,%4" "\n\t"
593
+        " sbc %10,%5" "\n\t"             /*  v0 -= v1, C=1 if result is negative */
594
+        " brcc 1f" "\n\t"                /* branch if result is positive (C=0), that means v0 >= v1 */
595
+
596
+        /*  Result was negative, get the absolute value*/
597
+        " com %10" "\n\t"
598
+        " com %1" "\n\t"
599
+        " neg %0" "\n\t"
600
+        " sbc %1,%2" "\n\t"
601
+        " sbc %10,%2" "\n\t"             /* %10:%1:%0 +1  -> %10:%1:%0 = -(v0 - v1) = (v1 - v0) */
602
+        " clr %2" "\n\t"                 /* %2 = 0, means A_negative = false */
603
+
604
+        /*  Store negative flag*/
605
+        "1:" "\n\t"
606
+        " sts A_negative, %2" "\n\t"     /* Store negative flag */
607
+
608
+        /*  Compute coefficients A,B and C   [20 cycles worst case]*/
609
+        " ldi %9,6" "\n\t"               /* %9 = 6 */
610
+        " mul %0,%9" "\n\t"              /* r1:r0 = 6*LO(v0-v1) */
611
+        " sts bezier_A, r0" "\n\t"
612
+        " mov %6,r1" "\n\t"
613
+        " clr %7" "\n\t"                 /* %7:%6:r0 = 6*LO(v0-v1) */
614
+        " mul %1,%9" "\n\t"              /* r1:r0 = 6*MI(v0-v1) */
615
+        " add %6,r0" "\n\t"
616
+        " adc %7,r1" "\n\t"              /* %7:%6:?? += 6*MI(v0-v1) << 8 */
617
+        " mul %10,%9" "\n\t"             /* r1:r0 = 6*HI(v0-v1) */
618
+        " add %7,r0" "\n\t"              /* %7:%6:?? += 6*HI(v0-v1) << 16 */
619
+        " sts bezier_A+1, %6" "\n\t"
620
+        " sts bezier_A+2, %7" "\n\t"     /* bezier_A = %7:%6:?? = 6*(v0-v1) [35 cycles worst] */
621
+
622
+        " ldi %9,15" "\n\t"              /* %9 = 15 */
623
+        " mul %0,%9" "\n\t"              /* r1:r0 = 5*LO(v0-v1) */
624
+        " sts bezier_B, r0" "\n\t"
625
+        " mov %6,r1" "\n\t"
626
+        " clr %7" "\n\t"                 /* %7:%6:?? = 5*LO(v0-v1) */
627
+        " mul %1,%9" "\n\t"              /* r1:r0 = 5*MI(v0-v1) */
628
+        " add %6,r0" "\n\t"
629
+        " adc %7,r1" "\n\t"              /* %7:%6:?? += 5*MI(v0-v1) << 8 */
630
+        " mul %10,%9" "\n\t"             /* r1:r0 = 5*HI(v0-v1) */
631
+        " add %7,r0" "\n\t"              /* %7:%6:?? += 5*HI(v0-v1) << 16 */
632
+        " sts bezier_B+1, %6" "\n\t"
633
+        " sts bezier_B+2, %7" "\n\t"     /* bezier_B = %7:%6:?? = 5*(v0-v1) [50 cycles worst] */
634
+
635
+        " ldi %9,10" "\n\t"              /* %9 = 10 */
636
+        " mul %0,%9" "\n\t"              /* r1:r0 = 10*LO(v0-v1) */
637
+        " sts bezier_C, r0" "\n\t"
638
+        " mov %6,r1" "\n\t"
639
+        " clr %7" "\n\t"                 /* %7:%6:?? = 10*LO(v0-v1) */
640
+        " mul %1,%9" "\n\t"              /* r1:r0 = 10*MI(v0-v1) */
641
+        " add %6,r0" "\n\t"
642
+        " adc %7,r1" "\n\t"              /* %7:%6:?? += 10*MI(v0-v1) << 8 */
643
+        " mul %10,%9" "\n\t"             /* r1:r0 = 10*HI(v0-v1) */
644
+        " add %7,r0" "\n\t"              /* %7:%6:?? += 10*HI(v0-v1) << 16 */
645
+        " sts bezier_C+1, %6" "\n\t"
646
+        " sts bezier_C+2, %7"            /* bezier_C = %7:%6:?? = 10*(v0-v1) [65 cycles worst] */
647
+        : "+r" (r2),
648
+          "+d" (r3),
649
+          "=r" (r4),
650
+          "+r" (r5),
651
+          "+r" (r6),
652
+          "+r" (r7),
653
+          "=r" (r8),
654
+          "=r" (r9),
655
+          "=r" (r10),
656
+          "=d" (r11),
657
+          "+r" (r12)
488 658
         :
489
-        : "cc"
659
+        : "r0", "r1", "cc", "memory"
490 660
       );
491
-      return alo;
661
+    }
492 662
 
493
-    #else
663
+    FORCE_INLINE int32_t Stepper::_eval_bezier_curve(const uint32_t curr_step) {
664
+
665
+      // If dealing with the first step, save expensive computing and return the initial speed
666
+      if (!curr_step)
667
+        return bezier_F;
668
+
669
+      register uint8_t r0 = 0; /* Zero register */
670
+      register uint8_t r2 = (curr_step) & 0xFF;
671
+      register uint8_t r3 = (curr_step >> 8) & 0xFF;
672
+      register uint8_t r4 = (curr_step >> 16) & 0xFF;
673
+      register uint8_t r1,r5,r6,r7,r8,r9,r10,r11; /* Temporary registers */
674
+
675
+      __asm__ __volatile(
676
+        /* umul24x24to16hi(t, bezier_AV, curr_step);  t: Range 0 - 1^16 = 16 bits*/
677
+        " lds %9,bezier_AV" "\n\t"       /* %9 = LO(AV)*/
678
+        " mul %9,%2" "\n\t"              /* r1:r0 = LO(bezier_AV)*LO(curr_step)*/
679
+        " mov %7,r1" "\n\t"              /* %7 = LO(bezier_AV)*LO(curr_step) >> 8*/
680
+        " clr %8" "\n\t"                 /* %8:%7  = LO(bezier_AV)*LO(curr_step) >> 8*/
681
+        " lds %10,bezier_AV+1" "\n\t"    /* %10 = MI(AV)*/
682
+        " mul %10,%2" "\n\t"             /* r1:r0  = MI(bezier_AV)*LO(curr_step)*/
683
+        " add %7,r0" "\n\t"
684
+        " adc %8,r1" "\n\t"              /* %8:%7 += MI(bezier_AV)*LO(curr_step)*/
685
+        " lds r1,bezier_AV+2" "\n\t"     /* r11 = HI(AV)*/
686
+        " mul r1,%2" "\n\t"              /* r1:r0  = HI(bezier_AV)*LO(curr_step)*/
687
+        " add %8,r0" "\n\t"              /* %8:%7 += HI(bezier_AV)*LO(curr_step) << 8*/
688
+        " mul %9,%3" "\n\t"              /* r1:r0 =  LO(bezier_AV)*MI(curr_step)*/
689
+        " add %7,r0" "\n\t"
690
+        " adc %8,r1" "\n\t"              /* %8:%7 += LO(bezier_AV)*MI(curr_step)*/
691
+        " mul %10,%3" "\n\t"             /* r1:r0 =  MI(bezier_AV)*MI(curr_step)*/
692
+        " add %8,r0" "\n\t"              /* %8:%7 += LO(bezier_AV)*MI(curr_step) << 8*/
693
+        " mul %9,%4" "\n\t"              /* r1:r0 =  LO(bezier_AV)*HI(curr_step)*/
694
+        " add %8,r0" "\n\t"              /* %8:%7 += LO(bezier_AV)*HI(curr_step) << 8*/
695
+        /* %8:%7 = t*/
696
+
697
+        /* uint16_t f = t;*/
698
+        " mov %5,%7" "\n\t"              /* %6:%5 = f*/
699
+        " mov %6,%8" "\n\t"
700
+        /* %6:%5 = f*/
701
+
702
+        /* umul16x16to16hi(f, f, t); / Range 16 bits (unsigned) [17] */
703
+        " mul %5,%7" "\n\t"              /* r1:r0 = LO(f) * LO(t)*/
704
+        " mov %9,r1" "\n\t"              /* store MIL(LO(f) * LO(t)) in %9, we need it for rounding*/
705
+        " clr %10" "\n\t"                /* %10 = 0*/
706
+        " clr %11" "\n\t"                /* %11 = 0*/
707
+        " mul %5,%8" "\n\t"              /* r1:r0 = LO(f) * HI(t)*/
708
+        " add %9,r0" "\n\t"              /* %9 += LO(LO(f) * HI(t))*/
709
+        " adc %10,r1" "\n\t"             /* %10 = HI(LO(f) * HI(t))*/
710
+        " adc %11,%0" "\n\t"             /* %11 += carry*/
711
+        " mul %6,%7" "\n\t"              /* r1:r0 = HI(f) * LO(t)*/
712
+        " add %9,r0" "\n\t"              /* %9 += LO(HI(f) * LO(t))*/
713
+        " adc %10,r1" "\n\t"             /* %10 += HI(HI(f) * LO(t)) */
714
+        " adc %11,%0" "\n\t"             /* %11 += carry*/
715
+        " mul %6,%8" "\n\t"              /* r1:r0 = HI(f) * HI(t)*/
716
+        " add %10,r0" "\n\t"             /* %10 += LO(HI(f) * HI(t))*/
717
+        " adc %11,r1" "\n\t"             /* %11 += HI(HI(f) * HI(t))*/
718
+        " mov %5,%10" "\n\t"             /* %6:%5 = */
719
+        " mov %6,%11" "\n\t"             /* f = %10:%11*/
720
+
721
+        /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^3  (unsigned) [17]*/
722
+        " mul %5,%7" "\n\t"              /* r1:r0 = LO(f) * LO(t)*/
723
+        " mov %1,r1" "\n\t"              /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
724
+        " clr %10" "\n\t"                /* %10 = 0*/
725
+        " clr %11" "\n\t"                /* %11 = 0*/
726
+        " mul %5,%8" "\n\t"              /* r1:r0 = LO(f) * HI(t)*/
727
+        " add %1,r0" "\n\t"              /* %1 += LO(LO(f) * HI(t))*/
728
+        " adc %10,r1" "\n\t"             /* %10 = HI(LO(f) * HI(t))*/
729
+        " adc %11,%0" "\n\t"             /* %11 += carry*/
730
+        " mul %6,%7" "\n\t"              /* r1:r0 = HI(f) * LO(t)*/
731
+        " add %1,r0" "\n\t"              /* %1 += LO(HI(f) * LO(t))*/
732
+        " adc %10,r1" "\n\t"             /* %10 += HI(HI(f) * LO(t))*/
733
+        " adc %11,%0" "\n\t"             /* %11 += carry*/
734
+        " mul %6,%8" "\n\t"              /* r1:r0 = HI(f) * HI(t)*/
735
+        " add %10,r0" "\n\t"             /* %10 += LO(HI(f) * HI(t))*/
736
+        " adc %11,r1" "\n\t"             /* %11 += HI(HI(f) * HI(t))*/
737
+        " mov %5,%10" "\n\t"             /* %6:%5 =*/
738
+        " mov %6,%11" "\n\t"             /* f = %10:%11*/
739
+        /* [15 +17*2] = [49]*/
740
+
741
+        /* %4:%3:%2 will be acc from now on*/
742
+
743
+        /* uint24_t acc = bezier_F; / Range 20 bits (unsigned)*/
744
+        " clr %9" "\n\t"                 /* "decimal place we get for free"*/
745
+        " lds %2,bezier_F" "\n\t"
746
+        " lds %3,bezier_F+1" "\n\t"
747
+        " lds %4,bezier_F+2" "\n\t"      /* %4:%3:%2 = acc*/
748
+
749
+        /* if (A_negative) {*/
750
+        " lds r0,A_negative" "\n\t"
751
+        " or r0,%0" "\n\t"               /* Is flag signalling negative? */
752
+        " brne 3f" "\n\t"                /* If yes, Skip next instruction if A was negative*/
753
+        " rjmp 1f" "\n\t"                /* Otherwise, jump */
754
+
755
+        /* uint24_t v; */
756
+        /* umul16x24to24hi(v, f, bezier_C); / Range 21bits [29] */
757
+        /* acc -= v; */
758
+        "3:" "\n\t"
759
+        " lds %10, bezier_C" "\n\t"      /* %10 = LO(bezier_C)*/
760
+        " mul %10,%5" "\n\t"             /* r1:r0 = LO(bezier_C) * LO(f)*/
761
+        " sub %9,r1" "\n\t"
762
+        " sbc %2,%0" "\n\t"
763
+        " sbc %3,%0" "\n\t"
764
+        " sbc %4,%0" "\n\t"              /* %4:%3:%2:%9 -= HI(LO(bezier_C) * LO(f))*/
765
+        " lds %11, bezier_C+1" "\n\t"    /* %11 = MI(bezier_C)*/
766
+        " mul %11,%5" "\n\t"             /* r1:r0 = MI(bezier_C) * LO(f)*/
767
+        " sub %9,r0" "\n\t"
768
+        " sbc %2,r1" "\n\t"
769
+        " sbc %3,%0" "\n\t"
770
+        " sbc %4,%0" "\n\t"              /* %4:%3:%2:%9 -= MI(bezier_C) * LO(f)*/
771
+        " lds %1, bezier_C+2" "\n\t"     /* %1 = HI(bezier_C)*/
772
+        " mul %1,%5" "\n\t"              /* r1:r0 = MI(bezier_C) * LO(f)*/
773
+        " sub %2,r0" "\n\t"
774
+        " sbc %3,r1" "\n\t"
775
+        " sbc %4,%0" "\n\t"              /* %4:%3:%2:%9 -= HI(bezier_C) * LO(f) << 8*/
776
+        " mul %10,%6" "\n\t"             /* r1:r0 = LO(bezier_C) * MI(f)*/
777
+        " sub %9,r0" "\n\t"
778
+        " sbc %2,r1" "\n\t"
779
+        " sbc %3,%0" "\n\t"
780
+        " sbc %4,%0" "\n\t"              /* %4:%3:%2:%9 -= LO(bezier_C) * MI(f)*/
781
+        " mul %11,%6" "\n\t"             /* r1:r0 = MI(bezier_C) * MI(f)*/
782
+        " sub %2,r0" "\n\t"
783
+        " sbc %3,r1" "\n\t"
784
+        " sbc %4,%0" "\n\t"              /* %4:%3:%2:%9 -= MI(bezier_C) * MI(f) << 8*/
785
+        " mul %1,%6" "\n\t"              /* r1:r0 = HI(bezier_C) * LO(f)*/
786
+        " sub %3,r0" "\n\t"
787
+        " sbc %4,r1" "\n\t"              /* %4:%3:%2:%9 -= HI(bezier_C) * LO(f) << 16*/
788
+
789
+        /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^3  (unsigned) [17]*/
790
+        " mul %5,%7" "\n\t"              /* r1:r0 = LO(f) * LO(t)*/
791
+        " mov %1,r1" "\n\t"              /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
792
+        " clr %10" "\n\t"                /* %10 = 0*/
793
+        " clr %11" "\n\t"                /* %11 = 0*/
794
+        " mul %5,%8" "\n\t"              /* r1:r0 = LO(f) * HI(t)*/
795
+        " add %1,r0" "\n\t"              /* %1 += LO(LO(f) * HI(t))*/
796
+        " adc %10,r1" "\n\t"             /* %10 = HI(LO(f) * HI(t))*/
797
+        " adc %11,%0" "\n\t"             /* %11 += carry*/
798
+        " mul %6,%7" "\n\t"              /* r1:r0 = HI(f) * LO(t)*/
799
+        " add %1,r0" "\n\t"              /* %1 += LO(HI(f) * LO(t))*/
800
+        " adc %10,r1" "\n\t"             /* %10 += HI(HI(f) * LO(t))*/
801
+        " adc %11,%0" "\n\t"             /* %11 += carry*/
802
+        " mul %6,%8" "\n\t"              /* r1:r0 = HI(f) * HI(t)*/
803
+        " add %10,r0" "\n\t"             /* %10 += LO(HI(f) * HI(t))*/
804
+        " adc %11,r1" "\n\t"             /* %11 += HI(HI(f) * HI(t))*/
805
+        " mov %5,%10" "\n\t"             /* %6:%5 =*/
806
+        " mov %6,%11" "\n\t"             /* f = %10:%11*/
807
+
808
+        /* umul16x24to24hi(v, f, bezier_B); / Range 22bits [29]*/
809
+        /* acc += v; */
810
+        " lds %10, bezier_B" "\n\t"      /* %10 = LO(bezier_B)*/
811
+        " mul %10,%5" "\n\t"             /* r1:r0 = LO(bezier_B) * LO(f)*/
812
+        " add %9,r1" "\n\t"
813
+        " adc %2,%0" "\n\t"
814
+        " adc %3,%0" "\n\t"
815
+        " adc %4,%0" "\n\t"              /* %4:%3:%2:%9 += HI(LO(bezier_B) * LO(f))*/
816
+        " lds %11, bezier_B+1" "\n\t"    /* %11 = MI(bezier_B)*/
817
+        " mul %11,%5" "\n\t"             /* r1:r0 = MI(bezier_B) * LO(f)*/
818
+        " add %9,r0" "\n\t"
819
+        " adc %2,r1" "\n\t"
820
+        " adc %3,%0" "\n\t"
821
+        " adc %4,%0" "\n\t"              /* %4:%3:%2:%9 += MI(bezier_B) * LO(f)*/
822
+        " lds %1, bezier_B+2" "\n\t"     /* %1 = HI(bezier_B)*/
823
+        " mul %1,%5" "\n\t"              /* r1:r0 = MI(bezier_B) * LO(f)*/
824
+        " add %2,r0" "\n\t"
825
+        " adc %3,r1" "\n\t"
826
+        " adc %4,%0" "\n\t"              /* %4:%3:%2:%9 += HI(bezier_B) * LO(f) << 8*/
827
+        " mul %10,%6" "\n\t"             /* r1:r0 = LO(bezier_B) * MI(f)*/
828
+        " add %9,r0" "\n\t"
829
+        " adc %2,r1" "\n\t"
830
+        " adc %3,%0" "\n\t"
831
+        " adc %4,%0" "\n\t"              /* %4:%3:%2:%9 += LO(bezier_B) * MI(f)*/
832
+        " mul %11,%6" "\n\t"             /* r1:r0 = MI(bezier_B) * MI(f)*/
833
+        " add %2,r0" "\n\t"
834
+        " adc %3,r1" "\n\t"
835
+        " adc %4,%0" "\n\t"              /* %4:%3:%2:%9 += MI(bezier_B) * MI(f) << 8*/
836
+        " mul %1,%6" "\n\t"              /* r1:r0 = HI(bezier_B) * LO(f)*/
837
+        " add %3,r0" "\n\t"
838
+        " adc %4,r1" "\n\t"              /* %4:%3:%2:%9 += HI(bezier_B) * LO(f) << 16*/
839
+
840
+        /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^5  (unsigned) [17]*/
841
+        " mul %5,%7" "\n\t"              /* r1:r0 = LO(f) * LO(t)*/
842
+        " mov %1,r1" "\n\t"              /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
843
+        " clr %10" "\n\t"                /* %10 = 0*/
844
+        " clr %11" "\n\t"                /* %11 = 0*/
845
+        " mul %5,%8" "\n\t"              /* r1:r0 = LO(f) * HI(t)*/
846
+        " add %1,r0" "\n\t"              /* %1 += LO(LO(f) * HI(t))*/
847
+        " adc %10,r1" "\n\t"             /* %10 = HI(LO(f) * HI(t))*/
848
+        " adc %11,%0" "\n\t"             /* %11 += carry*/
849
+        " mul %6,%7" "\n\t"              /* r1:r0 = HI(f) * LO(t)*/
850
+        " add %1,r0" "\n\t"              /* %1 += LO(HI(f) * LO(t))*/
851
+        " adc %10,r1" "\n\t"             /* %10 += HI(HI(f) * LO(t))*/
852
+        " adc %11,%0" "\n\t"             /* %11 += carry*/
853
+        " mul %6,%8" "\n\t"              /* r1:r0 = HI(f) * HI(t)*/
854
+        " add %10,r0" "\n\t"             /* %10 += LO(HI(f) * HI(t))*/
855
+        " adc %11,r1" "\n\t"             /* %11 += HI(HI(f) * HI(t))*/
856
+        " mov %5,%10" "\n\t"             /* %6:%5 =*/
857
+        " mov %6,%11" "\n\t"             /* f = %10:%11*/
858
+
859
+        /* umul16x24to24hi(v, f, bezier_A); / Range 21bits [29]*/
860
+        /* acc -= v; */
861
+        " lds %10, bezier_A" "\n\t"      /* %10 = LO(bezier_A)*/
862
+        " mul %10,%5" "\n\t"             /* r1:r0 = LO(bezier_A) * LO(f)*/
863
+        " sub %9,r1" "\n\t"
864
+        " sbc %2,%0" "\n\t"
865
+        " sbc %3,%0" "\n\t"
866
+        " sbc %4,%0" "\n\t"              /* %4:%3:%2:%9 -= HI(LO(bezier_A) * LO(f))*/
867
+        " lds %11, bezier_A+1" "\n\t"    /* %11 = MI(bezier_A)*/
868
+        " mul %11,%5" "\n\t"             /* r1:r0 = MI(bezier_A) * LO(f)*/
869
+        " sub %9,r0" "\n\t"
870
+        " sbc %2,r1" "\n\t"
871
+        " sbc %3,%0" "\n\t"
872
+        " sbc %4,%0" "\n\t"              /* %4:%3:%2:%9 -= MI(bezier_A) * LO(f)*/
873
+        " lds %1, bezier_A+2" "\n\t"     /* %1 = HI(bezier_A)*/
874
+        " mul %1,%5" "\n\t"              /* r1:r0 = MI(bezier_A) * LO(f)*/
875
+        " sub %2,r0" "\n\t"
876
+        " sbc %3,r1" "\n\t"
877
+        " sbc %4,%0" "\n\t"              /* %4:%3:%2:%9 -= HI(bezier_A) * LO(f) << 8*/
878
+        " mul %10,%6" "\n\t"             /* r1:r0 = LO(bezier_A) * MI(f)*/
879
+        " sub %9,r0" "\n\t"
880
+        " sbc %2,r1" "\n\t"
881
+        " sbc %3,%0" "\n\t"
882
+        " sbc %4,%0" "\n\t"              /* %4:%3:%2:%9 -= LO(bezier_A) * MI(f)*/
883
+        " mul %11,%6" "\n\t"             /* r1:r0 = MI(bezier_A) * MI(f)*/
884
+        " sub %2,r0" "\n\t"
885
+        " sbc %3,r1" "\n\t"
886
+        " sbc %4,%0" "\n\t"              /* %4:%3:%2:%9 -= MI(bezier_A) * MI(f) << 8*/
887
+        " mul %1,%6" "\n\t"              /* r1:r0 = HI(bezier_A) * LO(f)*/
888
+        " sub %3,r0" "\n\t"
889
+        " sbc %4,r1" "\n\t"              /* %4:%3:%2:%9 -= HI(bezier_A) * LO(f) << 16*/
890
+        " jmp 2f" "\n\t"                 /* Done!*/
891
+
892
+        "1:" "\n\t"
893
+
894
+        /* uint24_t v; */
895
+        /* umul16x24to24hi(v, f, bezier_C); / Range 21bits [29]*/
896
+        /* acc += v; */
897
+        " lds %10, bezier_C" "\n\t"      /* %10 = LO(bezier_C)*/
898
+        " mul %10,%5" "\n\t"             /* r1:r0 = LO(bezier_C) * LO(f)*/
899
+        " add %9,r1" "\n\t"
900
+        " adc %2,%0" "\n\t"
901
+        " adc %3,%0" "\n\t"
902
+        " adc %4,%0" "\n\t"              /* %4:%3:%2:%9 += HI(LO(bezier_C) * LO(f))*/
903
+        " lds %11, bezier_C+1" "\n\t"    /* %11 = MI(bezier_C)*/
904
+        " mul %11,%5" "\n\t"             /* r1:r0 = MI(bezier_C) * LO(f)*/
905
+        " add %9,r0" "\n\t"
906
+        " adc %2,r1" "\n\t"
907
+        " adc %3,%0" "\n\t"
908
+        " adc %4,%0" "\n\t"              /* %4:%3:%2:%9 += MI(bezier_C) * LO(f)*/
909
+        " lds %1, bezier_C+2" "\n\t"     /* %1 = HI(bezier_C)*/
910
+        " mul %1,%5" "\n\t"              /* r1:r0 = MI(bezier_C) * LO(f)*/
911
+        " add %2,r0" "\n\t"
912
+        " adc %3,r1" "\n\t"
913
+        " adc %4,%0" "\n\t"              /* %4:%3:%2:%9 += HI(bezier_C) * LO(f) << 8*/
914
+        " mul %10,%6" "\n\t"             /* r1:r0 = LO(bezier_C) * MI(f)*/
915
+        " add %9,r0" "\n\t"
916
+        " adc %2,r1" "\n\t"
917
+        " adc %3,%0" "\n\t"
918
+        " adc %4,%0" "\n\t"              /* %4:%3:%2:%9 += LO(bezier_C) * MI(f)*/
919
+        " mul %11,%6" "\n\t"             /* r1:r0 = MI(bezier_C) * MI(f)*/
920
+        " add %2,r0" "\n\t"
921
+        " adc %3,r1" "\n\t"
922
+        " adc %4,%0" "\n\t"              /* %4:%3:%2:%9 += MI(bezier_C) * MI(f) << 8*/
923
+        " mul %1,%6" "\n\t"              /* r1:r0 = HI(bezier_C) * LO(f)*/
924
+        " add %3,r0" "\n\t"
925
+        " adc %4,r1" "\n\t"              /* %4:%3:%2:%9 += HI(bezier_C) * LO(f) << 16*/
926
+
927
+        /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^3  (unsigned) [17]*/
928
+        " mul %5,%7" "\n\t"              /* r1:r0 = LO(f) * LO(t)*/
929
+        " mov %1,r1" "\n\t"              /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
930
+        " clr %10" "\n\t"                /* %10 = 0*/
931
+        " clr %11" "\n\t"                /* %11 = 0*/
932
+        " mul %5,%8" "\n\t"              /* r1:r0 = LO(f) * HI(t)*/
933
+        " add %1,r0" "\n\t"              /* %1 += LO(LO(f) * HI(t))*/
934
+        " adc %10,r1" "\n\t"             /* %10 = HI(LO(f) * HI(t))*/
935
+        " adc %11,%0" "\n\t"             /* %11 += carry*/
936
+        " mul %6,%7" "\n\t"              /* r1:r0 = HI(f) * LO(t)*/
937
+        " add %1,r0" "\n\t"              /* %1 += LO(HI(f) * LO(t))*/
938
+        " adc %10,r1" "\n\t"             /* %10 += HI(HI(f) * LO(t))*/
939
+        " adc %11,%0" "\n\t"             /* %11 += carry*/
940
+        " mul %6,%8" "\n\t"              /* r1:r0 = HI(f) * HI(t)*/
941
+        " add %10,r0" "\n\t"             /* %10 += LO(HI(f) * HI(t))*/
942
+        " adc %11,r1" "\n\t"             /* %11 += HI(HI(f) * HI(t))*/
943
+        " mov %5,%10" "\n\t"             /* %6:%5 =*/
944
+        " mov %6,%11" "\n\t"             /* f = %10:%11*/
945
+
946
+        /* umul16x24to24hi(v, f, bezier_B); / Range 22bits [29]*/
947
+        /* acc -= v;*/
948
+        " lds %10, bezier_B" "\n\t"      /* %10 = LO(bezier_B)*/
949
+        " mul %10,%5" "\n\t"             /* r1:r0 = LO(bezier_B) * LO(f)*/
950
+        " sub %9,r1" "\n\t"
951
+        " sbc %2,%0" "\n\t"
952
+        " sbc %3,%0" "\n\t"
953
+        " sbc %4,%0" "\n\t"              /* %4:%3:%2:%9 -= HI(LO(bezier_B) * LO(f))*/
954
+        " lds %11, bezier_B+1" "\n\t"    /* %11 = MI(bezier_B)*/
955
+        " mul %11,%5" "\n\t"             /* r1:r0 = MI(bezier_B) * LO(f)*/
956
+        " sub %9,r0" "\n\t"
957
+        " sbc %2,r1" "\n\t"
958
+        " sbc %3,%0" "\n\t"
959
+        " sbc %4,%0" "\n\t"              /* %4:%3:%2:%9 -= MI(bezier_B) * LO(f)*/
960
+        " lds %1, bezier_B+2" "\n\t"     /* %1 = HI(bezier_B)*/
961
+        " mul %1,%5" "\n\t"              /* r1:r0 = MI(bezier_B) * LO(f)*/
962
+        " sub %2,r0" "\n\t"
963
+        " sbc %3,r1" "\n\t"
964
+        " sbc %4,%0" "\n\t"              /* %4:%3:%2:%9 -= HI(bezier_B) * LO(f) << 8*/
965
+        " mul %10,%6" "\n\t"             /* r1:r0 = LO(bezier_B) * MI(f)*/
966
+        " sub %9,r0" "\n\t"
967
+        " sbc %2,r1" "\n\t"
968
+        " sbc %3,%0" "\n\t"
969
+        " sbc %4,%0" "\n\t"              /* %4:%3:%2:%9 -= LO(bezier_B) * MI(f)*/
970
+        " mul %11,%6" "\n\t"             /* r1:r0 = MI(bezier_B) * MI(f)*/
971
+        " sub %2,r0" "\n\t"
972
+        " sbc %3,r1" "\n\t"
973
+        " sbc %4,%0" "\n\t"              /* %4:%3:%2:%9 -= MI(bezier_B) * MI(f) << 8*/
974
+        " mul %1,%6" "\n\t"              /* r1:r0 = HI(bezier_B) * LO(f)*/
975
+        " sub %3,r0" "\n\t"
976
+        " sbc %4,r1" "\n\t"              /* %4:%3:%2:%9 -= HI(bezier_B) * LO(f) << 16*/
977
+
978
+        /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^5  (unsigned) [17]*/
979
+        " mul %5,%7" "\n\t"              /* r1:r0 = LO(f) * LO(t)*/
980
+        " mov %1,r1" "\n\t"              /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
981
+        " clr %10" "\n\t"                /* %10 = 0*/
982
+        " clr %11" "\n\t"                /* %11 = 0*/
983
+        " mul %5,%8" "\n\t"              /* r1:r0 = LO(f) * HI(t)*/
984
+        " add %1,r0" "\n\t"              /* %1 += LO(LO(f) * HI(t))*/
985
+        " adc %10,r1" "\n\t"             /* %10 = HI(LO(f) * HI(t))*/
986
+        " adc %11,%0" "\n\t"             /* %11 += carry*/
987
+        " mul %6,%7" "\n\t"              /* r1:r0 = HI(f) * LO(t)*/
988
+        " add %1,r0" "\n\t"              /* %1 += LO(HI(f) * LO(t))*/
989
+        " adc %10,r1" "\n\t"             /* %10 += HI(HI(f) * LO(t))*/
990
+        " adc %11,%0" "\n\t"             /* %11 += carry*/
991
+        " mul %6,%8" "\n\t"              /* r1:r0 = HI(f) * HI(t)*/
992
+        " add %10,r0" "\n\t"             /* %10 += LO(HI(f) * HI(t))*/
993
+        " adc %11,r1" "\n\t"             /* %11 += HI(HI(f) * HI(t))*/
994
+        " mov %5,%10" "\n\t"             /* %6:%5 =*/
995
+        " mov %6,%11" "\n\t"             /* f = %10:%11*/
996
+
997
+        /* umul16x24to24hi(v, f, bezier_A); / Range 21bits [29]*/
998
+        /* acc += v; */
999
+        " lds %10, bezier_A" "\n\t"      /* %10 = LO(bezier_A)*/
1000
+        " mul %10,%5" "\n\t"             /* r1:r0 = LO(bezier_A) * LO(f)*/
1001
+        " add %9,r1" "\n\t"
1002
+        " adc %2,%0" "\n\t"
1003
+        " adc %3,%0" "\n\t"
1004
+        " adc %4,%0" "\n\t"              /* %4:%3:%2:%9 += HI(LO(bezier_A) * LO(f))*/
1005
+        " lds %11, bezier_A+1" "\n\t"    /* %11 = MI(bezier_A)*/
1006
+        " mul %11,%5" "\n\t"             /* r1:r0 = MI(bezier_A) * LO(f)*/
1007
+        " add %9,r0" "\n\t"
1008
+        " adc %2,r1" "\n\t"
1009
+        " adc %3,%0" "\n\t"
1010
+        " adc %4,%0" "\n\t"              /* %4:%3:%2:%9 += MI(bezier_A) * LO(f)*/
1011
+        " lds %1, bezier_A+2" "\n\t"     /* %1 = HI(bezier_A)*/
1012
+        " mul %1,%5" "\n\t"              /* r1:r0 = MI(bezier_A) * LO(f)*/
1013
+        " add %2,r0" "\n\t"
1014
+        " adc %3,r1" "\n\t"
1015
+        " adc %4,%0" "\n\t"              /* %4:%3:%2:%9 += HI(bezier_A) * LO(f) << 8*/
1016
+        " mul %10,%6" "\n\t"             /* r1:r0 = LO(bezier_A) * MI(f)*/
1017
+        " add %9,r0" "\n\t"
1018
+        " adc %2,r1" "\n\t"
1019
+        " adc %3,%0" "\n\t"
1020
+        " adc %4,%0" "\n\t"              /* %4:%3:%2:%9 += LO(bezier_A) * MI(f)*/
1021
+        " mul %11,%6" "\n\t"             /* r1:r0 = MI(bezier_A) * MI(f)*/
1022
+        " add %2,r0" "\n\t"
1023
+        " adc %3,r1" "\n\t"
1024
+        " adc %4,%0" "\n\t"              /* %4:%3:%2:%9 += MI(bezier_A) * MI(f) << 8*/
1025
+        " mul %1,%6" "\n\t"              /* r1:r0 = HI(bezier_A) * LO(f)*/
1026
+        " add %3,r0" "\n\t"
1027
+        " adc %4,r1" "\n\t"              /* %4:%3:%2:%9 += HI(bezier_A) * LO(f) << 16*/
1028
+        "2:" "\n\t"
1029
+        " clr __zero_reg__"              /* C runtime expects r1 = __zero_reg__ = 0 */
1030
+        : "+r"(r0),
1031
+          "+r"(r1),
1032
+          "+r"(r2),
1033
+          "+r"(r3),
1034
+          "+r"(r4),
1035
+          "+r"(r5),
1036
+          "+r"(r6),
1037
+          "+r"(r7),
1038
+          "+r"(r8),
1039
+          "+r"(r9),
1040
+          "+r"(r10),
1041
+          "+r"(r11)
1042
+        :
1043
+        :"cc","r0","r1"
1044
+      );
1045
+      return (r2 | (uint16_t(r3) << 8)) | (uint32_t(r4) << 16);
1046
+    }
494 1047
 
495
-      // For non ARM targets, we provide a fallback implementation. Really doubt it
496
-      // will be useful, unless the processor is extremely fast.
497
-
498
-      uint32_t t = bezier_AV * curr_step;               // t: Range 0 - 1^32 = 32 bits
499
-      uint64_t f = t;
500
-      f *= t;                                           // Range 32*2 = 64 bits (unsigned)
501
-      f >>= 32;                                         // Range 32 bits  (unsigned)
502
-      f *= t;                                           // Range 32*2 = 64 bits  (unsigned)
503
-      f >>= 32;                                         // Range 32 bits : f = t^3  (unsigned)
504
-      int64_t acc = (int64_t) bezier_F << 31;           // Range 63 bits (signed)
505
-      acc += ((uint32_t) f >> 1) * (int64_t) bezier_C;  // Range 29bits + 31 = 60bits (plus sign)
506
-      f *= t;                                           // Range 32*2 = 64 bits
507
-      f >>= 32;                                         // Range 32 bits : f = t^3  (unsigned)
508
-      acc += ((uint32_t) f >> 1) * (int64_t) bezier_B;  // Range 29bits + 31 = 60bits (plus sign)
509
-      f *= t;                                           // Range 32*2 = 64 bits
510
-      f >>= 32;                                         // Range 32 bits : f = t^3  (unsigned)
511
-      acc += ((uint32_t) f >> 1) * (int64_t) bezier_A;  // Range 28bits + 31 = 59bits (plus sign)
512
-      acc >>= (31 + 7);                                 // Range 24bits (plus sign)
513
-      return (int32_t) acc;
1048
+  #else
514 1049
 
515
-    #endif
516
-  }
1050
+    // For all the other 32bit CPUs
1051
+    FORCE_INLINE void Stepper::_calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t av) {
1052
+      // Calculate the Bézier coefficients
1053
+      bezier_A =  768 * (v1 - v0);
1054
+      bezier_B = 1920 * (v0 - v1);
1055
+      bezier_C = 1280 * (v1 - v0);
1056
+      bezier_F =  128 * v0;
1057
+      bezier_AV = av;
1058
+    }
1059
+
1060
+    FORCE_INLINE int32_t Stepper::_eval_bezier_curve(const uint32_t curr_step) {
1061
+      #if defined(__ARM__) || defined(__thumb__)
1062
+
1063
+        // For ARM Cortex M3/M4 CPUs, we have the optimized assembler version, that takes 43 cycles to execute
1064
+        register uint32_t flo = 0;
1065
+        register uint32_t fhi = bezier_AV * curr_step;
1066
+        register uint32_t t = fhi;
1067
+        register int32_t alo = bezier_F;
1068
+        register int32_t ahi = 0;
1069
+        register int32_t A = bezier_A;
1070
+        register int32_t B = bezier_B;
1071
+        register int32_t C = bezier_C;
1072
+
1073
+         __asm__ __volatile__(
1074
+          ".syntax unified"                   "\n\t"  // is to prevent CM0,CM1 non-unified syntax
1075
+          " lsrs  %[ahi],%[alo],#1"           "\n\t"  // a  = F << 31      1 cycles
1076
+          " lsls  %[alo],%[alo],#31"          "\n\t"  //                   1 cycles
1077
+          " umull %[flo],%[fhi],%[fhi],%[t]"  "\n\t"  // f *= t            5 cycles [fhi:flo=64bits]
1078
+          " umull %[flo],%[fhi],%[fhi],%[t]"  "\n\t"  // f>>=32; f*=t      5 cycles [fhi:flo=64bits]
1079
+          " lsrs  %[flo],%[fhi],#1"           "\n\t"  //                   1 cycles [31bits]
1080
+          " smlal %[alo],%[ahi],%[flo],%[C]"  "\n\t"  // a+=(f>>33)*C;     5 cycles
1081
+          " umull %[flo],%[fhi],%[fhi],%[t]"  "\n\t"  // f>>=32; f*=t      5 cycles [fhi:flo=64bits]
1082
+          " lsrs  %[flo],%[fhi],#1"           "\n\t"  //                   1 cycles [31bits]
1083
+          " smlal %[alo],%[ahi],%[flo],%[B]"  "\n\t"  // a+=(f>>33)*B;     5 cycles
1084
+          " umull %[flo],%[fhi],%[fhi],%[t]"  "\n\t"  // f>>=32; f*=t      5 cycles [fhi:flo=64bits]
1085
+          " lsrs  %[flo],%[fhi],#1"           "\n\t"  // f>>=33;           1 cycles [31bits]
1086
+          " smlal %[alo],%[ahi],%[flo],%[A]"  "\n\t"  // a+=(f>>33)*A;     5 cycles
1087
+          " lsrs  %[alo],%[ahi],#6"           "\n\t"  // a>>=38            1 cycles
1088
+          : [alo]"+r"( alo ) ,
1089
+            [flo]"+r"( flo ) ,
1090
+            [fhi]"+r"( fhi ) ,
1091
+            [ahi]"+r"( ahi ) ,
1092
+            [A]"+r"( A ) ,  // <== Note: Even if A, B, C, and t registers are INPUT ONLY
1093
+            [B]"+r"( B ) ,  //  GCC does bad optimizations on the code if we list them as
1094
+            [C]"+r"( C ) ,  //  such, breaking this function. So, to avoid that problem,
1095
+            [t]"+r"( t )    //  we list all registers as input-outputs.
1096
+          :
1097
+          : "cc"
1098
+        );
1099
+        return alo;
1100
+
1101
+      #else
517 1102
 
1103
+        // For non ARM targets, we provide a fallback implementation. Really doubt it
1104
+        // will be useful, unless the processor is fast and 32bit
1105
+
1106
+        uint32_t t = bezier_AV * curr_step;               // t: Range 0 - 1^32 = 32 bits
1107
+        uint64_t f = t;
1108
+        f *= t;                                           // Range 32*2 = 64 bits (unsigned)
1109
+        f >>= 32;                                         // Range 32 bits  (unsigned)
1110
+        f *= t;                                           // Range 32*2 = 64 bits  (unsigned)
1111
+        f >>= 32;                                         // Range 32 bits : f = t^3  (unsigned)
1112
+        int64_t acc = (int64_t) bezier_F << 31;           // Range 63 bits (signed)
1113
+        acc += ((uint32_t) f >> 1) * (int64_t) bezier_C;  // Range 29bits + 31 = 60bits (plus sign)
1114
+        f *= t;                                           // Range 32*2 = 64 bits
1115
+        f >>= 32;                                         // Range 32 bits : f = t^3  (unsigned)
1116
+        acc += ((uint32_t) f >> 1) * (int64_t) bezier_B;  // Range 29bits + 31 = 60bits (plus sign)
1117
+        f *= t;                                           // Range 32*2 = 64 bits
1118
+        f >>= 32;                                         // Range 32 bits : f = t^3  (unsigned)
1119
+        acc += ((uint32_t) f >> 1) * (int64_t) bezier_A;  // Range 28bits + 31 = 59bits (plus sign)
1120
+        acc >>= (31 + 7);                                 // Range 24bits (plus sign)
1121
+        return (int32_t) acc;
1122
+
1123
+      #endif
1124
+    }
1125
+  #endif
518 1126
 #endif // BEZIER_JERK_CONTROL
519 1127
 
520 1128
 /**
@@ -660,7 +1268,7 @@ void Stepper::isr() {
660 1268
 
661 1269
       #if ENABLED(BEZIER_JERK_CONTROL)
662 1270
         // Initialize the Bézier speed curve
663
-        _calc_bezier_curve_coeffs(current_block->initial_rate, current_block->cruise_rate, current_block->acceleration_time);
1271
+        _calc_bezier_curve_coeffs(current_block->initial_rate, current_block->cruise_rate, current_block->acceleration_time_inverse);
664 1272
 
665 1273
         // We have not started the 2nd half of the trapezoid
666 1274
         bezier_2nd_half = false;
@@ -953,7 +1561,7 @@ void Stepper::isr() {
953 1561
       if (!bezier_2nd_half) {
954 1562
 
955 1563
         // Initialize the Bézier speed curve
956
-        _calc_bezier_curve_coeffs(current_block->cruise_rate, current_block->final_rate, current_block->deceleration_time);
1564
+        _calc_bezier_curve_coeffs(current_block->cruise_rate, current_block->final_rate, current_block->deceleration_time_inverse);
957 1565
         bezier_2nd_half = true;
958 1566
       }
959 1567
 

+ 10
- 7
Marlin/src/module/stepper.h Прегледај датотеку

@@ -98,12 +98,15 @@ class Stepper {
98 98
     static volatile uint32_t step_events_completed; // The number of step events executed in the current block
99 99
 
100 100
     #if ENABLED(BEZIER_JERK_CONTROL)
101
-      static int32_t bezier_A,        // A coefficient in Bézier speed curve
102
-                     bezier_B,        // B coefficient in Bézier speed curve
103
-                     bezier_C,        // C coefficient in Bézier speed curve
104
-                     bezier_F;        // F coefficient in Bézier speed curve
105
-      static uint32_t bezier_AV;      // AV coefficient in Bézier speed curve
106
-      static bool bezier_2nd_half;    // If Bézier curve has been initialized or not
101
+      static int32_t bezier_A,     // A coefficient in Bézier speed curve
102
+                     bezier_B,     // B coefficient in Bézier speed curve
103
+                     bezier_C;     // C coefficient in Bézier speed curve
104
+      static uint32_t bezier_F;    // F coefficient in Bézier speed curve
105
+      static uint32_t bezier_AV;   // AV coefficient in Bézier speed curve
106
+      #ifdef __AVR__
107
+        static bool A_negative;    // If A coefficient was negative
108
+      #endif
109
+      static bool bezier_2nd_half; // If Bézier curve has been initialized or not
107 110
     #endif
108 111
 
109 112
     #if ENABLED(LIN_ADVANCE)
@@ -361,7 +364,7 @@ class Stepper {
361 364
     }
362 365
 
363 366
     #if ENABLED(BEZIER_JERK_CONTROL)
364
-      static void _calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t steps);
367
+      static void _calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t av);
365 368
       static int32_t _eval_bezier_curve(const uint32_t curr_step);
366 369
     #endif
367 370
 

Loading…
Откажи
Сачувај