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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. // Clear endstops (i.e., they were hit intentionally) to suppress the report
  227. void Endstops::hit_on_purpose() {
  228. hit_state = 0;
  229. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  230. if (enabled) update(); // If enabling, update state now
  231. #endif
  232. }
  233. // Enable / disable endstop z-probe checking
  234. #if HAS_BED_PROBE
  235. void Endstops::enable_z_probe(bool onoff) {
  236. z_probe_enabled = onoff;
  237. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  238. if (enabled) update(); // If enabling, update state now
  239. #endif
  240. }
  241. #endif
  242. #if ENABLED(PINS_DEBUGGING)
  243. void Endstops::run_monitor() {
  244. if (!monitor_flag) return;
  245. static uint8_t monitor_count = 16; // offset this check from the others
  246. monitor_count += _BV(1); // 15 Hz
  247. monitor_count &= 0x7F;
  248. if (!monitor_count) monitor(); // report changes in endstop status
  249. }
  250. #endif
  251. void Endstops::report_state() {
  252. if (hit_state) {
  253. #if ENABLED(ULTRA_LCD)
  254. char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
  255. #define _SET_STOP_CHAR(A,C) (chr## A = C)
  256. #else
  257. #define _SET_STOP_CHAR(A,C) ;
  258. #endif
  259. #define _ENDSTOP_HIT_ECHO(A,C) do{ \
  260. SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", planner.triggered_position_mm(_AXIS(A))); \
  261. _SET_STOP_CHAR(A,C); }while(0)
  262. #define _ENDSTOP_HIT_TEST(A,C) \
  263. if (TEST(hit_state, A ##_MIN) || TEST(hit_state, A ##_MAX)) \
  264. _ENDSTOP_HIT_ECHO(A,C)
  265. #define ENDSTOP_HIT_TEST_X() _ENDSTOP_HIT_TEST(X,'X')
  266. #define ENDSTOP_HIT_TEST_Y() _ENDSTOP_HIT_TEST(Y,'Y')
  267. #define ENDSTOP_HIT_TEST_Z() _ENDSTOP_HIT_TEST(Z,'Z')
  268. SERIAL_ECHO_START();
  269. SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
  270. ENDSTOP_HIT_TEST_X();
  271. ENDSTOP_HIT_TEST_Y();
  272. ENDSTOP_HIT_TEST_Z();
  273. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  274. #define P_AXIS Z_AXIS
  275. if (TEST(hit_state, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
  276. #endif
  277. SERIAL_EOL();
  278. #if ENABLED(ULTRA_LCD)
  279. lcd_status_printf_P(0, PSTR(MSG_LCD_ENDSTOPS " %c %c %c %c"), chrX, chrY, chrZ, chrP);
  280. #endif
  281. hit_on_purpose();
  282. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && ENABLED(SDSUPPORT)
  283. if (planner.abort_on_endstop_hit) {
  284. card.sdprinting = false;
  285. card.closefile();
  286. quickstop_stepper();
  287. thermalManager.disable_all_heaters(); // switch off all heaters.
  288. }
  289. #endif
  290. }
  291. } // Endstops::report_state
  292. void Endstops::M119() {
  293. SERIAL_PROTOCOLLNPGM(MSG_M119_REPORT);
  294. #define ES_REPORT(AXIS) do{ \
  295. SERIAL_PROTOCOLPGM(MSG_##AXIS); \
  296. SERIAL_PROTOCOLLN(((READ(AXIS##_PIN)^AXIS##_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN)); \
  297. }while(0)
  298. #if HAS_X_MIN
  299. ES_REPORT(X_MIN);
  300. #endif
  301. #if HAS_X2_MIN
  302. ES_REPORT(X2_MIN);
  303. #endif
  304. #if HAS_X_MAX
  305. ES_REPORT(X_MAX);
  306. #endif
  307. #if HAS_X2_MAX
  308. ES_REPORT(X2_MAX);
  309. #endif
  310. #if HAS_Y_MIN
  311. ES_REPORT(Y_MIN);
  312. #endif
  313. #if HAS_Y2_MIN
  314. ES_REPORT(Y2_MIN);
  315. #endif
  316. #if HAS_Y_MAX
  317. ES_REPORT(Y_MAX);
  318. #endif
  319. #if HAS_Y2_MAX
  320. ES_REPORT(Y2_MAX);
  321. #endif
  322. #if HAS_Z_MIN
  323. ES_REPORT(Z_MIN);
  324. #endif
  325. #if HAS_Z2_MIN
  326. ES_REPORT(Z2_MIN);
  327. #endif
  328. #if HAS_Z_MAX
  329. ES_REPORT(Z_MAX);
  330. #endif
  331. #if HAS_Z2_MAX
  332. ES_REPORT(Z2_MAX);
  333. #endif
  334. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  335. SERIAL_PROTOCOLPGM(MSG_Z_PROBE);
  336. SERIAL_PROTOCOLLN(((READ(Z_MIN_PROBE_PIN)^Z_MIN_PROBE_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  337. #endif
  338. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  339. SERIAL_PROTOCOLPGM(MSG_FILAMENT_RUNOUT_SENSOR);
  340. SERIAL_PROTOCOLLN(((READ(FIL_RUNOUT_PIN)^FIL_RUNOUT_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  341. #endif
  342. } // Endstops::M119
  343. // The following routines are called from an ISR context. It could be the temperature ISR, the
  344. // endstop ISR or the Stepper ISR.
  345. #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
  346. #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
  347. #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
  348. // Check endstops - Could be called from ISR!
  349. void Endstops::update() {
  350. #define SET_BIT_TO(N,B,TF) do{ if (TF) SBI(N,B); else CBI(N,B); }while(0)
  351. // UPDATE_ENDSTOP_BIT: set the current endstop bits for an endstop to its status
  352. #define UPDATE_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT_TO(live_state, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
  353. // COPY_BIT: copy the value of SRC_BIT to DST_BIT in DST
  354. #define COPY_BIT(DST, SRC_BIT, DST_BIT) SET_BIT_TO(DST, DST_BIT, TEST(DST, SRC_BIT))
  355. #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && !(CORE_IS_XY || CORE_IS_XZ)
  356. // If G38 command is active check Z_MIN_PROBE for ALL movement
  357. if (G38_move) UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
  358. #endif
  359. // With Dual X, endstops are only checked in the homing direction for the active extruder
  360. #if ENABLED(DUAL_X_CARRIAGE)
  361. #define E0_ACTIVE stepper.movement_extruder() == 0
  362. #define X_MIN_TEST ((X_HOME_DIR < 0 && E0_ACTIVE) || (X2_HOME_DIR < 0 && !E0_ACTIVE))
  363. #define X_MAX_TEST ((X_HOME_DIR > 0 && E0_ACTIVE) || (X2_HOME_DIR > 0 && !E0_ACTIVE))
  364. #else
  365. #define X_MIN_TEST true
  366. #define X_MAX_TEST true
  367. #endif
  368. // Use HEAD for core axes, AXIS for others
  369. #if CORE_IS_XY || CORE_IS_XZ
  370. #define X_AXIS_HEAD X_HEAD
  371. #else
  372. #define X_AXIS_HEAD X_AXIS
  373. #endif
  374. #if CORE_IS_XY || CORE_IS_YZ
  375. #define Y_AXIS_HEAD Y_HEAD
  376. #else
  377. #define Y_AXIS_HEAD Y_AXIS
  378. #endif
  379. #if CORE_IS_XZ || CORE_IS_YZ
  380. #define Z_AXIS_HEAD Z_HEAD
  381. #else
  382. #define Z_AXIS_HEAD Z_AXIS
  383. #endif
  384. /**
  385. * Check and update endstops according to conditions
  386. */
  387. if (stepper.axis_is_moving(X_AXIS)) {
  388. if (stepper.motor_direction(X_AXIS_HEAD)) { // -direction
  389. #if HAS_X_MIN
  390. #if ENABLED(X_DUAL_ENDSTOPS)
  391. UPDATE_ENDSTOP_BIT(X, MIN);
  392. #if HAS_X2_MIN
  393. UPDATE_ENDSTOP_BIT(X2, MIN);
  394. #else
  395. COPY_BIT(live_state, X_MIN, X2_MIN);
  396. #endif
  397. #else
  398. if (X_MIN_TEST) UPDATE_ENDSTOP_BIT(X, MIN);
  399. #endif
  400. #endif
  401. }
  402. else { // +direction
  403. #if HAS_X_MAX
  404. #if ENABLED(X_DUAL_ENDSTOPS)
  405. UPDATE_ENDSTOP_BIT(X, MAX);
  406. #if HAS_X2_MAX
  407. UPDATE_ENDSTOP_BIT(X2, MAX);
  408. #else
  409. COPY_BIT(live_state, X_MAX, X2_MAX);
  410. #endif
  411. #else
  412. if (X_MAX_TEST) UPDATE_ENDSTOP_BIT(X, MAX);
  413. #endif
  414. #endif
  415. }
  416. }
  417. if (stepper.axis_is_moving(Y_AXIS)) {
  418. if (stepper.motor_direction(Y_AXIS_HEAD)) { // -direction
  419. #if HAS_Y_MIN
  420. #if ENABLED(Y_DUAL_ENDSTOPS)
  421. UPDATE_ENDSTOP_BIT(Y, MIN);
  422. #if HAS_Y2_MIN
  423. UPDATE_ENDSTOP_BIT(Y2, MIN);
  424. #else
  425. COPY_BIT(live_state, Y_MIN, Y2_MIN);
  426. #endif
  427. #else
  428. UPDATE_ENDSTOP_BIT(Y, MIN);
  429. #endif
  430. #endif
  431. }
  432. else { // +direction
  433. #if HAS_Y_MAX
  434. #if ENABLED(Y_DUAL_ENDSTOPS)
  435. UPDATE_ENDSTOP_BIT(Y, MAX);
  436. #if HAS_Y2_MAX
  437. UPDATE_ENDSTOP_BIT(Y2, MAX);
  438. #else
  439. COPY_BIT(live_state, Y_MAX, Y2_MAX);
  440. #endif
  441. #else
  442. UPDATE_ENDSTOP_BIT(Y, MAX);
  443. #endif
  444. #endif
  445. }
  446. }
  447. if (stepper.axis_is_moving(Z_AXIS)) {
  448. if (stepper.motor_direction(Z_AXIS_HEAD)) { // Z -direction. Gantry down, bed up.
  449. #if HAS_Z_MIN
  450. #if ENABLED(Z_DUAL_ENDSTOPS)
  451. UPDATE_ENDSTOP_BIT(Z, MIN);
  452. #if HAS_Z2_MIN
  453. UPDATE_ENDSTOP_BIT(Z2, MIN);
  454. #else
  455. COPY_BIT(live_state, Z_MIN, Z2_MIN);
  456. #endif
  457. #else
  458. #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  459. if (z_probe_enabled) UPDATE_ENDSTOP_BIT(Z, MIN);
  460. #else
  461. UPDATE_ENDSTOP_BIT(Z, MIN);
  462. #endif
  463. #endif
  464. #endif
  465. // When closing the gap check the enabled probe
  466. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  467. if (z_probe_enabled) UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
  468. #endif
  469. }
  470. else { // Z +direction. Gantry up, bed down.
  471. #if HAS_Z_MAX
  472. // Check both Z dual endstops
  473. #if ENABLED(Z_DUAL_ENDSTOPS)
  474. UPDATE_ENDSTOP_BIT(Z, MAX);
  475. #if HAS_Z2_MAX
  476. UPDATE_ENDSTOP_BIT(Z2, MAX);
  477. #else
  478. COPY_BIT(live_state, Z_MAX, Z2_MAX);
  479. #endif
  480. // If this pin is not hijacked for the bed probe
  481. // then it belongs to the Z endstop
  482. #elif DISABLED(Z_MIN_PROBE_ENDSTOP) || Z_MAX_PIN != Z_MIN_PROBE_PIN
  483. UPDATE_ENDSTOP_BIT(Z, MAX);
  484. #endif
  485. #endif
  486. }
  487. }
  488. // All endstops were updated.
  489. #if ENABLED(ENDSTOP_NOISE_FILTER)
  490. if (old_live_state != live_state) { // We detected a change. Reinit the timeout
  491. /**
  492. * Filtering out noise on endstops requires a delayed decision. Let's assume, due to noise,
  493. * that 50% of endstop signal samples are good and 50% are bad (assuming normal distribution
  494. * of random noise). Then the first sample has a 50% chance to be good or bad. The 2nd sample
  495. * also has a 50% chance to be good or bad. The chances of 2 samples both being bad becomes
  496. * 50% of 50%, or 25%. That was the previous implementation of Marlin endstop handling. It
  497. * reduces chances of bad readings in half, at the cost of 1 extra sample period, but chances
  498. * still exist. The only way to reduce them further is to increase the number of samples.
  499. * To reduce the chance to 1% (1/128th) requires 7 samples (adding 7ms of delay).
  500. */
  501. endstop_poll_count = 7;
  502. old_live_state = live_state;
  503. }
  504. else if (endstop_poll_count && !--endstop_poll_count)
  505. validated_live_state = live_state;
  506. #else
  507. // Lets accept the new endstop values as valid - We assume hardware filtering of lines
  508. esbits_t validated_live_state = live_state;
  509. #endif
  510. // Endstop readings are validated in validated_live_state
  511. // Test the current status of an endstop
  512. #define TEST_ENDSTOP(ENDSTOP) (TEST(validated_live_state, ENDSTOP))
  513. // Record endstop was hit
  514. #define _ENDSTOP_HIT(AXIS, MINMAX) SBI(hit_state, _ENDSTOP(AXIS, MINMAX))
  515. // Call the endstop triggered routine for single endstops
  516. #define PROCESS_ENDSTOP(AXIS,MINMAX) do { \
  517. if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX))) { \
  518. _ENDSTOP_HIT(AXIS, MINMAX); \
  519. planner.endstop_triggered(_AXIS(AXIS)); \
  520. } \
  521. }while(0)
  522. // Call the endstop triggered routine for single endstops
  523. #define PROCESS_DUAL_ENDSTOP(AXIS1, AXIS2, MINMAX) do { \
  524. if (TEST_ENDSTOP(_ENDSTOP(AXIS1, MINMAX)) || TEST_ENDSTOP(_ENDSTOP(AXIS2, MINMAX))) { \
  525. _ENDSTOP_HIT(AXIS1, MINMAX); \
  526. planner.endstop_triggered(_AXIS(AXIS1)); \
  527. } \
  528. }while(0)
  529. #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && !(CORE_IS_XY || CORE_IS_XZ)
  530. // If G38 command is active check Z_MIN_PROBE for ALL movement
  531. if (G38_move) {
  532. if (TEST_ENDSTOP(_ENDSTOP(Z, MIN_PROBE))) {
  533. if (stepper.axis_is_moving(X_AXIS)) { _ENDSTOP_HIT(X, MIN); planner.endstop_triggered(X_AXIS); }
  534. else if (stepper.axis_is_moving(Y_AXIS)) { _ENDSTOP_HIT(Y, MIN); planner.endstop_triggered(Y_AXIS); }
  535. else if (stepper.axis_is_moving(Z_AXIS)) { _ENDSTOP_HIT(Z, MIN); planner.endstop_triggered(Z_AXIS); }
  536. G38_endstop_hit = true;
  537. }
  538. }
  539. #endif
  540. // Now, we must signal, after validation, if an endstop limit is pressed or not
  541. if (stepper.axis_is_moving(X_AXIS)) {
  542. if (stepper.motor_direction(X_AXIS_HEAD)) { // -direction
  543. #if HAS_X_MIN
  544. #if ENABLED(X_DUAL_ENDSTOPS)
  545. PROCESS_DUAL_ENDSTOP(X, X2, MIN);
  546. #else
  547. if (X_MIN_TEST) PROCESS_ENDSTOP(X, MIN);
  548. #endif
  549. #endif
  550. }
  551. else { // +direction
  552. #if HAS_X_MAX
  553. #if ENABLED(X_DUAL_ENDSTOPS)
  554. PROCESS_DUAL_ENDSTOP(X, X2, MAX);
  555. #else
  556. if (X_MAX_TEST) PROCESS_ENDSTOP(X, MAX);
  557. #endif
  558. #endif
  559. }
  560. }
  561. if (stepper.axis_is_moving(Y_AXIS)) {
  562. if (stepper.motor_direction(Y_AXIS_HEAD)) { // -direction
  563. #if HAS_Y_MIN
  564. #if ENABLED(Y_DUAL_ENDSTOPS)
  565. PROCESS_DUAL_ENDSTOP(Y, Y2, MIN);
  566. #else
  567. PROCESS_ENDSTOP(Y, MIN);
  568. #endif
  569. #endif
  570. }
  571. else { // +direction
  572. #if HAS_Y_MAX
  573. #if ENABLED(Y_DUAL_ENDSTOPS)
  574. PROCESS_DUAL_ENDSTOP(Y, Y2, MAX);
  575. #else
  576. PROCESS_ENDSTOP(Y, MAX);
  577. #endif
  578. #endif
  579. }
  580. }
  581. if (stepper.axis_is_moving(Z_AXIS)) {
  582. if (stepper.motor_direction(Z_AXIS_HEAD)) { // Z -direction. Gantry down, bed up.
  583. #if HAS_Z_MIN
  584. #if ENABLED(Z_DUAL_ENDSTOPS)
  585. PROCESS_DUAL_ENDSTOP(Z, Z2, MIN);
  586. #else
  587. #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  588. if (z_probe_enabled) PROCESS_ENDSTOP(Z, MIN);
  589. #else
  590. PROCESS_ENDSTOP(Z, MIN);
  591. #endif
  592. #endif
  593. #endif
  594. // When closing the gap check the enabled probe
  595. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  596. if (z_probe_enabled) PROCESS_ENDSTOP(Z, MIN_PROBE);
  597. #endif
  598. }
  599. else { // Z +direction. Gantry up, bed down.
  600. #if HAS_Z_MAX
  601. #if ENABLED(Z_DUAL_ENDSTOPS)
  602. PROCESS_DUAL_ENDSTOP(Z, Z2, MAX);
  603. #elif DISABLED(Z_MIN_PROBE_ENDSTOP) || Z_MAX_PIN != Z_MIN_PROBE_PIN
  604. // If this pin is not hijacked for the bed probe
  605. // then it belongs to the Z endstop
  606. PROCESS_ENDSTOP(Z, MAX);
  607. #endif
  608. #endif
  609. }
  610. }
  611. } // Endstops::update()
  612. #if ENABLED(PINS_DEBUGGING)
  613. bool Endstops::monitor_flag = false;
  614. /**
  615. * monitors endstops & Z probe for changes
  616. *
  617. * If a change is detected then the LED is toggled and
  618. * a message is sent out the serial port
  619. *
  620. * Yes, we could miss a rapid back & forth change but
  621. * that won't matter because this is all manual.
  622. *
  623. */
  624. void Endstops::monitor() {
  625. static uint16_t old_live_state_local = 0;
  626. static uint8_t local_LED_status = 0;
  627. uint16_t live_state_local = 0;
  628. #if HAS_X_MIN
  629. if (READ(X_MIN_PIN)) SBI(live_state_local, X_MIN);
  630. #endif
  631. #if HAS_X_MAX
  632. if (READ(X_MAX_PIN)) SBI(live_state_local, X_MAX);
  633. #endif
  634. #if HAS_Y_MIN
  635. if (READ(Y_MIN_PIN)) SBI(live_state_local, Y_MIN);
  636. #endif
  637. #if HAS_Y_MAX
  638. if (READ(Y_MAX_PIN)) SBI(live_state_local, Y_MAX);
  639. #endif
  640. #if HAS_Z_MIN
  641. if (READ(Z_MIN_PIN)) SBI(live_state_local, Z_MIN);
  642. #endif
  643. #if HAS_Z_MAX
  644. if (READ(Z_MAX_PIN)) SBI(live_state_local, Z_MAX);
  645. #endif
  646. #if HAS_Z_MIN_PROBE_PIN
  647. if (READ(Z_MIN_PROBE_PIN)) SBI(live_state_local, Z_MIN_PROBE);
  648. #endif
  649. #if HAS_X2_MIN
  650. if (READ(X2_MIN_PIN)) SBI(live_state_local, X2_MIN);
  651. #endif
  652. #if HAS_X2_MAX
  653. if (READ(X2_MAX_PIN)) SBI(live_state_local, X2_MAX);
  654. #endif
  655. #if HAS_Y2_MIN
  656. if (READ(Y2_MIN_PIN)) SBI(live_state_local, Y2_MIN);
  657. #endif
  658. #if HAS_Y2_MAX
  659. if (READ(Y2_MAX_PIN)) SBI(live_state_local, Y2_MAX);
  660. #endif
  661. #if HAS_Z2_MIN
  662. if (READ(Z2_MIN_PIN)) SBI(live_state_local, Z2_MIN);
  663. #endif
  664. #if HAS_Z2_MAX
  665. if (READ(Z2_MAX_PIN)) SBI(live_state_local, Z2_MAX);
  666. #endif
  667. uint16_t endstop_change = live_state_local ^ old_live_state_local;
  668. if (endstop_change) {
  669. #if HAS_X_MIN
  670. if (TEST(endstop_change, X_MIN)) SERIAL_PROTOCOLPAIR(" X_MIN:", TEST(live_state_local, X_MIN));
  671. #endif
  672. #if HAS_X_MAX
  673. if (TEST(endstop_change, X_MAX)) SERIAL_PROTOCOLPAIR(" X_MAX:", TEST(live_state_local, X_MAX));
  674. #endif
  675. #if HAS_Y_MIN
  676. if (TEST(endstop_change, Y_MIN)) SERIAL_PROTOCOLPAIR(" Y_MIN:", TEST(live_state_local, Y_MIN));
  677. #endif
  678. #if HAS_Y_MAX
  679. if (TEST(endstop_change, Y_MAX)) SERIAL_PROTOCOLPAIR(" Y_MAX:", TEST(live_state_local, Y_MAX));
  680. #endif
  681. #if HAS_Z_MIN
  682. if (TEST(endstop_change, Z_MIN)) SERIAL_PROTOCOLPAIR(" Z_MIN:", TEST(live_state_local, Z_MIN));
  683. #endif
  684. #if HAS_Z_MAX
  685. if (TEST(endstop_change, Z_MAX)) SERIAL_PROTOCOLPAIR(" Z_MAX:", TEST(live_state_local, Z_MAX));
  686. #endif
  687. #if HAS_Z_MIN_PROBE_PIN
  688. if (TEST(endstop_change, Z_MIN_PROBE)) SERIAL_PROTOCOLPAIR(" PROBE:", TEST(live_state_local, Z_MIN_PROBE));
  689. #endif
  690. #if HAS_X2_MIN
  691. if (TEST(endstop_change, X2_MIN)) SERIAL_PROTOCOLPAIR(" X2_MIN:", TEST(live_state_local, X2_MIN));
  692. #endif
  693. #if HAS_X2_MAX
  694. if (TEST(endstop_change, X2_MAX)) SERIAL_PROTOCOLPAIR(" X2_MAX:", TEST(live_state_local, X2_MAX));
  695. #endif
  696. #if HAS_Y2_MIN
  697. if (TEST(endstop_change, Y2_MIN)) SERIAL_PROTOCOLPAIR(" Y2_MIN:", TEST(live_state_local, Y2_MIN));
  698. #endif
  699. #if HAS_Y2_MAX
  700. if (TEST(endstop_change, Y2_MAX)) SERIAL_PROTOCOLPAIR(" Y2_MAX:", TEST(live_state_local, Y2_MAX));
  701. #endif
  702. #if HAS_Z2_MIN
  703. if (TEST(endstop_change, Z2_MIN)) SERIAL_PROTOCOLPAIR(" Z2_MIN:", TEST(live_state_local, Z2_MIN));
  704. #endif
  705. #if HAS_Z2_MAX
  706. if (TEST(endstop_change, Z2_MAX)) SERIAL_PROTOCOLPAIR(" Z2_MAX:", TEST(live_state_local, Z2_MAX));
  707. #endif
  708. SERIAL_PROTOCOLPGM("\n\n");
  709. analogWrite(LED_PIN, local_LED_status);
  710. local_LED_status ^= 255;
  711. old_live_state_local = live_state_local;
  712. }
  713. }
  714. #endif // PINS_DEBUGGING