My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

endstops.cpp 27KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * endstops.cpp - A singleton object to manage endstops
  24. */
  25. #include "endstops.h"
  26. #include "stepper.h"
  27. #include "../MarlinCore.h"
  28. #include "../sd/cardreader.h"
  29. #include "temperature.h"
  30. #include "../lcd/ultralcd.h"
  31. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  32. #include HAL_PATH(../HAL, endstop_interrupts.h)
  33. #endif
  34. #if BOTH(SD_ABORT_ON_ENDSTOP_HIT, SDSUPPORT)
  35. #include "printcounter.h" // for print_job_timer
  36. #endif
  37. #if ENABLED(BLTOUCH)
  38. #include "../feature/bltouch.h"
  39. #endif
  40. #if ENABLED(JOYSTICK)
  41. #include "../feature/joystick.h"
  42. #endif
  43. Endstops endstops;
  44. // private:
  45. bool Endstops::enabled, Endstops::enabled_globally; // Initialized by settings.load()
  46. volatile uint8_t Endstops::hit_state;
  47. Endstops::esbits_t Endstops::live_state = 0;
  48. #if ENDSTOP_NOISE_THRESHOLD
  49. Endstops::esbits_t Endstops::validated_live_state;
  50. uint8_t Endstops::endstop_poll_count;
  51. #endif
  52. #if HAS_BED_PROBE
  53. volatile bool Endstops::z_probe_enabled = false;
  54. #endif
  55. // Initialized by settings.load()
  56. #if ENABLED(X_DUAL_ENDSTOPS)
  57. float Endstops::x2_endstop_adj;
  58. #endif
  59. #if ENABLED(Y_DUAL_ENDSTOPS)
  60. float Endstops::y2_endstop_adj;
  61. #endif
  62. #if 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. for (uint8_t i = 1; i <= NUM_RUNOUT_SENSORS; i++) {
  425. pin_t pin;
  426. switch (i) {
  427. default: continue;
  428. case 1: pin = FIL_RUNOUT_PIN; break;
  429. case 2: pin = FIL_RUNOUT2_PIN; break;
  430. #if NUM_RUNOUT_SENSORS > 2
  431. case 3: pin = FIL_RUNOUT3_PIN; break;
  432. #if NUM_RUNOUT_SENSORS > 3
  433. case 4: pin = FIL_RUNOUT4_PIN; break;
  434. #if NUM_RUNOUT_SENSORS > 4
  435. case 5: pin = FIL_RUNOUT5_PIN; break;
  436. #if NUM_RUNOUT_SENSORS > 5
  437. case 6: pin = FIL_RUNOUT6_PIN; break;
  438. #endif
  439. #endif
  440. #endif
  441. #endif
  442. }
  443. SERIAL_ECHOPGM(MSG_FILAMENT_RUNOUT_SENSOR);
  444. if (i > 1) SERIAL_CHAR(' ', '0' + i);
  445. print_es_state(extDigitalRead(pin) != FIL_RUNOUT_INVERTING);
  446. }
  447. #endif
  448. #endif
  449. #if ENABLED(BLTOUCH)
  450. bltouch._reset_SW_mode();
  451. #endif
  452. #if ENABLED(JOYSTICK_DEBUG)
  453. joystick.report();
  454. #endif
  455. } // Endstops::report_states
  456. // The following routines are called from an ISR context. It could be the temperature ISR, the
  457. // endstop ISR or the Stepper ISR.
  458. #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
  459. #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
  460. #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
  461. // Check endstops - Could be called from Temperature ISR!
  462. void Endstops::update() {
  463. #if !ENDSTOP_NOISE_THRESHOLD
  464. if (!abort_enabled()) return;
  465. #endif
  466. #define UPDATE_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT_TO(live_state, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
  467. #define COPY_LIVE_STATE(SRC_BIT, DST_BIT) SET_BIT_TO(live_state, DST_BIT, TEST(live_state, SRC_BIT))
  468. #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && !(CORE_IS_XY || CORE_IS_XZ)
  469. // If G38 command is active check Z_MIN_PROBE for ALL movement
  470. if (G38_move) UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
  471. #endif
  472. // With Dual X, endstops are only checked in the homing direction for the active extruder
  473. #if ENABLED(DUAL_X_CARRIAGE)
  474. #define E0_ACTIVE stepper.movement_extruder() == 0
  475. #define X_MIN_TEST ((X_HOME_DIR < 0 && E0_ACTIVE) || (X2_HOME_DIR < 0 && !E0_ACTIVE))
  476. #define X_MAX_TEST ((X_HOME_DIR > 0 && E0_ACTIVE) || (X2_HOME_DIR > 0 && !E0_ACTIVE))
  477. #else
  478. #define X_MIN_TEST true
  479. #define X_MAX_TEST true
  480. #endif
  481. // Use HEAD for core axes, AXIS for others
  482. #if CORE_IS_XY || CORE_IS_XZ
  483. #define X_AXIS_HEAD X_HEAD
  484. #else
  485. #define X_AXIS_HEAD X_AXIS
  486. #endif
  487. #if CORE_IS_XY || CORE_IS_YZ
  488. #define Y_AXIS_HEAD Y_HEAD
  489. #else
  490. #define Y_AXIS_HEAD Y_AXIS
  491. #endif
  492. #if CORE_IS_XZ || CORE_IS_YZ
  493. #define Z_AXIS_HEAD Z_HEAD
  494. #else
  495. #define Z_AXIS_HEAD Z_AXIS
  496. #endif
  497. /**
  498. * Check and update endstops
  499. */
  500. #if HAS_X_MIN && !X_SPI_SENSORLESS
  501. UPDATE_ENDSTOP_BIT(X, MIN);
  502. #if ENABLED(X_DUAL_ENDSTOPS)
  503. #if HAS_X2_MIN
  504. UPDATE_ENDSTOP_BIT(X2, MIN);
  505. #else
  506. COPY_LIVE_STATE(X_MIN, X2_MIN);
  507. #endif
  508. #endif
  509. #endif
  510. #if HAS_X_MAX && !X_SPI_SENSORLESS
  511. UPDATE_ENDSTOP_BIT(X, MAX);
  512. #if ENABLED(X_DUAL_ENDSTOPS)
  513. #if HAS_X2_MAX
  514. UPDATE_ENDSTOP_BIT(X2, MAX);
  515. #else
  516. COPY_LIVE_STATE(X_MAX, X2_MAX);
  517. #endif
  518. #endif
  519. #endif
  520. #if HAS_Y_MIN && !Y_SPI_SENSORLESS
  521. UPDATE_ENDSTOP_BIT(Y, MIN);
  522. #if ENABLED(Y_DUAL_ENDSTOPS)
  523. #if HAS_Y2_MIN
  524. UPDATE_ENDSTOP_BIT(Y2, MIN);
  525. #else
  526. COPY_LIVE_STATE(Y_MIN, Y2_MIN);
  527. #endif
  528. #endif
  529. #endif
  530. #if HAS_Y_MAX && !Y_SPI_SENSORLESS
  531. UPDATE_ENDSTOP_BIT(Y, MAX);
  532. #if ENABLED(Y_DUAL_ENDSTOPS)
  533. #if HAS_Y2_MAX
  534. UPDATE_ENDSTOP_BIT(Y2, MAX);
  535. #else
  536. COPY_LIVE_STATE(Y_MAX, Y2_MAX);
  537. #endif
  538. #endif
  539. #endif
  540. #if HAS_Z_MIN && !Z_SPI_SENSORLESS
  541. UPDATE_ENDSTOP_BIT(Z, MIN);
  542. #if ENABLED(Z_MULTI_ENDSTOPS)
  543. #if HAS_Z2_MIN
  544. UPDATE_ENDSTOP_BIT(Z2, MIN);
  545. #else
  546. COPY_LIVE_STATE(Z_MIN, Z2_MIN);
  547. #endif
  548. #if NUM_Z_STEPPER_DRIVERS >= 3
  549. #if HAS_Z3_MIN
  550. UPDATE_ENDSTOP_BIT(Z3, MIN);
  551. #else
  552. COPY_LIVE_STATE(Z_MIN, Z3_MIN);
  553. #endif
  554. #endif
  555. #if NUM_Z_STEPPER_DRIVERS >= 4
  556. #if HAS_Z4_MIN
  557. UPDATE_ENDSTOP_BIT(Z4, MIN);
  558. #else
  559. COPY_LIVE_STATE(Z_MIN, Z4_MIN);
  560. #endif
  561. #endif
  562. #endif
  563. #endif
  564. // When closing the gap check the enabled probe
  565. #if HAS_CUSTOM_PROBE_PIN
  566. UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
  567. #endif
  568. #if HAS_Z_MAX && !Z_SPI_SENSORLESS
  569. // Check both Z dual endstops
  570. #if ENABLED(Z_MULTI_ENDSTOPS)
  571. UPDATE_ENDSTOP_BIT(Z, MAX);
  572. #if HAS_Z2_MAX
  573. UPDATE_ENDSTOP_BIT(Z2, MAX);
  574. #else
  575. COPY_LIVE_STATE(Z_MAX, Z2_MAX);
  576. #endif
  577. #if NUM_Z_STEPPER_DRIVERS >= 3
  578. #if HAS_Z3_MAX
  579. UPDATE_ENDSTOP_BIT(Z3, MAX);
  580. #else
  581. COPY_LIVE_STATE(Z_MAX, Z3_MAX);
  582. #endif
  583. #endif
  584. #if NUM_Z_STEPPER_DRIVERS >= 4
  585. #if HAS_Z4_MAX
  586. UPDATE_ENDSTOP_BIT(Z4, MAX);
  587. #else
  588. COPY_LIVE_STATE(Z_MAX, Z4_MAX);
  589. #endif
  590. #endif
  591. #elif !HAS_CUSTOM_PROBE_PIN || Z_MAX_PIN != Z_MIN_PROBE_PIN
  592. // If this pin isn't the bed probe it's the Z endstop
  593. UPDATE_ENDSTOP_BIT(Z, MAX);
  594. #endif
  595. #endif
  596. #if ENDSTOP_NOISE_THRESHOLD
  597. /**
  598. * Filtering out noise on endstops requires a delayed decision. Let's assume, due to noise,
  599. * that 50% of endstop signal samples are good and 50% are bad (assuming normal distribution
  600. * of random noise). Then the first sample has a 50% chance to be good or bad. The 2nd sample
  601. * also has a 50% chance to be good or bad. The chances of 2 samples both being bad becomes
  602. * 50% of 50%, or 25%. That was the previous implementation of Marlin endstop handling. It
  603. * reduces chances of bad readings in half, at the cost of 1 extra sample period, but chances
  604. * still exist. The only way to reduce them further is to increase the number of samples.
  605. * To reduce the chance to 1% (1/128th) requires 7 samples (adding 7ms of delay).
  606. */
  607. static esbits_t old_live_state;
  608. if (old_live_state != live_state) {
  609. endstop_poll_count = ENDSTOP_NOISE_THRESHOLD;
  610. old_live_state = live_state;
  611. }
  612. else if (endstop_poll_count && !--endstop_poll_count)
  613. validated_live_state = live_state;
  614. if (!abort_enabled()) return;
  615. #endif
  616. // Test the current status of an endstop
  617. #define TEST_ENDSTOP(ENDSTOP) (TEST(state(), ENDSTOP))
  618. // Record endstop was hit
  619. #define _ENDSTOP_HIT(AXIS, MINMAX) SBI(hit_state, _ENDSTOP(AXIS, MINMAX))
  620. // Call the endstop triggered routine for single endstops
  621. #define PROCESS_ENDSTOP(AXIS,MINMAX) do { \
  622. if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX))) { \
  623. _ENDSTOP_HIT(AXIS, MINMAX); \
  624. planner.endstop_triggered(_AXIS(AXIS)); \
  625. } \
  626. }while(0)
  627. // Call the endstop triggered routine for dual endstops
  628. #define PROCESS_DUAL_ENDSTOP(AXIS1, AXIS2, MINMAX) do { \
  629. const byte dual_hit = TEST_ENDSTOP(_ENDSTOP(AXIS1, MINMAX)) | (TEST_ENDSTOP(_ENDSTOP(AXIS2, MINMAX)) << 1); \
  630. if (dual_hit) { \
  631. _ENDSTOP_HIT(AXIS1, MINMAX); \
  632. /* if not performing home or if both endstops were trigged during homing... */ \
  633. if (!stepper.separate_multi_axis || dual_hit == 0b11) \
  634. planner.endstop_triggered(_AXIS(AXIS1)); \
  635. } \
  636. }while(0)
  637. #define PROCESS_TRIPLE_ENDSTOP(AXIS1, AXIS2, AXIS3, MINMAX) do { \
  638. const byte triple_hit = TEST_ENDSTOP(_ENDSTOP(AXIS1, MINMAX)) | (TEST_ENDSTOP(_ENDSTOP(AXIS2, MINMAX)) << 1) | (TEST_ENDSTOP(_ENDSTOP(AXIS3, MINMAX)) << 2); \
  639. if (triple_hit) { \
  640. _ENDSTOP_HIT(AXIS1, MINMAX); \
  641. /* if not performing home or if both endstops were trigged during homing... */ \
  642. if (!stepper.separate_multi_axis || triple_hit == 0b111) \
  643. planner.endstop_triggered(_AXIS(AXIS1)); \
  644. } \
  645. }while(0)
  646. #define PROCESS_QUAD_ENDSTOP(AXIS1, AXIS2, AXIS3, AXIS4, MINMAX) do { \
  647. 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); \
  648. if (quad_hit) { \
  649. _ENDSTOP_HIT(AXIS1, MINMAX); \
  650. /* if not performing home or if both endstops were trigged during homing... */ \
  651. if (!stepper.separate_multi_axis || quad_hit == 0b1111) \
  652. planner.endstop_triggered(_AXIS(AXIS1)); \
  653. } \
  654. }while(0)
  655. #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && !(CORE_IS_XY || CORE_IS_XZ)
  656. #if ENABLED(G38_PROBE_AWAY)
  657. #define _G38_OPEN_STATE (G38_move >= 4)
  658. #else
  659. #define _G38_OPEN_STATE LOW
  660. #endif
  661. // If G38 command is active check Z_MIN_PROBE for ALL movement
  662. if (G38_move && TEST_ENDSTOP(_ENDSTOP(Z, MIN_PROBE)) != _G38_OPEN_STATE) {
  663. if (stepper.axis_is_moving(X_AXIS)) { _ENDSTOP_HIT(X, MIN); planner.endstop_triggered(X_AXIS); }
  664. else if (stepper.axis_is_moving(Y_AXIS)) { _ENDSTOP_HIT(Y, MIN); planner.endstop_triggered(Y_AXIS); }
  665. else if (stepper.axis_is_moving(Z_AXIS)) { _ENDSTOP_HIT(Z, MIN); planner.endstop_triggered(Z_AXIS); }
  666. G38_did_trigger = true;
  667. }
  668. #endif
  669. // Now, we must signal, after validation, if an endstop limit is pressed or not
  670. if (stepper.axis_is_moving(X_AXIS)) {
  671. if (stepper.motor_direction(X_AXIS_HEAD)) { // -direction
  672. #if HAS_X_MIN || (X_SPI_SENSORLESS && X_HOME_DIR < 0)
  673. #if ENABLED(X_DUAL_ENDSTOPS)
  674. PROCESS_DUAL_ENDSTOP(X, X2, MIN);
  675. #else
  676. if (X_MIN_TEST) PROCESS_ENDSTOP(X, MIN);
  677. #endif
  678. #endif
  679. }
  680. else { // +direction
  681. #if HAS_X_MAX || (X_SPI_SENSORLESS && X_HOME_DIR > 0)
  682. #if ENABLED(X_DUAL_ENDSTOPS)
  683. PROCESS_DUAL_ENDSTOP(X, X2, MAX);
  684. #else
  685. if (X_MAX_TEST) PROCESS_ENDSTOP(X, MAX);
  686. #endif
  687. #endif
  688. }
  689. }
  690. if (stepper.axis_is_moving(Y_AXIS)) {
  691. if (stepper.motor_direction(Y_AXIS_HEAD)) { // -direction
  692. #if HAS_Y_MIN || (Y_SPI_SENSORLESS && Y_HOME_DIR < 0)
  693. #if ENABLED(Y_DUAL_ENDSTOPS)
  694. PROCESS_DUAL_ENDSTOP(Y, Y2, MIN);
  695. #else
  696. PROCESS_ENDSTOP(Y, MIN);
  697. #endif
  698. #endif
  699. }
  700. else { // +direction
  701. #if HAS_Y_MAX || (Y_SPI_SENSORLESS && Y_HOME_DIR > 0)
  702. #if ENABLED(Y_DUAL_ENDSTOPS)
  703. PROCESS_DUAL_ENDSTOP(Y, Y2, MAX);
  704. #else
  705. PROCESS_ENDSTOP(Y, MAX);
  706. #endif
  707. #endif
  708. }
  709. }
  710. if (stepper.axis_is_moving(Z_AXIS)) {
  711. if (stepper.motor_direction(Z_AXIS_HEAD)) { // Z -direction. Gantry down, bed up.
  712. #if HAS_Z_MIN || (Z_SPI_SENSORLESS && Z_HOME_DIR < 0)
  713. #if ENABLED(Z_MULTI_ENDSTOPS)
  714. #if NUM_Z_STEPPER_DRIVERS == 4
  715. PROCESS_QUAD_ENDSTOP(Z, Z2, Z3, Z4, MIN);
  716. #elif NUM_Z_STEPPER_DRIVERS == 3
  717. PROCESS_TRIPLE_ENDSTOP(Z, Z2, Z3, MIN);
  718. #else
  719. PROCESS_DUAL_ENDSTOP(Z, Z2, MIN);
  720. #endif
  721. #else
  722. #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  723. if (z_probe_enabled) PROCESS_ENDSTOP(Z, MIN);
  724. #elif HAS_CUSTOM_PROBE_PIN
  725. if (!z_probe_enabled) PROCESS_ENDSTOP(Z, MIN);
  726. #else
  727. PROCESS_ENDSTOP(Z, MIN);
  728. #endif
  729. #endif
  730. #endif
  731. // When closing the gap check the enabled probe
  732. #if HAS_CUSTOM_PROBE_PIN
  733. if (z_probe_enabled) PROCESS_ENDSTOP(Z, MIN_PROBE);
  734. #endif
  735. }
  736. else { // Z +direction. Gantry up, bed down.
  737. #if HAS_Z_MAX || (Z_SPI_SENSORLESS && Z_HOME_DIR > 0)
  738. #if ENABLED(Z_MULTI_ENDSTOPS)
  739. #if NUM_Z_STEPPER_DRIVERS == 4
  740. PROCESS_QUAD_ENDSTOP(Z, Z2, Z3, Z4, MAX);
  741. #elif NUM_Z_STEPPER_DRIVERS == 3
  742. PROCESS_TRIPLE_ENDSTOP(Z, Z2, Z3, MAX);
  743. #else
  744. PROCESS_DUAL_ENDSTOP(Z, Z2, MAX);
  745. #endif
  746. #elif !HAS_CUSTOM_PROBE_PIN || Z_MAX_PIN != Z_MIN_PROBE_PIN
  747. // If this pin is not hijacked for the bed probe
  748. // then it belongs to the Z endstop
  749. PROCESS_ENDSTOP(Z, MAX);
  750. #endif
  751. #endif
  752. }
  753. }
  754. } // Endstops::update()
  755. #if ENABLED(SPI_ENDSTOPS)
  756. #define X_STOP (X_HOME_DIR < 0 ? X_MIN : X_MAX)
  757. #define Y_STOP (Y_HOME_DIR < 0 ? Y_MIN : Y_MAX)
  758. #define Z_STOP (Z_HOME_DIR < 0 ? Z_MIN : Z_MAX)
  759. bool Endstops::tmc_spi_homing_check() {
  760. bool hit = false;
  761. #if X_SPI_SENSORLESS
  762. if (tmc_spi_homing.x && stepperX.test_stall_status()) {
  763. SBI(live_state, X_STOP);
  764. hit = true;
  765. }
  766. #endif
  767. #if Y_SPI_SENSORLESS
  768. if (tmc_spi_homing.y && stepperY.test_stall_status()) {
  769. SBI(live_state, Y_STOP);
  770. hit = true;
  771. }
  772. #endif
  773. #if Z_SPI_SENSORLESS
  774. if (tmc_spi_homing.z && stepperZ.test_stall_status()) {
  775. SBI(live_state, Z_STOP);
  776. hit = true;
  777. }
  778. #endif
  779. return hit;
  780. }
  781. void Endstops::clear_endstop_state() {
  782. #if X_SPI_SENSORLESS
  783. CBI(live_state, X_STOP);
  784. #endif
  785. #if Y_SPI_SENSORLESS
  786. CBI(live_state, Y_STOP);
  787. #endif
  788. #if Z_SPI_SENSORLESS
  789. CBI(live_state, Z_STOP);
  790. #endif
  791. }
  792. #endif // SPI_ENDSTOPS
  793. #if ENABLED(PINS_DEBUGGING)
  794. bool Endstops::monitor_flag = false;
  795. /**
  796. * Monitor Endstops and Z Probe for changes
  797. *
  798. * If a change is detected then the LED is toggled and
  799. * a message is sent out the serial port.
  800. *
  801. * Yes, we could miss a rapid back & forth change but
  802. * that won't matter because this is all manual.
  803. */
  804. void Endstops::monitor() {
  805. static uint16_t old_live_state_local = 0;
  806. static uint8_t local_LED_status = 0;
  807. uint16_t live_state_local = 0;
  808. #define ES_GET_STATE(S) if (READ(S##_PIN)) SBI(live_state_local, S)
  809. #if HAS_X_MIN
  810. ES_GET_STATE(X_MIN);
  811. #endif
  812. #if HAS_X_MAX
  813. ES_GET_STATE(X_MAX);
  814. #endif
  815. #if HAS_Y_MIN
  816. ES_GET_STATE(Y_MIN);
  817. #endif
  818. #if HAS_Y_MAX
  819. ES_GET_STATE(Y_MAX);
  820. #endif
  821. #if HAS_Z_MIN
  822. ES_GET_STATE(Z_MIN);
  823. #endif
  824. #if HAS_Z_MAX
  825. ES_GET_STATE(Z_MAX);
  826. #endif
  827. #if HAS_Z_MIN_PROBE_PIN
  828. ES_GET_STATE(Z_MIN_PROBE);
  829. #endif
  830. #if HAS_X2_MIN
  831. ES_GET_STATE(X2_MIN);
  832. #endif
  833. #if HAS_X2_MAX
  834. ES_GET_STATE(X2_MAX);
  835. #endif
  836. #if HAS_Y2_MIN
  837. ES_GET_STATE(Y2_MIN);
  838. #endif
  839. #if HAS_Y2_MAX
  840. ES_GET_STATE(Y2_MAX);
  841. #endif
  842. #if HAS_Z2_MIN
  843. ES_GET_STATE(Z2_MIN);
  844. #endif
  845. #if HAS_Z2_MAX
  846. ES_GET_STATE(Z2_MAX);
  847. #endif
  848. #if HAS_Z3_MIN
  849. ES_GET_STATE(Z3_MIN);
  850. #endif
  851. #if HAS_Z3_MAX
  852. ES_GET_STATE(Z3_MAX);
  853. #endif
  854. #if HAS_Z4_MIN
  855. ES_GET_STATE(Z4_MIN);
  856. #endif
  857. #if HAS_Z4_MAX
  858. ES_GET_STATE(Z4_MAX);
  859. #endif
  860. uint16_t endstop_change = live_state_local ^ old_live_state_local;
  861. #define ES_REPORT_CHANGE(S) if (TEST(endstop_change, S)) SERIAL_ECHOPAIR(" " STRINGIFY(S) ":", TEST(live_state_local, S))
  862. if (endstop_change) {
  863. #if HAS_X_MIN
  864. ES_REPORT_CHANGE(X_MIN);
  865. #endif
  866. #if HAS_X_MAX
  867. ES_REPORT_CHANGE(X_MAX);
  868. #endif
  869. #if HAS_Y_MIN
  870. ES_REPORT_CHANGE(Y_MIN);
  871. #endif
  872. #if HAS_Y_MAX
  873. ES_REPORT_CHANGE(Y_MAX);
  874. #endif
  875. #if HAS_Z_MIN
  876. ES_REPORT_CHANGE(Z_MIN);
  877. #endif
  878. #if HAS_Z_MAX
  879. ES_REPORT_CHANGE(Z_MAX);
  880. #endif
  881. #if HAS_Z_MIN_PROBE_PIN
  882. ES_REPORT_CHANGE(Z_MIN_PROBE);
  883. #endif
  884. #if HAS_X2_MIN
  885. ES_REPORT_CHANGE(X2_MIN);
  886. #endif
  887. #if HAS_X2_MAX
  888. ES_REPORT_CHANGE(X2_MAX);
  889. #endif
  890. #if HAS_Y2_MIN
  891. ES_REPORT_CHANGE(Y2_MIN);
  892. #endif
  893. #if HAS_Y2_MAX
  894. ES_REPORT_CHANGE(Y2_MAX);
  895. #endif
  896. #if HAS_Z2_MIN
  897. ES_REPORT_CHANGE(Z2_MIN);
  898. #endif
  899. #if HAS_Z2_MAX
  900. ES_REPORT_CHANGE(Z2_MAX);
  901. #endif
  902. #if HAS_Z3_MIN
  903. ES_REPORT_CHANGE(Z3_MIN);
  904. #endif
  905. #if HAS_Z3_MAX
  906. ES_REPORT_CHANGE(Z3_MAX);
  907. #endif
  908. #if HAS_Z4_MIN
  909. ES_REPORT_CHANGE(Z4_MIN);
  910. #endif
  911. #if HAS_Z4_MAX
  912. ES_REPORT_CHANGE(Z4_MAX);
  913. #endif
  914. SERIAL_ECHOLNPGM("\n");
  915. analogWrite(pin_t(LED_PIN), local_LED_status);
  916. local_LED_status ^= 255;
  917. old_live_state_local = live_state_local;
  918. }
  919. }
  920. #endif // PINS_DEBUGGING