Sfoglia il codice sorgente

Fix MINIMUM_STEPPER_PULSE maths

Scott Lahteine 8 anni fa
parent
commit
6a9b008044
2 ha cambiato i file con 117 aggiunte e 64 eliminazioni
  1. 3
    2
      Marlin/macros.h
  2. 114
    62
      Marlin/stepper.cpp

+ 3
- 2
Marlin/macros.h Vedi File

36
   #define CRITICAL_SECTION_END    SREG = _sreg;
36
   #define CRITICAL_SECTION_END    SREG = _sreg;
37
 #endif
37
 #endif
38
 
38
 
39
-// Clock speed factor
40
-#define CYCLES_PER_MICROSECOND (F_CPU / 1000000UL) // 16 or 20
39
+// Clock speed factors
40
+#define CYCLES_PER_MICROSECOND (F_CPU / 1000000L) // 16 or 20
41
+#define INT0_PRESCALER 8
41
 
42
 
42
 // Highly granular delays for step pulses, etc.
43
 // Highly granular delays for step pulses, etc.
43
 #define DELAY_0_NOP NOOP
44
 #define DELAY_0_NOP NOOP

+ 114
- 62
Marlin/stepper.cpp Vedi File

460
 
460
 
461
   // Take multiple steps per interrupt (For high speed moves)
461
   // Take multiple steps per interrupt (For high speed moves)
462
   bool all_steps_done = false;
462
   bool all_steps_done = false;
463
-  for (int8_t i = 0; i < step_loops; i++) {
463
+  for (uint8_t i = step_loops; i--;) {
464
     #if ENABLED(LIN_ADVANCE)
464
     #if ENABLED(LIN_ADVANCE)
465
 
465
 
466
       counter_E += current_block->steps[E_AXIS];
466
       counter_E += current_block->steps[E_AXIS];
530
         _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS),0); \
530
         _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS),0); \
531
       }
531
       }
532
 
532
 
533
-    #define CYCLES_EATEN_BY_CODE 240
533
+    #if HAS_X_STEP
534
+      #define _COUNT_STEPPERS_1 1
535
+    #else
536
+      #define _COUNT_STEPPERS_1 0
537
+    #endif
538
+    #if HAS_Y_STEP
539
+      #define _COUNT_STEPPERS_2 _COUNT_STEPPERS_1 + 1
540
+    #else
541
+      #define _COUNT_STEPPERS_2 _COUNT_STEPPERS_1
542
+    #endif
543
+    #if HAS_Z_STEP
544
+      #define _COUNT_STEPPERS_3 _COUNT_STEPPERS_2 + 1
545
+    #else
546
+      #define _COUNT_STEPPERS_3 _COUNT_STEPPERS_2
547
+    #endif
548
+    #if DISABLED(ADVANCE) && DISABLED(LIN_ADVANCE)
549
+      #define _COUNT_STEPPERS_4 _COUNT_STEPPERS_3 + 1
550
+    #else
551
+      #define _COUNT_STEPPERS_4 _COUNT_STEPPERS_3
552
+    #endif
553
+
554
+    #define CYCLES_EATEN_XYZE ((_COUNT_STEPPERS_4) * 5)
555
+    #define EXTRA_CYCLES_XYZE (STEP_PULSE_CYCLES - (CYCLES_EATEN_XYZE))
534
 
556
 
535
-    // If a minimum pulse time was specified get the CPU clock
536
-    #if STEP_PULSE_CYCLES > CYCLES_EATEN_BY_CODE
557
+    // If a minimum pulse time was specified get the timer 0 value
558
+    // which increments every 4µs on 16MHz and every 3.2µs on 20MHz.
559
+    // Two or 3 counts of TCNT0 should be a sufficient delay.
560
+    #if EXTRA_CYCLES_XYZE > 20
537
       uint32_t pulse_start = TCNT0;
561
       uint32_t pulse_start = TCNT0;
538
     #endif
562
     #endif
539
 
