Subversion Repositories Tronxy-X3A-Marlin

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ron 1
/**
2
 * Marlin 3D Printer Firmware
3
 * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
 *
5
 * Based on Sprinter and grbl.
6
 * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
 
23
/**
24
 * endstops.cpp - A singleton object to manage endstops
25
 */
26
 
27
#include "Marlin.h"
28
#include "cardreader.h"
29
#include "endstops.h"
30
#include "temperature.h"
31
#include "stepper.h"
32
#include "ultralcd.h"
33
 
34
#if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
35
  #include "endstop_interrupts.h"
36
#endif
37
 
38
Endstops endstops;
39
 
40
// public:
41
 
42
bool Endstops::enabled, Endstops::enabled_globally; // Initialized by settings.load()
43
volatile uint8_t Endstops::hit_state;
44
 
45
Endstops::esbits_t Endstops::live_state = 0;
46
 
47
#if ENABLED(ENDSTOP_NOISE_FILTER)
48
  Endstops::esbits_t Endstops::validated_live_state;
49
  uint8_t Endstops::endstop_poll_count;
50
#endif
51
 
52
#if HAS_BED_PROBE
53
  volatile bool Endstops::z_probe_enabled = false;
54
#endif
55
 
56
// Initialized by settings.load()
57
#if ENABLED(X_DUAL_ENDSTOPS)
58
  float Endstops::x_endstop_adj;
59
#endif
60
#if ENABLED(Y_DUAL_ENDSTOPS)
61
  float Endstops::y_endstop_adj;
62
#endif
63
#if ENABLED(Z_DUAL_ENDSTOPS)
64
  float Endstops::z_endstop_adj;
65
#endif
66
 
67
/**
68
 * Class and Instance Methods
69
 */
70
 
71
void Endstops::init() {
72
 
73
  #if HAS_X_MIN
74
    #if ENABLED(ENDSTOPPULLUP_XMIN)
75
      SET_INPUT_PULLUP(X_MIN_PIN);
76
    #else
77
      SET_INPUT(X_MIN_PIN);
78
    #endif
79
  #endif
80
 
81
  #if HAS_X2_MIN
82
    #if ENABLED(ENDSTOPPULLUP_XMIN)
83
      SET_INPUT_PULLUP(X2_MIN_PIN);
84
    #else
85
      SET_INPUT(X2_MIN_PIN);
86
    #endif
87
  #endif
88
 
89
  #if HAS_Y_MIN
90
    #if ENABLED(ENDSTOPPULLUP_YMIN)
91
      SET_INPUT_PULLUP(Y_MIN_PIN);
92
    #else
93
      SET_INPUT(Y_MIN_PIN);
94
    #endif
95
  #endif
96
 
97
  #if HAS_Y2_MIN
98
    #if ENABLED(ENDSTOPPULLUP_YMIN)
99
      SET_INPUT_PULLUP(Y2_MIN_PIN);
100
    #else
101
      SET_INPUT(Y2_MIN_PIN);
102
    #endif
103
  #endif
104
 
105
  #if HAS_Z_MIN
106
    #if ENABLED(ENDSTOPPULLUP_ZMIN)
107
      SET_INPUT_PULLUP(Z_MIN_PIN);
108
    #else
109
      SET_INPUT(Z_MIN_PIN);
110
    #endif
111
  #endif
112
 
113
  #if HAS_Z2_MIN
114
    #if ENABLED(ENDSTOPPULLUP_ZMIN)
115
      SET_INPUT_PULLUP(Z2_MIN_PIN);
116
    #else
117
      SET_INPUT(Z2_MIN_PIN);
118
    #endif
119
  #endif
120
 
121
  #if HAS_X_MAX
122
    #if ENABLED(ENDSTOPPULLUP_XMAX)
123
      SET_INPUT_PULLUP(X_MAX_PIN);
124
    #else
125
      SET_INPUT(X_MAX_PIN);
126
    #endif
127
  #endif
128
 
129
  #if HAS_X2_MAX
130
    #if ENABLED(ENDSTOPPULLUP_XMAX)
131
      SET_INPUT_PULLUP(X2_MAX_PIN);
132
    #else
133
      SET_INPUT(X2_MAX_PIN);
134
    #endif
135
  #endif
136
 
137
  #if HAS_Y_MAX
138
    #if ENABLED(ENDSTOPPULLUP_YMAX)
139
      SET_INPUT_PULLUP(Y_MAX_PIN);
140
    #else
141
      SET_INPUT(Y_MAX_PIN);
142
    #endif
143
  #endif
144
 
145
  #if HAS_Y2_MAX
146
    #if ENABLED(ENDSTOPPULLUP_YMAX)
147
      SET_INPUT_PULLUP(Y2_MAX_PIN);
148
    #else
149
      SET_INPUT(Y2_MAX_PIN);
150
    #endif
151
  #endif
152
 
153
  #if HAS_Z_MAX
154
    #if ENABLED(ENDSTOPPULLUP_ZMAX)
155
      SET_INPUT_PULLUP(Z_MAX_PIN);
156
    #else
157
      SET_INPUT(Z_MAX_PIN);
158
    #endif
159
  #endif
160
 
161
  #if HAS_Z2_MAX
162
    #if ENABLED(ENDSTOPPULLUP_ZMAX)
163
      SET_INPUT_PULLUP(Z2_MAX_PIN);
164
    #else
165
      SET_INPUT(Z2_MAX_PIN);
166
    #endif
167
  #endif
168
 
169
  #if ENABLED(Z_MIN_PROBE_ENDSTOP)
170
    #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
171
      SET_INPUT_PULLUP(Z_MIN_PROBE_PIN);
172
    #else
173
      SET_INPUT(Z_MIN_PROBE_PIN);
174
    #endif
175
  #endif
176
 
177
  #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
178
    setup_endstop_interrupts();
179
  #endif
180
 
181
  // Enable endstops
182
  enable_globally(
183
    #if ENABLED(ENDSTOPS_ALWAYS_ON_DEFAULT)
184
      true
185
    #else
186
      false
187
    #endif
188
  );
189
 
190
} // Endstops::init
191
 
