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 34KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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_DRIVER(TMC2208)
  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_DRIVER(TMC2208)
  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
  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_ECHOPGM(" driver overtemperature warning! (");
  207. SERIAL_ECHO(st.getMilliamps());
  208. SERIAL_ECHOLNPGM("mA)");
  209. }
  210. template<typename TMC>
  211. void report_polled_driver_data(TMC &st, const TMC_driver_data &data) {
  212. const uint32_t pwm_scale = get_pwm_scale(st);
  213. st.printLabel();
  214. SERIAL_CHAR(':'); SERIAL_PRINT(pwm_scale, DEC);
  215. #if ENABLED(TMC_DEBUG)
  216. #if HAS_TMCX1X0 || HAS_DRIVER(TMC2208)
  217. SERIAL_CHAR('/'); SERIAL_PRINT(data.cs_actual, DEC);
  218. #endif
  219. #if HAS_STALLGUARD
  220. SERIAL_CHAR('/');
  221. if (data.sg_result_reasonable)
  222. SERIAL_ECHO(data.sg_result);
  223. else
  224. SERIAL_CHAR('-');
  225. #endif
  226. #endif
  227. SERIAL_CHAR('|');
  228. if (st.error_count) SERIAL_CHAR('E'); // Error
  229. if (data.is_ot) SERIAL_CHAR('O'); // Over-temperature
  230. if (data.is_otpw) SERIAL_CHAR('W'); // over-temperature pre-Warning
  231. #if ENABLED(TMC_DEBUG)
  232. if (data.is_stall) SERIAL_CHAR('G'); // stallGuard
  233. if (data.is_stealth) SERIAL_CHAR('T'); // stealthChop
  234. if (data.is_standstill) SERIAL_CHAR('I'); // standstIll
  235. #endif
  236. if (st.flag_otpw) SERIAL_CHAR('F'); // otpw Flag
  237. SERIAL_CHAR('|');
  238. if (st.otpw_count > 0) SERIAL_PRINT(st.otpw_count, DEC);
  239. SERIAL_CHAR('\t');
  240. }
  241. template<typename TMC>
  242. void monitor_tmc_driver(TMC &st, const bool need_update_error_counters, const bool need_debug_reporting) {
  243. TMC_driver_data data = get_driver_data(st);
  244. if (data.drv_status == 0xFFFFFFFF || data.drv_status == 0x0) return;
  245. if (need_update_error_counters) {
  246. if (data.is_ot /* | data.s2ga | data.s2gb*/) st.error_count++;
  247. else if (st.error_count > 0) st.error_count--;
  248. #if ENABLED(STOP_ON_ERROR)
  249. if (st.error_count >= 10) {
  250. SERIAL_EOL();
  251. st.printLabel();
  252. report_driver_error(data);
  253. }
  254. #endif
  255. // Report if a warning was triggered
  256. if (data.is_otpw && st.otpw_count == 0)
  257. report_driver_otpw(st);
  258. #if CURRENT_STEP_DOWN > 0
  259. // Decrease current if is_otpw is true and driver is enabled and there's been more than 4 warnings
  260. if (data.is_otpw && st.otpw_count > 4) {
  261. uint16_t I_rms = st.getMilliamps();
  262. if (st.isEnabled() && I_rms > 100) {
  263. st.rms_current(I_rms - (CURRENT_STEP_DOWN));
  264. #if ENABLED(REPORT_CURRENT_CHANGE)
  265. st.printLabel();
  266. SERIAL_ECHOLNPAIR(" current decreased to ", st.getMilliamps());
  267. #endif
  268. }
  269. }
  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)
  279. report_polled_driver_data(st, data);
  280. #endif
  281. }
  282. void monitor_tmc_driver() {
  283. static millis_t next_poll = 0;
  284. const millis_t ms = millis();
  285. bool need_update_error_counters = ELAPSED(ms, next_poll);
  286. bool need_debug_reporting = false;
  287. if (need_update_error_counters)
  288. next_poll = ms + MONITOR_DRIVER_STATUS_INTERVAL_MS;
  289. #if ENABLED(TMC_DEBUG)
  290. static millis_t next_debug_reporting = 0;
  291. if (report_tmc_status_interval && ELAPSED(ms, next_debug_reporting)) {
  292. need_debug_reporting = true;
  293. next_debug_reporting = ms + report_tmc_status_interval;
  294. }
  295. #endif
  296. if (need_update_error_counters || need_debug_reporting) {
  297. #if AXIS_IS_TMC(X)
  298. monitor_tmc_driver(stepperX, need_update_error_counters, need_debug_reporting);
  299. #endif
  300. #if AXIS_IS_TMC(Y)
  301. monitor_tmc_driver(stepperY, need_update_error_counters, need_debug_reporting);
  302. #endif
  303. #if AXIS_IS_TMC(Z)
  304. monitor_tmc_driver(stepperZ, need_update_error_counters, need_debug_reporting);
  305. #endif
  306. #if AXIS_IS_TMC(X2)
  307. monitor_tmc_driver(stepperX2, need_update_error_counters, need_debug_reporting);
  308. #endif
  309. #if AXIS_IS_TMC(Y2)
  310. monitor_tmc_driver(stepperY2, need_update_error_counters, need_debug_reporting);
  311. #endif
  312. #if AXIS_IS_TMC(Z2)
  313. monitor_tmc_driver(stepperZ2, need_update_error_counters, need_debug_reporting);
  314. #endif
  315. #if AXIS_IS_TMC(Z3)
  316. monitor_tmc_driver(stepperZ3, need_update_error_counters, need_debug_reporting);
  317. #endif
  318. #if AXIS_IS_TMC(E0)
  319. monitor_tmc_driver(stepperE0, need_update_error_counters, need_debug_reporting);
  320. #endif
  321. #if AXIS_IS_TMC(E1)
  322. monitor_tmc_driver(stepperE1, need_update_error_counters, need_debug_reporting);
  323. #endif
  324. #if AXIS_IS_TMC(E2)
  325. monitor_tmc_driver(stepperE2, need_update_error_counters, need_debug_reporting);
  326. #endif
  327. #if AXIS_IS_TMC(E3)
  328. monitor_tmc_driver(stepperE3, need_update_error_counters, need_debug_reporting);
  329. #endif
  330. #if AXIS_IS_TMC(E4)
  331. monitor_tmc_driver(stepperE4, need_update_error_counters, need_debug_reporting);
  332. #endif
  333. #if AXIS_IS_TMC(E5)
  334. monitor_tmc_driver(stepperE5, need_update_error_counters, need_debug_reporting);
  335. #endif
  336. #if ENABLED(TMC_DEBUG)
  337. if (need_debug_reporting) SERIAL_EOL();
  338. #endif
  339. }
  340. }
  341. #endif // MONITOR_DRIVER_STATUS
  342. #if ENABLED(TMC_DEBUG)
  343. /**
  344. * M122 [S<0|1>] [Pnnn] Enable periodic status reports
  345. */
  346. #if ENABLED(MONITOR_DRIVER_STATUS)
  347. void tmc_set_report_interval(const uint16_t update_interval) {
  348. if ((report_tmc_status_interval = update_interval))
  349. SERIAL_ECHOLNPGM("axis:pwm_scale"
  350. #if HAS_STEALTHCHOP
  351. "/current_scale"
  352. #endif
  353. #if HAS_STALLGUARD
  354. "/mech_load"
  355. #endif
  356. "|flags|warncount"
  357. );
  358. }
  359. #endif
  360. enum TMC_debug_enum : char {
  361. TMC_CODES,
  362. TMC_ENABLED,
  363. TMC_CURRENT,
  364. TMC_RMS_CURRENT,
  365. TMC_MAX_CURRENT,
  366. TMC_IRUN,
  367. TMC_IHOLD,
  368. TMC_GLOBAL_SCALER,
  369. TMC_CS_ACTUAL,
  370. TMC_PWM_SCALE,
  371. TMC_VSENSE,
  372. TMC_STEALTHCHOP,
  373. TMC_MICROSTEPS,
  374. TMC_TSTEP,
  375. TMC_TPWMTHRS,
  376. TMC_TPWMTHRS_MMS,
  377. TMC_OTPW,
  378. TMC_OTPW_TRIGGERED,
  379. TMC_TOFF,
  380. TMC_TBL,
  381. TMC_HEND,
  382. TMC_HSTRT,
  383. TMC_SGT
  384. };
  385. enum TMC_drv_status_enum : char {
  386. TMC_DRV_CODES,
  387. TMC_STST,
  388. TMC_OLB,
  389. TMC_OLA,
  390. TMC_S2GB,
  391. TMC_S2GA,
  392. TMC_DRV_OTPW,
  393. TMC_OT,
  394. TMC_STALLGUARD,
  395. TMC_DRV_CS_ACTUAL,
  396. TMC_FSACTIVE,
  397. TMC_SG_RESULT,
  398. TMC_DRV_STATUS_HEX,
  399. TMC_T157,
  400. TMC_T150,
  401. TMC_T143,
  402. TMC_T120,
  403. TMC_STEALTH,
  404. TMC_S2VSB,
  405. TMC_S2VSA
  406. };
  407. enum TMC_get_registers_enum : char {
  408. TMC_AXIS_CODES,
  409. TMC_GET_GCONF,
  410. TMC_GET_IHOLD_IRUN,
  411. TMC_GET_GSTAT,
  412. TMC_GET_IOIN,
  413. TMC_GET_TPOWERDOWN,
  414. TMC_GET_TSTEP,
  415. TMC_GET_TPWMTHRS,
  416. TMC_GET_TCOOLTHRS,
  417. TMC_GET_THIGH,
  418. TMC_GET_CHOPCONF,
  419. TMC_GET_COOLCONF,
  420. TMC_GET_PWMCONF,
  421. TMC_GET_PWM_SCALE,
  422. TMC_GET_DRV_STATUS,
  423. TMC_GET_DRVCONF,
  424. TMC_GET_DRVCTRL,
  425. TMC_GET_DRVSTATUS,
  426. TMC_GET_SGCSCONF,
  427. TMC_GET_SMARTEN
  428. };
  429. template<class TMC>
  430. static void print_vsense(TMC &st) { serialprintPGM(st.vsense() ? PSTR("1=.18") : PSTR("0=.325")); }
  431. #if HAS_DRIVER(TMC2130) || HAS_DRIVER(TMC5130)
  432. static void _tmc_status(TMC2130Stepper &st, const TMC_debug_enum i) {
  433. switch (i) {
  434. case TMC_PWM_SCALE: SERIAL_PRINT(st.PWM_SCALE(), DEC); break;
  435. case TMC_SGT: SERIAL_PRINT(st.sgt(), DEC); break;
  436. case TMC_STEALTHCHOP: serialprint_truefalse(st.en_pwm_mode()); break;
  437. default: break;
  438. }
  439. }
  440. #endif
  441. #if HAS_TMCX1X0
  442. static void _tmc_parse_drv_status(TMC2130Stepper &st, const TMC_drv_status_enum i) {
  443. switch (i) {
  444. case TMC_STALLGUARD: if (st.stallguard()) SERIAL_CHAR('X'); break;
  445. case TMC_SG_RESULT: SERIAL_PRINT(st.sg_result(), DEC); break;
  446. case TMC_FSACTIVE: if (st.fsactive()) SERIAL_CHAR('X'); break;
  447. case TMC_DRV_CS_ACTUAL: SERIAL_PRINT(st.cs_actual(), DEC); break;
  448. default: break;
  449. }
  450. }
  451. #endif
  452. #if HAS_DRIVER(TMC2160) || HAS_DRIVER(TMC5160)
  453. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  454. void print_vsense(TMCMarlin<TMC2160Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &) { }
  455. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  456. void print_vsense(TMCMarlin<TMC5160Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &) { }
  457. static void _tmc_status(TMC2160Stepper &st, const TMC_debug_enum i) {
  458. switch (i) {
  459. case TMC_PWM_SCALE: SERIAL_PRINT(st.PWM_SCALE(), DEC); break;
  460. case TMC_SGT: SERIAL_PRINT(st.sgt(), DEC); break;
  461. case TMC_STEALTHCHOP: serialprint_truefalse(st.en_pwm_mode()); break;
  462. case TMC_GLOBAL_SCALER:
  463. {
  464. uint16_t value = st.GLOBAL_SCALER();
  465. SERIAL_PRINT(value ? value : 256, DEC);
  466. SERIAL_ECHOPGM("/256");
  467. }
  468. break;
  469. default: break;
  470. }
  471. }
  472. #endif
  473. #if HAS_DRIVER(TMC2208)
  474. static void _tmc_status(TMC2208Stepper &st, const TMC_debug_enum i) {
  475. switch (i) {
  476. case TMC_PWM_SCALE: SERIAL_PRINT(st.pwm_scale_sum(), DEC); break;
  477. case TMC_STEALTHCHOP: serialprint_truefalse(st.stealth()); break;
  478. case TMC_S2VSA: if (st.s2vsa()) SERIAL_CHAR('X'); break;
  479. case TMC_S2VSB: if (st.s2vsb()) SERIAL_CHAR('X'); break;
  480. default: break;
  481. }
  482. }
  483. static void _tmc_parse_drv_status(TMC2208Stepper &st, const TMC_drv_status_enum i) {
  484. switch (i) {
  485. case TMC_T157: if (st.t157()) SERIAL_CHAR('X'); break;
  486. case TMC_T150: if (st.t150()) SERIAL_CHAR('X'); break;
  487. case TMC_T143: if (st.t143()) SERIAL_CHAR('X'); break;
  488. case TMC_T120: if (st.t120()) SERIAL_CHAR('X'); break;
  489. case TMC_DRV_CS_ACTUAL: SERIAL_PRINT(st.cs_actual(), DEC); break;
  490. default: break;
  491. }
  492. }
  493. #endif
  494. #if HAS_DRIVER(TMC2660)
  495. static void _tmc_parse_drv_status(TMC2660Stepper, const TMC_drv_status_enum) { }
  496. #endif
  497. template <typename TMC>
  498. static void tmc_status(TMC &st, const TMC_debug_enum i) {
  499. SERIAL_CHAR('\t');
  500. switch (i) {
  501. case TMC_CODES: st.printLabel(); break;
  502. case TMC_ENABLED: serialprint_truefalse(st.isEnabled()); break;
  503. case TMC_CURRENT: SERIAL_ECHO(st.getMilliamps()); break;
  504. case TMC_RMS_CURRENT: SERIAL_ECHO(st.rms_current()); break;
  505. case TMC_MAX_CURRENT: SERIAL_PRINT((float)st.rms_current() * 1.41, 0); break;
  506. case TMC_IRUN:
  507. SERIAL_PRINT(st.irun(), DEC);
  508. SERIAL_ECHOPGM("/31");
  509. break;
  510. case TMC_IHOLD:
  511. SERIAL_PRINT(st.ihold(), DEC);
  512. SERIAL_ECHOPGM("/31");
  513. break;
  514. case TMC_CS_ACTUAL:
  515. SERIAL_PRINT(st.cs_actual(), DEC);
  516. SERIAL_ECHOPGM("/31");
  517. break;
  518. case TMC_VSENSE: print_vsense(st); break;
  519. case TMC_MICROSTEPS: SERIAL_ECHO(st.microsteps()); break;
  520. case TMC_TSTEP: {
  521. const uint32_t tstep_value = st.TSTEP();
  522. if (tstep_value != 0xFFFFF) SERIAL_ECHO(tstep_value); else SERIAL_ECHOPGM("max");
  523. } break;
  524. #if ENABLED(HYBRID_THRESHOLD)
  525. case TMC_TPWMTHRS: SERIAL_ECHO(uint32_t(st.TPWMTHRS())); break;
  526. case TMC_TPWMTHRS_MMS: {
  527. const uint32_t tpwmthrs_val = st.get_pwm_thrs();
  528. if (tpwmthrs_val) SERIAL_ECHO(tpwmthrs_val); else SERIAL_CHAR('-');
  529. } break;
  530. #endif
  531. case TMC_OTPW: serialprint_truefalse(st.otpw()); break;
  532. #if ENABLED(MONITOR_DRIVER_STATUS)
  533. case TMC_OTPW_TRIGGERED: serialprint_truefalse(st.getOTPW()); break;
  534. #endif
  535. case TMC_TOFF: SERIAL_PRINT(st.toff(), DEC); break;
  536. case TMC_TBL: SERIAL_PRINT(st.blank_time(), DEC); break;
  537. case TMC_HEND: SERIAL_PRINT(st.hysteresis_end(), DEC); break;
  538. case TMC_HSTRT: SERIAL_PRINT(st.hysteresis_start(), DEC); break;
  539. default: _tmc_status(st, i); break;
  540. }
  541. }
  542. #if HAS_DRIVER(TMC2660)
  543. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  544. void tmc_status(TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const TMC_debug_enum i) {
  545. SERIAL_CHAR('\t');
  546. switch (i) {
  547. case TMC_CODES: st.printLabel(); break;
  548. case TMC_ENABLED: serialprint_truefalse(st.isEnabled()); break;
  549. case TMC_CURRENT: SERIAL_ECHO(st.getMilliamps()); break;
  550. case TMC_RMS_CURRENT: SERIAL_ECHO(st.rms_current()); break;
  551. case TMC_MAX_CURRENT: SERIAL_PRINT((float)st.rms_current() * 1.41, 0); break;
  552. case TMC_IRUN:
  553. SERIAL_PRINT(st.cs(), DEC);
  554. SERIAL_ECHOPGM("/31");
  555. break;
  556. case TMC_VSENSE: serialprintPGM(st.vsense() ? PSTR("1=.165") : PSTR("0=.310")); break;
  557. case TMC_MICROSTEPS: SERIAL_ECHO(st.microsteps()); break;
  558. //case TMC_OTPW: serialprint_truefalse(st.otpw()); break;
  559. //case TMC_OTPW_TRIGGERED: serialprint_truefalse(st.getOTPW()); break;
  560. case TMC_SGT: SERIAL_PRINT(st.sgt(), DEC); break;
  561. case TMC_TOFF: SERIAL_PRINT(st.toff(), DEC); break;
  562. case TMC_TBL: SERIAL_PRINT(st.blank_time(), DEC); break;
  563. case TMC_HEND: SERIAL_PRINT(st.hysteresis_end(), DEC); break;
  564. case TMC_HSTRT: SERIAL_PRINT(st.hysteresis_start(), DEC); break;
  565. default: break;
  566. }
  567. }
  568. #endif
  569. template <typename TMC>
  570. static void tmc_parse_drv_status(TMC &st, const TMC_drv_status_enum i) {
  571. SERIAL_CHAR('\t');
  572. switch (i) {
  573. case TMC_DRV_CODES: st.printLabel(); break;
  574. case TMC_STST: if (st.stst()) SERIAL_CHAR('X'); break;
  575. case TMC_OLB: if (st.olb()) SERIAL_CHAR('X'); break;
  576. case TMC_OLA: if (st.ola()) SERIAL_CHAR('X'); break;
  577. case TMC_S2GB: if (st.s2gb()) SERIAL_CHAR('X'); break;
  578. case TMC_S2GA: if (st.s2ga()) SERIAL_CHAR('X'); break;
  579. case TMC_DRV_OTPW: if (st.otpw()) SERIAL_CHAR('X'); break;
  580. case TMC_OT: if (st.ot()) SERIAL_CHAR('X'); break;
  581. case TMC_DRV_STATUS_HEX: {
  582. const uint32_t drv_status = st.DRV_STATUS();
  583. SERIAL_CHAR('\t');
  584. st.printLabel();
  585. SERIAL_CHAR('\t');
  586. print_hex_long(drv_status, ':');
  587. if (drv_status == 0xFFFFFFFF || drv_status == 0) SERIAL_ECHOPGM("\t Bad response!");
  588. SERIAL_EOL();
  589. break;
  590. }
  591. default: _tmc_parse_drv_status(st, i); break;
  592. }
  593. }
  594. 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) {
  595. if (print_x) {
  596. #if AXIS_IS_TMC(X)
  597. tmc_status(stepperX, i);
  598. #endif
  599. #if AXIS_IS_TMC(X2)
  600. tmc_status(stepperX2, i);
  601. #endif
  602. }
  603. if (print_y) {
  604. #if AXIS_IS_TMC(Y)
  605. tmc_status(stepperY, i);
  606. #endif
  607. #if AXIS_IS_TMC(Y2)
  608. tmc_status(stepperY2, i);
  609. #endif
  610. }
  611. if (print_z) {
  612. #if AXIS_IS_TMC(Z)
  613. tmc_status(stepperZ, i);
  614. #endif
  615. #if AXIS_IS_TMC(Z2)
  616. tmc_status(stepperZ2, i);
  617. #endif
  618. #if AXIS_IS_TMC(Z3)
  619. tmc_status(stepperZ3, i);
  620. #endif
  621. }
  622. if (print_e) {
  623. #if AXIS_IS_TMC(E0)
  624. tmc_status(stepperE0, i);
  625. #endif
  626. #if AXIS_IS_TMC(E1)
  627. tmc_status(stepperE1, i);
  628. #endif
  629. #if AXIS_IS_TMC(E2)
  630. tmc_status(stepperE2, i);
  631. #endif
  632. #if AXIS_IS_TMC(E3)
  633. tmc_status(stepperE3, i);
  634. #endif
  635. #if AXIS_IS_TMC(E4)
  636. tmc_status(stepperE4, i);
  637. #endif
  638. #if AXIS_IS_TMC(E5)
  639. tmc_status(stepperE5, i);
  640. #endif
  641. }
  642. SERIAL_EOL();
  643. }
  644. 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) {
  645. if (print_x) {
  646. #if AXIS_IS_TMC(X)
  647. tmc_parse_drv_status(stepperX, i);
  648. #endif
  649. #if AXIS_IS_TMC(X2)
  650. tmc_parse_drv_status(stepperX2, i);
  651. #endif
  652. }
  653. if (print_y) {
  654. #if AXIS_IS_TMC(Y)
  655. tmc_parse_drv_status(stepperY, i);
  656. #endif
  657. #if AXIS_IS_TMC(Y2)
  658. tmc_parse_drv_status(stepperY2, i);
  659. #endif
  660. }
  661. if (print_z) {
  662. #if AXIS_IS_TMC(Z)
  663. tmc_parse_drv_status(stepperZ, i);
  664. #endif
  665. #if AXIS_IS_TMC(Z2)
  666. tmc_parse_drv_status(stepperZ2, i);
  667. #endif
  668. #if AXIS_IS_TMC(Z3)
  669. tmc_parse_drv_status(stepperZ3, i);
  670. #endif
  671. }
  672. if (print_e) {
  673. #if AXIS_IS_TMC(E0)
  674. tmc_parse_drv_status(stepperE0, i);
  675. #endif
  676. #if AXIS_IS_TMC(E1)
  677. tmc_parse_drv_status(stepperE1, i);
  678. #endif
  679. #if AXIS_IS_TMC(E2)
  680. tmc_parse_drv_status(stepperE2, i);
  681. #endif
  682. #if AXIS_IS_TMC(E3)
  683. tmc_parse_drv_status(stepperE3, i);
  684. #endif
  685. #if AXIS_IS_TMC(E4)
  686. tmc_parse_drv_status(stepperE4, i);
  687. #endif
  688. #if AXIS_IS_TMC(E5)
  689. tmc_parse_drv_status(stepperE5, i);
  690. #endif
  691. }
  692. SERIAL_EOL();
  693. }
  694. /**
  695. * M122 report functions
  696. */
  697. void tmc_report_all(bool print_x, const bool print_y, const bool print_z, const bool print_e) {
  698. #define TMC_REPORT(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); tmc_debug_loop(ITEM, print_x, print_y, print_z, print_e); }while(0)
  699. #define DRV_REPORT(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); drv_status_loop(ITEM, print_x, print_y, print_z, print_e); }while(0)
  700. TMC_REPORT("\t", TMC_CODES);
  701. TMC_REPORT("Enabled\t", TMC_ENABLED);
  702. TMC_REPORT("Set current", TMC_CURRENT);
  703. TMC_REPORT("RMS current", TMC_RMS_CURRENT);
  704. TMC_REPORT("MAX current", TMC_MAX_CURRENT);
  705. TMC_REPORT("Run current", TMC_IRUN);
  706. TMC_REPORT("Hold current", TMC_IHOLD);
  707. #if HAS_DRIVER(TMC2160) || HAS_DRIVER(TMC5160)
  708. TMC_REPORT("Global scaler", TMC_GLOBAL_SCALER);
  709. #endif
  710. TMC_REPORT("CS actual\t", TMC_CS_ACTUAL);
  711. TMC_REPORT("PWM scale", TMC_PWM_SCALE);
  712. #if HAS_DRIVER(TMC2130) || HAS_DRIVER(TMC2224) || HAS_DRIVER(TMC2660) || HAS_DRIVER(TMC2208)
  713. TMC_REPORT("vsense\t", TMC_VSENSE);
  714. #endif
  715. TMC_REPORT("stealthChop", TMC_STEALTHCHOP);
  716. TMC_REPORT("msteps\t", TMC_MICROSTEPS);
  717. TMC_REPORT("tstep\t", TMC_TSTEP);
  718. TMC_REPORT("pwm\nthreshold\t", TMC_TPWMTHRS);
  719. TMC_REPORT("[mm/s]\t", TMC_TPWMTHRS_MMS);
  720. TMC_REPORT("OT prewarn", TMC_OTPW);
  721. #if ENABLED(MONITOR_DRIVER_STATUS)
  722. TMC_REPORT("OT prewarn has\n"
  723. "been triggered", TMC_OTPW_TRIGGERED);
  724. #endif
  725. TMC_REPORT("off time\t", TMC_TOFF);
  726. TMC_REPORT("blank time", TMC_TBL);
  727. TMC_REPORT("hysteresis\n-end\t", TMC_HEND);
  728. TMC_REPORT("-start\t", TMC_HSTRT);
  729. TMC_REPORT("Stallguard thrs", TMC_SGT);
  730. DRV_REPORT("DRVSTATUS", TMC_DRV_CODES);
  731. #if HAS_TMCX1X0
  732. DRV_REPORT("stallguard\t", TMC_STALLGUARD);
  733. DRV_REPORT("sg_result\t", TMC_SG_RESULT);
  734. DRV_REPORT("fsactive\t", TMC_FSACTIVE);
  735. #endif
  736. DRV_REPORT("stst\t", TMC_STST);
  737. DRV_REPORT("olb\t", TMC_OLB);
  738. DRV_REPORT("ola\t", TMC_OLA);
  739. DRV_REPORT("s2gb\t", TMC_S2GB);
  740. DRV_REPORT("s2ga\t", TMC_S2GA);
  741. DRV_REPORT("otpw\t", TMC_DRV_OTPW);
  742. DRV_REPORT("ot\t", TMC_OT);
  743. #if HAS_DRIVER(TMC2208)
  744. DRV_REPORT("157C\t", TMC_T157);
  745. DRV_REPORT("150C\t", TMC_T150);
  746. DRV_REPORT("143C\t", TMC_T143);
  747. DRV_REPORT("120C\t", TMC_T120);
  748. DRV_REPORT("s2vsa\t", TMC_S2VSA);
  749. DRV_REPORT("s2vsb\t", TMC_S2VSB);
  750. #endif
  751. DRV_REPORT("Driver registers:\n",TMC_DRV_STATUS_HEX);
  752. SERIAL_EOL();
  753. }
  754. #define PRINT_TMC_REGISTER(REG_CASE) case TMC_GET_##REG_CASE: print_hex_long(st.REG_CASE(), ':'); break
  755. #if HAS_TMCX1X0
  756. static void tmc_get_ic_registers(TMC2130Stepper &st, const TMC_get_registers_enum i) {
  757. switch (i) {
  758. PRINT_TMC_REGISTER(TCOOLTHRS);
  759. PRINT_TMC_REGISTER(THIGH);
  760. PRINT_TMC_REGISTER(COOLCONF);
  761. default: SERIAL_CHAR('\t'); break;
  762. }
  763. }
  764. #endif
  765. #if HAS_DRIVER(TMC2208)
  766. static void tmc_get_ic_registers(TMC2208Stepper, const TMC_get_registers_enum) { SERIAL_CHAR('\t'); }
  767. #endif
  768. #if HAS_TRINAMIC
  769. template<class TMC>
  770. static void tmc_get_registers(TMC &st, const TMC_get_registers_enum i) {
  771. switch (i) {
  772. case TMC_AXIS_CODES: SERIAL_CHAR('\t'); st.printLabel(); break;
  773. PRINT_TMC_REGISTER(GCONF);
  774. PRINT_TMC_REGISTER(IHOLD_IRUN);
  775. PRINT_TMC_REGISTER(GSTAT);
  776. PRINT_TMC_REGISTER(IOIN);
  777. PRINT_TMC_REGISTER(TPOWERDOWN);
  778. PRINT_TMC_REGISTER(TSTEP);
  779. PRINT_TMC_REGISTER(TPWMTHRS);
  780. PRINT_TMC_REGISTER(CHOPCONF);
  781. PRINT_TMC_REGISTER(PWMCONF);
  782. PRINT_TMC_REGISTER(PWM_SCALE);
  783. PRINT_TMC_REGISTER(DRV_STATUS);
  784. default: tmc_get_ic_registers(st, i); break;
  785. }
  786. SERIAL_CHAR('\t');
  787. }
  788. #endif
  789. #if HAS_DRIVER(TMC2660)
  790. template <char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  791. static void tmc_get_registers(TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const TMC_get_registers_enum i) {
  792. switch (i) {
  793. case TMC_AXIS_CODES: SERIAL_CHAR('\t'); st.printLabel(); break;
  794. PRINT_TMC_REGISTER(DRVCONF);
  795. PRINT_TMC_REGISTER(DRVCTRL);
  796. PRINT_TMC_REGISTER(CHOPCONF);
  797. PRINT_TMC_REGISTER(DRVSTATUS);
  798. PRINT_TMC_REGISTER(SGCSCONF);
  799. PRINT_TMC_REGISTER(SMARTEN);
  800. default: SERIAL_CHAR('\t'); break;
  801. }
  802. SERIAL_CHAR('\t');
  803. }
  804. #endif
  805. 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) {
  806. if (print_x) {
  807. #if AXIS_IS_TMC(X)
  808. tmc_get_registers(stepperX, i);
  809. #endif
  810. #if AXIS_IS_TMC(X2)
  811. tmc_get_registers(stepperX2, i);
  812. #endif
  813. }
  814. if (print_y) {
  815. #if AXIS_IS_TMC(Y)
  816. tmc_get_registers(stepperY, i);
  817. #endif
  818. #if AXIS_IS_TMC(Y2)
  819. tmc_get_registers(stepperY2, i);
  820. #endif
  821. }
  822. if (print_z) {
  823. #if AXIS_IS_TMC(Z)
  824. tmc_get_registers(stepperZ, i);
  825. #endif
  826. #if AXIS_IS_TMC(Z2)
  827. tmc_get_registers(stepperZ2, i);
  828. #endif
  829. #if AXIS_IS_TMC(Z3)
  830. tmc_get_registers(stepperZ3, i);
  831. #endif
  832. }
  833. if (print_e) {
  834. #if AXIS_IS_TMC(E0)
  835. tmc_get_registers(stepperE0, i);
  836. #endif
  837. #if AXIS_IS_TMC(E1)
  838. tmc_get_registers(stepperE1, i);
  839. #endif
  840. #if AXIS_IS_TMC(E2)
  841. tmc_get_registers(stepperE2, i);
  842. #endif
  843. #if AXIS_IS_TMC(E3)
  844. tmc_get_registers(stepperE3, i);
  845. #endif
  846. #if AXIS_IS_TMC(E4)
  847. tmc_get_registers(stepperE4, i);
  848. #endif
  849. #if AXIS_IS_TMC(E5)
  850. tmc_get_registers(stepperE5, i);
  851. #endif
  852. }
  853. SERIAL_EOL();
  854. }
  855. void tmc_get_registers(bool print_x, bool print_y, bool print_z, bool print_e) {
  856. #define _TMC_GET_REG(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); tmc_get_registers(ITEM, print_x, print_y, print_z, print_e); }while(0)
  857. #define TMC_GET_REG(NAME, TABS) _TMC_GET_REG(STRINGIFY(NAME) TABS, TMC_GET_##NAME)
  858. _TMC_GET_REG("\t", TMC_AXIS_CODES);
  859. TMC_GET_REG(GCONF, "\t\t");
  860. TMC_GET_REG(IHOLD_IRUN, "\t");
  861. TMC_GET_REG(GSTAT, "\t\t");
  862. TMC_GET_REG(IOIN, "\t\t");
  863. TMC_GET_REG(TPOWERDOWN, "\t");
  864. TMC_GET_REG(TSTEP, "\t\t");
  865. TMC_GET_REG(TPWMTHRS, "\t");
  866. TMC_GET_REG(TCOOLTHRS, "\t");
  867. TMC_GET_REG(THIGH, "\t\t");
  868. TMC_GET_REG(CHOPCONF, "\t");
  869. TMC_GET_REG(COOLCONF, "\t");
  870. TMC_GET_REG(PWMCONF, "\t");
  871. TMC_GET_REG(PWM_SCALE, "\t");
  872. TMC_GET_REG(DRV_STATUS, "\t");
  873. }
  874. #endif // TMC_DEBUG
  875. #if USE_SENSORLESS
  876. bool tmc_enable_stallguard(TMC2130Stepper &st) {
  877. bool stealthchop_was_enabled = st.en_pwm_mode();
  878. st.TCOOLTHRS(0xFFFFF);
  879. st.en_pwm_mode(false);
  880. st.diag1_stall(true);
  881. return stealthchop_was_enabled;
  882. }
  883. void tmc_disable_stallguard(TMC2130Stepper &st, const bool restore_stealth) {
  884. st.TCOOLTHRS(0);
  885. st.en_pwm_mode(restore_stealth);
  886. st.diag1_stall(false);
  887. }
  888. bool tmc_enable_stallguard(TMC2660Stepper) {
  889. // TODO
  890. return false;
  891. }
  892. void tmc_disable_stallguard(TMC2660Stepper, const bool) {};
  893. #endif // USE_SENSORLESS
  894. #if TMC_HAS_SPI
  895. #define SET_CS_PIN(st) OUT_WRITE(st##_CS_PIN, HIGH)
  896. void tmc_init_cs_pins() {
  897. #if AXIS_HAS_SPI(X)
  898. SET_CS_PIN(X);
  899. #endif
  900. #if AXIS_HAS_SPI(Y)
  901. SET_CS_PIN(Y);
  902. #endif
  903. #if AXIS_HAS_SPI(Z)
  904. SET_CS_PIN(Z);
  905. #endif
  906. #if AXIS_HAS_SPI(X2)
  907. SET_CS_PIN(X2);
  908. #endif
  909. #if AXIS_HAS_SPI(Y2)
  910. SET_CS_PIN(Y2);
  911. #endif
  912. #if AXIS_HAS_SPI(Z2)
  913. SET_CS_PIN(Z2);
  914. #endif
  915. #if AXIS_HAS_SPI(Z3)
  916. SET_CS_PIN(Z3);
  917. #endif
  918. #if AXIS_HAS_SPI(E0)
  919. SET_CS_PIN(E0);
  920. #endif
  921. #if AXIS_HAS_SPI(E1)
  922. SET_CS_PIN(E1);
  923. #endif
  924. #if AXIS_HAS_SPI(E2)
  925. SET_CS_PIN(E2);
  926. #endif
  927. #if AXIS_HAS_SPI(E3)
  928. SET_CS_PIN(E3);
  929. #endif
  930. #if AXIS_HAS_SPI(E4)
  931. SET_CS_PIN(E4);
  932. #endif
  933. #if AXIS_HAS_SPI(E5)
  934. SET_CS_PIN(E5);
  935. #endif
  936. }
  937. #endif // TMC_HAS_SPI
  938. template<typename TMC>
  939. static bool test_connection(TMC &st) {
  940. SERIAL_ECHOPGM("Testing ");
  941. st.printLabel();
  942. SERIAL_ECHOPGM(" connection... ");
  943. const uint8_t test_result = st.test_connection();
  944. if (test_result > 0) SERIAL_ECHOPGM("Error: All ");
  945. const char *stat;
  946. switch (test_result) {
  947. default:
  948. case 0: stat = PSTR("OK"); break;
  949. case 1: stat = PSTR("HIGH"); break;
  950. case 2: stat = PSTR("LOW"); break;
  951. }
  952. serialprintPGM(stat);
  953. SERIAL_EOL();
  954. return test_result;
  955. }
  956. void test_tmc_connection(const bool test_x, const bool test_y, const bool test_z, const bool test_e) {
  957. uint8_t axis_connection = 0;
  958. if (test_x) {
  959. #if AXIS_IS_TMC(X)
  960. axis_connection += test_connection(stepperX);
  961. #endif
  962. #if AXIS_IS_TMC(X2)
  963. axis_connection += test_connection(stepperX2);
  964. #endif
  965. }
  966. if (test_y) {
  967. #if AXIS_IS_TMC(Y)
  968. axis_connection += test_connection(stepperY);
  969. #endif
  970. #if AXIS_IS_TMC(Y2)
  971. axis_connection += test_connection(stepperY2);
  972. #endif
  973. }
  974. if (test_z) {
  975. #if AXIS_IS_TMC(Z)
  976. axis_connection += test_connection(stepperZ);
  977. #endif
  978. #if AXIS_IS_TMC(Z2)
  979. axis_connection += test_connection(stepperZ2);
  980. #endif
  981. #if AXIS_IS_TMC(Z3)
  982. axis_connection += test_connection(stepperZ3);
  983. #endif
  984. }
  985. if (test_e) {
  986. #if AXIS_IS_TMC(E0)
  987. axis_connection += test_connection(stepperE0);
  988. #endif
  989. #if AXIS_IS_TMC(E1)
  990. axis_connection += test_connection(stepperE1);
  991. #endif
  992. #if AXIS_IS_TMC(E2)
  993. axis_connection += test_connection(stepperE2);
  994. #endif
  995. #if AXIS_IS_TMC(E3)
  996. axis_connection += test_connection(stepperE3);
  997. #endif
  998. #if AXIS_IS_TMC(E4)
  999. axis_connection += test_connection(stepperE4);
  1000. #endif
  1001. #if AXIS_IS_TMC(E5)
  1002. axis_connection += test_connection(stepperE5);
  1003. #endif
  1004. }
  1005. if (axis_connection) ui.set_status_P(PSTR("TMC CONNECTION ERROR"));
  1006. }
  1007. #endif // HAS_TRINAMIC