Browse Source

Improve pulse timing and step reliability (#16128)

Jason Smith 4 years ago
parent
commit
1bad8f1b17

+ 6
- 6
Marlin/Configuration_adv.h View File

1548
 /**
1548
 /**
1549
  * Maximum stepping rate (in Hz) the stepper driver allows
1549
  * Maximum stepping rate (in Hz) the stepper driver allows
1550
  *  If undefined, defaults to 1MHz / (2 * MINIMUM_STEPPER_PULSE)
1550
  *  If undefined, defaults to 1MHz / (2 * MINIMUM_STEPPER_PULSE)
1551
- *  500000 : Maximum for A4988 stepper driver
1552
- *  400000 : Maximum for TMC2xxx stepper drivers
1553
- *  250000 : Maximum for DRV8825 stepper driver
1554
- *  200000 : Maximum for LV8729 stepper driver
1555
- *  150000 : Maximum for TB6600 stepper driver
1556
- *   15000 : Maximum for TB6560 stepper driver
1551
+ *  5000000 : Maximum for TMC2xxx stepper drivers
1552
+ *  1000000 : Maximum for LV8729 stepper driver
1553
+ *  500000  : Maximum for A4988 stepper driver
1554
+ *  250000  : Maximum for DRV8825 stepper driver
1555
+ *  150000  : Maximum for TB6600 stepper driver
1556
+ *   15000  : Maximum for TB6560 stepper driver
1557
  *
1557
  *
1558
  * Override the default value based on the driver type set in Configuration.h.
1558
  * Override the default value based on the driver type set in Configuration.h.
1559
  */
1559
  */

+ 5
- 9
Marlin/src/inc/Conditionals_post.h View File

595
   #elif HAS_DRIVER(A4988) || HAS_DRIVER(A5984)
595
   #elif HAS_DRIVER(A4988) || HAS_DRIVER(A5984)
596
     #define MINIMUM_STEPPER_PULSE 1
596
     #define MINIMUM_STEPPER_PULSE 1
597
   #elif TRINAMICS
597
   #elif TRINAMICS
598
-    #if ENABLED(LIN_ADVANCE) && (HAS_TMC_STANDALONE_E_DRIVER || (HAS_TMC_E_DRIVER && DISABLED(SQUARE_WAVE_STEPPING)))
599
-      #define MINIMUM_STEPPER_PULSE 1
600
-    #else
601
-      #define MINIMUM_STEPPER_PULSE 0
602
-    #endif
598
+    #define MINIMUM_STEPPER_PULSE 0
603
   #elif HAS_DRIVER(LV8729)
599
   #elif HAS_DRIVER(LV8729)
604
     #define MINIMUM_STEPPER_PULSE 0
600
     #define MINIMUM_STEPPER_PULSE 0
605
   #else
601
   #else
612
     #define MAXIMUM_STEPPER_RATE 15000
608
     #define MAXIMUM_STEPPER_RATE 15000
613
   #elif HAS_DRIVER(TB6600)
609
   #elif HAS_DRIVER(TB6600)
614
     #define MAXIMUM_STEPPER_RATE 150000
610
     #define MAXIMUM_STEPPER_RATE 150000
615
-  #elif HAS_DRIVER(LV8729)
616
-    #define MAXIMUM_STEPPER_RATE 200000
617
   #elif HAS_DRIVER(DRV8825)
611
   #elif HAS_DRIVER(DRV8825)
618
     #define MAXIMUM_STEPPER_RATE 250000
612
     #define MAXIMUM_STEPPER_RATE 250000
619
-  #elif TRINAMICS
620
-    #define MAXIMUM_STEPPER_RATE 400000
621
   #elif HAS_DRIVER(A4988)
613
   #elif HAS_DRIVER(A4988)
622
     #define MAXIMUM_STEPPER_RATE 500000
614
     #define MAXIMUM_STEPPER_RATE 500000
615
+  #elif HAS_DRIVER(LV8729)
616
+    #define MAXIMUM_STEPPER_RATE 1000000
617
+  #elif TRINAMICS
618
+    #define MAXIMUM_STEPPER_RATE 5000000
623
   #else
619
   #else
624
     #define MAXIMUM_STEPPER_RATE 250000
620
     #define MAXIMUM_STEPPER_RATE 250000
625
   #endif
621
   #endif

+ 0
- 8
Marlin/src/inc/SanityCheck.h View File

2551
     #error "SHOW_REMAINING_TIME currently requires a Graphical LCD."
2551
     #error "SHOW_REMAINING_TIME currently requires a Graphical LCD."
2552
   #endif
2552
   #endif
2553
 #endif
2553
 #endif
2554
-
2555
-#if ENABLED(LIN_ADVANCE) && MINIMUM_STEPPER_PULSE < 1
2556
-  #if HAS_TMC_STANDALONE_E_DRIVER
2557
-    #error "LIN_ADVANCE with TMC standalone driver on extruder requires MIMIMUM_STEPPER_PULSE >= 1"
2558
-  #elif HAS_TMC_E_DRIVER && DISABLED(SQUARE_WAVE_STEPPING)
2559
-    #error "LIN_ADVANCE with TMC driver on extruder requires SQUARE_WAVE_STEPPING or MINIMUM_STEPPER_PULSE >= 1"
2560
-  #endif
2561
-#endif

+ 90
- 60
Marlin/src/module/stepper.cpp View File

338
 #if DISABLED(MIXING_EXTRUDER)
338
 #if DISABLED(MIXING_EXTRUDER)
339
   #define E_APPLY_STEP(v,Q) E_STEP_WRITE(stepper_extruder, v)
339
   #define E_APPLY_STEP(v,Q) E_STEP_WRITE(stepper_extruder, v)
340
 #endif
340
 #endif
341
+#define TIMER_SETUP_NS (CYCLES_TO_NS(TIMER_READ_ADD_AND_STORE_CYCLES))
342
+
343
+#define PULSE_HIGH_TICK_COUNT hal_timer_t(NS_TO_PULSE_TIMER_TICKS(_MIN_PULSE_HIGH_NS - _MIN(_MIN_PULSE_HIGH_NS, TIMER_SETUP_NS)))
344
+#define PULSE_LOW_TICK_COUNT hal_timer_t(NS_TO_PULSE_TIMER_TICKS(_MIN_PULSE_LOW_NS - _MIN(_MIN_PULSE_LOW_NS, TIMER_SETUP_NS)))
345
+
346
+#define START_TIMED_PULSE(DIR) (end_tick_count = HAL_timer_get_count(PULSE_TIMER_NUM) + PULSE_##DIR##_TICK_COUNT)
347
+#define AWAIT_TIMED_PULSE() while (HAL_timer_get_count(PULSE_TIMER_NUM) < end_tick_count) { }
348
+#define START_HIGH_PULSE()  START_TIMED_PULSE(HIGH)
349
+#define START_LOW_PULSE()   START_TIMED_PULSE(LOW)
350
+#define AWAIT_HIGH_PULSE()  AWAIT_TIMED_PULSE()
351
+#define AWAIT_LOW_PULSE()   AWAIT_TIMED_PULSE()
341
 
352
 
342
 void Stepper::wake_up() {
353
 void Stepper::wake_up() {
343
   // TCNT1 = 0;
354
   // TCNT1 = 0;
1416
   // Just update the value we will get at the end of the loop
1427
   // Just update the value we will get at the end of the loop
1417
   step_events_completed += events_to_do;
1428
   step_events_completed += events_to_do;
1418
 
1429
 
1419
-  // Get the timer count and estimate the end of the pulse
1420
-  hal_timer_t pulse_end = HAL_timer_get_count(PULSE_TIMER_NUM) + hal_timer_t(MIN_PULSE_TICKS);
1421
-
1422
-  const hal_timer_t added_step_ticks = hal_timer_t(ADDED_STEP_TICKS);
1423
-
1424
   // Take multiple steps per interrupt (For high speed moves)
1430
   // Take multiple steps per interrupt (For high speed moves)
1425
-  do {
1431
+  bool firstStep = true;
1432
+  xyze_bool_t step_needed{0};
1433
+  hal_timer_t end_tick_count;
1426
 
1434
 
1435
+  do {
1427
     #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
1436
     #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
1428
     #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
1437
     #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
1429
 
1438
 
1439
+    // Determine if pulses are needed
1440
+    #define PULSE_PREP(AXIS) do{ \
1441
+      delta_error[_AXIS(AXIS)] += advance_dividend[_AXIS(AXIS)]; \
1442
+      step_needed[_AXIS(AXIS)] = (delta_error[_AXIS(AXIS)] >= 0); \
1443
+      if (step_needed[_AXIS(AXIS)]) { \
1444
+        count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
1445
+        delta_error[_AXIS(AXIS)] -= advance_divisor; \
1446
+      } \
1447
+    }while(0)
1448
+
1430
     // Start an active pulse, if Bresenham says so, and update position
1449
     // Start an active pulse, if Bresenham says so, and update position
1431
     #define PULSE_START(AXIS) do{ \
1450
     #define PULSE_START(AXIS) do{ \
1432
-      delta_error[_AXIS(AXIS)] += advance_dividend[_AXIS(AXIS)]; \
1433
-      if (delta_error[_AXIS(AXIS)] >= 0) { \
1451
+      if (step_needed[_AXIS(AXIS)]) { \
1434
         _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), 0); \
1452
         _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), 0); \
1435
-        count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
1436
       } \
1453
       } \
