Pārlūkot izejas kodu

Extract update_endstops from stepper ISR (PR#2474)

Scott Lahteine 10 gadus atpakaļ
vecāks
revīzija
000a6fce46
1 mainītis faili ar 179 papildinājumiem un 178 dzēšanām
  1. 179
    178
      Marlin/stepper.cpp

+ 179
- 178
Marlin/stepper.cpp Parādīt failu

286
 
286
 
287
 void enable_endstops(bool check) { check_endstops = check; }
287
 void enable_endstops(bool check) { check_endstops = check; }
288
 
288
 
289
+// Check endstops
290
+inline void update_endstops() {
291
+  
292
+  #ifdef Z_DUAL_ENDSTOPS
293
+    uint16_t
294
+  #else
295
+    byte
296
+  #endif
297
+      current_endstop_bits = 0;
298
+
299
+  #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
300
+  #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
301
+  #define _AXIS(AXIS) AXIS ##_AXIS
302
+  #define _ENDSTOP_HIT(AXIS) endstop_hit_bits |= BIT(_ENDSTOP(AXIS, MIN))
303
+  #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
304
+
305
+  // SET_ENDSTOP_BIT: set the current endstop bits for an endstop to its status
306
+  #define SET_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT(current_endstop_bits, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
307
+  // COPY_BIT: copy the value of COPY_BIT to BIT in bits
308
+  #define COPY_BIT(bits, COPY_BIT, BIT) SET_BIT(bits, BIT, TEST(bits, COPY_BIT))
309
+  // TEST_ENDSTOP: test the old and the current status of an endstop
310
+  #define TEST_ENDSTOP(ENDSTOP) (TEST(current_endstop_bits, ENDSTOP) && TEST(old_endstop_bits, ENDSTOP))
311
+
312
+  #define UPDATE_ENDSTOP(AXIS,MINMAX) \
313
+    SET_ENDSTOP_BIT(AXIS, MINMAX); \
314
+    if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX))  && (current_block->steps[_AXIS(AXIS)] > 0)) { \
315
+      endstops_trigsteps[_AXIS(AXIS)] = count_position[_AXIS(AXIS)]; \
316
+      _ENDSTOP_HIT(AXIS); \
317
+      step_events_completed = current_block->step_event_count; \
318
+    }
319
+  
320
+  #ifdef COREXY
321
+    // Head direction in -X axis for CoreXY bots.
322
+    // If DeltaX == -DeltaY, the movement is only in Y axis
323
+    if ((current_block->steps[A_AXIS] != current_block->steps[B_AXIS]) || (TEST(out_bits, A_AXIS) == TEST(out_bits, B_AXIS))) {
324
+      if (TEST(out_bits, X_HEAD))
325
+  #elif defined(COREXZ)
326
+    // Head direction in -X axis for CoreXZ bots.
327
+    // If DeltaX == -DeltaZ, the movement is only in Z axis
328
+    if ((current_block->steps[A_AXIS] != current_block->steps[C_AXIS]) || (TEST(out_bits, A_AXIS) == TEST(out_bits, C_AXIS))) {
329
+      if (TEST(out_bits, X_HEAD))
330
+  #else
331
+      if (TEST(out_bits, X_AXIS))   // stepping along -X axis (regular Cartesian bot)
332
+  #endif
333
+      { // -direction
334
+        #ifdef DUAL_X_CARRIAGE
335
+          // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
336
+          if ((current_block->active_extruder == 0 && X_HOME_DIR == -1) || (current_block->active_extruder != 0 && X2_HOME_DIR == -1))
337
+        #endif
338
+          {
339
+            #if HAS_X_MIN
340
+              UPDATE_ENDSTOP(X, MIN);
341
+            #endif
342
+          }
343
+      }
344
+      else { // +direction
345
+        #ifdef DUAL_X_CARRIAGE
346
+          // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
347
+          if ((current_block->active_extruder == 0 && X_HOME_DIR == 1) || (current_block->active_extruder != 0 && X2_HOME_DIR == 1))
348
+        #endif
349
+          {
350
+            #if HAS_X_MAX
351
+              UPDATE_ENDSTOP(X, MAX);
352
+            #endif
353
+          }
354
+      }
355
+  #if defined(COREXY) || defined(COREXZ)
356
+    }
357
+  #endif
358
+
359
+  #ifdef COREXY
360
+    // Head direction in -Y axis for CoreXY bots.
361
+    // If DeltaX == DeltaY, the movement is only in X axis
362
+    if ((current_block->steps[A_AXIS] != current_block->steps[B_AXIS]) || (TEST(out_bits, A_AXIS) != TEST(out_bits, B_AXIS))) {
363
+      if (TEST(out_bits, Y_HEAD))
364
+  #else
365
+      if (TEST(out_bits, Y_AXIS))   // -direction
366
+  #endif
367
+      { // -direction
368
+        #if HAS_Y_MIN
369
+          UPDATE_ENDSTOP(Y, MIN);
370
+        #endif
371
+      }
372
+      else { // +direction
373
+        #if HAS_Y_MAX
374
+          UPDATE_ENDSTOP(Y, MAX);
375
+        #endif
376
+      }
377
+  #if defined(COREXY) || defined(COREXZ)
378
+    }
379
+  #endif
380
+
381
+  #ifdef COREXZ
382
+    // Head direction in -Z axis for CoreXZ bots.
383
+    // If DeltaX == DeltaZ, the movement is only in X axis
384
+    if ((current_block->steps[A_AXIS] != current_block->steps[C_AXIS]) || (TEST(out_bits, A_AXIS) != TEST(out_bits, C_AXIS))) {
385
+      if (TEST(out_bits, Z_HEAD))
386
+  #else
387
+      if (TEST(out_bits, Z_AXIS))
388
+  #endif
389
+      { // z -direction
390
+        #if HAS_Z_MIN
391
+
392
+          #ifdef Z_DUAL_ENDSTOPS
393
+            SET_ENDSTOP_BIT(Z, MIN);
394
+              #if HAS_Z2_MIN
395
+                SET_ENDSTOP_BIT(Z2, MIN);
396
+              #else
397
+                COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN);
398
+              #endif
399
+
400
+            byte z_test = TEST_ENDSTOP(Z_MIN) << 0 + TEST_ENDSTOP(Z2_MIN) << 1; // bit 0 for Z, bit 1 for Z2
401
+
402
+            if (z_test && current_block->steps[Z_AXIS] > 0) { // z_test = Z_MIN || Z2_MIN
403
+              endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
404
+              endstop_hit_bits |= BIT(Z_MIN);
405
+              if (!performing_homing || (z_test == 0x3))  //if not performing home or if both endstops were trigged during homing...
406
+                step_events_completed = current_block->step_event_count;
407
+            }
408
+          #else // !Z_DUAL_ENDSTOPS
409
+
410
+            UPDATE_ENDSTOP(Z, MIN);
411
+          #endif // !Z_DUAL_ENDSTOPS
412
+        #endif // Z_MIN_PIN
413
+
414
+        #ifdef Z_PROBE_ENDSTOP
415
+          UPDATE_ENDSTOP(Z, PROBE);
416
+
417
+          if (TEST_ENDSTOP(Z_PROBE))
418
+          {
419
+            endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
420
+            endstop_hit_bits |= BIT(Z_PROBE);
421
+          }
422
+        #endif
423
+      }
424
+      else { // z +direction
425
+        #if HAS_Z_MAX
426
+
427
+          #ifdef Z_DUAL_ENDSTOPS
428
+
429
+            SET_ENDSTOP_BIT(Z, MAX);
430
+              #if HAS_Z2_MAX
431
+                SET_ENDSTOP_BIT(Z2, MAX);
432
+              #else
433
+                COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX)
434
+              #endif
435
+
436
+            byte z_test = TEST_ENDSTOP(Z_MAX) << 0 + TEST_ENDSTOP(Z2_MAX) << 1; // bit 0 for Z, bit 1 for Z2
437
+
438
+            if (z_test && current_block->steps[Z_AXIS] > 0) {  // t_test = Z_MAX || Z2_MAX
439
+              endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
440
+              endstop_hit_bits |= BIT(Z_MIN);
441
+              if (!performing_homing || (z_test == 0x3))  //if not performing home or if both endstops were trigged during homing...
442
+                step_events_completed = current_block->step_event_count;
443
+            }
444
+
445
+          #else // !Z_DUAL_ENDSTOPS
446
+
447
+            UPDATE_ENDSTOP(Z, MAX);
448
+
449
+          #endif // !Z_DUAL_ENDSTOPS
450
+        #endif // Z_MAX_PIN
451
+        
452
+        #ifdef Z_PROBE_ENDSTOP
453
+          UPDATE_ENDSTOP(Z, PROBE);
454
+          
455
+          if (TEST_ENDSTOP(Z_PROBE))
456
+          {
457
+            endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
458
+            endstop_hit_bits |= BIT(Z_PROBE);
459
+          }
460
+        #endif
461
+      }
462
+  old_endstop_bits = current_endstop_bits;
463
+}
464
+
289
 //         __________________________
