My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

endstops.cpp 37KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  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 <https://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 "../sd/cardreader.h"
  28. #include "temperature.h"
  29. #include "../lcd/marlinui.h"
  30. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  31. #include HAL_PATH(../HAL, endstop_interrupts.h)
  32. #endif
  33. #if BOTH(SD_ABORT_ON_ENDSTOP_HIT, SDSUPPORT)
  34. #include "printcounter.h" // for print_job_timer
  35. #endif
  36. #if ENABLED(BLTOUCH)
  37. #include "../feature/bltouch.h"
  38. #endif
  39. #if ENABLED(JOYSTICK)
  40. #include "../feature/joystick.h"
  41. #endif
  42. #if HAS_BED_PROBE
  43. #include "probe.h"
  44. #endif
  45. Endstops endstops;
  46. // private:
  47. bool Endstops::enabled, Endstops::enabled_globally; // Initialized by settings.load()
  48. volatile Endstops::endstop_mask_t Endstops::hit_state;
  49. Endstops::endstop_mask_t Endstops::live_state = 0;
  50. #if ENDSTOP_NOISE_THRESHOLD
  51. Endstops::endstop_mask_t Endstops::validated_live_state;
  52. uint8_t Endstops::endstop_poll_count;
  53. #endif
  54. #if HAS_BED_PROBE
  55. volatile bool Endstops::z_probe_enabled = false;
  56. #endif
  57. // Initialized by settings.load()
  58. #if ENABLED(X_DUAL_ENDSTOPS)
  59. float Endstops::x2_endstop_adj;
  60. #endif
  61. #if ENABLED(Y_DUAL_ENDSTOPS)
  62. float Endstops::y2_endstop_adj;
  63. #endif
  64. #if ENABLED(Z_MULTI_ENDSTOPS)
  65. float Endstops::z2_endstop_adj;
  66. #if NUM_Z_STEPPER_DRIVERS >= 3
  67. float Endstops::z3_endstop_adj;
  68. #if NUM_Z_STEPPER_DRIVERS >= 4
  69. float Endstops::z4_endstop_adj;
  70. #endif
  71. #endif
  72. #endif
  73. #if ENABLED(SPI_ENDSTOPS)
  74. Endstops::tmc_spi_homing_t Endstops::tmc_spi_homing; // = 0
  75. #endif
  76. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  77. millis_t sg_guard_period; // = 0
  78. #endif
  79. /**
  80. * Class and Instance Methods
  81. */
  82. void Endstops::init() {
  83. #if HAS_X_MIN
  84. #if ENABLED(ENDSTOPPULLUP_XMIN)
  85. SET_INPUT_PULLUP(X_MIN_PIN);
  86. #elif ENABLED(ENDSTOPPULLDOWN_XMIN)
  87. SET_INPUT_PULLDOWN(X_MIN_PIN);
  88. #else
  89. SET_INPUT(X_MIN_PIN);
  90. #endif
  91. #endif
  92. #if HAS_X2_MIN
  93. #if ENABLED(ENDSTOPPULLUP_XMIN)
  94. SET_INPUT_PULLUP(X2_MIN_PIN);
  95. #elif ENABLED(ENDSTOPPULLDOWN_XMIN)
  96. SET_INPUT_PULLDOWN(X2_MIN_PIN);
  97. #else
  98. SET_INPUT(X2_MIN_PIN);
  99. #endif
  100. #endif
  101. #if HAS_Y_MIN
  102. #if ENABLED(ENDSTOPPULLUP_YMIN)
  103. SET_INPUT_PULLUP(Y_MIN_PIN);
  104. #elif ENABLED(ENDSTOPPULLDOWN_YMIN)
  105. SET_INPUT_PULLDOWN(Y_MIN_PIN);
  106. #else
  107. SET_INPUT(Y_MIN_PIN);
  108. #endif
  109. #endif
  110. #if HAS_Y2_MIN
  111. #if ENABLED(ENDSTOPPULLUP_YMIN)
  112. SET_INPUT_PULLUP(Y2_MIN_PIN);
  113. #elif ENABLED(ENDSTOPPULLDOWN_YMIN)
  114. SET_INPUT_PULLDOWN(Y2_MIN_PIN);
  115. #else
  116. SET_INPUT(Y2_MIN_PIN);
  117. #endif
  118. #endif
  119. #if HAS_Z_MIN
  120. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  121. SET_INPUT_PULLUP(Z_MIN_PIN);
  122. #elif ENABLED(ENDSTOPPULLDOWN_ZMIN)
  123. SET_INPUT_PULLDOWN(Z_MIN_PIN);
  124. #else
  125. SET_INPUT(Z_MIN_PIN);
  126. #endif
  127. #endif
  128. #if HAS_Z2_MIN
  129. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  130. SET_INPUT_PULLUP(Z2_MIN_PIN);
  131. #elif ENABLED(ENDSTOPPULLDOWN_ZMIN)
  132. SET_INPUT_PULLDOWN(Z2_MIN_PIN);
  133. #else
  134. SET_INPUT(Z2_MIN_PIN);
  135. #endif
  136. #endif
  137. #if HAS_Z3_MIN
  138. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  139. SET_INPUT_PULLUP(Z3_MIN_PIN);
  140. #elif ENABLED(ENDSTOPPULLDOWN_ZMIN)
  141. SET_INPUT_PULLDOWN(Z3_MIN_PIN);
  142. #else
  143. SET_INPUT(Z3_MIN_PIN);
  144. #endif
  145. #endif
  146. #if HAS_Z4_MIN
  147. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  148. SET_INPUT_PULLUP(Z4_MIN_PIN);
  149. #elif ENABLED(ENDSTOPPULLDOWN_ZMIN)
  150. SET_INPUT_PULLDOWN(Z4_MIN_PIN);
  151. #else
  152. SET_INPUT(Z4_MIN_PIN);
  153. #endif
  154. #endif
  155. #if HAS_X_MAX
  156. #if ENABLED(ENDSTOPPULLUP_XMAX)
  157. SET_INPUT_PULLUP(X_MAX_PIN);
  158. #elif ENABLED(ENDSTOPPULLDOWN_XMAX)
  159. SET_INPUT_PULLDOWN(X_MAX_PIN);
  160. #else
  161. SET_INPUT(X_MAX_PIN);
  162. #endif
  163. #endif
  164. #if HAS_X2_MAX
  165. #if ENABLED(ENDSTOPPULLUP_XMAX)
  166. SET_INPUT_PULLUP(X2_MAX_PIN);
  167. #elif ENABLED(ENDSTOPPULLDOWN_XMAX)
  168. SET_INPUT_PULLDOWN(X2_MAX_PIN);
  169. #else
  170. SET_INPUT(X2_MAX_PIN);
  171. #endif
  172. #endif
  173. #if HAS_Y_MAX
  174. #if ENABLED(ENDSTOPPULLUP_YMAX)
  175. SET_INPUT_PULLUP(Y_MAX_PIN);
  176. #elif ENABLED(ENDSTOPPULLDOWN_YMAX)
  177. SET_INPUT_PULLDOWN(Y_MAX_PIN);
  178. #else
  179. SET_INPUT(Y_MAX_PIN);
  180. #endif
  181. #endif
  182. #if HAS_Y2_MAX
  183. #if ENABLED(ENDSTOPPULLUP_YMAX)
  184. SET_INPUT_PULLUP(Y2_MAX_PIN);
  185. #elif ENABLED(ENDSTOPPULLDOWN_YMAX)
  186. SET_INPUT_PULLDOWN(Y2_MAX_PIN);
  187. #else
  188. SET_INPUT(Y2_MAX_PIN);
  189. #endif
  190. #endif
  191. #if HAS_Z_MAX
  192. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  193. SET_INPUT_PULLUP(Z_MAX_PIN);
  194. #elif ENABLED(ENDSTOPPULLDOWN_ZMAX)
  195. SET_INPUT_PULLDOWN(Z_MAX_PIN);
  196. #else
  197. SET_INPUT(Z_MAX_PIN);
  198. #endif
  199. #endif
  200. #if HAS_Z2_MAX
  201. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  202. SET_INPUT_PULLUP(Z2_MAX_PIN);
  203. #elif ENABLED(ENDSTOPPULLDOWN_ZMAX)
  204. SET_INPUT_PULLDOWN(Z2_MAX_PIN);
  205. #else
  206. SET_INPUT(Z2_MAX_PIN);
  207. #endif
  208. #endif
  209. #if HAS_Z3_MAX
  210. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  211. SET_INPUT_PULLUP(Z3_MAX_PIN);
  212. #elif ENABLED(ENDSTOPPULLDOWN_ZMAX)
  213. SET_INPUT_PULLDOWN(Z3_MAX_PIN);
  214. #else
  215. SET_INPUT(Z3_MAX_PIN);
  216. #endif
  217. #endif
  218. #if HAS_Z4_MAX
  219. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  220. SET_INPUT_PULLUP(Z4_MAX_PIN);
  221. #elif ENABLED(ENDSTOPPULLDOWN_ZMAX)
  222. SET_INPUT_PULLDOWN(Z4_MAX_PIN);
  223. #else
  224. SET_INPUT(Z4_MAX_PIN);
  225. #endif
  226. #endif
  227. #if HAS_I_MIN
  228. #if ENABLED(ENDSTOPPULLUP_IMIN)
  229. SET_INPUT_PULLUP(I_MIN_PIN);
  230. #elif ENABLED(ENDSTOPPULLDOWN_IMIN)
  231. SET_INPUT_PULLDOWN(I_MIN_PIN);
  232. #else
  233. SET_INPUT(I_MIN_PIN);
  234. #endif
  235. #endif
  236. #if HAS_I_MAX
  237. #if ENABLED(ENDSTOPPULLUP_IMAX)
  238. SET_INPUT_PULLUP(I_MAX_PIN);
  239. #elif ENABLED(ENDSTOPPULLDOWN_IMAX)
  240. SET_INPUT_PULLDOWN(I_MAX_PIN);
  241. #else
  242. SET_INPUT(I_MAX_PIN);
  243. #endif
  244. #endif
  245. #if HAS_J_MIN
  246. #if ENABLED(ENDSTOPPULLUP_JMIN)
  247. SET_INPUT_PULLUP(J_MIN_PIN);
  248. #elif ENABLED(ENDSTOPPULLDOWN_IMIN)
  249. SET_INPUT_PULLDOWN(J_MIN_PIN);
  250. #else
  251. SET_INPUT(J_MIN_PIN);
  252. #endif
  253. #endif
  254. #if HAS_J_MAX
  255. #if ENABLED(ENDSTOPPULLUP_JMAX)
  256. SET_INPUT_PULLUP(J_MAX_PIN);
  257. #elif ENABLED(ENDSTOPPULLDOWN_JMAX)
  258. SET_INPUT_PULLDOWN(J_MAX_PIN);
  259. #else
  260. SET_INPUT(J_MAX_PIN);
  261. #endif
  262. #endif
  263. #if HAS_K_MIN
  264. #if ENABLED(ENDSTOPPULLUP_KMIN)
  265. SET_INPUT_PULLUP(K_MIN_PIN);
  266. #elif ENABLED(ENDSTOPPULLDOWN_KMIN)
  267. SET_INPUT_PULLDOWN(K_MIN_PIN);
  268. #else
  269. SET_INPUT(K_MIN_PIN);
  270. #endif
  271. #endif
  272. #if HAS_K_MAX
  273. #if ENABLED(ENDSTOPPULLUP_KMAX)
  274. SET_INPUT_PULLUP(K_MAX_PIN);
  275. #elif ENABLED(ENDSTOPPULLDOWN_KMIN)
  276. SET_INPUT_PULLDOWN(K_MAX_PIN);
  277. #else
  278. SET_INPUT(K_MAX_PIN);
  279. #endif
  280. #endif
  281. #if PIN_EXISTS(CALIBRATION)
  282. #if ENABLED(CALIBRATION_PIN_PULLUP)
  283. SET_INPUT_PULLUP(CALIBRATION_PIN);
  284. #elif ENABLED(CALIBRATION_PIN_PULLDOWN)
  285. SET_INPUT_PULLDOWN(CALIBRATION_PIN);
  286. #else
  287. SET_INPUT(CALIBRATION_PIN);
  288. #endif
  289. #endif
  290. #if USES_Z_MIN_PROBE_PIN
  291. #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
  292. SET_INPUT_PULLUP(Z_MIN_PROBE_PIN);
  293. #elif ENABLED(ENDSTOPPULLDOWN_ZMIN_PROBE)
  294. SET_INPUT_PULLDOWN(Z_MIN_PROBE_PIN);
  295. #else
  296. SET_INPUT(Z_MIN_PROBE_PIN);
  297. #endif
  298. #endif
  299. #if ENABLED(PROBE_ACTIVATION_SWITCH)
  300. SET_INPUT(PROBE_ACTIVATION_SWITCH_PIN);
  301. #endif
  302. TERN_(PROBE_TARE, probe.tare());
  303. TERN_(ENDSTOP_INTERRUPTS_FEATURE, setup_endstop_interrupts());
  304. // Enable endstops
  305. enable_globally(ENABLED(ENDSTOPS_ALWAYS_ON_DEFAULT));
  306. } // Endstops::init
  307. // Called at ~1kHz from Temperature ISR: Poll endstop state if required
  308. void Endstops::poll() {
  309. TERN_(PINS_DEBUGGING, run_monitor()); // Report changes in endstop status
  310. #if DISABLED(ENDSTOP_INTERRUPTS_FEATURE)
  311. update();
  312. #elif ENDSTOP_NOISE_THRESHOLD
  313. if (endstop_poll_count) update();
  314. #endif
  315. }
  316. void Endstops::enable_globally(const bool onoff) {
  317. enabled_globally = enabled = onoff;
  318. resync();
  319. }
  320. // Enable / disable endstop checking
  321. void Endstops::enable(const bool onoff) {
  322. enabled = onoff;
  323. resync();
  324. }
  325. // Disable / Enable endstops based on ENSTOPS_ONLY_FOR_HOMING and global enable
  326. void Endstops::not_homing() {
  327. enabled = enabled_globally;
  328. }
  329. #if ENABLED(VALIDATE_HOMING_ENDSTOPS)
  330. // If the last move failed to trigger an endstop, call kill
  331. void Endstops::validate_homing_move() {
  332. if (trigger_state()) hit_on_purpose();
  333. else kill(GET_TEXT_F(MSG_KILL_HOMING_FAILED));
  334. }
  335. #endif
  336. // Enable / disable endstop z-probe checking
  337. #if HAS_BED_PROBE
  338. void Endstops::enable_z_probe(const bool onoff) {
  339. z_probe_enabled = onoff;
  340. #if PIN_EXISTS(PROBE_ENABLE)
  341. WRITE(PROBE_ENABLE_PIN, onoff);
  342. #endif
  343. resync();
  344. }
  345. #endif
  346. // Get the stable endstop states when enabled
  347. void Endstops::resync() {
  348. if (!abort_enabled()) return; // If endstops/probes are disabled the loop below can hang
  349. // Wait for Temperature ISR to run at least once (runs at 1kHz)
  350. TERN(ENDSTOP_INTERRUPTS_FEATURE, update(), safe_delay(2));
  351. while (TERN0(ENDSTOP_NOISE_THRESHOLD, endstop_poll_count)) safe_delay(1);
  352. }
  353. #if ENABLED(PINS_DEBUGGING)
  354. void Endstops::run_monitor() {
  355. if (!monitor_flag) return;
  356. static uint8_t monitor_count = 16; // offset this check from the others
  357. monitor_count += _BV(1); // 15 Hz
  358. monitor_count &= 0x7F;
  359. if (!monitor_count) monitor(); // report changes in endstop status
  360. }
  361. #endif
  362. void Endstops::event_handler() {
  363. static endstop_mask_t prev_hit_state; // = 0
  364. if (hit_state == prev_hit_state) return;
  365. prev_hit_state = hit_state;
  366. if (hit_state) {
  367. #if HAS_STATUS_MESSAGE
  368. char LINEAR_AXIS_LIST(chrX = ' ', chrY = ' ', chrZ = ' ', chrI = ' ', chrJ = ' ', chrK = ' '),
  369. chrP = ' ';
  370. #define _SET_STOP_CHAR(A,C) (chr## A = C)
  371. #else
  372. #define _SET_STOP_CHAR(A,C) NOOP
  373. #endif
  374. #define _ENDSTOP_HIT_ECHO(A,C) do{ \
  375. SERIAL_ECHOPGM(" " STRINGIFY(A) ":", planner.triggered_position_mm(_AXIS(A))); _SET_STOP_CHAR(A,C); }while(0)
  376. #define _ENDSTOP_HIT_TEST(A,C) \
  377. if (TERN0(HAS_##A##_MIN, TEST(hit_state, A##_MIN)) || TERN0(HAS_##A##_MAX, TEST(hit_state, A##_MAX))) \
  378. _ENDSTOP_HIT_ECHO(A,C)
  379. #define ENDSTOP_HIT_TEST_X() _ENDSTOP_HIT_TEST(X,'X')
  380. #define ENDSTOP_HIT_TEST_Y() _ENDSTOP_HIT_TEST(Y,'Y')
  381. #define ENDSTOP_HIT_TEST_Z() _ENDSTOP_HIT_TEST(Z,'Z')
  382. #define ENDSTOP_HIT_TEST_I() _ENDSTOP_HIT_TEST(I,'I')
  383. #define ENDSTOP_HIT_TEST_J() _ENDSTOP_HIT_TEST(J,'J')
  384. #define ENDSTOP_HIT_TEST_K() _ENDSTOP_HIT_TEST(K,'K')
  385. SERIAL_ECHO_START();
  386. SERIAL_ECHOPGM(STR_ENDSTOPS_HIT);
  387. LINEAR_AXIS_CODE(
  388. ENDSTOP_HIT_TEST_X(),
  389. ENDSTOP_HIT_TEST_Y(),
  390. ENDSTOP_HIT_TEST_Z(),
  391. _ENDSTOP_HIT_TEST(I,'I'),
  392. _ENDSTOP_HIT_TEST(J,'J'),
  393. _ENDSTOP_HIT_TEST(K,'K')
  394. );
  395. #if USES_Z_MIN_PROBE_PIN
  396. #define P_AXIS Z_AXIS
  397. if (TEST(hit_state, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
  398. #endif
  399. SERIAL_EOL();
  400. TERN_(HAS_STATUS_MESSAGE,
  401. ui.status_printf(0,
  402. F(S_FMT GANG_N_1(LINEAR_AXES, " %c") " %c"),
  403. GET_TEXT(MSG_LCD_ENDSTOPS),
  404. LINEAR_AXIS_LIST(chrX, chrY, chrZ, chrI, chrJ, chrK), chrP
  405. )
  406. );
  407. #if BOTH(SD_ABORT_ON_ENDSTOP_HIT, SDSUPPORT)
  408. if (planner.abort_on_endstop_hit) {
  409. card.abortFilePrintNow();
  410. quickstop_stepper();
  411. thermalManager.disable_all_heaters();
  412. print_job_timer.stop();
  413. }
  414. #endif
  415. }
  416. }
  417. #pragma GCC diagnostic push
  418. #if GCC_VERSION <= 50000
  419. #pragma GCC diagnostic ignored "-Wunused-function"
  420. #endif
  421. static void print_es_state(const bool is_hit, FSTR_P const flabel=nullptr) {
  422. if (flabel) SERIAL_ECHOF(flabel);
  423. SERIAL_ECHOPGM(": ");
  424. SERIAL_ECHOLNF(is_hit ? F(STR_ENDSTOP_HIT) : F(STR_ENDSTOP_OPEN));
  425. }
  426. #pragma GCC diagnostic pop
  427. void _O2 Endstops::report_states() {
  428. TERN_(BLTOUCH, bltouch._set_SW_mode());
  429. SERIAL_ECHOLNPGM(STR_M119_REPORT);
  430. #define ES_REPORT(S) print_es_state(READ(S##_PIN) != S##_ENDSTOP_INVERTING, F(STR_##S))
  431. #if HAS_X_MIN
  432. ES_REPORT(X_MIN);
  433. #endif
  434. #if HAS_X2_MIN
  435. ES_REPORT(X2_MIN);
  436. #endif
  437. #if HAS_X_MAX
  438. ES_REPORT(X_MAX);
  439. #endif
  440. #if HAS_X2_MAX
  441. ES_REPORT(X2_MAX);
  442. #endif
  443. #if HAS_Y_MIN
  444. ES_REPORT(Y_MIN);
  445. #endif
  446. #if HAS_Y2_MIN
  447. ES_REPORT(Y2_MIN);
  448. #endif
  449. #if HAS_Y_MAX
  450. ES_REPORT(Y_MAX);
  451. #endif
  452. #if HAS_Y2_MAX
  453. ES_REPORT(Y2_MAX);
  454. #endif
  455. #if HAS_Z_MIN
  456. ES_REPORT(Z_MIN);
  457. #endif
  458. #if HAS_Z2_MIN
  459. ES_REPORT(Z2_MIN);
  460. #endif
  461. #if HAS_Z3_MIN
  462. ES_REPORT(Z3_MIN);
  463. #endif
  464. #if HAS_Z4_MIN
  465. ES_REPORT(Z4_MIN);
  466. #endif
  467. #if HAS_Z_MAX
  468. ES_REPORT(Z_MAX);
  469. #endif
  470. #if HAS_Z2_MAX
  471. ES_REPORT(Z2_MAX);
  472. #endif
  473. #if HAS_Z3_MAX
  474. ES_REPORT(Z3_MAX);
  475. #endif
  476. #if HAS_Z4_MAX
  477. ES_REPORT(Z4_MAX);
  478. #endif
  479. #if HAS_I_MIN
  480. ES_REPORT(I_MIN);
  481. #endif
  482. #if HAS_I_MAX
  483. ES_REPORT(I_MAX);
  484. #endif
  485. #if HAS_J_MIN
  486. ES_REPORT(J_MIN);
  487. #endif
  488. #if HAS_J_MAX
  489. ES_REPORT(J_MAX);
  490. #endif
  491. #if HAS_K_MIN
  492. ES_REPORT(K_MIN);
  493. #endif
  494. #if HAS_K_MAX
  495. ES_REPORT(K_MAX);
  496. #endif
  497. #if ENABLED(PROBE_ACTIVATION_SWITCH)
  498. print_es_state(probe_switch_activated(), F(STR_PROBE_EN));
  499. #endif
  500. #if USES_Z_MIN_PROBE_PIN
  501. print_es_state(PROBE_TRIGGERED(), F(STR_Z_PROBE));
  502. #endif
  503. #if MULTI_FILAMENT_SENSOR
  504. #define _CASE_RUNOUT(N) case N: pin = FIL_RUNOUT##N##_PIN; state = FIL_RUNOUT##N##_STATE; break;
  505. LOOP_S_LE_N(i, 1, NUM_RUNOUT_SENSORS) {
  506. pin_t pin;
  507. uint8_t state;
  508. switch (i) {
  509. default: continue;
  510. REPEAT_1(NUM_RUNOUT_SENSORS, _CASE_RUNOUT)
  511. }
  512. SERIAL_ECHOPGM(STR_FILAMENT);
  513. if (i > 1) SERIAL_CHAR(' ', '0' + i);
  514. print_es_state(extDigitalRead(pin) != state);
  515. }
  516. #undef _CASE_RUNOUT
  517. #elif HAS_FILAMENT_SENSOR
  518. print_es_state(READ(FIL_RUNOUT1_PIN) != FIL_RUNOUT1_STATE, F(STR_FILAMENT));
  519. #endif
  520. TERN_(BLTOUCH, bltouch._reset_SW_mode());
  521. TERN_(JOYSTICK_DEBUG, joystick.report());
  522. } // Endstops::report_states
  523. #if HAS_DELTA_SENSORLESS_PROBING
  524. #define __ENDSTOP(AXIS, ...) AXIS ##_MAX
  525. #define _ENDSTOP_PIN(AXIS, ...) AXIS ##_MAX_PIN
  526. #define _ENDSTOP_INVERTING(AXIS, ...) AXIS ##_MAX_ENDSTOP_INVERTING
  527. #else
  528. #define __ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
  529. #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
  530. #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
  531. #endif
  532. #define _ENDSTOP(AXIS, MINMAX) __ENDSTOP(AXIS, MINMAX)
  533. /**
  534. * Called from interrupt context by the Endstop ISR or Stepper ISR!
  535. * Read endstops to get their current states, register hits for all
  536. * axes moving in the direction of their endstops, and abort moves.
  537. */
  538. void Endstops::update() {
  539. #if !ENDSTOP_NOISE_THRESHOLD // If not debouncing...
  540. if (!abort_enabled()) return; // ...and not enabled, exit.
  541. #endif
  542. // Macros to update / copy the live_state
  543. #define UPDATE_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT_TO(live_state, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
  544. #define COPY_LIVE_STATE(SRC_BIT, DST_BIT) SET_BIT_TO(live_state, DST_BIT, TEST(live_state, SRC_BIT))
  545. #if ENABLED(G38_PROBE_TARGET) && NONE(CORE_IS_XY, CORE_IS_XZ, MARKFORGED_XY, MARKFORGED_XY)
  546. #define HAS_G38_PROBE 1
  547. // For G38 moves check the probe's pin for ALL movement
  548. if (G38_move) UPDATE_ENDSTOP_BIT(Z, TERN(USES_Z_MIN_PROBE_PIN, MIN_PROBE, MIN));
  549. #endif
  550. // With Dual X, endstops are only checked in the homing direction for the active extruder
  551. #define X_MIN_TEST() TERN1(DUAL_X_CARRIAGE, TERN0(X_HOME_TO_MIN, stepper.last_moved_extruder == 0) || TERN0(X2_HOME_TO_MIN, stepper.last_moved_extruder != 0))
  552. #define X_MAX_TEST() TERN1(DUAL_X_CARRIAGE, TERN0(X_HOME_TO_MAX, stepper.last_moved_extruder == 0) || TERN0(X2_HOME_TO_MAX, stepper.last_moved_extruder != 0))
  553. // Use HEAD for core axes, AXIS for others
  554. #if ANY(CORE_IS_XY, CORE_IS_XZ, MARKFORGED_XY, MARKFORGED_XY)
  555. #define X_AXIS_HEAD X_HEAD
  556. #else
  557. #define X_AXIS_HEAD X_AXIS
  558. #endif
  559. #if ANY(CORE_IS_XY, CORE_IS_YZ, MARKFORGED_XY, MARKFORGED_YX)
  560. #define Y_AXIS_HEAD Y_HEAD
  561. #else
  562. #define Y_AXIS_HEAD Y_AXIS
  563. #endif
  564. #if CORE_IS_XZ || CORE_IS_YZ
  565. #define Z_AXIS_HEAD Z_HEAD
  566. #else
  567. #define Z_AXIS_HEAD Z_AXIS
  568. #endif
  569. #define I_AXIS_HEAD I_AXIS
  570. #define J_AXIS_HEAD J_AXIS
  571. #define K_AXIS_HEAD K_AXIS
  572. /**
  573. * Check and update endstops
  574. */
  575. #if HAS_X_MIN && !X_SPI_SENSORLESS
  576. UPDATE_ENDSTOP_BIT(X, MIN);
  577. #if ENABLED(X_DUAL_ENDSTOPS)
  578. #if HAS_X2_MIN
  579. UPDATE_ENDSTOP_BIT(X2, MIN);
  580. #else
  581. COPY_LIVE_STATE(X_MIN, X2_MIN);
  582. #endif
  583. #endif
  584. #endif
  585. #if HAS_X_MAX && !X_SPI_SENSORLESS
  586. UPDATE_ENDSTOP_BIT(X, MAX);
  587. #if ENABLED(X_DUAL_ENDSTOPS)
  588. #if HAS_X2_MAX
  589. UPDATE_ENDSTOP_BIT(X2, MAX);
  590. #else
  591. COPY_LIVE_STATE(X_MAX, X2_MAX);
  592. #endif
  593. #endif
  594. #endif
  595. #if HAS_Y_MIN && !Y_SPI_SENSORLESS
  596. UPDATE_ENDSTOP_BIT(Y, MIN);
  597. #if ENABLED(Y_DUAL_ENDSTOPS)
  598. #if HAS_Y2_MIN
  599. UPDATE_ENDSTOP_BIT(Y2, MIN);
  600. #else
  601. COPY_LIVE_STATE(Y_MIN, Y2_MIN);
  602. #endif
  603. #endif
  604. #endif
  605. #if HAS_Y_MAX && !Y_SPI_SENSORLESS
  606. UPDATE_ENDSTOP_BIT(Y, MAX);
  607. #if ENABLED(Y_DUAL_ENDSTOPS)
  608. #if HAS_Y2_MAX
  609. UPDATE_ENDSTOP_BIT(Y2, MAX);
  610. #else
  611. COPY_LIVE_STATE(Y_MAX, Y2_MAX);
  612. #endif
  613. #endif
  614. #endif
  615. #if HAS_Z_MIN && NONE(Z_SPI_SENSORLESS, Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  616. UPDATE_ENDSTOP_BIT(Z, MIN);
  617. #if ENABLED(Z_MULTI_ENDSTOPS)
  618. #if HAS_Z2_MIN
  619. UPDATE_ENDSTOP_BIT(Z2, MIN);
  620. #else
  621. COPY_LIVE_STATE(Z_MIN, Z2_MIN);
  622. #endif
  623. #if NUM_Z_STEPPER_DRIVERS >= 3
  624. #if HAS_Z3_MIN
  625. UPDATE_ENDSTOP_BIT(Z3, MIN);
  626. #else
  627. COPY_LIVE_STATE(Z_MIN, Z3_MIN);
  628. #endif
  629. #endif
  630. #if NUM_Z_STEPPER_DRIVERS >= 4
  631. #if HAS_Z4_MIN
  632. UPDATE_ENDSTOP_BIT(Z4, MIN);
  633. #else
  634. COPY_LIVE_STATE(Z_MIN, Z4_MIN);
  635. #endif
  636. #endif
  637. #endif
  638. #endif
  639. #if HAS_BED_PROBE
  640. // When closing the gap check the enabled probe
  641. if (probe_switch_activated())
  642. UPDATE_ENDSTOP_BIT(Z, TERN(USES_Z_MIN_PROBE_PIN, MIN_PROBE, MIN));
  643. #endif
  644. #if HAS_Z_MAX && !Z_SPI_SENSORLESS
  645. // Check both Z dual endstops
  646. #if ENABLED(Z_MULTI_ENDSTOPS)
  647. UPDATE_ENDSTOP_BIT(Z, MAX);
  648. #if HAS_Z2_MAX
  649. UPDATE_ENDSTOP_BIT(Z2, MAX);
  650. #else
  651. COPY_LIVE_STATE(Z_MAX, Z2_MAX);
  652. #endif
  653. #if NUM_Z_STEPPER_DRIVERS >= 3
  654. #if HAS_Z3_MAX
  655. UPDATE_ENDSTOP_BIT(Z3, MAX);
  656. #else
  657. COPY_LIVE_STATE(Z_MAX, Z3_MAX);
  658. #endif
  659. #endif
  660. #if NUM_Z_STEPPER_DRIVERS >= 4
  661. #if HAS_Z4_MAX
  662. UPDATE_ENDSTOP_BIT(Z4, MAX);
  663. #else
  664. COPY_LIVE_STATE(Z_MAX, Z4_MAX);
  665. #endif
  666. #endif
  667. #elif TERN1(USES_Z_MIN_PROBE_PIN, Z_MAX_PIN != Z_MIN_PROBE_PIN)
  668. // If this pin isn't the bed probe it's the Z endstop
  669. UPDATE_ENDSTOP_BIT(Z, MAX);
  670. #endif
  671. #endif
  672. #if HAS_I_MIN && !I_SPI_SENSORLESS
  673. #if ENABLED(I_DUAL_ENDSTOPS)
  674. UPDATE_ENDSTOP_BIT(I, MIN);
  675. #if HAS_I2_MIN
  676. UPDATE_ENDSTOP_BIT(I2, MAX);
  677. #else
  678. COPY_LIVE_STATE(I_MIN, I2_MIN);
  679. #endif
  680. #else
  681. UPDATE_ENDSTOP_BIT(I, MIN);
  682. #endif
  683. #endif
  684. #if HAS_I_MAX && !I_SPI_SENSORLESS
  685. #if ENABLED(I_DUAL_ENDSTOPS)
  686. UPDATE_ENDSTOP_BIT(I, MAX);
  687. #if HAS_I2_MAX
  688. UPDATE_ENDSTOP_BIT(I2, MAX);
  689. #else
  690. COPY_LIVE_STATE(I_MAX, I2_MAX);
  691. #endif
  692. #else
  693. UPDATE_ENDSTOP_BIT(I, MAX);
  694. #endif
  695. #endif
  696. #if HAS_J_MIN && !J_SPI_SENSORLESS
  697. #if ENABLED(J_DUAL_ENDSTOPS)
  698. UPDATE_ENDSTOP_BIT(J, MIN);
  699. #if HAS_J2_MIN
  700. UPDATE_ENDSTOP_BIT(J2, MIN);
  701. #else
  702. COPY_LIVE_STATE(J_MIN, J2_MIN);
  703. #endif
  704. #else
  705. UPDATE_ENDSTOP_BIT(J, MIN);
  706. #endif
  707. #endif
  708. #if HAS_J_MAX && !J_SPI_SENSORLESS
  709. #if ENABLED(J_DUAL_ENDSTOPS)
  710. UPDATE_ENDSTOP_BIT(J, MAX);
  711. #if HAS_J2_MAX
  712. UPDATE_ENDSTOP_BIT(J2, MAX);
  713. #else
  714. COPY_LIVE_STATE(J_MAX, J2_MAX);
  715. #endif
  716. #else
  717. UPDATE_ENDSTOP_BIT(J, MAX);
  718. #endif
  719. #endif
  720. #if HAS_K_MIN && !K_SPI_SENSORLESS
  721. #if ENABLED(K_DUAL_ENDSTOPS)
  722. UPDATE_ENDSTOP_BIT(K, MIN);
  723. #if HAS_K2_MIN
  724. UPDATE_ENDSTOP_BIT(K2, MIN);
  725. #else
  726. COPY_LIVE_STATE(K_MIN, K2_MIN);
  727. #endif
  728. #else
  729. UPDATE_ENDSTOP_BIT(K, MIN);
  730. #endif
  731. #endif
  732. #if HAS_K_MAX && !K_SPI_SENSORLESS
  733. #if ENABLED(K_DUAL_ENDSTOPS)
  734. UPDATE_ENDSTOP_BIT(K, MAX);
  735. #if HAS_K2_MAX
  736. UPDATE_ENDSTOP_BIT(K2, MAX);
  737. #else
  738. COPY_LIVE_STATE(K_MAX, K2_MAX);
  739. #endif
  740. #else
  741. UPDATE_ENDSTOP_BIT(K, MAX);
  742. #endif
  743. #endif
  744. #if ENDSTOP_NOISE_THRESHOLD
  745. /**
  746. * Filtering out noise on endstops requires a delayed decision. Let's assume, due to noise,
  747. * that 50% of endstop signal samples are good and 50% are bad (assuming normal distribution
  748. * of random noise). Then the first sample has a 50% chance to be good or bad. The 2nd sample
  749. * also has a 50% chance to be good or bad. The chances of 2 samples both being bad becomes
  750. * 50% of 50%, or 25%. That was the previous implementation of Marlin endstop handling. It
  751. * reduces chances of bad readings in half, at the cost of 1 extra sample period, but chances
  752. * still exist. The only way to reduce them further is to increase the number of samples.
  753. * To reduce the chance to 1% (1/128th) requires 7 samples (adding 7ms of delay).
  754. */
  755. static endstop_mask_t old_live_state;
  756. if (old_live_state != live_state) {
  757. endstop_poll_count = ENDSTOP_NOISE_THRESHOLD;
  758. old_live_state = live_state;
  759. }
  760. else if (endstop_poll_count && !--endstop_poll_count)
  761. validated_live_state = live_state;
  762. if (!abort_enabled()) return;
  763. #endif
  764. // Test the current status of an endstop
  765. #define TEST_ENDSTOP(ENDSTOP) (TEST(state(), ENDSTOP))
  766. // Record endstop was hit
  767. #define _ENDSTOP_HIT(AXIS, MINMAX) SBI(hit_state, _ENDSTOP(AXIS, MINMAX))
  768. // Call the endstop triggered routine for single endstops
  769. #define PROCESS_ENDSTOP(AXIS, MINMAX) do { \
  770. if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX))) { \
  771. _ENDSTOP_HIT(AXIS, MINMAX); \
  772. planner.endstop_triggered(_AXIS(AXIS)); \
  773. } \
  774. }while(0)
  775. // Core Sensorless Homing needs to test an Extra Pin
  776. #define CORE_DIAG(QQ,A,MM) (CORE_IS_##QQ && A##_SENSORLESS && !A##_SPI_SENSORLESS && HAS_##A##_##MM)
  777. #define PROCESS_CORE_ENDSTOP(A1,M1,A2,M2) do { \
  778. if (TEST_ENDSTOP(_ENDSTOP(A1,M1))) { \
  779. _ENDSTOP_HIT(A2,M2); \
  780. planner.endstop_triggered(_AXIS(A2)); \
  781. } \
  782. }while(0)
  783. // Call the endstop triggered routine for dual endstops
  784. #define PROCESS_DUAL_ENDSTOP(A, MINMAX) do { \
  785. const byte dual_hit = TEST_ENDSTOP(_ENDSTOP(A, MINMAX)) | (TEST_ENDSTOP(_ENDSTOP(A##2, MINMAX)) << 1); \
  786. if (dual_hit) { \
  787. _ENDSTOP_HIT(A, MINMAX); \
  788. /* if not performing home or if both endstops were triggered during homing... */ \
  789. if (!stepper.separate_multi_axis || dual_hit == 0b11) \
  790. planner.endstop_triggered(_AXIS(A)); \
  791. } \
  792. }while(0)
  793. #define PROCESS_TRIPLE_ENDSTOP(A, MINMAX) do { \
  794. const byte triple_hit = TEST_ENDSTOP(_ENDSTOP(A, MINMAX)) | (TEST_ENDSTOP(_ENDSTOP(A##2, MINMAX)) << 1) | (TEST_ENDSTOP(_ENDSTOP(A##3, MINMAX)) << 2); \
  795. if (triple_hit) { \
  796. _ENDSTOP_HIT(A, MINMAX); \
  797. /* if not performing home or if both endstops were triggered during homing... */ \
  798. if (!stepper.separate_multi_axis || triple_hit == 0b111) \
  799. planner.endstop_triggered(_AXIS(A)); \
  800. } \
  801. }while(0)
  802. #define PROCESS_QUAD_ENDSTOP(A, MINMAX) do { \
  803. const byte quad_hit = TEST_ENDSTOP(_ENDSTOP(A, MINMAX)) | (TEST_ENDSTOP(_ENDSTOP(A##2, MINMAX)) << 1) | (TEST_ENDSTOP(_ENDSTOP(A##3, MINMAX)) << 2) | (TEST_ENDSTOP(_ENDSTOP(A##4, MINMAX)) << 3); \
  804. if (quad_hit) { \
  805. _ENDSTOP_HIT(A, MINMAX); \
  806. /* if not performing home or if both endstops were triggered during homing... */ \
  807. if (!stepper.separate_multi_axis || quad_hit == 0b1111) \
  808. planner.endstop_triggered(_AXIS(A)); \
  809. } \
  810. }while(0)
  811. #if ENABLED(X_DUAL_ENDSTOPS)
  812. #define PROCESS_ENDSTOP_X(MINMAX) PROCESS_DUAL_ENDSTOP(X, MINMAX)
  813. #else
  814. #define PROCESS_ENDSTOP_X(MINMAX) if (X_##MINMAX##_TEST()) PROCESS_ENDSTOP(X, MINMAX)
  815. #endif
  816. #if ENABLED(Y_DUAL_ENDSTOPS)
  817. #define PROCESS_ENDSTOP_Y(MINMAX) PROCESS_DUAL_ENDSTOP(Y, MINMAX)
  818. #else
  819. #define PROCESS_ENDSTOP_Y(MINMAX) PROCESS_ENDSTOP(Y, MINMAX)
  820. #endif
  821. #if DISABLED(Z_MULTI_ENDSTOPS)
  822. #define PROCESS_ENDSTOP_Z(MINMAX) PROCESS_ENDSTOP(Z, MINMAX)
  823. #elif NUM_Z_STEPPER_DRIVERS == 4
  824. #define PROCESS_ENDSTOP_Z(MINMAX) PROCESS_QUAD_ENDSTOP(Z, MINMAX)
  825. #elif NUM_Z_STEPPER_DRIVERS == 3
  826. #define PROCESS_ENDSTOP_Z(MINMAX) PROCESS_TRIPLE_ENDSTOP(Z, MINMAX)
  827. #else
  828. #define PROCESS_ENDSTOP_Z(MINMAX) PROCESS_DUAL_ENDSTOP(Z, MINMAX)
  829. #endif
  830. #if HAS_G38_PROBE
  831. #define _G38_OPEN_STATE TERN(G38_PROBE_AWAY, (G38_move >= 4), LOW)
  832. // For G38 moves check the probe's pin for ALL movement
  833. if (G38_move && TEST_ENDSTOP(_ENDSTOP(Z, TERN(USES_Z_MIN_PROBE_PIN, MIN_PROBE, MIN))) != _G38_OPEN_STATE) {
  834. if (stepper.axis_is_moving(X_AXIS)) { _ENDSTOP_HIT(X, TERN(X_HOME_TO_MIN, MIN, MAX)); planner.endstop_triggered(X_AXIS); }
  835. #if HAS_Y_AXIS
  836. else if (stepper.axis_is_moving(Y_AXIS)) { _ENDSTOP_HIT(Y, TERN(Y_HOME_TO_MIN, MIN, MAX)); planner.endstop_triggered(Y_AXIS); }
  837. #endif
  838. #if HAS_Z_AXIS
  839. else if (stepper.axis_is_moving(Z_AXIS)) { _ENDSTOP_HIT(Z, TERN(Z_HOME_TO_MIN, MIN, MAX)); planner.endstop_triggered(Z_AXIS); }
  840. #endif
  841. G38_did_trigger = true;
  842. }
  843. #endif
  844. // Signal, after validation, if an endstop limit is pressed or not
  845. if (stepper.axis_is_moving(X_AXIS)) {
  846. if (stepper.motor_direction(X_AXIS_HEAD)) { // -direction
  847. #if HAS_X_MIN || (X_SPI_SENSORLESS && X_HOME_TO_MIN)
  848. PROCESS_ENDSTOP_X(MIN);
  849. #if CORE_DIAG(XY, Y, MIN)
  850. PROCESS_CORE_ENDSTOP(Y,MIN,X,MIN);
  851. #elif CORE_DIAG(XY, Y, MAX)
  852. PROCESS_CORE_ENDSTOP(Y,MAX,X,MIN);
  853. #elif CORE_DIAG(XZ, Z, MIN)
  854. PROCESS_CORE_ENDSTOP(Z,MIN,X,MIN);
  855. #elif CORE_DIAG(XZ, Z, MAX)
  856. PROCESS_CORE_ENDSTOP(Z,MAX,X,MIN);
  857. #endif
  858. #endif
  859. }
  860. else { // +direction
  861. #if HAS_X_MAX || (X_SPI_SENSORLESS && X_HOME_TO_MAX)
  862. PROCESS_ENDSTOP_X(MAX);
  863. #if CORE_DIAG(XY, Y, MIN)
  864. PROCESS_CORE_ENDSTOP(Y,MIN,X,MAX);
  865. #elif CORE_DIAG(XY, Y, MAX)
  866. PROCESS_CORE_ENDSTOP(Y,MAX,X,MAX);
  867. #elif CORE_DIAG(XZ, Z, MIN)
  868. PROCESS_CORE_ENDSTOP(Z,MIN,X,MAX);
  869. #elif CORE_DIAG(XZ, Z, MAX)
  870. PROCESS_CORE_ENDSTOP(Z,MAX,X,MAX);
  871. #endif
  872. #endif
  873. }
  874. }
  875. #if HAS_Y_AXIS
  876. if (stepper.axis_is_moving(Y_AXIS)) {
  877. if (stepper.motor_direction(Y_AXIS_HEAD)) { // -direction
  878. #if HAS_Y_MIN || (Y_SPI_SENSORLESS && Y_HOME_TO_MIN)
  879. PROCESS_ENDSTOP_Y(MIN);
  880. #if CORE_DIAG(XY, X, MIN)
  881. PROCESS_CORE_ENDSTOP(X,MIN,Y,MIN);
  882. #elif CORE_DIAG(XY, X, MAX)
  883. PROCESS_CORE_ENDSTOP(X,MAX,Y,MIN);
  884. #elif CORE_DIAG(YZ, Z, MIN)
  885. PROCESS_CORE_ENDSTOP(Z,MIN,Y,MIN);
  886. #elif CORE_DIAG(YZ, Z, MAX)
  887. PROCESS_CORE_ENDSTOP(Z,MAX,Y,MIN);
  888. #endif
  889. #endif
  890. }
  891. else { // +direction
  892. #if HAS_Y_MAX || (Y_SPI_SENSORLESS && Y_HOME_TO_MAX)
  893. PROCESS_ENDSTOP_Y(MAX);
  894. #if CORE_DIAG(XY, X, MIN)
  895. PROCESS_CORE_ENDSTOP(X,MIN,Y,MAX);
  896. #elif CORE_DIAG(XY, X, MAX)
  897. PROCESS_CORE_ENDSTOP(X,MAX,Y,MAX);
  898. #elif CORE_DIAG(YZ, Z, MIN)
  899. PROCESS_CORE_ENDSTOP(Z,MIN,Y,MAX);
  900. #elif CORE_DIAG(YZ, Z, MAX)
  901. PROCESS_CORE_ENDSTOP(Z,MAX,Y,MAX);
  902. #endif
  903. #endif
  904. }
  905. }
  906. #endif
  907. #if HAS_Z_AXIS
  908. if (stepper.axis_is_moving(Z_AXIS)) {
  909. if (stepper.motor_direction(Z_AXIS_HEAD)) { // Z -direction. Gantry down, bed up.
  910. #if HAS_Z_MIN || (Z_SPI_SENSORLESS && Z_HOME_TO_MIN)
  911. if ( TERN1(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN, z_probe_enabled)
  912. && TERN1(USES_Z_MIN_PROBE_PIN, !z_probe_enabled)
  913. ) PROCESS_ENDSTOP_Z(MIN);
  914. #if CORE_DIAG(XZ, X, MIN)
  915. PROCESS_CORE_ENDSTOP(X,MIN,Z,MIN);
  916. #elif CORE_DIAG(XZ, X, MAX)
  917. PROCESS_CORE_ENDSTOP(X,MAX,Z,MIN);
  918. #elif CORE_DIAG(YZ, Y, MIN)
  919. PROCESS_CORE_ENDSTOP(Y,MIN,Z,MIN);
  920. #elif CORE_DIAG(YZ, Y, MAX)
  921. PROCESS_CORE_ENDSTOP(Y,MAX,Z,MIN);
  922. #endif
  923. #endif
  924. // When closing the gap check the enabled probe
  925. #if USES_Z_MIN_PROBE_PIN
  926. if (z_probe_enabled) PROCESS_ENDSTOP(Z, MIN_PROBE);
  927. #endif
  928. }
  929. else { // Z +direction. Gantry up, bed down.
  930. #if HAS_Z_MAX || (Z_SPI_SENSORLESS && Z_HOME_TO_MAX)
  931. #if ENABLED(Z_MULTI_ENDSTOPS)
  932. PROCESS_ENDSTOP_Z(MAX);
  933. #elif TERN1(USES_Z_MIN_PROBE_PIN, Z_MAX_PIN != Z_MIN_PROBE_PIN) // No probe or probe is Z_MIN || Probe is not Z_MAX
  934. PROCESS_ENDSTOP(Z, MAX);
  935. #endif
  936. #if CORE_DIAG(XZ, X, MIN)
  937. PROCESS_CORE_ENDSTOP(X,MIN,Z,MAX);
  938. #elif CORE_DIAG(XZ, X, MAX)
  939. PROCESS_CORE_ENDSTOP(X,MAX,Z,MAX);
  940. #elif CORE_DIAG(YZ, Y, MIN)
  941. PROCESS_CORE_ENDSTOP(Y,MIN,Z,MAX);
  942. #elif CORE_DIAG(YZ, Y, MAX)
  943. PROCESS_CORE_ENDSTOP(Y,MAX,Z,MAX);
  944. #endif
  945. #endif
  946. }
  947. }
  948. #endif
  949. #if HAS_I_AXIS
  950. if (stepper.axis_is_moving(I_AXIS)) {
  951. if (stepper.motor_direction(I_AXIS_HEAD)) { // -direction
  952. #if HAS_I_MIN || (I_SPI_SENSORLESS && I_HOME_TO_MIN)
  953. PROCESS_ENDSTOP(I, MIN);
  954. #endif
  955. }
  956. else { // +direction
  957. #if HAS_I_MAX || (I_SPI_SENSORLESS && I_HOME_TO_MAX)
  958. PROCESS_ENDSTOP(I, MAX);
  959. #endif
  960. }
  961. }
  962. #endif
  963. #if HAS_J_AXIS
  964. if (stepper.axis_is_moving(J_AXIS)) {
  965. if (stepper.motor_direction(J_AXIS_HEAD)) { // -direction
  966. #if HAS_J_MIN || (J_SPI_SENSORLESS && J_HOME_TO_MIN)
  967. PROCESS_ENDSTOP(J, MIN);
  968. #endif
  969. }
  970. else { // +direction
  971. #if HAS_J_MAX || (J_SPI_SENSORLESS && J_HOME_TO_MAX)
  972. PROCESS_ENDSTOP(J, MAX);
  973. #endif
  974. }
  975. }
  976. #endif
  977. #if HAS_K_AXIS
  978. if (stepper.axis_is_moving(K_AXIS)) {
  979. if (stepper.motor_direction(K_AXIS_HEAD)) { // -direction
  980. #if HAS_K_MIN || (K_SPI_SENSORLESS && K_HOME_TO_MIN)
  981. PROCESS_ENDSTOP(K, MIN);
  982. #endif
  983. }
  984. else { // +direction
  985. #if HAS_K_MAX || (K_SPI_SENSORLESS && K_HOME_TO_MAX)
  986. PROCESS_ENDSTOP(K, MAX);
  987. #endif
  988. }
  989. }
  990. #endif
  991. } // Endstops::update()
  992. #if ENABLED(SPI_ENDSTOPS)
  993. // Called from idle() to read Trinamic stall states
  994. bool Endstops::tmc_spi_homing_check() {
  995. bool hit = false;
  996. #if X_SPI_SENSORLESS
  997. if (tmc_spi_homing.x && (stepperX.test_stall_status()
  998. #if ANY(CORE_IS_XY, MARKFORGED_XY, MARKFORGED_YX) && Y_SPI_SENSORLESS
  999. || stepperY.test_stall_status()
  1000. #elif CORE_IS_XZ && Z_SPI_SENSORLESS
  1001. || stepperZ.test_stall_status()
  1002. #endif
  1003. )) {
  1004. SBI(live_state, X_ENDSTOP);
  1005. hit = true;
  1006. }
  1007. #endif
  1008. #if Y_SPI_SENSORLESS
  1009. if (tmc_spi_homing.y && (stepperY.test_stall_status()
  1010. #if ANY(CORE_IS_XY, MARKFORGED_XY, MARKFORGED_YX) && X_SPI_SENSORLESS
  1011. || stepperX.test_stall_status()
  1012. #elif CORE_IS_YZ && Z_SPI_SENSORLESS
  1013. || stepperZ.test_stall_status()
  1014. #endif
  1015. )) {
  1016. SBI(live_state, Y_ENDSTOP);
  1017. hit = true;
  1018. }
  1019. #endif
  1020. #if Z_SPI_SENSORLESS
  1021. if (tmc_spi_homing.z && (stepperZ.test_stall_status()
  1022. #if CORE_IS_XZ && X_SPI_SENSORLESS
  1023. || stepperX.test_stall_status()
  1024. #elif CORE_IS_YZ && Y_SPI_SENSORLESS
  1025. || stepperY.test_stall_status()
  1026. #endif
  1027. )) {
  1028. SBI(live_state, Z_ENDSTOP);
  1029. hit = true;
  1030. }
  1031. #endif
  1032. #if I_SPI_SENSORLESS
  1033. if (tmc_spi_homing.i && stepperI.test_stall_status()) {
  1034. SBI(live_state, I_ENDSTOP);
  1035. hit = true;
  1036. }
  1037. #endif
  1038. #if J_SPI_SENSORLESS
  1039. if (tmc_spi_homing.j && stepperJ.test_stall_status()) {
  1040. SBI(live_state, J_ENDSTOP);
  1041. hit = true;
  1042. }
  1043. #endif
  1044. #if K_SPI_SENSORLESS
  1045. if (tmc_spi_homing.k && stepperK.test_stall_status()) {
  1046. SBI(live_state, K_ENDSTOP);
  1047. hit = true;
  1048. }
  1049. #endif
  1050. if (TERN0(ENDSTOP_INTERRUPTS_FEATURE, hit)) update();
  1051. return hit;
  1052. }
  1053. void Endstops::clear_endstop_state() {
  1054. TERN_(X_SPI_SENSORLESS, CBI(live_state, X_ENDSTOP));
  1055. TERN_(Y_SPI_SENSORLESS, CBI(live_state, Y_ENDSTOP));
  1056. TERN_(Z_SPI_SENSORLESS, CBI(live_state, Z_ENDSTOP));
  1057. TERN_(I_SPI_SENSORLESS, CBI(live_state, I_ENDSTOP));
  1058. TERN_(J_SPI_SENSORLESS, CBI(live_state, J_ENDSTOP));
  1059. TERN_(K_SPI_SENSORLESS, CBI(live_state, K_ENDSTOP));
  1060. }
  1061. #endif // SPI_ENDSTOPS
  1062. #if ENABLED(PINS_DEBUGGING)
  1063. bool Endstops::monitor_flag = false;
  1064. /**
  1065. * Monitor Endstops and Z Probe for changes
  1066. *
  1067. * If a change is detected then the LED is toggled and
  1068. * a message is sent out the serial port.
  1069. *
  1070. * Yes, we could miss a rapid back & forth change but
  1071. * that won't matter because this is all manual.
  1072. */
  1073. void Endstops::monitor() {
  1074. static uint16_t old_live_state_local = 0;
  1075. static uint8_t local_LED_status = 0;
  1076. uint16_t live_state_local = 0;
  1077. #define ES_GET_STATE(S) if (READ(S##_PIN)) SBI(live_state_local, S)
  1078. #if HAS_X_MIN
  1079. ES_GET_STATE(X_MIN);
  1080. #endif
  1081. #if HAS_X_MAX
  1082. ES_GET_STATE(X_MAX);
  1083. #endif
  1084. #if HAS_Y_MIN
  1085. ES_GET_STATE(Y_MIN);
  1086. #endif
  1087. #if HAS_Y_MAX
  1088. ES_GET_STATE(Y_MAX);
  1089. #endif
  1090. #if HAS_Z_MIN
  1091. ES_GET_STATE(Z_MIN);
  1092. #endif
  1093. #if HAS_Z_MAX
  1094. ES_GET_STATE(Z_MAX);
  1095. #endif
  1096. #if HAS_Z_MIN_PROBE_PIN
  1097. ES_GET_STATE(Z_MIN_PROBE);
  1098. #endif
  1099. #if HAS_X2_MIN
  1100. ES_GET_STATE(X2_MIN);
  1101. #endif
  1102. #if HAS_X2_MAX
  1103. ES_GET_STATE(X2_MAX);
  1104. #endif
  1105. #if HAS_Y2_MIN
  1106. ES_GET_STATE(Y2_MIN);
  1107. #endif
  1108. #if HAS_Y2_MAX
  1109. ES_GET_STATE(Y2_MAX);
  1110. #endif
  1111. #if HAS_Z2_MIN
  1112. ES_GET_STATE(Z2_MIN);
  1113. #endif
  1114. #if HAS_Z2_MAX
  1115. ES_GET_STATE(Z2_MAX);
  1116. #endif
  1117. #if HAS_Z3_MIN
  1118. ES_GET_STATE(Z3_MIN);
  1119. #endif
  1120. #if HAS_Z3_MAX
  1121. ES_GET_STATE(Z3_MAX);
  1122. #endif
  1123. #if HAS_Z4_MIN
  1124. ES_GET_STATE(Z4_MIN);
  1125. #endif
  1126. #if HAS_Z4_MAX
  1127. ES_GET_STATE(Z4_MAX);
  1128. #endif
  1129. #if HAS_I_MAX
  1130. ES_GET_STATE(I_MAX);
  1131. #endif
  1132. #if HAS_I_MIN
  1133. ES_GET_STATE(I_MIN);
  1134. #endif
  1135. #if HAS_J_MAX
  1136. ES_GET_STATE(J_MAX);
  1137. #endif
  1138. #if HAS_J_MIN
  1139. ES_GET_STATE(J_MIN);
  1140. #endif
  1141. #if HAS_K_MAX
  1142. ES_GET_STATE(K_MAX);
  1143. #endif
  1144. #if HAS_K_MIN
  1145. ES_GET_STATE(K_MIN);
  1146. #endif
  1147. uint16_t endstop_change = live_state_local ^ old_live_state_local;
  1148. #define ES_REPORT_CHANGE(S) if (TEST(endstop_change, S)) SERIAL_ECHOPGM(" " STRINGIFY(S) ":", TEST(live_state_local, S))
  1149. if (endstop_change) {
  1150. #if HAS_X_MIN
  1151. ES_REPORT_CHANGE(X_MIN);
  1152. #endif
  1153. #if HAS_X_MAX
  1154. ES_REPORT_CHANGE(X_MAX);
  1155. #endif
  1156. #if HAS_Y_MIN
  1157. ES_REPORT_CHANGE(Y_MIN);
  1158. #endif
  1159. #if HAS_Y_MAX
  1160. ES_REPORT_CHANGE(Y_MAX);
  1161. #endif
  1162. #if HAS_Z_MIN
  1163. ES_REPORT_CHANGE(Z_MIN);
  1164. #endif
  1165. #if HAS_Z_MAX
  1166. ES_REPORT_CHANGE(Z_MAX);
  1167. #endif
  1168. #if HAS_Z_MIN_PROBE_PIN
  1169. ES_REPORT_CHANGE(Z_MIN_PROBE);
  1170. #endif
  1171. #if HAS_X2_MIN
  1172. ES_REPORT_CHANGE(X2_MIN);
  1173. #endif
  1174. #if HAS_X2_MAX
  1175. ES_REPORT_CHANGE(X2_MAX);
  1176. #endif
  1177. #if HAS_Y2_MIN
  1178. ES_REPORT_CHANGE(Y2_MIN);
  1179. #endif
  1180. #if HAS_Y2_MAX
  1181. ES_REPORT_CHANGE(Y2_MAX);
  1182. #endif
  1183. #if HAS_Z2_MIN
  1184. ES_REPORT_CHANGE(Z2_MIN);
  1185. #endif
  1186. #if HAS_Z2_MAX
  1187. ES_REPORT_CHANGE(Z2_MAX);
  1188. #endif
  1189. #if HAS_Z3_MIN
  1190. ES_REPORT_CHANGE(Z3_MIN);
  1191. #endif
  1192. #if HAS_Z3_MAX
  1193. ES_REPORT_CHANGE(Z3_MAX);
  1194. #endif
  1195. #if HAS_Z4_MIN
  1196. ES_REPORT_CHANGE(Z4_MIN);
  1197. #endif
  1198. #if HAS_Z4_MAX
  1199. ES_REPORT_CHANGE(Z4_MAX);
  1200. #endif
  1201. #if HAS_I_MIN
  1202. ES_REPORT_CHANGE(I_MIN);
  1203. #endif
  1204. #if HAS_I_MAX
  1205. ES_REPORT_CHANGE(I_MAX);
  1206. #endif
  1207. #if HAS_J_MIN
  1208. ES_REPORT_CHANGE(J_MIN);
  1209. #endif
  1210. #if HAS_J_MAX
  1211. ES_REPORT_CHANGE(J_MAX);
  1212. #endif
  1213. #if HAS_K_MIN
  1214. ES_REPORT_CHANGE(K_MIN);
  1215. #endif
  1216. #if HAS_K_MAX
  1217. ES_REPORT_CHANGE(K_MAX);
  1218. #endif
  1219. SERIAL_ECHOLNPGM("\n");
  1220. hal.set_pwm_duty(pin_t(LED_PIN), local_LED_status);
  1221. local_LED_status ^= 255;
  1222. old_live_state_local = live_state_local;
  1223. }
  1224. }
  1225. #endif // PINS_DEBUGGING