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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * endstops.cpp - A singleton object to manage endstops
  24. */
  25. #include "endstops.h"
  26. #include "stepper.h"
  27. #include "../Marlin.h"
  28. #include "../sd/cardreader.h"
  29. #include "../module/temperature.h"
  30. #include "../lcd/ultralcd.h"
  31. // TEST_ENDSTOP: test the old and the current status of an endstop
  32. #define TEST_ENDSTOP(ENDSTOP) (TEST(current_endstop_bits & old_endstop_bits, ENDSTOP))
  33. Endstops endstops;
  34. // public:
  35. bool Endstops::enabled, Endstops::enabled_globally; // Initialized by settings.load()
  36. volatile char Endstops::endstop_hit_bits; // use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT value
  37. Endstops::esbits_t Endstops::current_endstop_bits = 0,
  38. Endstops::old_endstop_bits = 0;
  39. #if HAS_BED_PROBE
  40. volatile bool Endstops::z_probe_enabled = false;
  41. #endif
  42. // Initialized by settings.load()
  43. #if ENABLED(X_DUAL_ENDSTOPS)
  44. float Endstops::x_endstop_adj;
  45. #endif
  46. #if ENABLED(Y_DUAL_ENDSTOPS)
  47. float Endstops::y_endstop_adj;
  48. #endif
  49. #if ENABLED(Z_DUAL_ENDSTOPS)
  50. float Endstops::z_endstop_adj;
  51. #endif
  52. /**
  53. * Class and Instance Methods
  54. */
  55. void Endstops::init() {
  56. #if HAS_X_MIN
  57. #if ENABLED(ENDSTOPPULLUP_XMIN)
  58. SET_INPUT_PULLUP(X_MIN_PIN);
  59. #elif ENABLED(ENDSTOPPULLDOWN_XMIN)
  60. SET_INPUT_PULLDOWN(X_MIN_PIN);
  61. #else
  62. SET_INPUT(X_MIN_PIN);
  63. #endif
  64. #endif
  65. #if HAS_X2_MIN
  66. #if ENABLED(ENDSTOPPULLUP_XMIN)
  67. SET_INPUT_PULLUP(X2_MIN_PIN);
  68. #elif ENABLED(ENDSTOPPULLDOWN_XMIN)
  69. SET_INPUT_PULLDOWN(X2_MIN_PIN);
  70. #else
  71. SET_INPUT(X2_MIN_PIN);
  72. #endif
  73. #endif
  74. #if HAS_Y_MIN
  75. #if ENABLED(ENDSTOPPULLUP_YMIN)
  76. SET_INPUT_PULLUP(Y_MIN_PIN);
  77. #elif ENABLED(ENDSTOPPULLDOWN_YMIN)
  78. SET_INPUT_PULLDOWN(Y_MIN_PIN);
  79. #else
  80. SET_INPUT(Y_MIN_PIN);
  81. #endif
  82. #endif
  83. #if HAS_Y2_MIN
  84. #if ENABLED(ENDSTOPPULLUP_YMIN)
  85. SET_INPUT_PULLUP(Y2_MIN_PIN);
  86. #elif ENABLED(ENDSTOPPULLDOWN_YMIN)
  87. SET_INPUT_PULLDOWN(Y2_MIN_PIN);
  88. #else
  89. SET_INPUT(Y2_MIN_PIN);
  90. #endif
  91. #endif
  92. #if HAS_Z_MIN
  93. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  94. SET_INPUT_PULLUP(Z_MIN_PIN);
  95. #elif ENABLED(ENDSTOPPULLDOWN_ZMIN)
  96. SET_INPUT_PULLDOWN(Z_MIN_PIN);
  97. #else
  98. SET_INPUT(Z_MIN_PIN);
  99. #endif
  100. #endif
  101. #if HAS_Z2_MIN
  102. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  103. SET_INPUT_PULLUP(Z2_MIN_PIN);
  104. #elif ENABLED(ENDSTOPPULLDOWN_ZMIN)
  105. SET_INPUT_PULLDOWN(Z2_MIN_PIN);
  106. #else
  107. SET_INPUT(Z2_MIN_PIN);
  108. #endif
  109. #endif
  110. #if HAS_X_MAX
  111. #if ENABLED(ENDSTOPPULLUP_XMAX)
  112. SET_INPUT_PULLUP(X_MAX_PIN);
  113. #elif ENABLED(ENDSTOPPULLDOWN_XMAX)
  114. SET_INPUT_PULLDOWN(X_MAX_PIN);
  115. #else
  116. SET_INPUT(X_MAX_PIN);
  117. #endif
  118. #endif
  119. #if HAS_X2_MAX
  120. #if ENABLED(ENDSTOPPULLUP_XMAX)
  121. SET_INPUT_PULLUP(X2_MAX_PIN);
  122. #elif ENABLED(ENDSTOPPULLDOWN_XMAX)
  123. SET_INPUT_PULLDOWN(X2_MAX_PIN);
  124. #else
  125. SET_INPUT(X2_MAX_PIN);
  126. #endif
  127. #endif
  128. #if HAS_Y_MAX
  129. #if ENABLED(ENDSTOPPULLUP_YMAX)
  130. SET_INPUT_PULLUP(Y_MAX_PIN);
  131. #elif ENABLED(ENDSTOPPULLDOWN_YMAX)
  132. SET_INPUT_PULLDOWN(Y_MAX_PIN);
  133. #else
  134. SET_INPUT(Y_MAX_PIN);
  135. #endif
  136. #endif
  137. #if HAS_Y2_MAX
  138. #if ENABLED(ENDSTOPPULLUP_YMAX)
  139. SET_INPUT_PULLUP(Y2_MAX_PIN);
  140. #elif ENABLED(ENDSTOPPULLDOWN_YMAX)
  141. SET_INPUT_PULLDOWN(Y2_MAX_PIN);
  142. #else
  143. SET_INPUT(Y2_MAX_PIN);
  144. #endif
  145. #endif
  146. #if HAS_Z_MAX
  147. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  148. SET_INPUT_PULLUP(Z_MAX_PIN);
  149. #elif ENABLED(ENDSTOPPULLDOWN_ZMAX)
  150. SET_INPUT_PULLDOWN(Z_MAX_PIN);
  151. #else
  152. SET_INPUT(Z_MAX_PIN);
  153. #endif
  154. #endif
  155. #if HAS_Z2_MAX
  156. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  157. SET_INPUT_PULLUP(Z2_MAX_PIN);
  158. #elif ENABLED(ENDSTOPPULLDOWN_ZMAX)
  159. SET_INPUT_PULLDOWN(Z2_MAX_PIN);
  160. #else
  161. SET_INPUT(Z2_MAX_PIN);
  162. #endif
  163. #endif
  164. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  165. #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
  166. SET_INPUT_PULLUP(Z_MIN_PROBE_PIN);
  167. #elif ENABLED(ENDSTOPPULLDOWN_ZMIN_PROBE)
  168. SET_INPUT_PULLDOWN(Z_MIN_PROBE_PIN);
  169. #else
  170. SET_INPUT(Z_MIN_PROBE_PIN);
  171. #endif
  172. #endif
  173. } // Endstops::init
  174. void Endstops::report_state() {
  175. if (endstop_hit_bits) {
  176. #if ENABLED(ULTRA_LCD)
  177. char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
  178. #define _SET_STOP_CHAR(A,C) (chr## A = C)
  179. #else
  180. #define _SET_STOP_CHAR(A,C) ;
  181. #endif
  182. #define _ENDSTOP_HIT_ECHO(A,C) do{ \
  183. SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", stepper.triggered_position_mm(A ##_AXIS)); \
  184. _SET_STOP_CHAR(A,C); }while(0)
  185. #define _ENDSTOP_HIT_TEST(A,C) \
  186. if (TEST(endstop_hit_bits, A ##_MIN) || TEST(endstop_hit_bits, A ##_MAX)) \
  187. _ENDSTOP_HIT_ECHO(A,C)
  188. #define ENDSTOP_HIT_TEST_X() _ENDSTOP_HIT_TEST(X,'X')
  189. #define ENDSTOP_HIT_TEST_Y() _ENDSTOP_HIT_TEST(Y,'Y')
  190. #define ENDSTOP_HIT_TEST_Z() _ENDSTOP_HIT_TEST(Z,'Z')
  191. SERIAL_ECHO_START();
  192. SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
  193. ENDSTOP_HIT_TEST_X();
  194. ENDSTOP_HIT_TEST_Y();
  195. ENDSTOP_HIT_TEST_Z();
  196. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  197. #define P_AXIS Z_AXIS
  198. if (TEST(endstop_hit_bits, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
  199. #endif
  200. SERIAL_EOL();
  201. #if ENABLED(ULTRA_LCD)
  202. lcd_status_printf_P(0, PSTR(MSG_LCD_ENDSTOPS " %c %c %c %c"), chrX, chrY, chrZ, chrP);
  203. #endif
  204. hit_on_purpose();
  205. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && ENABLED(SDSUPPORT)
  206. if (stepper.abort_on_endstop_hit) {
  207. card.sdprinting = false;
  208. card.closefile();
  209. quickstop_stepper();
  210. thermalManager.disable_all_heaters(); // switch off all heaters.
  211. }
  212. #endif
  213. }
  214. } // Endstops::report_state
  215. void Endstops::M119() {
  216. SERIAL_PROTOCOLLNPGM(MSG_M119_REPORT);
  217. #define ES_REPORT(AXIS) do{ \
  218. SERIAL_PROTOCOLPGM(MSG_##AXIS); \
  219. SERIAL_PROTOCOLLN(((READ(AXIS##_PIN)^AXIS##_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN)); \
  220. }while(0)
  221. #if HAS_X_MIN
  222. ES_REPORT(X_MIN);
  223. #endif
  224. #if HAS_X2_MIN
  225. ES_REPORT(X2_MIN);
  226. #endif
  227. #if HAS_X_MAX
  228. ES_REPORT(X_MAX);
  229. #endif
  230. #if HAS_X2_MAX
  231. ES_REPORT(X2_MAX);
  232. #endif
  233. #if HAS_Y_MIN
  234. ES_REPORT(Y_MIN);
  235. #endif
  236. #if HAS_Y2_MIN
  237. ES_REPORT(Y2_MIN);
  238. #endif
  239. #if HAS_Y_MAX
  240. ES_REPORT(Y_MAX);
  241. #endif
  242. #if HAS_Y2_MAX
  243. ES_REPORT(Y2_MAX);
  244. #endif
  245. #if HAS_Z_MIN
  246. ES_REPORT(Z_MIN);
  247. #endif
  248. #if HAS_Z2_MIN
  249. ES_REPORT(Z2_MIN);
  250. #endif
  251. #if HAS_Z_MAX
  252. ES_REPORT(Z_MAX);
  253. #endif
  254. #if HAS_Z2_MAX
  255. ES_REPORT(Z2_MAX);
  256. #endif
  257. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  258. SERIAL_PROTOCOLPGM(MSG_Z_PROBE);
  259. SERIAL_PROTOCOLLN(((READ(Z_MIN_PROBE_PIN)^Z_MIN_PROBE_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  260. #endif
  261. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  262. SERIAL_PROTOCOLPGM(MSG_FILAMENT_RUNOUT_SENSOR);
  263. SERIAL_PROTOCOLLN(((READ(FIL_RUNOUT_PIN)^FIL_RUNOUT_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  264. #endif
  265. } // Endstops::M119
  266. #if ENABLED(X_DUAL_ENDSTOPS)
  267. void Endstops::test_dual_x_endstops(const EndstopEnum es1, const EndstopEnum es2) {
  268. const byte x_test = TEST_ENDSTOP(es1) | (TEST_ENDSTOP(es2) << 1); // bit 0 for X, bit 1 for X2
  269. if (x_test && stepper.current_block->steps[X_AXIS] > 0) {
  270. SBI(endstop_hit_bits, X_MIN);
  271. if (!stepper.performing_homing || (x_test == 0x3)) //if not performing home or if both endstops were trigged during homing...
  272. stepper.kill_current_block();
  273. }
  274. }
  275. #endif
  276. #if ENABLED(Y_DUAL_ENDSTOPS)
  277. void Endstops::test_dual_y_endstops(const EndstopEnum es1, const EndstopEnum es2) {
  278. const byte y_test = TEST_ENDSTOP(es1) | (TEST_ENDSTOP(es2) << 1); // bit 0 for Y, bit 1 for Y2
  279. if (y_test && stepper.current_block->steps[Y_AXIS] > 0) {
  280. SBI(endstop_hit_bits, Y_MIN);
  281. if (!stepper.performing_homing || (y_test == 0x3)) //if not performing home or if both endstops were trigged during homing...
  282. stepper.kill_current_block();
  283. }
  284. }
  285. #endif
  286. #if ENABLED(Z_DUAL_ENDSTOPS)
  287. void Endstops::test_dual_z_endstops(const EndstopEnum es1, const EndstopEnum es2) {
  288. const byte z_test = TEST_ENDSTOP(es1) | (TEST_ENDSTOP(es2) << 1); // bit 0 for Z, bit 1 for Z2
  289. if (z_test && stepper.current_block->steps[Z_AXIS] > 0) {
  290. SBI(endstop_hit_bits, Z_MIN);
  291. if (!stepper.performing_homing || (z_test == 0x3)) //if not performing home or if both endstops were trigged during homing...
  292. stepper.kill_current_block();
  293. }
  294. }
  295. #endif
  296. // Check endstops - Called from ISR!
  297. void Endstops::update() {
  298. #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
  299. #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
  300. #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
  301. #define _ENDSTOP_HIT(AXIS, MINMAX) SBI(endstop_hit_bits, _ENDSTOP(AXIS, MINMAX))
  302. #define SET_BIT(N,B,TF) do{ if (TF) SBI(N,B); else CBI(N,B); }while(0)
  303. // UPDATE_ENDSTOP_BIT: set the current endstop bits for an endstop to its status
  304. #define UPDATE_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT(current_endstop_bits, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
  305. // COPY_BIT: copy the value of SRC_BIT to DST_BIT in DST
  306. #define COPY_BIT(DST, SRC_BIT, DST_BIT) SET_BIT(DST, DST_BIT, TEST(DST, SRC_BIT))
  307. #define UPDATE_ENDSTOP(AXIS,MINMAX) do { \
  308. UPDATE_ENDSTOP_BIT(AXIS, MINMAX); \
  309. if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX))) { \
  310. _ENDSTOP_HIT(AXIS, MINMAX); \
  311. stepper.endstop_triggered(_AXIS(AXIS)); \
  312. } \
  313. }while(0)
  314. #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && !(CORE_IS_XY || CORE_IS_XZ)
  315. // If G38 command is active check Z_MIN_PROBE for ALL movement
  316. if (G38_move) {
  317. UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
  318. if (TEST_ENDSTOP(_ENDSTOP(Z, MIN_PROBE))) {
  319. if (stepper.current_block->steps[_AXIS(X)] > 0) { _ENDSTOP_HIT(X, MIN); stepper.endstop_triggered(_AXIS(X)); }
  320. else if (stepper.current_block->steps[_AXIS(Y)] > 0) { _ENDSTOP_HIT(Y, MIN); stepper.endstop_triggered(_AXIS(Y)); }
  321. else if (stepper.current_block->steps[_AXIS(Z)] > 0) { _ENDSTOP_HIT(Z, MIN); stepper.endstop_triggered(_AXIS(Z)); }
  322. G38_endstop_hit = true;
  323. }
  324. }
  325. #endif
  326. /**
  327. * Define conditions for checking endstops
  328. */
  329. #if IS_CORE
  330. #define S_(N) stepper.current_block->steps[CORE_AXIS_##N]
  331. #define D_(N) stepper.motor_direction(CORE_AXIS_##N)
  332. #endif
  333. #if CORE_IS_XY || CORE_IS_XZ
  334. /**
  335. * Head direction in -X axis for CoreXY and CoreXZ bots.
  336. *
  337. * If steps differ, both axes are moving.
  338. * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Y or Z, handled below)
  339. * If DeltaA == DeltaB, the movement is only in the 1st axis (X)
  340. */
  341. #if ENABLED(COREXY) || ENABLED(COREXZ)
  342. #define X_CMP ==
  343. #else
  344. #define X_CMP !=
  345. #endif
  346. #define X_MOVE_TEST ( S_(1) != S_(2) || (S_(1) > 0 && D_(1) X_CMP D_(2)) )
  347. #define X_AXIS_HEAD X_HEAD
  348. #else
  349. #define X_MOVE_TEST stepper.current_block->steps[X_AXIS] > 0
  350. #define X_AXIS_HEAD X_AXIS
  351. #endif
  352. #if CORE_IS_XY || CORE_IS_YZ
  353. /**
  354. * Head direction in -Y axis for CoreXY / CoreYZ bots.
  355. *
  356. * If steps differ, both axes are moving
  357. * If DeltaA == DeltaB, the movement is only in the 1st axis (X or Y)
  358. * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Y or Z)
  359. */
  360. #if ENABLED(COREYX) || ENABLED(COREYZ)
  361. #define Y_CMP ==
  362. #else
  363. #define Y_CMP !=
  364. #endif
  365. #define Y_MOVE_TEST ( S_(1) != S_(2) || (S_(1) > 0 && D_(1) Y_CMP D_(2)) )
  366. #define Y_AXIS_HEAD Y_HEAD
  367. #else
  368. #define Y_MOVE_TEST stepper.current_block->steps[Y_AXIS] > 0
  369. #define Y_AXIS_HEAD Y_AXIS
  370. #endif
  371. #if CORE_IS_XZ || CORE_IS_YZ
  372. /**
  373. * Head direction in -Z axis for CoreXZ or CoreYZ bots.
  374. *
  375. * If steps differ, both axes are moving
  376. * If DeltaA == DeltaB, the movement is only in the 1st axis (X or Y, already handled above)
  377. * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Z)
  378. */
  379. #if ENABLED(COREZX) || ENABLED(COREZY)
  380. #define Z_CMP ==
  381. #else
  382. #define Z_CMP !=
  383. #endif
  384. #define Z_MOVE_TEST ( S_(1) != S_(2) || (S_(1) > 0 && D_(1) Z_CMP D_(2)) )
  385. #define Z_AXIS_HEAD Z_HEAD
  386. #else
  387. #define Z_MOVE_TEST stepper.current_block->steps[Z_AXIS] > 0
  388. #define Z_AXIS_HEAD Z_AXIS
  389. #endif
  390. // With Dual X, endstops are only checked in the homing direction for the active extruder
  391. #if ENABLED(DUAL_X_CARRIAGE)
  392. #define E0_ACTIVE stepper.current_block->active_extruder == 0
  393. #define X_MIN_TEST ((X_HOME_DIR < 0 && E0_ACTIVE) || (X2_HOME_DIR < 0 && !E0_ACTIVE))
  394. #define X_MAX_TEST ((X_HOME_DIR > 0 && E0_ACTIVE) || (X2_HOME_DIR > 0 && !E0_ACTIVE))
  395. #else
  396. #define X_MIN_TEST true
  397. #define X_MAX_TEST true
  398. #endif
  399. /**
  400. * Check and update endstops according to conditions
  401. */
  402. if (stepper.current_block) {
  403. if (X_MOVE_TEST) {
  404. if (stepper.motor_direction(X_AXIS_HEAD)) { // -direction
  405. #if HAS_X_MIN
  406. #if ENABLED(X_DUAL_ENDSTOPS)
  407. UPDATE_ENDSTOP_BIT(X, MIN);
  408. #if HAS_X2_MIN
  409. UPDATE_ENDSTOP_BIT(X2, MIN);
  410. #else
  411. COPY_BIT(current_endstop_bits, X_MIN, X2_MIN);
  412. #endif
  413. test_dual_x_endstops(X_MIN, X2_MIN);
  414. #else
  415. if (X_MIN_TEST) UPDATE_ENDSTOP(X, MIN);
  416. #endif
  417. #endif
  418. }
  419. else { // +direction
  420. #if HAS_X_MAX
  421. #if ENABLED(X_DUAL_ENDSTOPS)
  422. UPDATE_ENDSTOP_BIT(X, MAX);
  423. #if HAS_X2_MAX
  424. UPDATE_ENDSTOP_BIT(X2, MAX);
  425. #else
  426. COPY_BIT(current_endstop_bits, X_MAX, X2_MAX);
  427. #endif
  428. test_dual_x_endstops(X_MAX, X2_MAX);
  429. #else
  430. if (X_MAX_TEST) UPDATE_ENDSTOP(X, MAX);
  431. #endif
  432. #endif
  433. }
  434. }
  435. if (Y_MOVE_TEST) {
  436. if (stepper.motor_direction(Y_AXIS_HEAD)) { // -direction
  437. #if HAS_Y_MIN
  438. #if ENABLED(Y_DUAL_ENDSTOPS)
  439. UPDATE_ENDSTOP_BIT(Y, MIN);
  440. #if HAS_Y2_MIN
  441. UPDATE_ENDSTOP_BIT(Y2, MIN);
  442. #else
  443. COPY_BIT(current_endstop_bits, Y_MIN, Y2_MIN);
  444. #endif
  445. test_dual_y_endstops(Y_MIN, Y2_MIN);
  446. #else
  447. UPDATE_ENDSTOP(Y, MIN);
  448. #endif
  449. #endif
  450. }
  451. else { // +direction
  452. #if HAS_Y_MAX
  453. #if ENABLED(Y_DUAL_ENDSTOPS)
  454. UPDATE_ENDSTOP_BIT(Y, MAX);
  455. #if HAS_Y2_MAX
  456. UPDATE_ENDSTOP_BIT(Y2, MAX);
  457. #else
  458. COPY_BIT(current_endstop_bits, Y_MAX, Y2_MAX);
  459. #endif
  460. test_dual_y_endstops(Y_MAX, Y2_MAX);
  461. #else
  462. UPDATE_ENDSTOP(Y, MAX);
  463. #endif
  464. #endif
  465. }
  466. }
  467. if (Z_MOVE_TEST) {
  468. if (stepper.motor_direction(Z_AXIS_HEAD)) { // Z -direction. Gantry down, bed up.
  469. #if HAS_Z_MIN
  470. #if ENABLED(Z_DUAL_ENDSTOPS)
  471. UPDATE_ENDSTOP_BIT(Z, MIN);
  472. #if HAS_Z2_MIN
  473. UPDATE_ENDSTOP_BIT(Z2, MIN);
  474. #else
  475. COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN);
  476. #endif
  477. test_dual_z_endstops(Z_MIN, Z2_MIN);
  478. #else
  479. #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  480. if (z_probe_enabled) UPDATE_ENDSTOP(Z, MIN);
  481. #else
  482. UPDATE_ENDSTOP(Z, MIN);
  483. #endif
  484. #endif
  485. #endif
  486. // When closing the gap check the enabled probe
  487. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  488. if (z_probe_enabled) {
  489. UPDATE_ENDSTOP(Z, MIN_PROBE);
  490. if (TEST_ENDSTOP(Z_MIN_PROBE)) SBI(endstop_hit_bits, Z_MIN_PROBE);
  491. }
  492. #endif
  493. }
  494. else { // Z +direction. Gantry up, bed down.
  495. #if HAS_Z_MAX
  496. // Check both Z dual endstops
  497. #if ENABLED(Z_DUAL_ENDSTOPS)
  498. UPDATE_ENDSTOP_BIT(Z, MAX);
  499. #if HAS_Z2_MAX
  500. UPDATE_ENDSTOP_BIT(Z2, MAX);
  501. #else
  502. COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX);
  503. #endif
  504. test_dual_z_endstops(Z_MAX, Z2_MAX);
  505. // If this pin is not hijacked for the bed probe
  506. // then it belongs to the Z endstop
  507. #elif DISABLED(Z_MIN_PROBE_ENDSTOP) || Z_MAX_PIN != Z_MIN_PROBE_PIN
  508. UPDATE_ENDSTOP(Z, MAX);
  509. #endif
  510. #endif
  511. }
  512. }
  513. } // stepper.current_block
  514. old_endstop_bits = current_endstop_bits;
  515. } // Endstops::update()
  516. #if ENABLED(PINS_DEBUGGING)
  517. bool Endstops::monitor_flag = false;
  518. /**
  519. * monitors endstops & Z probe for changes
  520. *
  521. * If a change is detected then the LED is toggled and
  522. * a message is sent out the serial port
  523. *
  524. * Yes, we could miss a rapid back & forth change but
  525. * that won't matter because this is all manual.
  526. *
  527. */
  528. void Endstops::monitor() {
  529. static uint16_t old_endstop_bits_local = 0;
  530. static uint8_t local_LED_status = 0;
  531. uint16_t current_endstop_bits_local = 0;
  532. #if HAS_X_MIN
  533. if (READ(X_MIN_PIN)) SBI(current_endstop_bits_local, X_MIN);
  534. #endif
  535. #if HAS_X_MAX
  536. if (READ(X_MAX_PIN)) SBI(current_endstop_bits_local, X_MAX);
  537. #endif
  538. #if HAS_Y_MIN
  539. if (READ(Y_MIN_PIN)) SBI(current_endstop_bits_local, Y_MIN);
  540. #endif
  541. #if HAS_Y_MAX
  542. if (READ(Y_MAX_PIN)) SBI(current_endstop_bits_local, Y_MAX);
  543. #endif
  544. #if HAS_Z_MIN
  545. if (READ(Z_MIN_PIN)) SBI(current_endstop_bits_local, Z_MIN);
  546. #endif
  547. #if HAS_Z_MAX
  548. if (READ(Z_MAX_PIN)) SBI(current_endstop_bits_local, Z_MAX);
  549. #endif
  550. #if HAS_Z_MIN_PROBE_PIN
  551. if (READ(Z_MIN_PROBE_PIN)) SBI(current_endstop_bits_local, Z_MIN_PROBE);
  552. #endif
  553. #if HAS_X2_MIN
  554. if (READ(X2_MIN_PIN)) SBI(current_endstop_bits_local, X2_MIN);
  555. #endif
  556. #if HAS_X2_MAX
  557. if (READ(X2_MAX_PIN)) SBI(current_endstop_bits_local, X2_MAX);
  558. #endif
  559. #if HAS_Y2_MIN
  560. if (READ(Y2_MIN_PIN)) SBI(current_endstop_bits_local, Y2_MIN);
  561. #endif
  562. #if HAS_Y2_MAX
  563. if (READ(Y2_MAX_PIN)) SBI(current_endstop_bits_local, Y2_MAX);
  564. #endif
  565. #if HAS_Z2_MIN
  566. if (READ(Z2_MIN_PIN)) SBI(current_endstop_bits_local, Z2_MIN);
  567. #endif
  568. #if HAS_Z2_MAX
  569. if (READ(Z2_MAX_PIN)) SBI(current_endstop_bits_local, Z2_MAX);
  570. #endif
  571. uint16_t endstop_change = current_endstop_bits_local ^ old_endstop_bits_local;
  572. if (endstop_change) {
  573. #if HAS_X_MIN
  574. if (TEST(endstop_change, X_MIN)) SERIAL_PROTOCOLPAIR(" X_MIN:", TEST(current_endstop_bits_local, X_MIN));
  575. #endif
  576. #if HAS_X_MAX
  577. if (TEST(endstop_change, X_MAX)) SERIAL_PROTOCOLPAIR(" X_MAX:", TEST(current_endstop_bits_local, X_MAX));
  578. #endif
  579. #if HAS_Y_MIN
  580. if (TEST(endstop_change, Y_MIN)) SERIAL_PROTOCOLPAIR(" Y_MIN:", TEST(current_endstop_bits_local, Y_MIN));
  581. #endif
  582. #if HAS_Y_MAX
  583. if (TEST(endstop_change, Y_MAX)) SERIAL_PROTOCOLPAIR(" Y_MAX:", TEST(current_endstop_bits_local, Y_MAX));
  584. #endif
  585. #if HAS_Z_MIN
  586. if (TEST(endstop_change, Z_MIN)) SERIAL_PROTOCOLPAIR(" Z_MIN:", TEST(current_endstop_bits_local, Z_MIN));
  587. #endif
  588. #if HAS_Z_MAX
  589. if (TEST(endstop_change, Z_MAX)) SERIAL_PROTOCOLPAIR(" Z_MAX:", TEST(current_endstop_bits_local, Z_MAX));
  590. #endif
  591. #if HAS_Z_MIN_PROBE_PIN
  592. if (TEST(endstop_change, Z_MIN_PROBE)) SERIAL_PROTOCOLPAIR(" PROBE:", TEST(current_endstop_bits_local, Z_MIN_PROBE));
  593. #endif
  594. #if HAS_X2_MIN
  595. if (TEST(endstop_change, X2_MIN)) SERIAL_PROTOCOLPAIR(" X2_MIN:", TEST(current_endstop_bits_local, X2_MIN));
  596. #endif
  597. #if HAS_X2_MAX
  598. if (TEST(endstop_change, X2_MAX)) SERIAL_PROTOCOLPAIR(" X2_MAX:", TEST(current_endstop_bits_local, X2_MAX));
  599. #endif
  600. #if HAS_Y2_MIN
  601. if (TEST(endstop_change, Y2_MIN)) SERIAL_PROTOCOLPAIR(" Y2_MIN:", TEST(current_endstop_bits_local, Y2_MIN));
  602. #endif
  603. #if HAS_Y2_MAX
  604. if (TEST(endstop_change, Y2_MAX)) SERIAL_PROTOCOLPAIR(" Y2_MAX:", TEST(current_endstop_bits_local, Y2_MAX));
  605. #endif
  606. #if HAS_Z2_MIN
  607. if (TEST(endstop_change, Z2_MIN)) SERIAL_PROTOCOLPAIR(" Z2_MIN:", TEST(current_endstop_bits_local, Z2_MIN));
  608. #endif
  609. #if HAS_Z2_MAX
  610. if (TEST(endstop_change, Z2_MAX)) SERIAL_PROTOCOLPAIR(" Z2_MAX:", TEST(current_endstop_bits_local, Z2_MAX));
  611. #endif
  612. SERIAL_PROTOCOLPGM("\n\n");
  613. analogWrite(LED_PIN, local_LED_status);
  614. local_LED_status ^= 255;
  615. old_endstop_bits_local = current_endstop_bits_local;
  616. }
  617. }
  618. #endif // PINS_DEBUGGING