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

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