My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

endstops.cpp 27KB

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