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.

M43.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../inc/MarlinConfig.h"
  23. #if ENABLED(PINS_DEBUGGING)
  24. #include "../gcode.h"
  25. #include "../../MarlinCore.h" // for pin_is_protected
  26. #include "../../pins/pinsDebug.h"
  27. #include "../../module/endstops.h"
  28. #if HAS_Z_SERVO_PROBE
  29. #include "../../module/probe.h"
  30. #include "../../module/servo.h"
  31. #endif
  32. #if ENABLED(BLTOUCH)
  33. #include "../../feature/bltouch.h"
  34. #endif
  35. #if ENABLED(HOST_PROMPT_SUPPORT)
  36. #include "../../feature/host_actions.h"
  37. #endif
  38. #if ENABLED(EXTENSIBLE_UI)
  39. #include "../../lcd/extui/ui_api.h"
  40. #endif
  41. #ifndef GET_PIN_MAP_PIN_M43
  42. #define GET_PIN_MAP_PIN_M43(Q) GET_PIN_MAP_PIN(Q)
  43. #endif
  44. inline void toggle_pins() {
  45. const bool ignore_protection = parser.boolval('I');
  46. const int repeat = parser.intval('R', 1),
  47. start = PARSED_PIN_INDEX('S', 0),
  48. end = PARSED_PIN_INDEX('L', NUM_DIGITAL_PINS - 1),
  49. wait = parser.intval('W', 500);
  50. LOOP_S_LE_N(i, start, end) {
  51. pin_t pin = GET_PIN_MAP_PIN_M43(i);
  52. if (!VALID_PIN(pin)) continue;
  53. if (M43_NEVER_TOUCH(i) || (!ignore_protection && pin_is_protected(pin))) {
  54. report_pin_state_extended(pin, ignore_protection, true, PSTR("Untouched "));
  55. SERIAL_EOL();
  56. }
  57. else {
  58. watchdog_refresh();
  59. report_pin_state_extended(pin, ignore_protection, true, PSTR("Pulsing "));
  60. #if AVR_AT90USB1286_FAMILY // Teensy IDEs don't know about these pins so must use FASTIO
  61. if (pin == TEENSY_E2) {
  62. SET_OUTPUT(TEENSY_E2);
  63. for (int16_t j = 0; j < repeat; j++) {
  64. WRITE(TEENSY_E2, LOW); safe_delay(wait);
  65. WRITE(TEENSY_E2, HIGH); safe_delay(wait);
  66. WRITE(TEENSY_E2, LOW); safe_delay(wait);
  67. }
  68. }
  69. else if (pin == TEENSY_E3) {
  70. SET_OUTPUT(TEENSY_E3);
  71. for (int16_t j = 0; j < repeat; j++) {
  72. WRITE(TEENSY_E3, LOW); safe_delay(wait);
  73. WRITE(TEENSY_E3, HIGH); safe_delay(wait);
  74. WRITE(TEENSY_E3, LOW); safe_delay(wait);
  75. }
  76. }
  77. else
  78. #endif
  79. {
  80. pinMode(pin, OUTPUT);
  81. for (int16_t j = 0; j < repeat; j++) {
  82. watchdog_refresh(); extDigitalWrite(pin, 0); safe_delay(wait);
  83. watchdog_refresh(); extDigitalWrite(pin, 1); safe_delay(wait);
  84. watchdog_refresh(); extDigitalWrite(pin, 0); safe_delay(wait);
  85. watchdog_refresh();
  86. }
  87. }
  88. }
  89. SERIAL_EOL();
  90. }
  91. SERIAL_ECHOLNPGM("Done.");
  92. } // toggle_pins
  93. inline void servo_probe_test() {
  94. #if !(NUM_SERVOS > 0 && HAS_SERVO_0)
  95. SERIAL_ERROR_MSG("SERVO not set up.");
  96. #elif !HAS_Z_SERVO_PROBE
  97. SERIAL_ERROR_MSG("Z_PROBE_SERVO_NR not set up.");
  98. #else // HAS_Z_SERVO_PROBE
  99. const uint8_t probe_index = parser.byteval('P', Z_PROBE_SERVO_NR);
  100. SERIAL_ECHOLNPAIR("Servo probe test\n"
  101. ". using index: ", int(probe_index),
  102. ", deploy angle: ", servo_angles[probe_index][0],
  103. ", stow angle: ", servo_angles[probe_index][1]
  104. );
  105. bool deploy_state = false, stow_state;
  106. #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  107. #define PROBE_TEST_PIN Z_MIN_PIN
  108. constexpr bool probe_inverting = Z_MIN_ENDSTOP_INVERTING;
  109. SERIAL_ECHOLNPAIR(". Probe Z_MIN_PIN: ", int(PROBE_TEST_PIN));
  110. SERIAL_ECHOPGM(". Z_MIN_ENDSTOP_INVERTING: ");
  111. #else
  112. #define PROBE_TEST_PIN Z_MIN_PROBE_PIN
  113. constexpr bool probe_inverting = Z_MIN_PROBE_ENDSTOP_INVERTING;
  114. SERIAL_ECHOLNPAIR(". Probe Z_MIN_PROBE_PIN: ", int(PROBE_TEST_PIN));
  115. SERIAL_ECHOPGM( ". Z_MIN_PROBE_ENDSTOP_INVERTING: ");
  116. #endif
  117. serialprint_truefalse(probe_inverting);
  118. SERIAL_EOL();
  119. SET_INPUT_PULLUP(PROBE_TEST_PIN);
  120. // First, check for a probe that recognizes an advanced BLTouch sequence.
  121. // In addition to STOW and DEPLOY, it uses SW MODE (and RESET in the beginning)
  122. // to see if this is one of the following: BLTOUCH Classic 1.2, 1.3, or
  123. // BLTouch Smart 1.0, 2.0, 2.2, 3.0, 3.1. But only if the user has actually
  124. // configured a BLTouch as being present. If the user has not configured this,
  125. // the BLTouch will be detected in the last phase of these tests (see further on).
  126. bool blt = false;
  127. // This code will try to detect a BLTouch probe or clone
  128. #if ENABLED(BLTOUCH)
  129. SERIAL_ECHOLNPGM(". Check for BLTOUCH");
  130. bltouch._reset();
  131. bltouch._stow();
  132. if (probe_inverting == READ(PROBE_TEST_PIN)) {
  133. bltouch._set_SW_mode();
  134. if (probe_inverting != READ(PROBE_TEST_PIN)) {
  135. bltouch._deploy();
  136. if (probe_inverting == READ(PROBE_TEST_PIN)) {
  137. bltouch._stow();
  138. SERIAL_ECHOLNPGM("= BLTouch Classic 1.2, 1.3, Smart 1.0, 2.0, 2.2, 3.0, 3.1 detected.");
  139. // Check for a 3.1 by letting the user trigger it, later
  140. blt = true;
  141. }
  142. }
  143. }
  144. #endif
  145. // The following code is common to all kinds of servo probes.
  146. // Since it could be a real servo or a BLTouch (any kind) or a clone,
  147. // use only "common" functions - i.e. SERVO_MOVE. No bltouch.xxxx stuff.
  148. // If it is already recognised as a being a BLTouch, no need for this test
  149. if (!blt) {
  150. // DEPLOY and STOW 4 times and see if the signal follows
  151. // Then it is a mechanical switch
  152. uint8_t i = 0;
  153. SERIAL_ECHOLNPGM(". Deploy & stow 4 times");
  154. do {
  155. MOVE_SERVO(probe_index, servo_angles[Z_PROBE_SERVO_NR][0]); // Deploy
  156. safe_delay(500);
  157. deploy_state = READ(PROBE_TEST_PIN);
  158. MOVE_SERVO(probe_index, servo_angles[Z_PROBE_SERVO_NR][1]); // Stow
  159. safe_delay(500);
  160. stow_state = READ(PROBE_TEST_PIN);
  161. } while (++i < 4);
  162. if (probe_inverting != deploy_state) SERIAL_ECHOLNPGM("WARNING: INVERTING setting probably backwards.");
  163. if (deploy_state != stow_state) {
  164. SERIAL_ECHOLNPGM("= Mechanical Switch detected");
  165. if (deploy_state) {
  166. SERIAL_ECHOLNPAIR(" DEPLOYED state: HIGH (logic 1)",
  167. " STOWED (triggered) state: LOW (logic 0)");
  168. }
  169. else {
  170. SERIAL_ECHOLNPAIR(" DEPLOYED state: LOW (logic 0)",
  171. " STOWED (triggered) state: HIGH (logic 1)");
  172. }
  173. #if ENABLED(BLTOUCH)
  174. SERIAL_ECHOLNPGM("FAIL: BLTOUCH enabled - Set up this device as a Servo Probe with INVERTING set to 'true'.");
  175. #endif
  176. return;
  177. }
  178. }
  179. // Ask the user for a trigger event and measure the pulse width.
  180. MOVE_SERVO(probe_index, servo_angles[Z_PROBE_SERVO_NR][0]); // Deploy
  181. safe_delay(500);
  182. SERIAL_ECHOLNPGM("** Please trigger probe within 30 sec **");
  183. uint16_t probe_counter = 0;
  184. // Wait 30 seconds for user to trigger probe
  185. for (uint16_t j = 0; j < 500 * 30 && probe_counter == 0 ; j++) {
  186. safe_delay(2);
  187. if (0 == j % (500 * 1)) gcode.reset_stepper_timeout(); // Keep steppers powered
  188. if (deploy_state != READ(PROBE_TEST_PIN)) { // probe triggered
  189. for (probe_counter = 0; probe_counter < 15 && deploy_state != READ(PROBE_TEST_PIN); ++probe_counter) safe_delay(2);
  190. SERIAL_ECHOPGM(". Pulse width");
  191. if (probe_counter == 15)
  192. SERIAL_ECHOLNPGM(": 30ms or more");
  193. else
  194. SERIAL_ECHOLNPAIR(" (+/- 4ms): ", probe_counter * 2);
  195. if (probe_counter >= 4) {
  196. if (probe_counter == 15) {
  197. if (blt) SERIAL_ECHOPGM("= BLTouch V3.1");
  198. else SERIAL_ECHOPGM("= Z Servo Probe");
  199. }
  200. else SERIAL_ECHOPGM("= BLTouch pre V3.1 (or compatible)");
  201. SERIAL_ECHOLNPGM(" detected.");
  202. }
  203. else SERIAL_ECHOLNPGM("FAIL: Noise detected - please re-run test");
  204. MOVE_SERVO(probe_index, servo_angles[Z_PROBE_SERVO_NR][1]); // Stow
  205. return;
  206. }
  207. }
  208. if (!probe_counter) SERIAL_ECHOLNPGM("FAIL: No trigger detected");
  209. #endif // HAS_Z_SERVO_PROBE
  210. } // servo_probe_test
  211. /**
  212. * M43: Pin debug - report pin state, watch pins, toggle pins and servo probe test/report
  213. *
  214. * M43 - report name and state of pin(s)
  215. * P<pin> Pin to read or watch. If omitted, reads all pins.
  216. * I Flag to ignore Marlin's pin protection.
  217. *
  218. * M43 W - Watch pins -reporting changes- until reset, click, or M108.
  219. * P<pin> Pin to read or watch. If omitted, read/watch all pins.
  220. * I Flag to ignore Marlin's pin protection.
  221. *
  222. * M43 E<bool> - Enable / disable background endstop monitoring
  223. * - Machine continues to operate
  224. * - Reports changes to endstops
  225. * - Toggles LED_PIN when an endstop changes
  226. * - Cannot reliably catch the 5mS pulse from BLTouch type probes
  227. *
  228. * M43 T - Toggle pin(s) and report which pin is being toggled
  229. * S<pin> - Start Pin number. If not given, will default to 0
  230. * L<pin> - End Pin number. If not given, will default to last pin defined for this board
  231. * I<bool> - Flag to ignore Marlin's pin protection. Use with caution!!!!
  232. * R - Repeat pulses on each pin this number of times before continueing to next pin
  233. * W - Wait time (in miliseconds) between pulses. If not given will default to 500
  234. *
  235. * M43 S - Servo probe test
  236. * P<index> - Probe index (optional - defaults to 0
  237. */
  238. void GcodeSuite::M43() {
  239. // 'T' must be first. It uses 'S' and 'E' differently.
  240. if (parser.seen('T')) return toggle_pins();
  241. // 'E' Enable or disable endstop monitoring and return
  242. if (parser.seen('E')) {
  243. endstops.monitor_flag = parser.value_bool();
  244. SERIAL_ECHOPGM("endstop monitor ");
  245. serialprintPGM(endstops.monitor_flag ? PSTR("en") : PSTR("dis"));
  246. SERIAL_ECHOLNPGM("abled");
  247. return;
  248. }
  249. // 'S' Run servo probe test and return
  250. if (parser.seen('S')) return servo_probe_test();
  251. // 'P' Get the range of pins to test or watch
  252. uint8_t first_pin = PARSED_PIN_INDEX('P', 0),
  253. last_pin = parser.seenval('P') ? first_pin : NUMBER_PINS_TOTAL - 1;
  254. if (first_pin > last_pin) return;
  255. // 'I' to ignore protected pins
  256. const bool ignore_protection = parser.boolval('I');
  257. // 'W' Watch until click, M108, or reset
  258. if (parser.boolval('W')) {
  259. SERIAL_ECHOLNPGM("Watching pins");
  260. #ifdef ARDUINO_ARCH_SAM
  261. NOLESS(first_pin, 2); // Don't hijack the UART pins
  262. #endif
  263. uint8_t pin_state[last_pin - first_pin + 1];
  264. LOOP_S_LE_N(i, first_pin, last_pin) {
  265. pin_t pin = GET_PIN_MAP_PIN_M43(i);
  266. if (!VALID_PIN(pin)) continue;
  267. if (M43_NEVER_TOUCH(i) || (!ignore_protection && pin_is_protected(pin))) continue;
  268. pinMode(pin, INPUT_PULLUP);
  269. delay(1);
  270. /*
  271. if (IS_ANALOG(pin))
  272. pin_state[pin - first_pin] = analogRead(DIGITAL_PIN_TO_ANALOG_PIN(pin)); // int16_t pin_state[...]
  273. else
  274. //*/
  275. pin_state[i - first_pin] = extDigitalRead(pin);
  276. }
  277. #if HAS_RESUME_CONTINUE
  278. KEEPALIVE_STATE(PAUSED_FOR_USER);
  279. wait_for_user = true;
  280. #if ENABLED(HOST_PROMPT_SUPPORT)
  281. host_prompt_do(PROMPT_USER_CONTINUE, PSTR("M43 Wait Called"), CONTINUE_STR);
  282. #endif
  283. #if ENABLED(EXTENSIBLE_UI)
  284. ExtUI::onUserConfirmRequired_P(PSTR("M43 Wait Called"));
  285. #endif
  286. #endif
  287. for (;;) {
  288. LOOP_S_LE_N(i, first_pin, last_pin) {
  289. pin_t pin = GET_PIN_MAP_PIN_M43(i);
  290. if (!VALID_PIN(pin)) continue;
  291. if (M43_NEVER_TOUCH(i) || (!ignore_protection && pin_is_protected(pin))) continue;
  292. const byte val =
  293. /*
  294. IS_ANALOG(pin)
  295. ? analogRead(DIGITAL_PIN_TO_ANALOG_PIN(pin)) : // int16_t val
  296. :
  297. //*/
  298. extDigitalRead(pin);
  299. if (val != pin_state[i - first_pin]) {
  300. report_pin_state_extended(pin, ignore_protection, false);
  301. pin_state[i - first_pin] = val;
  302. }
  303. }
  304. #if HAS_RESUME_CONTINUE
  305. if (!wait_for_user) break;
  306. #endif
  307. safe_delay(200);
  308. }
  309. }
  310. else {
  311. // Report current state of selected pin(s)
  312. LOOP_S_LE_N(i, first_pin, last_pin) {
  313. pin_t pin = GET_PIN_MAP_PIN_M43(i);
  314. if (VALID_PIN(pin)) report_pin_state_extended(pin, ignore_protection, true);
  315. }
  316. }
  317. }
  318. #endif // PINS_DEBUGGING