1437
     }while(0)
1454
     }while(0)
1438
 
1455
 
1439
     // Stop an active pulse, if any, and adjust error term
1456
     // Stop an active pulse, if any, and adjust error term
1440
     #define PULSE_STOP(AXIS) do { \
1457
     #define PULSE_STOP(AXIS) do { \
1441
-      if (delta_error[_AXIS(AXIS)] >= 0) { \
1442
-        delta_error[_AXIS(AXIS)] -= advance_divisor; \
1458
+      if (step_needed[_AXIS(AXIS)]) { \
1443
         _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), 0); \
1459
         _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), 0); \
1444
       } \
1460
       } \
1445
     }while(0)
1461
     }while(0)
1446
 
1462
 
1447
-    // Pulse start
1463
+    // Determine if pulses are needed
1448
     #if HAS_X_STEP
1464
     #if HAS_X_STEP
1449
-      PULSE_START(X);
1465
+      PULSE_PREP(X);
1450
     #endif
1466
     #endif
1451
     #if HAS_Y_STEP
1467
     #if HAS_Y_STEP
1452
-      PULSE_START(Y);
1468
+      PULSE_PREP(Y);
1453
     #endif
1469
     #endif
1454
     #if HAS_Z_STEP
1470
     #if HAS_Z_STEP
