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.

tmc_util.cpp 37KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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 "../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 "../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) || AXIS_IS_TMC(X2)
  306. {
  307. bool result = false;
  308. #if AXIS_IS_TMC(X)
  309. if (monitor_tmc_driver(stepperX, need_update_error_counters, need_debug_reporting)) result = true;
  310. #endif
  311. #if AXIS_IS_TMC(X2)
  312. if (monitor_tmc_driver(stepperX2, need_update_error_counters, need_debug_reporting)) result = true;
  313. #endif
  314. if (result) {
  315. #if AXIS_IS_TMC(X)
  316. step_current_down(stepperX);
  317. #endif
  318. #if AXIS_IS_TMC(X2)
  319. step_current_down(stepperX2);
  320. #endif
  321. }
  322. }
  323. #endif
  324. #if AXIS_IS_TMC(Y) || AXIS_IS_TMC(Y2)
  325. {
  326. bool result = false;
  327. #if AXIS_IS_TMC(Y)
  328. if (monitor_tmc_driver(stepperY, need_update_error_counters, need_debug_reporting)) result = true;
  329. #endif
  330. #if AXIS_IS_TMC(Y2)
  331. if (monitor_tmc_driver(stepperY2, need_update_error_counters, need_debug_reporting)) result = true;
  332. #endif
  333. if (result) {
  334. #if AXIS_IS_TMC(Y)
  335. step_current_down(stepperY);
  336. #endif
  337. #if AXIS_IS_TMC(Y2)
  338. step_current_down(stepperY2);
  339. #endif
  340. }
  341. }
  342. #endif
  343. #if AXIS_IS_TMC(Z) || AXIS_IS_TMC(Z2) || AXIS_IS_TMC(Z3) || AXIS_IS_TMC(Z4)
  344. {
  345. bool result = false;
  346. #if AXIS_IS_TMC(Z)
  347. if (monitor_tmc_driver(stepperZ, need_update_error_counters, need_debug_reporting)) result = true;
  348. #endif
  349. #if AXIS_IS_TMC(Z2)
  350. if (monitor_tmc_driver(stepperZ2, need_update_error_counters, need_debug_reporting)) result = true;
  351. #endif
  352. #if AXIS_IS_TMC(Z3)
  353. if (monitor_tmc_driver(stepperZ3, need_update_error_counters, need_debug_reporting)) result = true;
  354. #endif
  355. #if AXIS_IS_TMC(Z4)
  356. if (monitor_tmc_driver(stepperZ4, need_update_error_counters, need_debug_reporting)) result = true;
  357. #endif
  358. if (result) {
  359. #if AXIS_IS_TMC(Z)
  360. step_current_down(stepperZ);
  361. #endif
  362. #if AXIS_IS_TMC(Z2)
  363. step_current_down(stepperZ2);
  364. #endif
  365. #if AXIS_IS_TMC(Z3)
  366. step_current_down(stepperZ3);
  367. #endif
  368. #if AXIS_IS_TMC(Z4)
  369. step_current_down(stepperZ4);
  370. #endif
  371. }
  372. }
  373. #endif
  374. #if AXIS_IS_TMC(E0)
  375. (void)monitor_tmc_driver(stepperE0, need_update_error_counters, need_debug_reporting);
  376. #endif
  377. #if AXIS_IS_TMC(E1)
  378. (void)monitor_tmc_driver(stepperE1, need_update_error_counters, need_debug_reporting);
  379. #endif
  380. #if AXIS_IS_TMC(E2)
  381. (void)monitor_tmc_driver(stepperE2, need_update_error_counters, need_debug_reporting);
  382. #endif
  383. #if AXIS_IS_TMC(E3)
  384. (void)monitor_tmc_driver(stepperE3, need_update_error_counters, need_debug_reporting);
  385. #endif
  386. #if AXIS_IS_TMC(E4)
  387. (void)monitor_tmc_driver(stepperE4, need_update_error_counters, need_debug_reporting);
  388. #endif
  389. #if AXIS_IS_TMC(E5)
  390. (void)monitor_tmc_driver(stepperE5, need_update_error_counters, need_debug_reporting);
  391. #endif
  392. #if ENABLED(TMC_DEBUG)
  393. if (need_debug_reporting) SERIAL_EOL();
  394. #endif
  395. }
  396. }
  397. #endif // MONITOR_DRIVER_STATUS
  398. #if ENABLED(TMC_DEBUG)
  399. /**
  400. * M122 [S<0|1>] [Pnnn] Enable periodic status reports
  401. */
  402. #if ENABLED(MONITOR_DRIVER_STATUS)
  403. void tmc_set_report_interval(const uint16_t update_interval) {
  404. if ((report_tmc_status_interval = update_interval))
  405. SERIAL_ECHOLNPGM("axis:pwm_scale"
  406. #if HAS_STEALTHCHOP
  407. "/curr_scale"
  408. #endif
  409. #if HAS_STALLGUARD
  410. "/mech_load"
  411. #endif
  412. "|flags|warncount"
  413. );
  414. }
  415. #endif
  416. enum TMC_debug_enum : char {
  417. TMC_CODES,
  418. TMC_UART_ADDR,
  419. TMC_ENABLED,
  420. TMC_CURRENT,
  421. TMC_RMS_CURRENT,
  422. TMC_MAX_CURRENT,
  423. TMC_IRUN,
  424. TMC_IHOLD,
  425. TMC_GLOBAL_SCALER,
  426. TMC_CS_ACTUAL,
  427. TMC_PWM_SCALE,
  428. TMC_VSENSE,
  429. TMC_STEALTHCHOP,
  430. TMC_MICROSTEPS,
  431. TMC_TSTEP,
  432. TMC_TPWMTHRS,
  433. TMC_TPWMTHRS_MMS,
  434. TMC_OTPW,
  435. TMC_OTPW_TRIGGERED,
  436. TMC_TOFF,
  437. TMC_TBL,
  438. TMC_HEND,
  439. TMC_HSTRT,
  440. TMC_SGT
  441. };
  442. enum TMC_drv_status_enum : char {
  443. TMC_DRV_CODES,
  444. TMC_STST,
  445. TMC_OLB,
  446. TMC_OLA,
  447. TMC_S2GB,
  448. TMC_S2GA,
  449. TMC_DRV_OTPW,
  450. TMC_OT,
  451. TMC_STALLGUARD,
  452. TMC_DRV_CS_ACTUAL,
  453. TMC_FSACTIVE,
  454. TMC_SG_RESULT,
  455. TMC_DRV_STATUS_HEX,
  456. TMC_T157,
  457. TMC_T150,
  458. TMC_T143,
  459. TMC_T120,
  460. TMC_STEALTH,
  461. TMC_S2VSB,
  462. TMC_S2VSA
  463. };
  464. enum TMC_get_registers_enum : char {
  465. TMC_AXIS_CODES,
  466. TMC_GET_GCONF,
  467. TMC_GET_IHOLD_IRUN,
  468. TMC_GET_GSTAT,
  469. TMC_GET_IOIN,
  470. TMC_GET_TPOWERDOWN,
  471. TMC_GET_TSTEP,
  472. TMC_GET_TPWMTHRS,
  473. TMC_GET_TCOOLTHRS,
  474. TMC_GET_THIGH,
  475. TMC_GET_CHOPCONF,
  476. TMC_GET_COOLCONF,
  477. TMC_GET_PWMCONF,
  478. TMC_GET_PWM_SCALE,
  479. TMC_GET_DRV_STATUS,
  480. TMC_GET_DRVCONF,
  481. TMC_GET_DRVCTRL,
  482. TMC_GET_DRVSTATUS,
  483. TMC_GET_SGCSCONF,
  484. TMC_GET_SMARTEN
  485. };
  486. template<class TMC>
  487. static void print_vsense(TMC &st) { serialprintPGM(st.vsense() ? PSTR("1=.18") : PSTR("0=.325")); }
  488. #if HAS_DRIVER(TMC2130) || HAS_DRIVER(TMC5130)
  489. static void _tmc_status(TMC2130Stepper &st, const TMC_debug_enum i) {
  490. switch (i) {
  491. case TMC_PWM_SCALE: SERIAL_PRINT(st.PWM_SCALE(), DEC); break;
  492. case TMC_SGT: SERIAL_PRINT(st.sgt(), DEC); break;
  493. case TMC_STEALTHCHOP: serialprint_truefalse(st.en_pwm_mode()); break;
  494. default: break;
  495. }
  496. }
  497. #endif
  498. #if HAS_TMCX1X0
  499. static void _tmc_parse_drv_status(TMC2130Stepper &st, const TMC_drv_status_enum i) {
  500. switch (i) {
  501. case TMC_STALLGUARD: if (st.stallguard()) SERIAL_CHAR('*'); break;
  502. case TMC_SG_RESULT: SERIAL_PRINT(st.sg_result(), DEC); break;
  503. case TMC_FSACTIVE: if (st.fsactive()) SERIAL_CHAR('*'); break;
  504. case TMC_DRV_CS_ACTUAL: SERIAL_PRINT(st.cs_actual(), DEC); break;
  505. default: break;
  506. }
  507. }
  508. #endif
  509. #if HAS_DRIVER(TMC2160) || HAS_DRIVER(TMC5160)
  510. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  511. void print_vsense(TMCMarlin<TMC2160Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &) { }
  512. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  513. void print_vsense(TMCMarlin<TMC5160Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &) { }
  514. static void _tmc_status(TMC2160Stepper &st, const TMC_debug_enum i) {
  515. switch (i) {
  516. case TMC_PWM_SCALE: SERIAL_PRINT(st.PWM_SCALE(), DEC); break;
  517. case TMC_SGT: SERIAL_PRINT(st.sgt(), DEC); break;
  518. case TMC_STEALTHCHOP: serialprint_truefalse(st.en_pwm_mode()); break;
  519. case TMC_GLOBAL_SCALER:
  520. {
  521. uint16_t value = st.GLOBAL_SCALER();
  522. SERIAL_PRINT(value ?: 256, DEC);
  523. SERIAL_ECHOPGM("/256");
  524. }
  525. break;
  526. default: break;
  527. }
  528. }
  529. #endif
  530. #if HAS_TMC220x
  531. static void _tmc_status(TMC2208Stepper &st, const TMC_debug_enum i) {
  532. switch (i) {
  533. case TMC_PWM_SCALE: SERIAL_PRINT(st.pwm_scale_sum(), DEC); break;
  534. case TMC_STEALTHCHOP: serialprint_truefalse(st.stealth()); break;
  535. case TMC_S2VSA: if (st.s2vsa()) SERIAL_CHAR('*'); break;
  536. case TMC_S2VSB: if (st.s2vsb()) SERIAL_CHAR('*'); break;
  537. default: break;
  538. }
  539. }
  540. #if HAS_DRIVER(TMC2209)
  541. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  542. static void _tmc_status(TMCMarlin<TMC2209Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const TMC_debug_enum i) {
  543. switch (i) {
  544. case TMC_SGT: SERIAL_PRINT(st.SGTHRS(), DEC); break;
  545. case TMC_UART_ADDR: SERIAL_PRINT(st.get_address(), DEC); break;
  546. default:
  547. TMC2208Stepper *parent = &st;
  548. _tmc_status(*parent, i);
  549. break;
  550. }
  551. }
  552. #endif
  553. static void _tmc_parse_drv_status(TMC2208Stepper &st, const TMC_drv_status_enum i) {
  554. switch (i) {
  555. case TMC_T157: if (st.t157()) SERIAL_CHAR('*'); break;
  556. case TMC_T150: if (st.t150()) SERIAL_CHAR('*'); break;
  557. case TMC_T143: if (st.t143()) SERIAL_CHAR('*'); break;
  558. case TMC_T120: if (st.t120()) SERIAL_CHAR('*'); break;
  559. case TMC_DRV_CS_ACTUAL: SERIAL_PRINT(st.cs_actual(), DEC); break;
  560. default: break;
  561. }
  562. }
  563. #endif
  564. #if HAS_DRIVER(TMC2660)
  565. static void _tmc_parse_drv_status(TMC2660Stepper, const TMC_drv_status_enum) { }
  566. #endif
  567. template <typename TMC>
  568. static void tmc_status(TMC &st, const TMC_debug_enum i) {
  569. SERIAL_CHAR('\t');
  570. switch (i) {
  571. case TMC_CODES: st.printLabel(); break;
  572. case TMC_ENABLED: serialprint_truefalse(st.isEnabled()); break;
  573. case TMC_CURRENT: SERIAL_ECHO(st.getMilliamps()); break;
  574. case TMC_RMS_CURRENT: SERIAL_ECHO(st.rms_current()); break;
  575. case TMC_MAX_CURRENT: SERIAL_PRINT((float)st.rms_current() * 1.41, 0); break;
  576. case TMC_IRUN:
  577. SERIAL_PRINT(st.irun(), DEC);
  578. SERIAL_ECHOPGM("/31");
  579. break;
  580. case TMC_IHOLD:
  581. SERIAL_PRINT(st.ihold(), DEC);
  582. SERIAL_ECHOPGM("/31");
  583. break;
  584. case TMC_CS_ACTUAL:
  585. SERIAL_PRINT(st.cs_actual(), DEC);
  586. SERIAL_ECHOPGM("/31");
  587. break;
  588. case TMC_VSENSE: print_vsense(st); break;
  589. case TMC_MICROSTEPS: SERIAL_ECHO(st.microsteps()); break;
  590. case TMC_TSTEP: {
  591. const uint32_t tstep_value = st.TSTEP();
  592. if (tstep_value != 0xFFFFF) SERIAL_ECHO(tstep_value); else SERIAL_ECHOPGM("max");
  593. } break;
  594. #if ENABLED(HYBRID_THRESHOLD)
  595. case TMC_TPWMTHRS: SERIAL_ECHO(uint32_t(st.TPWMTHRS())); break;
  596. case TMC_TPWMTHRS_MMS: {
  597. const uint32_t tpwmthrs_val = st.get_pwm_thrs();
  598. if (tpwmthrs_val) SERIAL_ECHO(tpwmthrs_val); else SERIAL_CHAR('-');
  599. } break;
  600. #endif
  601. case TMC_OTPW: serialprint_truefalse(st.otpw()); break;
  602. #if ENABLED(MONITOR_DRIVER_STATUS)
  603. case TMC_OTPW_TRIGGERED: serialprint_truefalse(st.getOTPW()); break;
  604. #endif
  605. case TMC_TOFF: SERIAL_PRINT(st.toff(), DEC); break;
  606. case TMC_TBL: SERIAL_PRINT(st.blank_time(), DEC); break;
  607. case TMC_HEND: SERIAL_PRINT(st.hysteresis_end(), DEC); break;
  608. case TMC_HSTRT: SERIAL_PRINT(st.hysteresis_start(), DEC); break;
  609. default: _tmc_status(st, i); break;
  610. }
  611. }
  612. #if HAS_DRIVER(TMC2660)
  613. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  614. void tmc_status(TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const TMC_debug_enum i) {
  615. SERIAL_CHAR('\t');
  616. switch (i) {
  617. case TMC_CODES: st.printLabel(); break;
  618. case TMC_ENABLED: serialprint_truefalse(st.isEnabled()); break;
  619. case TMC_CURRENT: SERIAL_ECHO(st.getMilliamps()); break;
  620. case TMC_RMS_CURRENT: SERIAL_ECHO(st.rms_current()); break;
  621. case TMC_MAX_CURRENT: SERIAL_PRINT((float)st.rms_current() * 1.41, 0); break;
  622. case TMC_IRUN:
  623. SERIAL_PRINT(st.cs(), DEC);
  624. SERIAL_ECHOPGM("/31");
  625. break;
  626. case TMC_VSENSE: serialprintPGM(st.vsense() ? PSTR("1=.165") : PSTR("0=.310")); break;
  627. case TMC_MICROSTEPS: SERIAL_ECHO(st.microsteps()); break;
  628. //case TMC_OTPW: serialprint_truefalse(st.otpw()); break;
  629. //case TMC_OTPW_TRIGGERED: serialprint_truefalse(st.getOTPW()); break;
  630. case TMC_SGT: SERIAL_PRINT(st.sgt(), DEC); break;
  631. case TMC_TOFF: SERIAL_PRINT(st.toff(), DEC); break;
  632. case TMC_TBL: SERIAL_PRINT(st.blank_time(), DEC); break;
  633. case TMC_HEND: SERIAL_PRINT(st.hysteresis_end(), DEC); break;
  634. case TMC_HSTRT: SERIAL_PRINT(st.hysteresis_start(), DEC); break;
  635. default: break;
  636. }
  637. }
  638. #endif
  639. template <typename TMC>
  640. static void tmc_parse_drv_status(TMC &st, const TMC_drv_status_enum i) {
  641. SERIAL_CHAR('\t');
  642. switch (i) {
  643. case TMC_DRV_CODES: st.printLabel(); break;
  644. case TMC_STST: if (st.stst()) SERIAL_CHAR('*'); break;
  645. case TMC_OLB: if (st.olb()) SERIAL_CHAR('*'); break;
  646. case TMC_OLA: if (st.ola()) SERIAL_CHAR('*'); break;
  647. case TMC_S2GB: if (st.s2gb()) SERIAL_CHAR('*'); break;
  648. case TMC_S2GA: if (st.s2ga()) SERIAL_CHAR('*'); break;
  649. case TMC_DRV_OTPW: if (st.otpw()) SERIAL_CHAR('*'); break;
  650. case TMC_OT: if (st.ot()) SERIAL_CHAR('*'); break;
  651. case TMC_DRV_STATUS_HEX: {
  652. const uint32_t drv_status = st.DRV_STATUS();
  653. SERIAL_CHAR('\t');
  654. st.printLabel();
  655. SERIAL_CHAR('\t');
  656. print_hex_long(drv_status, ':');
  657. if (drv_status == 0xFFFFFFFF || drv_status == 0) SERIAL_ECHOPGM("\t Bad response!");
  658. SERIAL_EOL();
  659. break;
  660. }
  661. default: _tmc_parse_drv_status(st, i); break;
  662. }
  663. }
  664. 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) {
  665. if (print_x) {
  666. #if AXIS_IS_TMC(X)
  667. tmc_status(stepperX, i);
  668. #endif
  669. #if AXIS_IS_TMC(X2)
  670. tmc_status(stepperX2, i);
  671. #endif
  672. }
  673. if (print_y) {
  674. #if AXIS_IS_TMC(Y)
  675. tmc_status(stepperY, i);
  676. #endif
  677. #if AXIS_IS_TMC(Y2)
  678. tmc_status(stepperY2, i);
  679. #endif
  680. }
  681. if (print_z) {
  682. #if AXIS_IS_TMC(Z)
  683. tmc_status(stepperZ, i);
  684. #endif
  685. #if AXIS_IS_TMC(Z2)
  686. tmc_status(stepperZ2, i);
  687. #endif
  688. #if AXIS_IS_TMC(Z3)
  689. tmc_status(stepperZ3, i);
  690. #endif
  691. #if AXIS_IS_TMC(Z4)
  692. tmc_status(stepperZ4, i);
  693. #endif
  694. }
  695. if (print_e) {
  696. #if AXIS_IS_TMC(E0)
  697. tmc_status(stepperE0, i);
  698. #endif
  699. #if AXIS_IS_TMC(E1)
  700. tmc_status(stepperE1, i);
  701. #endif
  702. #if AXIS_IS_TMC(E2)
  703. tmc_status(stepperE2, i);
  704. #endif
  705. #if AXIS_IS_TMC(E3)
  706. tmc_status(stepperE3, i);
  707. #endif
  708. #if AXIS_IS_TMC(E4)
  709. tmc_status(stepperE4, i);
  710. #endif
  711. #if AXIS_IS_TMC(E5)
  712. tmc_status(stepperE5, i);
  713. #endif
  714. }
  715. SERIAL_EOL();
  716. }
  717. 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) {
  718. if (print_x) {
  719. #if AXIS_IS_TMC(X)
  720. tmc_parse_drv_status(stepperX, i);
  721. #endif
  722. #if AXIS_IS_TMC(X2)
  723. tmc_parse_drv_status(stepperX2, i);
  724. #endif
  725. }
  726. if (print_y) {
  727. #if AXIS_IS_TMC(Y)
  728. tmc_parse_drv_status(stepperY, i);
  729. #endif
  730. #if AXIS_IS_TMC(Y2)
  731. tmc_parse_drv_status(stepperY2, i);
  732. #endif
  733. }
  734. if (print_z) {
  735. #if AXIS_IS_TMC(Z)
  736. tmc_parse_drv_status(stepperZ, i);
  737. #endif
  738. #if AXIS_IS_TMC(Z2)
  739. tmc_parse_drv_status(stepperZ2, i);
  740. #endif
  741. #if AXIS_IS_TMC(Z3)
  742. tmc_parse_drv_status(stepperZ3, i);
  743. #endif
  744. #if AXIS_IS_TMC(Z4)
  745. tmc_parse_drv_status(stepperZ4, i);
  746. #endif
  747. }
  748. if (print_e) {
  749. #if AXIS_IS_TMC(E0)
  750. tmc_parse_drv_status(stepperE0, i);
  751. #endif
  752. #if AXIS_IS_TMC(E1)
  753. tmc_parse_drv_status(stepperE1, i);
  754. #endif
  755. #if AXIS_IS_TMC(E2)
  756. tmc_parse_drv_status(stepperE2, i);
  757. #endif
  758. #if AXIS_IS_TMC(E3)
  759. tmc_parse_drv_status(stepperE3, i);
  760. #endif
  761. #if AXIS_IS_TMC(E4)
  762. tmc_parse_drv_status(stepperE4, i);
  763. #endif
  764. #if AXIS_IS_TMC(E5)
  765. tmc_parse_drv_status(stepperE5, i);
  766. #endif
  767. }
  768. SERIAL_EOL();
  769. }
  770. /**
  771. * M122 report functions
  772. */
  773. void tmc_report_all(bool print_x, const bool print_y, const bool print_z, const bool print_e) {
  774. #define TMC_REPORT(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); tmc_debug_loop(ITEM, print_x, print_y, print_z, print_e); }while(0)
  775. #define DRV_REPORT(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); drv_status_loop(ITEM, print_x, print_y, print_z, print_e); }while(0)
  776. TMC_REPORT("\t", TMC_CODES);
  777. #if HAS_DRIVER(TMC2209)
  778. TMC_REPORT("Address\t", TMC_UART_ADDR);
  779. #endif
  780. TMC_REPORT("Enabled\t", TMC_ENABLED);
  781. TMC_REPORT("Set current", TMC_CURRENT);
  782. TMC_REPORT("RMS current", TMC_RMS_CURRENT);
  783. TMC_REPORT("MAX current", TMC_MAX_CURRENT);
  784. TMC_REPORT("Run current", TMC_IRUN);
  785. TMC_REPORT("Hold current", TMC_IHOLD);
  786. #if HAS_DRIVER(TMC2160) || HAS_DRIVER(TMC5160)
  787. TMC_REPORT("Global scaler", TMC_GLOBAL_SCALER);
  788. #endif
  789. TMC_REPORT("CS actual", TMC_CS_ACTUAL);
  790. TMC_REPORT("PWM scale", TMC_PWM_SCALE);
  791. #if HAS_DRIVER(TMC2130) || HAS_DRIVER(TMC2224) || HAS_DRIVER(TMC2660) || HAS_TMC220x
  792. TMC_REPORT("vsense\t", TMC_VSENSE);
  793. #endif
  794. TMC_REPORT("stealthChop", TMC_STEALTHCHOP);
  795. TMC_REPORT("msteps\t", TMC_MICROSTEPS);
  796. TMC_REPORT("tstep\t", TMC_TSTEP);
  797. TMC_REPORT("pwm\nthreshold", TMC_TPWMTHRS);
  798. TMC_REPORT("[mm/s]\t", TMC_TPWMTHRS_MMS);
  799. TMC_REPORT("OT prewarn", TMC_OTPW);
  800. #if ENABLED(MONITOR_DRIVER_STATUS)
  801. TMC_REPORT("OT prewarn has\n"
  802. "been triggered", TMC_OTPW_TRIGGERED);
  803. #endif
  804. TMC_REPORT("off time", TMC_TOFF);
  805. TMC_REPORT("blank time", TMC_TBL);
  806. TMC_REPORT("hysteresis\n-end\t", TMC_HEND);
  807. TMC_REPORT("-start\t", TMC_HSTRT);
  808. TMC_REPORT("Stallguard thrs", TMC_SGT);
  809. DRV_REPORT("DRVSTATUS", TMC_DRV_CODES);
  810. #if HAS_TMCX1X0
  811. DRV_REPORT("stallguard\t", TMC_STALLGUARD);
  812. DRV_REPORT("sg_result", TMC_SG_RESULT);
  813. DRV_REPORT("fsactive\t", TMC_FSACTIVE);
  814. #endif
  815. DRV_REPORT("stst\t", TMC_STST);
  816. DRV_REPORT("olb\t", TMC_OLB);
  817. DRV_REPORT("ola\t", TMC_OLA);
  818. DRV_REPORT("s2gb\t", TMC_S2GB);
  819. DRV_REPORT("s2ga\t", TMC_S2GA);
  820. DRV_REPORT("otpw\t", TMC_DRV_OTPW);
  821. DRV_REPORT("ot\t", TMC_OT);
  822. #if HAS_TMC220x
  823. DRV_REPORT("157C\t", TMC_T157);
  824. DRV_REPORT("150C\t", TMC_T150);
  825. DRV_REPORT("143C\t", TMC_T143);
  826. DRV_REPORT("120C\t", TMC_T120);
  827. DRV_REPORT("s2vsa\t", TMC_S2VSA);
  828. DRV_REPORT("s2vsb\t", TMC_S2VSB);
  829. #endif
  830. DRV_REPORT("Driver registers:\n",TMC_DRV_STATUS_HEX);
  831. SERIAL_EOL();
  832. }
  833. #define PRINT_TMC_REGISTER(REG_CASE) case TMC_GET_##REG_CASE: print_hex_long(st.REG_CASE(), ':'); break
  834. #if HAS_TMCX1X0
  835. static void tmc_get_ic_registers(TMC2130Stepper &st, const TMC_get_registers_enum i) {
  836. switch (i) {
  837. PRINT_TMC_REGISTER(TCOOLTHRS);
  838. PRINT_TMC_REGISTER(THIGH);
  839. PRINT_TMC_REGISTER(COOLCONF);
  840. default: SERIAL_CHAR('\t'); break;
  841. }
  842. }
  843. #endif
  844. #if HAS_TMC220x
  845. static void tmc_get_ic_registers(TMC2208Stepper, const TMC_get_registers_enum) { SERIAL_CHAR('\t'); }
  846. #endif
  847. #if HAS_TRINAMIC
  848. template<class TMC>
  849. static void tmc_get_registers(TMC &st, const TMC_get_registers_enum i) {
  850. switch (i) {
  851. case TMC_AXIS_CODES: SERIAL_CHAR('\t'); st.printLabel(); break;
  852. PRINT_TMC_REGISTER(GCONF);
  853. PRINT_TMC_REGISTER(IHOLD_IRUN);
  854. PRINT_TMC_REGISTER(GSTAT);
  855. PRINT_TMC_REGISTER(IOIN);
  856. PRINT_TMC_REGISTER(TPOWERDOWN);
  857. PRINT_TMC_REGISTER(TSTEP);
  858. PRINT_TMC_REGISTER(TPWMTHRS);
  859. PRINT_TMC_REGISTER(CHOPCONF);
  860. PRINT_TMC_REGISTER(PWMCONF);
  861. PRINT_TMC_REGISTER(PWM_SCALE);
  862. PRINT_TMC_REGISTER(DRV_STATUS);
  863. default: tmc_get_ic_registers(st, i); break;
  864. }
  865. SERIAL_CHAR('\t');
  866. }
  867. #endif
  868. #if HAS_DRIVER(TMC2660)
  869. template <char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  870. static void tmc_get_registers(TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const TMC_get_registers_enum i) {
  871. switch (i) {
  872. case TMC_AXIS_CODES: SERIAL_CHAR('\t'); st.printLabel(); break;
  873. PRINT_TMC_REGISTER(DRVCONF);
  874. PRINT_TMC_REGISTER(DRVCTRL);
  875. PRINT_TMC_REGISTER(CHOPCONF);
  876. PRINT_TMC_REGISTER(DRVSTATUS);
  877. PRINT_TMC_REGISTER(SGCSCONF);
  878. PRINT_TMC_REGISTER(SMARTEN);
  879. default: SERIAL_CHAR('\t'); break;
  880. }
  881. SERIAL_CHAR('\t');
  882. }
  883. #endif
  884. 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) {
  885. if (print_x) {
  886. #if AXIS_IS_TMC(X)
  887. tmc_get_registers(stepperX, i);
  888. #endif
  889. #if AXIS_IS_TMC(X2)
  890. tmc_get_registers(stepperX2, i);
  891. #endif
  892. }
  893. if (print_y) {
  894. #if AXIS_IS_TMC(Y)
  895. tmc_get_registers(stepperY, i);
  896. #endif
  897. #if AXIS_IS_TMC(Y2)
  898. tmc_get_registers(stepperY2, i);
  899. #endif
  900. }
  901. if (print_z) {
  902. #if AXIS_IS_TMC(Z)
  903. tmc_get_registers(stepperZ, i);
  904. #endif
  905. #if AXIS_IS_TMC(Z2)
  906. tmc_get_registers(stepperZ2, i);
  907. #endif
  908. #if AXIS_IS_TMC(Z3)
  909. tmc_get_registers(stepperZ3, i);
  910. #endif
  911. #if AXIS_IS_TMC(Z4)
  912. tmc_get_registers(stepperZ4, i);
  913. #endif
  914. }
  915. if (print_e) {
  916. #if AXIS_IS_TMC(E0)
  917. tmc_get_registers(stepperE0, i);
  918. #endif
  919. #if AXIS_IS_TMC(E1)
  920. tmc_get_registers(stepperE1, i);
  921. #endif
  922. #if AXIS_IS_TMC(E2)
  923. tmc_get_registers(stepperE2, i);
  924. #endif
  925. #if AXIS_IS_TMC(E3)
  926. tmc_get_registers(stepperE3, i);
  927. #endif
  928. #if AXIS_IS_TMC(E4)
  929. tmc_get_registers(stepperE4, i);
  930. #endif
  931. #if AXIS_IS_TMC(E5)
  932. tmc_get_registers(stepperE5, i);
  933. #endif
  934. }
  935. SERIAL_EOL();
  936. }
  937. void tmc_get_registers(bool print_x, bool print_y, bool print_z, bool print_e) {
  938. #define _TMC_GET_REG(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); tmc_get_registers(ITEM, print_x, print_y, print_z, print_e); }while(0)
  939. #define TMC_GET_REG(NAME, TABS) _TMC_GET_REG(STRINGIFY(NAME) TABS, TMC_GET_##NAME)
  940. _TMC_GET_REG("\t", TMC_AXIS_CODES);
  941. TMC_GET_REG(GCONF, "\t\t");
  942. TMC_GET_REG(IHOLD_IRUN, "\t");
  943. TMC_GET_REG(GSTAT, "\t\t");
  944. TMC_GET_REG(IOIN, "\t\t");
  945. TMC_GET_REG(TPOWERDOWN, "\t");
  946. TMC_GET_REG(TSTEP, "\t\t");
  947. TMC_GET_REG(TPWMTHRS, "\t");
  948. TMC_GET_REG(TCOOLTHRS, "\t");
  949. TMC_GET_REG(THIGH, "\t\t");
  950. TMC_GET_REG(CHOPCONF, "\t");
  951. TMC_GET_REG(COOLCONF, "\t");
  952. TMC_GET_REG(PWMCONF, "\t");
  953. TMC_GET_REG(PWM_SCALE, "\t");
  954. TMC_GET_REG(DRV_STATUS, "\t");
  955. }
  956. #endif // TMC_DEBUG
  957. #if USE_SENSORLESS
  958. bool tmc_enable_stallguard(TMC2130Stepper &st) {
  959. const bool stealthchop_was_enabled = st.en_pwm_mode();
  960. st.TCOOLTHRS(0xFFFFF);
  961. st.en_pwm_mode(false);
  962. st.diag1_stall(true);
  963. return stealthchop_was_enabled;
  964. }
  965. void tmc_disable_stallguard(TMC2130Stepper &st, const bool restore_stealth) {
  966. st.TCOOLTHRS(0);
  967. st.en_pwm_mode(restore_stealth);
  968. st.diag1_stall(false);
  969. }
  970. bool tmc_enable_stallguard(TMC2209Stepper &st) {
  971. st.TCOOLTHRS(0xFFFFF);
  972. return !st.en_spreadCycle();
  973. }
  974. void tmc_disable_stallguard(TMC2209Stepper &st, const bool restore_stealth) {
  975. st.en_spreadCycle(!restore_stealth);
  976. st.TCOOLTHRS(0);
  977. }
  978. bool tmc_enable_stallguard(TMC2660Stepper) {
  979. // TODO
  980. return false;
  981. }
  982. void tmc_disable_stallguard(TMC2660Stepper, const bool) {};
  983. #endif // USE_SENSORLESS
  984. #if TMC_HAS_SPI
  985. #define SET_CS_PIN(st) OUT_WRITE(st##_CS_PIN, HIGH)
  986. void tmc_init_cs_pins() {
  987. #if AXIS_HAS_SPI(X)
  988. SET_CS_PIN(X);
  989. #endif
  990. #if AXIS_HAS_SPI(Y)
  991. SET_CS_PIN(Y);
  992. #endif
  993. #if AXIS_HAS_SPI(Z)
  994. SET_CS_PIN(Z);
  995. #endif
  996. #if AXIS_HAS_SPI(X2)
  997. SET_CS_PIN(X2);
  998. #endif
  999. #if AXIS_HAS_SPI(Y2)
  1000. SET_CS_PIN(Y2);
  1001. #endif
  1002. #if AXIS_HAS_SPI(Z2)
  1003. SET_CS_PIN(Z2);
  1004. #endif
  1005. #if AXIS_HAS_SPI(Z3)
  1006. SET_CS_PIN(Z3);
  1007. #endif
  1008. #if AXIS_HAS_SPI(Z4)
  1009. SET_CS_PIN(Z4);
  1010. #endif
  1011. #if AXIS_HAS_SPI(E0)
  1012. SET_CS_PIN(E0);
  1013. #endif
  1014. #if AXIS_HAS_SPI(E1)
  1015. SET_CS_PIN(E1);
  1016. #endif
  1017. #if AXIS_HAS_SPI(E2)
  1018. SET_CS_PIN(E2);
  1019. #endif
  1020. #if AXIS_HAS_SPI(E3)
  1021. SET_CS_PIN(E3);
  1022. #endif
  1023. #if AXIS_HAS_SPI(E4)
  1024. SET_CS_PIN(E4);
  1025. #endif
  1026. #if AXIS_HAS_SPI(E5)
  1027. SET_CS_PIN(E5);
  1028. #endif
  1029. }
  1030. #endif // TMC_HAS_SPI
  1031. template<typename TMC>
  1032. static bool test_connection(TMC &st) {
  1033. SERIAL_ECHOPGM("Testing ");
  1034. st.printLabel();
  1035. SERIAL_ECHOPGM(" connection... ");
  1036. const uint8_t test_result = st.test_connection();
  1037. if (test_result > 0) SERIAL_ECHOPGM("Error: All ");
  1038. const char *stat;
  1039. switch (test_result) {
  1040. default:
  1041. case 0: stat = PSTR("OK"); break;
  1042. case 1: stat = PSTR("HIGH"); break;
  1043. case 2: stat = PSTR("LOW"); break;
  1044. }
  1045. serialprintPGM(stat);
  1046. SERIAL_EOL();
  1047. return test_result;
  1048. }
  1049. void test_tmc_connection(const bool test_x, const bool test_y, const bool test_z, const bool test_e) {
  1050. uint8_t axis_connection = 0;
  1051. if (test_x) {
  1052. #if AXIS_IS_TMC(X)
  1053. axis_connection += test_connection(stepperX);
  1054. #endif
  1055. #if AXIS_IS_TMC(X2)
  1056. axis_connection += test_connection(stepperX2);
  1057. #endif
  1058. }
  1059. if (test_y) {
  1060. #if AXIS_IS_TMC(Y)
  1061. axis_connection += test_connection(stepperY);
  1062. #endif
  1063. #if AXIS_IS_TMC(Y2)
  1064. axis_connection += test_connection(stepperY2);
  1065. #endif
  1066. }
  1067. if (test_z) {
  1068. #if AXIS_IS_TMC(Z)
  1069. axis_connection += test_connection(stepperZ);
  1070. #endif
  1071. #if AXIS_IS_TMC(Z2)
  1072. axis_connection += test_connection(stepperZ2);
  1073. #endif
  1074. #if AXIS_IS_TMC(Z3)
  1075. axis_connection += test_connection(stepperZ3);
  1076. #endif
  1077. #if AXIS_IS_TMC(Z4)
  1078. axis_connection += test_connection(stepperZ4);
  1079. #endif
  1080. }
  1081. if (test_e) {
  1082. #if AXIS_IS_TMC(E0)
  1083. axis_connection += test_connection(stepperE0);
  1084. #endif
  1085. #if AXIS_IS_TMC(E1)
  1086. axis_connection += test_connection(stepperE1);
  1087. #endif
  1088. #if AXIS_IS_TMC(E2)
  1089. axis_connection += test_connection(stepperE2);
  1090. #endif
  1091. #if AXIS_IS_TMC(E3)
  1092. axis_connection += test_connection(stepperE3);
  1093. #endif
  1094. #if AXIS_IS_TMC(E4)
  1095. axis_connection += test_connection(stepperE4);
  1096. #endif
  1097. #if AXIS_IS_TMC(E5)
  1098. axis_connection += test_connection(stepperE5);
  1099. #endif
  1100. }
  1101. if (axis_connection) ui.set_status_P(GET_TEXT(MSG_ERROR_TMC));
  1102. }
  1103. #endif // HAS_TRINAMIC