My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

tmc_util.cpp 36KB

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