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

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