192
// Called at ~1KHz from Temperature ISR: Poll endstop state if required
193
void Endstops::poll() {
194
 
195
  #if ENABLED(PINS_DEBUGGING)
196
    run_monitor();  // report changes in endstop status
197
  #endif
198
 
199
  #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE) && ENABLED(ENDSTOP_NOISE_FILTER)
200
    if (endstop_poll_count) update();
201
  #elif DISABLED(ENDSTOP_INTERRUPTS_FEATURE) || ENABLED(ENDSTOP_NOISE_FILTER)
202
    update();
203
  #endif
204
}
205
 
206
void Endstops::enable_globally(const bool onoff) {
207
  enabled_globally = enabled = onoff;
208
 
209
  #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
210
    update();
211
  #endif
212
}
213
 
214
// Enable / disable endstop checking
215
void Endstops::enable(const bool onoff) {
216
  enabled = onoff;
217
 
218
  #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
219
    update();
220
  #endif
221
}
222
 
223
// Disable / Enable endstops based on ENSTOPS_ONLY_FOR_HOMING and global enable
224
void Endstops::not_homing() {
225
  enabled = enabled_globally;
226
 
227
  #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
228
    update();
229
  #endif
230
}
231
 
232
#if ENABLED(VALIDATE_HOMING_ENDSTOPS)
233
  // If the last move failed to trigger an endstop, call kill
234
  void Endstops::validate_homing_move() {
235
    if (trigger_state()) hit_on_purpose();
236
    else kill(PSTR(MSG_ERR_HOMING_FAILED));
237
  }
238
#endif
239
 
240
// Enable / disable endstop z-probe checking
241
#if HAS_BED_PROBE
242
  void Endstops::enable_z_probe(const bool onoff) {
243
    z_probe_enabled = onoff;
244
 
245
    #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
246
      update();
247
    #endif
248
  }
249
#endif
250
 
251
#if ENABLED(PINS_DEBUGGING)
252
  void Endstops::run_monitor() {
253
    if (!monitor_flag) return;
254
    static uint8_t monitor_count = 16;  // offset this check from the others
255
    monitor_count += _BV(1);            //  15 Hz
256
    monitor_count &= 0x7F;
257
    if (!monitor_count) monitor();      // report changes in endstop status
258
  }
259
#endif
260
 