1455
-      PULSE_START(Z);
1471
+      PULSE_PREP(Z);
1456
     #endif
1472
     #endif
1457
 
1473
 
1458
-    // Pulse Extruders
1459
-    // Tick the E axis, correct error term and update position
1460
     #if EITHER(LIN_ADVANCE, MIXING_EXTRUDER)
1474
     #if EITHER(LIN_ADVANCE, MIXING_EXTRUDER)
1461
       delta_error.e += advance_dividend.e;
1475
       delta_error.e += advance_dividend.e;
1462
       if (delta_error.e >= 0) {
1476
       if (delta_error.e >= 0) {
1465
           delta_error.e -= advance_divisor;
1479
           delta_error.e -= advance_divisor;
1466
           // Don't step E here - But remember the number of steps to perform
1480
           // Don't step E here - But remember the number of steps to perform
1467
           motor_direction(E_AXIS) ? --LA_steps : ++LA_steps;
1481
           motor_direction(E_AXIS) ? --LA_steps : ++LA_steps;
1468
-        #else // !LIN_ADVANCE && MIXING_EXTRUDER
1469
-          // Don't adjust delta_error.e here!
1470
-          // Being positive is the criteria for ending the pulse.
1471
-          E_STEP_WRITE(mixer.get_next_stepper(), !INVERT_E_STEP_PIN);
1482
+        #else
1483
+          step_needed[E_AXIS] = delta_error.e >= 0;
1472
         #endif
1484
         #endif
1473
       }
1485
       }
