123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623 |
- /**
- * Marlin 3D Printer Firmware
- * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
- *
- * Based on Sprinter and grbl.
- * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
- *
- * 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
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- *
- */
-
- /**
- * endstops.cpp - A singleton object to manage endstops
- */
-
- #include "endstops.h"
- #include "stepper.h"
-
- #include "../sd/cardreader.h"
- #include "temperature.h"
- #include "../lcd/marlinui.h"
-
- #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
- #include HAL_PATH(../HAL, endstop_interrupts.h)
- #endif
-
- #if BOTH(SD_ABORT_ON_ENDSTOP_HIT, SDSUPPORT)
- #include "printcounter.h" // for print_job_timer
- #endif
-
- #if ENABLED(BLTOUCH)
- #include "../feature/bltouch.h"
- #endif
-
- #if ENABLED(JOYSTICK)
- #include "../feature/joystick.h"
- #endif
-
- #if HAS_BED_PROBE
- #include "probe.h"
- #endif
-
- Endstops endstops;
-
- // private:
-
- bool Endstops::enabled, Endstops::enabled_globally; // Initialized by settings.load()
-
- volatile Endstops::endstop_mask_t Endstops::hit_state;
- Endstops::endstop_mask_t Endstops::live_state = 0;
-
- #if ENDSTOP_NOISE_THRESHOLD
- Endstops::endstop_mask_t Endstops::validated_live_state;
- uint8_t Endstops::endstop_poll_count;
- #endif
-
- #if HAS_BED_PROBE
- volatile bool Endstops::z_probe_enabled = false;
- #endif
-
- // Initialized by settings.load()
- #if ENABLED(X_DUAL_ENDSTOPS)
- float Endstops::x2_endstop_adj;
- #endif
- #if ENABLED(Y_DUAL_ENDSTOPS)
- float Endstops::y2_endstop_adj;
- #endif
- #if ENABLED(Z_MULTI_ENDSTOPS)
- float Endstops::z2_endstop_adj;
- #if NUM_Z_STEPPERS >= 3
- float Endstops::z3_endstop_adj;
- #if NUM_Z_STEPPERS >= 4
- float Endstops::z4_endstop_adj;
- #endif
- #endif
- #endif
-
- #if ENABLED(SPI_ENDSTOPS)
- Endstops::tmc_spi_homing_t Endstops::tmc_spi_homing; // = 0
- #endif
- #if ENABLED(IMPROVE_HOMING_RELIABILITY)
- millis_t sg_guard_period; // = 0
- #endif
-
- /**
- * Class and Instance Methods
- */
-
- void Endstops::init() {
-
- #if HAS_X_MIN
- #if ENABLED(ENDSTOPPULLUP_XMIN)
- SET_INPUT_PULLUP(X_MIN_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_XMIN)
- SET_INPUT_PULLDOWN(X_MIN_PIN);
- #else
- SET_INPUT(X_MIN_PIN);
- #endif
- #endif
-
- #if HAS_X2_MIN
- #if ENABLED(ENDSTOPPULLUP_XMIN)
- SET_INPUT_PULLUP(X2_MIN_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_XMIN)
- SET_INPUT_PULLDOWN(X2_MIN_PIN);
- #else
- SET_INPUT(X2_MIN_PIN);
- #endif
- #endif
-
- #if HAS_Y_MIN
- #if ENABLED(ENDSTOPPULLUP_YMIN)
- SET_INPUT_PULLUP(Y_MIN_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_YMIN)
- SET_INPUT_PULLDOWN(Y_MIN_PIN);
- #else
- SET_INPUT(Y_MIN_PIN);
- #endif
- #endif
-
- #if HAS_Y2_MIN
- #if ENABLED(ENDSTOPPULLUP_YMIN)
- SET_INPUT_PULLUP(Y2_MIN_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_YMIN)
- SET_INPUT_PULLDOWN(Y2_MIN_PIN);
- #else
- SET_INPUT(Y2_MIN_PIN);
- #endif
- #endif
-
- #if HAS_Z_MIN
- #if ENABLED(ENDSTOPPULLUP_ZMIN)
- SET_INPUT_PULLUP(Z_MIN_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_ZMIN)
- SET_INPUT_PULLDOWN(Z_MIN_PIN);
- #else
- SET_INPUT(Z_MIN_PIN);
- #endif
- #endif
-
- #if HAS_Z2_MIN
- #if ENABLED(ENDSTOPPULLUP_ZMIN)
- SET_INPUT_PULLUP(Z2_MIN_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_ZMIN)
- SET_INPUT_PULLDOWN(Z2_MIN_PIN);
- #else
- SET_INPUT(Z2_MIN_PIN);
- #endif
- #endif
-
- #if HAS_Z3_MIN
- #if ENABLED(ENDSTOPPULLUP_ZMIN)
- SET_INPUT_PULLUP(Z3_MIN_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_ZMIN)
- SET_INPUT_PULLDOWN(Z3_MIN_PIN);
- #else
- SET_INPUT(Z3_MIN_PIN);
- #endif
- #endif
-
- #if HAS_Z4_MIN
- #if ENABLED(ENDSTOPPULLUP_ZMIN)
- SET_INPUT_PULLUP(Z4_MIN_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_ZMIN)
- SET_INPUT_PULLDOWN(Z4_MIN_PIN);
- #else
- SET_INPUT(Z4_MIN_PIN);
- #endif
- #endif
-
- #if HAS_X_MAX
- #if ENABLED(ENDSTOPPULLUP_XMAX)
- SET_INPUT_PULLUP(X_MAX_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_XMAX)
- SET_INPUT_PULLDOWN(X_MAX_PIN);
- #else
- SET_INPUT(X_MAX_PIN);
- #endif
- #endif
-
- #if HAS_X2_MAX
- #if ENABLED(ENDSTOPPULLUP_XMAX)
- SET_INPUT_PULLUP(X2_MAX_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_XMAX)
- SET_INPUT_PULLDOWN(X2_MAX_PIN);
- #else
- SET_INPUT(X2_MAX_PIN);
- #endif
- #endif
-
- #if HAS_Y_MAX
- #if ENABLED(ENDSTOPPULLUP_YMAX)
- SET_INPUT_PULLUP(Y_MAX_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_YMAX)
- SET_INPUT_PULLDOWN(Y_MAX_PIN);
- #else
- SET_INPUT(Y_MAX_PIN);
- #endif
- #endif
-
- #if HAS_Y2_MAX
- #if ENABLED(ENDSTOPPULLUP_YMAX)
- SET_INPUT_PULLUP(Y2_MAX_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_YMAX)
- SET_INPUT_PULLDOWN(Y2_MAX_PIN);
- #else
- SET_INPUT(Y2_MAX_PIN);
- #endif
- #endif
-
- #if HAS_Z_MAX
- #if ENABLED(ENDSTOPPULLUP_ZMAX)
- SET_INPUT_PULLUP(Z_MAX_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_ZMAX)
- SET_INPUT_PULLDOWN(Z_MAX_PIN);
- #else
- SET_INPUT(Z_MAX_PIN);
- #endif
- #endif
-
- #if HAS_Z2_MAX
- #if ENABLED(ENDSTOPPULLUP_ZMAX)
- SET_INPUT_PULLUP(Z2_MAX_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_ZMAX)
- SET_INPUT_PULLDOWN(Z2_MAX_PIN);
- #else
- SET_INPUT(Z2_MAX_PIN);
- #endif
- #endif
-
- #if HAS_Z3_MAX
- #if ENABLED(ENDSTOPPULLUP_ZMAX)
- SET_INPUT_PULLUP(Z3_MAX_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_ZMAX)
- SET_INPUT_PULLDOWN(Z3_MAX_PIN);
- #else
- SET_INPUT(Z3_MAX_PIN);
- #endif
- #endif
-
- #if HAS_Z4_MAX
- #if ENABLED(ENDSTOPPULLUP_ZMAX)
- SET_INPUT_PULLUP(Z4_MAX_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_ZMAX)
- SET_INPUT_PULLDOWN(Z4_MAX_PIN);
- #else
- SET_INPUT(Z4_MAX_PIN);
- #endif
- #endif
-
- #if HAS_I_MIN
- #if ENABLED(ENDSTOPPULLUP_IMIN)
- SET_INPUT_PULLUP(I_MIN_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_IMIN)
- SET_INPUT_PULLDOWN(I_MIN_PIN);
- #else
- SET_INPUT(I_MIN_PIN);
- #endif
- #endif
-
- #if HAS_I_MAX
- #if ENABLED(ENDSTOPPULLUP_IMAX)
- SET_INPUT_PULLUP(I_MAX_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_IMAX)
- SET_INPUT_PULLDOWN(I_MAX_PIN);
- #else
- SET_INPUT(I_MAX_PIN);
- #endif
- #endif
-
- #if HAS_J_MIN
- #if ENABLED(ENDSTOPPULLUP_JMIN)
- SET_INPUT_PULLUP(J_MIN_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_IMIN)
- SET_INPUT_PULLDOWN(J_MIN_PIN);
- #else
- SET_INPUT(J_MIN_PIN);
- #endif
- #endif
-
- #if HAS_J_MAX
- #if ENABLED(ENDSTOPPULLUP_JMAX)
- SET_INPUT_PULLUP(J_MAX_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_JMAX)
- SET_INPUT_PULLDOWN(J_MAX_PIN);
- #else
- SET_INPUT(J_MAX_PIN);
- #endif
- #endif
-
- #if HAS_K_MIN
- #if ENABLED(ENDSTOPPULLUP_KMIN)
- SET_INPUT_PULLUP(K_MIN_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_KMIN)
- SET_INPUT_PULLDOWN(K_MIN_PIN);
- #else
- SET_INPUT(K_MIN_PIN);
- #endif
- #endif
-
- #if HAS_K_MAX
- #if ENABLED(ENDSTOPPULLUP_KMAX)
- SET_INPUT_PULLUP(K_MAX_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_KMIN)
- SET_INPUT_PULLDOWN(K_MAX_PIN);
- #else
- SET_INPUT(K_MAX_PIN);
- #endif
- #endif
-
- #if HAS_U_MIN
- #if ENABLED(ENDSTOPPULLUP_UMIN)
- SET_INPUT_PULLUP(U_MIN_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_UMIN)
- SET_INPUT_PULLDOWN(U_MIN_PIN);
- #else
- SET_INPUT(U_MIN_PIN);
- #endif
- #endif
-
- #if HAS_U_MAX
- #if ENABLED(ENDSTOPPULLUP_UMAX)
- SET_INPUT_PULLUP(U_MAX_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_UMIN)
- SET_INPUT_PULLDOWN(U_MAX_PIN);
- #else
- SET_INPUT(U_MAX_PIN);
- #endif
- #endif
-
- #if HAS_V_MIN
- #if ENABLED(ENDSTOPPULLUP_VMIN)
- SET_INPUT_PULLUP(V_MIN_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_VMIN)
- SET_INPUT_PULLDOWN(V_MIN_PIN);
- #else
- SET_INPUT(V_MIN_PIN);
- #endif
- #endif
-
- #if HAS_V_MAX
- #if ENABLED(ENDSTOPPULLUP_VMAX)
- SET_INPUT_PULLUP(V_MAX_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_VMIN)
- SET_INPUT_PULLDOWN(V_MAX_PIN);
- #else
- SET_INPUT(V_MAX_PIN);
- #endif
- #endif
-
- #if HAS_W_MIN
- #if ENABLED(ENDSTOPPULLUP_WMIN)
- SET_INPUT_PULLUP(W_MIN_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_WMIN)
- SET_INPUT_PULLDOWN(W_MIN_PIN);
- #else
- SET_INPUT(W_MIN_PIN);
- #endif
- #endif
-
- #if HAS_W_MAX
- #if ENABLED(ENDSTOPPULLUP_WMAX)
- SET_INPUT_PULLUP(W_MAX_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_WMIN)
- SET_INPUT_PULLDOWN(W_MAX_PIN);
- #else
- SET_INPUT(W_MAX_PIN);
- #endif
- #endif
-
- #if PIN_EXISTS(CALIBRATION)
- #if ENABLED(CALIBRATION_PIN_PULLUP)
- SET_INPUT_PULLUP(CALIBRATION_PIN);
- #elif ENABLED(CALIBRATION_PIN_PULLDOWN)
- SET_INPUT_PULLDOWN(CALIBRATION_PIN);
- #else
- SET_INPUT(CALIBRATION_PIN);
- #endif
- #endif
-
- #if USES_Z_MIN_PROBE_PIN
- #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
- SET_INPUT_PULLUP(Z_MIN_PROBE_PIN);
- #elif ENABLED(ENDSTOPPULLDOWN_ZMIN_PROBE)
- SET_INPUT_PULLDOWN(Z_MIN_PROBE_PIN);
- #else
- SET_INPUT(Z_MIN_PROBE_PIN);
- #endif
- #endif
-
- #if ENABLED(PROBE_ACTIVATION_SWITCH)
- SET_INPUT(PROBE_ACTIVATION_SWITCH_PIN);
- #endif
-
- TERN_(PROBE_TARE, probe.tare());
-
- TERN_(ENDSTOP_INTERRUPTS_FEATURE, setup_endstop_interrupts());
-
- // Enable endstops
- enable_globally(ENABLED(ENDSTOPS_ALWAYS_ON_DEFAULT));
-
- } // Endstops::init
-
- // Called at ~1kHz from Temperature ISR: Poll endstop state if required
- void Endstops::poll() {
-
- TERN_(PINS_DEBUGGING, run_monitor()); // Report changes in endstop status
-
- #if DISABLED(ENDSTOP_INTERRUPTS_FEATURE)
- update();
- #elif ENDSTOP_NOISE_THRESHOLD
- if (endstop_poll_count) update();
- #endif
- }
-
- void Endstops::enable_globally(const bool onoff) {
- enabled_globally = enabled = onoff;
- resync();
- }
-
- // Enable / disable endstop checking
- void Endstops::enable(const bool onoff) {
- enabled = onoff;
- resync();
- }
-
- // Disable / Enable endstops based on ENSTOPS_ONLY_FOR_HOMING and global enable
- void Endstops::not_homing() {
- enabled = enabled_globally;
- }
-
- #if ENABLED(VALIDATE_HOMING_ENDSTOPS)
- // If the last move failed to trigger an endstop, call kill
- void Endstops::validate_homing_move() {
- if (trigger_state()) hit_on_purpose();
- else kill(GET_TEXT_F(MSG_KILL_HOMING_FAILED));
- }
- #endif
-
- // Enable / disable endstop z-probe checking
- #if HAS_BED_PROBE
- void Endstops::enable_z_probe(const bool onoff) {
- z_probe_enabled = onoff;
- #if PIN_EXISTS(PROBE_ENABLE)
- WRITE(PROBE_ENABLE_PIN, onoff);
- #endif
- resync();
- }
- #endif
-
- // Get the stable endstop states when enabled
- void Endstops::resync() {
- if (!abort_enabled()) return; // If endstops/probes are disabled the loop below can hang
-
- // Wait for Temperature ISR to run at least once (runs at 1kHz)
- TERN(ENDSTOP_INTERRUPTS_FEATURE, update(), safe_delay(2));
- while (TERN0(ENDSTOP_NOISE_THRESHOLD, endstop_poll_count)) safe_delay(1);
- }
-
- #if ENABLED(PINS_DEBUGGING)
- void Endstops::run_monitor() {
- if (!monitor_flag) return;
- static uint8_t monitor_count = 16; // offset this check from the others
- monitor_count += _BV(1); // 15 Hz
- monitor_count &= 0x7F;
- if (!monitor_count) monitor(); // report changes in endstop status
- }
- #endif
-
- void Endstops::event_handler() {
- static endstop_mask_t prev_hit_state; // = 0
- if (hit_state == prev_hit_state) return;
- prev_hit_state = hit_state;
- if (hit_state) {
- #if HAS_STATUS_MESSAGE
- char NUM_AXIS_LIST(chrX = ' ', chrY = ' ', chrZ = ' ', chrI = ' ', chrJ = ' ', chrK = ' ', chrU = ' ', chrV = ' ', chrW = ' '),
- chrP = ' ';
- #define _SET_STOP_CHAR(A,C) (chr## A = C)
- #else
- #define _SET_STOP_CHAR(A,C) NOOP
- #endif
-
- #define _ENDSTOP_HIT_ECHO(A,C) do{ \
- SERIAL_ECHOPGM(" " STRINGIFY(A) ":", planner.triggered_position_mm(_AXIS(A))); _SET_STOP_CHAR(A,C); }while(0)
-
- #define _ENDSTOP_HIT_TEST(A,C) \
- if (TERN0(HAS_##A##_MIN, TEST(hit_state, A##_MIN)) || TERN0(HAS_##A##_MAX, TEST(hit_state, A##_MAX))) \
- _ENDSTOP_HIT_ECHO(A,C)
-
- #define ENDSTOP_HIT_TEST_X() _ENDSTOP_HIT_TEST(X,'X')
- #define ENDSTOP_HIT_TEST_Y() _ENDSTOP_HIT_TEST(Y,'Y')
- #define ENDSTOP_HIT_TEST_Z() _ENDSTOP_HIT_TEST(Z,'Z')
- #define ENDSTOP_HIT_TEST_I() _ENDSTOP_HIT_TEST(I,'I')
- #define ENDSTOP_HIT_TEST_J() _ENDSTOP_HIT_TEST(J,'J')
- #define ENDSTOP_HIT_TEST_K() _ENDSTOP_HIT_TEST(K,'K')
- #define ENDSTOP_HIT_TEST_U() _ENDSTOP_HIT_TEST(U,'U')
- #define ENDSTOP_HIT_TEST_V() _ENDSTOP_HIT_TEST(V,'V')
- #define ENDSTOP_HIT_TEST_W() _ENDSTOP_HIT_TEST(W,'W')
-
- SERIAL_ECHO_START();
- SERIAL_ECHOPGM(STR_ENDSTOPS_HIT);
- NUM_AXIS_CODE(
- ENDSTOP_HIT_TEST_X(),
- ENDSTOP_HIT_TEST_Y(),
- ENDSTOP_HIT_TEST_Z(),
- _ENDSTOP_HIT_TEST(I,'I'),
- _ENDSTOP_HIT_TEST(J,'J'),
- _ENDSTOP_HIT_TEST(K,'K'),
- _ENDSTOP_HIT_TEST(U,'U'),
- _ENDSTOP_HIT_TEST(V,'V'),
- _ENDSTOP_HIT_TEST(W,'W')
- );
-
- #if USES_Z_MIN_PROBE_PIN
- #define P_AXIS Z_AXIS
- if (TEST(hit_state, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
- #endif
- SERIAL_EOL();
-
- TERN_(HAS_STATUS_MESSAGE,
- ui.status_printf(0,
- F(S_FMT GANG_N_1(NUM_AXES, " %c") " %c"),
- GET_TEXT(MSG_LCD_ENDSTOPS),
- NUM_AXIS_LIST(chrX, chrY, chrZ, chrI, chrJ, chrK, chrU, chrV, chrW), chrP
- )
- );
-
- #if BOTH(SD_ABORT_ON_ENDSTOP_HIT, SDSUPPORT)
- if (planner.abort_on_endstop_hit) {
- card.abortFilePrintNow();
- quickstop_stepper();
- thermalManager.disable_all_heaters();
- print_job_timer.stop();
- }
- #endif
- }
- }
-
- #pragma GCC diagnostic push
- #if GCC_VERSION <= 50000
- #pragma GCC diagnostic ignored "-Wunused-function"
- #endif
-
- static void print_es_state(const bool is_hit, FSTR_P const flabel=nullptr) {
- if (flabel) SERIAL_ECHOF(flabel);
- SERIAL_ECHOPGM(": ");
- SERIAL_ECHOLNF(is_hit ? F(STR_ENDSTOP_HIT) : F(STR_ENDSTOP_OPEN));
- }
-
- #pragma GCC diagnostic pop
-
- void __O2 Endstops::report_states() {
- TERN_(BLTOUCH, bltouch._set_SW_mode());
- SERIAL_ECHOLNPGM(STR_M119_REPORT);
- #define ES_REPORT(S) print_es_state(READ(S##_PIN) != S##_ENDSTOP_INVERTING, F(STR_##S))
- #if HAS_X_MIN
- ES_REPORT(X_MIN);
- #endif
- #if HAS_X2_MIN
- ES_REPORT(X2_MIN);
- #endif
- #if HAS_X_MAX
- ES_REPORT(X_MAX);
- #endif
- #if HAS_X2_MAX
- ES_REPORT(X2_MAX);
- #endif
- #if HAS_Y_MIN
- ES_REPORT(Y_MIN);
- #endif
- #if HAS_Y2_MIN
- ES_REPORT(Y2_MIN);
- #endif
- #if HAS_Y_MAX
- ES_REPORT(Y_MAX);
- #endif
- #if HAS_Y2_MAX
- ES_REPORT(Y2_MAX);
- #endif
- #if HAS_Z_MIN
- ES_REPORT(Z_MIN);
- #endif
- #if HAS_Z2_MIN
- ES_REPORT(Z2_MIN);
- #endif
- #if HAS_Z3_MIN
- ES_REPORT(Z3_MIN);
- #endif
- #if HAS_Z4_MIN
- ES_REPORT(Z4_MIN);
- #endif
- #if HAS_Z_MAX
- ES_REPORT(Z_MAX);
- #endif
- #if HAS_Z2_MAX
- ES_REPORT(Z2_MAX);
- #endif
- #if HAS_Z3_MAX
- ES_REPORT(Z3_MAX);
- #endif
- #if HAS_Z4_MAX
- ES_REPORT(Z4_MAX);
- #endif
- #if HAS_I_MIN
- ES_REPORT(I_MIN);
- #endif
- #if HAS_I_MAX
- ES_REPORT(I_MAX);
- #endif
- #if HAS_J_MIN
- ES_REPORT(J_MIN);
- #endif
- #if HAS_J_MAX
- ES_REPORT(J_MAX);
- #endif
- #if HAS_K_MIN
- ES_REPORT(K_MIN);
- #endif
- #if HAS_K_MAX
- ES_REPORT(K_MAX);
- #endif
- #if HAS_U_MIN
- ES_REPORT(U_MIN);
- #endif
- #if HAS_U_MAX
- ES_REPORT(U_MAX);
- #endif
- #if HAS_V_MIN
- ES_REPORT(V_MIN);
- #endif
- #if HAS_V_MAX
- ES_REPORT(V_MAX);
- #endif
- #if HAS_W_MIN
- ES_REPORT(W_MIN);
- #endif
- #if HAS_W_MAX
- ES_REPORT(W_MAX);
- #endif
- #if ENABLED(PROBE_ACTIVATION_SWITCH)
- print_es_state(probe_switch_activated(), F(STR_PROBE_EN));
- #endif
- #if USES_Z_MIN_PROBE_PIN
- print_es_state(PROBE_TRIGGERED(), F(STR_Z_PROBE));
- #endif
- #if MULTI_FILAMENT_SENSOR
- #define _CASE_RUNOUT(N) case N: pin = FIL_RUNOUT##N##_PIN; state = FIL_RUNOUT##N##_STATE; break;
- LOOP_S_LE_N(i, 1, NUM_RUNOUT_SENSORS) {
- pin_t pin;
- uint8_t state;
- switch (i) {
- default: continue;
- REPEAT_1(NUM_RUNOUT_SENSORS, _CASE_RUNOUT)
- }
- SERIAL_ECHOPGM(STR_FILAMENT);
- if (i > 1) SERIAL_CHAR(' ', '0' + i);
- print_es_state(extDigitalRead(pin) != state);
- }
- #undef _CASE_RUNOUT
- #elif HAS_FILAMENT_SENSOR
- print_es_state(READ(FIL_RUNOUT1_PIN) != FIL_RUNOUT1_STATE, F(STR_FILAMENT));
- #endif
-
- TERN_(BLTOUCH, bltouch._reset_SW_mode());
- TERN_(JOYSTICK_DEBUG, joystick.report());
-
- } // Endstops::report_states
-
- #if HAS_DELTA_SENSORLESS_PROBING
- #define __ENDSTOP(AXIS, ...) AXIS ##_MAX
- #define _ENDSTOP_PIN(AXIS, ...) AXIS ##_MAX_PIN
- #define _ENDSTOP_INVERTING(AXIS, ...) AXIS ##_MAX_ENDSTOP_INVERTING
- #else
- #define __ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
- #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
- #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
- #endif
- #define _ENDSTOP(AXIS, MINMAX) __ENDSTOP(AXIS, MINMAX)
-
- /**
- * Called from interrupt context by the Endstop ISR or Stepper ISR!
- * Read endstops to get their current states, register hits for all
- * axes moving in the direction of their endstops, and abort moves.
- */
- void Endstops::update() {
-
- #if !ENDSTOP_NOISE_THRESHOLD // If not debouncing...
- if (!abort_enabled()) return; // ...and not enabled, exit.
- #endif
-
- // Macros to update / copy the live_state
- #define UPDATE_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT_TO(live_state, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
- #define COPY_LIVE_STATE(SRC_BIT, DST_BIT) SET_BIT_TO(live_state, DST_BIT, TEST(live_state, SRC_BIT))
-
- #if ENABLED(G38_PROBE_TARGET) && NONE(CORE_IS_XY, CORE_IS_XZ, MARKFORGED_XY, MARKFORGED_YX)
- #define HAS_G38_PROBE 1
- // For G38 moves check the probe's pin for ALL movement
- if (G38_move) UPDATE_ENDSTOP_BIT(Z, TERN(USES_Z_MIN_PROBE_PIN, MIN_PROBE, MIN));
- #endif
-
- // With Dual X, endstops are only checked in the homing direction for the active extruder
- #define X_MIN_TEST() TERN1(DUAL_X_CARRIAGE, TERN0(X_HOME_TO_MIN, stepper.last_moved_extruder == 0) || TERN0(X2_HOME_TO_MIN, stepper.last_moved_extruder != 0))
- #define X_MAX_TEST() TERN1(DUAL_X_CARRIAGE, TERN0(X_HOME_TO_MAX, stepper.last_moved_extruder == 0) || TERN0(X2_HOME_TO_MAX, stepper.last_moved_extruder != 0))
-
- // Use HEAD for core axes, AXIS for others
- #if ANY(CORE_IS_XY, CORE_IS_XZ, MARKFORGED_XY, MARKFORGED_YX)
- #define X_AXIS_HEAD X_HEAD
- #else
- #define X_AXIS_HEAD X_AXIS
- #endif
- #if ANY(CORE_IS_XY, CORE_IS_YZ, MARKFORGED_XY, MARKFORGED_YX)
- #define Y_AXIS_HEAD Y_HEAD
- #else
- #define Y_AXIS_HEAD Y_AXIS
- #endif
- #if CORE_IS_XZ || CORE_IS_YZ
- #define Z_AXIS_HEAD Z_HEAD
- #else
- #define Z_AXIS_HEAD Z_AXIS
- #endif
-
- #define I_AXIS_HEAD I_AXIS
- #define J_AXIS_HEAD J_AXIS
- #define K_AXIS_HEAD K_AXIS
- #define U_AXIS_HEAD U_AXIS
- #define V_AXIS_HEAD V_AXIS
- #define W_AXIS_HEAD W_AXIS
-
- /**
- * Check and update endstops
- */
- #if HAS_X_MIN && !X_SPI_SENSORLESS
- UPDATE_ENDSTOP_BIT(X, MIN);
- #if ENABLED(X_DUAL_ENDSTOPS)
- #if HAS_X2_MIN
- UPDATE_ENDSTOP_BIT(X2, MIN);
- #else
- COPY_LIVE_STATE(X_MIN, X2_MIN);
- #endif
- #endif
- #endif
-
- #if HAS_X_MAX && !X_SPI_SENSORLESS
- UPDATE_ENDSTOP_BIT(X, MAX);
- #if ENABLED(X_DUAL_ENDSTOPS)
- #if HAS_X2_MAX
- UPDATE_ENDSTOP_BIT(X2, MAX);
- #else
- COPY_LIVE_STATE(X_MAX, X2_MAX);
- #endif
- #endif
- #endif
-
- #if HAS_Y_MIN && !Y_SPI_SENSORLESS
- UPDATE_ENDSTOP_BIT(Y, MIN);
- #if ENABLED(Y_DUAL_ENDSTOPS)
- #if HAS_Y2_MIN
- UPDATE_ENDSTOP_BIT(Y2, MIN);
- #else
- COPY_LIVE_STATE(Y_MIN, Y2_MIN);
- #endif
- #endif
- #endif
-
- #if HAS_Y_MAX && !Y_SPI_SENSORLESS
- UPDATE_ENDSTOP_BIT(Y, MAX);
- #if ENABLED(Y_DUAL_ENDSTOPS)
- #if HAS_Y2_MAX
- UPDATE_ENDSTOP_BIT(Y2, MAX);
- #else
- COPY_LIVE_STATE(Y_MAX, Y2_MAX);
- #endif
- #endif
- #endif
-
- #if HAS_Z_MIN && NONE(Z_SPI_SENSORLESS, Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
- UPDATE_ENDSTOP_BIT(Z, MIN);
- #if ENABLED(Z_MULTI_ENDSTOPS)
- #if HAS_Z2_MIN
- UPDATE_ENDSTOP_BIT(Z2, MIN);
- #else
- COPY_LIVE_STATE(Z_MIN, Z2_MIN);
- #endif
- #if NUM_Z_STEPPERS >= 3
- #if HAS_Z3_MIN
- UPDATE_ENDSTOP_BIT(Z3, MIN);
- #else
- COPY_LIVE_STATE(Z_MIN, Z3_MIN);
- #endif
- #endif
- #if NUM_Z_STEPPERS >= 4
- #if HAS_Z4_MIN
- UPDATE_ENDSTOP_BIT(Z4, MIN);
- #else
- COPY_LIVE_STATE(Z_MIN, Z4_MIN);
- #endif
- #endif
- #endif
- #endif
-
- #if HAS_BED_PROBE
- // When closing the gap check the enabled probe
- if (probe_switch_activated())
- UPDATE_ENDSTOP_BIT(Z, TERN(USES_Z_MIN_PROBE_PIN, MIN_PROBE, MIN));
- #endif
-
- #if HAS_Z_MAX && !Z_SPI_SENSORLESS
- // Check both Z dual endstops
- #if ENABLED(Z_MULTI_ENDSTOPS)
- UPDATE_ENDSTOP_BIT(Z, MAX);
- #if HAS_Z2_MAX
- UPDATE_ENDSTOP_BIT(Z2, MAX);
- #else
- COPY_LIVE_STATE(Z_MAX, Z2_MAX);
- #endif
- #if NUM_Z_STEPPERS >= 3
- #if HAS_Z3_MAX
- UPDATE_ENDSTOP_BIT(Z3, MAX);
- #else
- COPY_LIVE_STATE(Z_MAX, Z3_MAX);
- #endif
- #endif
- #if NUM_Z_STEPPERS >= 4
- #if HAS_Z4_MAX
- UPDATE_ENDSTOP_BIT(Z4, MAX);
- #else
- COPY_LIVE_STATE(Z_MAX, Z4_MAX);
- #endif
- #endif
- #elif TERN1(USES_Z_MIN_PROBE_PIN, Z_MAX_PIN != Z_MIN_PROBE_PIN)
- // If this pin isn't the bed probe it's the Z endstop
- UPDATE_ENDSTOP_BIT(Z, MAX);
- #endif
- #endif
-
- #if HAS_I_MIN && !I_SPI_SENSORLESS
- #if ENABLED(I_DUAL_ENDSTOPS)
- UPDATE_ENDSTOP_BIT(I, MIN);
- #if HAS_I2_MIN
- UPDATE_ENDSTOP_BIT(I2, MAX);
- #else
- COPY_LIVE_STATE(I_MIN, I2_MIN);
- #endif
- #else
- UPDATE_ENDSTOP_BIT(I, MIN);
- #endif
- #endif
-
- #if HAS_I_MAX && !I_SPI_SENSORLESS
- #if ENABLED(I_DUAL_ENDSTOPS)
- UPDATE_ENDSTOP_BIT(I, MAX);
- #if HAS_I2_MAX
- UPDATE_ENDSTOP_BIT(I2, MAX);
- #else
- COPY_LIVE_STATE(I_MAX, I2_MAX);
- #endif
- #else
- UPDATE_ENDSTOP_BIT(I, MAX);
- #endif
- #endif
-
- #if HAS_J_MIN && !J_SPI_SENSORLESS
- #if ENABLED(J_DUAL_ENDSTOPS)
- UPDATE_ENDSTOP_BIT(J, MIN);
- #if HAS_J2_MIN
- UPDATE_ENDSTOP_BIT(J2, MIN);
- #else
- COPY_LIVE_STATE(J_MIN, J2_MIN);
- #endif
- #else
- UPDATE_ENDSTOP_BIT(J, MIN);
- #endif
- #endif
-
- #if HAS_J_MAX && !J_SPI_SENSORLESS
- #if ENABLED(J_DUAL_ENDSTOPS)
- UPDATE_ENDSTOP_BIT(J, MAX);
- #if HAS_J2_MAX
- UPDATE_ENDSTOP_BIT(J2, MAX);
- #else
- COPY_LIVE_STATE(J_MAX, J2_MAX);
- #endif
- #else
- UPDATE_ENDSTOP_BIT(J, MAX);
- #endif
- #endif
-
- #if HAS_K_MIN && !K_SPI_SENSORLESS
- #if ENABLED(K_DUAL_ENDSTOPS)
- UPDATE_ENDSTOP_BIT(K, MIN);
- #if HAS_K2_MIN
- UPDATE_ENDSTOP_BIT(K2, MIN);
- #else
- COPY_LIVE_STATE(K_MIN, K2_MIN);
- #endif
- #else
- UPDATE_ENDSTOP_BIT(K, MIN);
- #endif
- #endif
-
- #if HAS_K_MAX && !K_SPI_SENSORLESS
- #if ENABLED(K_DUAL_ENDSTOPS)
- UPDATE_ENDSTOP_BIT(K, MAX);
- #if HAS_K2_MAX
- UPDATE_ENDSTOP_BIT(K2, MAX);
- #else
- COPY_LIVE_STATE(K_MAX, K2_MAX);
- #endif
- #else
- UPDATE_ENDSTOP_BIT(K, MAX);
- #endif
- #endif
-
- #if HAS_U_MIN && !U_SPI_SENSORLESS
- #if ENABLED(U_DUAL_ENDSTOPS)
- UPDATE_ENDSTOP_BIT(U, MIN);
- #if HAS_U2_MIN
- UPDATE_ENDSTOP_BIT(U2, MIN);
- #else
- COPY_LIVE_STATE(U_MIN, U2_MIN);
- #endif
- #else
- UPDATE_ENDSTOP_BIT(U, MIN);
- #endif
- #endif
-
- #if HAS_U_MAX && !U_SPI_SENSORLESS
- #if ENABLED(U_DUAL_ENDSTOPS)
- UPDATE_ENDSTOP_BIT(U, MAX);
- #if HAS_U2_MAX
- UPDATE_ENDSTOP_BIT(U2, MAX);
- #else
- COPY_LIVE_STATE(U_MAX, U2_MAX);
- #endif
- #else
- UPDATE_ENDSTOP_BIT(U, MAX);
- #endif
- #endif
-
- #if HAS_V_MIN && !V_SPI_SENSORLESS
- #if ENABLED(V_DUAL_ENDSTOPS)
- UPDATE_ENDSTOP_BIT(V, MIN);
- #if HAS_V2_MIN
- UPDATE_ENDSTOP_BIT(V2, MIN);
- #else
- COPY_LIVE_STATE(V_MIN, V2_MIN);
- #endif
- #else
- UPDATE_ENDSTOP_BIT(V, MIN);
- #endif
- #endif
- #if HAS_V_MAX && !V_SPI_SENSORLESS
- #if ENABLED(O_DUAL_ENDSTOPS)
- UPDATE_ENDSTOP_BIT(V, MAX);
- #if HAS_V2_MAX
- UPDATE_ENDSTOP_BIT(V2, MAX);
- #else
- COPY_LIVE_STATE(V_MAX, V2_MAX);
- #endif
- #else
- UPDATE_ENDSTOP_BIT(V, MAX);
- #endif
- #endif
-
- #if HAS_W_MIN && !W_SPI_SENSORLESS
- #if ENABLED(W_DUAL_ENDSTOPS)
- UPDATE_ENDSTOP_BIT(W, MIN);
- #if HAS_W2_MIN
- UPDATE_ENDSTOP_BIT(W2, MIN);
- #else
- COPY_LIVE_STATE(W_MIN, W2_MIN);
- #endif
- #else
- UPDATE_ENDSTOP_BIT(W, MIN);
- #endif
- #endif
- #if HAS_W_MAX && !W_SPI_SENSORLESS
- #if ENABLED(W_DUAL_ENDSTOPS)
- UPDATE_ENDSTOP_BIT(W, MAX);
- #if HAS_W2_MAX
- UPDATE_ENDSTOP_BIT(W2, MAX);
- #else
- COPY_LIVE_STATE(W_MAX, W2_MAX);
- #endif
- #else
- UPDATE_ENDSTOP_BIT(W, MAX);
- #endif
- #endif
-
- #if ENDSTOP_NOISE_THRESHOLD
-
- /**
- * Filtering out noise on endstops requires a delayed decision. Let's assume, due to noise,
- * that 50% of endstop signal samples are good and 50% are bad (assuming normal distribution
- * of random noise). Then the first sample has a 50% chance to be good or bad. The 2nd sample
- * also has a 50% chance to be good or bad. The chances of 2 samples both being bad becomes
- * 50% of 50%, or 25%. That was the previous implementation of Marlin endstop handling. It
- * reduces chances of bad readings in half, at the cost of 1 extra sample period, but chances
- * still exist. The only way to reduce them further is to increase the number of samples.
- * To reduce the chance to 1% (1/128th) requires 7 samples (adding 7ms of delay).
- */
- static endstop_mask_t old_live_state;
- if (old_live_state != live_state) {
- endstop_poll_count = ENDSTOP_NOISE_THRESHOLD;
- old_live_state = live_state;
- }
- else if (endstop_poll_count && !--endstop_poll_count)
- validated_live_state = live_state;
-
- if (!abort_enabled()) return;
-
- #endif
-
- // Test the current status of an endstop
- #define TEST_ENDSTOP(ENDSTOP) (TEST(state(), ENDSTOP))
-
- // Record endstop was hit
- #define _ENDSTOP_HIT(AXIS, MINMAX) SBI(hit_state, _ENDSTOP(AXIS, MINMAX))
-
- // Call the endstop triggered routine for single endstops
- #define PROCESS_ENDSTOP(AXIS, MINMAX) do { \
- if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX))) { \
- _ENDSTOP_HIT(AXIS, MINMAX); \
- planner.endstop_triggered(_AXIS(AXIS)); \
- } \
- }while(0)
-
- // Core Sensorless Homing needs to test an Extra Pin
- #define CORE_DIAG(QQ,A,MM) (CORE_IS_##QQ && A##_SENSORLESS && !A##_SPI_SENSORLESS && HAS_##A##_##MM)
- #define PROCESS_CORE_ENDSTOP(A1,M1,A2,M2) do { \
- if (TEST_ENDSTOP(_ENDSTOP(A1,M1))) { \
- _ENDSTOP_HIT(A2,M2); \
- planner.endstop_triggered(_AXIS(A2)); \
- } \
- }while(0)
-
- // Call the endstop triggered routine for dual endstops
- #define PROCESS_DUAL_ENDSTOP(A, MINMAX) do { \
- const byte dual_hit = TEST_ENDSTOP(_ENDSTOP(A, MINMAX)) | (TEST_ENDSTOP(_ENDSTOP(A##2, MINMAX)) << 1); \
- if (dual_hit) { \
- _ENDSTOP_HIT(A, MINMAX); \
- /* if not performing home or if both endstops were triggered during homing... */ \
- if (!stepper.separate_multi_axis || dual_hit == 0b11) \
- planner.endstop_triggered(_AXIS(A)); \
- } \
- }while(0)
-
- #define PROCESS_TRIPLE_ENDSTOP(A, MINMAX) do { \
- const byte triple_hit = TEST_ENDSTOP(_ENDSTOP(A, MINMAX)) | (TEST_ENDSTOP(_ENDSTOP(A##2, MINMAX)) << 1) | (TEST_ENDSTOP(_ENDSTOP(A##3, MINMAX)) << 2); \
- if (triple_hit) { \
- _ENDSTOP_HIT(A, MINMAX); \
- /* if not performing home or if both endstops were triggered during homing... */ \
- if (!stepper.separate_multi_axis || triple_hit == 0b111) \
- planner.endstop_triggered(_AXIS(A)); \
- } \
- }while(0)
-
- #define PROCESS_QUAD_ENDSTOP(A, MINMAX) do { \
- const byte quad_hit = TEST_ENDSTOP(_ENDSTOP(A, MINMAX)) | (TEST_ENDSTOP(_ENDSTOP(A##2, MINMAX)) << 1) | (TEST_ENDSTOP(_ENDSTOP(A##3, MINMAX)) << 2) | (TEST_ENDSTOP(_ENDSTOP(A##4, MINMAX)) << 3); \
- if (quad_hit) { \
- _ENDSTOP_HIT(A, MINMAX); \
- /* if not performing home or if both endstops were triggered during homing... */ \
- if (!stepper.separate_multi_axis || quad_hit == 0b1111) \
- planner.endstop_triggered(_AXIS(A)); \
- } \
- }while(0)
-
- #if ENABLED(X_DUAL_ENDSTOPS)
- #define PROCESS_ENDSTOP_X(MINMAX) PROCESS_DUAL_ENDSTOP(X, MINMAX)
- #else
- #define PROCESS_ENDSTOP_X(MINMAX) if (X_##MINMAX##_TEST()) PROCESS_ENDSTOP(X, MINMAX)
- #endif
-
- #if ENABLED(Y_DUAL_ENDSTOPS)
- #define PROCESS_ENDSTOP_Y(MINMAX) PROCESS_DUAL_ENDSTOP(Y, MINMAX)
- #else
- #define PROCESS_ENDSTOP_Y(MINMAX) PROCESS_ENDSTOP(Y, MINMAX)
- #endif
-
- #if DISABLED(Z_MULTI_ENDSTOPS)
- #define PROCESS_ENDSTOP_Z(MINMAX) PROCESS_ENDSTOP(Z, MINMAX)
- #elif NUM_Z_STEPPERS == 4
- #define PROCESS_ENDSTOP_Z(MINMAX) PROCESS_QUAD_ENDSTOP(Z, MINMAX)
- #elif NUM_Z_STEPPERS == 3
- #define PROCESS_ENDSTOP_Z(MINMAX) PROCESS_TRIPLE_ENDSTOP(Z, MINMAX)
- #else
- #define PROCESS_ENDSTOP_Z(MINMAX) PROCESS_DUAL_ENDSTOP(Z, MINMAX)
- #endif
-
- #if HAS_G38_PROBE // TODO (DerAndere): Add support for HAS_I_AXIS
- #define _G38_OPEN_STATE TERN(G38_PROBE_AWAY, (G38_move >= 4), LOW)
- // For G38 moves check the probe's pin for ALL movement
- if (G38_move && TEST_ENDSTOP(_ENDSTOP(Z, TERN(USES_Z_MIN_PROBE_PIN, MIN_PROBE, MIN))) != _G38_OPEN_STATE) {
- if (stepper.axis_is_moving(X_AXIS)) { _ENDSTOP_HIT(X, TERN(X_HOME_TO_MIN, MIN, MAX)); planner.endstop_triggered(X_AXIS); }
- #if HAS_Y_AXIS
- else if (stepper.axis_is_moving(Y_AXIS)) { _ENDSTOP_HIT(Y, TERN(Y_HOME_TO_MIN, MIN, MAX)); planner.endstop_triggered(Y_AXIS); }
- #endif
- #if HAS_Z_AXIS
- else if (stepper.axis_is_moving(Z_AXIS)) { _ENDSTOP_HIT(Z, TERN(Z_HOME_TO_MIN, MIN, MAX)); planner.endstop_triggered(Z_AXIS); }
- #endif
- G38_did_trigger = true;
- }
- #endif
-
- // Signal, after validation, if an endstop limit is pressed or not
-
- if (stepper.axis_is_moving(X_AXIS)) {
- if (stepper.motor_direction(X_AXIS_HEAD)) { // -direction
- #if HAS_X_MIN || (X_SPI_SENSORLESS && X_HOME_TO_MIN)
- PROCESS_ENDSTOP_X(MIN);
- #if CORE_DIAG(XY, Y, MIN)
- PROCESS_CORE_ENDSTOP(Y,MIN,X,MIN);
- #elif CORE_DIAG(XY, Y, MAX)
- PROCESS_CORE_ENDSTOP(Y,MAX,X,MIN);
- #elif CORE_DIAG(XZ, Z, MIN)
- PROCESS_CORE_ENDSTOP(Z,MIN,X,MIN);
- #elif CORE_DIAG(XZ, Z, MAX)
- PROCESS_CORE_ENDSTOP(Z,MAX,X,MIN);
- #endif
- #endif
- }
- else { // +direction
- #if HAS_X_MAX || (X_SPI_SENSORLESS && X_HOME_TO_MAX)
- PROCESS_ENDSTOP_X(MAX);
- #if CORE_DIAG(XY, Y, MIN)
- PROCESS_CORE_ENDSTOP(Y,MIN,X,MAX);
- #elif CORE_DIAG(XY, Y, MAX)
- PROCESS_CORE_ENDSTOP(Y,MAX,X,MAX);
- #elif CORE_DIAG(XZ, Z, MIN)
- PROCESS_CORE_ENDSTOP(Z,MIN,X,MAX);
- #elif CORE_DIAG(XZ, Z, MAX)
- PROCESS_CORE_ENDSTOP(Z,MAX,X,MAX);
- #endif
- #endif
- }
- }
-
- #if HAS_Y_AXIS
- if (stepper.axis_is_moving(Y_AXIS)) {
- if (stepper.motor_direction(Y_AXIS_HEAD)) { // -direction
- #if HAS_Y_MIN || (Y_SPI_SENSORLESS && Y_HOME_TO_MIN)
- PROCESS_ENDSTOP_Y(MIN);
- #if CORE_DIAG(XY, X, MIN)
- PROCESS_CORE_ENDSTOP(X,MIN,Y,MIN);
- #elif CORE_DIAG(XY, X, MAX)
- PROCESS_CORE_ENDSTOP(X,MAX,Y,MIN);
- #elif CORE_DIAG(YZ, Z, MIN)
- PROCESS_CORE_ENDSTOP(Z,MIN,Y,MIN);
- #elif CORE_DIAG(YZ, Z, MAX)
- PROCESS_CORE_ENDSTOP(Z,MAX,Y,MIN);
- #endif
- #endif
- }
- else { // +direction
- #if HAS_Y_MAX || (Y_SPI_SENSORLESS && Y_HOME_TO_MAX)
- PROCESS_ENDSTOP_Y(MAX);
- #if CORE_DIAG(XY, X, MIN)
- PROCESS_CORE_ENDSTOP(X,MIN,Y,MAX);
- #elif CORE_DIAG(XY, X, MAX)
- PROCESS_CORE_ENDSTOP(X,MAX,Y,MAX);
- #elif CORE_DIAG(YZ, Z, MIN)
- PROCESS_CORE_ENDSTOP(Z,MIN,Y,MAX);
- #elif CORE_DIAG(YZ, Z, MAX)
- PROCESS_CORE_ENDSTOP(Z,MAX,Y,MAX);
- #endif
- #endif
- }
- }
- #endif
-
- #if HAS_Z_AXIS
- if (stepper.axis_is_moving(Z_AXIS)) {
- if (stepper.motor_direction(Z_AXIS_HEAD)) { // Z -direction. Gantry down, bed up.
-
- #if HAS_Z_MIN || (Z_SPI_SENSORLESS && Z_HOME_TO_MIN)
- if ( TERN1(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN, z_probe_enabled)
- && TERN1(USES_Z_MIN_PROBE_PIN, !z_probe_enabled)
- ) PROCESS_ENDSTOP_Z(MIN);
- #if CORE_DIAG(XZ, X, MIN)
- PROCESS_CORE_ENDSTOP(X,MIN,Z,MIN);
- #elif CORE_DIAG(XZ, X, MAX)
- PROCESS_CORE_ENDSTOP(X,MAX,Z,MIN);
- #elif CORE_DIAG(YZ, Y, MIN)
- PROCESS_CORE_ENDSTOP(Y,MIN,Z,MIN);
- #elif CORE_DIAG(YZ, Y, MAX)
- PROCESS_CORE_ENDSTOP(Y,MAX,Z,MIN);
- #endif
- #endif
-
- // When closing the gap check the enabled probe
- #if USES_Z_MIN_PROBE_PIN
- if (z_probe_enabled) PROCESS_ENDSTOP(Z, MIN_PROBE);
- #endif
- }
- else { // Z +direction. Gantry up, bed down.
- #if HAS_Z_MAX || (Z_SPI_SENSORLESS && Z_HOME_TO_MAX)
- #if ENABLED(Z_MULTI_ENDSTOPS)
- PROCESS_ENDSTOP_Z(MAX);
- #elif TERN1(USES_Z_MIN_PROBE_PIN, Z_MAX_PIN != Z_MIN_PROBE_PIN) // No probe or probe is Z_MIN || Probe is not Z_MAX
- PROCESS_ENDSTOP(Z, MAX);
- #endif
- #if CORE_DIAG(XZ, X, MIN)
- PROCESS_CORE_ENDSTOP(X,MIN,Z,MAX);
- #elif CORE_DIAG(XZ, X, MAX)
- PROCESS_CORE_ENDSTOP(X,MAX,Z,MAX);
- #elif CORE_DIAG(YZ, Y, MIN)
- PROCESS_CORE_ENDSTOP(Y,MIN,Z,MAX);
- #elif CORE_DIAG(YZ, Y, MAX)
- PROCESS_CORE_ENDSTOP(Y,MAX,Z,MAX);
- #endif
- #endif
- }
- }
- #endif
-
- #if HAS_I_AXIS
- if (stepper.axis_is_moving(I_AXIS)) {
- if (stepper.motor_direction(I_AXIS_HEAD)) { // -direction
- #if HAS_I_MIN || (I_SPI_SENSORLESS && I_HOME_TO_MIN)
- PROCESS_ENDSTOP(I, MIN);
- #endif
- }
- else { // +direction
- #if HAS_I_MAX || (I_SPI_SENSORLESS && I_HOME_TO_MAX)
- PROCESS_ENDSTOP(I, MAX);
- #endif
- }
- }
- #endif
-
- #if HAS_J_AXIS
- if (stepper.axis_is_moving(J_AXIS)) {
- if (stepper.motor_direction(J_AXIS_HEAD)) { // -direction
- #if HAS_J_MIN || (J_SPI_SENSORLESS && J_HOME_TO_MIN)
- PROCESS_ENDSTOP(J, MIN);
- #endif
- }
- else { // +direction
- #if HAS_J_MAX || (J_SPI_SENSORLESS && J_HOME_TO_MAX)
- PROCESS_ENDSTOP(J, MAX);
- #endif
- }
- }
- #endif
-
- #if HAS_K_AXIS
- if (stepper.axis_is_moving(K_AXIS)) {
- if (stepper.motor_direction(K_AXIS_HEAD)) { // -direction
- #if HAS_K_MIN || (K_SPI_SENSORLESS && K_HOME_TO_MIN)
- PROCESS_ENDSTOP(K, MIN);
- #endif
- }
- else { // +direction
- #if HAS_K_MAX || (K_SPI_SENSORLESS && K_HOME_TO_MAX)
- PROCESS_ENDSTOP(K, MAX);
- #endif
- }
- }
- #endif
-
- #if HAS_U_AXIS
- if (stepper.axis_is_moving(U_AXIS)) {
- if (stepper.motor_direction(U_AXIS_HEAD)) { // -direction
- #if HAS_U_MIN || (U_SPI_SENSORLESS && U_HOME_TO_MIN)
- PROCESS_ENDSTOP(U, MIN);
- #endif
- }
- else { // +direction
- #if HAS_U_MAX || (U_SPI_SENSORLESS && U_HOME_TO_MAX)
- PROCESS_ENDSTOP(U, MAX);
- #endif
- }
- }
- #endif
-
- #if HAS_V_AXIS
- if (stepper.axis_is_moving(V_AXIS)) {
- if (stepper.motor_direction(V_AXIS_HEAD)) { // -direction
- #if HAS_V_MIN || (V_SPI_SENSORLESS && V_HOME_TO_MIN)
- PROCESS_ENDSTOP(V, MIN);
- #endif
- }
- else { // +direction
- #if HAS_V_MAX || (V_SPI_SENSORLESS && V_HOME_TO_MAX)
- PROCESS_ENDSTOP(V, MAX);
- #endif
- }
- }
- #endif
-
- #if HAS_W_AXIS
- if (stepper.axis_is_moving(W_AXIS)) {
- if (stepper.motor_direction(W_AXIS_HEAD)) { // -direction
- #if HAS_W_MIN || (W_SPI_SENSORLESS && W_HOME_TO_MIN)
- PROCESS_ENDSTOP(W, MIN);
- #endif
- }
- else { // +direction
- #if HAS_W_MAX || (W_SPI_SENSORLESS && W_HOME_TO_MAX)
- PROCESS_ENDSTOP(W, MAX);
- #endif
- }
- }
- #endif
- } // Endstops::update()
-
- #if ENABLED(SPI_ENDSTOPS)
-
- // Called from idle() to read Trinamic stall states
- bool Endstops::tmc_spi_homing_check() {
- bool hit = false;
- #if X_SPI_SENSORLESS
- if (tmc_spi_homing.x && (stepperX.test_stall_status()
- #if ANY(CORE_IS_XY, MARKFORGED_XY, MARKFORGED_YX) && Y_SPI_SENSORLESS
- || stepperY.test_stall_status()
- #elif CORE_IS_XZ && Z_SPI_SENSORLESS
- || stepperZ.test_stall_status()
- #endif
- )) {
- SBI(live_state, X_ENDSTOP);
- hit = true;
- }
- #endif
- #if Y_SPI_SENSORLESS
- if (tmc_spi_homing.y && (stepperY.test_stall_status()
- #if ANY(CORE_IS_XY, MARKFORGED_XY, MARKFORGED_YX) && X_SPI_SENSORLESS
- || stepperX.test_stall_status()
- #elif CORE_IS_YZ && Z_SPI_SENSORLESS
- || stepperZ.test_stall_status()
- #endif
- )) {
- SBI(live_state, Y_ENDSTOP);
- hit = true;
- }
- #endif
- #if Z_SPI_SENSORLESS
- if (tmc_spi_homing.z && (stepperZ.test_stall_status()
- #if CORE_IS_XZ && X_SPI_SENSORLESS
- || stepperX.test_stall_status()
- #elif CORE_IS_YZ && Y_SPI_SENSORLESS
- || stepperY.test_stall_status()
- #endif
- )) {
- SBI(live_state, Z_ENDSTOP);
- hit = true;
- }
- #endif
- #if I_SPI_SENSORLESS
- if (tmc_spi_homing.i && stepperI.test_stall_status()) {
- SBI(live_state, I_ENDSTOP);
- hit = true;
- }
- #endif
- #if J_SPI_SENSORLESS
- if (tmc_spi_homing.j && stepperJ.test_stall_status()) {
- SBI(live_state, J_ENDSTOP);
- hit = true;
- }
- #endif
- #if K_SPI_SENSORLESS
- if (tmc_spi_homing.k && stepperK.test_stall_status()) {
- SBI(live_state, K_ENDSTOP);
- hit = true;
- }
- #endif
- #if U_SPI_SENSORLESS
- if (tmc_spi_homing.u && stepperU.test_stall_status()) {
- SBI(live_state, U_ENDSTOP);
- hit = true;
- }
- #endif
- #if V_SPI_SENSORLESS
- if (tmc_spi_homing.v && stepperV.test_stall_status()) {
- SBI(live_state, V_ENDSTOP);
- hit = true;
- }
- #endif
- #if W_SPI_SENSORLESS
- if (tmc_spi_homing.w && stepperW.test_stall_status()) {
- SBI(live_state, W_ENDSTOP);
- hit = true;
- }
- #endif
-
- if (TERN0(ENDSTOP_INTERRUPTS_FEATURE, hit)) update();
-
- return hit;
- }
-
- void Endstops::clear_endstop_state() {
- TERN_(X_SPI_SENSORLESS, CBI(live_state, X_ENDSTOP));
- TERN_(Y_SPI_SENSORLESS, CBI(live_state, Y_ENDSTOP));
- TERN_(Z_SPI_SENSORLESS, CBI(live_state, Z_ENDSTOP));
- TERN_(I_SPI_SENSORLESS, CBI(live_state, I_ENDSTOP));
- TERN_(J_SPI_SENSORLESS, CBI(live_state, J_ENDSTOP));
- TERN_(K_SPI_SENSORLESS, CBI(live_state, K_ENDSTOP));
- TERN_(U_SPI_SENSORLESS, CBI(live_state, U_ENDSTOP));
- TERN_(V_SPI_SENSORLESS, CBI(live_state, V_ENDSTOP));
- TERN_(W_SPI_SENSORLESS, CBI(live_state, W_ENDSTOP));
- }
-
- #endif // SPI_ENDSTOPS
-
- #if ENABLED(PINS_DEBUGGING)
-
- bool Endstops::monitor_flag = false;
-
- /**
- * Monitor Endstops and Z Probe for changes
- *
- * If a change is detected then the LED is toggled and
- * a message is sent out the serial port.
- *
- * Yes, we could miss a rapid back & forth change but
- * that won't matter because this is all manual.
- */
- void Endstops::monitor() {
-
- static uint16_t old_live_state_local = 0;
- static uint8_t local_LED_status = 0;
- uint16_t live_state_local = 0;
-
- #define ES_GET_STATE(S) if (READ(S##_PIN)) SBI(live_state_local, S)
-
- #if HAS_X_MIN
- ES_GET_STATE(X_MIN);
- #endif
- #if HAS_X_MAX
- ES_GET_STATE(X_MAX);
- #endif
- #if HAS_Y_MIN
- ES_GET_STATE(Y_MIN);
- #endif
- #if HAS_Y_MAX
- ES_GET_STATE(Y_MAX);
- #endif
- #if HAS_Z_MIN
- ES_GET_STATE(Z_MIN);
- #endif
- #if HAS_Z_MAX
- ES_GET_STATE(Z_MAX);
- #endif
- #if HAS_Z_MIN_PROBE_PIN
- ES_GET_STATE(Z_MIN_PROBE);
- #endif
- #if HAS_X2_MIN
- ES_GET_STATE(X2_MIN);
- #endif
- #if HAS_X2_MAX
- ES_GET_STATE(X2_MAX);
- #endif
- #if HAS_Y2_MIN
- ES_GET_STATE(Y2_MIN);
- #endif
- #if HAS_Y2_MAX
- ES_GET_STATE(Y2_MAX);
- #endif
- #if HAS_Z2_MIN
- ES_GET_STATE(Z2_MIN);
- #endif
- #if HAS_Z2_MAX
- ES_GET_STATE(Z2_MAX);
- #endif
- #if HAS_Z3_MIN
- ES_GET_STATE(Z3_MIN);
- #endif
- #if HAS_Z3_MAX
- ES_GET_STATE(Z3_MAX);
- #endif
- #if HAS_Z4_MIN
- ES_GET_STATE(Z4_MIN);
- #endif
- #if HAS_Z4_MAX
- ES_GET_STATE(Z4_MAX);
- #endif
- #if HAS_I_MAX
- ES_GET_STATE(I_MAX);
- #endif
- #if HAS_I_MIN
- ES_GET_STATE(I_MIN);
- #endif
- #if HAS_J_MAX
- ES_GET_STATE(J_MAX);
- #endif
- #if HAS_J_MIN
- ES_GET_STATE(J_MIN);
- #endif
- #if HAS_K_MAX
- ES_GET_STATE(K_MAX);
- #endif
- #if HAS_K_MIN
- ES_GET_STATE(K_MIN);
- #endif
- #if HAS_U_MAX
- ES_GET_STATE(U_MAX);
- #endif
- #if HAS_U_MIN
- ES_GET_STATE(U_MIN);
- #endif
- #if HAS_V_MAX
- ES_GET_STATE(V_MAX);
- #endif
- #if HAS_V_MIN
- ES_GET_STATE(V_MIN);
- #endif
- #if HAS_W_MAX
- ES_GET_STATE(W_MAX);
- #endif
- #if HAS_W_MIN
- ES_GET_STATE(W_MIN);
- #endif
-
- uint16_t endstop_change = live_state_local ^ old_live_state_local;
- #define ES_REPORT_CHANGE(S) if (TEST(endstop_change, S)) SERIAL_ECHOPGM(" " STRINGIFY(S) ":", TEST(live_state_local, S))
-
- if (endstop_change) {
- #if HAS_X_MIN
- ES_REPORT_CHANGE(X_MIN);
- #endif
- #if HAS_X_MAX
- ES_REPORT_CHANGE(X_MAX);
- #endif
- #if HAS_Y_MIN
- ES_REPORT_CHANGE(Y_MIN);
- #endif
- #if HAS_Y_MAX
- ES_REPORT_CHANGE(Y_MAX);
- #endif
- #if HAS_Z_MIN
- ES_REPORT_CHANGE(Z_MIN);
- #endif
- #if HAS_Z_MAX
- ES_REPORT_CHANGE(Z_MAX);
- #endif
- #if HAS_Z_MIN_PROBE_PIN
- ES_REPORT_CHANGE(Z_MIN_PROBE);
- #endif
- #if HAS_X2_MIN
- ES_REPORT_CHANGE(X2_MIN);
- #endif
- #if HAS_X2_MAX
- ES_REPORT_CHANGE(X2_MAX);
- #endif
- #if HAS_Y2_MIN
- ES_REPORT_CHANGE(Y2_MIN);
- #endif
- #if HAS_Y2_MAX
- ES_REPORT_CHANGE(Y2_MAX);
- #endif
- #if HAS_Z2_MIN
- ES_REPORT_CHANGE(Z2_MIN);
- #endif
- #if HAS_Z2_MAX
- ES_REPORT_CHANGE(Z2_MAX);
- #endif
- #if HAS_Z3_MIN
- ES_REPORT_CHANGE(Z3_MIN);
- #endif
- #if HAS_Z3_MAX
- ES_REPORT_CHANGE(Z3_MAX);
- #endif
- #if HAS_Z4_MIN
- ES_REPORT_CHANGE(Z4_MIN);
- #endif
- #if HAS_Z4_MAX
- ES_REPORT_CHANGE(Z4_MAX);
- #endif
- #if HAS_I_MIN
- ES_REPORT_CHANGE(I_MIN);
- #endif
- #if HAS_I_MAX
- ES_REPORT_CHANGE(I_MAX);
- #endif
- #if HAS_J_MIN
- ES_REPORT_CHANGE(J_MIN);
- #endif
- #if HAS_J_MAX
- ES_REPORT_CHANGE(J_MAX);
- #endif
- #if HAS_K_MIN
- ES_REPORT_CHANGE(K_MIN);
- #endif
- #if HAS_K_MAX
- ES_REPORT_CHANGE(K_MAX);
- #endif
- #if HAS_U_MIN
- ES_REPORT_CHANGE(U_MIN);
- #endif
- #if HAS_U_MAX
- ES_REPORT_CHANGE(U_MAX);
- #endif
- #if HAS_V_MIN
- ES_REPORT_CHANGE(V_MIN);
- #endif
- #if HAS_V_MAX
- ES_REPORT_CHANGE(V_MAX);
- #endif
- #if HAS_W_MIN
- ES_REPORT_CHANGE(W_MIN);
- #endif
- #if HAS_W_MAX
- ES_REPORT_CHANGE(W_MAX);
- #endif
-
- SERIAL_ECHOLNPGM("\n");
- hal.set_pwm_duty(pin_t(LED_PIN), local_LED_status);
- local_LED_status ^= 255;
- old_live_state_local = live_state_local;
- }
- }
-
- #endif // PINS_DEBUGGING
|