261
void Endstops::event_handler() {
262
  static uint8_t prev_hit_state; // = 0
263
  if (hit_state && hit_state != prev_hit_state) {
264
    #if ENABLED(ULTRA_LCD)
265
      char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
266
      #define _SET_STOP_CHAR(A,C) (chr## A = C)
267
    #else
268
      #define _SET_STOP_CHAR(A,C) ;
269
    #endif
270
 
271
    #define _ENDSTOP_HIT_ECHO(A,C) do{ \
272
      SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", planner.triggered_position_mm(_AXIS(A))); \
273
      _SET_STOP_CHAR(A,C); }while(0)
274
 
275
    #define _ENDSTOP_HIT_TEST(A,C) \
276
      if (TEST(hit_state, A ##_MIN) || TEST(hit_state, A ##_MAX)) \
277
        _ENDSTOP_HIT_ECHO(A,C)
278
 
279
    #define ENDSTOP_HIT_TEST_X() _ENDSTOP_HIT_TEST(X,'X')
280
    #define ENDSTOP_HIT_TEST_Y() _ENDSTOP_HIT_TEST(Y,'Y')
281
    #define ENDSTOP_HIT_TEST_Z() _ENDSTOP_HIT_TEST(Z,'Z')
282
 
283
    SERIAL_ECHO_START();
284
    SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
285
    ENDSTOP_HIT_TEST_X();
286
    ENDSTOP_HIT_TEST_Y();
287
    ENDSTOP_HIT_TEST_Z();
288
 
289
    #if ENABLED(Z_MIN_PROBE_ENDSTOP)
290
      #define P_AXIS Z_AXIS
291
      if (TEST(hit_state, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
292
    #endif
293
    SERIAL_EOL();
294
 
295
    #if ENABLED(ULTRA_LCD)
296
      lcd_status_printf_P(0, PSTR(MSG_LCD_ENDSTOPS " %c %c %c %c"), chrX, chrY, chrZ, chrP);
297
    #endif
298
 
299
    #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && ENABLED(SDSUPPORT)
300
      if (planner.abort_on_endstop_hit) {
301
        card.sdprinting = false;
302
        card.closefile();
303
        quickstop_stepper();
304
        thermalManager.disable_all_heaters(); // switch off all heaters.
305
      }
306
    #endif
307
  }
308
  prev_hit_state = hit_state;
309
} // Endstops::report_state
310
 
311
static void print_es_state(const bool is_hit, const char * const label=NULL) {
312
  if (label) serialprintPGM(label);
313
  SERIAL_PROTOCOLPGM(": ");
314
  serialprintPGM(is_hit ? PSTR(MSG_ENDSTOP_HIT) : PSTR(MSG_ENDSTOP_OPEN));
315
  SERIAL_EOL();
316
}
317
 
318
void _O2 Endstops::M119() {
319
  #if ENABLED(BLTOUCH)
320
    extern void _bltouch_set_SW_mode();
321
    _bltouch_set_SW_mode();
322
  #endif
323
  SERIAL_PROTOCOLLNPGM(MSG_M119_REPORT);
324
  #define ES_REPORT(S) print_es_state(READ(S##_PIN) != S##_ENDSTOP_INVERTING, PSTR(MSG_##S))
325
  #if HAS_X_MIN
326
    ES_REPORT(X_MIN);
327
  #endif
328
  #if HAS_X2_MIN
329
    ES_REPORT(X2_MIN);
330
  #endif
331
  #if HAS_X_MAX
332
    ES_REPORT(X_MAX);
333
  #endif
334
  #if HAS_X2_MAX
335
    ES_REPORT(X2_MAX);
336
  #endif
337
  #if HAS_Y_MIN
338
    ES_REPORT(Y_MIN);
339
  #endif
340
  #if HAS_Y2_MIN
341
    ES_REPORT(Y2_MIN);
342
  #endif
343
  #if HAS_Y_MAX
344
    ES_REPORT(Y_MAX);
345
  #endif
346
  #if HAS_Y2_MAX
347
    ES_REPORT(Y2_MAX);
348
  #endif
349
  #if HAS_Z_MIN
350
    ES_REPORT(Z_MIN);
351
  #endif
352
  #if HAS_Z2_MIN
353
    ES_REPORT(Z2_MIN);
354
  #endif
355
  #if HAS_Z_MAX
356
    ES_REPORT(Z_MAX);
357
  #endif
358
  #if HAS_Z2_MAX
359
    ES_REPORT(Z2_MAX);
360
  #endif
361
  #if ENABLED(Z_MIN_PROBE_ENDSTOP)
362
    print_es_state(READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING, PSTR(MSG_Z_PROBE));
363
  #endif
364
  #if ENABLED(FILAMENT_RUNOUT_SENSOR)
365
    #if NUM_RUNOUT_SENSORS == 1
366
      print_es_state(READ(FIL_RUNOUT_PIN) != FIL_RUNOUT_INVERTING, PSTR(MSG_FILAMENT_RUNOUT_SENSOR));
367
    #else
368
      for (uint8_t i = 1; i <= NUM_RUNOUT_SENSORS; i++) {
369
        pin_t pin;
370
        switch (i) {
371
          default: continue;
372
          case 1: pin = FIL_RUNOUT_PIN; break;
373
          case 2: pin = FIL_RUNOUT2_PIN; break;
374
          #if NUM_RUNOUT_SENSORS > 2
375
            case 3: pin = FIL_RUNOUT3_PIN; break;
376
            #if NUM_RUNOUT_SENSORS > 3
377
              case 4: pin = FIL_RUNOUT4_PIN; break;
378
              #if NUM_RUNOUT_SENSORS > 4
379
                case 5: pin = FIL_RUNOUT5_PIN; break;
380
              #endif
381
            #endif
382
          #endif
383
        }
384
        SERIAL_PROTOCOLPGM(MSG_FILAMENT_RUNOUT_SENSOR);
385
        if (i > 1) { SERIAL_CHAR(' '); SERIAL_CHAR('0' + i); }
386
        print_es_state(digitalRead(pin) != FIL_RUNOUT_INVERTING);
387
      }
388
    #endif
389
  #endif
390
  #if ENABLED(BLTOUCH)
391
    extern void _bltouch_reset_SW_mode();
392
    _bltouch_reset_SW_mode();
393
  #endif
394
} // Endstops::M119
395
 
396
// The following routines are called from an ISR context. It could be the temperature ISR, the
397
// endstop ISR or the Stepper ISR.
398
 
399
#define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
400
#define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
401
#define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
402
 
403
// Check endstops - Could be called from Temperature ISR!
404
void Endstops::update() {
405
 
406
  #if DISABLED(ENDSTOP_NOISE_FILTER)
407
    if (!abort_enabled()) return;
408
  #endif
409
 
410
  #define UPDATE_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT_TO(live_state, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
411
  #define COPY_LIVE_STATE(SRC_BIT, DST_BIT) SET_BIT_TO(live_state, DST_BIT, TEST(live_state, SRC_BIT))
412
 
413
  #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && !(CORE_IS_XY || CORE_IS_XZ)
414
    // If G38 command is active check Z_MIN_PROBE for ALL movement
415
    if (G38_move) UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
416
  #endif
417
 
418
  // With Dual X, endstops are only checked in the homing direction for the active extruder
419
  #if ENABLED(DUAL_X_CARRIAGE)
420
    #define E0_ACTIVE stepper.movement_extruder() == 0
421
    #define X_MIN_TEST ((X_HOME_DIR < 0 && E0_ACTIVE) || (X2_HOME_DIR < 0 && !E0_ACTIVE))
422
    #define X_MAX_TEST ((X_HOME_DIR > 0 && E0_ACTIVE) || (X2_HOME_DIR > 0 && !E0_ACTIVE))
423
  #else
424
    #define X_MIN_TEST true
425
    #define X_MAX_TEST true
426
  #endif
427
 
428
  // Use HEAD for core axes, AXIS for others
429
  #if CORE_IS_XY || CORE_IS_XZ
430
    #define X_AXIS_HEAD X_HEAD
431
  #else
432
    #define X_AXIS_HEAD X_AXIS
433
  #endif
434
  #if CORE_IS_XY || CORE_IS_YZ
435
    #define Y_AXIS_HEAD Y_HEAD
436
  #else
437
    #define Y_AXIS_HEAD Y_AXIS
438
  #endif
439
  #if CORE_IS_XZ || CORE_IS_YZ
440
    #define Z_AXIS_HEAD Z_HEAD
441
  #else
442
    #define Z_AXIS_HEAD Z_AXIS
443
  #endif
444
 
445
  /**
446
   * Check and update endstops
447
   */
448
  #if HAS_X_MIN
449
    #if ENABLED(X_DUAL_ENDSTOPS)
450
      UPDATE_ENDSTOP_BIT(X, MIN);
451
      #if HAS_X2_MIN
452
        UPDATE_ENDSTOP_BIT(X2, MIN);
453
      #else
454
        COPY_LIVE_STATE(X_MIN, X2_MIN);
455
      #endif
456
    #else
457
      UPDATE_ENDSTOP_BIT(X, MIN);
458
    #endif
459
  #endif
460
 
461
  #if HAS_X_MAX
462
    #if ENABLED(X_DUAL_ENDSTOPS)
463
      UPDATE_ENDSTOP_BIT(X, MAX);
464
      #if HAS_X2_MAX
465
        UPDATE_ENDSTOP_BIT(X2, MAX);
466
      #else
467
        COPY_LIVE_STATE(X_MAX, X2_MAX);
468
      #endif
469
    #else
470
      UPDATE_ENDSTOP_BIT(X, MAX);
471
    #endif
472
  #endif
473
 
474
  #if HAS_Y_MIN
475
    #if ENABLED(Y_DUAL_ENDSTOPS)
476
      UPDATE_ENDSTOP_BIT(Y, MIN);
477
      #if HAS_Y2_MIN
478
        UPDATE_ENDSTOP_BIT(Y2, MIN);
479
      #else
480
        COPY_LIVE_STATE(Y_MIN, Y2_MIN);
481
      #endif
482
    #else
483
      UPDATE_ENDSTOP_BIT(Y, MIN);
484
    #endif
485
  #endif
486
 
487
  #if HAS_Y_MAX
488
    #if ENABLED(Y_DUAL_ENDSTOPS)
489
      UPDATE_ENDSTOP_BIT(Y, MAX);
490
      #if HAS_Y2_MAX
491
        UPDATE_ENDSTOP_BIT(Y2, MAX);
492
      #else
493
        COPY_LIVE_STATE(Y_MAX, Y2_MAX);
494
      #endif
495
    #else
496
      UPDATE_ENDSTOP_BIT(Y, MAX);
497
    #endif
498
  #endif
499
 
500
  #if HAS_Z_MIN
501
    #if ENABLED(Z_DUAL_ENDSTOPS)
502
      UPDATE_ENDSTOP_BIT(Z, MIN);
503
      #if HAS_Z2_MIN
504
        UPDATE_ENDSTOP_BIT(Z2, MIN);
505
      #else
506
        COPY_LIVE_STATE(Z_MIN, Z2_MIN);
507
      #endif
508
    #elif ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
509
      UPDATE_ENDSTOP_BIT(Z, MIN);
510
    #elif Z_HOME_DIR < 0
511
      UPDATE_ENDSTOP_BIT(Z, MIN);
512
    #endif
513
  #endif
514
 
515
  // When closing the gap check the enabled probe
516
  #if ENABLED(Z_MIN_PROBE_ENDSTOP)
517
    UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
518
  #endif
519
 
520
  #if HAS_Z_MAX
521
    // Check both Z dual endstops
522
    #if ENABLED(Z_DUAL_ENDSTOPS)
523
      UPDATE_ENDSTOP_BIT(Z, MAX);
524
      #if HAS_Z2_MAX
525
        UPDATE_ENDSTOP_BIT(Z2, MAX);
526
      #else
527
        COPY_LIVE_STATE(Z_MAX, Z2_MAX);
528
      #endif
529
    #elif DISABLED(Z_MIN_PROBE_ENDSTOP) || Z_MAX_PIN != Z_MIN_PROBE_PIN
530
      // If this pin isn't the bed probe it's the Z endstop
531
      UPDATE_ENDSTOP_BIT(Z, MAX);
532
    #endif
533
  #endif
534
 
535
  #if ENABLED(ENDSTOP_NOISE_FILTER)
536
    /**
537
     * Filtering out noise on endstops requires a delayed decision. Let's assume, due to noise,
538
     * that 50% of endstop signal samples are good and 50% are bad (assuming normal distribution
539
     * of random noise). Then the first sample has a 50% chance to be good or bad. The 2nd sample
540
     * also has a 50% chance to be good or bad. The chances of 2 samples both being bad becomes
541
     * 50% of 50%, or 25%. That was the previous implementation of Marlin endstop handling. It
542
     * reduces chances of bad readings in half, at the cost of 1 extra sample period, but chances
543
     * still exist. The only way to reduce them further is to increase the number of samples.
544
     * To reduce the chance to 1% (1/128th) requires 7 samples (adding 7ms of delay).
545
     */
546
    static esbits_t old_live_state;
547
    if (old_live_state != live_state) {
548
      endstop_poll_count = 7;
549
      old_live_state = live_state;
550
    }
551
    else if (endstop_poll_count && !--endstop_poll_count)
552
      validated_live_state = live_state;
553
 
554
    if (!abort_enabled()) return;
555
 
556
  #endif
557
 
558
  // Test the current status of an endstop
559
  #define TEST_ENDSTOP(ENDSTOP) (TEST(state(), ENDSTOP))
560
 
561
  // Record endstop was hit
562
  #define _ENDSTOP_HIT(AXIS, MINMAX) SBI(hit_state, _ENDSTOP(AXIS, MINMAX))
563
 
564
  // Call the endstop triggered routine for single endstops
565
  #define PROCESS_ENDSTOP(AXIS,MINMAX) do { \
566
    if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX))) { \
567
      _ENDSTOP_HIT(AXIS, MINMAX); \
568
      planner.endstop_triggered(_AXIS(AXIS)); \
569
    } \
570
  }while(0)
