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

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