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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 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 "../../Marlin.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(HOST_PROMPT_SUPPORT)
  33. #include "../../feature/host_actions.h"
  34. #endif
  35. inline void toggle_pins() {
  36. const bool ignore_protection = parser.boolval('I');
  37. const int repeat = parser.intval('R', 1),
  38. start = PARSED_PIN_INDEX('S', 0),
  39. end = PARSED_PIN_INDEX('L', NUM_DIGITAL_PINS - 1),
  40. wait = parser.intval('W', 500);
  41. for (uint8_t i = start; i <= end; i++) {
  42. pin_t pin = GET_PIN_MAP_PIN(i);
  43. if (!VALID_PIN(pin)) continue;
  44. if (M43_NEVER_TOUCH(i) || (!ignore_protection && pin_is_protected(pin))) {
  45. report_pin_state_extended(pin, ignore_protection, true, "Untouched ");
  46. SERIAL_EOL();
  47. }
  48. else {
  49. watchdog_reset();
  50. report_pin_state_extended(pin, ignore_protection, true, "Pulsing ");
  51. #if AVR_AT90USB1286_FAMILY // Teensy IDEs don't know about these pins so must use FASTIO
  52. if (pin == TEENSY_E2) {
  53. SET_OUTPUT(TEENSY_E2);
  54. for (int16_t j = 0; j < repeat; j++) {
  55. WRITE(TEENSY_E2, LOW); safe_delay(wait);
  56. WRITE(TEENSY_E2, HIGH); safe_delay(wait);
  57. WRITE(TEENSY_E2, LOW); safe_delay(wait);
  58. }
  59. }
  60. else if (pin == TEENSY_E3) {
  61. SET_OUTPUT(TEENSY_E3);
  62. for (int16_t j = 0; j < repeat; j++) {
  63. WRITE(TEENSY_E3, LOW); safe_delay(wait);
  64. WRITE(TEENSY_E3, HIGH); safe_delay(wait);
  65. WRITE(TEENSY_E3, LOW); safe_delay(wait);
  66. }
  67. }
  68. else
  69. #endif
  70. {
  71. pinMode(pin, OUTPUT);
  72. for (int16_t j = 0; j < repeat; j++) {
  73. watchdog_reset(); extDigitalWrite(pin, 0); safe_delay(wait);
  74. watchdog_reset(); extDigitalWrite(pin, 1); safe_delay(wait);
  75. watchdog_reset(); extDigitalWrite(pin, 0); safe_delay(wait);
  76. watchdog_reset();
  77. }
  78. }
  79. }
  80. SERIAL_EOL();
  81. }
  82. SERIAL_ECHOLNPGM("Done.");
  83. } // toggle_pins
  84. inline void servo_probe_test() {
  85. #if !(NUM_SERVOS > 0 && HAS_SERVO_0)
  86. SERIAL_ERROR_MSG("SERVO not setup");
  87. #elif !HAS_Z_SERVO_PROBE
  88. SERIAL_ERROR_MSG("Z_PROBE_SERVO_NR not setup");
  89. #else // HAS_Z_SERVO_PROBE
  90. const uint8_t probe_index = parser.byteval('P', Z_PROBE_SERVO_NR);
  91. SERIAL_ECHOLNPGM("Servo probe test");
  92. SERIAL_ECHOLNPAIR(". using index: ", probe_index);
  93. SERIAL_ECHOLNPAIR(". deploy angle: ", servo_angles[probe_index][0]);
  94. SERIAL_ECHOLNPAIR(". stow angle: ", servo_angles[probe_index][1]);
  95. bool probe_inverting;
  96. #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  97. #define PROBE_TEST_PIN Z_MIN_PIN
  98. SERIAL_ECHOLNPAIR(". probe uses Z_MIN pin: ", PROBE_TEST_PIN);
  99. SERIAL_ECHOLNPGM(". uses Z_MIN_ENDSTOP_INVERTING (ignores Z_MIN_PROBE_ENDSTOP_INVERTING)");
  100. SERIAL_ECHOPGM(". Z_MIN_ENDSTOP_INVERTING: ");
  101. #if Z_MIN_ENDSTOP_INVERTING
  102. SERIAL_ECHOLNPGM("true");
  103. #else
  104. SERIAL_ECHOLNPGM("false");
  105. #endif
  106. probe_inverting = Z_MIN_ENDSTOP_INVERTING;
  107. #elif USES_Z_MIN_PROBE_ENDSTOP
  108. #define PROBE_TEST_PIN Z_MIN_PROBE_PIN
  109. SERIAL_ECHOLNPAIR(". probe uses Z_MIN_PROBE_PIN: ", PROBE_TEST_PIN);
  110. SERIAL_ECHOLNPGM(". uses Z_MIN_PROBE_ENDSTOP_INVERTING (ignores Z_MIN_ENDSTOP_INVERTING)");
  111. SERIAL_ECHOPGM(". Z_MIN_PROBE_ENDSTOP_INVERTING: ");
  112. #if Z_MIN_PROBE_ENDSTOP_INVERTING
  113. SERIAL_ECHOLNPGM("true");
  114. #else
  115. SERIAL_ECHOLNPGM("false");
  116. #endif
  117. probe_inverting = Z_MIN_PROBE_ENDSTOP_INVERTING;
  118. #endif
  119. SERIAL_ECHOLNPGM(". deploy & stow 4 times");
  120. SET_INPUT_PULLUP(PROBE_TEST_PIN);
  121. uint8_t i = 0;
  122. bool deploy_state, stow_state;
  123. do {
  124. MOVE_SERVO(probe_index, servo_angles[Z_PROBE_SERVO_NR][0]); // Deploy
  125. safe_delay(500);
  126. deploy_state = READ(PROBE_TEST_PIN);
  127. MOVE_SERVO(probe_index, servo_angles[Z_PROBE_SERVO_NR][1]); // Stow
  128. safe_delay(500);
  129. stow_state = READ(PROBE_TEST_PIN);
  130. } while (++i < 4);
  131. if (probe_inverting != deploy_state) SERIAL_ECHOLNPGM("WARNING - INVERTING setting probably backwards");
  132. if (deploy_state != stow_state) {
  133. SERIAL_ECHOLNPGM("BLTouch clone detected");
  134. if (deploy_state) {
  135. SERIAL_ECHOLNPGM(". DEPLOYED state: HIGH (logic 1)");
  136. SERIAL_ECHOLNPGM(". STOWED (triggered) state: LOW (logic 0)");
  137. }
  138. else {
  139. SERIAL_ECHOLNPGM(". DEPLOYED state: LOW (logic 0)");
  140. SERIAL_ECHOLNPGM(". STOWED (triggered) state: HIGH (logic 1)");
  141. }
  142. #if ENABLED(BLTOUCH)
  143. SERIAL_ECHOLNPGM("ERROR: BLTOUCH enabled - set this device up as a Z Servo Probe with inverting as true.");
  144. #endif
  145. }
  146. else { // measure active signal length
  147. MOVE_SERVO(probe_index, servo_angles[Z_PROBE_SERVO_NR][0]); // Deploy
  148. safe_delay(500);
  149. SERIAL_ECHOLNPGM("please trigger probe");
  150. uint16_t probe_counter = 0;
  151. // Allow 30 seconds max for operator to trigger probe
  152. for (uint16_t j = 0; j < 500 * 30 && probe_counter == 0 ; j++) {
  153. safe_delay(2);
  154. if (0 == j % (500 * 1)) gcode.reset_stepper_timeout(); // Keep steppers powered
  155. if (deploy_state != READ(PROBE_TEST_PIN)) { // probe triggered
  156. for (probe_counter = 1; probe_counter < 50 && deploy_state != READ(PROBE_TEST_PIN); ++probe_counter)
  157. safe_delay(2);
  158. if (probe_counter == 50)
  159. SERIAL_ECHOLNPGM("Z Servo Probe detected"); // >= 100mS active time
  160. else if (probe_counter >= 2)
  161. SERIAL_ECHOLNPAIR("BLTouch compatible probe detected - pulse width (+/- 4mS): ", probe_counter * 2); // allow 4 - 100mS pulse
  162. else
  163. SERIAL_ECHOLNPGM("noise detected - please re-run test"); // less than 2mS pulse
  164. MOVE_SERVO(probe_index, servo_angles[Z_PROBE_SERVO_NR][1]); // Stow
  165. } // pulse detected
  166. } // for loop waiting for trigger
  167. if (probe_counter == 0) SERIAL_ECHOLNPGM("trigger not detected");
  168. } // measure active signal length
  169. #endif
  170. } // servo_probe_test
  171. /**
  172. * M43: Pin debug - report pin state, watch pins, toggle pins and servo probe test/report
  173. *
  174. * M43 - report name and state of pin(s)
  175. * P<pin> Pin to read or watch. If omitted, reads all pins.
  176. * I Flag to ignore Marlin's pin protection.
  177. *
  178. * M43 W - Watch pins -reporting changes- until reset, click, or M108.
  179. * P<pin> Pin to read or watch. If omitted, read/watch all pins.
  180. * I Flag to ignore Marlin's pin protection.
  181. *
  182. * M43 E<bool> - Enable / disable background endstop monitoring
  183. * - Machine continues to operate
  184. * - Reports changes to endstops
  185. * - Toggles LED_PIN when an endstop changes
  186. * - Cannot reliably catch the 5mS pulse from BLTouch type probes
  187. *
  188. * M43 T - Toggle pin(s) and report which pin is being toggled
  189. * S<pin> - Start Pin number. If not given, will default to 0
  190. * L<pin> - End Pin number. If not given, will default to last pin defined for this board
  191. * I<bool> - Flag to ignore Marlin's pin protection. Use with caution!!!!
  192. * R - Repeat pulses on each pin this number of times before continueing to next pin
  193. * W - Wait time (in miliseconds) between pulses. If not given will default to 500
  194. *
  195. * M43 S - Servo probe test
  196. * P<index> - Probe index (optional - defaults to 0
  197. */
  198. void GcodeSuite::M43() {
  199. if (parser.seen('T')) { // must be first or else its "S" and "E" parameters will execute endstop or servo test
  200. toggle_pins();
  201. return;
  202. }
  203. // Enable or disable endstop monitoring
  204. if (parser.seen('E')) {
  205. endstops.monitor_flag = parser.value_bool();
  206. SERIAL_ECHOPGM("endstop monitor ");
  207. serialprintPGM(endstops.monitor_flag ? PSTR("en") : PSTR("dis"));
  208. SERIAL_ECHOLNPGM("abled");
  209. return;
  210. }
  211. if (parser.seen('S')) {
  212. servo_probe_test();
  213. return;
  214. }
  215. // Get the range of pins to test or watch
  216. uint8_t first_pin = PARSED_PIN_INDEX('P', 0),
  217. last_pin = parser.seenval('P') ? first_pin : NUMBER_PINS_TOTAL - 1;
  218. if (first_pin > last_pin) return;
  219. const bool ignore_protection = parser.boolval('I');
  220. // Watch until click, M108, or reset
  221. if (parser.boolval('W')) {
  222. SERIAL_ECHOLNPGM("Watching pins");
  223. #ifdef ARDUINO_ARCH_SAM
  224. NOLESS(first_pin, 2); // don't hijack the UART pins
  225. #endif
  226. uint8_t pin_state[last_pin - first_pin + 1];
  227. for (uint8_t i = first_pin; i <= last_pin; i++) {
  228. pin_t pin = GET_PIN_MAP_PIN(i);
  229. if (!VALID_PIN(pin)) continue;
  230. if (M43_NEVER_TOUCH(i) || (!ignore_protection && pin_is_protected(pin))) continue;
  231. pinMode(pin, INPUT_PULLUP);
  232. delay(1);
  233. /*
  234. if (IS_ANALOG(pin))
  235. pin_state[pin - first_pin] = analogRead(DIGITAL_PIN_TO_ANALOG_PIN(pin)); // int16_t pin_state[...]
  236. else
  237. //*/
  238. pin_state[i - first_pin] = extDigitalRead(pin);
  239. }
  240. #if HAS_RESUME_CONTINUE
  241. wait_for_user = true;
  242. #if ENABLED(HOST_PROMPT_SUPPORT)
  243. host_prompt_do(PROMPT_USER_CONTINUE, PSTR("M43 Wait Called"), PSTR("Continue"));
  244. #endif
  245. KEEPALIVE_STATE(PAUSED_FOR_USER);
  246. #endif
  247. for (;;) {
  248. for (uint8_t i = first_pin; i <= last_pin; i++) {
  249. pin_t pin = GET_PIN_MAP_PIN(i);
  250. if (!VALID_PIN(pin)) continue;
  251. if (M43_NEVER_TOUCH(i) || (!ignore_protection && pin_is_protected(pin))) continue;
  252. const byte val =
  253. /*
  254. IS_ANALOG(pin)
  255. ? analogRead(DIGITAL_PIN_TO_ANALOG_PIN(pin)) : // int16_t val
  256. :
  257. //*/
  258. extDigitalRead(pin);
  259. if (val != pin_state[i - first_pin]) {
  260. report_pin_state_extended(pin, ignore_protection, false);
  261. pin_state[i - first_pin] = val;
  262. }
  263. }
  264. #if HAS_RESUME_CONTINUE
  265. if (!wait_for_user) {
  266. KEEPALIVE_STATE(IN_HANDLER);
  267. break;
  268. }
  269. #endif
  270. safe_delay(200);
  271. }
  272. return;
  273. }
  274. // Report current state of selected pin(s)
  275. for (uint8_t i = first_pin; i <= last_pin; i++) {
  276. pin_t pin = GET_PIN_MAP_PIN(i);
  277. if (VALID_PIN(pin)) report_pin_state_extended(pin, ignore_protection, true);
  278. }
  279. }
  280. #endif // PINS_DEBUGGING