My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

tmc_util.cpp 42KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  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 HAS_TRINAMIC_CONFIG
  24. #include "tmc_util.h"
  25. #include "../MarlinCore.h"
  26. #include "../module/stepper/indirection.h"
  27. #include "../module/printcounter.h"
  28. #include "../libs/duration_t.h"
  29. #include "../gcode/gcode.h"
  30. #if ENABLED(TMC_DEBUG)
  31. #include "../libs/hex_print.h"
  32. #if ENABLED(MONITOR_DRIVER_STATUS)
  33. static uint16_t report_tmc_status_interval; // = 0
  34. #endif
  35. #endif
  36. /**
  37. * Check for over temperature or short to ground error flags.
  38. * Report and log warning of overtemperature condition.
  39. * Reduce driver current in a persistent otpw condition.
  40. * Keep track of otpw counter so we don't reduce current on a single instance,
  41. * and so we don't repeatedly report warning before the condition is cleared.
  42. */
  43. #if ENABLED(MONITOR_DRIVER_STATUS)
  44. struct TMC_driver_data {
  45. uint32_t drv_status;
  46. bool is_otpw:1,
  47. is_ot:1,
  48. is_s2g:1,
  49. is_error:1
  50. #if ENABLED(TMC_DEBUG)
  51. , is_stall:1
  52. , is_stealth:1
  53. , is_standstill:1
  54. #if HAS_STALLGUARD
  55. , sg_result_reasonable:1
  56. #endif
  57. #endif
  58. ;
  59. #if ENABLED(TMC_DEBUG)
  60. #if HAS_TMCX1X0 || HAS_TMC220x
  61. uint8_t cs_actual;
  62. #endif
  63. #if HAS_STALLGUARD
  64. uint16_t sg_result;
  65. #endif
  66. #endif
  67. };
  68. #if HAS_TMCX1X0
  69. #if ENABLED(TMC_DEBUG)
  70. static uint32_t get_pwm_scale(TMC2130Stepper &st) { return st.PWM_SCALE(); }
  71. #endif
  72. static TMC_driver_data get_driver_data(TMC2130Stepper &st) {
  73. constexpr uint8_t OT_bp = 25, OTPW_bp = 26;
  74. constexpr uint32_t S2G_bm = 0x18000000;
  75. #if ENABLED(TMC_DEBUG)
  76. constexpr uint16_t SG_RESULT_bm = 0x3FF; // 0:9
  77. constexpr uint8_t STEALTH_bp = 14;
  78. constexpr uint32_t CS_ACTUAL_bm = 0x1F0000; // 16:20
  79. constexpr uint8_t STALL_GUARD_bp = 24;
  80. constexpr uint8_t STST_bp = 31;
  81. #endif
  82. TMC_driver_data data;
  83. const auto ds = data.drv_status = st.DRV_STATUS();
  84. #ifdef __AVR__
  85. // 8-bit optimization saves up to 70 bytes of PROGMEM per axis
  86. uint8_t spart;
  87. #if ENABLED(TMC_DEBUG)
  88. data.sg_result = ds & SG_RESULT_bm;
  89. spart = ds >> 8;
  90. data.is_stealth = TEST(spart, STEALTH_bp - 8);
  91. spart = ds >> 16;
  92. data.cs_actual = spart & (CS_ACTUAL_bm >> 16);
  93. #endif
  94. spart = ds >> 24;
  95. data.is_ot = TEST(spart, OT_bp - 24);
  96. data.is_otpw = TEST(spart, OTPW_bp - 24);
  97. data.is_s2g = !!(spart & (S2G_bm >> 24));
  98. #if ENABLED(TMC_DEBUG)
  99. data.is_stall = TEST(spart, STALL_GUARD_bp - 24);
  100. data.is_standstill = TEST(spart, STST_bp - 24);
  101. data.sg_result_reasonable = !data.is_standstill; // sg_result has no reasonable meaning while standstill
  102. #endif
  103. #else // !__AVR__
  104. data.is_ot = TEST(ds, OT_bp);
  105. data.is_otpw = TEST(ds, OTPW_bp);
  106. data.is_s2g = !!(ds & S2G_bm);
  107. #if ENABLED(TMC_DEBUG)
  108. constexpr uint8_t CS_ACTUAL_sb = 16;
  109. data.sg_result = ds & SG_RESULT_bm;
  110. data.is_stealth = TEST(ds, STEALTH_bp);
  111. data.cs_actual = (ds & CS_ACTUAL_bm) >> CS_ACTUAL_sb;
  112. data.is_stall = TEST(ds, STALL_GUARD_bp);
  113. data.is_standstill = TEST(ds, STST_bp);
  114. data.sg_result_reasonable = !data.is_standstill; // sg_result has no reasonable meaning while standstill
  115. #endif
  116. #endif // !__AVR__
  117. return data;
  118. }
  119. #endif // HAS_TMCX1X0
  120. #if HAS_TMC220x
  121. #if ENABLED(TMC_DEBUG)
  122. static uint32_t get_pwm_scale(TMC2208Stepper &st) { return st.pwm_scale_sum(); }
  123. #endif
  124. static TMC_driver_data get_driver_data(TMC2208Stepper &st) {
  125. constexpr uint8_t OTPW_bp = 0, OT_bp = 1;
  126. constexpr uint8_t S2G_bm = 0b111100; // 2..5
  127. TMC_driver_data data;
  128. const auto ds = data.drv_status = st.DRV_STATUS();
  129. data.is_otpw = TEST(ds, OTPW_bp);
  130. data.is_ot = TEST(ds, OT_bp);
  131. data.is_s2g = !!(ds & S2G_bm);
  132. #if ENABLED(TMC_DEBUG)
  133. constexpr uint32_t CS_ACTUAL_bm = 0x1F0000; // 16:20
  134. constexpr uint8_t STEALTH_bp = 30, STST_bp = 31;
  135. #ifdef __AVR__
  136. // 8-bit optimization saves up to 12 bytes of PROGMEM per axis
  137. uint8_t spart = ds >> 16;
  138. data.cs_actual = spart & (CS_ACTUAL_bm >> 16);
  139. spart = ds >> 24;
  140. data.is_stealth = TEST(spart, STEALTH_bp - 24);
  141. data.is_standstill = TEST(spart, STST_bp - 24);
  142. #else
  143. constexpr uint8_t CS_ACTUAL_sb = 16;
  144. data.cs_actual = (ds & CS_ACTUAL_bm) >> CS_ACTUAL_sb;
  145. data.is_stealth = TEST(ds, STEALTH_bp);
  146. data.is_standstill = TEST(ds, STST_bp);
  147. #endif
  148. TERN_(HAS_STALLGUARD, data.sg_result_reasonable = false);
  149. #endif
  150. return data;
  151. }
  152. #endif // TMC2208 || TMC2209
  153. #if HAS_DRIVER(TMC2660)
  154. #if ENABLED(TMC_DEBUG)
  155. static uint32_t get_pwm_scale(TMC2660Stepper) { return 0; }
  156. #endif
  157. static TMC_driver_data get_driver_data(TMC2660Stepper &st) {
  158. constexpr uint8_t OT_bp = 1, OTPW_bp = 2;
  159. constexpr uint8_t S2G_bm = 0b11000;
  160. TMC_driver_data data;
  161. const auto ds = data.drv_status = st.DRVSTATUS();
  162. uint8_t spart = ds & 0xFF;
  163. data.is_otpw = TEST(spart, OTPW_bp);
  164. data.is_ot = TEST(spart, OT_bp);
  165. data.is_s2g = !!(ds & S2G_bm);
  166. #if ENABLED(TMC_DEBUG)
  167. constexpr uint8_t STALL_GUARD_bp = 0;
  168. constexpr uint8_t STST_bp = 7, SG_RESULT_sp = 10;
  169. constexpr uint32_t SG_RESULT_bm = 0xFFC00; // 10:19
  170. data.is_stall = TEST(spart, STALL_GUARD_bp);
  171. data.is_standstill = TEST(spart, STST_bp);
  172. data.sg_result = (ds & SG_RESULT_bm) >> SG_RESULT_sp;
  173. data.sg_result_reasonable = true;
  174. #endif
  175. return data;
  176. }
  177. #endif // TMC2660
  178. #if ENABLED(STOP_ON_ERROR)
  179. void report_driver_error(const TMC_driver_data &data) {
  180. SERIAL_ECHOPGM(" driver error detected: 0x");
  181. SERIAL_PRINTLN(data.drv_status, PrintBase::Hex);
  182. if (data.is_ot) SERIAL_ECHOLNPGM("overtemperature");
  183. if (data.is_s2g) SERIAL_ECHOLNPGM("coil short circuit");
  184. TERN_(TMC_DEBUG, tmc_report_all());
  185. kill(F("Driver error"));
  186. }
  187. #endif
  188. template<typename TMC>
  189. void report_driver_otpw(TMC &st) {
  190. char timestamp[14];
  191. duration_t elapsed = print_job_timer.duration();
  192. const bool has_days = (elapsed.value > 60*60*24L);
  193. (void)elapsed.toDigital(timestamp, has_days);
  194. SERIAL_EOL();
  195. SERIAL_ECHO(timestamp);
  196. SERIAL_ECHOPGM(": ");
  197. st.printLabel();
  198. SERIAL_ECHOLNPGM(" driver overtemperature warning! (", st.getMilliamps(), "mA)");
  199. }
  200. template<typename TMC>
  201. void report_polled_driver_data(TMC &st, const TMC_driver_data &data) {
  202. const uint32_t pwm_scale = get_pwm_scale(st);
  203. st.printLabel();
  204. SERIAL_CHAR(':'); SERIAL_ECHO(pwm_scale);
  205. #if ENABLED(TMC_DEBUG)
  206. #if HAS_TMCX1X0 || HAS_TMC220x
  207. SERIAL_CHAR('/'); SERIAL_ECHO(data.cs_actual);
  208. #endif
  209. #if HAS_STALLGUARD
  210. SERIAL_CHAR('/');
  211. if (data.sg_result_reasonable)
  212. SERIAL_ECHO(data.sg_result);
  213. else
  214. SERIAL_CHAR('-');
  215. #endif
  216. #endif
  217. SERIAL_CHAR('|');
  218. if (st.error_count) SERIAL_CHAR('E'); // Error
  219. if (data.is_ot) SERIAL_CHAR('O'); // Over-temperature
  220. if (data.is_otpw) SERIAL_CHAR('W'); // over-temperature pre-Warning
  221. #if ENABLED(TMC_DEBUG)
  222. if (data.is_stall) SERIAL_CHAR('G'); // stallGuard
  223. if (data.is_stealth) SERIAL_CHAR('T'); // stealthChop
  224. if (data.is_standstill) SERIAL_CHAR('I'); // standstIll
  225. #endif
  226. if (st.flag_otpw) SERIAL_CHAR('F'); // otpw Flag
  227. SERIAL_CHAR('|');
  228. if (st.otpw_count > 0) SERIAL_ECHO(st.otpw_count);
  229. SERIAL_CHAR('\t');
  230. }
  231. #if CURRENT_STEP_DOWN > 0
  232. template<typename TMC>
  233. void step_current_down(TMC &st) {
  234. if (st.isEnabled()) {
  235. const uint16_t I_rms = st.getMilliamps() - (CURRENT_STEP_DOWN);
  236. if (I_rms > 50) {
  237. st.rms_current(I_rms);
  238. #if ENABLED(REPORT_CURRENT_CHANGE)
  239. st.printLabel();
  240. SERIAL_ECHOLNPGM(" current decreased to ", I_rms);
  241. #endif
  242. }
  243. }
  244. }
  245. #else
  246. #define step_current_down(...)
  247. #endif
  248. template<typename TMC>
  249. bool monitor_tmc_driver(TMC &st, const bool need_update_error_counters, const bool need_debug_reporting) {
  250. TMC_driver_data data = get_driver_data(st);
  251. if (data.drv_status == 0xFFFFFFFF || data.drv_status == 0x0) return false;
  252. bool should_step_down = false;
  253. if (need_update_error_counters) {
  254. if (data.is_ot | data.is_s2g) st.error_count++;
  255. else if (st.error_count > 0) st.error_count--;
  256. #if ENABLED(STOP_ON_ERROR)
  257. if (st.error_count >= 10) {
  258. SERIAL_EOL();
  259. st.printLabel();
  260. report_driver_error(data);
  261. }
  262. #endif
  263. // Report if a warning was triggered
  264. if (data.is_otpw && st.otpw_count == 0)
  265. report_driver_otpw(st);
  266. #if CURRENT_STEP_DOWN > 0
  267. // Decrease current if is_otpw is true and driver is enabled and there's been more than 4 warnings
  268. if (data.is_otpw && st.otpw_count > 4 && st.isEnabled())
  269. should_step_down = true;
  270. #endif
  271. if (data.is_otpw) {
  272. st.otpw_count++;
  273. st.flag_otpw = true;
  274. }
  275. else if (st.otpw_count > 0) st.otpw_count = 0;
  276. }
  277. #if ENABLED(TMC_DEBUG)
  278. if (need_debug_reporting) report_polled_driver_data(st, data);
  279. #endif
  280. return should_step_down;
  281. }
  282. void monitor_tmc_drivers() {
  283. const millis_t ms = millis();
  284. // Poll TMC drivers at the configured interval
  285. static millis_t next_poll = 0;
  286. const bool need_update_error_counters = ELAPSED(ms, next_poll);
  287. if (need_update_error_counters) next_poll = ms + MONITOR_DRIVER_STATUS_INTERVAL_MS;
  288. // Also poll at intervals for debugging
  289. #if ENABLED(TMC_DEBUG)
  290. static millis_t next_debug_reporting = 0;
  291. const bool need_debug_reporting = report_tmc_status_interval && ELAPSED(ms, next_debug_reporting);
  292. if (need_debug_reporting) next_debug_reporting = ms + report_tmc_status_interval;
  293. #else
  294. constexpr bool need_debug_reporting = false;
  295. #endif
  296. if (need_update_error_counters || need_debug_reporting) {
  297. #if AXIS_IS_TMC(X) || AXIS_IS_TMC(X2)
  298. {
  299. bool result = false;
  300. #if AXIS_IS_TMC(X)
  301. if (monitor_tmc_driver(stepperX, need_update_error_counters, need_debug_reporting)) result = true;
  302. #endif
  303. #if AXIS_IS_TMC(X2)
  304. if (monitor_tmc_driver(stepperX2, need_update_error_counters, need_debug_reporting)) result = true;
  305. #endif
  306. if (result) {
  307. #if AXIS_IS_TMC(X)
  308. step_current_down(stepperX);
  309. #endif
  310. #if AXIS_IS_TMC(X2)
  311. step_current_down(stepperX2);
  312. #endif
  313. }
  314. }
  315. #endif
  316. #if AXIS_IS_TMC(Y) || AXIS_IS_TMC(Y2)
  317. {
  318. bool result = false;
  319. #if AXIS_IS_TMC(Y)
  320. if (monitor_tmc_driver(stepperY, need_update_error_counters, need_debug_reporting)) result = true;
  321. #endif
  322. #if AXIS_IS_TMC(Y2)
  323. if (monitor_tmc_driver(stepperY2, need_update_error_counters, need_debug_reporting)) result = true;
  324. #endif
  325. if (result) {
  326. #if AXIS_IS_TMC(Y)
  327. step_current_down(stepperY);
  328. #endif
  329. #if AXIS_IS_TMC(Y2)
  330. step_current_down(stepperY2);
  331. #endif
  332. }
  333. }
  334. #endif
  335. #if AXIS_IS_TMC(Z) || AXIS_IS_TMC(Z2) || AXIS_IS_TMC(Z3) || AXIS_IS_TMC(Z4)
  336. {
  337. bool result = false;
  338. #if AXIS_IS_TMC(Z)
  339. if (monitor_tmc_driver(stepperZ, need_update_error_counters, need_debug_reporting)) result = true;
  340. #endif
  341. #if AXIS_IS_TMC(Z2)
  342. if (monitor_tmc_driver(stepperZ2, need_update_error_counters, need_debug_reporting)) result = true;
  343. #endif
  344. #if AXIS_IS_TMC(Z3)
  345. if (monitor_tmc_driver(stepperZ3, need_update_error_counters, need_debug_reporting)) result = true;
  346. #endif
  347. #if AXIS_IS_TMC(Z4)
  348. if (monitor_tmc_driver(stepperZ4, need_update_error_counters, need_debug_reporting)) result = true;
  349. #endif
  350. if (result) {
  351. #if AXIS_IS_TMC(Z)
  352. step_current_down(stepperZ);
  353. #endif
  354. #if AXIS_IS_TMC(Z2)
  355. step_current_down(stepperZ2);
  356. #endif
  357. #if AXIS_IS_TMC(Z3)
  358. step_current_down(stepperZ3);
  359. #endif
  360. #if AXIS_IS_TMC(Z4)
  361. step_current_down(stepperZ4);
  362. #endif
  363. }
  364. }
  365. #endif
  366. #if AXIS_IS_TMC(I)
  367. if (monitor_tmc_driver(stepperI, need_update_error_counters, need_debug_reporting))
  368. step_current_down(stepperI);
  369. #endif
  370. #if AXIS_IS_TMC(J)
  371. if (monitor_tmc_driver(stepperJ, need_update_error_counters, need_debug_reporting))
  372. step_current_down(stepperJ);
  373. #endif
  374. #if AXIS_IS_TMC(K)
  375. if (monitor_tmc_driver(stepperK, need_update_error_counters, need_debug_reporting))
  376. step_current_down(stepperK);
  377. #endif
  378. #if AXIS_IS_TMC(U)
  379. if (monitor_tmc_driver(stepperU, need_update_error_counters, need_debug_reporting))
  380. step_current_down(stepperU);
  381. #endif
  382. #if AXIS_IS_TMC(V)
  383. if (monitor_tmc_driver(stepperV, need_update_error_counters, need_debug_reporting))
  384. step_current_down(stepperV);
  385. #endif
  386. #if AXIS_IS_TMC(W)
  387. if (monitor_tmc_driver(stepperW, need_update_error_counters, need_debug_reporting))
  388. step_current_down(stepperW);
  389. #endif
  390. #if AXIS_IS_TMC(E0)
  391. (void)monitor_tmc_driver(stepperE0, need_update_error_counters, need_debug_reporting);
  392. #endif
  393. #if AXIS_IS_TMC(E1)
  394. (void)monitor_tmc_driver(stepperE1, need_update_error_counters, need_debug_reporting);
  395. #endif
  396. #if AXIS_IS_TMC(E2)
  397. (void)monitor_tmc_driver(stepperE2, need_update_error_counters, need_debug_reporting);
  398. #endif
  399. #if AXIS_IS_TMC(E3)
  400. (void)monitor_tmc_driver(stepperE3, need_update_error_counters, need_debug_reporting);
  401. #endif
  402. #if AXIS_IS_TMC(E4)
  403. (void)monitor_tmc_driver(stepperE4, need_update_error_counters, need_debug_reporting);
  404. #endif
  405. #if AXIS_IS_TMC(E5)
  406. (void)monitor_tmc_driver(stepperE5, need_update_error_counters, need_debug_reporting);
  407. #endif
  408. #if AXIS_IS_TMC(E6)
  409. (void)monitor_tmc_driver(stepperE6, need_update_error_counters, need_debug_reporting);
  410. #endif
  411. #if AXIS_IS_TMC(E7)
  412. (void)monitor_tmc_driver(stepperE7, need_update_error_counters, need_debug_reporting);
  413. #endif
  414. if (TERN0(TMC_DEBUG, need_debug_reporting)) SERIAL_EOL();
  415. }
  416. }
  417. #endif // MONITOR_DRIVER_STATUS
  418. #if ENABLED(TMC_DEBUG)
  419. /**
  420. * M122 [S<0|1>] [Pnnn] Enable periodic status reports
  421. */
  422. #if ENABLED(MONITOR_DRIVER_STATUS)
  423. void tmc_set_report_interval(const uint16_t update_interval) {
  424. if ((report_tmc_status_interval = update_interval))
  425. SERIAL_ECHOLNPGM("axis:pwm_scale"
  426. TERN_(HAS_STEALTHCHOP, "/curr_scale")
  427. TERN_(HAS_STALLGUARD, "/mech_load")
  428. "|flags|warncount"
  429. );
  430. }
  431. #endif
  432. enum TMC_debug_enum : char {
  433. TMC_CODES,
  434. TMC_UART_ADDR,
  435. TMC_ENABLED,
  436. TMC_CURRENT,
  437. TMC_RMS_CURRENT,
  438. TMC_MAX_CURRENT,
  439. TMC_IRUN,
  440. TMC_IHOLD,
  441. TMC_GLOBAL_SCALER,
  442. TMC_CS_ACTUAL,
  443. TMC_PWM_SCALE,
  444. TMC_PWM_SCALE_SUM,
  445. TMC_PWM_SCALE_AUTO,
  446. TMC_PWM_OFS_AUTO,
  447. TMC_PWM_GRAD_AUTO,
  448. TMC_VSENSE,
  449. TMC_STEALTHCHOP,
  450. TMC_MICROSTEPS,
  451. TMC_TSTEP,
  452. TMC_TPWMTHRS,
  453. TMC_TPWMTHRS_MMS,
  454. TMC_OTPW,
  455. TMC_OTPW_TRIGGERED,
  456. TMC_TOFF,
  457. TMC_TBL,
  458. TMC_HEND,
  459. TMC_HSTRT,
  460. TMC_SGT,
  461. TMC_MSCNT,
  462. TMC_INTERPOLATE
  463. };
  464. enum TMC_drv_status_enum : char {
  465. TMC_DRV_CODES,
  466. TMC_STST,
  467. TMC_OLB,
  468. TMC_OLA,
  469. TMC_S2GB,
  470. TMC_S2GA,
  471. TMC_DRV_OTPW,
  472. TMC_OT,
  473. TMC_STALLGUARD,
  474. TMC_DRV_CS_ACTUAL,
  475. TMC_FSACTIVE,
  476. TMC_SG_RESULT,
  477. TMC_DRV_STATUS_HEX,
  478. TMC_T157,
  479. TMC_T150,
  480. TMC_T143,
  481. TMC_T120,
  482. TMC_STEALTH,
  483. TMC_S2VSB,
  484. TMC_S2VSA
  485. };
  486. enum TMC_get_registers_enum : char {
  487. TMC_AXIS_CODES,
  488. TMC_GET_GCONF,
  489. TMC_GET_IHOLD_IRUN,
  490. TMC_GET_GSTAT,
  491. TMC_GET_IOIN,
  492. TMC_GET_TPOWERDOWN,
  493. TMC_GET_TSTEP,
  494. TMC_GET_TPWMTHRS,
  495. TMC_GET_TCOOLTHRS,
  496. TMC_GET_THIGH,
  497. TMC_GET_CHOPCONF,
  498. TMC_GET_COOLCONF,
  499. TMC_GET_PWMCONF,
  500. TMC_GET_PWM_SCALE,
  501. TMC_GET_DRV_STATUS,
  502. TMC_GET_DRVCONF,
  503. TMC_GET_DRVCTRL,
  504. TMC_GET_DRVSTATUS,
  505. TMC_GET_SGCSCONF,
  506. TMC_GET_SMARTEN
  507. };
  508. template<class TMC>
  509. static void print_vsense(TMC &st) { SERIAL_ECHOF(st.vsense() ? F("1=.18") : F("0=.325")); }
  510. #if HAS_DRIVER(TMC2130) || HAS_DRIVER(TMC5130)
  511. static void _tmc_status(TMC2130Stepper &st, const TMC_debug_enum i) {
  512. switch (i) {
  513. case TMC_PWM_SCALE: SERIAL_ECHO(st.PWM_SCALE()); break;
  514. case TMC_SGT: SERIAL_ECHO(st.sgt()); break;
  515. case TMC_STEALTHCHOP: serialprint_truefalse(st.en_pwm_mode()); break;
  516. case TMC_INTERPOLATE: serialprint_truefalse(st.intpol()); break;
  517. default: break;
  518. }
  519. }
  520. #endif
  521. #if HAS_TMCX1X0
  522. static void _tmc_parse_drv_status(TMC2130Stepper &st, const TMC_drv_status_enum i) {
  523. switch (i) {
  524. case TMC_STALLGUARD: if (st.stallguard()) SERIAL_CHAR('*'); break;
  525. case TMC_SG_RESULT: SERIAL_ECHO(st.sg_result()); break;
  526. case TMC_FSACTIVE: if (st.fsactive()) SERIAL_CHAR('*'); break;
  527. case TMC_DRV_CS_ACTUAL: SERIAL_ECHO(st.cs_actual()); break;
  528. default: break;
  529. }
  530. }
  531. #endif
  532. #if HAS_DRIVER(TMC2160) || HAS_DRIVER(TMC5160)
  533. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  534. void print_vsense(TMCMarlin<TMC2160Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &) { }
  535. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  536. void print_vsense(TMCMarlin<TMC5160Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &) { }
  537. static void _tmc_status(TMC2160Stepper &st, const TMC_debug_enum i) {
  538. switch (i) {
  539. case TMC_PWM_SCALE: SERIAL_ECHO(st.PWM_SCALE()); break;
  540. case TMC_SGT: SERIAL_ECHO(st.sgt()); break;
  541. case TMC_STEALTHCHOP: serialprint_truefalse(st.en_pwm_mode()); break;
  542. case TMC_GLOBAL_SCALER:
  543. {
  544. uint16_t value = st.GLOBAL_SCALER();
  545. SERIAL_ECHO(value ? value : 256);
  546. SERIAL_ECHOPGM("/256");
  547. }
  548. break;
  549. case TMC_INTERPOLATE: serialprint_truefalse(st.intpol()); break;
  550. default: break;
  551. }
  552. }
  553. #endif
  554. #if HAS_TMC220x
  555. static void _tmc_status(TMC2208Stepper &st, const TMC_debug_enum i) {
  556. switch (i) {
  557. case TMC_PWM_SCALE_SUM: SERIAL_ECHO(st.pwm_scale_sum()); break;
  558. case TMC_PWM_SCALE_AUTO: SERIAL_ECHO(st.pwm_scale_auto()); break;
  559. case TMC_PWM_OFS_AUTO: SERIAL_ECHO(st.pwm_ofs_auto()); break;
  560. case TMC_PWM_GRAD_AUTO: SERIAL_ECHO(st.pwm_grad_auto()); break;
  561. case TMC_STEALTHCHOP: serialprint_truefalse(st.stealth()); break;
  562. case TMC_INTERPOLATE: serialprint_truefalse(st.intpol()); break;
  563. default: break;
  564. }
  565. }
  566. #if HAS_DRIVER(TMC2209)
  567. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  568. static void _tmc_status(TMCMarlin<TMC2209Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const TMC_debug_enum i) {
  569. switch (i) {
  570. case TMC_SGT: SERIAL_ECHO(st.SGTHRS()); break;
  571. case TMC_UART_ADDR: SERIAL_ECHO(st.get_address()); break;
  572. default:
  573. TMC2208Stepper *parent = &st;
  574. _tmc_status(*parent, i);
  575. break;
  576. }
  577. }
  578. #endif
  579. static void _tmc_parse_drv_status(TMC2208Stepper &st, const TMC_drv_status_enum i) {
  580. switch (i) {
  581. case TMC_T157: if (st.t157()) SERIAL_CHAR('*'); break;
  582. case TMC_T150: if (st.t150()) SERIAL_CHAR('*'); break;
  583. case TMC_T143: if (st.t143()) SERIAL_CHAR('*'); break;
  584. case TMC_T120: if (st.t120()) SERIAL_CHAR('*'); break;
  585. case TMC_S2VSA: if (st.s2vsa()) SERIAL_CHAR('*'); break;
  586. case TMC_S2VSB: if (st.s2vsb()) SERIAL_CHAR('*'); break;
  587. case TMC_DRV_CS_ACTUAL: SERIAL_ECHO(st.cs_actual()); break;
  588. default: break;
  589. }
  590. }
  591. #if HAS_DRIVER(TMC2209)
  592. static void _tmc_parse_drv_status(TMC2209Stepper &st, const TMC_drv_status_enum i) {
  593. switch (i) {
  594. case TMC_SG_RESULT: SERIAL_ECHO(st.SG_RESULT()); break;
  595. default: _tmc_parse_drv_status(static_cast<TMC2208Stepper &>(st), i); break;
  596. }
  597. }
  598. #endif
  599. #endif
  600. #if HAS_DRIVER(TMC2660)
  601. static void _tmc_parse_drv_status(TMC2660Stepper, const TMC_drv_status_enum) { }
  602. static void _tmc_status(TMC2660Stepper &st, const TMC_debug_enum i) {
  603. switch (i) {
  604. case TMC_INTERPOLATE: serialprint_truefalse(st.intpol()); break;
  605. default: break;
  606. }
  607. }
  608. #endif
  609. template <typename TMC>
  610. static void tmc_status(TMC &st, const TMC_debug_enum i) {
  611. SERIAL_CHAR('\t');
  612. switch (i) {
  613. case TMC_CODES: st.printLabel(); break;
  614. case TMC_ENABLED: serialprint_truefalse(st.isEnabled()); break;
  615. case TMC_CURRENT: SERIAL_ECHO(st.getMilliamps()); break;
  616. case TMC_RMS_CURRENT: SERIAL_ECHO(st.rms_current()); break;
  617. case TMC_MAX_CURRENT: SERIAL_PRINT((float)st.rms_current() * 1.41, 0); break;
  618. case TMC_IRUN:
  619. SERIAL_ECHO(st.irun());
  620. SERIAL_ECHOPGM("/31");
  621. break;
  622. case TMC_IHOLD:
  623. SERIAL_ECHO(st.ihold());
  624. SERIAL_ECHOPGM("/31");
  625. break;
  626. case TMC_CS_ACTUAL:
  627. SERIAL_ECHO(st.cs_actual());
  628. SERIAL_ECHOPGM("/31");
  629. break;
  630. case TMC_VSENSE: print_vsense(st); break;
  631. case TMC_MICROSTEPS: SERIAL_ECHO(st.microsteps()); break;
  632. case TMC_TSTEP: {
  633. const uint32_t tstep_value = st.TSTEP();
  634. if (tstep_value != 0xFFFFF) SERIAL_ECHO(tstep_value); else SERIAL_ECHOPGM("max");
  635. } break;
  636. #if ENABLED(HYBRID_THRESHOLD)
  637. case TMC_TPWMTHRS: SERIAL_ECHO(uint32_t(st.TPWMTHRS())); break;
  638. case TMC_TPWMTHRS_MMS: {
  639. const uint32_t tpwmthrs_val = st.get_pwm_thrs();
  640. if (tpwmthrs_val) SERIAL_ECHO(tpwmthrs_val); else SERIAL_CHAR('-');
  641. } break;
  642. #endif
  643. case TMC_OTPW: serialprint_truefalse(st.otpw()); break;
  644. #if ENABLED(MONITOR_DRIVER_STATUS)
  645. case TMC_OTPW_TRIGGERED: serialprint_truefalse(st.getOTPW()); break;
  646. #endif
  647. case TMC_TOFF: SERIAL_ECHO(st.toff()); break;
  648. case TMC_TBL: SERIAL_ECHO(st.blank_time()); break;
  649. case TMC_HEND: SERIAL_ECHO(st.hysteresis_end()); break;
  650. case TMC_HSTRT: SERIAL_ECHO(st.hysteresis_start()); break;
  651. case TMC_MSCNT: SERIAL_ECHO(st.get_microstep_counter()); break;
  652. default: _tmc_status(st, i); break;
  653. }
  654. }
  655. #if HAS_DRIVER(TMC2660)
  656. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  657. void tmc_status(TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const TMC_debug_enum i) {
  658. SERIAL_CHAR('\t');
  659. switch (i) {
  660. case TMC_CODES: st.printLabel(); break;
  661. case TMC_ENABLED: serialprint_truefalse(st.isEnabled()); break;
  662. case TMC_CURRENT: SERIAL_ECHO(st.getMilliamps()); break;
  663. case TMC_RMS_CURRENT: SERIAL_ECHO(st.rms_current()); break;
  664. case TMC_MAX_CURRENT: SERIAL_PRINT((float)st.rms_current() * 1.41, 0); break;
  665. case TMC_IRUN:
  666. SERIAL_ECHO(st.cs());
  667. SERIAL_ECHOPGM("/31");
  668. break;
  669. case TMC_VSENSE: SERIAL_ECHOF(st.vsense() ? F("1=.165") : F("0=.310")); break;
  670. case TMC_MICROSTEPS: SERIAL_ECHO(st.microsteps()); break;
  671. //case TMC_OTPW: serialprint_truefalse(st.otpw()); break;
  672. //case TMC_OTPW_TRIGGERED: serialprint_truefalse(st.getOTPW()); break;
  673. case TMC_SGT: SERIAL_ECHO(st.sgt()); break;
  674. case TMC_TOFF: SERIAL_ECHO(st.toff()); break;
  675. case TMC_TBL: SERIAL_ECHO(st.blank_time()); break;
  676. case TMC_HEND: SERIAL_ECHO(st.hysteresis_end()); break;
  677. case TMC_HSTRT: SERIAL_ECHO(st.hysteresis_start()); break;
  678. default: break;
  679. }
  680. }
  681. #endif
  682. template <typename TMC>
  683. static void tmc_parse_drv_status(TMC &st, const TMC_drv_status_enum i) {
  684. SERIAL_CHAR('\t');
  685. switch (i) {
  686. case TMC_DRV_CODES: st.printLabel(); break;
  687. case TMC_STST: if (!st.stst()) SERIAL_CHAR('*'); break;
  688. case TMC_OLB: if (st.olb()) SERIAL_CHAR('*'); break;
  689. case TMC_OLA: if (st.ola()) SERIAL_CHAR('*'); break;
  690. case TMC_S2GB: if (st.s2gb()) SERIAL_CHAR('*'); break;
  691. case TMC_S2GA: if (st.s2ga()) SERIAL_CHAR('*'); break;
  692. case TMC_DRV_OTPW: if (st.otpw()) SERIAL_CHAR('*'); break;
  693. case TMC_OT: if (st.ot()) SERIAL_CHAR('*'); break;
  694. case TMC_DRV_STATUS_HEX: {
  695. const uint32_t drv_status = st.DRV_STATUS();
  696. SERIAL_CHAR('\t');
  697. st.printLabel();
  698. SERIAL_CHAR('\t');
  699. print_hex_long(drv_status, ':');
  700. if (drv_status == 0xFFFFFFFF || drv_status == 0) SERIAL_ECHOPGM("\t Bad response!");
  701. SERIAL_EOL();
  702. break;
  703. }
  704. default: _tmc_parse_drv_status(st, i); break;
  705. }
  706. }
  707. static void tmc_debug_loop(const TMC_debug_enum n, LOGICAL_AXIS_ARGS(const bool)) {
  708. if (x) {
  709. #if AXIS_IS_TMC(X)
  710. tmc_status(stepperX, n);
  711. #endif
  712. #if AXIS_IS_TMC(X2)
  713. tmc_status(stepperX2, n);
  714. #endif
  715. }
  716. if (TERN0(HAS_Y_AXIS, y)) {
  717. #if AXIS_IS_TMC(Y)
  718. tmc_status(stepperY, n);
  719. #endif
  720. #if AXIS_IS_TMC(Y2)
  721. tmc_status(stepperY2, n);
  722. #endif
  723. }
  724. if (TERN0(HAS_Z_AXIS, z)) {
  725. #if AXIS_IS_TMC(Z)
  726. tmc_status(stepperZ, n);
  727. #endif
  728. #if AXIS_IS_TMC(Z2)
  729. tmc_status(stepperZ2, n);
  730. #endif
  731. #if AXIS_IS_TMC(Z3)
  732. tmc_status(stepperZ3, n);
  733. #endif
  734. #if AXIS_IS_TMC(Z4)
  735. tmc_status(stepperZ4, n);
  736. #endif
  737. }
  738. #if AXIS_IS_TMC(I)
  739. if (i) tmc_status(stepperI, n);
  740. #endif
  741. #if AXIS_IS_TMC(J)
  742. if (j) tmc_status(stepperJ, n);
  743. #endif
  744. #if AXIS_IS_TMC(K)
  745. if (k) tmc_status(stepperK, n);
  746. #endif
  747. #if AXIS_IS_TMC(U)
  748. if (u) tmc_status(stepperU, n);
  749. #endif
  750. #if AXIS_IS_TMC(V)
  751. if (v) tmc_status(stepperV, n);
  752. #endif
  753. #if AXIS_IS_TMC(W)
  754. if (w) tmc_status(stepperW, n);
  755. #endif
  756. if (TERN0(HAS_EXTRUDERS, e)) {
  757. #if AXIS_IS_TMC(E0)
  758. tmc_status(stepperE0, n);
  759. #endif
  760. #if AXIS_IS_TMC(E1)
  761. tmc_status(stepperE1, n);
  762. #endif
  763. #if AXIS_IS_TMC(E2)
  764. tmc_status(stepperE2, n);
  765. #endif
  766. #if AXIS_IS_TMC(E3)
  767. tmc_status(stepperE3, n);
  768. #endif
  769. #if AXIS_IS_TMC(E4)
  770. tmc_status(stepperE4, n);
  771. #endif
  772. #if AXIS_IS_TMC(E5)
  773. tmc_status(stepperE5, n);
  774. #endif
  775. #if AXIS_IS_TMC(E6)
  776. tmc_status(stepperE6, n);
  777. #endif
  778. #if AXIS_IS_TMC(E7)
  779. tmc_status(stepperE7, n);
  780. #endif
  781. }
  782. SERIAL_EOL();
  783. }
  784. static void drv_status_loop(const TMC_drv_status_enum n, LOGICAL_AXIS_ARGS(const bool)) {
  785. if (x) {
  786. #if AXIS_IS_TMC(X)
  787. tmc_parse_drv_status(stepperX, n);
  788. #endif
  789. #if AXIS_IS_TMC(X2)
  790. tmc_parse_drv_status(stepperX2, n);
  791. #endif
  792. }
  793. if (TERN0(HAS_Y_AXIS, y)) {
  794. #if AXIS_IS_TMC(Y)
  795. tmc_parse_drv_status(stepperY, n);
  796. #endif
  797. #if AXIS_IS_TMC(Y2)
  798. tmc_parse_drv_status(stepperY2, n);
  799. #endif
  800. }
  801. if (TERN0(HAS_Z_AXIS, z)) {
  802. #if AXIS_IS_TMC(Z)
  803. tmc_parse_drv_status(stepperZ, n);
  804. #endif
  805. #if AXIS_IS_TMC(Z2)
  806. tmc_parse_drv_status(stepperZ2, n);
  807. #endif
  808. #if AXIS_IS_TMC(Z3)
  809. tmc_parse_drv_status(stepperZ3, n);
  810. #endif
  811. #if AXIS_IS_TMC(Z4)
  812. tmc_parse_drv_status(stepperZ4, n);
  813. #endif
  814. }
  815. #if AXIS_IS_TMC(I)
  816. if (i) tmc_parse_drv_status(stepperI, n);
  817. #endif
  818. #if AXIS_IS_TMC(J)
  819. if (j) tmc_parse_drv_status(stepperJ, n);
  820. #endif
  821. #if AXIS_IS_TMC(K)
  822. if (k) tmc_parse_drv_status(stepperK, n);
  823. #endif
  824. #if AXIS_IS_TMC(U)
  825. if (u) tmc_parse_drv_status(stepperU, n);
  826. #endif
  827. #if AXIS_IS_TMC(V)
  828. if (v) tmc_parse_drv_status(stepperV, n);
  829. #endif
  830. #if AXIS_IS_TMC(W)
  831. if (w) tmc_parse_drv_status(stepperW, n);
  832. #endif
  833. if (TERN0(HAS_EXTRUDERS, e)) {
  834. #if AXIS_IS_TMC(E0)
  835. tmc_parse_drv_status(stepperE0, n);
  836. #endif
  837. #if AXIS_IS_TMC(E1)
  838. tmc_parse_drv_status(stepperE1, n);
  839. #endif
  840. #if AXIS_IS_TMC(E2)
  841. tmc_parse_drv_status(stepperE2, n);
  842. #endif
  843. #if AXIS_IS_TMC(E3)
  844. tmc_parse_drv_status(stepperE3, n);
  845. #endif
  846. #if AXIS_IS_TMC(E4)
  847. tmc_parse_drv_status(stepperE4, n);
  848. #endif
  849. #if AXIS_IS_TMC(E5)
  850. tmc_parse_drv_status(stepperE5, n);
  851. #endif
  852. #if AXIS_IS_TMC(E6)
  853. tmc_parse_drv_status(stepperE6, n);
  854. #endif
  855. #if AXIS_IS_TMC(E7)
  856. tmc_parse_drv_status(stepperE7, n);
  857. #endif
  858. }
  859. SERIAL_EOL();
  860. }
  861. /**
  862. * M122 report functions
  863. */
  864. void tmc_report_all(LOGICAL_AXIS_ARGS(const bool)) {
  865. #define TMC_REPORT(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); tmc_debug_loop(ITEM, LOGICAL_AXIS_ARGS()); }while(0)
  866. #define DRV_REPORT(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); drv_status_loop(ITEM, LOGICAL_AXIS_ARGS()); }while(0)
  867. TMC_REPORT("\t", TMC_CODES);
  868. #if HAS_DRIVER(TMC2209)
  869. TMC_REPORT("Address\t", TMC_UART_ADDR);
  870. #endif
  871. TMC_REPORT("Enabled\t", TMC_ENABLED);
  872. TMC_REPORT("Set current", TMC_CURRENT);
  873. TMC_REPORT("RMS current", TMC_RMS_CURRENT);
  874. TMC_REPORT("MAX current", TMC_MAX_CURRENT);
  875. TMC_REPORT("Run current", TMC_IRUN);
  876. TMC_REPORT("Hold current", TMC_IHOLD);
  877. #if HAS_DRIVER(TMC2160) || HAS_DRIVER(TMC5160)
  878. TMC_REPORT("Global scaler", TMC_GLOBAL_SCALER);
  879. #endif
  880. TMC_REPORT("CS actual", TMC_CS_ACTUAL);
  881. TMC_REPORT("PWM scale", TMC_PWM_SCALE);
  882. #if HAS_DRIVER(TMC2130) || HAS_DRIVER(TMC2224) || HAS_DRIVER(TMC2660) || HAS_TMC220x
  883. TMC_REPORT("vsense\t", TMC_VSENSE);
  884. #endif
  885. TMC_REPORT("stealthChop", TMC_STEALTHCHOP);
  886. TMC_REPORT("msteps\t", TMC_MICROSTEPS);
  887. TMC_REPORT("interp\t", TMC_INTERPOLATE);
  888. TMC_REPORT("tstep\t", TMC_TSTEP);
  889. TMC_REPORT("PWM thresh.", TMC_TPWMTHRS);
  890. TMC_REPORT("[mm/s]\t", TMC_TPWMTHRS_MMS);
  891. TMC_REPORT("OT prewarn", TMC_OTPW);
  892. #if ENABLED(MONITOR_DRIVER_STATUS)
  893. TMC_REPORT("triggered\n OTP\t", TMC_OTPW_TRIGGERED);
  894. #endif
  895. #if HAS_TMC220x
  896. TMC_REPORT("pwm scale sum", TMC_PWM_SCALE_SUM);
  897. TMC_REPORT("pwm scale auto", TMC_PWM_SCALE_AUTO);
  898. TMC_REPORT("pwm offset auto", TMC_PWM_OFS_AUTO);
  899. TMC_REPORT("pwm grad auto", TMC_PWM_GRAD_AUTO);
  900. #endif
  901. TMC_REPORT("off time", TMC_TOFF);
  902. TMC_REPORT("blank time", TMC_TBL);
  903. TMC_REPORT("hysteresis\n -end\t", TMC_HEND);
  904. TMC_REPORT(" -start\t", TMC_HSTRT);
  905. TMC_REPORT("Stallguard thrs", TMC_SGT);
  906. TMC_REPORT("uStep count", TMC_MSCNT);
  907. DRV_REPORT("DRVSTATUS", TMC_DRV_CODES);
  908. #if HAS_TMCX1X0 || HAS_TMC220x
  909. DRV_REPORT("sg_result", TMC_SG_RESULT);
  910. #endif
  911. #if HAS_TMCX1X0
  912. DRV_REPORT("stallguard", TMC_STALLGUARD);
  913. DRV_REPORT("fsactive", TMC_FSACTIVE);
  914. #endif
  915. DRV_REPORT("stst\t", TMC_STST);
  916. DRV_REPORT("olb\t", TMC_OLB);
  917. DRV_REPORT("ola\t", TMC_OLA);
  918. DRV_REPORT("s2gb\t", TMC_S2GB);
  919. DRV_REPORT("s2ga\t", TMC_S2GA);
  920. DRV_REPORT("otpw\t", TMC_DRV_OTPW);
  921. DRV_REPORT("ot\t", TMC_OT);
  922. #if HAS_TMC220x
  923. DRV_REPORT("157C\t", TMC_T157);
  924. DRV_REPORT("150C\t", TMC_T150);
  925. DRV_REPORT("143C\t", TMC_T143);
  926. DRV_REPORT("120C\t", TMC_T120);
  927. DRV_REPORT("s2vsa\t", TMC_S2VSA);
  928. DRV_REPORT("s2vsb\t", TMC_S2VSB);
  929. #endif
  930. DRV_REPORT("Driver registers:\n",TMC_DRV_STATUS_HEX);
  931. SERIAL_EOL();
  932. }
  933. #define PRINT_TMC_REGISTER(REG_CASE) case TMC_GET_##REG_CASE: print_hex_long(st.REG_CASE(), ':'); break
  934. #if HAS_TMCX1X0
  935. static void tmc_get_ic_registers(TMC2130Stepper &st, const TMC_get_registers_enum i) {
  936. switch (i) {
  937. PRINT_TMC_REGISTER(TCOOLTHRS);
  938. PRINT_TMC_REGISTER(THIGH);
  939. PRINT_TMC_REGISTER(COOLCONF);
  940. default: SERIAL_CHAR('\t'); break;
  941. }
  942. }
  943. #endif
  944. #if HAS_TMC220x
  945. static void tmc_get_ic_registers(TMC2208Stepper, const TMC_get_registers_enum) { SERIAL_CHAR('\t'); }
  946. #endif
  947. #if HAS_TRINAMIC_CONFIG
  948. template<class TMC>
  949. static void tmc_get_registers(TMC &st, const TMC_get_registers_enum i) {
  950. switch (i) {
  951. case TMC_AXIS_CODES: SERIAL_CHAR('\t'); st.printLabel(); break;
  952. PRINT_TMC_REGISTER(GCONF);
  953. PRINT_TMC_REGISTER(IHOLD_IRUN);
  954. PRINT_TMC_REGISTER(GSTAT);
  955. PRINT_TMC_REGISTER(IOIN);
  956. PRINT_TMC_REGISTER(TPOWERDOWN);
  957. PRINT_TMC_REGISTER(TSTEP);
  958. PRINT_TMC_REGISTER(TPWMTHRS);
  959. PRINT_TMC_REGISTER(CHOPCONF);
  960. PRINT_TMC_REGISTER(PWMCONF);
  961. PRINT_TMC_REGISTER(PWM_SCALE);
  962. PRINT_TMC_REGISTER(DRV_STATUS);
  963. default: tmc_get_ic_registers(st, i); break;
  964. }
  965. SERIAL_CHAR('\t');
  966. }
  967. #endif
  968. #if HAS_DRIVER(TMC2660)
  969. template <char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  970. static void tmc_get_registers(TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const TMC_get_registers_enum i) {
  971. switch (i) {
  972. case TMC_AXIS_CODES: SERIAL_CHAR('\t'); st.printLabel(); break;
  973. PRINT_TMC_REGISTER(DRVCONF);
  974. PRINT_TMC_REGISTER(DRVCTRL);
  975. PRINT_TMC_REGISTER(CHOPCONF);
  976. PRINT_TMC_REGISTER(DRVSTATUS);
  977. PRINT_TMC_REGISTER(SGCSCONF);
  978. PRINT_TMC_REGISTER(SMARTEN);
  979. default: SERIAL_CHAR('\t'); break;
  980. }
  981. SERIAL_CHAR('\t');
  982. }
  983. #endif
  984. static void tmc_get_registers(TMC_get_registers_enum n, LOGICAL_AXIS_ARGS(const bool)) {
  985. if (x) {
  986. #if AXIS_IS_TMC(X)
  987. tmc_get_registers(stepperX, n);
  988. #endif
  989. #if AXIS_IS_TMC(X2)
  990. tmc_get_registers(stepperX2, n);
  991. #endif
  992. }
  993. if (TERN0(HAS_Y_AXIS, y)) {
  994. #if AXIS_IS_TMC(Y)
  995. tmc_get_registers(stepperY, n);
  996. #endif
  997. #if AXIS_IS_TMC(Y2)
  998. tmc_get_registers(stepperY2, n);
  999. #endif
  1000. }
  1001. if (TERN0(HAS_Z_AXIS, z)) {
  1002. #if AXIS_IS_TMC(Z)
  1003. tmc_get_registers(stepperZ, n);
  1004. #endif
  1005. #if AXIS_IS_TMC(Z2)
  1006. tmc_get_registers(stepperZ2, n);
  1007. #endif
  1008. #if AXIS_IS_TMC(Z3)
  1009. tmc_get_registers(stepperZ3, n);
  1010. #endif
  1011. #if AXIS_IS_TMC(Z4)
  1012. tmc_get_registers(stepperZ4, n);
  1013. #endif
  1014. }
  1015. #if AXIS_IS_TMC(I)
  1016. if (i) tmc_get_registers(stepperI, n);
  1017. #endif
  1018. #if AXIS_IS_TMC(J)
  1019. if (j) tmc_get_registers(stepperJ, n);
  1020. #endif
  1021. #if AXIS_IS_TMC(K)
  1022. if (k) tmc_get_registers(stepperK, n);
  1023. #endif
  1024. #if AXIS_IS_TMC(U)
  1025. if (u) tmc_get_registers(stepperU, n);
  1026. #endif
  1027. #if AXIS_IS_TMC(V)
  1028. if (v) tmc_get_registers(stepperV, n);
  1029. #endif
  1030. #if AXIS_IS_TMC(W)
  1031. if (w) tmc_get_registers(stepperW, n);
  1032. #endif
  1033. if (TERN0(HAS_EXTRUDERS, e)) {
  1034. #if AXIS_IS_TMC(E0)
  1035. tmc_get_registers(stepperE0, n);
  1036. #endif
  1037. #if AXIS_IS_TMC(E1)
  1038. tmc_get_registers(stepperE1, n);
  1039. #endif
  1040. #if AXIS_IS_TMC(E2)
  1041. tmc_get_registers(stepperE2, n);
  1042. #endif
  1043. #if AXIS_IS_TMC(E3)
  1044. tmc_get_registers(stepperE3, n);
  1045. #endif
  1046. #if AXIS_IS_TMC(E4)
  1047. tmc_get_registers(stepperE4, n);
  1048. #endif
  1049. #if AXIS_IS_TMC(E5)
  1050. tmc_get_registers(stepperE5, n);
  1051. #endif
  1052. #if AXIS_IS_TMC(E6)
  1053. tmc_get_registers(stepperE6, n);
  1054. #endif
  1055. #if AXIS_IS_TMC(E7)
  1056. tmc_get_registers(stepperE7, n);
  1057. #endif
  1058. }
  1059. SERIAL_EOL();
  1060. }
  1061. void tmc_get_registers(LOGICAL_AXIS_ARGS(bool)) {
  1062. #define _TMC_GET_REG(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); tmc_get_registers(ITEM, LOGICAL_AXIS_ARGS()); }while(0)
  1063. #define TMC_GET_REG(NAME, TABS) _TMC_GET_REG(STRINGIFY(NAME) TABS, TMC_GET_##NAME)
  1064. _TMC_GET_REG("\t", TMC_AXIS_CODES);
  1065. TMC_GET_REG(GCONF, "\t\t");
  1066. TMC_GET_REG(IHOLD_IRUN, "\t");
  1067. TMC_GET_REG(GSTAT, "\t\t");
  1068. TMC_GET_REG(IOIN, "\t\t");
  1069. TMC_GET_REG(TPOWERDOWN, "\t");
  1070. TMC_GET_REG(TSTEP, "\t\t");
  1071. TMC_GET_REG(TPWMTHRS, "\t");
  1072. TMC_GET_REG(TCOOLTHRS, "\t");
  1073. TMC_GET_REG(THIGH, "\t\t");
  1074. TMC_GET_REG(CHOPCONF, "\t");
  1075. TMC_GET_REG(COOLCONF, "\t");
  1076. TMC_GET_REG(PWMCONF, "\t");
  1077. TMC_GET_REG(PWM_SCALE, "\t");
  1078. TMC_GET_REG(DRV_STATUS, "\t");
  1079. }
  1080. #endif // TMC_DEBUG
  1081. #if USE_SENSORLESS
  1082. bool tmc_enable_stallguard(TMC2130Stepper &st) {
  1083. const bool stealthchop_was_enabled = st.en_pwm_mode();
  1084. st.TCOOLTHRS(0xFFFFF);
  1085. st.en_pwm_mode(false);
  1086. st.diag1_stall(true);
  1087. return stealthchop_was_enabled;
  1088. }
  1089. void tmc_disable_stallguard(TMC2130Stepper &st, const bool restore_stealth) {
  1090. st.TCOOLTHRS(0);
  1091. st.en_pwm_mode(restore_stealth);
  1092. st.diag1_stall(false);
  1093. }
  1094. bool tmc_enable_stallguard(TMC2209Stepper &st) {
  1095. const bool stealthchop_was_enabled = !st.en_spreadCycle();
  1096. st.TCOOLTHRS(0xFFFFF);
  1097. st.en_spreadCycle(false);
  1098. return stealthchop_was_enabled;
  1099. }
  1100. void tmc_disable_stallguard(TMC2209Stepper &st, const bool restore_stealth) {
  1101. st.en_spreadCycle(!restore_stealth);
  1102. st.TCOOLTHRS(0);
  1103. }
  1104. bool tmc_enable_stallguard(TMC2660Stepper) {
  1105. // TODO
  1106. return false;
  1107. }
  1108. void tmc_disable_stallguard(TMC2660Stepper, const bool) {};
  1109. #endif // USE_SENSORLESS
  1110. template<typename TMC>
  1111. static bool test_connection(TMC &st) {
  1112. SERIAL_ECHOPGM("Testing ");
  1113. st.printLabel();
  1114. SERIAL_ECHOPGM(" connection... ");
  1115. const uint8_t test_result = st.test_connection();
  1116. if (test_result > 0) SERIAL_ECHOPGM("Error: All ");
  1117. FSTR_P stat;
  1118. switch (test_result) {
  1119. default:
  1120. case 0: stat = F("OK"); break;
  1121. case 1: stat = F("HIGH"); break;
  1122. case 2: stat = F("LOW"); break;
  1123. }
  1124. SERIAL_ECHOLNF(stat);
  1125. return test_result;
  1126. }
  1127. void test_tmc_connection(LOGICAL_AXIS_ARGS(const bool)) {
  1128. uint8_t axis_connection = 0;
  1129. if (x) {
  1130. #if AXIS_IS_TMC(X)
  1131. axis_connection += test_connection(stepperX);
  1132. #endif
  1133. #if AXIS_IS_TMC(X2)
  1134. axis_connection += test_connection(stepperX2);
  1135. #endif
  1136. }
  1137. if (TERN0(HAS_Y_AXIS, y)) {
  1138. #if AXIS_IS_TMC(Y)
  1139. axis_connection += test_connection(stepperY);
  1140. #endif
  1141. #if AXIS_IS_TMC(Y2)
  1142. axis_connection += test_connection(stepperY2);
  1143. #endif
  1144. }
  1145. if (TERN0(HAS_Z_AXIS, z)) {
  1146. #if AXIS_IS_TMC(Z)
  1147. axis_connection += test_connection(stepperZ);
  1148. #endif
  1149. #if AXIS_IS_TMC(Z2)
  1150. axis_connection += test_connection(stepperZ2);
  1151. #endif
  1152. #if AXIS_IS_TMC(Z3)
  1153. axis_connection += test_connection(stepperZ3);
  1154. #endif
  1155. #if AXIS_IS_TMC(Z4)
  1156. axis_connection += test_connection(stepperZ4);
  1157. #endif
  1158. }
  1159. #if AXIS_IS_TMC(I)
  1160. if (i) axis_connection += test_connection(stepperI);
  1161. #endif
  1162. #if AXIS_IS_TMC(J)
  1163. if (j) axis_connection += test_connection(stepperJ);
  1164. #endif
  1165. #if AXIS_IS_TMC(K)
  1166. if (k) axis_connection += test_connection(stepperK);
  1167. #endif
  1168. #if AXIS_IS_TMC(U)
  1169. if (u) axis_connection += test_connection(stepperU);
  1170. #endif
  1171. #if AXIS_IS_TMC(V)
  1172. if (v) axis_connection += test_connection(stepperV);
  1173. #endif
  1174. #if AXIS_IS_TMC(W)
  1175. if (w) axis_connection += test_connection(stepperW);
  1176. #endif
  1177. if (TERN0(HAS_EXTRUDERS, e)) {
  1178. #if AXIS_IS_TMC(E0)
  1179. axis_connection += test_connection(stepperE0);
  1180. #endif
  1181. #if AXIS_IS_TMC(E1)
  1182. axis_connection += test_connection(stepperE1);
  1183. #endif
  1184. #if AXIS_IS_TMC(E2)
  1185. axis_connection += test_connection(stepperE2);
  1186. #endif
  1187. #if AXIS_IS_TMC(E3)
  1188. axis_connection += test_connection(stepperE3);
  1189. #endif
  1190. #if AXIS_IS_TMC(E4)
  1191. axis_connection += test_connection(stepperE4);
  1192. #endif
  1193. #if AXIS_IS_TMC(E5)
  1194. axis_connection += test_connection(stepperE5);
  1195. #endif
  1196. #if AXIS_IS_TMC(E6)
  1197. axis_connection += test_connection(stepperE6);
  1198. #endif
  1199. #if AXIS_IS_TMC(E7)
  1200. axis_connection += test_connection(stepperE7);
  1201. #endif
  1202. }
  1203. if (axis_connection) LCD_MESSAGE(MSG_ERROR_TMC);
  1204. }
  1205. #endif // HAS_TRINAMIC_CONFIG
  1206. #if HAS_TMC_SPI
  1207. #define SET_CS_PIN(st) OUT_WRITE(st##_CS_PIN, HIGH)
  1208. void tmc_init_cs_pins() {
  1209. #if AXIS_HAS_SPI(X)
  1210. SET_CS_PIN(X);
  1211. #endif
  1212. #if AXIS_HAS_SPI(Y)
  1213. SET_CS_PIN(Y);
  1214. #endif
  1215. #if AXIS_HAS_SPI(Z)
  1216. SET_CS_PIN(Z);
  1217. #endif
  1218. #if AXIS_HAS_SPI(X2)
  1219. SET_CS_PIN(X2);
  1220. #endif
  1221. #if AXIS_HAS_SPI(Y2)
  1222. SET_CS_PIN(Y2);
  1223. #endif
  1224. #if AXIS_HAS_SPI(Z2)
  1225. SET_CS_PIN(Z2);
  1226. #endif
  1227. #if AXIS_HAS_SPI(Z3)
  1228. SET_CS_PIN(Z3);
  1229. #endif
  1230. #if AXIS_HAS_SPI(Z4)
  1231. SET_CS_PIN(Z4);
  1232. #endif
  1233. #if AXIS_HAS_SPI(I)
  1234. SET_CS_PIN(I);
  1235. #endif
  1236. #if AXIS_HAS_SPI(J)
  1237. SET_CS_PIN(J);
  1238. #endif
  1239. #if AXIS_HAS_SPI(K)
  1240. SET_CS_PIN(K);
  1241. #endif
  1242. #if AXIS_HAS_SPI(U)
  1243. SET_CS_PIN(U);
  1244. #endif
  1245. #if AXIS_HAS_SPI(V)
  1246. SET_CS_PIN(V);
  1247. #endif
  1248. #if AXIS_HAS_SPI(W)
  1249. SET_CS_PIN(W);
  1250. #endif
  1251. #if AXIS_HAS_SPI(E0)
  1252. SET_CS_PIN(E0);
  1253. #endif
  1254. #if AXIS_HAS_SPI(E1)
  1255. SET_CS_PIN(E1);
  1256. #endif
  1257. #if AXIS_HAS_SPI(E2)
  1258. SET_CS_PIN(E2);
  1259. #endif
  1260. #if AXIS_HAS_SPI(E3)
  1261. SET_CS_PIN(E3);
  1262. #endif
  1263. #if AXIS_HAS_SPI(E4)
  1264. SET_CS_PIN(E4);
  1265. #endif
  1266. #if AXIS_HAS_SPI(E5)
  1267. SET_CS_PIN(E5);
  1268. #endif
  1269. #if AXIS_HAS_SPI(E6)
  1270. SET_CS_PIN(E6);
  1271. #endif
  1272. #if AXIS_HAS_SPI(E7)
  1273. SET_CS_PIN(E7);
  1274. #endif
  1275. }
  1276. #endif // HAS_TMC_SPI