1474
-    #else // !LIN_ADVANCE && !MIXING_EXTRUDER
1475
-      #if HAS_E0_STEP
1486
+    #elif HAS_E0_STEP
1487
+      PULSE_PREP(E);
1488
+    #endif
1489
+
1490
+    #if (MINIMUM_STEPPER_PULSE || MAXIMUM_STEPPER_RATE) && DISABLED(I2S_STEPPER_STREAM)
1491
+      if (firstStep)
1492
+        firstStep = false;
1493
+      else
1494
+        AWAIT_LOW_PULSE();
1495
+    #endif
1496
+
1497
+    // Pulse start
1498
+    #if HAS_X_STEP
1499
+      PULSE_START(X);
1500
+    #endif
1501
+    #if HAS_Y_STEP
1502
+      PULSE_START(Y);
1503
+    #endif
1504
+    #if HAS_Z_STEP
1505
+      PULSE_START(Z);
1506
+    #endif
1507
+
1508
+    #if DISABLED(LIN_ADVANCE)
1509
+      #if ENABLED(MIXING_EXTRUDER)
1510
+        if (step_needed[E_AXIS]) E_STEP_WRITE(mixer.get_next_stepper(), !INVERT_E_STEP_PIN);
1511
+      #elif HAS_E0_STEP
1476
         PULSE_START(E);
1512
         PULSE_START(E);
1477
       #endif
1513
       #endif
1478
     #endif
1514
     #endif
1482
     #endif
1518
     #endif
1483
 
1519
 
1484
     // TODO: need to deal with MINIMUM_STEPPER_PULSE over i2s
1520
     // TODO: need to deal with MINIMUM_STEPPER_PULSE over i2s
1485
-    #if MINIMUM_STEPPER_PULSE && DISABLED(I2S_STEPPER_STREAM)
1486
-      // Just wait for the requested pulse duration
1487
-      while (HAL_timer_get_count(PULSE_TIMER_NUM) < pulse_end) { /* nada */ }
1521
+    #if (MINIMUM_STEPPER_PULSE || MAXIMUM_STEPPER_RATE) && DISABLED(I2S_STEPPER_STREAM)
1522
+      START_HIGH_PULSE();
1523
+      AWAIT_HIGH_PULSE();
1488
     #endif
1524
     #endif
1489
 
1525
 
1490
-    // Add the delay needed to ensure the maximum driver rate is enforced
1491
-    if (signed(added_step_ticks) > 0) pulse_end += hal_timer_t(added_step_ticks);
1492
-
1493
     // Pulse stop
1526
     // Pulse stop
1494
     #if HAS_X_STEP
1527
     #if HAS_X_STEP
1495
       PULSE_STOP(X);
1528
       PULSE_STOP(X);
1503
 
1536
 
1504
     #if DISABLED(LIN_ADVANCE)
1537
     #if DISABLED(LIN_ADVANCE)
1505
       #if ENABLED(MIXING_EXTRUDER)
1538
       #if ENABLED(MIXING_EXTRUDER)
1539
+
1506
         if (delta_error.e >= 0) {
1540
         if (delta_error.e >= 0) {
1507
           delta_error.e -= advance_divisor;
1541
           delta_error.e -= advance_divisor;
1508
           E_STEP_WRITE(mixer.get_stepper(), INVERT_E_STEP_PIN);
1542
           E_STEP_WRITE(mixer.get_stepper(), INVERT_E_STEP_PIN);
1509
         }
1543
         }
1544
+
1510
       #else // !MIXING_EXTRUDER
1545
       #else // !MIXING_EXTRUDER
1546
+
1511
         #if HAS_E0_STEP
1547
         #if HAS_E0_STEP
