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.

M911-M914.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 HAS_TRINAMIC
  24. #include "../../gcode.h"
  25. #include "../../../feature/tmc_util.h"
  26. #include "../../../module/stepper/indirection.h"
  27. #include "../../../module/planner.h"
  28. #include "../../queue.h"
  29. #if ENABLED(MONITOR_DRIVER_STATUS)
  30. #define M91x_USE(ST) (AXIS_DRIVER_TYPE(ST, TMC2130) || AXIS_DRIVER_TYPE(ST, TMC2160) || AXIS_DRIVER_TYPE(ST, TMC2208) || AXIS_DRIVER_TYPE(ST, TMC2209) || AXIS_DRIVER_TYPE(ST, TMC2660) || AXIS_DRIVER_TYPE(ST, TMC5130) || AXIS_DRIVER_TYPE(ST, TMC5160))
  31. #define M91x_USE_E(N) (E_STEPPERS > N && M91x_USE(E##N))
  32. #define M91x_SOME_X (M91x_USE(X) || M91x_USE(X2))
  33. #define M91x_SOME_Y (M91x_USE(Y) || M91x_USE(Y2))
  34. #define M91x_SOME_Z (M91x_USE(Z) || M91x_USE(Z2) || M91x_USE(Z3) || M91x_USE(Z4))
  35. #define M91x_SOME_E (M91x_USE_E(0) || M91x_USE_E(1) || M91x_USE_E(2) || M91x_USE_E(3) || M91x_USE_E(4) || M91x_USE_E(5))
  36. #if !M91x_SOME_X && !M91x_SOME_Y && !M91x_SOME_Z && !M91x_SOME_E
  37. #error "MONITOR_DRIVER_STATUS requires at least one TMC2130, 2160, 2208, 2209, 2660, 5130, or 5160."
  38. #endif
  39. /**
  40. * M911: Report TMC stepper driver overtemperature pre-warn flag
  41. * This flag is held by the library, persisting until cleared by M912
  42. */
  43. void GcodeSuite::M911() {
  44. #if M91x_USE(X)
  45. tmc_report_otpw(stepperX);
  46. #endif
  47. #if M91x_USE(X2)
  48. tmc_report_otpw(stepperX2);
  49. #endif
  50. #if M91x_USE(Y)
  51. tmc_report_otpw(stepperY);
  52. #endif
  53. #if M91x_USE(Y2)
  54. tmc_report_otpw(stepperY2);
  55. #endif
  56. #if M91x_USE(Z)
  57. tmc_report_otpw(stepperZ);
  58. #endif
  59. #if M91x_USE(Z2)
  60. tmc_report_otpw(stepperZ2);
  61. #endif
  62. #if M91x_USE(Z3)
  63. tmc_report_otpw(stepperZ3);
  64. #endif
  65. #if M91x_USE(Z4)
  66. tmc_report_otpw(stepperZ4);
  67. #endif
  68. #if M91x_USE_E(0)
  69. tmc_report_otpw(stepperE0);
  70. #endif
  71. #if M91x_USE_E(1)
  72. tmc_report_otpw(stepperE1);
  73. #endif
  74. #if M91x_USE_E(2)
  75. tmc_report_otpw(stepperE2);
  76. #endif
  77. #if M91x_USE_E(3)
  78. tmc_report_otpw(stepperE3);
  79. #endif
  80. #if M91x_USE_E(4)
  81. tmc_report_otpw(stepperE4);
  82. #endif
  83. #if M91x_USE_E(5)
  84. tmc_report_otpw(stepperE5);
  85. #endif
  86. }
  87. /**
  88. * M912: Clear TMC stepper driver overtemperature pre-warn flag held by the library
  89. * Specify one or more axes with X, Y, Z, X1, Y1, Z1, X2, Y2, Z2, Z3, Z4 and E[index].
  90. * If no axes are given, clear all.
  91. *
  92. * Examples:
  93. * M912 X ; clear X and X2
  94. * M912 X1 ; clear X1 only
  95. * M912 X2 ; clear X2 only
  96. * M912 X E ; clear X, X2, and all E
  97. * M912 E1 ; clear E1 only
  98. */
  99. void GcodeSuite::M912() {
  100. #if M91x_SOME_X
  101. const bool hasX = parser.seen(axis_codes.x);
  102. #else
  103. constexpr bool hasX = false;
  104. #endif
  105. #if M91x_SOME_Y
  106. const bool hasY = parser.seen(axis_codes.y);
  107. #else
  108. constexpr bool hasY = false;
  109. #endif
  110. #if M91x_SOME_Z
  111. const bool hasZ = parser.seen(axis_codes.z);
  112. #else
  113. constexpr bool hasZ = false;
  114. #endif
  115. #if M91x_SOME_E
  116. const bool hasE = parser.seen(axis_codes.e);
  117. #else
  118. constexpr bool hasE = false;
  119. #endif
  120. const bool hasNone = !hasX && !hasY && !hasZ && !hasE;
  121. #if M91x_SOME_X
  122. const int8_t xval = int8_t(parser.byteval(axis_codes.x, 0xFF));
  123. #if M91x_USE(X)
  124. if (hasNone || xval == 1 || (hasX && xval < 0)) tmc_clear_otpw(stepperX);
  125. #endif
  126. #if M91x_USE(X2)
  127. if (hasNone || xval == 2 || (hasX && xval < 0)) tmc_clear_otpw(stepperX2);
  128. #endif
  129. #endif
  130. #if M91x_SOME_Y
  131. const int8_t yval = int8_t(parser.byteval(axis_codes.y, 0xFF));
  132. #if M91x_USE(Y)
  133. if (hasNone || yval == 1 || (hasY && yval < 0)) tmc_clear_otpw(stepperY);
  134. #endif
  135. #if M91x_USE(Y2)
  136. if (hasNone || yval == 2 || (hasY && yval < 0)) tmc_clear_otpw(stepperY2);
  137. #endif
  138. #endif
  139. #if M91x_SOME_Z
  140. const int8_t zval = int8_t(parser.byteval(axis_codes.z, 0xFF));
  141. #if M91x_USE(Z)
  142. if (hasNone || zval == 1 || (hasZ && zval < 0)) tmc_clear_otpw(stepperZ);
  143. #endif
  144. #if M91x_USE(Z2)
  145. if (hasNone || zval == 2 || (hasZ && zval < 0)) tmc_clear_otpw(stepperZ2);
  146. #endif
  147. #if M91x_USE(Z3)
  148. if (hasNone || zval == 3 || (hasZ && zval < 0)) tmc_clear_otpw(stepperZ3);
  149. #endif
  150. #if M91x_USE(Z4)
  151. if (hasNone || zval == 4 || (hasZ && zval < 0)) tmc_clear_otpw(stepperZ4);
  152. #endif
  153. #endif
  154. #if M91x_SOME_E
  155. const int8_t eval = int8_t(parser.byteval(axis_codes.e, 0xFF));
  156. #if M91x_USE_E(0)
  157. if (hasNone || eval == 0 || (hasE && eval < 0)) tmc_clear_otpw(stepperE0);
  158. #endif
  159. #if M91x_USE_E(1)
  160. if (hasNone || eval == 1 || (hasE && eval < 0)) tmc_clear_otpw(stepperE1);
  161. #endif
  162. #if M91x_USE_E(2)
  163. if (hasNone || eval == 2 || (hasE && eval < 0)) tmc_clear_otpw(stepperE2);
  164. #endif
  165. #if M91x_USE_E(3)
  166. if (hasNone || eval == 3 || (hasE && eval < 0)) tmc_clear_otpw(stepperE3);
  167. #endif
  168. #if M91x_USE_E(4)
  169. if (hasNone || eval == 4 || (hasE && eval < 0)) tmc_clear_otpw(stepperE4);
  170. #endif
  171. #if M91x_USE_E(5)
  172. if (hasNone || eval == 5 || (hasE && eval < 0)) tmc_clear_otpw(stepperE5);
  173. #endif
  174. #endif
  175. }
  176. #endif // MONITOR_DRIVER_STATUS
  177. /**
  178. * M913: Set HYBRID_THRESHOLD speed.
  179. */
  180. #if ENABLED(HYBRID_THRESHOLD)
  181. void GcodeSuite::M913() {
  182. #define TMC_SAY_PWMTHRS(A,Q) tmc_print_pwmthrs(stepper##Q)
  183. #define TMC_SET_PWMTHRS(A,Q) stepper##Q.set_pwm_thrs(value)
  184. #define TMC_SAY_PWMTHRS_E(E) tmc_print_pwmthrs(stepperE##E)
  185. #define TMC_SET_PWMTHRS_E(E) stepperE##E.set_pwm_thrs(value)
  186. bool report = true;
  187. #if AXIS_IS_TMC(X) || AXIS_IS_TMC(X2) || AXIS_IS_TMC(Y) || AXIS_IS_TMC(Y2) || AXIS_IS_TMC(Z) || AXIS_IS_TMC(Z2) || AXIS_IS_TMC(Z3) || AXIS_IS_TMC(Z4)
  188. const uint8_t index = parser.byteval('I');
  189. #endif
  190. LOOP_XYZE(i) if (int32_t value = parser.longval(axis_codes[i])) {
  191. report = false;
  192. switch (i) {
  193. case X_AXIS:
  194. #if AXIS_HAS_STEALTHCHOP(X)
  195. if (index < 2) TMC_SET_PWMTHRS(X,X);
  196. #endif
  197. #if AXIS_HAS_STEALTHCHOP(X2)
  198. if (!(index & 1)) TMC_SET_PWMTHRS(X,X2);
  199. #endif
  200. break;
  201. case Y_AXIS:
  202. #if AXIS_HAS_STEALTHCHOP(Y)
  203. if (index < 2) TMC_SET_PWMTHRS(Y,Y);
  204. #endif
  205. #if AXIS_HAS_STEALTHCHOP(Y2)
  206. if (!(index & 1)) TMC_SET_PWMTHRS(Y,Y2);
  207. #endif
  208. break;
  209. case Z_AXIS:
  210. #if AXIS_HAS_STEALTHCHOP(Z)
  211. if (index < 2) TMC_SET_PWMTHRS(Z,Z);
  212. #endif
  213. #if AXIS_HAS_STEALTHCHOP(Z2)
  214. if (index == 0 || index == 2) TMC_SET_PWMTHRS(Z,Z2);
  215. #endif
  216. #if AXIS_HAS_STEALTHCHOP(Z3)
  217. if (index == 0 || index == 3) TMC_SET_PWMTHRS(Z,Z3);
  218. #endif
  219. #if AXIS_HAS_STEALTHCHOP(Z4)
  220. if (index == 0 || index == 4) TMC_SET_PWMTHRS(Z,Z4);
  221. #endif
  222. break;
  223. case E_AXIS: {
  224. #if E_STEPPERS
  225. const int8_t target_extruder = get_target_extruder_from_command();
  226. if (target_extruder < 0) return;
  227. switch (target_extruder) {
  228. #if AXIS_HAS_STEALTHCHOP(E0)
  229. case 0: TMC_SET_PWMTHRS_E(0); break;
  230. #endif
  231. #if E_STEPPERS > 1 && AXIS_HAS_STEALTHCHOP(E1)
  232. case 1: TMC_SET_PWMTHRS_E(1); break;
  233. #endif
  234. #if E_STEPPERS > 2 && AXIS_HAS_STEALTHCHOP(E2)
  235. case 2: TMC_SET_PWMTHRS_E(2); break;
  236. #endif
  237. #if E_STEPPERS > 3 && AXIS_HAS_STEALTHCHOP(E3)
  238. case 3: TMC_SET_PWMTHRS_E(3); break;
  239. #endif
  240. #if E_STEPPERS > 4 && AXIS_HAS_STEALTHCHOP(E4)
  241. case 4: TMC_SET_PWMTHRS_E(4); break;
  242. #endif
  243. #if E_STEPPERS > 5 && AXIS_HAS_STEALTHCHOP(E5)
  244. case 5: TMC_SET_PWMTHRS_E(5); break;
  245. #endif
  246. }
  247. #endif // E_STEPPERS
  248. } break;
  249. }
  250. }
  251. if (report) {
  252. #if AXIS_HAS_STEALTHCHOP(X)
  253. TMC_SAY_PWMTHRS(X,X);
  254. #endif
  255. #if AXIS_HAS_STEALTHCHOP(X2)
  256. TMC_SAY_PWMTHRS(X,X2);
  257. #endif
  258. #if AXIS_HAS_STEALTHCHOP(Y)
  259. TMC_SAY_PWMTHRS(Y,Y);
  260. #endif
  261. #if AXIS_HAS_STEALTHCHOP(Y2)
  262. TMC_SAY_PWMTHRS(Y,Y2);
  263. #endif
  264. #if AXIS_HAS_STEALTHCHOP(Z)
  265. TMC_SAY_PWMTHRS(Z,Z);
  266. #endif
  267. #if AXIS_HAS_STEALTHCHOP(Z2)
  268. TMC_SAY_PWMTHRS(Z,Z2);
  269. #endif
  270. #if AXIS_HAS_STEALTHCHOP(Z3)
  271. TMC_SAY_PWMTHRS(Z,Z3);
  272. #endif
  273. #if AXIS_HAS_STEALTHCHOP(Z4)
  274. TMC_SAY_PWMTHRS(Z,Z4);
  275. #endif
  276. #if E_STEPPERS && AXIS_HAS_STEALTHCHOP(E0)
  277. TMC_SAY_PWMTHRS_E(0);
  278. #endif
  279. #if E_STEPPERS > 1 && AXIS_HAS_STEALTHCHOP(E1)
  280. TMC_SAY_PWMTHRS_E(1);
  281. #endif
  282. #if E_STEPPERS > 2 && AXIS_HAS_STEALTHCHOP(E2)
  283. TMC_SAY_PWMTHRS_E(2);
  284. #endif
  285. #if E_STEPPERS > 3 && AXIS_HAS_STEALTHCHOP(E3)
  286. TMC_SAY_PWMTHRS_E(3);
  287. #endif
  288. #if E_STEPPERS > 4 && AXIS_HAS_STEALTHCHOP(E4)
  289. TMC_SAY_PWMTHRS_E(4);
  290. #endif
  291. #if E_STEPPERS > 5 && AXIS_HAS_STEALTHCHOP(E5)
  292. TMC_SAY_PWMTHRS_E(5);
  293. #endif
  294. }
  295. }
  296. #endif // HYBRID_THRESHOLD
  297. /**
  298. * M914: Set StallGuard sensitivity.
  299. */
  300. #if USE_SENSORLESS
  301. void GcodeSuite::M914() {
  302. bool report = true;
  303. const uint8_t index = parser.byteval('I');
  304. LOOP_XYZ(i) if (parser.seen(axis_codes[i])) {
  305. const int16_t value = parser.value_int();
  306. report = false;
  307. switch (i) {
  308. #if X_SENSORLESS
  309. case X_AXIS:
  310. #if AXIS_HAS_STALLGUARD(X)
  311. if (index < 2) stepperX.homing_threshold(value);
  312. #endif
  313. #if AXIS_HAS_STALLGUARD(X2)
  314. if (!(index & 1)) stepperX2.homing_threshold(value);
  315. #endif
  316. break;
  317. #endif
  318. #if Y_SENSORLESS
  319. case Y_AXIS:
  320. #if AXIS_HAS_STALLGUARD(Y)
  321. if (index < 2) stepperY.homing_threshold(value);
  322. #endif
  323. #if AXIS_HAS_STALLGUARD(Y2)
  324. if (!(index & 1)) stepperY2.homing_threshold(value);
  325. #endif
  326. break;
  327. #endif
  328. #if Z_SENSORLESS
  329. case Z_AXIS:
  330. #if AXIS_HAS_STALLGUARD(Z)
  331. if (index < 2) stepperZ.homing_threshold(value);
  332. #endif
  333. #if AXIS_HAS_STALLGUARD(Z2)
  334. if (index == 0 || index == 2) stepperZ2.homing_threshold(value);
  335. #endif
  336. #if AXIS_HAS_STALLGUARD(Z3)
  337. if (index == 0 || index == 3) stepperZ3.homing_threshold(value);
  338. #endif
  339. #if AXIS_HAS_STALLGUARD(Z4)
  340. if (index == 0 || index == 4) stepperZ4.homing_threshold(value);
  341. #endif
  342. break;
  343. #endif
  344. }
  345. }
  346. if (report) {
  347. #if X_SENSORLESS
  348. #if AXIS_HAS_STALLGUARD(X)
  349. tmc_print_sgt(stepperX);
  350. #endif
  351. #if AXIS_HAS_STALLGUARD(X2)
  352. tmc_print_sgt(stepperX2);
  353. #endif
  354. #endif
  355. #if Y_SENSORLESS
  356. #if AXIS_HAS_STALLGUARD(Y)
  357. tmc_print_sgt(stepperY);
  358. #endif
  359. #if AXIS_HAS_STALLGUARD(Y2)
  360. tmc_print_sgt(stepperY2);
  361. #endif
  362. #endif
  363. #if Z_SENSORLESS
  364. #if AXIS_HAS_STALLGUARD(Z)
  365. tmc_print_sgt(stepperZ);
  366. #endif
  367. #if AXIS_HAS_STALLGUARD(Z2)
  368. tmc_print_sgt(stepperZ2);
  369. #endif
  370. #if AXIS_HAS_STALLGUARD(Z3)
  371. tmc_print_sgt(stepperZ3);
  372. #endif
  373. #if AXIS_HAS_STALLGUARD(Z4)
  374. tmc_print_sgt(stepperZ4);
  375. #endif
  376. #endif
  377. }
  378. }
  379. #endif // USE_SENSORLESS
  380. #endif // HAS_TRINAMIC