571
 
572
  // Call the endstop triggered routine for dual endstops
573
  #define PROCESS_DUAL_ENDSTOP(AXIS1, AXIS2, MINMAX) do { \
574
    const byte dual_hit = TEST_ENDSTOP(_ENDSTOP(AXIS1, MINMAX)) | (TEST_ENDSTOP(_ENDSTOP(AXIS2, MINMAX)) << 1); \
575
    if (dual_hit) { \
576
      _ENDSTOP_HIT(AXIS1, MINMAX); \
577
      /* if not performing home or if both endstops were trigged during homing... */ \
578
      if (!stepper.homing_dual_axis || dual_hit == 0b11) \
579
        planner.endstop_triggered(_AXIS(AXIS1)); \
580
    } \
581
  }while(0)
582
 
583
  #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && !(CORE_IS_XY || CORE_IS_XZ)
584
    // If G38 command is active check Z_MIN_PROBE for ALL movement
585
    if (G38_move) {
586
      if (TEST_ENDSTOP(_ENDSTOP(Z, MIN_PROBE))) {
587
        if      (stepper.axis_is_moving(X_AXIS)) { _ENDSTOP_HIT(X, MIN); planner.endstop_triggered(X_AXIS); }
588
        else if (stepper.axis_is_moving(Y_AXIS)) { _ENDSTOP_HIT(Y, MIN); planner.endstop_triggered(Y_AXIS); }
589
        else if (stepper.axis_is_moving(Z_AXIS)) { _ENDSTOP_HIT(Z, MIN); planner.endstop_triggered(Z_AXIS); }
590
        G38_endstop_hit = true;
591
      }
592
    }
