2016-04-27 09:15:20 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2016-04-27 09:15:20 -05:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2016-04-27 09:15:20 -05:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-07-22 22:20:14 -05:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2016-04-27 09:15:20 -05:00
|
|
|
*
|
|
|
|
*/
|
2018-11-04 02:25:55 -06:00
|
|
|
#pragma once
|
2016-04-27 09:15:20 -05:00
|
|
|
|
|
|
|
/**
|
2018-03-10 05:56:04 -06:00
|
|
|
* endstops.h - manages endstops
|
2016-04-27 09:15:20 -05:00
|
|
|
*/
|
|
|
|
|
2017-09-18 05:55:09 -05:00
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2021-05-18 22:53:52 -05:00
|
|
|
#define __ES_ITEM(N) N,
|
|
|
|
#define _ES_ITEM(K,N) TERN_(K,DEFER4(__ES_ITEM)(N))
|
|
|
|
|
2018-03-06 22:35:22 -06:00
|
|
|
enum EndstopEnum : char {
|
2021-05-18 22:53:52 -05:00
|
|
|
_ES_ITEM(HAS_X_M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y_MIN)
|
2021-05-22 17:35:07 -05:00
|
|
|
#define Z_ENDSTOP TERN(Z_HOME_TO_MAX, Z_MAX, TERN(HOMING_Z_WITH_PROBE, Z_MIN_PROBE, Z_MIN))
|
2020-07-12 13:16:56 -05:00
|
|
|
|
2021-05-18 22:53:52 -05:00
|
|
|
#undef __ES_ITEM
|
|
|
|
#undef _ES_ITEM
|
|
|
|
|
2016-04-27 09:15:20 -05:00
|
|
|
class Endstops {
|
|
|
|
public:
|
2021-05-18 22:53:52 -05:00
|
|
|
|
|
|
|
typedef IF<(NUM_ENDSTOP_STATES > 8), uint16_t, uint8_t>::type endstop_mask_t;
|
|
|
|
|
|
|
|
#if ENABLED(X_DUAL_ENDSTOPS)
|
|
|
|
static float x2_endstop_adj;
|
|
|
|
#endif
|
|
|
|
#if ENABLED(Y_DUAL_ENDSTOPS)
|
|
|
|
static float y2_endstop_adj;
|
|
|
|
#endif
|
|
|
|
#if ENABLED(Z_MULTI_ENDSTOPS)
|
|
|
|
static float z2_endstop_adj;
|
|
|
|
#endif
|
|
|
|
#if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 3
|
|
|
|
static float z3_endstop_adj;
|
|
|
|
#endif
|
|
|
|
#if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 4
|
|
|
|
static float z4_endstop_adj;
|
2016-04-27 09:15:20 -05:00
|
|
|
#endif
|
2017-09-18 05:55:09 -05:00
|
|
|
|
2018-05-21 15:51:38 -05:00
|
|
|
private:
|
2018-09-24 15:56:01 -05:00
|
|
|
static bool enabled, enabled_globally;
|
2021-05-18 22:53:52 -05:00
|
|
|
static endstop_mask_t live_state;
|
|
|
|
static volatile endstop_mask_t hit_state; // Use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT index
|
2018-06-21 20:14:16 -05:00
|
|
|
|
2018-09-24 20:59:12 -05:00
|
|
|
#if ENDSTOP_NOISE_THRESHOLD
|
2021-05-18 22:53:52 -05:00
|
|
|
static endstop_mask_t validated_live_state;
|
2018-05-21 15:51:38 -05:00
|
|
|
static uint8_t endstop_poll_count; // Countdown from threshold for polling
|
|
|
|
#endif
|
2016-07-19 08:31:09 -05:00
|
|
|
|
2018-05-21 15:51:38 -05:00
|
|
|
public:
|
2018-05-16 02:08:43 -05:00
|
|
|
Endstops() {};
|
2016-04-27 09:15:20 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the endstop pins
|
|
|
|
*/
|
2017-09-06 06:28:32 -05:00
|
|
|
static void init();
|
2016-04-27 09:15:20 -05:00
|
|
|
|
2018-05-16 02:08:43 -05:00
|
|
|
/**
|
2018-06-21 20:14:16 -05:00
|
|
|
* Are endstops or the probe set to abort the move?
|
2018-05-16 02:08:43 -05:00
|
|
|
*/
|
2018-06-21 20:14:16 -05:00
|
|
|
FORCE_INLINE static bool abort_enabled() {
|
2020-04-22 16:35:03 -05:00
|
|
|
return enabled || TERN0(HAS_BED_PROBE, z_probe_enabled);
|
2018-06-21 20:14:16 -05:00
|
|
|
}
|
2018-05-16 02:08:43 -05:00
|
|
|
|
2019-02-06 17:20:17 -06:00
|
|
|
static inline bool global_enabled() { return enabled_globally; }
|
|
|
|
|
2018-05-16 02:08:43 -05:00
|
|
|
/**
|
|
|
|
* Periodic call to poll endstops if required. Called from temperature ISR
|
|
|
|
*/
|
|
|
|
static void poll();
|
|
|
|
|
2016-04-27 09:15:20 -05:00
|
|
|
/**
|
2018-06-21 20:14:16 -05:00
|
|
|
* Update endstops bits from the pins. Apply filtering to get a verified state.
|
|
|
|
* If abort_enabled() and moving towards a triggered switch, abort the current move.
|
|
|
|
* Called from ISR contexts.
|
2016-04-27 09:15:20 -05:00
|
|
|
*/
|
2016-05-26 13:01:20 -05:00
|
|
|
static void update();
|
2016-04-27 09:15:20 -05:00
|
|
|
|
2018-05-21 15:51:38 -05:00
|
|
|
/**
|
|
|
|
* Get Endstop hit state.
|
|
|
|
*/
|
2021-05-18 22:53:52 -05:00
|
|
|
FORCE_INLINE static endstop_mask_t trigger_state() { return hit_state; }
|
2018-05-21 15:51:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get current endstops state
|
|
|
|
*/
|
2021-05-18 22:53:52 -05:00
|
|
|
FORCE_INLINE static endstop_mask_t state() {
|
2018-06-01 19:02:22 -05:00
|
|
|
return
|
2018-09-24 20:59:12 -05:00
|
|
|
#if ENDSTOP_NOISE_THRESHOLD
|
2018-06-01 19:02:22 -05:00
|
|
|
validated_live_state
|
|
|
|
#else
|
|
|
|
live_state
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
}
|
2018-05-21 15:51:38 -05:00
|
|
|
|
2020-12-27 16:21:01 -06:00
|
|
|
static inline bool probe_switch_activated() {
|
|
|
|
return (true
|
|
|
|
#if ENABLED(PROBE_ACTIVATION_SWITCH)
|
|
|
|
&& READ(PROBE_ACTIVATION_SWITCH_PIN) == PROBE_ACTIVATION_SWITCH_STATE
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-27 09:15:20 -05:00
|
|
|
/**
|
2018-05-24 19:28:15 -05:00
|
|
|
* Report endstop hits to serial. Called from loop().
|
2016-04-27 09:15:20 -05:00
|
|
|
*/
|
2018-07-11 17:33:26 -05:00
|
|
|
static void event_handler();
|
2016-04-27 09:15:20 -05:00
|
|
|
|
2016-04-27 16:46:24 -05:00
|
|
|
/**
|
2019-10-30 15:07:24 -05:00
|
|
|
* Report endstop states in response to M119
|
2016-04-27 16:46:24 -05:00
|
|
|
*/
|
2019-10-30 15:07:24 -05:00
|
|
|
static void report_states();
|
2016-04-27 16:46:24 -05:00
|
|
|
|
2016-04-27 09:15:20 -05:00
|
|
|
// Enable / disable endstop checking globally
|
2018-05-16 02:08:43 -05:00
|
|
|
static void enable_globally(const bool onoff=true);
|
2016-04-27 09:15:20 -05:00
|
|
|
|
|
|
|
// Enable / disable endstop checking
|
2018-05-16 02:08:43 -05:00
|
|
|
static void enable(const bool onoff=true);
|
2016-04-27 09:15:20 -05:00
|
|
|
|
|
|
|
// Disable / Enable endstops based on ENSTOPS_ONLY_FOR_HOMING and global enable
|
2018-05-16 02:08:43 -05:00
|
|
|
static void not_homing();
|
2016-04-27 09:15:20 -05:00
|
|
|
|
2018-08-06 23:11:37 -05:00
|
|
|
#if ENABLED(VALIDATE_HOMING_ENDSTOPS)
|
|
|
|
// If the last move failed to trigger an endstop, call kill
|
|
|
|
static void validate_homing_move();
|
|
|
|
#else
|
|
|
|
FORCE_INLINE static void validate_homing_move() { hit_on_purpose(); }
|
|
|
|
#endif
|
2018-06-30 21:54:07 -05:00
|
|
|
|
2016-04-27 09:15:20 -05:00
|
|
|
// Clear endstops (i.e., they were hit intentionally) to suppress the report
|
2018-05-24 19:28:15 -05:00
|
|
|
FORCE_INLINE static void hit_on_purpose() { hit_state = 0; }
|
2016-04-27 09:15:20 -05:00
|
|
|
|
|
|
|
// Enable / disable endstop z-probe checking
|
2016-05-03 22:15:18 -05:00
|
|
|
#if HAS_BED_PROBE
|
2016-05-26 13:01:20 -05:00
|
|
|
static volatile bool z_probe_enabled;
|
2018-06-13 01:22:02 -05:00
|
|
|
static void enable_z_probe(const bool onoff=true);
|
2016-04-27 09:15:20 -05:00
|
|
|
#endif
|
2016-04-28 04:15:53 -05:00
|
|
|
|
2018-11-13 04:31:58 -06:00
|
|
|
static void resync();
|
|
|
|
|
2017-09-18 05:55:09 -05:00
|
|
|
// Debugging of endstops
|
|
|
|
#if ENABLED(PINS_DEBUGGING)
|
|
|
|
static bool monitor_flag;
|
|
|
|
static void monitor();
|
2018-05-16 02:08:43 -05:00
|
|
|
static void run_monitor();
|
2017-09-18 05:55:09 -05:00
|
|
|
#endif
|
2019-08-04 22:22:58 -05:00
|
|
|
|
|
|
|
#if ENABLED(SPI_ENDSTOPS)
|
|
|
|
typedef struct {
|
|
|
|
union {
|
|
|
|
bool any;
|
|
|
|
struct { bool x:1, y:1, z:1; };
|
|
|
|
};
|
|
|
|
} tmc_spi_homing_t;
|
|
|
|
static tmc_spi_homing_t tmc_spi_homing;
|
|
|
|
static void clear_endstop_state();
|
|
|
|
static bool tmc_spi_homing_check();
|
|
|
|
#endif
|
2016-04-27 09:15:20 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
extern Endstops endstops;
|
2019-02-06 17:20:17 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A class to save and change the endstop state,
|
|
|
|
* then restore it when it goes out of scope.
|
|
|
|
*/
|
|
|
|
class TemporaryGlobalEndstopsState {
|
|
|
|
bool saved;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TemporaryGlobalEndstopsState(const bool enable) : saved(endstops.global_enabled()) {
|
|
|
|
endstops.enable_globally(enable);
|
|
|
|
}
|
|
|
|
~TemporaryGlobalEndstopsState() { endstops.enable_globally(saved); }
|
|
|
|
};
|