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

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