593
  #endif
594
 
595
  // Now, we must signal, after validation, if an endstop limit is pressed or not
596
  if (stepper.axis_is_moving(X_AXIS)) {
597
    if (stepper.motor_direction(X_AXIS_HEAD)) { // -direction
598
      #if HAS_X_MIN
599
        #if ENABLED(X_DUAL_ENDSTOPS)
600
          PROCESS_DUAL_ENDSTOP(X, X2, MIN);
601
        #else
602
          if (X_MIN_TEST) PROCESS_ENDSTOP(X, MIN);
603
        #endif
604
      #endif
605
    }
606
    else { // +direction
607
      #if HAS_X_MAX
608
        #if ENABLED(X_DUAL_ENDSTOPS)
609
          PROCESS_DUAL_ENDSTOP(X, X2, MAX);
610
        #else
611
          if (X_MAX_TEST) PROCESS_ENDSTOP(X, MAX);
612
        #endif
613
      #endif
614
    }
615
  }
616
 
617
  if (stepper.axis_is_moving(Y_AXIS)) {
618
    if (stepper.motor_direction(Y_AXIS_HEAD)) { // -direction
619
      #if HAS_Y_MIN
620
        #if ENABLED(Y_DUAL_ENDSTOPS)
621
          PROCESS_DUAL_ENDSTOP(Y, Y2, MIN);
622
        #else
623
          PROCESS_ENDSTOP(Y, MIN);
624
        #endif
625
      #endif
626
    }
627
    else { // +direction
628
      #if HAS_Y_MAX
629
        #if ENABLED(Y_DUAL_ENDSTOPS)
630
          PROCESS_DUAL_ENDSTOP(Y, Y2, MAX);
631
        #else
632
          PROCESS_ENDSTOP(Y, MAX);
633
        #endif
634
      #endif
635
    }
636
  }