1512
           PULSE_STOP(E);
1548
           PULSE_STOP(E);
1513
         #endif
1549
         #endif
1514
-      #endif
1515
-    #endif // !LIN_ADVANCE
1516
 
1550
 
1517
-    // Decrement the count of pending pulses to do
1518
-    --events_to_do;
1551
+      #endif  // !MIXING_EXTRUDER
1552
+    #endif // !LIN_ADVANCE
1519
 
1553
 
1520
-    // For minimum pulse time wait after stopping pulses also
1521
-    if (events_to_do) {
1522
-      // Just wait for the requested pulse duration
1523
-      while (HAL_timer_get_count(PULSE_TIMER_NUM) < pulse_end) { /* nada */ }
1524
-      #if MINIMUM_STEPPER_PULSE
1525
-        // Add to the value, the time that the pulse must be active (to be used on the next loop)
1526
-        pulse_end += hal_timer_t(MIN_PULSE_TICKS);
1527
-      #endif
1528
-    }
1554
+    #if (MINIMUM_STEPPER_PULSE || MAXIMUM_STEPPER_RATE) && DISABLED(I2S_STEPPER_STREAM)
1555
+      if (events_to_do) START_LOW_PULSE();
1556
+    #endif
1529
 
1557
 
1530
-  } while (events_to_do);
1558
+  } while (--events_to_do);
1531
 }
1559
 }
1532
 
1560
 
1533
 // This is the last half of the stepper interrupt: This one processes and
1561
 // This is the last half of the stepper interrupt: This one processes and
1909
       DELAY_NS(MINIMUM_STEPPER_POST_DIR_DELAY);
1937
       DELAY_NS(MINIMUM_STEPPER_POST_DIR_DELAY);
1910
     #endif
1938
     #endif
1911
 
1939
 
1912
-    // Get the timer count and estimate the end of the pulse
1913
-    hal_timer_t pulse_end = HAL_timer_get_count(PULSE_TIMER_NUM) + hal_timer_t(MIN_PULSE_TICKS);
1914
-
1915
-    const hal_timer_t added_step_ticks = hal_timer_t(ADDED_STEP_TICKS);
1940
+    //const hal_timer_t added_step_ticks = hal_timer_t(ADDED_STEP_TICKS);
1916
 
1941
 
1917
     // Step E stepper if we have steps
1942
     // Step E stepper if we have steps
