My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

endstops.cpp 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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 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 USES_Z_MIN_PROBE_ENDSTOP
  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 && hit_state != prev_hit_state) {
  296. #if HAS_SPI_LCD
  297. char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
  298. #define _SET_STOP_CHAR(A,C) (chr## A = C)
  299. #else
  300. #define _SET_STOP_CHAR(A,C) ;
  301. #endif
  302. #define _ENDSTOP_HIT_ECHO(A,C) do{ \
  303. SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", planner.triggered_position_mm(_AXIS(A))); \
  304. _SET_STOP_CHAR(A,C); }while(0)
  305. #define _ENDSTOP_HIT_TEST(A,C) \
  306. if (TEST(hit_state, A ##_MIN) || TEST(hit_state, A ##_MAX)) \
  307. _ENDSTOP_HIT_ECHO(A,C)
  308. #define ENDSTOP_HIT_TEST_X() _ENDSTOP_HIT_TEST(X,'X')
  309. #define ENDSTOP_HIT_TEST_Y() _ENDSTOP_HIT_TEST(Y,'Y')
  310. #define ENDSTOP_HIT_TEST_Z() _ENDSTOP_HIT_TEST(Z,'Z')
  311. SERIAL_ECHO_START();
  312. SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
  313. ENDSTOP_HIT_TEST_X();
  314. ENDSTOP_HIT_TEST_Y();
  315. ENDSTOP_HIT_TEST_Z();
  316. #if USES_Z_MIN_PROBE_ENDSTOP
  317. #define P_AXIS Z_AXIS
  318. if (TEST(hit_state, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
  319. #endif
  320. SERIAL_EOL();
  321. #if HAS_SPI_LCD
  322. ui.status_printf_P(0, PSTR(S_FMT " %c %c %c %c"), GET_TEXT(MSG_LCD_ENDSTOPS), chrX, chrY, chrZ, chrP);
  323. #endif
  324. #if BOTH(SD_ABORT_ON_ENDSTOP_HIT, SDSUPPORT)
  325. if (planner.abort_on_endstop_hit) {
  326. card.stopSDPrint();
  327. quickstop_stepper();
  328. thermalManager.disable_all_heaters();
  329. print_job_timer.stop();
  330. }
  331. #endif
  332. }
  333. prev_hit_state = hit_state;
  334. }
  335. static void print_es_state(const bool is_hit, PGM_P const label=nullptr) {
  336. if (label) serialprintPGM(label);
  337. SERIAL_ECHOPGM(": ");
  338. serialprintPGM(is_hit ? PSTR(MSG_ENDSTOP_HIT) : PSTR(MSG_ENDSTOP_OPEN));
  339. SERIAL_EOL();
  340. }
  341. void _O2 Endstops::M119() {
  342. #if ENABLED(BLTOUCH)
  343. bltouch._set_SW_mode();
  344. #endif
  345. SERIAL_ECHOLNPGM(MSG_M119_REPORT);
  346. #define ES_REPORT(S) print_es_state(READ(S##_PIN) != S##_ENDSTOP_INVERTING, PSTR(MSG_##S))
  347. #if HAS_X_MIN
  348. ES_REPORT(X_MIN);
  349. #endif
  350. #if HAS_X2_MIN
  351. ES_REPORT(X2_MIN);
  352. #endif
  353. #if HAS_X_MAX
  354. ES_REPORT(X_MAX);
  355. #endif
  356. #if HAS_X2_MAX
  357. ES_REPORT(X2_MAX);
  358. #endif
  359. #if HAS_Y_MIN
  360. ES_REPORT(Y_MIN);
  361. #endif
  362. #if HAS_Y2_MIN
  363. ES_REPORT(Y2_MIN);
  364. #endif
  365. #if HAS_Y_MAX
  366. ES_REPORT(Y_MAX);
  367. #endif
  368. #if HAS_Y2_MAX
  369. ES_REPORT(Y2_MAX);
  370. #endif
  371. #if HAS_Z_MIN
  372. ES_REPORT(Z_MIN);
  373. #endif
  374. #if HAS_Z2_MIN
  375. ES_REPORT(Z2_MIN);
  376. #endif
  377. #if HAS_Z3_MIN
  378. ES_REPORT(Z3_MIN);
  379. #endif
  380. #if HAS_Z_MAX
  381. ES_REPORT(Z_MAX);
  382. #endif
  383. #if HAS_Z2_MAX
  384. ES_REPORT(Z2_MAX);
  385. #endif
  386. #if HAS_Z3_MAX
  387. ES_REPORT(Z3_MAX);
  388. #endif
  389. #if USES_Z_MIN_PROBE_ENDSTOP
  390. print_es_state(READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING, PSTR(MSG_Z_PROBE));
  391. #endif
  392. #if HAS_FILAMENT_SENSOR
  393. #if NUM_RUNOUT_SENSORS == 1
  394. print_es_state(READ(FIL_RUNOUT_PIN) != FIL_RUNOUT_INVERTING, PSTR(MSG_FILAMENT_RUNOUT_SENSOR));
  395. #else
  396. for (uint8_t i = 1; i <= NUM_RUNOUT_SENSORS; i++) {
  397. pin_t pin;
  398. switch (i) {
  399. default: continue;
  400. case 1: pin = FIL_RUNOUT_PIN; break;
  401. case 2: pin = FIL_RUNOUT2_PIN; break;
  402. #if NUM_RUNOUT_SENSORS > 2
  403. case 3: pin = FIL_RUNOUT3_PIN; break;
  404. #if NUM_RUNOUT_SENSORS > 3
  405. case 4: pin = FIL_RUNOUT4_PIN; break;
  406. #if NUM_RUNOUT_SENSORS > 4
  407. case 5: pin = FIL_RUNOUT5_PIN; break;
  408. #if NUM_RUNOUT_SENSORS > 5
  409. case 6: pin = FIL_RUNOUT6_PIN; break;
  410. #endif
  411. #endif
  412. #endif
  413. #endif
  414. }
  415. SERIAL_ECHOPGM(MSG_FILAMENT_RUNOUT_SENSOR);
  416. if (i > 1) { SERIAL_CHAR(' '); SERIAL_CHAR('0' + i); }
  417. print_es_state(extDigitalRead(pin) != FIL_RUNOUT_INVERTING);
  418. }
  419. #endif
  420. #endif
  421. #if ENABLED(BLTOUCH)
  422. bltouch._reset_SW_mode();
  423. #endif
  424. #if ENABLED(JOYSTICK_DEBUG)
  425. joystick.report();
  426. #endif
  427. } // Endstops::M119
  428. // The following routines are called from an ISR context. It could be the temperature ISR, the
  429. // endstop ISR or the Stepper ISR.
  430. #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
  431. #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
  432. #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
  433. // Check endstops - Could be called from Temperature ISR!
  434. void Endstops::update() {
  435. #if !ENDSTOP_NOISE_THRESHOLD
  436. if (!abort_enabled()) return;
  437. #endif
  438. #define UPDATE_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT_TO(live_state, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
  439. #define COPY_LIVE_STATE(SRC_BIT, DST_BIT) SET_BIT_TO(live_state, DST_BIT, TEST(live_state, SRC_BIT))
  440. #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && !(CORE_IS_XY || CORE_IS_XZ)
  441. // If G38 command is active check Z_MIN_PROBE for ALL movement
  442. if (G38_move) UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
  443. #endif
  444. // With Dual X, endstops are only checked in the homing direction for the active extruder
  445. #if ENABLED(DUAL_X_CARRIAGE)
  446. #define E0_ACTIVE stepper.movement_extruder() == 0
  447. #define X_MIN_TEST ((X_HOME_DIR < 0 && E0_ACTIVE) || (X2_HOME_DIR < 0 && !E0_ACTIVE))
  448. #define X_MAX_TEST ((X_HOME_DIR > 0 && E0_ACTIVE) || (X2_HOME_DIR > 0 && !E0_ACTIVE))
  449. #else
  450. #define X_MIN_TEST true
  451. #define X_MAX_TEST true
  452. #endif
  453. // Use HEAD for core axes, AXIS for others
  454. #if CORE_IS_XY || CORE_IS_XZ
  455. #define X_AXIS_HEAD X_HEAD
  456. #else
  457. #define X_AXIS_HEAD X_AXIS
  458. #endif
  459. #if CORE_IS_XY || CORE_IS_YZ
  460. #define Y_AXIS_HEAD Y_HEAD
  461. #else
  462. #define Y_AXIS_HEAD Y_AXIS
  463. #endif
  464. #if CORE_IS_XZ || CORE_IS_YZ
  465. #define Z_AXIS_HEAD Z_HEAD
  466. #else
  467. #define Z_AXIS_HEAD Z_AXIS
  468. #endif
  469. /**
  470. * Check and update endstops
  471. */
  472. #if HAS_X_MIN
  473. #if ENABLED(X_DUAL_ENDSTOPS)
  474. UPDATE_ENDSTOP_BIT(X, MIN);
  475. #if HAS_X2_MIN
  476. UPDATE_ENDSTOP_BIT(X2, MIN);
  477. #else
  478. COPY_LIVE_STATE(X_MIN, X2_MIN);
  479. #endif
  480. #else
  481. UPDATE_ENDSTOP_BIT(X, MIN);
  482. #endif
  483. #endif
  484. #if HAS_X_MAX
  485. #if ENABLED(X_DUAL_ENDSTOPS)
  486. UPDATE_ENDSTOP_BIT(X, MAX);
  487. #if HAS_X2_MAX
  488. UPDATE_ENDSTOP_BIT(X2, MAX);
  489. #else
  490. COPY_LIVE_STATE(X_MAX, X2_MAX);
  491. #endif
  492. #else
  493. UPDATE_ENDSTOP_BIT(X, MAX);
  494. #endif
  495. #endif
  496. #if HAS_Y_MIN
  497. #if ENABLED(Y_DUAL_ENDSTOPS)
  498. UPDATE_ENDSTOP_BIT(Y, MIN);
  499. #if HAS_Y2_MIN
  500. UPDATE_ENDSTOP_BIT(Y2, MIN);
  501. #else
  502. COPY_LIVE_STATE(Y_MIN, Y2_MIN);
  503. #endif
  504. #else
  505. UPDATE_ENDSTOP_BIT(Y, MIN);
  506. #endif
  507. #endif
  508. #if HAS_Y_MAX
  509. #if ENABLED(Y_DUAL_ENDSTOPS)
  510. UPDATE_ENDSTOP_BIT(Y, MAX);
  511. #if HAS_Y2_MAX
  512. UPDATE_ENDSTOP_BIT(Y2, MAX);
  513. #else
  514. COPY_LIVE_STATE(Y_MAX, Y2_MAX);
  515. #endif
  516. #else
  517. UPDATE_ENDSTOP_BIT(Y, MAX);
  518. #endif
  519. #endif
  520. #if HAS_Z_MIN
  521. #if Z_MULTI_ENDSTOPS
  522. UPDATE_ENDSTOP_BIT(Z, MIN);
  523. #if HAS_Z2_MIN
  524. UPDATE_ENDSTOP_BIT(Z2, MIN);
  525. #else
  526. COPY_LIVE_STATE(Z_MIN, Z2_MIN);
  527. #endif
  528. #if ENABLED(Z_TRIPLE_ENDSTOPS)
  529. #if HAS_Z3_MIN
  530. UPDATE_ENDSTOP_BIT(Z3, MIN);
  531. #else
  532. COPY_LIVE_STATE(Z_MIN, Z3_MIN);
  533. #endif
  534. #endif
  535. #elif ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  536. UPDATE_ENDSTOP_BIT(Z, MIN);
  537. #elif Z_HOME_DIR < 0
  538. UPDATE_ENDSTOP_BIT(Z, MIN);
  539. #endif
  540. #endif
  541. // When closing the gap check the enabled probe
  542. #if USES_Z_MIN_PROBE_ENDSTOP
  543. UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
  544. #endif
  545. #if HAS_Z_MAX
  546. // Check both Z dual endstops
  547. #if Z_MULTI_ENDSTOPS
  548. UPDATE_ENDSTOP_BIT(Z, MAX);
  549. #if HAS_Z2_MAX
  550. UPDATE_ENDSTOP_BIT(Z2, MAX);
  551. #else
  552. COPY_LIVE_STATE(Z_MAX, Z2_MAX);
  553. #endif
  554. #if ENABLED(Z_TRIPLE_ENDSTOPS)
  555. #if HAS_Z3_MAX
  556. UPDATE_ENDSTOP_BIT(Z3, MAX);
  557. #else
  558. COPY_LIVE_STATE(Z_MAX, Z3_MAX);
  559. #endif
  560. #endif
  561. #elif !USES_Z_MIN_PROBE_ENDSTOP || Z_MAX_PIN != Z_MIN_PROBE_PIN
  562. // If this pin isn't the bed probe it's the Z endstop
  563. UPDATE_ENDSTOP_BIT(Z, MAX);
  564. #endif
  565. #endif
  566. #if ENDSTOP_NOISE_THRESHOLD
  567. /**
  568. * Filtering out noise on endstops requires a delayed decision. Let's assume, due to noise,
  569. * that 50% of endstop signal samples are good and 50% are bad (assuming normal distribution
  570. * of random noise). Then the first sample has a 50% chance to be good or bad. The 2nd sample
  571. * also has a 50% chance to be good or bad. The chances of 2 samples both being bad becomes
  572. * 50% of 50%, or 25%. That was the previous implementation of Marlin endstop handling. It
  573. * reduces chances of bad readings in half, at the cost of 1 extra sample period, but chances
  574. * still exist. The only way to reduce them further is to increase the number of samples.
  575. * To reduce the chance to 1% (1/128th) requires 7 samples (adding 7ms of delay).
  576. */
  577. static esbits_t old_live_state;
  578. if (old_live_state != live_state) {
  579. endstop_poll_count = ENDSTOP_NOISE_THRESHOLD;
  580. old_live_state = live_state;
  581. }
  582. else if (endstop_poll_count && !--endstop_poll_count)
  583. validated_live_state = live_state;
  584. if (!abort_enabled()) return;
  585. #endif
  586. // Test the current status of an endstop
  587. #define TEST_ENDSTOP(ENDSTOP) (TEST(state(), ENDSTOP))
  588. // Record endstop was hit
  589. #define _ENDSTOP_HIT(AXIS, MINMAX) SBI(hit_state, _ENDSTOP(AXIS, MINMAX))
  590. // Call the endstop triggered routine for single endstops
  591. #define PROCESS_ENDSTOP(AXIS,MINMAX) do { \
  592. if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX))) { \
  593. _ENDSTOP_HIT(AXIS, MINMAX); \
  594. planner.endstop_triggered(_AXIS(AXIS)); \
  595. } \
  596. }while(0)
  597. // Call the endstop triggered routine for dual endstops
  598. #define PROCESS_DUAL_ENDSTOP(AXIS1, AXIS2, MINMAX) do { \
  599. const byte dual_hit = TEST_ENDSTOP(_ENDSTOP(AXIS1, MINMAX)) | (TEST_ENDSTOP(_ENDSTOP(AXIS2, MINMAX)) << 1); \
  600. if (dual_hit) { \
  601. _ENDSTOP_HIT(AXIS1, MINMAX); \
  602. /* if not performing home or if both endstops were trigged during homing... */ \
  603. if (!stepper.separate_multi_axis || dual_hit == 0b11) \
  604. planner.endstop_triggered(_AXIS(AXIS1)); \
  605. } \
  606. }while(0)
  607. #define PROCESS_TRIPLE_ENDSTOP(AXIS1, AXIS2, AXIS3, MINMAX) do { \
  608. const byte triple_hit = TEST_ENDSTOP(_ENDSTOP(AXIS1, MINMAX)) | (TEST_ENDSTOP(_ENDSTOP(AXIS2, MINMAX)) << 1) | (TEST_ENDSTOP(_ENDSTOP(AXIS3, MINMAX)) << 2); \
  609. if (triple_hit) { \
  610. _ENDSTOP_HIT(AXIS1, MINMAX); \
  611. /* if not performing home or if both endstops were trigged during homing... */ \
  612. if (!stepper.separate_multi_axis || triple_hit == 0b111) \
  613. planner.endstop_triggered(_AXIS(AXIS1)); \
  614. } \
  615. }while(0)
  616. #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && !(CORE_IS_XY || CORE_IS_XZ)
  617. #if ENABLED(G38_PROBE_AWAY)
  618. #define _G38_OPEN_STATE (G38_move >= 4)
  619. #else
  620. #define _G38_OPEN_STATE LOW
  621. #endif
  622. // If G38 command is active check Z_MIN_PROBE for ALL movement
  623. if (G38_move && TEST_ENDSTOP(_ENDSTOP(Z, MIN_PROBE)) != _G38_OPEN_STATE) {
  624. if (stepper.axis_is_moving(X_AXIS)) { _ENDSTOP_HIT(X, MIN); planner.endstop_triggered(X_AXIS); }
  625. else if (stepper.axis_is_moving(Y_AXIS)) { _ENDSTOP_HIT(Y, MIN); planner.endstop_triggered(Y_AXIS); }
  626. else if (stepper.axis_is_moving(Z_AXIS)) { _ENDSTOP_HIT(Z, MIN); planner.endstop_triggered(Z_AXIS); }
  627. G38_did_trigger = true;
  628. }
  629. #endif
  630. // Now, we must signal, after validation, if an endstop limit is pressed or not
  631. if (stepper.axis_is_moving(X_AXIS)) {
  632. if (stepper.motor_direction(X_AXIS_HEAD)) { // -direction
  633. #if HAS_X_MIN || (X_SPI_SENSORLESS && X_HOME_DIR < 0)
  634. #if ENABLED(X_DUAL_ENDSTOPS)
  635. PROCESS_DUAL_ENDSTOP(X, X2, MIN);
  636. #else
  637. if (X_MIN_TEST) PROCESS_ENDSTOP(X, MIN);
  638. #endif
  639. #endif
  640. }
  641. else { // +direction
  642. #if HAS_X_MAX || (X_SPI_SENSORLESS && X_HOME_DIR > 0)
  643. #if ENABLED(X_DUAL_ENDSTOPS)
  644. PROCESS_DUAL_ENDSTOP(X, X2, MAX);
  645. #else
  646. if (X_MAX_TEST) PROCESS_ENDSTOP(X, MAX);
  647. #endif
  648. #endif
  649. }
  650. }
  651. if (stepper.axis_is_moving(Y_AXIS)) {
  652. if (stepper.motor_direction(Y_AXIS_HEAD)) { // -direction
  653. #if HAS_Y_MIN || (Y_SPI_SENSORLESS && Y_HOME_DIR < 0)
  654. #if ENABLED(Y_DUAL_ENDSTOPS)
  655. PROCESS_DUAL_ENDSTOP(Y, Y2, MIN);
  656. #else
  657. PROCESS_ENDSTOP(Y, MIN);
  658. #endif
  659. #endif
  660. }
  661. else { // +direction
  662. #if HAS_Y_MAX || (Y_SPI_SENSORLESS && Y_HOME_DIR > 0)
  663. #if ENABLED(Y_DUAL_ENDSTOPS)
  664. PROCESS_DUAL_ENDSTOP(Y, Y2, MAX);
  665. #else
  666. PROCESS_ENDSTOP(Y, MAX);
  667. #endif
  668. #endif
  669. }
  670. }
  671. if (stepper.axis_is_moving(Z_AXIS)) {
  672. if (stepper.motor_direction(Z_AXIS_HEAD)) { // Z -direction. Gantry down, bed up.
  673. #if HAS_Z_MIN || (Z_SPI_SENSORLESS && Z_HOME_DIR < 0)
  674. #if ENABLED(Z_TRIPLE_ENDSTOPS)
  675. PROCESS_TRIPLE_ENDSTOP(Z, Z2, Z3, MIN);
  676. #elif ENABLED(Z_DUAL_ENDSTOPS)
  677. PROCESS_DUAL_ENDSTOP(Z, Z2, MIN);
  678. #else
  679. #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  680. if (z_probe_enabled) PROCESS_ENDSTOP(Z, MIN);
  681. #elif USES_Z_MIN_PROBE_ENDSTOP
  682. if (!z_probe_enabled) PROCESS_ENDSTOP(Z, MIN);
  683. #else
  684. PROCESS_ENDSTOP(Z, MIN);
  685. #endif
  686. #endif
  687. #endif
  688. // When closing the gap check the enabled probe
  689. #if USES_Z_MIN_PROBE_ENDSTOP
  690. if (z_probe_enabled) PROCESS_ENDSTOP(Z, MIN_PROBE);
  691. #endif
  692. }
  693. else { // Z +direction. Gantry up, bed down.
  694. #if HAS_Z_MAX || (Z_SPI_SENSORLESS && Z_HOME_DIR > 0)
  695. #if ENABLED(Z_TRIPLE_ENDSTOPS)
  696. PROCESS_TRIPLE_ENDSTOP(Z, Z2, Z3, MAX);
  697. #elif ENABLED(Z_DUAL_ENDSTOPS)
  698. PROCESS_DUAL_ENDSTOP(Z, Z2, MAX);
  699. #elif !USES_Z_MIN_PROBE_ENDSTOP || Z_MAX_PIN != Z_MIN_PROBE_PIN
  700. // If this pin is not hijacked for the bed probe
  701. // then it belongs to the Z endstop
  702. PROCESS_ENDSTOP(Z, MAX);
  703. #endif
  704. #endif
  705. }
  706. }
  707. } // Endstops::update()
  708. #if ENABLED(SPI_ENDSTOPS)
  709. #define X_STOP (X_HOME_DIR < 0 ? X_MIN : X_MAX)
  710. #define Y_STOP (Y_HOME_DIR < 0 ? Y_MIN : Y_MAX)
  711. #define Z_STOP (Z_HOME_DIR < 0 ? Z_MIN : Z_MAX)
  712. bool Endstops::tmc_spi_homing_check() {
  713. bool hit = false;
  714. #if X_SPI_SENSORLESS
  715. if (tmc_spi_homing.x && stepperX.test_stall_status()) {
  716. SBI(live_state, X_STOP);
  717. hit = true;
  718. }
  719. #endif
  720. #if Y_SPI_SENSORLESS
  721. if (tmc_spi_homing.y && stepperY.test_stall_status()) {
  722. SBI(live_state, Y_STOP);
  723. hit = true;
  724. }
  725. #endif
  726. #if Z_SPI_SENSORLESS
  727. if (tmc_spi_homing.z && stepperZ.test_stall_status()) {
  728. SBI(live_state, Z_STOP);
  729. hit = true;
  730. }
  731. #endif
  732. return hit;
  733. }
  734. void Endstops::clear_endstop_state() {
  735. #if X_SPI_SENSORLESS
  736. CBI(live_state, X_STOP);
  737. #endif
  738. #if Y_SPI_SENSORLESS
  739. CBI(live_state, Y_STOP);
  740. #endif
  741. #if Z_SPI_SENSORLESS
  742. CBI(live_state, Z_STOP);
  743. #endif
  744. }
  745. #endif // SPI_ENDSTOPS
  746. #if ENABLED(PINS_DEBUGGING)
  747. bool Endstops::monitor_flag = false;
  748. /**
  749. * Monitor Endstops and Z Probe for changes
  750. *
  751. * If a change is detected then the LED is toggled and
  752. * a message is sent out the serial port.
  753. *
  754. * Yes, we could miss a rapid back & forth change but
  755. * that won't matter because this is all manual.
  756. */
  757. void Endstops::monitor() {
  758. static uint16_t old_live_state_local = 0;
  759. static uint8_t local_LED_status = 0;
  760. uint16_t live_state_local = 0;
  761. #define ES_GET_STATE(S) if (READ(S##_PIN)) SBI(live_state_local, S)
  762. #if HAS_X_MIN
  763. ES_GET_STATE(X_MIN);
  764. #endif
  765. #if HAS_X_MAX
  766. ES_GET_STATE(X_MAX);
  767. #endif
  768. #if HAS_Y_MIN
  769. ES_GET_STATE(Y_MIN);
  770. #endif
  771. #if HAS_Y_MAX
  772. ES_GET_STATE(Y_MAX);
  773. #endif
  774. #if HAS_Z_MIN
  775. ES_GET_STATE(Z_MIN);
  776. #endif
  777. #if HAS_Z_MAX
  778. ES_GET_STATE(Z_MAX);
  779. #endif
  780. #if HAS_Z_MIN_PROBE_PIN
  781. ES_GET_STATE(Z_MIN_PROBE);
  782. #endif
  783. #if HAS_X2_MIN
  784. ES_GET_STATE(X2_MIN);
  785. #endif
  786. #if HAS_X2_MAX
  787. ES_GET_STATE(X2_MAX);
  788. #endif
  789. #if HAS_Y2_MIN
  790. ES_GET_STATE(Y2_MIN);
  791. #endif
  792. #if HAS_Y2_MAX
  793. ES_GET_STATE(Y2_MAX);
  794. #endif
  795. #if HAS_Z2_MIN
  796. ES_GET_STATE(Z2_MIN);
  797. #endif
  798. #if HAS_Z2_MAX
  799. ES_GET_STATE(Z2_MAX);
  800. #endif
  801. #if HAS_Z3_MIN
  802. ES_GET_STATE(Z3_MIN);
  803. #endif
  804. #if HAS_Z3_MAX
  805. ES_GET_STATE(Z3_MAX);
  806. #endif
  807. uint16_t endstop_change = live_state_local ^ old_live_state_local;
  808. #define ES_REPORT_CHANGE(S) if (TEST(endstop_change, S)) SERIAL_ECHOPAIR(" " STRINGIFY(S) ":", TEST(live_state_local, S))
  809. if (endstop_change) {
  810. #if HAS_X_MIN
  811. ES_REPORT_CHANGE(X_MIN);
  812. #endif
  813. #if HAS_X_MAX
  814. ES_REPORT_CHANGE(X_MAX);
  815. #endif
  816. #if HAS_Y_MIN
  817. ES_REPORT_CHANGE(Y_MIN);
  818. #endif
  819. #if HAS_Y_MAX
  820. ES_REPORT_CHANGE(Y_MAX);
  821. #endif
  822. #if HAS_Z_MIN
  823. ES_REPORT_CHANGE(Z_MIN);
  824. #endif
  825. #if HAS_Z_MAX
  826. ES_REPORT_CHANGE(Z_MAX);
  827. #endif
  828. #if HAS_Z_MIN_PROBE_PIN
  829. ES_REPORT_CHANGE(Z_MIN_PROBE);
  830. #endif
  831. #if HAS_X2_MIN
  832. ES_REPORT_CHANGE(X2_MIN);
  833. #endif
  834. #if HAS_X2_MAX
  835. ES_REPORT_CHANGE(X2_MAX);
  836. #endif
  837. #if HAS_Y2_MIN
  838. ES_REPORT_CHANGE(Y2_MIN);
  839. #endif
  840. #if HAS_Y2_MAX
  841. ES_REPORT_CHANGE(Y2_MAX);
  842. #endif
  843. #if HAS_Z2_MIN
  844. ES_REPORT_CHANGE(Z2_MIN);
  845. #endif
  846. #if HAS_Z2_MAX
  847. ES_REPORT_CHANGE(Z2_MAX);
  848. #endif
  849. #if HAS_Z3_MIN
  850. ES_REPORT_CHANGE(Z3_MIN);
  851. #endif
  852. #if HAS_Z3_MAX
  853. ES_REPORT_CHANGE(Z3_MAX);
  854. #endif
  855. SERIAL_ECHOLNPGM("\n");
  856. analogWrite(pin_t(LED_PIN), local_LED_status);
  857. local_LED_status ^= 255;
  858. old_live_state_local = live_state_local;
  859. }
  860. }
  861. #endif // PINS_DEBUGGING