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

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