1943
+    bool firstStep = true;
1944
+    hal_timer_t end_tick_count;
1945
+
1918
     while (LA_steps) {
1946
     while (LA_steps) {
1947
+      #if (MINIMUM_STEPPER_PULSE || MAXIMUM_STEPPER_RATE) && DISABLED(I2S_STEPPER_STREAM)
1948
+        if (firstStep)
1949
+          firstStep = false;
1950
+        else
1951
+          AWAIT_LOW_PULSE();
1952
+      #endif
1919
 
1953
 
1920
       // Set the STEP pulse ON
1954
       // Set the STEP pulse ON
1921
       #if ENABLED(MIXING_EXTRUDER)
1955
       #if ENABLED(MIXING_EXTRUDER)
1925
       #endif
1959
       #endif
1926
 
1960
 
1927
       // Enforce a minimum duration for STEP pulse ON
1961
       // Enforce a minimum duration for STEP pulse ON
1928
-      #if MINIMUM_STEPPER_PULSE
1929
-        // Just wait for the requested pulse duration
1930
-        while (HAL_timer_get_count(PULSE_TIMER_NUM) < pulse_end) { /* nada */ }
1962
+      #if (MINIMUM_STEPPER_PULSE || MAXIMUM_STEPPER_RATE)
1963
+        START_HIGH_PULSE();
1931
       #endif
1964
       #endif
1932
 
1965
 
1933
-      // Add the delay needed to ensure the maximum driver rate is enforced
1934
-      if (signed(added_step_ticks) > 0) pulse_end += hal_timer_t(added_step_ticks);
1935
-
1936
       LA_steps < 0 ? ++LA_steps : --LA_steps;
1966
       LA_steps < 0 ? ++LA_steps : --LA_steps;
1937
 
1967
 
1968
+      #if (MINIMUM_STEPPER_PULSE || MAXIMUM_STEPPER_RATE)
1969
+        AWAIT_HIGH_PULSE();
1970
+      #endif
1971
+
1938
       // Set the STEP pulse OFF
1972
       // Set the STEP pulse OFF
1939
       #if ENABLED(MIXING_EXTRUDER)
1973
       #if ENABLED(MIXING_EXTRUDER)
1940
         E_STEP_WRITE(mixer.get_stepper(), INVERT_E_STEP_PIN);
1974
         E_STEP_WRITE(mixer.get_stepper(), INVERT_E_STEP_PIN);
1944
 
1978
 
1945
       // For minimum pulse time wait before looping
1979
       // For minimum pulse time wait before looping
1946
       // Just wait for the requested pulse duration
1980
       // Just wait for the requested pulse duration
1947
-      if (LA_steps) {
1948
-        while (HAL_timer_get_count(PULSE_TIMER_NUM) < pulse_end) { /* nada */ }
1949
-        #if MINIMUM_STEPPER_PULSE
1950
-          // Add to the value, the time that the pulse must be active (to be used on the next loop)
1951
-          pulse_end += hal_timer_t(MIN_PULSE_TICKS);
1952
-        #endif
1953
-      }
1981
+      #if (MINIMUM_STEPPER_PULSE || MAXIMUM_STEPPER_RATE)
1982
+        if (LA_steps) START_LOW_PULSE();
1983
+      #endif
1954
     } // LA_steps
1984
     } // LA_steps
1955
 
1985
 
1956
     return interval;
1986
     return interval;

+ 21
- 16
Marlin/src/module/stepper.h View File

57
 //
57
 //
58
 
58
 
59
 #ifdef CPU_32_BIT
59
 #ifdef CPU_32_BIT
60
+  #define TIMER_READ_ADD_AND_STORE_CYCLES 34UL
60
 
61
 
61
   // The base ISR takes 792 cycles
62
   // The base ISR takes 792 cycles
62
   #define ISR_BASE_CYCLES  792UL
63
   #define ISR_BASE_CYCLES  792UL
85
   #define ISR_STEPPER_CYCLES 16UL
86
   #define ISR_STEPPER_CYCLES 16UL
86
 
87
 
87
 #else
88
 #else
89
+  #define TIMER_READ_ADD_AND_STORE_CYCLES 13UL
88
 
90
 
89
   // The base ISR takes 752 cycles
91
   // The base ISR takes 752 cycles
90
   #define ISR_BASE_CYCLES  752UL
92
   #define ISR_BASE_CYCLES  752UL
116
 
118
 
117
 // Add time for each stepper
119
 // Add time for each stepper
118
 #if HAS_X_STEP
120
 #if HAS_X_STEP
119
-  #define ISR_START_X_STEPPER_CYCLES ISR_START_STEPPER_CYCLES
120
   #define ISR_X_STEPPER_CYCLES       ISR_STEPPER_CYCLES
121
   #define ISR_X_STEPPER_CYCLES       ISR_STEPPER_CYCLES
121
 #else
122
 #else
122
-  #define ISR_START_X_STEPPER_CYCLES 0UL
123
   #define ISR_X_STEPPER_CYCLES       0UL
123
   #define ISR_X_STEPPER_CYCLES       0UL
124
 #endif
124
 #endif
125
 #if HAS_Y_STEP
125
 #if HAS_Y_STEP
126
-  #define ISR_START_Y_STEPPER_CYCLES ISR_START_STEPPER_CYCLES
127
   #define ISR_Y_STEPPER_CYCLES       ISR_STEPPER_CYCLES
126
   #define ISR_Y_STEPPER_CYCLES       ISR_STEPPER_CYCLES
128
 #else
127
 #else
129
   #define ISR_START_Y_STEPPER_CYCLES 0UL
