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

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