563
 
564
       #endif
588
       #endif
565
     #endif // !ADVANCE && !LIN_ADVANCE
589
     #endif // !ADVANCE && !LIN_ADVANCE
566
 
590
 
567
-    // For a minimum pulse time wait before stopping pulses
568
-    #if STEP_PULSE_CYCLES > CYCLES_EATEN_BY_CODE
569
-      while ((uint32_t)(TCNT0 - pulse_start) < STEP_PULSE_CYCLES - CYCLES_EATEN_BY_CODE) { /* nada */ }
591
+    // For minimum pulse time wait before stopping pulses
592
+    #if EXTRA_CYCLES_XYZE > 20
593
+      while (EXTRA_CYCLES_XYZE > (uint32_t)(TCNT0 - pulse_start) * (INT0_PRESCALER)) { /* nada */ }
594
+      pulse_start = TCNT0;
595
+    #elif EXTRA_CYCLES_XYZE > 0
596
+      DELAY_NOPS(EXTRA_CYCLES_XYZE);
570
     #endif
597
     #endif
571
 
598
 
572
     #if HAS_X_STEP
599
     #if HAS_X_STEP
601
       all_steps_done = true;
628
       all_steps_done = true;
602
       break;
629
       break;
603
     }
630
     }
604
-  }
631
+
632
+    // For minimum pulse time wait before stopping pulses
633
+    #if EXTRA_CYCLES_XYZE > 20
634
+      if (i) while (EXTRA_CYCLES_XYZE > (uint32_t)(TCNT0 - pulse_start) * (INT0_PRESCALER)) { /* nada */ }
635
+    #elif EXTRA_CYCLES_XYZE > 0
636
+      if (i) DELAY_NOPS(EXTRA_CYCLES_XYZE);
637
+    #endif
638
+
639
+  } // steps_loop
605
 
640
 
606
   #if ENABLED(LIN_ADVANCE)
641
   #if ENABLED(LIN_ADVANCE)
