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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../inc/MarlinConfig.h"
  23. #if HAS_TRINAMIC_CONFIG
  24. #include "tmc_util.h"
  25. #include "../MarlinCore.h"
  26. #include "../module/stepper/indirection.h"
  27. #include "../module/printcounter.h"
  28. #include "../libs/duration_t.h"
  29. #include "../gcode/gcode.h"
  30. #if ENABLED(TMC_DEBUG)
  31. #include "../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. TERN_(HAS_STALLGUARD, data.sg_result_reasonable = false);
  153. #endif
  154. return data;
  155. }
  156. #endif // TMC2208 || TMC2209
  157. #if HAS_DRIVER(TMC2660)
  158. #if ENABLED(TMC_DEBUG)
  159. static uint32_t get_pwm_scale(TMC2660Stepper) { return 0; }
  160. #endif
  161. static TMC_driver_data get_driver_data(TMC2660Stepper &st) {
  162. constexpr uint8_t OT_bp = 1, OTPW_bp = 2;
  163. constexpr uint8_t S2G_bm = 0b11000;
  164. TMC_driver_data data;
  165. const auto ds = data.drv_status = st.DRVSTATUS();
  166. uint8_t spart = ds & 0xFF;
  167. data.is_otpw = TEST(spart, OTPW_bp);
  168. data.is_ot = TEST(spart, OT_bp);
  169. data.is_s2g = !!(ds & S2G_bm);
  170. #if ENABLED(TMC_DEBUG)
  171. constexpr uint8_t STALL_GUARD_bp = 0;
  172. constexpr uint8_t STST_bp = 7, SG_RESULT_sp = 10;
  173. constexpr uint32_t SG_RESULT_bm = 0xFFC00; // 10:19
  174. data.is_stall = TEST(spart, STALL_GUARD_bp);
  175. data.is_standstill = TEST(spart, STST_bp);
  176. data.sg_result = (ds & SG_RESULT_bm) >> SG_RESULT_sp;
  177. data.sg_result_reasonable = true;
  178. #endif
  179. return data;
  180. }
  181. #endif // TMC2660
  182. #if ENABLED(STOP_ON_ERROR)
  183. void report_driver_error(const TMC_driver_data &data) {
  184. SERIAL_ECHOPGM(" driver error detected: 0x");
  185. SERIAL_PRINTLN(data.drv_status, HEX);
  186. if (data.is_ot) SERIAL_ECHOLNPGM("overtemperature");
  187. if (data.is_s2g) SERIAL_ECHOLNPGM("coil short circuit");
  188. TERN_(TMC_DEBUG, tmc_report_all(true, true, true, true));
  189. kill(PSTR("Driver error"));
  190. }
  191. #endif
  192. template<typename TMC>
  193. void report_driver_otpw(TMC &st) {
  194. char timestamp[14];
  195. duration_t elapsed = print_job_timer.duration();
  196. const bool has_days = (elapsed.value > 60*60*24L);
  197. (void)elapsed.toDigital(timestamp, has_days);
  198. SERIAL_EOL();
  199. SERIAL_ECHO(timestamp);
  200. SERIAL_ECHOPGM(": ");
  201. st.printLabel();
  202. SERIAL_ECHOLNPAIR(" driver overtemperature warning! (", st.getMilliamps(), "mA)");
  203. }
  204. template<typename TMC>
  205. void report_polled_driver_data(TMC &st, const TMC_driver_data &data) {
  206. const uint32_t pwm_scale = get_pwm_scale(st);
  207. st.printLabel();
  208. SERIAL_CHAR(':'); SERIAL_PRINT(pwm_scale, DEC);
  209. #if ENABLED(TMC_DEBUG)
  210. #if HAS_TMCX1X0 || HAS_TMC220x
  211. SERIAL_CHAR('/'); SERIAL_PRINT(data.cs_actual, DEC);
  212. #endif
  213. #if HAS_STALLGUARD
  214. SERIAL_CHAR('/');
  215. if (data.sg_result_reasonable)
  216. SERIAL_ECHO(data.sg_result);
  217. else
  218. SERIAL_CHAR('-');
  219. #endif
  220. #endif
  221. SERIAL_CHAR('|');
  222. if (st.error_count) SERIAL_CHAR('E'); // Error
  223. if (data.is_ot) SERIAL_CHAR('O'); // Over-temperature
  224. if (data.is_otpw) SERIAL_CHAR('W'); // over-temperature pre-Warning
  225. #if ENABLED(TMC_DEBUG)
  226. if (data.is_stall) SERIAL_CHAR('G'); // stallGuard
  227. if (data.is_stealth) SERIAL_CHAR('T'); // stealthChop
  228. if (data.is_standstill) SERIAL_CHAR('I'); // standstIll
  229. #endif
  230. if (st.flag_otpw) SERIAL_CHAR('F'); // otpw Flag
  231. SERIAL_CHAR('|');
  232. if (st.otpw_count > 0) SERIAL_PRINT(st.otpw_count, DEC);
  233. SERIAL_CHAR('\t');
  234. }
  235. #if CURRENT_STEP_DOWN > 0
  236. template<typename TMC>
  237. void step_current_down(TMC &st) {
  238. if (st.isEnabled()) {
  239. const uint16_t I_rms = st.getMilliamps() - (CURRENT_STEP_DOWN);
  240. if (I_rms > 50) {
  241. st.rms_current(I_rms);
  242. #if ENABLED(REPORT_CURRENT_CHANGE)
  243. st.printLabel();
  244. SERIAL_ECHOLNPAIR(" current decreased to ", I_rms);
  245. #endif
  246. }
  247. }
  248. }
  249. #else
  250. #define step_current_down(...)
  251. #endif
  252. template<typename TMC>
  253. bool monitor_tmc_driver(TMC &st, const bool need_update_error_counters, const bool need_debug_reporting) {
  254. TMC_driver_data data = get_driver_data(st);
  255. if (data.drv_status == 0xFFFFFFFF || data.drv_status == 0x0) return false;
  256. bool should_step_down = false;
  257. if (need_update_error_counters) {
  258. if (data.is_ot /* | data.s2ga | data.s2gb*/) st.error_count++;
  259. else if (st.error_count > 0) st.error_count--;
  260. #if ENABLED(STOP_ON_ERROR)
  261. if (st.error_count >= 10) {
  262. SERIAL_EOL();
  263. st.printLabel();
  264. report_driver_error(data);
  265. }
  266. #endif
  267. // Report if a warning was triggered
  268. if (data.is_otpw && st.otpw_count == 0)
  269. report_driver_otpw(st);
  270. #if CURRENT_STEP_DOWN > 0
  271. // Decrease current if is_otpw is true and driver is enabled and there's been more than 4 warnings
  272. if (data.is_otpw && st.otpw_count > 4 && st.isEnabled())
  273. should_step_down = true;
  274. #endif
  275. if (data.is_otpw) {
  276. st.otpw_count++;
  277. st.flag_otpw = true;
  278. }
  279. else if (st.otpw_count > 0) st.otpw_count = 0;
  280. }
  281. #if ENABLED(TMC_DEBUG)
  282. if (need_debug_reporting) report_polled_driver_data(st, data);
  283. #endif
  284. return should_step_down;
  285. }
  286. void monitor_tmc_drivers() {
  287. const millis_t ms = millis();
  288. // Poll TMC drivers at the configured interval
  289. static millis_t next_poll = 0;
  290. const bool need_update_error_counters = ELAPSED(ms, next_poll);
  291. if (need_update_error_counters) next_poll = ms + MONITOR_DRIVER_STATUS_INTERVAL_MS;
  292. // Also poll at intervals for debugging
  293. #if ENABLED(TMC_DEBUG)
  294. static millis_t next_debug_reporting = 0;
  295. const bool need_debug_reporting = report_tmc_status_interval && ELAPSED(ms, next_debug_reporting);
  296. if (need_debug_reporting) next_debug_reporting = ms + report_tmc_status_interval;
  297. #else
  298. constexpr bool need_debug_reporting = false;
  299. #endif
  300. if (need_update_error_counters || need_debug_reporting) {
  301. #if AXIS_IS_TMC(X) || AXIS_IS_TMC(X2)
  302. {
  303. bool result = false;
  304. #if AXIS_IS_TMC(X)
  305. if (monitor_tmc_driver(stepperX, need_update_error_counters, need_debug_reporting)) result = true;
  306. #endif
  307. #if AXIS_IS_TMC(X2)
  308. if (monitor_tmc_driver(stepperX2, need_update_error_counters, need_debug_reporting)) result = true;
  309. #endif
  310. if (result) {
  311. #if AXIS_IS_TMC(X)
  312. step_current_down(stepperX);
  313. #endif
  314. #if AXIS_IS_TMC(X2)
  315. step_current_down(stepperX2);
  316. #endif
  317. }
  318. }
  319. #endif
  320. #if AXIS_IS_TMC(Y) || AXIS_IS_TMC(Y2)
  321. {
  322. bool result = false;
  323. #if AXIS_IS_TMC(Y)
  324. if (monitor_tmc_driver(stepperY, need_update_error_counters, need_debug_reporting)) result = true;
  325. #endif
  326. #if AXIS_IS_TMC(Y2)
  327. if (monitor_tmc_driver(stepperY2, need_update_error_counters, need_debug_reporting)) result = true;
  328. #endif
  329. if (result) {
  330. #if AXIS_IS_TMC(Y)
  331. step_current_down(stepperY);
  332. #endif
  333. #if AXIS_IS_TMC(Y2)
  334. step_current_down(stepperY2);
  335. #endif
  336. }
  337. }
  338. #endif
  339. #if AXIS_IS_TMC(Z) || AXIS_IS_TMC(Z2) || AXIS_IS_TMC(Z3) || AXIS_IS_TMC(Z4)
  340. {
  341. bool result = false;
  342. #if AXIS_IS_TMC(Z)
  343. if (monitor_tmc_driver(stepperZ, need_update_error_counters, need_debug_reporting)) result = true;
  344. #endif
  345. #if AXIS_IS_TMC(Z2)
  346. if (monitor_tmc_driver(stepperZ2, need_update_error_counters, need_debug_reporting)) result = true;
  347. #endif
  348. #if AXIS_IS_TMC(Z3)
  349. if (monitor_tmc_driver(stepperZ3, need_update_error_counters, need_debug_reporting)) result = true;
  350. #endif
  351. #if AXIS_IS_TMC(Z4)
  352. if (monitor_tmc_driver(stepperZ4, need_update_error_counters, need_debug_reporting)) result = true;
  353. #endif
  354. if (result) {
  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. #if AXIS_IS_TMC(Z3)
  362. step_current_down(stepperZ3);
  363. #endif
  364. #if AXIS_IS_TMC(Z4)
  365. step_current_down(stepperZ4);
  366. #endif
  367. }
  368. }
  369. #endif
  370. #if AXIS_IS_TMC(E0)
  371. (void)monitor_tmc_driver(stepperE0, need_update_error_counters, need_debug_reporting);
  372. #endif
  373. #if AXIS_IS_TMC(E1)
  374. (void)monitor_tmc_driver(stepperE1, need_update_error_counters, need_debug_reporting);
  375. #endif
  376. #if AXIS_IS_TMC(E2)
  377. (void)monitor_tmc_driver(stepperE2, need_update_error_counters, need_debug_reporting);
  378. #endif
  379. #if AXIS_IS_TMC(E3)
  380. (void)monitor_tmc_driver(stepperE3, need_update_error_counters, need_debug_reporting);
  381. #endif
  382. #if AXIS_IS_TMC(E4)
  383. (void)monitor_tmc_driver(stepperE4, need_update_error_counters, need_debug_reporting);
  384. #endif
  385. #if AXIS_IS_TMC(E5)
  386. (void)monitor_tmc_driver(stepperE5, need_update_error_counters, need_debug_reporting);
  387. #endif
  388. #if AXIS_IS_TMC(E6)
  389. (void)monitor_tmc_driver(stepperE6, need_update_error_counters, need_debug_reporting);
  390. #endif
  391. #if AXIS_IS_TMC(E7)
  392. (void)monitor_tmc_driver(stepperE7, need_update_error_counters, need_debug_reporting);
  393. #endif
  394. if (TERN0(TMC_DEBUG, need_debug_reporting)) SERIAL_EOL();
  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. #if HAS_DRIVER(TMC2209)
  564. static void _tmc_parse_drv_status(TMC2209Stepper &st, const TMC_drv_status_enum i) {
  565. switch (i) {
  566. case TMC_SG_RESULT: SERIAL_PRINT(st.SG_RESULT(), DEC); break;
  567. default: _tmc_parse_drv_status(static_cast<TMC2208Stepper &>(st), i); break;
  568. }
  569. }
  570. #endif
  571. #endif
  572. #if HAS_DRIVER(TMC2660)
  573. static void _tmc_parse_drv_status(TMC2660Stepper, const TMC_drv_status_enum) { }
  574. #endif
  575. template <typename TMC>
  576. static void tmc_status(TMC &st, const TMC_debug_enum i) {
  577. SERIAL_CHAR('\t');
  578. switch (i) {
  579. case TMC_CODES: st.printLabel(); break;
  580. case TMC_ENABLED: serialprint_truefalse(st.isEnabled()); break;
  581. case TMC_CURRENT: SERIAL_ECHO(st.getMilliamps()); break;
  582. case TMC_RMS_CURRENT: SERIAL_ECHO(st.rms_current()); break;
  583. case TMC_MAX_CURRENT: SERIAL_PRINT((float)st.rms_current() * 1.41, 0); break;
  584. case TMC_IRUN:
  585. SERIAL_PRINT(st.irun(), DEC);
  586. SERIAL_ECHOPGM("/31");
  587. break;
  588. case TMC_IHOLD:
  589. SERIAL_PRINT(st.ihold(), DEC);
  590. SERIAL_ECHOPGM("/31");
  591. break;
  592. case TMC_CS_ACTUAL:
  593. SERIAL_PRINT(st.cs_actual(), DEC);
  594. SERIAL_ECHOPGM("/31");
  595. break;
  596. case TMC_VSENSE: print_vsense(st); break;
  597. case TMC_MICROSTEPS: SERIAL_ECHO(st.microsteps()); break;
  598. case TMC_TSTEP: {
  599. const uint32_t tstep_value = st.TSTEP();
  600. if (tstep_value != 0xFFFFF) SERIAL_ECHO(tstep_value); else SERIAL_ECHOPGM("max");
  601. } break;
  602. #if ENABLED(HYBRID_THRESHOLD)
  603. case TMC_TPWMTHRS: SERIAL_ECHO(uint32_t(st.TPWMTHRS())); break;
  604. case TMC_TPWMTHRS_MMS: {
  605. const uint32_t tpwmthrs_val = st.get_pwm_thrs();
  606. if (tpwmthrs_val) SERIAL_ECHO(tpwmthrs_val); else SERIAL_CHAR('-');
  607. } break;
  608. #endif
  609. case TMC_OTPW: serialprint_truefalse(st.otpw()); break;
  610. #if ENABLED(MONITOR_DRIVER_STATUS)
  611. case TMC_OTPW_TRIGGERED: serialprint_truefalse(st.getOTPW()); break;
  612. #endif
  613. case TMC_TOFF: SERIAL_PRINT(st.toff(), DEC); break;
  614. case TMC_TBL: SERIAL_PRINT(st.blank_time(), DEC); break;
  615. case TMC_HEND: SERIAL_PRINT(st.hysteresis_end(), DEC); break;
  616. case TMC_HSTRT: SERIAL_PRINT(st.hysteresis_start(), DEC); break;
  617. default: _tmc_status(st, i); break;
  618. }
  619. }
  620. #if HAS_DRIVER(TMC2660)
  621. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  622. void tmc_status(TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const TMC_debug_enum i) {
  623. SERIAL_CHAR('\t');
  624. switch (i) {
  625. case TMC_CODES: st.printLabel(); break;
  626. case TMC_ENABLED: serialprint_truefalse(st.isEnabled()); break;
  627. case TMC_CURRENT: SERIAL_ECHO(st.getMilliamps()); break;
  628. case TMC_RMS_CURRENT: SERIAL_ECHO(st.rms_current()); break;
  629. case TMC_MAX_CURRENT: SERIAL_PRINT((float)st.rms_current() * 1.41, 0); break;
  630. case TMC_IRUN:
  631. SERIAL_PRINT(st.cs(), DEC);
  632. SERIAL_ECHOPGM("/31");
  633. break;
  634. case TMC_VSENSE: serialprintPGM(st.vsense() ? PSTR("1=.165") : PSTR("0=.310")); break;
  635. case TMC_MICROSTEPS: SERIAL_ECHO(st.microsteps()); break;
  636. //case TMC_OTPW: serialprint_truefalse(st.otpw()); break;
  637. //case TMC_OTPW_TRIGGERED: serialprint_truefalse(st.getOTPW()); break;
  638. case TMC_SGT: SERIAL_PRINT(st.sgt(), DEC); break;
  639. case TMC_TOFF: SERIAL_PRINT(st.toff(), DEC); break;
  640. case TMC_TBL: SERIAL_PRINT(st.blank_time(), DEC); break;
  641. case TMC_HEND: SERIAL_PRINT(st.hysteresis_end(), DEC); break;
  642. case TMC_HSTRT: SERIAL_PRINT(st.hysteresis_start(), DEC); break;
  643. default: break;
  644. }
  645. }
  646. #endif
  647. template <typename TMC>
  648. static void tmc_parse_drv_status(TMC &st, const TMC_drv_status_enum i) {
  649. SERIAL_CHAR('\t');
  650. switch (i) {
  651. case TMC_DRV_CODES: st.printLabel(); break;
  652. case TMC_STST: if (!st.stst()) SERIAL_CHAR('*'); break;
  653. case TMC_OLB: if (st.olb()) SERIAL_CHAR('*'); break;
  654. case TMC_OLA: if (st.ola()) SERIAL_CHAR('*'); break;
  655. case TMC_S2GB: if (st.s2gb()) SERIAL_CHAR('*'); break;
  656. case TMC_S2GA: if (st.s2ga()) SERIAL_CHAR('*'); break;
  657. case TMC_DRV_OTPW: if (st.otpw()) SERIAL_CHAR('*'); break;
  658. case TMC_OT: if (st.ot()) SERIAL_CHAR('*'); break;
  659. case TMC_DRV_STATUS_HEX: {
  660. const uint32_t drv_status = st.DRV_STATUS();
  661. SERIAL_CHAR('\t');
  662. st.printLabel();
  663. SERIAL_CHAR('\t');
  664. print_hex_long(drv_status, ':');
  665. if (drv_status == 0xFFFFFFFF || drv_status == 0) SERIAL_ECHOPGM("\t Bad response!");
  666. SERIAL_EOL();
  667. break;
  668. }
  669. default: _tmc_parse_drv_status(st, i); break;
  670. }
  671. }
  672. 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) {
  673. if (print_x) {
  674. #if AXIS_IS_TMC(X)
  675. tmc_status(stepperX, i);
  676. #endif
  677. #if AXIS_IS_TMC(X2)
  678. tmc_status(stepperX2, i);
  679. #endif
  680. }
  681. if (print_y) {
  682. #if AXIS_IS_TMC(Y)
  683. tmc_status(stepperY, i);
  684. #endif
  685. #if AXIS_IS_TMC(Y2)
  686. tmc_status(stepperY2, i);
  687. #endif
  688. }
  689. if (print_z) {
  690. #if AXIS_IS_TMC(Z)
  691. tmc_status(stepperZ, i);
  692. #endif
  693. #if AXIS_IS_TMC(Z2)
  694. tmc_status(stepperZ2, i);
  695. #endif
  696. #if AXIS_IS_TMC(Z3)
  697. tmc_status(stepperZ3, i);
  698. #endif
  699. #if AXIS_IS_TMC(Z4)
  700. tmc_status(stepperZ4, i);
  701. #endif
  702. }
  703. if (print_e) {
  704. #if AXIS_IS_TMC(E0)
  705. tmc_status(stepperE0, i);
  706. #endif
  707. #if AXIS_IS_TMC(E1)
  708. tmc_status(stepperE1, i);
  709. #endif
  710. #if AXIS_IS_TMC(E2)
  711. tmc_status(stepperE2, i);
  712. #endif
  713. #if AXIS_IS_TMC(E3)
  714. tmc_status(stepperE3, i);
  715. #endif
  716. #if AXIS_IS_TMC(E4)
  717. tmc_status(stepperE4, i);
  718. #endif
  719. #if AXIS_IS_TMC(E5)
  720. tmc_status(stepperE5, i);
  721. #endif
  722. #if AXIS_IS_TMC(E6)
  723. tmc_status(stepperE6, i);
  724. #endif
  725. #if AXIS_IS_TMC(E7)
  726. tmc_status(stepperE7, i);
  727. #endif
  728. }
  729. SERIAL_EOL();
  730. }
  731. 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) {
  732. if (print_x) {
  733. #if AXIS_IS_TMC(X)
  734. tmc_parse_drv_status(stepperX, i);
  735. #endif
  736. #if AXIS_IS_TMC(X2)
  737. tmc_parse_drv_status(stepperX2, i);
  738. #endif
  739. }
  740. if (print_y) {
  741. #if AXIS_IS_TMC(Y)
  742. tmc_parse_drv_status(stepperY, i);
  743. #endif
  744. #if AXIS_IS_TMC(Y2)
  745. tmc_parse_drv_status(stepperY2, i);
  746. #endif
  747. }
  748. if (print_z) {
  749. #if AXIS_IS_TMC(Z)
  750. tmc_parse_drv_status(stepperZ, i);
  751. #endif
  752. #if AXIS_IS_TMC(Z2)
  753. tmc_parse_drv_status(stepperZ2, i);
  754. #endif
  755. #if AXIS_IS_TMC(Z3)
  756. tmc_parse_drv_status(stepperZ3, i);
  757. #endif
  758. #if AXIS_IS_TMC(Z4)
  759. tmc_parse_drv_status(stepperZ4, i);
  760. #endif
  761. }
  762. if (print_e) {
  763. #if AXIS_IS_TMC(E0)
  764. tmc_parse_drv_status(stepperE0, i);
  765. #endif
  766. #if AXIS_IS_TMC(E1)
  767. tmc_parse_drv_status(stepperE1, i);
  768. #endif
  769. #if AXIS_IS_TMC(E2)
  770. tmc_parse_drv_status(stepperE2, i);
  771. #endif
  772. #if AXIS_IS_TMC(E3)
  773. tmc_parse_drv_status(stepperE3, i);
  774. #endif
  775. #if AXIS_IS_TMC(E4)
  776. tmc_parse_drv_status(stepperE4, i);
  777. #endif
  778. #if AXIS_IS_TMC(E5)
  779. tmc_parse_drv_status(stepperE5, i);
  780. #endif
  781. #if AXIS_IS_TMC(E6)
  782. tmc_parse_drv_status(stepperE6, i);
  783. #endif
  784. #if AXIS_IS_TMC(E7)
  785. tmc_parse_drv_status(stepperE7, i);
  786. #endif
  787. }
  788. SERIAL_EOL();
  789. }
  790. /**
  791. * M122 report functions
  792. */
  793. void tmc_report_all(bool print_x, const bool print_y, const bool print_z, const bool print_e) {
  794. #define TMC_REPORT(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); tmc_debug_loop(ITEM, print_x, print_y, print_z, print_e); }while(0)
  795. #define DRV_REPORT(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); drv_status_loop(ITEM, print_x, print_y, print_z, print_e); }while(0)
  796. TMC_REPORT("\t", TMC_CODES);
  797. #if HAS_DRIVER(TMC2209)
  798. TMC_REPORT("Address\t", TMC_UART_ADDR);
  799. #endif
  800. TMC_REPORT("Enabled\t", TMC_ENABLED);
  801. TMC_REPORT("Set current", TMC_CURRENT);
  802. TMC_REPORT("RMS current", TMC_RMS_CURRENT);
  803. TMC_REPORT("MAX current", TMC_MAX_CURRENT);
  804. TMC_REPORT("Run current", TMC_IRUN);
  805. TMC_REPORT("Hold current", TMC_IHOLD);
  806. #if HAS_DRIVER(TMC2160) || HAS_DRIVER(TMC5160)
  807. TMC_REPORT("Global scaler", TMC_GLOBAL_SCALER);
  808. #endif
  809. TMC_REPORT("CS actual", TMC_CS_ACTUAL);
  810. TMC_REPORT("PWM scale", TMC_PWM_SCALE);
  811. #if HAS_DRIVER(TMC2130) || HAS_DRIVER(TMC2224) || HAS_DRIVER(TMC2660) || HAS_TMC220x
  812. TMC_REPORT("vsense\t", TMC_VSENSE);
  813. #endif
  814. TMC_REPORT("stealthChop", TMC_STEALTHCHOP);
  815. TMC_REPORT("msteps\t", TMC_MICROSTEPS);
  816. TMC_REPORT("tstep\t", TMC_TSTEP);
  817. TMC_REPORT("PWM thresh.", TMC_TPWMTHRS);
  818. TMC_REPORT("[mm/s]\t", TMC_TPWMTHRS_MMS);
  819. TMC_REPORT("OT prewarn", TMC_OTPW);
  820. #if ENABLED(MONITOR_DRIVER_STATUS)
  821. TMC_REPORT("triggered\n OTP\t", TMC_OTPW_TRIGGERED);
  822. #endif
  823. TMC_REPORT("off time", TMC_TOFF);
  824. TMC_REPORT("blank time", TMC_TBL);
  825. TMC_REPORT("hysteresis\n -end\t", TMC_HEND);
  826. TMC_REPORT(" -start\t", TMC_HSTRT);
  827. TMC_REPORT("Stallguard thrs", TMC_SGT);
  828. DRV_REPORT("DRVSTATUS", TMC_DRV_CODES);
  829. #if HAS_TMCX1X0 || HAS_TMC220x
  830. DRV_REPORT("sg_result", TMC_SG_RESULT);
  831. #endif
  832. #if HAS_TMCX1X0
  833. DRV_REPORT("stallguard", TMC_STALLGUARD);
  834. DRV_REPORT("fsactive", TMC_FSACTIVE);
  835. #endif
  836. DRV_REPORT("stst\t", TMC_STST);
  837. DRV_REPORT("olb\t", TMC_OLB);
  838. DRV_REPORT("ola\t", TMC_OLA);
  839. DRV_REPORT("s2gb\t", TMC_S2GB);
  840. DRV_REPORT("s2ga\t", TMC_S2GA);
  841. DRV_REPORT("otpw\t", TMC_DRV_OTPW);
  842. DRV_REPORT("ot\t", TMC_OT);
  843. #if HAS_TMC220x
  844. DRV_REPORT("157C\t", TMC_T157);
  845. DRV_REPORT("150C\t", TMC_T150);
  846. DRV_REPORT("143C\t", TMC_T143);
  847. DRV_REPORT("120C\t", TMC_T120);
  848. DRV_REPORT("s2vsa\t", TMC_S2VSA);
  849. DRV_REPORT("s2vsb\t", TMC_S2VSB);
  850. #endif
  851. DRV_REPORT("Driver registers:\n",TMC_DRV_STATUS_HEX);
  852. SERIAL_EOL();
  853. }
  854. #define PRINT_TMC_REGISTER(REG_CASE) case TMC_GET_##REG_CASE: print_hex_long(st.REG_CASE(), ':'); break
  855. #if HAS_TMCX1X0
  856. static void tmc_get_ic_registers(TMC2130Stepper &st, const TMC_get_registers_enum i) {
  857. switch (i) {
  858. PRINT_TMC_REGISTER(TCOOLTHRS);
  859. PRINT_TMC_REGISTER(THIGH);
  860. PRINT_TMC_REGISTER(COOLCONF);
  861. default: SERIAL_CHAR('\t'); break;
  862. }
  863. }
  864. #endif
  865. #if HAS_TMC220x
  866. static void tmc_get_ic_registers(TMC2208Stepper, const TMC_get_registers_enum) { SERIAL_CHAR('\t'); }
  867. #endif
  868. #if HAS_TRINAMIC_CONFIG
  869. template<class TMC>
  870. static void tmc_get_registers(TMC &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(GCONF);
  874. PRINT_TMC_REGISTER(IHOLD_IRUN);
  875. PRINT_TMC_REGISTER(GSTAT);
  876. PRINT_TMC_REGISTER(IOIN);
  877. PRINT_TMC_REGISTER(TPOWERDOWN);
  878. PRINT_TMC_REGISTER(TSTEP);
  879. PRINT_TMC_REGISTER(TPWMTHRS);
  880. PRINT_TMC_REGISTER(CHOPCONF);
  881. PRINT_TMC_REGISTER(PWMCONF);
  882. PRINT_TMC_REGISTER(PWM_SCALE);
  883. PRINT_TMC_REGISTER(DRV_STATUS);
  884. default: tmc_get_ic_registers(st, i); break;
  885. }
  886. SERIAL_CHAR('\t');
  887. }
  888. #endif
  889. #if HAS_DRIVER(TMC2660)
  890. template <char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  891. static void tmc_get_registers(TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const TMC_get_registers_enum i) {
  892. switch (i) {
  893. case TMC_AXIS_CODES: SERIAL_CHAR('\t'); st.printLabel(); break;
  894. PRINT_TMC_REGISTER(DRVCONF);
  895. PRINT_TMC_REGISTER(DRVCTRL);
  896. PRINT_TMC_REGISTER(CHOPCONF);
  897. PRINT_TMC_REGISTER(DRVSTATUS);
  898. PRINT_TMC_REGISTER(SGCSCONF);
  899. PRINT_TMC_REGISTER(SMARTEN);
  900. default: SERIAL_CHAR('\t'); break;
  901. }
  902. SERIAL_CHAR('\t');
  903. }
  904. #endif
  905. 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) {
  906. if (print_x) {
  907. #if AXIS_IS_TMC(X)
  908. tmc_get_registers(stepperX, i);
  909. #endif
  910. #if AXIS_IS_TMC(X2)
  911. tmc_get_registers(stepperX2, i);
  912. #endif
  913. }
  914. if (print_y) {
  915. #if AXIS_IS_TMC(Y)
  916. tmc_get_registers(stepperY, i);
  917. #endif
  918. #if AXIS_IS_TMC(Y2)
  919. tmc_get_registers(stepperY2, i);
  920. #endif
  921. }
  922. if (print_z) {
  923. #if AXIS_IS_TMC(Z)
  924. tmc_get_registers(stepperZ, i);
  925. #endif
  926. #if AXIS_IS_TMC(Z2)
  927. tmc_get_registers(stepperZ2, i);
  928. #endif
  929. #if AXIS_IS_TMC(Z3)
  930. tmc_get_registers(stepperZ3, i);
  931. #endif
  932. #if AXIS_IS_TMC(Z4)
  933. tmc_get_registers(stepperZ4, i);
  934. #endif
  935. }
  936. if (print_e) {
  937. #if AXIS_IS_TMC(E0)
  938. tmc_get_registers(stepperE0, i);
  939. #endif
  940. #if AXIS_IS_TMC(E1)
  941. tmc_get_registers(stepperE1, i);
  942. #endif
  943. #if AXIS_IS_TMC(E2)
  944. tmc_get_registers(stepperE2, i);
  945. #endif
  946. #if AXIS_IS_TMC(E3)
  947. tmc_get_registers(stepperE3, i);
  948. #endif
  949. #if AXIS_IS_TMC(E4)
  950. tmc_get_registers(stepperE4, i);
  951. #endif
  952. #if AXIS_IS_TMC(E5)
  953. tmc_get_registers(stepperE5, i);
  954. #endif
  955. #if AXIS_IS_TMC(E6)
  956. tmc_get_registers(stepperE6, i);
  957. #endif
  958. #if AXIS_IS_TMC(E7)
  959. tmc_get_registers(stepperE7, i);
  960. #endif
  961. }
  962. SERIAL_EOL();
  963. }
  964. void tmc_get_registers(bool print_x, bool print_y, bool print_z, bool print_e) {
  965. #define _TMC_GET_REG(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); tmc_get_registers(ITEM, print_x, print_y, print_z, print_e); }while(0)
  966. #define TMC_GET_REG(NAME, TABS) _TMC_GET_REG(STRINGIFY(NAME) TABS, TMC_GET_##NAME)
  967. _TMC_GET_REG("\t", TMC_AXIS_CODES);
  968. TMC_GET_REG(GCONF, "\t\t");
  969. TMC_GET_REG(IHOLD_IRUN, "\t");
  970. TMC_GET_REG(GSTAT, "\t\t");
  971. TMC_GET_REG(IOIN, "\t\t");
  972. TMC_GET_REG(TPOWERDOWN, "\t");
  973. TMC_GET_REG(TSTEP, "\t\t");
  974. TMC_GET_REG(TPWMTHRS, "\t");
  975. TMC_GET_REG(TCOOLTHRS, "\t");
  976. TMC_GET_REG(THIGH, "\t\t");
  977. TMC_GET_REG(CHOPCONF, "\t");
  978. TMC_GET_REG(COOLCONF, "\t");
  979. TMC_GET_REG(PWMCONF, "\t");
  980. TMC_GET_REG(PWM_SCALE, "\t");
  981. TMC_GET_REG(DRV_STATUS, "\t");
  982. }
  983. #endif // TMC_DEBUG
  984. #if USE_SENSORLESS
  985. bool tmc_enable_stallguard(TMC2130Stepper &st) {
  986. const bool stealthchop_was_enabled = st.en_pwm_mode();
  987. st.TCOOLTHRS(0xFFFFF);
  988. st.en_pwm_mode(false);
  989. st.diag1_stall(true);
  990. return stealthchop_was_enabled;
  991. }
  992. void tmc_disable_stallguard(TMC2130Stepper &st, const bool restore_stealth) {
  993. st.TCOOLTHRS(0);
  994. st.en_pwm_mode(restore_stealth);
  995. st.diag1_stall(false);
  996. }
  997. bool tmc_enable_stallguard(TMC2209Stepper &st) {
  998. const bool stealthchop_was_enabled = !st.en_spreadCycle();
  999. st.TCOOLTHRS(0xFFFFF);
  1000. st.en_spreadCycle(false);
  1001. return stealthchop_was_enabled;
  1002. }
  1003. void tmc_disable_stallguard(TMC2209Stepper &st, const bool restore_stealth) {
  1004. st.en_spreadCycle(!restore_stealth);
  1005. st.TCOOLTHRS(0);
  1006. }
  1007. bool tmc_enable_stallguard(TMC2660Stepper) {
  1008. // TODO
  1009. return false;
  1010. }
  1011. void tmc_disable_stallguard(TMC2660Stepper, const bool) {};
  1012. #endif // USE_SENSORLESS
  1013. #if HAS_TMC_SPI
  1014. #define SET_CS_PIN(st) OUT_WRITE(st##_CS_PIN, HIGH)
  1015. void tmc_init_cs_pins() {
  1016. #if AXIS_HAS_SPI(X)
  1017. SET_CS_PIN(X);
  1018. #endif
  1019. #if AXIS_HAS_SPI(Y)
  1020. SET_CS_PIN(Y);
  1021. #endif
  1022. #if AXIS_HAS_SPI(Z)
  1023. SET_CS_PIN(Z);
  1024. #endif
  1025. #if AXIS_HAS_SPI(X2)
  1026. SET_CS_PIN(X2);
  1027. #endif
  1028. #if AXIS_HAS_SPI(Y2)
  1029. SET_CS_PIN(Y2);
  1030. #endif
  1031. #if AXIS_HAS_SPI(Z2)
  1032. SET_CS_PIN(Z2);
  1033. #endif
  1034. #if AXIS_HAS_SPI(Z3)
  1035. SET_CS_PIN(Z3);
  1036. #endif
  1037. #if AXIS_HAS_SPI(Z4)
  1038. SET_CS_PIN(Z4);
  1039. #endif
  1040. #if AXIS_HAS_SPI(E0)
  1041. SET_CS_PIN(E0);
  1042. #endif
  1043. #if AXIS_HAS_SPI(E1)
  1044. SET_CS_PIN(E1);
  1045. #endif
  1046. #if AXIS_HAS_SPI(E2)
  1047. SET_CS_PIN(E2);
  1048. #endif
  1049. #if AXIS_HAS_SPI(E3)
  1050. SET_CS_PIN(E3);
  1051. #endif
  1052. #if AXIS_HAS_SPI(E4)
  1053. SET_CS_PIN(E4);
  1054. #endif
  1055. #if AXIS_HAS_SPI(E5)
  1056. SET_CS_PIN(E5);
  1057. #endif
  1058. #if AXIS_HAS_SPI(E6)
  1059. SET_CS_PIN(E6);
  1060. #endif
  1061. #if AXIS_HAS_SPI(E7)
  1062. SET_CS_PIN(E7);
  1063. #endif
  1064. }
  1065. #endif // HAS_TMC_SPI
  1066. template<typename TMC>
  1067. static bool test_connection(TMC &st) {
  1068. SERIAL_ECHOPGM("Testing ");
  1069. st.printLabel();
  1070. SERIAL_ECHOPGM(" connection... ");
  1071. const uint8_t test_result = st.test_connection();
  1072. if (test_result > 0) SERIAL_ECHOPGM("Error: All ");
  1073. const char *stat;
  1074. switch (test_result) {
  1075. default:
  1076. case 0: stat = PSTR("OK"); break;
  1077. case 1: stat = PSTR("HIGH"); break;
  1078. case 2: stat = PSTR("LOW"); break;
  1079. }
  1080. serialprintPGM(stat);
  1081. SERIAL_EOL();
  1082. return test_result;
  1083. }
  1084. void test_tmc_connection(const bool test_x, const bool test_y, const bool test_z, const bool test_e) {
  1085. uint8_t axis_connection = 0;
  1086. if (test_x) {
  1087. #if AXIS_IS_TMC(X)
  1088. axis_connection += test_connection(stepperX);
  1089. #endif
  1090. #if AXIS_IS_TMC(X2)
  1091. axis_connection += test_connection(stepperX2);
  1092. #endif
  1093. }
  1094. if (test_y) {
  1095. #if AXIS_IS_TMC(Y)
  1096. axis_connection += test_connection(stepperY);
  1097. #endif
  1098. #if AXIS_IS_TMC(Y2)
  1099. axis_connection += test_connection(stepperY2);
  1100. #endif
  1101. }
  1102. if (test_z) {
  1103. #if AXIS_IS_TMC(Z)
  1104. axis_connection += test_connection(stepperZ);
  1105. #endif
  1106. #if AXIS_IS_TMC(Z2)
  1107. axis_connection += test_connection(stepperZ2);
  1108. #endif
  1109. #if AXIS_IS_TMC(Z3)
  1110. axis_connection += test_connection(stepperZ3);
  1111. #endif
  1112. #if AXIS_IS_TMC(Z4)
  1113. axis_connection += test_connection(stepperZ4);
  1114. #endif
  1115. }
  1116. if (test_e) {
  1117. #if AXIS_IS_TMC(E0)
  1118. axis_connection += test_connection(stepperE0);
  1119. #endif
  1120. #if AXIS_IS_TMC(E1)
  1121. axis_connection += test_connection(stepperE1);
  1122. #endif
  1123. #if AXIS_IS_TMC(E2)
  1124. axis_connection += test_connection(stepperE2);
  1125. #endif
  1126. #if AXIS_IS_TMC(E3)
  1127. axis_connection += test_connection(stepperE3);
  1128. #endif
  1129. #if AXIS_IS_TMC(E4)
  1130. axis_connection += test_connection(stepperE4);
  1131. #endif
  1132. #if AXIS_IS_TMC(E5)
  1133. axis_connection += test_connection(stepperE5);
  1134. #endif
  1135. #if AXIS_IS_TMC(E6)
  1136. axis_connection += test_connection(stepperE6);
  1137. #endif
  1138. #if AXIS_IS_TMC(E7)
  1139. axis_connection += test_connection(stepperE7);
  1140. #endif
  1141. }
  1142. if (axis_connection) LCD_MESSAGEPGM(MSG_ERROR_TMC);
  1143. }
  1144. #endif // HAS_TRINAMIC_CONFIG