128
   #define ISR_START_Y_STEPPER_CYCLES 0UL
130
   #define ISR_Y_STEPPER_CYCLES       0UL
129
   #define ISR_Y_STEPPER_CYCLES       0UL
131
 #endif
130
 #endif
132
 #if HAS_Z_STEP
131
 #if HAS_Z_STEP
133
-  #define ISR_START_Z_STEPPER_CYCLES ISR_START_STEPPER_CYCLES
134
   #define ISR_Z_STEPPER_CYCLES       ISR_STEPPER_CYCLES
132
   #define ISR_Z_STEPPER_CYCLES       ISR_STEPPER_CYCLES
135
 #else
133
 #else
136
-  #define ISR_START_Z_STEPPER_CYCLES 0UL
137
   #define ISR_Z_STEPPER_CYCLES       0UL
134
   #define ISR_Z_STEPPER_CYCLES       0UL
138
 #endif
135
 #endif
139
 
136
 
140
 // E is always interpolated, even for mixing extruders
137
 // E is always interpolated, even for mixing extruders
141
-#define ISR_START_E_STEPPER_CYCLES   ISR_START_STEPPER_CYCLES
142
 #define ISR_E_STEPPER_CYCLES         ISR_STEPPER_CYCLES
138
 #define ISR_E_STEPPER_CYCLES         ISR_STEPPER_CYCLES
143
 
139
 
144
 // If linear advance is disabled, the loop also handles them
140
 // If linear advance is disabled, the loop also handles them
145
 #if DISABLED(LIN_ADVANCE) && ENABLED(MIXING_EXTRUDER)
141
 #if DISABLED(LIN_ADVANCE) && ENABLED(MIXING_EXTRUDER)
146
-  #define ISR_START_MIXING_STEPPER_CYCLES ((MIXING_STEPPERS) * (ISR_START_STEPPER_CYCLES))
147
   #define ISR_MIXING_STEPPER_CYCLES ((MIXING_STEPPERS) * (ISR_STEPPER_CYCLES))
142
   #define ISR_MIXING_STEPPER_CYCLES ((MIXING_STEPPERS) * (ISR_STEPPER_CYCLES))
148
 #else
143
 #else
149
-  #define ISR_START_MIXING_STEPPER_CYCLES 0UL
150
   #define ISR_MIXING_STEPPER_CYCLES  0UL
144
   #define ISR_MIXING_STEPPER_CYCLES  0UL
151
 #endif
145
 #endif
152
 
146
 
153
-// Calculate the minimum time to start all stepper pulses in the ISR loop
154
-#define MIN_ISR_START_LOOP_CYCLES (ISR_START_X_STEPPER_CYCLES + ISR_START_Y_STEPPER_CYCLES + ISR_START_Z_STEPPER_CYCLES + ISR_START_E_STEPPER_CYCLES + ISR_START_MIXING_STEPPER_CYCLES)
155
-
156
 // And the total minimum loop time, not including the base
147
 // And the total minimum loop time, not including the base
157
 #define MIN_ISR_LOOP_CYCLES (ISR_X_STEPPER_CYCLES + ISR_Y_STEPPER_CYCLES + ISR_Z_STEPPER_CYCLES + ISR_E_STEPPER_CYCLES + ISR_MIXING_STEPPER_CYCLES)
148
 #define MIN_ISR_LOOP_CYCLES (ISR_X_STEPPER_CYCLES + ISR_Y_STEPPER_CYCLES + ISR_Z_STEPPER_CYCLES + ISR_E_STEPPER_CYCLES + ISR_MIXING_STEPPER_CYCLES)
158
 
149
 
170
 // adding the "start stepper pulse" code section execution cycles to account for that not all
161
 // adding the "start stepper pulse" code section execution cycles to account for that not all
171
 // pulses start at the beginning of the loop, so an extra time must be added to compensate so
162
 // pulses start at the beginning of the loop, so an extra time must be added to compensate so
172
 // the last generated pulse (usually the extruder stepper) has the right length
