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

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