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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 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 "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 ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && ENABLED(SDSUPPORT)
  35. #include "printcounter.h" // for print_job_timer
  36. #endif
  37. Endstops endstops;
  38. // private:
  39. bool Endstops::enabled, Endstops::enabled_globally; // Initialized by settings.load()
  40. volatile uint8_t Endstops::hit_state;
  41. Endstops::esbits_t Endstops::live_state = 0;
  42. #if ENDSTOP_NOISE_THRESHOLD
  43. Endstops::esbits_t Endstops::validated_live_state;
  44. uint8_t Endstops::endstop_poll_count;
  45. #endif
  46. #if HAS_BED_PROBE
  47. volatile bool Endstops::z_probe_enabled = false;
  48. #endif
  49. // Initialized by settings.load()
  50. #if ENABLED(X_DUAL_ENDSTOPS)
  51. float Endstops::x2_endstop_adj;
  52. #endif
  53. #if ENABLED(Y_DUAL_ENDSTOPS)
  54. float Endstops::y2_endstop_adj;
  55. #endif
  56. #if Z_MULTI_ENDSTOPS
  57. float Endstops::z2_endstop_adj;
  58. #endif
  59. #if ENABLED(Z_TRIPLE_ENDSTOPS)
  60. float Endstops::z3_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_Z3_MIN
  121. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  122. SET_INPUT_PULLUP(Z3_MIN_PIN);
  123. #elif ENABLED(ENDSTOPPULLDOWN_ZMIN)
  124. SET_INPUT_PULLDOWN(Z3_MIN_PIN);
  125. #else
  126. SET_INPUT(Z3_MIN_PIN);
  127. #endif
  128. #endif
  129. #if HAS_X_MAX
  130. #if ENABLED(ENDSTOPPULLUP_XMAX)
  131. SET_INPUT_PULLUP(X_MAX_PIN);
  132. #elif ENABLED(ENDSTOPPULLDOWN_XMAX)
  133. SET_INPUT_PULLDOWN(X_MAX_PIN);
  134. #else
  135. SET_INPUT(X_MAX_PIN);
  136. #endif
  137. #endif
  138. #if HAS_X2_MAX
  139. #if ENABLED(ENDSTOPPULLUP_XMAX)
  140. SET_INPUT_PULLUP(X2_MAX_PIN);
  141. #elif ENABLED(ENDSTOPPULLDOWN_XMAX)
  142. SET_INPUT_PULLDOWN(X2_MAX_PIN);
  143. #else
  144. SET_INPUT(X2_MAX_PIN);
  145. #endif
  146. #endif
  147. #if HAS_Y_MAX
  148. #if ENABLED(ENDSTOPPULLUP_YMAX)
  149. SET_INPUT_PULLUP(Y_MAX_PIN);
  150. #elif ENABLED(ENDSTOPPULLDOWN_YMAX)
  151. SET_INPUT_PULLDOWN(Y_MAX_PIN);
  152. #else
  153. SET_INPUT(Y_MAX_PIN);
  154. #endif
  155. #endif
  156. #if HAS_Y2_MAX
  157. #if ENABLED(ENDSTOPPULLUP_YMAX)
  158. SET_INPUT_PULLUP(Y2_MAX_PIN);
  159. #elif ENABLED(ENDSTOPPULLDOWN_YMAX)
  160. SET_INPUT_PULLDOWN(Y2_MAX_PIN);
  161. #else
  162. SET_INPUT(Y2_MAX_PIN);
  163. #endif
  164. #endif
  165. #if HAS_Z_MAX
  166. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  167. SET_INPUT_PULLUP(Z_MAX_PIN);
  168. #elif ENABLED(ENDSTOPPULLDOWN_ZMAX)
  169. SET_INPUT_PULLDOWN(Z_MAX_PIN);
  170. #else
  171. SET_INPUT(Z_MAX_PIN);
  172. #endif
  173. #endif
  174. #if HAS_Z2_MAX
  175. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  176. SET_INPUT_PULLUP(Z2_MAX_PIN);
  177. #elif ENABLED(ENDSTOPPULLDOWN_ZMAX)
  178. SET_INPUT_PULLDOWN(Z2_MAX_PIN);
  179. #else
  180. SET_INPUT(Z2_MAX_PIN);
  181. #endif
  182. #endif
  183. #if HAS_Z3_MAX
  184. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  185. SET_INPUT_PULLUP(Z3_MAX_PIN);
  186. #elif ENABLED(ENDSTOPPULLDOWN_ZMAX)
  187. SET_INPUT_PULLDOWN(Z3_MAX_PIN);
  188. #else
  189. SET_INPUT(Z3_MAX_PIN);
  190. #endif
  191. #endif
  192. #if HAS_CALIBRATION_PIN
  193. #if ENABLED(CALIBRATION_PIN_PULLUP)
  194. SET_INPUT_PULLUP(CALIBRATION_PIN);
  195. #elif ENABLED(CALIBRATION_PIN_PULLDOWN)
  196. SET_INPUT_PULLDOWN(CALIBRATION_PIN);
  197. #else
  198. SET_INPUT(CALIBRATION_PIN);
  199. #endif
  200. #endif
  201. #if USES_Z_MIN_PROBE_ENDSTOP
  202. #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
  203. SET_INPUT_PULLUP(Z_MIN_PROBE_PIN);
  204. #elif ENABLED(ENDSTOPPULLDOWN_ZMIN_PROBE)
  205. SET_INPUT_PULLDOWN(Z_MIN_PROBE_PIN);
  206. #else
  207. SET_INPUT(Z_MIN_PROBE_PIN);
  208. #endif
  209. #endif
  210. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  211. setup_endstop_interrupts();
  212. #endif
  213. // Enable endstops
  214. enable_globally(
  215. #if ENABLED(ENDSTOPS_ALWAYS_ON_DEFAULT)
  216. true
  217. #else
  218. false
  219. #endif
  220. );
  221. } // Endstops::init
  222. // Called at ~1KHz from Temperature ISR: Poll endstop state if required
  223. void Endstops::poll() {
  224. #if ENABLED(PINS_DEBUGGING)
  225. run_monitor(); // report changes in endstop status
  226. #endif
  227. #if DISABLED(ENDSTOP_INTERRUPTS_FEATURE)
  228. update();
  229. #elif ENDSTOP_NOISE_THRESHOLD
  230. if (endstop_poll_count) update();
  231. #endif
  232. }
  233. void Endstops::enable_globally(const bool onoff) {
  234. enabled_globally = enabled = onoff;
  235. resync();
  236. }
  237. // Enable / disable endstop checking
  238. void Endstops::enable(const bool onoff) {
  239. enabled = onoff;
  240. resync();
  241. }
  242. // Disable / Enable endstops based on ENSTOPS_ONLY_FOR_HOMING and global enable
  243. void Endstops::not_homing() {
  244. enabled = enabled_globally;
  245. }
  246. #if ENABLED(VALIDATE_HOMING_ENDSTOPS)
  247. // If the last move failed to trigger an endstop, call kill
  248. void Endstops::validate_homing_move() {
  249. if (trigger_state()) hit_on_purpose();
  250. else kill(PSTR(MSG_ERR_HOMING_FAILED));
  251. }
  252. #endif
  253. // Enable / disable endstop z-probe checking
  254. #if HAS_BED_PROBE
  255. void Endstops::enable_z_probe(const bool onoff) {
  256. z_probe_enabled = onoff;
  257. resync();
  258. }
  259. #endif
  260. // Get the stable endstop states when enabled
  261. void Endstops::resync() {
  262. if (!abort_enabled()) return; // If endstops/probes are disabled the loop below can hang
  263. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  264. update();
  265. #else
  266. safe_delay(2); // Wait for Temperature ISR to run at least once (runs at 1KHz)
  267. #endif
  268. #if ENDSTOP_NOISE_THRESHOLD
  269. while (endstop_poll_count) safe_delay(1);
  270. #endif
  271. }
  272. #if ENABLED(PINS_DEBUGGING)
  273. void Endstops::run_monitor() {
  274. if (!monitor_flag) return;
  275. static uint8_t monitor_count = 16; // offset this check from the others
  276. monitor_count += _BV(1); // 15 Hz
  277. monitor_count &= 0x7F;
  278. if (!monitor_count) monitor(); // report changes in endstop status
  279. }
  280. #endif
  281. void Endstops::event_handler() {
  282. static uint8_t prev_hit_state; // = 0
  283. if (hit_state && hit_state != prev_hit_state) {
  284. #if ENABLED(ULTRA_LCD)
  285. char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
  286. #define _SET_STOP_CHAR(A,C) (chr## A = C)
  287. #else
  288. #define _SET_STOP_CHAR(A,C) ;
  289. #endif
  290. #define _ENDSTOP_HIT_ECHO(A,C) do{ \
  291. SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", planner.triggered_position_mm(_AXIS(A))); \
  292. _SET_STOP_CHAR(A,C); }while(0)
  293. #define _ENDSTOP_HIT_TEST(A,C) \
  294. if (TEST(hit_state, A ##_MIN) || TEST(hit_state, A ##_MAX)) \
  295. _ENDSTOP_HIT_ECHO(A,C)
  296. #define ENDSTOP_HIT_TEST_X() _ENDSTOP_HIT_TEST(X,'X')
  297. #define ENDSTOP_HIT_TEST_Y() _ENDSTOP_HIT_TEST(Y,'Y')
  298. #define ENDSTOP_HIT_TEST_Z() _ENDSTOP_HIT_TEST(Z,'Z')
  299. SERIAL_ECHO_START();
  300. SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
  301. ENDSTOP_HIT_TEST_X();
  302. ENDSTOP_HIT_TEST_Y();
  303. ENDSTOP_HIT_TEST_Z();
  304. #if USES_Z_MIN_PROBE_ENDSTOP
  305. #define P_AXIS Z_AXIS
  306. if (TEST(hit_state, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
  307. #endif
  308. SERIAL_EOL();
  309. #if ENABLED(ULTRA_LCD)
  310. ui.status_printf_P(0, PSTR(MSG_LCD_ENDSTOPS " %c %c %c %c"), chrX, chrY, chrZ, chrP);
  311. #endif
  312. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && ENABLED(SDSUPPORT)
  313. if (planner.abort_on_endstop_hit) {
  314. card.stopSDPrint();
  315. quickstop_stepper();
  316. thermalManager.disable_all_heaters();
  317. print_job_timer.stop();
  318. }
  319. #endif
  320. }
  321. prev_hit_state = hit_state;
  322. }
  323. static void print_es_state(const bool is_hit, PGM_P const label=NULL) {
  324. if (label) serialprintPGM(label);
  325. SERIAL_ECHOPGM(": ");
  326. serialprintPGM(is_hit ? PSTR(MSG_ENDSTOP_HIT) : PSTR(MSG_ENDSTOP_OPEN));
  327. SERIAL_EOL();
  328. }
  329. void _O2 Endstops::M119() {
  330. SERIAL_ECHOLNPGM(MSG_M119_REPORT);
  331. #define ES_REPORT(S) print_es_state(READ(S##_PIN) != S##_ENDSTOP_INVERTING, PSTR(MSG_##S))
  332. #if HAS_X_MIN
  333. ES_REPORT(X_MIN);
  334. #endif
  335. #if HAS_X2_MIN
  336. ES_REPORT(X2_MIN);
  337. #endif
  338. #if HAS_X_MAX
  339. ES_REPORT(X_MAX);
  340. #endif
  341. #if HAS_X2_MAX
  342. ES_REPORT(X2_MAX);
  343. #endif
  344. #if HAS_Y_MIN
  345. ES_REPORT(Y_MIN);
  346. #endif
  347. #if HAS_Y2_MIN
  348. ES_REPORT(Y2_MIN);
  349. #endif
  350. #if HAS_Y_MAX
  351. ES_REPORT(Y_MAX);
  352. #endif
  353. #if HAS_Y2_MAX
  354. ES_REPORT(Y2_MAX);
  355. #endif
  356. #if HAS_Z_MIN
  357. ES_REPORT(Z_MIN);
  358. #endif
  359. #if HAS_Z2_MIN
  360. ES_REPORT(Z2_MIN);
  361. #endif
  362. #if HAS_Z3_MIN
  363. ES_REPORT(Z3_MIN);
  364. #endif
  365. #if HAS_Z_MAX
  366. ES_REPORT(Z_MAX);
  367. #endif
  368. #if HAS_Z2_MAX
  369. ES_REPORT(Z2_MAX);
  370. #endif
  371. #if HAS_Z3_MAX
  372. ES_REPORT(Z3_MAX);
  373. #endif
  374. #if USES_Z_MIN_PROBE_ENDSTOP
  375. print_es_state(READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING, PSTR(MSG_Z_PROBE));
  376. #endif
  377. #if HAS_FILAMENT_SENSOR
  378. #if NUM_RUNOUT_SENSORS == 1
  379. print_es_state(READ(FIL_RUNOUT_PIN) != FIL_RUNOUT_INVERTING, PSTR(MSG_FILAMENT_RUNOUT_SENSOR));
  380. #else
  381. for (uint8_t i = 1; i <= NUM_RUNOUT_SENSORS; i++) {
  382. pin_t pin;
  383. switch (i) {
  384. default: continue;
  385. case 1: pin = FIL_RUNOUT_PIN; break;
  386. case 2: pin = FIL_RUNOUT2_PIN; break;
  387. #if NUM_RUNOUT_SENSORS > 2
  388. case 3: pin = FIL_RUNOUT3_PIN; break;
  389. #if NUM_RUNOUT_SENSORS > 3
  390. case 4: pin = FIL_RUNOUT4_PIN; break;
  391. #if NUM_RUNOUT_SENSORS > 4
  392. case 5: pin = FIL_RUNOUT5_PIN; break;
  393. #if NUM_RUNOUT_SENSORS > 5
  394. case 6: pin = FIL_RUNOUT6_PIN; break;
  395. #endif
  396. #endif
  397. #endif
  398. #endif
  399. }
  400. SERIAL_ECHOPGM(MSG_FILAMENT_RUNOUT_SENSOR);
  401. if (i > 1) { SERIAL_CHAR(' '); SERIAL_CHAR('0' + i); }
  402. print_es_state(extDigitalRead(pin) != FIL_RUNOUT_INVERTING);
  403. }
  404. #endif
  405. #endif
  406. } // Endstops::M119
  407. // The following routines are called from an ISR context. It could be the temperature ISR, the
  408. // endstop ISR or the Stepper ISR.
  409. #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
  410. #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
  411. #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
  412. // Check endstops - Could be called from Temperature ISR!
  413. void Endstops::update() {
  414. #if !ENDSTOP_NOISE_THRESHOLD
  415. if (!abort_enabled()) return;
  416. #endif
  417. #define UPDATE_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT_TO(live_state, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
  418. #define COPY_LIVE_STATE(SRC_BIT, DST_BIT) SET_BIT_TO(live_state, DST_BIT, TEST(live_state, SRC_BIT))
  419. #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && !(CORE_IS_XY || CORE_IS_XZ)
  420. // If G38 command is active check Z_MIN_PROBE for ALL movement
  421. if (G38_move) UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
  422. #endif
  423. // With Dual X, endstops are only checked in the homing direction for the active extruder
  424. #if ENABLED(DUAL_X_CARRIAGE)
  425. #define E0_ACTIVE stepper.movement_extruder() == 0
  426. #define X_MIN_TEST ((X_HOME_DIR < 0 && E0_ACTIVE) || (X2_HOME_DIR < 0 && !E0_ACTIVE))
  427. #define X_MAX_TEST ((X_HOME_DIR > 0 && E0_ACTIVE) || (X2_HOME_DIR > 0 && !E0_ACTIVE))
  428. #else
  429. #define X_MIN_TEST true
  430. #define X_MAX_TEST true
  431. #endif
  432. // Use HEAD for core axes, AXIS for others
  433. #if CORE_IS_XY || CORE_IS_XZ
  434. #define X_AXIS_HEAD X_HEAD
  435. #else
  436. #define X_AXIS_HEAD X_AXIS
  437. #endif
  438. #if CORE_IS_XY || CORE_IS_YZ
  439. #define Y_AXIS_HEAD Y_HEAD
  440. #else
  441. #define Y_AXIS_HEAD Y_AXIS
  442. #endif
  443. #if CORE_IS_XZ || CORE_IS_YZ
  444. #define Z_AXIS_HEAD Z_HEAD
  445. #else
  446. #define Z_AXIS_HEAD Z_AXIS
  447. #endif
  448. /**
  449. * Check and update endstops
  450. */
  451. #if HAS_X_MIN
  452. #if ENABLED(X_DUAL_ENDSTOPS)
  453. UPDATE_ENDSTOP_BIT(X, MIN);
  454. #if HAS_X2_MIN
  455. UPDATE_ENDSTOP_BIT(X2, MIN);
  456. #else
  457. COPY_LIVE_STATE(X_MIN, X2_MIN);
  458. #endif
  459. #else
  460. UPDATE_ENDSTOP_BIT(X, MIN);
  461. #endif
  462. #endif
  463. #if HAS_X_MAX
  464. #if ENABLED(X_DUAL_ENDSTOPS)
  465. UPDATE_ENDSTOP_BIT(X, MAX);
  466. #if HAS_X2_MAX
  467. UPDATE_ENDSTOP_BIT(X2, MAX);
  468. #else
  469. COPY_LIVE_STATE(X_MAX, X2_MAX);
  470. #endif
  471. #else
  472. UPDATE_ENDSTOP_BIT(X, MAX);
  473. #endif
  474. #endif
  475. #if HAS_Y_MIN
  476. #if ENABLED(Y_DUAL_ENDSTOPS)
  477. UPDATE_ENDSTOP_BIT(Y, MIN);
  478. #if HAS_Y2_MIN
  479. UPDATE_ENDSTOP_BIT(Y2, MIN);
  480. #else
  481. COPY_LIVE_STATE(Y_MIN, Y2_MIN);
  482. #endif
  483. #else
  484. UPDATE_ENDSTOP_BIT(Y, MIN);
  485. #endif
  486. #endif
  487. #if HAS_Y_MAX
  488. #if ENABLED(Y_DUAL_ENDSTOPS)
  489. UPDATE_ENDSTOP_BIT(Y, MAX);
  490. #if HAS_Y2_MAX
  491. UPDATE_ENDSTOP_BIT(Y2, MAX);
  492. #else
  493. COPY_LIVE_STATE(Y_MAX, Y2_MAX);
  494. #endif
  495. #else
  496. UPDATE_ENDSTOP_BIT(Y, MAX);
  497. #endif
  498. #endif
  499. #if HAS_Z_MIN
  500. #if Z_MULTI_ENDSTOPS
  501. UPDATE_ENDSTOP_BIT(Z, MIN);
  502. #if HAS_Z2_MIN
  503. UPDATE_ENDSTOP_BIT(Z2, MIN);
  504. #else
  505. COPY_LIVE_STATE(Z_MIN, Z2_MIN);
  506. #endif
  507. #if ENABLED(Z_TRIPLE_ENDSTOPS)
  508. #if HAS_Z3_MIN
  509. UPDATE_ENDSTOP_BIT(Z3, MIN);
  510. #else
  511. COPY_LIVE_STATE(Z_MIN, Z3_MIN);
  512. #endif
  513. #endif
  514. #elif ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  515. UPDATE_ENDSTOP_BIT(Z, MIN);
  516. #elif Z_HOME_DIR < 0
  517. UPDATE_ENDSTOP_BIT(Z, MIN);
  518. #endif
  519. #endif
  520. // When closing the gap check the enabled probe
  521. #if USES_Z_MIN_PROBE_ENDSTOP
  522. UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
  523. #endif
  524. #if HAS_Z_MAX
  525. // Check both Z dual endstops
  526. #if Z_MULTI_ENDSTOPS
  527. UPDATE_ENDSTOP_BIT(Z, MAX);
  528. #if HAS_Z2_MAX
  529. UPDATE_ENDSTOP_BIT(Z2, MAX);
  530. #else
  531. COPY_LIVE_STATE(Z_MAX, Z2_MAX);
  532. #endif
  533. #if ENABLED(Z_TRIPLE_ENDSTOPS)
  534. #if HAS_Z3_MAX
  535. UPDATE_ENDSTOP_BIT(Z3, MAX);
  536. #else
  537. COPY_LIVE_STATE(Z_MAX, Z3_MAX);
  538. #endif
  539. #endif
  540. #elif !USES_Z_MIN_PROBE_ENDSTOP || Z_MAX_PIN != Z_MIN_PROBE_PIN
  541. // If this pin isn't the bed probe it's the Z endstop
  542. UPDATE_ENDSTOP_BIT(Z, MAX);
  543. #endif
  544. #endif
  545. #if ENDSTOP_NOISE_THRESHOLD
  546. /**
  547. * Filtering out noise on endstops requires a delayed decision. Let's assume, due to noise,
  548. * that 50% of endstop signal samples are good and 50% are bad (assuming normal distribution
  549. * of random noise). Then the first sample has a 50% chance to be good or bad. The 2nd sample
  550. * also has a 50% chance to be good or bad. The chances of 2 samples both being bad becomes
  551. * 50% of 50%, or 25%. That was the previous implementation of Marlin endstop handling. It
  552. * reduces chances of bad readings in half, at the cost of 1 extra sample period, but chances
  553. * still exist. The only way to reduce them further is to increase the number of samples.
  554. * To reduce the chance to 1% (1/128th) requires 7 samples (adding 7ms of delay).
  555. */
  556. static esbits_t old_live_state;
  557. if (old_live_state != live_state) {
  558. endstop_poll_count = ENDSTOP_NOISE_THRESHOLD;
  559. old_live_state = live_state;
  560. }
  561. else if (endstop_poll_count && !--endstop_poll_count)
  562. validated_live_state = live_state;
  563. if (!abort_enabled()) return;
  564. #endif
  565. // Test the current status of an endstop
  566. #define TEST_ENDSTOP(ENDSTOP) (TEST(state(), ENDSTOP))
  567. // Record endstop was hit
  568. #define _ENDSTOP_HIT(AXIS, MINMAX) SBI(hit_state, _ENDSTOP(AXIS, MINMAX))
  569. // Call the endstop triggered routine for single endstops
  570. #define PROCESS_ENDSTOP(AXIS,MINMAX) do { \
  571. if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX))) { \
  572. _ENDSTOP_HIT(AXIS, MINMAX); \
  573. planner.endstop_triggered(_AXIS(AXIS)); \
  574. } \
  575. }while(0)
  576. // Call the endstop triggered routine for dual endstops
  577. #define PROCESS_DUAL_ENDSTOP(AXIS1, AXIS2, MINMAX) do { \
  578. const byte dual_hit = TEST_ENDSTOP(_ENDSTOP(AXIS1, MINMAX)) | (TEST_ENDSTOP(_ENDSTOP(AXIS2, MINMAX)) << 1); \
  579. if (dual_hit) { \
  580. _ENDSTOP_HIT(AXIS1, MINMAX); \
  581. /* if not performing home or if both endstops were trigged during homing... */ \
  582. if (!stepper.separate_multi_axis || dual_hit == 0b11) \
  583. planner.endstop_triggered(_AXIS(AXIS1)); \
  584. } \
  585. }while(0)
  586. #define PROCESS_TRIPLE_ENDSTOP(AXIS1, AXIS2, AXIS3, MINMAX) do { \
  587. const byte triple_hit = TEST_ENDSTOP(_ENDSTOP(AXIS1, MINMAX)) | (TEST_ENDSTOP(_ENDSTOP(AXIS2, MINMAX)) << 1) | (TEST_ENDSTOP(_ENDSTOP(AXIS3, MINMAX)) << 2); \
  588. if (triple_hit) { \
  589. _ENDSTOP_HIT(AXIS1, MINMAX); \
  590. /* if not performing home or if both endstops were trigged during homing... */ \
  591. if (!stepper.separate_multi_axis || triple_hit == 0b111) \
  592. planner.endstop_triggered(_AXIS(AXIS1)); \
  593. } \
  594. }while(0)
  595. #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && !(CORE_IS_XY || CORE_IS_XZ)
  596. #if ENABLED(G38_PROBE_AWAY)
  597. #define _G38_OPEN_STATE (G38_move >= 4)
  598. #else
  599. #define _G38_OPEN_STATE LOW
  600. #endif
  601. // If G38 command is active check Z_MIN_PROBE for ALL movement
  602. if (G38_move && TEST_ENDSTOP(_ENDSTOP(Z, MIN_PROBE)) != _G38_OPEN_STATE) {
  603. if (stepper.axis_is_moving(X_AXIS)) { _ENDSTOP_HIT(X, MIN); planner.endstop_triggered(X_AXIS); }
  604. else if (stepper.axis_is_moving(Y_AXIS)) { _ENDSTOP_HIT(Y, MIN); planner.endstop_triggered(Y_AXIS); }
  605. else if (stepper.axis_is_moving(Z_AXIS)) { _ENDSTOP_HIT(Z, MIN); planner.endstop_triggered(Z_AXIS); }
  606. G38_did_trigger = true;
  607. }
  608. #endif
  609. // Now, we must signal, after validation, if an endstop limit is pressed or not
  610. if (stepper.axis_is_moving(X_AXIS)) {
  611. if (stepper.motor_direction(X_AXIS_HEAD)) { // -direction
  612. #if HAS_X_MIN
  613. #if ENABLED(X_DUAL_ENDSTOPS)
  614. PROCESS_DUAL_ENDSTOP(X, X2, MIN);
  615. #else
  616. if (X_MIN_TEST) PROCESS_ENDSTOP(X, MIN);
  617. #endif
  618. #endif
  619. }
  620. else { // +direction
  621. #if HAS_X_MAX
  622. #if ENABLED(X_DUAL_ENDSTOPS)
  623. PROCESS_DUAL_ENDSTOP(X, X2, MAX);
  624. #else
  625. if (X_MAX_TEST) PROCESS_ENDSTOP(X, MAX);
  626. #endif
  627. #endif
  628. }
  629. }
  630. if (stepper.axis_is_moving(Y_AXIS)) {
  631. if (stepper.motor_direction(Y_AXIS_HEAD)) { // -direction
  632. #if HAS_Y_MIN
  633. #if ENABLED(Y_DUAL_ENDSTOPS)
  634. PROCESS_DUAL_ENDSTOP(Y, Y2, MIN);
  635. #else
  636. PROCESS_ENDSTOP(Y, MIN);
  637. #endif
  638. #endif
  639. }
  640. else { // +direction
  641. #if HAS_Y_MAX
  642. #if ENABLED(Y_DUAL_ENDSTOPS)
  643. PROCESS_DUAL_ENDSTOP(Y, Y2, MAX);
  644. #else
  645. PROCESS_ENDSTOP(Y, MAX);
  646. #endif
  647. #endif
  648. }
  649. }
  650. if (stepper.axis_is_moving(Z_AXIS)) {
  651. if (stepper.motor_direction(Z_AXIS_HEAD)) { // Z -direction. Gantry down, bed up.
  652. #if HAS_Z_MIN
  653. #if ENABLED(Z_TRIPLE_ENDSTOPS)
  654. PROCESS_TRIPLE_ENDSTOP(Z, Z2, Z3, MIN);
  655. #elif ENABLED(Z_DUAL_ENDSTOPS)
  656. PROCESS_DUAL_ENDSTOP(Z, Z2, MIN);
  657. #else
  658. #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  659. if (z_probe_enabled) PROCESS_ENDSTOP(Z, MIN);
  660. #elif USES_Z_MIN_PROBE_ENDSTOP
  661. if (!z_probe_enabled) PROCESS_ENDSTOP(Z, MIN);
  662. #else
  663. PROCESS_ENDSTOP(Z, MIN);
  664. #endif
  665. #endif
  666. #endif
  667. // When closing the gap check the enabled probe
  668. #if USES_Z_MIN_PROBE_ENDSTOP
  669. if (z_probe_enabled) PROCESS_ENDSTOP(Z, MIN_PROBE);
  670. #endif
  671. }
  672. else { // Z +direction. Gantry up, bed down.
  673. #if HAS_Z_MAX
  674. #if ENABLED(Z_TRIPLE_ENDSTOPS)
  675. PROCESS_TRIPLE_ENDSTOP(Z, Z2, Z3, MAX);
  676. #elif ENABLED(Z_DUAL_ENDSTOPS)
  677. PROCESS_DUAL_ENDSTOP(Z, Z2, MAX);
  678. #elif !USES_Z_MIN_PROBE_ENDSTOP || Z_MAX_PIN != Z_MIN_PROBE_PIN
  679. // If this pin is not hijacked for the bed probe
  680. // then it belongs to the Z endstop
  681. PROCESS_ENDSTOP(Z, MAX);
  682. #endif
  683. #endif
  684. }
  685. }
  686. } // Endstops::update()
  687. #if ENABLED(PINS_DEBUGGING)
  688. bool Endstops::monitor_flag = false;
  689. /**
  690. * monitors endstops & Z probe for changes
  691. *
  692. * If a change is detected then the LED is toggled and
  693. * a message is sent out the serial port
  694. *
  695. * Yes, we could miss a rapid back & forth change but
  696. * that won't matter because this is all manual.
  697. *
  698. */
  699. void Endstops::monitor() {
  700. static uint16_t old_live_state_local = 0;
  701. static uint8_t local_LED_status = 0;
  702. uint16_t live_state_local = 0;
  703. #define ES_GET_STATE(S) if (READ(S##_PIN)) SBI(live_state_local, S)
  704. #if HAS_X_MIN
  705. ES_GET_STATE(X_MIN);
  706. #endif
  707. #if HAS_X_MAX
  708. ES_GET_STATE(X_MAX);
  709. #endif
  710. #if HAS_Y_MIN
  711. ES_GET_STATE(Y_MIN);
  712. #endif
  713. #if HAS_Y_MAX
  714. ES_GET_STATE(Y_MAX);
  715. #endif
  716. #if HAS_Z_MIN
  717. ES_GET_STATE(Z_MIN);
  718. #endif
  719. #if HAS_Z_MAX
  720. ES_GET_STATE(Z_MAX);
  721. #endif
  722. #if HAS_Z_MIN_PROBE_PIN
  723. ES_GET_STATE(Z_MIN_PROBE);
  724. #endif
  725. #if HAS_X2_MIN
  726. ES_GET_STATE(X2_MIN);
  727. #endif
  728. #if HAS_X2_MAX
  729. ES_GET_STATE(X2_MAX);
  730. #endif
  731. #if HAS_Y2_MIN
  732. ES_GET_STATE(Y2_MIN);
  733. #endif
  734. #if HAS_Y2_MAX
  735. ES_GET_STATE(Y2_MAX);
  736. #endif
  737. #if HAS_Z2_MIN
  738. ES_GET_STATE(Z2_MIN);
  739. #endif
  740. #if HAS_Z2_MAX
  741. ES_GET_STATE(Z2_MAX);
  742. #endif
  743. #if HAS_Z3_MIN
  744. ES_GET_STATE(Z3_MIN);
  745. #endif
  746. #if HAS_Z3_MAX
  747. ES_GET_STATE(Z3_MAX);
  748. #endif
  749. uint16_t endstop_change = live_state_local ^ old_live_state_local;
  750. #define ES_REPORT_CHANGE(S) if (TEST(endstop_change, S)) SERIAL_ECHOPAIR(" " STRINGIFY(S) ":", TEST(live_state_local, S))
  751. if (endstop_change) {
  752. #if HAS_X_MIN
  753. ES_REPORT_CHANGE(X_MIN);
  754. #endif
  755. #if HAS_X_MAX
  756. ES_REPORT_CHANGE(X_MAX);
  757. #endif
  758. #if HAS_Y_MIN
  759. ES_REPORT_CHANGE(Y_MIN);
  760. #endif
  761. #if HAS_Y_MAX
  762. ES_REPORT_CHANGE(Y_MAX);
  763. #endif
  764. #if HAS_Z_MIN
  765. ES_REPORT_CHANGE(Z_MIN);
  766. #endif
  767. #if HAS_Z_MAX
  768. ES_REPORT_CHANGE(Z_MAX);
  769. #endif
  770. #if HAS_Z_MIN_PROBE_PIN
  771. ES_REPORT_CHANGE(Z_MIN_PROBE);
  772. #endif
  773. #if HAS_X2_MIN
  774. ES_REPORT_CHANGE(X2_MIN);
  775. #endif
  776. #if HAS_X2_MAX
  777. ES_REPORT_CHANGE(X2_MAX);
  778. #endif
  779. #if HAS_Y2_MIN
  780. ES_REPORT_CHANGE(Y2_MIN);
  781. #endif
  782. #if HAS_Y2_MAX
  783. ES_REPORT_CHANGE(Y2_MAX);
  784. #endif
  785. #if HAS_Z2_MIN
  786. ES_REPORT_CHANGE(Z2_MIN);
  787. #endif
  788. #if HAS_Z2_MAX
  789. ES_REPORT_CHANGE(Z2_MAX);
  790. #endif
  791. #if HAS_Z3_MIN
  792. ES_REPORT_CHANGE(Z3_MIN);
  793. #endif
  794. #if HAS_Z3_MAX
  795. ES_REPORT_CHANGE(Z3_MAX);
  796. #endif
  797. SERIAL_ECHOLNPGM("\n");
  798. ANALOG_WRITE(LED_PIN, local_LED_status);
  799. local_LED_status ^= 255;
  800. old_live_state_local = live_state_local;
  801. }
  802. }
  803. #endif // PINS_DEBUGGING