My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

endstops.cpp 30KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * endstops.cpp - A singleton object to manage endstops
  24. */
  25. #include "endstops.h"
  26. #include "stepper.h"
  27. #include "../MarlinCore.h"
  28. #include "../sd/cardreader.h"
  29. #include "temperature.h"
  30. #include "../lcd/marlinui.h"
  31. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  32. #include HAL_PATH(../HAL, endstop_interrupts.h)
  33. #endif
  34. #if BOTH(SD_ABORT_ON_ENDSTOP_HIT, SDSUPPORT)
  35. #include "printcounter.h" // for print_job_timer
  36. #endif
  37. #if ENABLED(BLTOUCH)
  38. #include "../feature/bltouch.h"
  39. #endif
  40. #if ENABLED(JOYSTICK)
  41. #include "../feature/joystick.h"
  42. #endif
  43. #if HAS_BED_PROBE
  44. #include "probe.h"
  45. #endif
  46. Endstops endstops;
  47. // private:
  48. bool Endstops::enabled, Endstops::enabled_globally; // Initialized by settings.load()
  49. volatile uint8_t Endstops::hit_state;
  50. Endstops::esbits_t Endstops::live_state = 0;
  51. #if ENDSTOP_NOISE_THRESHOLD
  52. Endstops::esbits_t Endstops::validated_live_state;
  53. uint8_t Endstops::endstop_poll_count;
  54. #endif
  55. #if HAS_BED_PROBE
  56. volatile bool Endstops::z_probe_enabled = false;
  57. #endif
  58. // Initialized by settings.load()
  59. #if ENABLED(X_DUAL_ENDSTOPS)
  60. float Endstops::x2_endstop_adj;
  61. #endif
  62. #if ENABLED(Y_DUAL_ENDSTOPS)
  63. float Endstops::y2_endstop_adj;
  64. #endif
  65. #if ENABLED(Z_MULTI_ENDSTOPS)
  66. float Endstops::z2_endstop_adj;
  67. #if NUM_Z_STEPPER_DRIVERS >= 3
  68. float Endstops::z3_endstop_adj;
  69. #if NUM_Z_STEPPER_DRIVERS >= 4
  70. float Endstops::z4_endstop_adj;
  71. #endif
  72. #endif
  73. #endif
  74. #if ENABLED(SPI_ENDSTOPS)
  75. Endstops::tmc_spi_homing_t Endstops::tmc_spi_homing; // = 0
  76. #endif
  77. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  78. millis_t sg_guard_period; // = 0
  79. #endif
  80. /**
  81. * Class and Instance Methods
  82. */
  83. void Endstops::init() {
  84. #if HAS_X_MIN
  85. #if ENABLED(ENDSTOPPULLUP_XMIN)
  86. SET_INPUT_PULLUP(X_MIN_PIN);
  87. #elif ENABLED(ENDSTOPPULLDOWN_XMIN)
  88. SET_INPUT_PULLDOWN(X_MIN_PIN);
  89. #else
  90. SET_INPUT(X_MIN_PIN);
  91. #endif
  92. #endif
  93. #if HAS_X2_MIN
  94. #if ENABLED(ENDSTOPPULLUP_XMIN)
  95. SET_INPUT_PULLUP(X2_MIN_PIN);
  96. #elif ENABLED(ENDSTOPPULLDOWN_XMIN)
  97. SET_INPUT_PULLDOWN(X2_MIN_PIN);
  98. #else
  99. SET_INPUT(X2_MIN_PIN);
  100. #endif
  101. #endif
  102. #if HAS_Y_MIN
  103. #if ENABLED(ENDSTOPPULLUP_YMIN)
  104. SET_INPUT_PULLUP(Y_MIN_PIN);
  105. #elif ENABLED(ENDSTOPPULLDOWN_YMIN)
  106. SET_INPUT_PULLDOWN(Y_MIN_PIN);
  107. #else
  108. SET_INPUT(Y_MIN_PIN);
  109. #endif
  110. #endif
  111. #if HAS_Y2_MIN
  112. #if ENABLED(ENDSTOPPULLUP_YMIN)
  113. SET_INPUT_PULLUP(Y2_MIN_PIN);
  114. #elif ENABLED(ENDSTOPPULLDOWN_YMIN)
  115. SET_INPUT_PULLDOWN(Y2_MIN_PIN);
  116. #else
  117. SET_INPUT(Y2_MIN_PIN);
  118. #endif
  119. #endif
  120. #if HAS_Z_MIN
  121. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  122. SET_INPUT_PULLUP(Z_MIN_PIN);
  123. #elif ENABLED(ENDSTOPPULLDOWN_ZMIN)
  124. SET_INPUT_PULLDOWN(Z_MIN_PIN);
  125. #else
  126. SET_INPUT(Z_MIN_PIN);
  127. #endif
  128. #endif
  129. #if HAS_Z2_MIN
  130. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  131. SET_INPUT_PULLUP(Z2_MIN_PIN);
  132. #elif ENABLED(ENDSTOPPULLDOWN_ZMIN)
  133. SET_INPUT_PULLDOWN(Z2_MIN_PIN);
  134. #else
  135. SET_INPUT(Z2_MIN_PIN);
  136. #endif
  137. #endif
  138. #if HAS_Z3_MIN
  139. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  140. SET_INPUT_PULLUP(Z3_MIN_PIN);
  141. #elif ENABLED(ENDSTOPPULLDOWN_ZMIN)
  142. SET_INPUT_PULLDOWN(Z3_MIN_PIN);
  143. #else
  144. SET_INPUT(Z3_MIN_PIN);
  145. #endif
  146. #endif
  147. #if HAS_Z4_MIN
  148. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  149. SET_INPUT_PULLUP(Z4_MIN_PIN);
  150. #elif ENABLED(ENDSTOPPULLDOWN_ZMIN)
  151. SET_INPUT_PULLDOWN(Z4_MIN_PIN);
  152. #else
  153. SET_INPUT(Z4_MIN_PIN);
  154. #endif
  155. #endif
  156. #if HAS_X_MAX
  157. #if ENABLED(ENDSTOPPULLUP_XMAX)
  158. SET_INPUT_PULLUP(X_MAX_PIN);
  159. #elif ENABLED(ENDSTOPPULLDOWN_XMAX)
  160. SET_INPUT_PULLDOWN(X_MAX_PIN);
  161. #else
  162. SET_INPUT(X_MAX_PIN);
  163. #endif
  164. #endif
  165. #if HAS_X2_MAX
  166. #if ENABLED(ENDSTOPPULLUP_XMAX)
  167. SET_INPUT_PULLUP(X2_MAX_PIN);
  168. #elif ENABLED(ENDSTOPPULLDOWN_XMAX)
  169. SET_INPUT_PULLDOWN(X2_MAX_PIN);
  170. #else
  171. SET_INPUT(X2_MAX_PIN);
  172. #endif
  173. #endif
  174. #if HAS_Y_MAX
  175. #if ENABLED(ENDSTOPPULLUP_YMAX)
  176. SET_INPUT_PULLUP(Y_MAX_PIN);
  177. #elif ENABLED(ENDSTOPPULLDOWN_YMAX)
  178. SET_INPUT_PULLDOWN(Y_MAX_PIN);
  179. #else
  180. SET_INPUT(Y_MAX_PIN);
  181. #endif
  182. #endif
  183. #if HAS_Y2_MAX
  184. #if ENABLED(ENDSTOPPULLUP_YMAX)
  185. SET_INPUT_PULLUP(Y2_MAX_PIN);
  186. #elif ENABLED(ENDSTOPPULLDOWN_YMAX)
  187. SET_INPUT_PULLDOWN(Y2_MAX_PIN);
  188. #else
  189. SET_INPUT(Y2_MAX_PIN);
  190. #endif
  191. #endif
  192. #if HAS_Z_MAX
  193. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  194. SET_INPUT_PULLUP(Z_MAX_PIN);
  195. #elif ENABLED(ENDSTOPPULLDOWN_ZMAX)
  196. SET_INPUT_PULLDOWN(Z_MAX_PIN);
  197. #else
  198. SET_INPUT(Z_MAX_PIN);
  199. #endif
  200. #endif
  201. #if HAS_Z2_MAX
  202. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  203. SET_INPUT_PULLUP(Z2_MAX_PIN);
  204. #elif ENABLED(ENDSTOPPULLDOWN_ZMAX)
  205. SET_INPUT_PULLDOWN(Z2_MAX_PIN);
  206. #else
  207. SET_INPUT(Z2_MAX_PIN);
  208. #endif
  209. #endif
  210. #if HAS_Z3_MAX
  211. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  212. SET_INPUT_PULLUP(Z3_MAX_PIN);
  213. #elif ENABLED(ENDSTOPPULLDOWN_ZMAX)
  214. SET_INPUT_PULLDOWN(Z3_MAX_PIN);
  215. #else
  216. SET_INPUT(Z3_MAX_PIN);
  217. #endif
  218. #endif
  219. #if HAS_Z4_MAX
  220. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  221. SET_INPUT_PULLUP(Z4_MAX_PIN);
  222. #elif ENABLED(ENDSTOPPULLDOWN_ZMAX)
  223. SET_INPUT_PULLDOWN(Z4_MAX_PIN);
  224. #else
  225. SET_INPUT(Z4_MAX_PIN);
  226. #endif
  227. #endif
  228. #if PIN_EXISTS(CALIBRATION)
  229. #if ENABLED(CALIBRATION_PIN_PULLUP)
  230. SET_INPUT_PULLUP(CALIBRATION_PIN);
  231. #elif ENABLED(CALIBRATION_PIN_PULLDOWN)
  232. SET_INPUT_PULLDOWN(CALIBRATION_PIN);
  233. #else
  234. SET_INPUT(CALIBRATION_PIN);
  235. #endif
  236. #endif
  237. #if HAS_CUSTOM_PROBE_PIN
  238. #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
  239. SET_INPUT_PULLUP(Z_MIN_PROBE_PIN);
  240. #elif ENABLED(ENDSTOPPULLDOWN_ZMIN_PROBE)
  241. SET_INPUT_PULLDOWN(Z_MIN_PROBE_PIN);
  242. #else
  243. SET_INPUT(Z_MIN_PROBE_PIN);
  244. #endif
  245. #endif
  246. #if ENABLED(PROBE_ACTIVATION_SWITCH)
  247. SET_INPUT(PROBE_ACTIVATION_SWITCH_PIN);
  248. #endif
  249. TERN_(PROBE_TARE, probe.tare());
  250. TERN_(ENDSTOP_INTERRUPTS_FEATURE, setup_endstop_interrupts());
  251. // Enable endstops
  252. enable_globally(ENABLED(ENDSTOPS_ALWAYS_ON_DEFAULT));
  253. } // Endstops::init
  254. // Called at ~1KHz from Temperature ISR: Poll endstop state if required
  255. void Endstops::poll() {
  256. TERN_(PINS_DEBUGGING, run_monitor()); // Report changes in endstop status
  257. #if DISABLED(ENDSTOP_INTERRUPTS_FEATURE)
  258. update();
  259. #elif ENDSTOP_NOISE_THRESHOLD
  260. if (endstop_poll_count) update();
  261. #endif
  262. }
  263. void Endstops::enable_globally(const bool onoff) {
  264. enabled_globally = enabled = onoff;
  265. resync();
  266. }
  267. // Enable / disable endstop checking
  268. void Endstops::enable(const bool onoff) {
  269. enabled = onoff;
  270. resync();
  271. }
  272. // Disable / Enable endstops based on ENSTOPS_ONLY_FOR_HOMING and global enable
  273. void Endstops::not_homing() {
  274. enabled = enabled_globally;
  275. }
  276. #if ENABLED(VALIDATE_HOMING_ENDSTOPS)
  277. // If the last move failed to trigger an endstop, call kill
  278. void Endstops::validate_homing_move() {
  279. if (trigger_state()) hit_on_purpose();
  280. else kill(GET_TEXT(MSG_KILL_HOMING_FAILED));
  281. }
  282. #endif
  283. // Enable / disable endstop z-probe checking
  284. #if HAS_BED_PROBE
  285. void Endstops::enable_z_probe(const bool onoff) {
  286. z_probe_enabled = onoff;
  287. resync();
  288. }
  289. #endif
  290. // Get the stable endstop states when enabled
  291. void Endstops::resync() {
  292. if (!abort_enabled()) return; // If endstops/probes are disabled the loop below can hang
  293. // Wait for Temperature ISR to run at least once (runs at 1KHz)
  294. TERN(ENDSTOP_INTERRUPTS_FEATURE, update(), safe_delay(2));
  295. while (TERN0(ENDSTOP_NOISE_THRESHOLD, endstop_poll_count)) safe_delay(1);
  296. }
  297. #if ENABLED(PINS_DEBUGGING)
  298. void Endstops::run_monitor() {
  299. if (!monitor_flag) return;
  300. static uint8_t monitor_count = 16; // offset this check from the others
  301. monitor_count += _BV(1); // 15 Hz
  302. monitor_count &= 0x7F;
  303. if (!monitor_count) monitor(); // report changes in endstop status
  304. }
  305. #endif
  306. void Endstops::event_handler() {
  307. static uint8_t prev_hit_state; // = 0
  308. if (hit_state == prev_hit_state) return;
  309. prev_hit_state = hit_state;
  310. if (hit_state) {
  311. #if HAS_WIRED_LCD
  312. char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
  313. #define _SET_STOP_CHAR(A,C) (chr## A = C)
  314. #else
  315. #define _SET_STOP_CHAR(A,C) ;
  316. #endif
  317. #define _ENDSTOP_HIT_ECHO(A,C) do{ \
  318. SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", planner.triggered_position_mm(_AXIS(A))); \
  319. _SET_STOP_CHAR(A,C); }while(0)
  320. #define _ENDSTOP_HIT_TEST(A,C) \
  321. if (TEST(hit_state, A ##_MIN) || TEST(hit_state, A ##_MAX)) \
  322. _ENDSTOP_HIT_ECHO(A,C)
  323. #define ENDSTOP_HIT_TEST_X() _ENDSTOP_HIT_TEST(X,'X')
  324. #define ENDSTOP_HIT_TEST_Y() _ENDSTOP_HIT_TEST(Y,'Y')
  325. #define ENDSTOP_HIT_TEST_Z() _ENDSTOP_HIT_TEST(Z,'Z')
  326. SERIAL_ECHO_START();
  327. SERIAL_ECHOPGM(STR_ENDSTOPS_HIT);
  328. ENDSTOP_HIT_TEST_X();
  329. ENDSTOP_HIT_TEST_Y();
  330. ENDSTOP_HIT_TEST_Z();
  331. #if HAS_CUSTOM_PROBE_PIN
  332. #define P_AXIS Z_AXIS
  333. if (TEST(hit_state, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
  334. #endif
  335. SERIAL_EOL();
  336. TERN_(HAS_WIRED_LCD, ui.status_printf_P(0, PSTR(S_FMT " %c %c %c %c"), GET_TEXT(MSG_LCD_ENDSTOPS), chrX, chrY, chrZ, chrP));
  337. #if BOTH(SD_ABORT_ON_ENDSTOP_HIT, SDSUPPORT)
  338. if (planner.abort_on_endstop_hit) {
  339. card.endFilePrint();
  340. quickstop_stepper();
  341. thermalManager.disable_all_heaters();
  342. print_job_timer.stop();
  343. }
  344. #endif
  345. }
  346. }
  347. static void print_es_state(const bool is_hit, PGM_P const label=nullptr) {
  348. if (label) serialprintPGM(label);
  349. SERIAL_ECHOPGM(": ");
  350. serialprintPGM(is_hit ? PSTR(STR_ENDSTOP_HIT) : PSTR(STR_ENDSTOP_OPEN));
  351. SERIAL_EOL();
  352. }
  353. void _O2 Endstops::report_states() {
  354. TERN_(BLTOUCH, bltouch._set_SW_mode());
  355. SERIAL_ECHOLNPGM(STR_M119_REPORT);
  356. #define ES_REPORT(S) print_es_state(READ(S##_PIN) != S##_ENDSTOP_INVERTING, PSTR(STR_##S))
  357. #if HAS_X_MIN
  358. ES_REPORT(X_MIN);
  359. #endif
  360. #if HAS_X2_MIN
  361. ES_REPORT(X2_MIN);
  362. #endif
  363. #if HAS_X_MAX
  364. ES_REPORT(X_MAX);
  365. #endif
  366. #if HAS_X2_MAX
  367. ES_REPORT(X2_MAX);
  368. #endif
  369. #if HAS_Y_MIN
  370. ES_REPORT(Y_MIN);
  371. #endif
  372. #if HAS_Y2_MIN
  373. ES_REPORT(Y2_MIN);
  374. #endif
  375. #if HAS_Y_MAX
  376. ES_REPORT(Y_MAX);
  377. #endif
  378. #if HAS_Y2_MAX
  379. ES_REPORT(Y2_MAX);
  380. #endif
  381. #if HAS_Z_MIN
  382. ES_REPORT(Z_MIN);
  383. #endif
  384. #if HAS_Z2_MIN
  385. ES_REPORT(Z2_MIN);
  386. #endif
  387. #if HAS_Z3_MIN
  388. ES_REPORT(Z3_MIN);
  389. #endif
  390. #if HAS_Z4_MIN
  391. ES_REPORT(Z4_MIN);
  392. #endif
  393. #if HAS_Z_MAX
  394. ES_REPORT(Z_MAX);
  395. #endif
  396. #if HAS_Z2_MAX
  397. ES_REPORT(Z2_MAX);
  398. #endif
  399. #if HAS_Z3_MAX
  400. ES_REPORT(Z3_MAX);
  401. #endif
  402. #if HAS_Z4_MAX
  403. ES_REPORT(Z4_MAX);
  404. #endif
  405. #if BOTH(MARLIN_DEV_MODE, PROBE_ACTIVATION_SWITCH)
  406. print_es_state(READ(PROBE_ACTIVATION_SWITCH_PIN) == PROBE_ACTIVATION_SWITCH_STATE, PSTR(STR_PROBE_EN));
  407. #endif
  408. #if HAS_CUSTOM_PROBE_PIN
  409. print_es_state(PROBE_TRIGGERED(), PSTR(STR_Z_PROBE));
  410. #endif
  411. #if HAS_FILAMENT_SENSOR
  412. #if NUM_RUNOUT_SENSORS == 1
  413. print_es_state(READ(FIL_RUNOUT1_PIN) != FIL_RUNOUT1_STATE, PSTR(STR_FILAMENT_RUNOUT_SENSOR));
  414. #else
  415. #define _CASE_RUNOUT(N) case N: pin = FIL_RUNOUT##N##_PIN; state = FIL_RUNOUT##N##_STATE; break;
  416. LOOP_S_LE_N(i, 1, NUM_RUNOUT_SENSORS) {
  417. pin_t pin;
  418. uint8_t state;
  419. switch (i) {
  420. default: continue;
  421. REPEAT_S(1, INCREMENT(NUM_RUNOUT_SENSORS), _CASE_RUNOUT)
  422. }
  423. SERIAL_ECHOPGM(STR_FILAMENT_RUNOUT_SENSOR);
  424. if (i > 1) SERIAL_CHAR(' ', '0' + i);
  425. print_es_state(extDigitalRead(pin) != state);
  426. }
  427. #undef _CASE_RUNOUT
  428. #endif
  429. #endif
  430. TERN_(BLTOUCH, bltouch._reset_SW_mode());
  431. TERN_(JOYSTICK_DEBUG, joystick.report());
  432. } // Endstops::report_states
  433. // The following routines are called from an ISR context. It could be the temperature ISR, the
  434. // endstop ISR or the Stepper ISR.
  435. #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
  436. #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
  437. #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
  438. // Check endstops - Could be called from Temperature ISR!
  439. void Endstops::update() {
  440. #if !ENDSTOP_NOISE_THRESHOLD
  441. if (!abort_enabled()) return;
  442. #endif
  443. #define UPDATE_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT_TO(live_state, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
  444. #define COPY_LIVE_STATE(SRC_BIT, DST_BIT) SET_BIT_TO(live_state, DST_BIT, TEST(live_state, SRC_BIT))
  445. #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && NONE(CORE_IS_XY, CORE_IS_XZ, MARKFORGED_XY)
  446. // If G38 command is active check Z_MIN_PROBE for ALL movement
  447. if (G38_move) UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
  448. #endif
  449. // With Dual X, endstops are only checked in the homing direction for the active extruder
  450. #if ENABLED(DUAL_X_CARRIAGE)
  451. #define E0_ACTIVE stepper.last_moved_extruder == 0
  452. #define X_MIN_TEST() ((X_HOME_DIR < 0 && E0_ACTIVE) || (X2_HOME_DIR < 0 && !E0_ACTIVE))
  453. #define X_MAX_TEST() ((X_HOME_DIR > 0 && E0_ACTIVE) || (X2_HOME_DIR > 0 && !E0_ACTIVE))
  454. #else
  455. #define X_MIN_TEST() true
  456. #define X_MAX_TEST() true
  457. #endif
  458. // Use HEAD for core axes, AXIS for others
  459. #if ANY(CORE_IS_XY, CORE_IS_XZ, MARKFORGED_XY)
  460. #define X_AXIS_HEAD X_HEAD
  461. #else
  462. #define X_AXIS_HEAD X_AXIS
  463. #endif
  464. #if ANY(CORE_IS_XY, CORE_IS_YZ, MARKFORGED_XY)
  465. #define Y_AXIS_HEAD Y_HEAD
  466. #else
  467. #define Y_AXIS_HEAD Y_AXIS
  468. #endif
  469. #if CORE_IS_XZ || CORE_IS_YZ
  470. #define Z_AXIS_HEAD Z_HEAD
  471. #else
  472. #define Z_AXIS_HEAD Z_AXIS
  473. #endif
  474. /**
  475. * Check and update endstops
  476. */
  477. #if HAS_X_MIN && !X_SPI_SENSORLESS
  478. UPDATE_ENDSTOP_BIT(X, MIN);
  479. #if ENABLED(X_DUAL_ENDSTOPS)
  480. #if HAS_X2_MIN
  481. UPDATE_ENDSTOP_BIT(X2, MIN);
  482. #else
  483. COPY_LIVE_STATE(X_MIN, X2_MIN);
  484. #endif
  485. #endif
  486. #endif
  487. #if HAS_X_MAX && !X_SPI_SENSORLESS
  488. UPDATE_ENDSTOP_BIT(X, MAX);
  489. #if ENABLED(X_DUAL_ENDSTOPS)
  490. #if HAS_X2_MAX
  491. UPDATE_ENDSTOP_BIT(X2, MAX);
  492. #else
  493. COPY_LIVE_STATE(X_MAX, X2_MAX);
  494. #endif
  495. #endif
  496. #endif
  497. #if HAS_Y_MIN && !Y_SPI_SENSORLESS
  498. UPDATE_ENDSTOP_BIT(Y, MIN);
  499. #if ENABLED(Y_DUAL_ENDSTOPS)
  500. #if HAS_Y2_MIN
  501. UPDATE_ENDSTOP_BIT(Y2, MIN);
  502. #else
  503. COPY_LIVE_STATE(Y_MIN, Y2_MIN);
  504. #endif
  505. #endif
  506. #endif
  507. #if HAS_Y_MAX && !Y_SPI_SENSORLESS
  508. UPDATE_ENDSTOP_BIT(Y, MAX);
  509. #if ENABLED(Y_DUAL_ENDSTOPS)
  510. #if HAS_Y2_MAX
  511. UPDATE_ENDSTOP_BIT(Y2, MAX);
  512. #else
  513. COPY_LIVE_STATE(Y_MAX, Y2_MAX);
  514. #endif
  515. #endif
  516. #endif
  517. #if HAS_Z_MIN && NONE(Z_SPI_SENSORLESS, Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  518. UPDATE_ENDSTOP_BIT(Z, MIN);
  519. #if ENABLED(Z_MULTI_ENDSTOPS)
  520. #if HAS_Z2_MIN
  521. UPDATE_ENDSTOP_BIT(Z2, MIN);
  522. #else
  523. COPY_LIVE_STATE(Z_MIN, Z2_MIN);
  524. #endif
  525. #if NUM_Z_STEPPER_DRIVERS >= 3
  526. #if HAS_Z3_MIN
  527. UPDATE_ENDSTOP_BIT(Z3, MIN);
  528. #else
  529. COPY_LIVE_STATE(Z_MIN, Z3_MIN);
  530. #endif
  531. #endif
  532. #if NUM_Z_STEPPER_DRIVERS >= 4
  533. #if HAS_Z4_MIN
  534. UPDATE_ENDSTOP_BIT(Z4, MIN);
  535. #else
  536. COPY_LIVE_STATE(Z_MIN, Z4_MIN);
  537. #endif
  538. #endif
  539. #endif
  540. #endif
  541. #if HAS_BED_PROBE
  542. // When closing the gap check the enabled probe
  543. if (true
  544. #if ENABLED(PROBE_ACTIVATION_SWITCH)
  545. || READ(PROBE_ACTIVATION_SWITCH_PIN) == PROBE_ACTIVATION_SWITCH_STATE
  546. #endif
  547. ) UPDATE_ENDSTOP_BIT(Z, TERN(HAS_CUSTOM_PROBE_PIN, MIN_PROBE, MIN));
  548. #endif
  549. #if HAS_Z_MAX && !Z_SPI_SENSORLESS
  550. // Check both Z dual endstops
  551. #if ENABLED(Z_MULTI_ENDSTOPS)
  552. UPDATE_ENDSTOP_BIT(Z, MAX);
  553. #if HAS_Z2_MAX
  554. UPDATE_ENDSTOP_BIT(Z2, MAX);
  555. #else
  556. COPY_LIVE_STATE(Z_MAX, Z2_MAX);
  557. #endif
  558. #if NUM_Z_STEPPER_DRIVERS >= 3
  559. #if HAS_Z3_MAX
  560. UPDATE_ENDSTOP_BIT(Z3, MAX);
  561. #else
  562. COPY_LIVE_STATE(Z_MAX, Z3_MAX);
  563. #endif
  564. #endif
  565. #if NUM_Z_STEPPER_DRIVERS >= 4
  566. #if HAS_Z4_MAX
  567. UPDATE_ENDSTOP_BIT(Z4, MAX);
  568. #else
  569. COPY_LIVE_STATE(Z_MAX, Z4_MAX);
  570. #endif
  571. #endif
  572. #elif !HAS_CUSTOM_PROBE_PIN || Z_MAX_PIN != Z_MIN_PROBE_PIN
  573. // If this pin isn't the bed probe it's the Z endstop
  574. UPDATE_ENDSTOP_BIT(Z, MAX);
  575. #endif
  576. #endif
  577. #if ENDSTOP_NOISE_THRESHOLD
  578. /**
  579. * Filtering out noise on endstops requires a delayed decision. Let's assume, due to noise,
  580. * that 50% of endstop signal samples are good and 50% are bad (assuming normal distribution
  581. * of random noise). Then the first sample has a 50% chance to be good or bad. The 2nd sample
  582. * also has a 50% chance to be good or bad. The chances of 2 samples both being bad becomes
  583. * 50% of 50%, or 25%. That was the previous implementation of Marlin endstop handling. It
  584. * reduces chances of bad readings in half, at the cost of 1 extra sample period, but chances
  585. * still exist. The only way to reduce them further is to increase the number of samples.
  586. * To reduce the chance to 1% (1/128th) requires 7 samples (adding 7ms of delay).
  587. */
  588. static esbits_t old_live_state;
  589. if (old_live_state != live_state) {
  590. endstop_poll_count = ENDSTOP_NOISE_THRESHOLD;
  591. old_live_state = live_state;
  592. }
  593. else if (endstop_poll_count && !--endstop_poll_count)
  594. validated_live_state = live_state;
  595. if (!abort_enabled()) return;
  596. #endif
  597. // Test the current status of an endstop
  598. #define TEST_ENDSTOP(ENDSTOP) (TEST(state(), ENDSTOP))
  599. // Record endstop was hit
  600. #define _ENDSTOP_HIT(AXIS, MINMAX) SBI(hit_state, _ENDSTOP(AXIS, MINMAX))
  601. // Call the endstop triggered routine for single endstops
  602. #define PROCESS_ENDSTOP(AXIS, MINMAX) do { \
  603. if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX))) { \
  604. _ENDSTOP_HIT(AXIS, MINMAX); \
  605. planner.endstop_triggered(_AXIS(AXIS)); \
  606. } \
  607. }while(0)
  608. // Core Sensorless Homing needs to test an Extra Pin
  609. #define CORE_DIAG(QQ,A,MM) (CORE_IS_##QQ && A##_SENSORLESS && !A##_SPI_SENSORLESS && HAS_##A##_##MM)
  610. #define PROCESS_CORE_ENDSTOP(A1,M1,A2,M2) do { \
  611. if (TEST_ENDSTOP(_ENDSTOP(A1,M1))) { \
  612. _ENDSTOP_HIT(A2,M2); \
  613. planner.endstop_triggered(_AXIS(A2)); \
  614. } \
  615. }while(0)
  616. // Call the endstop triggered routine for dual endstops
  617. #define PROCESS_DUAL_ENDSTOP(A, MINMAX) do { \
  618. const byte dual_hit = TEST_ENDSTOP(_ENDSTOP(A, MINMAX)) | (TEST_ENDSTOP(_ENDSTOP(A##2, MINMAX)) << 1); \
  619. if (dual_hit) { \
  620. _ENDSTOP_HIT(A, MINMAX); \
  621. /* if not performing home or if both endstops were trigged during homing... */ \
  622. if (!stepper.separate_multi_axis || dual_hit == 0b11) \
  623. planner.endstop_triggered(_AXIS(A)); \
  624. } \
  625. }while(0)
  626. #define PROCESS_TRIPLE_ENDSTOP(A, MINMAX) do { \
  627. const byte triple_hit = TEST_ENDSTOP(_ENDSTOP(A, MINMAX)) | (TEST_ENDSTOP(_ENDSTOP(A##2, MINMAX)) << 1) | (TEST_ENDSTOP(_ENDSTOP(A##3, MINMAX)) << 2); \
  628. if (triple_hit) { \
  629. _ENDSTOP_HIT(A, MINMAX); \
  630. /* if not performing home or if both endstops were trigged during homing... */ \
  631. if (!stepper.separate_multi_axis || triple_hit == 0b111) \
  632. planner.endstop_triggered(_AXIS(A)); \
  633. } \
  634. }while(0)
  635. #define PROCESS_QUAD_ENDSTOP(A, MINMAX) do { \
  636. 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); \
  637. if (quad_hit) { \
  638. _ENDSTOP_HIT(A, MINMAX); \
  639. /* if not performing home or if both endstops were trigged during homing... */ \
  640. if (!stepper.separate_multi_axis || quad_hit == 0b1111) \
  641. planner.endstop_triggered(_AXIS(A)); \
  642. } \
  643. }while(0)
  644. #if ENABLED(X_DUAL_ENDSTOPS)
  645. #define PROCESS_ENDSTOP_X(MINMAX) PROCESS_DUAL_ENDSTOP(X, MINMAX)
  646. #else
  647. #define PROCESS_ENDSTOP_X(MINMAX) if (X_##MINMAX##_TEST()) PROCESS_ENDSTOP(X, MINMAX)
  648. #endif
  649. #if ENABLED(Y_DUAL_ENDSTOPS)
  650. #define PROCESS_ENDSTOP_Y(MINMAX) PROCESS_DUAL_ENDSTOP(Y, MINMAX)
  651. #else
  652. #define PROCESS_ENDSTOP_Y(MINMAX) PROCESS_ENDSTOP(Y, MINMAX)
  653. #endif
  654. #if DISABLED(Z_MULTI_ENDSTOPS)
  655. #define PROCESS_ENDSTOP_Z(MINMAX) PROCESS_ENDSTOP(Z, MINMAX)
  656. #elif NUM_Z_STEPPER_DRIVERS == 4
  657. #define PROCESS_ENDSTOP_Z(MINMAX) PROCESS_QUAD_ENDSTOP(Z, MINMAX)
  658. #elif NUM_Z_STEPPER_DRIVERS == 3
  659. #define PROCESS_ENDSTOP_Z(MINMAX) PROCESS_TRIPLE_ENDSTOP(Z, MINMAX)
  660. #else
  661. #define PROCESS_ENDSTOP_Z(MINMAX) PROCESS_DUAL_ENDSTOP(Z, MINMAX)
  662. #endif
  663. #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && NONE(CORE_IS_XY, CORE_IS_XZ, MARKFORGED_XY)
  664. #if ENABLED(G38_PROBE_AWAY)
  665. #define _G38_OPEN_STATE (G38_move >= 4)
  666. #else
  667. #define _G38_OPEN_STATE LOW
  668. #endif
  669. // If G38 command is active check Z_MIN_PROBE for ALL movement
  670. if (G38_move && TEST_ENDSTOP(_ENDSTOP(Z, MIN_PROBE)) != _G38_OPEN_STATE) {
  671. if (stepper.axis_is_moving(X_AXIS)) { _ENDSTOP_HIT(X, MIN); planner.endstop_triggered(X_AXIS); }
  672. else if (stepper.axis_is_moving(Y_AXIS)) { _ENDSTOP_HIT(Y, MIN); planner.endstop_triggered(Y_AXIS); }
  673. else if (stepper.axis_is_moving(Z_AXIS)) { _ENDSTOP_HIT(Z, MIN); planner.endstop_triggered(Z_AXIS); }
  674. G38_did_trigger = true;
  675. }
  676. #endif
  677. // Signal, after validation, if an endstop limit is pressed or not
  678. if (stepper.axis_is_moving(X_AXIS)) {
  679. if (stepper.motor_direction(X_AXIS_HEAD)) { // -direction
  680. #if HAS_X_MIN || (X_SPI_SENSORLESS && X_HOME_DIR < 0)
  681. PROCESS_ENDSTOP_X(MIN);
  682. #if CORE_DIAG(XY, Y, MIN)
  683. PROCESS_CORE_ENDSTOP(Y,MIN,X,MIN);
  684. #elif CORE_DIAG(XY, Y, MAX)
  685. PROCESS_CORE_ENDSTOP(Y,MAX,X,MIN);
  686. #elif CORE_DIAG(XZ, Z, MIN)
  687. PROCESS_CORE_ENDSTOP(Z,MIN,X,MIN);
  688. #elif CORE_DIAG(XZ, Z, MAX)
  689. PROCESS_CORE_ENDSTOP(Z,MAX,X,MIN);
  690. #endif
  691. #endif
  692. }
  693. else { // +direction
  694. #if HAS_X_MAX || (X_SPI_SENSORLESS && X_HOME_DIR > 0)
  695. PROCESS_ENDSTOP_X(MAX);
  696. #if CORE_DIAG(XY, Y, MIN)
  697. PROCESS_CORE_ENDSTOP(Y,MIN,X,MAX);
  698. #elif CORE_DIAG(XY, Y, MAX)
  699. PROCESS_CORE_ENDSTOP(Y,MAX,X,MAX);
  700. #elif CORE_DIAG(XZ, Z, MIN)
  701. PROCESS_CORE_ENDSTOP(Z,MIN,X,MAX);
  702. #elif CORE_DIAG(XZ, Z, MAX)
  703. PROCESS_CORE_ENDSTOP(Z,MAX,X,MAX);
  704. #endif
  705. #endif
  706. }
  707. }
  708. if (stepper.axis_is_moving(Y_AXIS)) {
  709. if (stepper.motor_direction(Y_AXIS_HEAD)) { // -direction
  710. #if HAS_Y_MIN || (Y_SPI_SENSORLESS && Y_HOME_DIR < 0)
  711. PROCESS_ENDSTOP_Y(MIN);
  712. #if CORE_DIAG(XY, X, MIN)
  713. PROCESS_CORE_ENDSTOP(X,MIN,Y,MIN);
  714. #elif CORE_DIAG(XY, X, MAX)
  715. PROCESS_CORE_ENDSTOP(X,MAX,Y,MIN);
  716. #elif CORE_DIAG(YZ, Z, MIN)
  717. PROCESS_CORE_ENDSTOP(Z,MIN,Y,MIN);
  718. #elif CORE_DIAG(YZ, Z, MAX)
  719. PROCESS_CORE_ENDSTOP(Z,MAX,Y,MIN);
  720. #endif
  721. #endif
  722. }
  723. else { // +direction
  724. #if HAS_Y_MAX || (Y_SPI_SENSORLESS && Y_HOME_DIR > 0)
  725. PROCESS_ENDSTOP_Y(MAX);
  726. #if CORE_DIAG(XY, X, MIN)
  727. PROCESS_CORE_ENDSTOP(X,MIN,Y,MAX);
  728. #elif CORE_DIAG(XY, X, MAX)
  729. PROCESS_CORE_ENDSTOP(X,MAX,Y,MAX);
  730. #elif CORE_DIAG(YZ, Z, MIN)
  731. PROCESS_CORE_ENDSTOP(Z,MIN,Y,MAX);
  732. #elif CORE_DIAG(YZ, Z, MAX)
  733. PROCESS_CORE_ENDSTOP(Z,MAX,Y,MAX);
  734. #endif
  735. #endif
  736. }
  737. }
  738. if (stepper.axis_is_moving(Z_AXIS)) {
  739. if (stepper.motor_direction(Z_AXIS_HEAD)) { // Z -direction. Gantry down, bed up.
  740. #if HAS_Z_MIN || (Z_SPI_SENSORLESS && Z_HOME_DIR < 0)
  741. if ( TERN1(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN, z_probe_enabled)
  742. && TERN1(HAS_CUSTOM_PROBE_PIN, !z_probe_enabled)
  743. ) PROCESS_ENDSTOP_Z(MIN);
  744. #if CORE_DIAG(XZ, X, MIN)
  745. PROCESS_CORE_ENDSTOP(X,MIN,Z,MIN);
  746. #elif CORE_DIAG(XZ, X, MAX)
  747. PROCESS_CORE_ENDSTOP(X,MAX,Z,MIN);
  748. #elif CORE_DIAG(YZ, Y, MIN)
  749. PROCESS_CORE_ENDSTOP(Y,MIN,Z,MIN);
  750. #elif CORE_DIAG(YZ, Y, MAX)
  751. PROCESS_CORE_ENDSTOP(Y,MAX,Z,MIN);
  752. #endif
  753. #endif
  754. // When closing the gap check the enabled probe
  755. #if HAS_CUSTOM_PROBE_PIN
  756. if (z_probe_enabled) PROCESS_ENDSTOP(Z, MIN_PROBE);
  757. #endif
  758. }
  759. else { // Z +direction. Gantry up, bed down.
  760. #if HAS_Z_MAX || (Z_SPI_SENSORLESS && Z_HOME_DIR > 0)
  761. #if ENABLED(Z_MULTI_ENDSTOPS)
  762. PROCESS_ENDSTOP_Z(MAX);
  763. #elif !HAS_CUSTOM_PROBE_PIN || Z_MAX_PIN != Z_MIN_PROBE_PIN // No probe or probe is Z_MIN || Probe is not Z_MAX
  764. PROCESS_ENDSTOP(Z, MAX);
  765. #endif
  766. #if CORE_DIAG(XZ, X, MIN)
  767. PROCESS_CORE_ENDSTOP(X,MIN,Z,MAX);
  768. #elif CORE_DIAG(XZ, X, MAX)
  769. PROCESS_CORE_ENDSTOP(X,MAX,Z,MAX);
  770. #elif CORE_DIAG(YZ, Y, MIN)
  771. PROCESS_CORE_ENDSTOP(Y,MIN,Z,MAX);
  772. #elif CORE_DIAG(YZ, Y, MAX)
  773. PROCESS_CORE_ENDSTOP(Y,MAX,Z,MAX);
  774. #endif
  775. #endif
  776. }
  777. }
  778. } // Endstops::update()
  779. #if ENABLED(SPI_ENDSTOPS)
  780. bool Endstops::tmc_spi_homing_check() {
  781. bool hit = false;
  782. #if X_SPI_SENSORLESS
  783. if (tmc_spi_homing.x && (stepperX.test_stall_status()
  784. #if ANY(CORE_IS_XY, MARKFORGED_XY) && Y_SPI_SENSORLESS
  785. || stepperY.test_stall_status()
  786. #elif CORE_IS_XZ && Z_SPI_SENSORLESS
  787. || stepperZ.test_stall_status()
  788. #endif
  789. )) {
  790. SBI(live_state, X_ENDSTOP);
  791. hit = true;
  792. }
  793. #endif
  794. #if Y_SPI_SENSORLESS
  795. if (tmc_spi_homing.y && (stepperY.test_stall_status()
  796. #if ANY(CORE_IS_XY, MARKFORGED_XY) && X_SPI_SENSORLESS
  797. || stepperX.test_stall_status()
  798. #elif CORE_IS_YZ && Z_SPI_SENSORLESS
  799. || stepperZ.test_stall_status()
  800. #endif
  801. )) {
  802. SBI(live_state, Y_ENDSTOP);
  803. hit = true;
  804. }
  805. #endif
  806. #if Z_SPI_SENSORLESS
  807. if (tmc_spi_homing.z && (stepperZ.test_stall_status()
  808. #if CORE_IS_XZ && X_SPI_SENSORLESS
  809. || stepperX.test_stall_status()
  810. #elif CORE_IS_YZ && Y_SPI_SENSORLESS
  811. || stepperY.test_stall_status()
  812. #endif
  813. )) {
  814. SBI(live_state, Z_ENDSTOP);
  815. hit = true;
  816. }
  817. #endif
  818. if (TERN0(ENDSTOP_INTERRUPTS_FEATURE, hit)) update();
  819. return hit;
  820. }
  821. void Endstops::clear_endstop_state() {
  822. TERN_(X_SPI_SENSORLESS, CBI(live_state, X_ENDSTOP));
  823. TERN_(Y_SPI_SENSORLESS, CBI(live_state, Y_ENDSTOP));
  824. TERN_(Z_SPI_SENSORLESS, CBI(live_state, Z_ENDSTOP));
  825. }
  826. #endif // SPI_ENDSTOPS
  827. #if ENABLED(PINS_DEBUGGING)
  828. bool Endstops::monitor_flag = false;
  829. /**
  830. * Monitor Endstops and Z Probe for changes
  831. *
  832. * If a change is detected then the LED is toggled and
  833. * a message is sent out the serial port.
  834. *
  835. * Yes, we could miss a rapid back & forth change but
  836. * that won't matter because this is all manual.
  837. */
  838. void Endstops::monitor() {
  839. static uint16_t old_live_state_local = 0;
  840. static uint8_t local_LED_status = 0;
  841. uint16_t live_state_local = 0;
  842. #define ES_GET_STATE(S) if (READ(S##_PIN)) SBI(live_state_local, S)
  843. #if HAS_X_MIN
  844. ES_GET_STATE(X_MIN);
  845. #endif
  846. #if HAS_X_MAX
  847. ES_GET_STATE(X_MAX);
  848. #endif
  849. #if HAS_Y_MIN
  850. ES_GET_STATE(Y_MIN);
  851. #endif
  852. #if HAS_Y_MAX
  853. ES_GET_STATE(Y_MAX);
  854. #endif
  855. #if HAS_Z_MIN
  856. ES_GET_STATE(Z_MIN);
  857. #endif
  858. #if HAS_Z_MAX
  859. ES_GET_STATE(Z_MAX);
  860. #endif
  861. #if HAS_Z_MIN_PROBE_PIN
  862. ES_GET_STATE(Z_MIN_PROBE);
  863. #endif
  864. #if HAS_X2_MIN
  865. ES_GET_STATE(X2_MIN);
  866. #endif
  867. #if HAS_X2_MAX
  868. ES_GET_STATE(X2_MAX);
  869. #endif
  870. #if HAS_Y2_MIN
  871. ES_GET_STATE(Y2_MIN);
  872. #endif
  873. #if HAS_Y2_MAX
  874. ES_GET_STATE(Y2_MAX);
  875. #endif
  876. #if HAS_Z2_MIN
  877. ES_GET_STATE(Z2_MIN);
  878. #endif
  879. #if HAS_Z2_MAX
  880. ES_GET_STATE(Z2_MAX);
  881. #endif
  882. #if HAS_Z3_MIN
  883. ES_GET_STATE(Z3_MIN);
  884. #endif
  885. #if HAS_Z3_MAX
  886. ES_GET_STATE(Z3_MAX);
  887. #endif
  888. #if HAS_Z4_MIN
  889. ES_GET_STATE(Z4_MIN);
  890. #endif
  891. #if HAS_Z4_MAX
  892. ES_GET_STATE(Z4_MAX);
  893. #endif
  894. uint16_t endstop_change = live_state_local ^ old_live_state_local;
  895. #define ES_REPORT_CHANGE(S) if (TEST(endstop_change, S)) SERIAL_ECHOPAIR(" " STRINGIFY(S) ":", TEST(live_state_local, S))
  896. if (endstop_change) {
  897. #if HAS_X_MIN
  898. ES_REPORT_CHANGE(X_MIN);
  899. #endif
  900. #if HAS_X_MAX
  901. ES_REPORT_CHANGE(X_MAX);
  902. #endif
  903. #if HAS_Y_MIN
  904. ES_REPORT_CHANGE(Y_MIN);
  905. #endif
  906. #if HAS_Y_MAX
  907. ES_REPORT_CHANGE(Y_MAX);
  908. #endif
  909. #if HAS_Z_MIN
  910. ES_REPORT_CHANGE(Z_MIN);
  911. #endif
  912. #if HAS_Z_MAX
  913. ES_REPORT_CHANGE(Z_MAX);
  914. #endif
  915. #if HAS_Z_MIN_PROBE_PIN
  916. ES_REPORT_CHANGE(Z_MIN_PROBE);
  917. #endif
  918. #if HAS_X2_MIN
  919. ES_REPORT_CHANGE(X2_MIN);
  920. #endif
  921. #if HAS_X2_MAX
  922. ES_REPORT_CHANGE(X2_MAX);
  923. #endif
  924. #if HAS_Y2_MIN
  925. ES_REPORT_CHANGE(Y2_MIN);
  926. #endif
  927. #if HAS_Y2_MAX
  928. ES_REPORT_CHANGE(Y2_MAX);
  929. #endif
  930. #if HAS_Z2_MIN
  931. ES_REPORT_CHANGE(Z2_MIN);
  932. #endif
  933. #if HAS_Z2_MAX
  934. ES_REPORT_CHANGE(Z2_MAX);
  935. #endif
  936. #if HAS_Z3_MIN
  937. ES_REPORT_CHANGE(Z3_MIN);
  938. #endif
  939. #if HAS_Z3_MAX
  940. ES_REPORT_CHANGE(Z3_MAX);
  941. #endif
  942. #if HAS_Z4_MIN
  943. ES_REPORT_CHANGE(Z4_MIN);
  944. #endif
  945. #if HAS_Z4_MAX
  946. ES_REPORT_CHANGE(Z4_MAX);
  947. #endif
  948. SERIAL_ECHOLNPGM("\n");
  949. analogWrite(pin_t(LED_PIN), local_LED_status);
  950. local_LED_status ^= 255;
  951. old_live_state_local = live_state_local;
  952. }
  953. }
  954. #endif // PINS_DEBUGGING