607
     if (current_block->use_advance_lead) {
642
     if (current_block->use_advance_lead) {
765
 
800
 
766
 #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
801
 #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
767
 
802
 
803
+  #define CYCLES_EATEN_E (E_STEPPERS * 5)
804
+  #define EXTRA_CYCLES_E (STEP_PULSE_CYCLES - (CYCLES_EATEN_E))
805
+
768
   // Timer interrupt for E. e_steps is set in the main routine;
806
   // Timer interrupt for E. e_steps is set in the main routine;
769
 
807
 
770
   void Stepper::advance_isr() {
808
   void Stepper::advance_isr() {
794
       #endif
832
       #endif
795
     #endif
833
     #endif
796
 
834
 
797
-    #define CYCLES_EATEN_BY_E 60
798
-
799
     // Step all E steppers that have steps
835
     // Step all E steppers that have steps
800
-    for (uint8_t i = 0; i < step_loops; i++) {
836
+    for (uint8_t i = step_loops; i--;) {
801
 
837
 
802
-      #if STEP_PULSE_CYCLES > CYCLES_EATEN_BY_E
838
+      #if EXTRA_CYCLES_E > 20
803
         uint32_t pulse_start = TCNT0;
839
         uint32_t pulse_start = TCNT0;
804
       #endif
840
       #endif
805
 
841
 
814
         #endif
850
         #endif
815
       #endif
851
       #endif
816
 
852
 
817
-      // For a minimum pulse time wait before stopping pulses
818
-      #if STEP_PULSE_CYCLES > CYCLES_EATEN_BY_E
819
-        while ((uint32_t)(TCNT0 - pulse_start) < STEP_PULSE_CYCLES - CYCLES_EATEN_BY_E) { /* nada */ }
853
+      // For minimum pulse time wait before stopping pulses
854
+      #if EXTRA_CYCLES_E > 20
855
+        while (EXTRA_CYCLES_E > (uint32_t)(TCNT0 - pulse_start) * (INT0_PRESCALER)) { /* nada */ }
856
+        pulse_start = TCNT0;
857
+      #elif EXTRA_CYCLES_E > 0
858
+        DELAY_NOPS(EXTRA_CYCLES_E);
820
       #endif
859
       #endif
821
 
860
 
822
       STOP_E_PULSE(0);
861
       STOP_E_PULSE(0);
829
           #endif
868
           #endif
830
         #endif
869
         #endif
831
       #endif
870
       #endif
832
-    }
833
 
871
 
872
+      // For minimum pulse time wait before looping
873
+      #if EXTRA_CYCLES_E > 20
874
+        if (i) while (EXTRA_CYCLES_E > (uint32_t)(TCNT0 - pulse_start) * (INT0_PRESCALER)) { /* nada */ }
875
+      #elif EXTRA_CYCLES_E > 0
876
+        if (i) DELAY_NOPS(EXTRA_CYCLES_E);
877
+      #endif
878
+
879
+    } // steps_loop
834
   }
880
   }
835
 
881
 
836
   void Stepper::advance_isr_scheduler() {
882
   void Stepper::advance_isr_scheduler() {
1056
   ENABLE_STEPPER_DRIVER_INTERRUPT();
1102
   ENABLE_STEPPER_DRIVER_INTERRUPT();
1057
 
1103
 
1058
   #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
1104
   #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
1059
-
1060
-    for (int i = 0; i < E_STEPPERS; i++) {
1061
-      e_steps[i] = 0;
1062
-      #if ENABLED(LIN_ADVANCE)
1063
-        current_adv_steps[i] = 0;
1064
-      #endif
1065
-    }
1066
-
1067
-  #endif // ADVANCE or LIN_ADVANCE
1105
+    ZERO(e_steps);
1106
+    #if ENABLED(LIN_ADVANCE)
1107
+      ZERO(current_adv_steps);
1108
+    #endif
1109
+  #endif // ADVANCE || LIN_ADVANCE
1068
 
1110
 
1069
   endstops.enable(true); // Start with endstops active. After homing they can be disabled
1111
   endstops.enable(true); // Start with endstops active. After homing they can be disabled
1070
   sei();
1112
   sei();
1235
 
1277
 
1236
 #if ENABLED(BABYSTEPPING)
1278
 #if ENABLED(BABYSTEPPING)
1237
 
1279
 
1238
-  #define CYCLES_EATEN_BY_BABYSTEP 60
1280
+  #if ENABLED(DELTA)
1281
+    #define CYCLES_EATEN_BABYSTEP (2 * 15)
1282
+  #else
1283
+    #define CYCLES_EATEN_BABYSTEP 0
1284
+  #endif
1285
+  #define EXTRA_CYCLES_BABYSTEP (STEP_PULSE_CYCLES - (CYCLES_EATEN_BABYSTEP))
1239
 
1286
 
1240
   #define _ENABLE(AXIS) enable_## AXIS()
1287
   #define _ENABLE(AXIS) enable_## AXIS()
1241
   #define _READ_DIR(AXIS) AXIS ##_DIR_READ
1288
   #define _READ_DIR(AXIS) AXIS ##_DIR_READ
1242
   #define _INVERT_DIR(AXIS) INVERT_## AXIS ##_DIR
1289
   #define _INVERT_DIR(AXIS) INVERT_## AXIS ##_DIR
1243
   #define _APPLY_DIR(AXIS, INVERT) AXIS ##_APPLY_DIR(INVERT, true)
1290
   #define _APPLY_DIR(AXIS, INVERT) AXIS ##_APPLY_DIR(INVERT, true)
1244
 
1291
 
1245
-  #if STEP_PULSE_CYCLES > CYCLES_EATEN_BY_BABYSTEP
1292
+  #if EXTRA_CYCLES_BABYSTEP > 20
1246
     #define _SAVE_START (pulse_start = TCNT0)
1293
     #define _SAVE_START (pulse_start = TCNT0)
1247
-    #define _PULSE_WAIT while ((uint32_t)(TCNT0 - pulse_start) < STEP_PULSE_CYCLES - CYCLES_EATEN_BY_BABYSTEP) { /* nada */ }
1294
+    #define _PULSE_WAIT while (EXTRA_CYCLES_BABYSTEP > (uint32_t)(TCNT0 - pulse_start) * (INT0_PRESCALER)) { /* nada */ }
1248
   #else
1295
   #else
1249
     #define _SAVE_START NOOP
1296
     #define _SAVE_START NOOP
1250
-    #define _PULSE_WAIT NOOP
1297
+    #if EXTRA_CYCLES_BABYSTEP > 0
1298
+      #define _PULSE_WAIT DELAY_NOPS(EXTRA_CYCLES_BABYSTEP)
1299
+    #elif STEP_PULSE_CYCLES > 0
1300
+      #define _PULSE_WAIT NOOP
1301
+    #elif ENABLED(DELTA)
1302
+      #define _PULSE_WAIT delayMicroseconds(2);
1303
+    #else
1304
+      #define _PULSE_WAIT delayMicroseconds(4);
1305
+    #endif
1251
   #endif
1306
   #endif
1252
 
1307
 
1253
-  #define START_BABYSTEP_AXIS(AXIS, INVERT) { \
1254
-      old_dir = _READ_DIR(AXIS); \
1255
-      _SAVE_START; \
1308
+  #define BABYSTEP_AXIS(AXIS, INVERT) {                     \
1309
+      const uint8_t old_dir = _READ_DIR(AXIS);              \
1310
+      _ENABLE(AXIS);                                        \
1311
+      _SAVE_START;                                          \
1256
       _APPLY_DIR(AXIS, _INVERT_DIR(AXIS)^direction^INVERT); \
1312
       _APPLY_DIR(AXIS, _INVERT_DIR(AXIS)^direction^INVERT); \
1257
-      _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), true); \
1258
-    }
1259
-
1260
-  #define STOP_BABYSTEP_AXIS(AXIS) { \
1261
-      _PULSE_WAIT; \
1262
-      _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), true); \
1263
-      _APPLY_DIR(AXIS, old_dir); \
1313
+      _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), true);     \
1314
+      _PULSE_WAIT;                                          \
1315
+      _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), true);      \
1316
+      _APPLY_DIR(AXIS, old_dir);                            \
1264
     }