163
 // the last generated pulse (usually the extruder stepper) has the right length
173
-#if HAS_DRIVER(LV8729) && MINIMUM_STEPPER_PULSE == 0
174
-  #define MIN_PULSE_TICKS ((((PULSE_TIMER_TICKS_PER_US) + 1) / 2) + ((MIN_ISR_START_LOOP_CYCLES) / uint32_t(PULSE_TIMER_PRESCALE)))
164
+#if MINIMUM_STEPPER_PULSE && MAXIMUM_STEPPER_RATE
165
+  constexpr uint32_t _MIN_STEP_PERIOD_NS = 1000000000UL / MAXIMUM_STEPPER_RATE;
166
+  constexpr uint32_t _MIN_PULSE_HIGH_NS = 1000UL * MINIMUM_STEPPER_PULSE;
167
+  constexpr uint32_t _MIN_PULSE_LOW_NS = _MAX((_MIN_STEP_PERIOD_NS - _MIN(_MIN_STEP_PERIOD_NS, _MIN_PULSE_HIGH_NS)), _MIN_PULSE_HIGH_NS);
168
+#elif MINIMUM_STEPPER_PULSE
169
+  // Assume 50% duty cycle
170
+  constexpr uint32_t _MIN_STEP_PERIOD_NS = 1000000000UL / MAXIMUM_STEPPER_RATE;
171
+  constexpr uint32_t _MIN_PULSE_HIGH_NS = 1000UL * MINIMUM_STEPPER_PULSE;
172
+  constexpr uint32_t _MIN_PULSE_LOW_NS = _MIN_PULSE_HIGH_NS;
173
+#elif MAXIMUM_STEPPER_RATE
174
+  // Assume 50% duty cycle
175
+  constexpr uint32_t _MIN_PULSE_HIGH_NS = 500000000UL / MAXIMUM_STEPPER_RATE;
176
+  constexpr uint32_t _MIN_PULSE_LOW_NS = _MIN_PULSE_HIGH_NS;
175
 #else
177
 #else
176
-  #define MIN_PULSE_TICKS (((PULSE_TIMER_TICKS_PER_US) * uint32_t(MINIMUM_STEPPER_PULSE)) + ((MIN_ISR_START_LOOP_CYCLES) / uint32_t(PULSE_TIMER_PRESCALE)))
178
+  #error "Expected at least one of MINIMUM_STEPPER_PULSE or MAXIMUM_STEPPER_RATE to be defined"
177
 #endif
179
 #endif
178
 
180
 
179
-// Calculate the extra ticks of the PULSE timer between step pulses
180
-#define ADDED_STEP_TICKS (((MIN_STEPPER_PULSE_CYCLES) / (PULSE_TIMER_PRESCALE)) - (MIN_PULSE_TICKS))
181
+// TODO: NS_TO_PULSE_TIMER_TICKS has some rounding issues:
182
+//   1. PULSE_TIMER_TICKS_PER_US rounds to an integer, which loses 20% of the count for a 2.5 MHz pulse tick (such as for LPC1768)
183
+//   2. The math currently rounds down to the closes tick. Perhaps should round up.
184
+constexpr uint32_t NS_TO_PULSE_TIMER_TICKS(uint32_t NS) { return PULSE_TIMER_TICKS_PER_US * (NS) / 1000UL; }
185
+#define CYCLES_TO_NS(CYC) (1000UL * (CYC) / ((F_CPU) / 1000000))
181
 
186
 
182
 // But the user could be enforcing a minimum time, so the loop time is
187
 // But the user could be enforcing a minimum time, so the loop time is
183
 #define ISR_LOOP_CYCLES (ISR_LOOP_BASE_CYCLES + _MAX(MIN_STEPPER_PULSE_CYCLES, MIN_ISR_LOOP_CYCLES))
188
 #define ISR_LOOP_CYCLES (ISR_LOOP_BASE_CYCLES + _MAX(MIN_STEPPER_PULSE_CYCLES, MIN_ISR_LOOP_CYCLES))

Loading…
Cancel
Save