637
 
638
  if (stepper.axis_is_moving(Z_AXIS)) {
639
    if (stepper.motor_direction(Z_AXIS_HEAD)) { // Z -direction. Gantry down, bed up.
640
      #if HAS_Z_MIN
641
        #if ENABLED(Z_DUAL_ENDSTOPS)
642
          PROCESS_DUAL_ENDSTOP(Z, Z2, MIN);
643
        #else
644
          #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
645
            if (z_probe_enabled) PROCESS_ENDSTOP(Z, MIN);
646
          #elif ENABLED(Z_MIN_PROBE_ENDSTOP)
647
            if (!z_probe_enabled) PROCESS_ENDSTOP(Z, MIN);
648
          #else
649
            PROCESS_ENDSTOP(Z, MIN);
650
          #endif
651
        #endif
652
      #endif
653
 
654
      // When closing the gap check the enabled probe
655
      #if ENABLED(Z_MIN_PROBE_ENDSTOP)
656
        if (z_probe_enabled) PROCESS_ENDSTOP(Z, MIN_PROBE);
657
      #endif
658
    }
659
    else { // Z +direction. Gantry up, bed down.
660
      #if HAS_Z_MAX
661
        #if ENABLED(Z_DUAL_ENDSTOPS)
662
          PROCESS_DUAL_ENDSTOP(Z, Z2, MAX);
663
        #elif DISABLED(Z_MIN_PROBE_ENDSTOP) || Z_MAX_PIN != Z_MIN_PROBE_PIN
664
          // If this pin is not hijacked for the bed probe
665
          // then it belongs to the Z endstop
666
          PROCESS_ENDSTOP(Z, MAX);
667
        #endif
668
      #endif
669
    }
670
  }