1317
     }
1265
 
1318
 
1266
   // MUST ONLY BE CALLED BY AN ISR,
1319
   // MUST ONLY BE CALLED BY AN ISR,
1268
   void Stepper::babystep(const AxisEnum axis, const bool direction) {
1321
   void Stepper::babystep(const AxisEnum axis, const bool direction) {
1269
     cli();
1322
     cli();
1270
     uint8_t old_dir;
1323
     uint8_t old_dir;
1271
-    #if STEP_PULSE_CYCLES > CYCLES_EATEN_BY_BABYSTEP
1324
+
1325
+    #if EXTRA_CYCLES_BABYSTEP > 20
1272
       uint32_t pulse_start;
1326
       uint32_t pulse_start;
1273
     #endif
1327
     #endif
1274
 
1328
 
1275
     switch (axis) {
1329
     switch (axis) {
1276
 
1330
 
1277
-      case X_AXIS:
1278
-        _ENABLE(x);
1279
-        START_BABYSTEP_AXIS(X, false);
1280
-        STOP_BABYSTEP_AXIS(X);
1281
-        break;
1331
+      #if ENABLED(BABYSTEP_XY)
1332
+
1333
+        case X_AXIS:
1334
+          BABYSTEP_AXIS(X, false);
1335
+          break;
1282
 
1336
 
1283
-      case Y_AXIS:
1284
-        _ENABLE(y);
1285
-        START_BABYSTEP_AXIS(Y, false);
1286
-        STOP_BABYSTEP_AXIS(Y);
1287
-        break;
1337
+        case Y_AXIS:
1338
+          BABYSTEP_AXIS(Y, false);
1339
+          break;
1340
+
1341
+      #endif
1288
 
1342
 
1289
       case Z_AXIS: {
1343
       case Z_AXIS: {
1290
 
1344
 
1291
         #if DISABLED(DELTA)
1345
         #if DISABLED(DELTA)
1292
 
1346
 
1293
-          _ENABLE(z);
1294
-          START_BABYSTEP_AXIS(Z, BABYSTEP_INVERT_Z);
1295
-          STOP_BABYSTEP_AXIS(Z);
1347
+          BABYSTEP_AXIS(Z, BABYSTEP_INVERT_Z);
1296
 
1348
 
1297
         #else // DELTA
1349
         #else // DELTA
1298
 
1350
 
1305
           uint8_t old_x_dir_pin = X_DIR_READ,
1357
           uint8_t old_x_dir_pin = X_DIR_READ,
1306
                   old_y_dir_pin = Y_DIR_READ,
1358
                   old_y_dir_pin = Y_DIR_READ,
1307
                   old_z_dir_pin = Z_DIR_READ;
1359
                   old_z_dir_pin = Z_DIR_READ;
1308
-          //setup new step
1360
+
1309
           X_DIR_WRITE(INVERT_X_DIR ^ z_direction);
1361
           X_DIR_WRITE(INVERT_X_DIR ^ z_direction);
1310
           Y_DIR_WRITE(INVERT_Y_DIR ^ z_direction);
1362
           Y_DIR_WRITE(INVERT_Y_DIR ^ z_direction);
1311
           Z_DIR_WRITE(INVERT_Z_DIR ^ z_direction);
1363
           Z_DIR_WRITE(INVERT_Z_DIR ^ z_direction);
1312
-          //perform step
1313
-          #if STEP_PULSE_CYCLES > CYCLES_EATEN_BY_BABYSTEP
1314
-            pulse_start = TCNT0;
1315
-          #endif
1364
+
1365
+          _SAVE_START;
1366
+
1316
           X_STEP_WRITE(!INVERT_X_STEP_PIN);
1367
           X_STEP_WRITE(!INVERT_X_STEP_PIN);
1317
           Y_STEP_WRITE(!INVERT_Y_STEP_PIN);
1368
           Y_STEP_WRITE(!INVERT_Y_STEP_PIN);
1318
           Z_STEP_WRITE(!INVERT_Z_STEP_PIN);
1369
           Z_STEP_WRITE(!INVERT_Z_STEP_PIN);
1319
-          #if STEP_PULSE_CYCLES > CYCLES_EATEN_BY_BABYSTEP
1320
-            while ((uint32_t)(TCNT0 - pulse_start) < STEP_PULSE_CYCLES - CYCLES_EATEN_BY_BABYSTEP) { /* nada */ }
1321
-          #endif
1370
+
1371
+          _PULSE_WAIT;
1372
+
1322
           X_STEP_WRITE(INVERT_X_STEP_PIN);
1373
           X_STEP_WRITE(INVERT_X_STEP_PIN);
1323
           Y_STEP_WRITE(INVERT_Y_STEP_PIN);
1374
           Y_STEP_WRITE(INVERT_Y_STEP_PIN);
1324
           Z_STEP_WRITE(INVERT_Z_STEP_PIN);
1375
           Z_STEP_WRITE(INVERT_Z_STEP_PIN);
1325
-          //get old pin state back.
1376
+
1377
+          // Restore direction bits
1326
           X_DIR_WRITE(old_x_dir_pin);
1378
           X_DIR_WRITE(old_x_dir_pin);
1327
           Y_DIR_WRITE(old_y_dir_pin);
1379
           Y_DIR_WRITE(old_y_dir_pin);
1328
           Z_DIR_WRITE(old_z_dir_pin);
1380
           Z_DIR_WRITE(old_z_dir_pin);

Loading…
Annulla
Salva