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

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