671
} // Endstops::update()
672
 
673
#if ENABLED(PINS_DEBUGGING)
674
 
675
  bool Endstops::monitor_flag = false;
676
 
677
  /**
678
   * monitors endstops & Z probe for changes
679
   *
680
   * If a change is detected then the LED is toggled and
681
   * a message is sent out the serial port
682
   *
683
   * Yes, we could miss a rapid back & forth change but
684
   * that won't matter because this is all manual.
685
   *
686
   */
687
  void Endstops::monitor() {
688
 
689
    static uint16_t old_live_state_local = 0;
690
    static uint8_t local_LED_status = 0;
691
    uint16_t live_state_local = 0;
692
 
693
    #if HAS_X_MIN
694
      if (READ(X_MIN_PIN)) SBI(live_state_local, X_MIN);
695
    #endif
696
    #if HAS_X_MAX
697
      if (READ(X_MAX_PIN)) SBI(live_state_local, X_MAX);
698
    #endif
699
    #if HAS_Y_MIN
700
      if (READ(Y_MIN_PIN)) SBI(live_state_local, Y_MIN);
701
    #endif
702
    #if HAS_Y_MAX
703
      if (READ(Y_MAX_PIN)) SBI(live_state_local, Y_MAX);
704
    #endif
705
    #if HAS_Z_MIN
706
      if (READ(Z_MIN_PIN)) SBI(live_state_local, Z_MIN);
707
    #endif
708
    #if HAS_Z_MAX
709
      if (READ(Z_MAX_PIN)) SBI(live_state_local, Z_MAX);
710
    #endif
711
    #if HAS_Z_MIN_PROBE_PIN
712
      if (READ(Z_MIN_PROBE_PIN)) SBI(live_state_local, Z_MIN_PROBE);
713
    #endif
714
    #if HAS_X2_MIN
715
      if (READ(X2_MIN_PIN)) SBI(live_state_local, X2_MIN);
716
    #endif
717
    #if HAS_X2_MAX
718
      if (READ(X2_MAX_PIN)) SBI(live_state_local, X2_MAX);
719
    #endif
720
    #if HAS_Y2_MIN
721
      if (READ(Y2_MIN_PIN)) SBI(live_state_local, Y2_MIN);
722
    #endif
723
    #if HAS_Y2_MAX
724
      if (READ(Y2_MAX_PIN)) SBI(live_state_local, Y2_MAX);
725
    #endif
726
    #if HAS_Z2_MIN
727
      if (READ(Z2_MIN_PIN)) SBI(live_state_local, Z2_MIN);
728
    #endif
729
    #if HAS_Z2_MAX
730
      if (READ(Z2_MAX_PIN)) SBI(live_state_local, Z2_MAX);
731
    #endif
732
 
733
    uint16_t endstop_change = live_state_local ^ old_live_state_local;
734
 
735
    if (endstop_change) {
736
      #if HAS_X_MIN
737
        if (TEST(endstop_change, X_MIN)) SERIAL_PROTOCOLPAIR("  X_MIN:", TEST(live_state_local, X_MIN));
738
      #endif
739
      #if HAS_X_MAX
740
        if (TEST(endstop_change, X_MAX)) SERIAL_PROTOCOLPAIR("  X_MAX:", TEST(live_state_local, X_MAX));
741
      #endif
742
      #if HAS_Y_MIN
743
        if (TEST(endstop_change, Y_MIN)) SERIAL_PROTOCOLPAIR("  Y_MIN:", TEST(live_state_local, Y_MIN));
744
      #endif
745
      #if HAS_Y_MAX
746
        if (TEST(endstop_change, Y_MAX)) SERIAL_PROTOCOLPAIR("  Y_MAX:", TEST(live_state_local, Y_MAX));
747
      #endif
748
      #if HAS_Z_MIN
749
        if (TEST(endstop_change, Z_MIN)) SERIAL_PROTOCOLPAIR("  Z_MIN:", TEST(live_state_local, Z_MIN));
750
      #endif
751
      #if HAS_Z_MAX
752
        if (TEST(endstop_change, Z_MAX)) SERIAL_PROTOCOLPAIR("  Z_MAX:", TEST(live_state_local, Z_MAX));
753
      #endif
754
      #if HAS_Z_MIN_PROBE_PIN
755
        if (TEST(endstop_change, Z_MIN_PROBE)) SERIAL_PROTOCOLPAIR("  PROBE:", TEST(live_state_local, Z_MIN_PROBE));
756
      #endif
757
      #if HAS_X2_MIN
758
        if (TEST(endstop_change, X2_MIN)) SERIAL_PROTOCOLPAIR("  X2_MIN:", TEST(live_state_local, X2_MIN));
759
      #endif
760
      #if HAS_X2_MAX
761
        if (TEST(endstop_change, X2_MAX)) SERIAL_PROTOCOLPAIR("  X2_MAX:", TEST(live_state_local, X2_MAX));
762
      #endif
763
      #if HAS_Y2_MIN
764
        if (TEST(endstop_change, Y2_MIN)) SERIAL_PROTOCOLPAIR("  Y2_MIN:", TEST(live_state_local, Y2_MIN));
765
      #endif
766
      #if HAS_Y2_MAX
767
        if (TEST(endstop_change, Y2_MAX)) SERIAL_PROTOCOLPAIR("  Y2_MAX:", TEST(live_state_local, Y2_MAX));
768
      #endif
769
      #if HAS_Z2_MIN
770
        if (TEST(endstop_change, Z2_MIN)) SERIAL_PROTOCOLPAIR("  Z2_MIN:", TEST(live_state_local, Z2_MIN));
771
      #endif
772
      #if HAS_Z2_MAX
773
        if (TEST(endstop_change, Z2_MAX)) SERIAL_PROTOCOLPAIR("  Z2_MAX:", TEST(live_state_local, Z2_MAX));
774
      #endif
775
      SERIAL_PROTOCOLPGM("\n\n");
776
      analogWrite(LED_PIN, local_LED_status);
777
      local_LED_status ^= 255;
778
      old_live_state_local = live_state_local;
779
    }
780
  }
781
 
782
#endif // PINS_DEBUGGING