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 27KB

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