465
 //         __________________________
290
 //        /|                        |\     _________________         ^
466
 //        /|                        |\     _________________         ^
291
 //       / |                        | \   /|               |\        |
467
 //       / |                        | \   /|               |\        |
429
 // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
605
 // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
430
 ISR(TIMER1_COMPA_vect) {
606
 ISR(TIMER1_COMPA_vect) {
431
 
607
 
432
-  if (cleaning_buffer_counter)
433
-  {
608
+  if (cleaning_buffer_counter) {
434
     current_block = NULL;
609
     current_block = NULL;
435
     plan_discard_current_block();
610
     plan_discard_current_block();
436
     #ifdef SD_FINISHED_RELEASECOMMAND
611
     #ifdef SD_FINISHED_RELEASECOMMAND
471
 
646
 
472
   if (current_block != NULL) {
647
   if (current_block != NULL) {
473
 
648
 
474
-    // Check endstops
475
-    if (check_endstops) {
476
-      
477
-      #ifdef Z_DUAL_ENDSTOPS
478
-        uint16_t
479
-      #else
480
-        byte
481
-      #endif
482
-          current_endstop_bits = 0;
483
-
484
-      #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
485
-      #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
486
-      #define _AXIS(AXIS) AXIS ##_AXIS
487
-      #define _ENDSTOP_HIT(AXIS) endstop_hit_bits |= BIT(_ENDSTOP(AXIS, MIN))
488
-      #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
489
-
490
-      // SET_ENDSTOP_BIT: set the current endstop bits for an endstop to its status
491
-      #define SET_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT(current_endstop_bits, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
492
-      // COPY_BIT: copy the value of COPY_BIT to BIT in bits
493
-      #define COPY_BIT(bits, COPY_BIT, BIT) SET_BIT(bits, BIT, TEST(bits, COPY_BIT))
494
-      // TEST_ENDSTOP: test the old and the current status of an endstop
495
-      #define TEST_ENDSTOP(ENDSTOP) (TEST(current_endstop_bits, ENDSTOP) && TEST(old_endstop_bits, ENDSTOP))
496
-
497
-      #define UPDATE_ENDSTOP(AXIS,MINMAX) \
498
-        SET_ENDSTOP_BIT(AXIS, MINMAX); \
499
-        if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX))  && (current_block->steps[_AXIS(AXIS)] > 0)) { \
500
-          endstops_trigsteps[_AXIS(AXIS)] = count_position[_AXIS(AXIS)]; \
501
-          _ENDSTOP_HIT(AXIS); \
502
-          step_events_completed = current_block->step_event_count; \
503
-        }
504
-      
505
-      #ifdef COREXY
506
-        // Head direction in -X axis for CoreXY bots.
507
-        // If DeltaX == -DeltaY, the movement is only in Y axis
508
-        if ((current_block->steps[A_AXIS] != current_block->steps[B_AXIS]) || (TEST(out_bits, A_AXIS) == TEST(out_bits, B_AXIS))) {
509
-          if (TEST(out_bits, X_HEAD))
510
-      #elif defined(COREXZ)
511
-        // Head direction in -X axis for CoreXZ bots.
512
-        // If DeltaX == -DeltaZ, the movement is only in Z axis
513
-        if ((current_block->steps[A_AXIS] != current_block->steps[C_AXIS]) || (TEST(out_bits, A_AXIS) == TEST(out_bits, C_AXIS))) {
514
-          if (TEST(out_bits, X_HEAD))
515
-      #else
516
-          if (TEST(out_bits, X_AXIS))   // stepping along -X axis (regular Cartesian bot)
517
-      #endif
518
-          { // -direction
519
-            #ifdef DUAL_X_CARRIAGE
520
-              // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
521
-              if ((current_block->active_extruder == 0 && X_HOME_DIR == -1) || (current_block->active_extruder != 0 && X2_HOME_DIR == -1))
522
-            #endif
523
-              {
524
-                #if HAS_X_MIN
525
-                  UPDATE_ENDSTOP(X, MIN);
526
-                #endif
527
-              }
528
-          }
529
-          else { // +direction
530
-            #ifdef DUAL_X_CARRIAGE
531
-              // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
532
-              if ((current_block->active_extruder == 0 && X_HOME_DIR == 1) || (current_block->active_extruder != 0 && X2_HOME_DIR == 1))
533
-            #endif
534
-              {
535
-                #if HAS_X_MAX
536
-                  UPDATE_ENDSTOP(X, MAX);
537
-                #endif
538
-              }
539
-          }
540
-      #if defined(COREXY) || defined(COREXZ)
541
-        }
542
-      #endif
543
-
544
-      #ifdef COREXY
545
-        // Head direction in -Y axis for CoreXY bots.
546
-        // If DeltaX == DeltaY, the movement is only in X axis
547
-        if ((current_block->steps[A_AXIS] != current_block->steps[B_AXIS]) || (TEST(out_bits, A_AXIS) != TEST(out_bits, B_AXIS))) {
548
-          if (TEST(out_bits, Y_HEAD))
549
-      #else
550
-          if (TEST(out_bits, Y_AXIS))   // -direction
551
-      #endif
552
-          { // -direction
553
-            #if HAS_Y_MIN
554
-              UPDATE_ENDSTOP(Y, MIN);
555
-            #endif
556
-          }
557
-          else { // +direction
558
-            #if HAS_Y_MAX
559
-              UPDATE_ENDSTOP(Y, MAX);
560
-            #endif
561
-          }
562
-      #if defined(COREXY) || defined(COREXZ)
563
-        }
564
-      #endif
565
-
566
-      #ifdef COREXZ
567
-        // Head direction in -Z axis for CoreXZ bots.
568
-        // If DeltaX == DeltaZ, the movement is only in X axis
569
-        if ((current_block->steps[A_AXIS] != current_block->steps[C_AXIS]) || (TEST(out_bits, A_AXIS) != TEST(out_bits, C_AXIS))) {
570
-          if (TEST(out_bits, Z_HEAD))
571
-      #else
572
-          if (TEST(out_bits, Z_AXIS))
573
-      #endif
574
-          { // z -direction
575
-            #if HAS_Z_MIN
576
-
577
-              #ifdef Z_DUAL_ENDSTOPS
578
-                SET_ENDSTOP_BIT(Z, MIN);
579
-                  #if HAS_Z2_MIN
580
-                    SET_ENDSTOP_BIT(Z2, MIN);
581
-                  #else
582
-                    COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN);
583
-                  #endif
584
-
585
-                byte z_test = TEST_ENDSTOP(Z_MIN) << 0 + TEST_ENDSTOP(Z2_MIN) << 1; // bit 0 for Z, bit 1 for Z2
586
-
587
-                if (z_test && current_block->steps[Z_AXIS] > 0) { // z_test = Z_MIN || Z2_MIN
588
-                  endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
589
-                  endstop_hit_bits |= BIT(Z_MIN);
590
-                  if (!performing_homing || (z_test == 0x3))  //if not performing home or if both endstops were trigged during homing...
591
-                    step_events_completed = current_block->step_event_count;
592
-                }
593
-              #else // !Z_DUAL_ENDSTOPS
594
-
595
-                UPDATE_ENDSTOP(Z, MIN);
596
-              #endif // !Z_DUAL_ENDSTOPS
597
-            #endif // Z_MIN_PIN
598
-
599
-            #ifdef Z_PROBE_ENDSTOP
600
-              UPDATE_ENDSTOP(Z, PROBE);
601
-
602
-              if (TEST_ENDSTOP(Z_PROBE))
603
-              {
604
-                endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
605
-                endstop_hit_bits |= BIT(Z_PROBE);
606
-              }
607
-            #endif
608
-          }
609
-          else { // z +direction
610
-            #if HAS_Z_MAX
611
-
612
-              #ifdef Z_DUAL_ENDSTOPS
613
-
614
-                SET_ENDSTOP_BIT(Z, MAX);
615
-                  #if HAS_Z2_MAX
616
-                    SET_ENDSTOP_BIT(Z2, MAX);
617
-                  #else
618
-                    COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX)
619
-                  #endif
620
-
621
-                byte z_test = TEST_ENDSTOP(Z_MAX) << 0 + TEST_ENDSTOP(Z2_MAX) << 1; // bit 0 for Z, bit 1 for Z2
622
-
623
-                if (z_test && current_block->steps[Z_AXIS] > 0) {  // t_test = Z_MAX || Z2_MAX
624
-                  endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
625
-                  endstop_hit_bits |= BIT(Z_MIN);
626
-                  if (!performing_homing || (z_test == 0x3))  //if not performing home or if both endstops were trigged during homing...
627
-                    step_events_completed = current_block->step_event_count;
628
-                }
629
-
630
-              #else // !Z_DUAL_ENDSTOPS
631
-
632
-                UPDATE_ENDSTOP(Z, MAX);
633
-
634
-              #endif // !Z_DUAL_ENDSTOPS
635
-            #endif // Z_MAX_PIN
636
-            
637
-            #ifdef Z_PROBE_ENDSTOP
638
-              UPDATE_ENDSTOP(Z, PROBE);
639
-              
640
-              if (TEST_ENDSTOP(Z_PROBE))
641
-              {
642
-                endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
643
-                endstop_hit_bits |= BIT(Z_PROBE);
644
-              }
645
-            #endif
646
-          }
647
-      old_endstop_bits = current_endstop_bits;
648
-    }
649
-
649
+    // Update endstops state, if enabled
650
+    if (check_endstops) update_endstops();
650
 
651
 
651
     // Take multiple steps per interrupt (For high speed moves)
652
     // Take multiple steps per interrupt (For high speed moves)
652
     for (int8_t i = 0; i < step_loops; i++) {
653
     for (int8_t i = 0; i < step_loops; i++) {

Notiek ielāde…
Atcelt
Saglabāt