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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../inc/MarlinConfig.h"
  23. #if HAS_TRINAMIC_CONFIG
  24. #include "tmc_util.h"
  25. #include "../MarlinCore.h"
  26. #include "../module/stepper/indirection.h"
  27. #include "../module/printcounter.h"
  28. #include "../libs/duration_t.h"
  29. #include "../gcode/gcode.h"
  30. #if ENABLED(TMC_DEBUG)
  31. #include "../module/planner.h"
  32. #include "../libs/hex_print.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_PWM_SCALE_SUM,
  429. TMC_PWM_SCALE_AUTO,
  430. TMC_PWM_OFS_AUTO,
  431. TMC_PWM_GRAD_AUTO,
  432. TMC_VSENSE,
  433. TMC_STEALTHCHOP,
  434. TMC_MICROSTEPS,
  435. TMC_TSTEP,
  436. TMC_TPWMTHRS,
  437. TMC_TPWMTHRS_MMS,
  438. TMC_OTPW,
  439. TMC_OTPW_TRIGGERED,
  440. TMC_TOFF,
  441. TMC_TBL,
  442. TMC_HEND,
  443. TMC_HSTRT,
  444. TMC_SGT,
  445. TMC_MSCNT
  446. };
  447. enum TMC_drv_status_enum : char {
  448. TMC_DRV_CODES,
  449. TMC_STST,
  450. TMC_OLB,
  451. TMC_OLA,
  452. TMC_S2GB,
  453. TMC_S2GA,
  454. TMC_DRV_OTPW,
  455. TMC_OT,
  456. TMC_STALLGUARD,
  457. TMC_DRV_CS_ACTUAL,
  458. TMC_FSACTIVE,
  459. TMC_SG_RESULT,
  460. TMC_DRV_STATUS_HEX,
  461. TMC_T157,
  462. TMC_T150,
  463. TMC_T143,
  464. TMC_T120,
  465. TMC_STEALTH,
  466. TMC_S2VSB,
  467. TMC_S2VSA
  468. };
  469. enum TMC_get_registers_enum : char {
  470. TMC_AXIS_CODES,
  471. TMC_GET_GCONF,
  472. TMC_GET_IHOLD_IRUN,
  473. TMC_GET_GSTAT,
  474. TMC_GET_IOIN,
  475. TMC_GET_TPOWERDOWN,
  476. TMC_GET_TSTEP,
  477. TMC_GET_TPWMTHRS,
  478. TMC_GET_TCOOLTHRS,
  479. TMC_GET_THIGH,
  480. TMC_GET_CHOPCONF,
  481. TMC_GET_COOLCONF,
  482. TMC_GET_PWMCONF,
  483. TMC_GET_PWM_SCALE,
  484. TMC_GET_DRV_STATUS,
  485. TMC_GET_DRVCONF,
  486. TMC_GET_DRVCTRL,
  487. TMC_GET_DRVSTATUS,
  488. TMC_GET_SGCSCONF,
  489. TMC_GET_SMARTEN
  490. };
  491. template<class TMC>
  492. static void print_vsense(TMC &st) { serialprintPGM(st.vsense() ? PSTR("1=.18") : PSTR("0=.325")); }
  493. #if HAS_DRIVER(TMC2130) || HAS_DRIVER(TMC5130)
  494. static void _tmc_status(TMC2130Stepper &st, const TMC_debug_enum i) {
  495. switch (i) {
  496. case TMC_PWM_SCALE: SERIAL_PRINT(st.PWM_SCALE(), DEC); break;
  497. case TMC_SGT: SERIAL_PRINT(st.sgt(), DEC); break;
  498. case TMC_STEALTHCHOP: serialprint_truefalse(st.en_pwm_mode()); break;
  499. default: break;
  500. }
  501. }
  502. #endif
  503. #if HAS_TMCX1X0
  504. static void _tmc_parse_drv_status(TMC2130Stepper &st, const TMC_drv_status_enum i) {
  505. switch (i) {
  506. case TMC_STALLGUARD: if (st.stallguard()) SERIAL_CHAR('*'); break;
  507. case TMC_SG_RESULT: SERIAL_PRINT(st.sg_result(), DEC); break;
  508. case TMC_FSACTIVE: if (st.fsactive()) SERIAL_CHAR('*'); break;
  509. case TMC_DRV_CS_ACTUAL: SERIAL_PRINT(st.cs_actual(), DEC); break;
  510. default: break;
  511. }
  512. }
  513. #endif
  514. #if HAS_DRIVER(TMC2160) || HAS_DRIVER(TMC5160)
  515. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  516. void print_vsense(TMCMarlin<TMC2160Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &) { }
  517. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  518. void print_vsense(TMCMarlin<TMC5160Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &) { }
  519. static void _tmc_status(TMC2160Stepper &st, const TMC_debug_enum i) {
  520. switch (i) {
  521. case TMC_PWM_SCALE: SERIAL_PRINT(st.PWM_SCALE(), DEC); break;
  522. case TMC_SGT: SERIAL_PRINT(st.sgt(), DEC); break;
  523. case TMC_STEALTHCHOP: serialprint_truefalse(st.en_pwm_mode()); break;
  524. case TMC_GLOBAL_SCALER:
  525. {
  526. uint16_t value = st.GLOBAL_SCALER();
  527. SERIAL_PRINT(value ?: 256, DEC);
  528. SERIAL_ECHOPGM("/256");
  529. }
  530. break;
  531. default: break;
  532. }
  533. }
  534. #endif
  535. #if HAS_TMC220x
  536. static void _tmc_status(TMC2208Stepper &st, const TMC_debug_enum i) {
  537. switch (i) {
  538. case TMC_PWM_SCALE_SUM: SERIAL_PRINT(st.pwm_scale_sum(), DEC); break;
  539. case TMC_PWM_SCALE_AUTO: SERIAL_PRINT(st.pwm_scale_auto(), DEC); break;
  540. case TMC_PWM_OFS_AUTO: SERIAL_PRINT(st.pwm_ofs_auto(), DEC); break;
  541. case TMC_PWM_GRAD_AUTO: SERIAL_PRINT(st.pwm_grad_auto(), DEC); break;
  542. case TMC_STEALTHCHOP: serialprint_truefalse(st.stealth()); break;
  543. case TMC_S2VSA: if (st.s2vsa()) SERIAL_CHAR('*'); break;
  544. case TMC_S2VSB: if (st.s2vsb()) SERIAL_CHAR('*'); break;
  545. default: break;
  546. }
  547. }
  548. #if HAS_DRIVER(TMC2209)
  549. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  550. static void _tmc_status(TMCMarlin<TMC2209Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const TMC_debug_enum i) {
  551. switch (i) {
  552. case TMC_SGT: SERIAL_PRINT(st.SGTHRS(), DEC); break;
  553. case TMC_UART_ADDR: SERIAL_PRINT(st.get_address(), DEC); break;
  554. default:
  555. TMC2208Stepper *parent = &st;
  556. _tmc_status(*parent, i);
  557. break;
  558. }
  559. }
  560. #endif
  561. static void _tmc_parse_drv_status(TMC2208Stepper &st, const TMC_drv_status_enum i) {
  562. switch (i) {
  563. case TMC_T157: if (st.t157()) SERIAL_CHAR('*'); break;
  564. case TMC_T150: if (st.t150()) SERIAL_CHAR('*'); break;
  565. case TMC_T143: if (st.t143()) SERIAL_CHAR('*'); break;
  566. case TMC_T120: if (st.t120()) SERIAL_CHAR('*'); break;
  567. case TMC_DRV_CS_ACTUAL: SERIAL_PRINT(st.cs_actual(), DEC); break;
  568. default: break;
  569. }
  570. }
  571. #if HAS_DRIVER(TMC2209)
  572. static void _tmc_parse_drv_status(TMC2209Stepper &st, const TMC_drv_status_enum i) {
  573. switch (i) {
  574. case TMC_SG_RESULT: SERIAL_PRINT(st.SG_RESULT(), DEC); break;
  575. default: _tmc_parse_drv_status(static_cast<TMC2208Stepper &>(st), i); break;
  576. }
  577. }
  578. #endif
  579. #endif
  580. #if HAS_DRIVER(TMC2660)
  581. static void _tmc_parse_drv_status(TMC2660Stepper, const TMC_drv_status_enum) { }
  582. #endif
  583. template <typename TMC>
  584. static void tmc_status(TMC &st, const TMC_debug_enum i) {
  585. SERIAL_CHAR('\t');
  586. switch (i) {
  587. case TMC_CODES: st.printLabel(); break;
  588. case TMC_ENABLED: serialprint_truefalse(st.isEnabled()); break;
  589. case TMC_CURRENT: SERIAL_ECHO(st.getMilliamps()); break;
  590. case TMC_RMS_CURRENT: SERIAL_ECHO(st.rms_current()); break;
  591. case TMC_MAX_CURRENT: SERIAL_PRINT((float)st.rms_current() * 1.41, 0); break;
  592. case TMC_IRUN:
  593. SERIAL_PRINT(st.irun(), DEC);
  594. SERIAL_ECHOPGM("/31");
  595. break;
  596. case TMC_IHOLD:
  597. SERIAL_PRINT(st.ihold(), DEC);
  598. SERIAL_ECHOPGM("/31");
  599. break;
  600. case TMC_CS_ACTUAL:
  601. SERIAL_PRINT(st.cs_actual(), DEC);
  602. SERIAL_ECHOPGM("/31");
  603. break;
  604. case TMC_VSENSE: print_vsense(st); break;
  605. case TMC_MICROSTEPS: SERIAL_ECHO(st.microsteps()); break;
  606. case TMC_TSTEP: {
  607. const uint32_t tstep_value = st.TSTEP();
  608. if (tstep_value != 0xFFFFF) SERIAL_ECHO(tstep_value); else SERIAL_ECHOPGM("max");
  609. } break;
  610. #if ENABLED(HYBRID_THRESHOLD)
  611. case TMC_TPWMTHRS: SERIAL_ECHO(uint32_t(st.TPWMTHRS())); break;
  612. case TMC_TPWMTHRS_MMS: {
  613. const uint32_t tpwmthrs_val = st.get_pwm_thrs();
  614. if (tpwmthrs_val) SERIAL_ECHO(tpwmthrs_val); else SERIAL_CHAR('-');
  615. } break;
  616. #endif
  617. case TMC_OTPW: serialprint_truefalse(st.otpw()); break;
  618. #if ENABLED(MONITOR_DRIVER_STATUS)
  619. case TMC_OTPW_TRIGGERED: serialprint_truefalse(st.getOTPW()); break;
  620. #endif
  621. case TMC_TOFF: SERIAL_PRINT(st.toff(), DEC); break;
  622. case TMC_TBL: SERIAL_PRINT(st.blank_time(), DEC); break;
  623. case TMC_HEND: SERIAL_PRINT(st.hysteresis_end(), DEC); break;
  624. case TMC_HSTRT: SERIAL_PRINT(st.hysteresis_start(), DEC); break;
  625. case TMC_MSCNT: SERIAL_PRINT(st.get_microstep_counter(), DEC); break;
  626. default: _tmc_status(st, i); break;
  627. }
  628. }
  629. #if HAS_DRIVER(TMC2660)
  630. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  631. void tmc_status(TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const TMC_debug_enum i) {
  632. SERIAL_CHAR('\t');
  633. switch (i) {
  634. case TMC_CODES: st.printLabel(); break;
  635. case TMC_ENABLED: serialprint_truefalse(st.isEnabled()); break;
  636. case TMC_CURRENT: SERIAL_ECHO(st.getMilliamps()); break;
  637. case TMC_RMS_CURRENT: SERIAL_ECHO(st.rms_current()); break;
  638. case TMC_MAX_CURRENT: SERIAL_PRINT((float)st.rms_current() * 1.41, 0); break;
  639. case TMC_IRUN:
  640. SERIAL_PRINT(st.cs(), DEC);
  641. SERIAL_ECHOPGM("/31");
  642. break;
  643. case TMC_VSENSE: serialprintPGM(st.vsense() ? PSTR("1=.165") : PSTR("0=.310")); break;
  644. case TMC_MICROSTEPS: SERIAL_ECHO(st.microsteps()); break;
  645. //case TMC_OTPW: serialprint_truefalse(st.otpw()); break;
  646. //case TMC_OTPW_TRIGGERED: serialprint_truefalse(st.getOTPW()); break;
  647. case TMC_SGT: SERIAL_PRINT(st.sgt(), DEC); break;
  648. case TMC_TOFF: SERIAL_PRINT(st.toff(), DEC); break;
  649. case TMC_TBL: SERIAL_PRINT(st.blank_time(), DEC); break;
  650. case TMC_HEND: SERIAL_PRINT(st.hysteresis_end(), DEC); break;
  651. case TMC_HSTRT: SERIAL_PRINT(st.hysteresis_start(), DEC); break;
  652. default: break;
  653. }
  654. }
  655. #endif
  656. template <typename TMC>
  657. static void tmc_parse_drv_status(TMC &st, const TMC_drv_status_enum i) {
  658. SERIAL_CHAR('\t');
  659. switch (i) {
  660. case TMC_DRV_CODES: st.printLabel(); break;
  661. case TMC_STST: if (!st.stst()) SERIAL_CHAR('*'); break;
  662. case TMC_OLB: if (st.olb()) SERIAL_CHAR('*'); break;
  663. case TMC_OLA: if (st.ola()) SERIAL_CHAR('*'); break;
  664. case TMC_S2GB: if (st.s2gb()) SERIAL_CHAR('*'); break;
  665. case TMC_S2GA: if (st.s2ga()) SERIAL_CHAR('*'); break;
  666. case TMC_DRV_OTPW: if (st.otpw()) SERIAL_CHAR('*'); break;
  667. case TMC_OT: if (st.ot()) SERIAL_CHAR('*'); break;
  668. case TMC_DRV_STATUS_HEX: {
  669. const uint32_t drv_status = st.DRV_STATUS();
  670. SERIAL_CHAR('\t');
  671. st.printLabel();
  672. SERIAL_CHAR('\t');
  673. print_hex_long(drv_status, ':');
  674. if (drv_status == 0xFFFFFFFF || drv_status == 0) SERIAL_ECHOPGM("\t Bad response!");
  675. SERIAL_EOL();
  676. break;
  677. }
  678. default: _tmc_parse_drv_status(st, i); break;
  679. }
  680. }
  681. 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) {
  682. if (print_x) {
  683. #if AXIS_IS_TMC(X)
  684. tmc_status(stepperX, i);
  685. #endif
  686. #if AXIS_IS_TMC(X2)
  687. tmc_status(stepperX2, i);
  688. #endif
  689. }
  690. if (print_y) {
  691. #if AXIS_IS_TMC(Y)
  692. tmc_status(stepperY, i);
  693. #endif
  694. #if AXIS_IS_TMC(Y2)
  695. tmc_status(stepperY2, i);
  696. #endif
  697. }
  698. if (print_z) {
  699. #if AXIS_IS_TMC(Z)
  700. tmc_status(stepperZ, i);
  701. #endif
  702. #if AXIS_IS_TMC(Z2)
  703. tmc_status(stepperZ2, i);
  704. #endif
  705. #if AXIS_IS_TMC(Z3)
  706. tmc_status(stepperZ3, i);
  707. #endif
  708. #if AXIS_IS_TMC(Z4)
  709. tmc_status(stepperZ4, i);
  710. #endif
  711. }
  712. if (print_e) {
  713. #if AXIS_IS_TMC(E0)
  714. tmc_status(stepperE0, i);
  715. #endif
  716. #if AXIS_IS_TMC(E1)
  717. tmc_status(stepperE1, i);
  718. #endif
  719. #if AXIS_IS_TMC(E2)
  720. tmc_status(stepperE2, i);
  721. #endif
  722. #if AXIS_IS_TMC(E3)
  723. tmc_status(stepperE3, i);
  724. #endif
  725. #if AXIS_IS_TMC(E4)
  726. tmc_status(stepperE4, i);
  727. #endif
  728. #if AXIS_IS_TMC(E5)
  729. tmc_status(stepperE5, i);
  730. #endif
  731. #if AXIS_IS_TMC(E6)
  732. tmc_status(stepperE6, i);
  733. #endif
  734. #if AXIS_IS_TMC(E7)
  735. tmc_status(stepperE7, i);
  736. #endif
  737. }
  738. SERIAL_EOL();
  739. }
  740. 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) {
  741. if (print_x) {
  742. #if AXIS_IS_TMC(X)
  743. tmc_parse_drv_status(stepperX, i);
  744. #endif
  745. #if AXIS_IS_TMC(X2)
  746. tmc_parse_drv_status(stepperX2, i);
  747. #endif
  748. }
  749. if (print_y) {
  750. #if AXIS_IS_TMC(Y)
  751. tmc_parse_drv_status(stepperY, i);
  752. #endif
  753. #if AXIS_IS_TMC(Y2)
  754. tmc_parse_drv_status(stepperY2, i);
  755. #endif
  756. }
  757. if (print_z) {
  758. #if AXIS_IS_TMC(Z)
  759. tmc_parse_drv_status(stepperZ, i);
  760. #endif
  761. #if AXIS_IS_TMC(Z2)
  762. tmc_parse_drv_status(stepperZ2, i);
  763. #endif
  764. #if AXIS_IS_TMC(Z3)
  765. tmc_parse_drv_status(stepperZ3, i);
  766. #endif
  767. #if AXIS_IS_TMC(Z4)
  768. tmc_parse_drv_status(stepperZ4, i);
  769. #endif
  770. }
  771. if (print_e) {
  772. #if AXIS_IS_TMC(E0)
  773. tmc_parse_drv_status(stepperE0, i);
  774. #endif
  775. #if AXIS_IS_TMC(E1)
  776. tmc_parse_drv_status(stepperE1, i);
  777. #endif
  778. #if AXIS_IS_TMC(E2)
  779. tmc_parse_drv_status(stepperE2, i);
  780. #endif
  781. #if AXIS_IS_TMC(E3)
  782. tmc_parse_drv_status(stepperE3, i);
  783. #endif
  784. #if AXIS_IS_TMC(E4)
  785. tmc_parse_drv_status(stepperE4, i);
  786. #endif
  787. #if AXIS_IS_TMC(E5)
  788. tmc_parse_drv_status(stepperE5, i);
  789. #endif
  790. #if AXIS_IS_TMC(E6)
  791. tmc_parse_drv_status(stepperE6, i);
  792. #endif
  793. #if AXIS_IS_TMC(E7)
  794. tmc_parse_drv_status(stepperE7, i);
  795. #endif
  796. }
  797. SERIAL_EOL();
  798. }
  799. /**
  800. * M122 report functions
  801. */
  802. void tmc_report_all(bool print_x, const bool print_y, const bool print_z, const bool print_e) {
  803. #define TMC_REPORT(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); tmc_debug_loop(ITEM, print_x, print_y, print_z, print_e); }while(0)
  804. #define DRV_REPORT(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); drv_status_loop(ITEM, print_x, print_y, print_z, print_e); }while(0)
  805. TMC_REPORT("\t", TMC_CODES);
  806. #if HAS_DRIVER(TMC2209)
  807. TMC_REPORT("Address\t", TMC_UART_ADDR);
  808. #endif
  809. TMC_REPORT("Enabled\t", TMC_ENABLED);
  810. TMC_REPORT("Set current", TMC_CURRENT);
  811. TMC_REPORT("RMS current", TMC_RMS_CURRENT);
  812. TMC_REPORT("MAX current", TMC_MAX_CURRENT);
  813. TMC_REPORT("Run current", TMC_IRUN);
  814. TMC_REPORT("Hold current", TMC_IHOLD);
  815. #if HAS_DRIVER(TMC2160) || HAS_DRIVER(TMC5160)
  816. TMC_REPORT("Global scaler", TMC_GLOBAL_SCALER);
  817. #endif
  818. TMC_REPORT("CS actual", TMC_CS_ACTUAL);
  819. TMC_REPORT("PWM scale", TMC_PWM_SCALE);
  820. #if HAS_DRIVER(TMC2130) || HAS_DRIVER(TMC2224) || HAS_DRIVER(TMC2660) || HAS_TMC220x
  821. TMC_REPORT("vsense\t", TMC_VSENSE);
  822. #endif
  823. TMC_REPORT("stealthChop", TMC_STEALTHCHOP);
  824. TMC_REPORT("msteps\t", TMC_MICROSTEPS);
  825. TMC_REPORT("tstep\t", TMC_TSTEP);
  826. TMC_REPORT("PWM thresh.", TMC_TPWMTHRS);
  827. TMC_REPORT("[mm/s]\t", TMC_TPWMTHRS_MMS);
  828. TMC_REPORT("OT prewarn", TMC_OTPW);
  829. #if ENABLED(MONITOR_DRIVER_STATUS)
  830. TMC_REPORT("triggered\n OTP\t", TMC_OTPW_TRIGGERED);
  831. #endif
  832. #if HAS_TMC220x
  833. TMC_REPORT("pwm scale sum", TMC_PWM_SCALE_SUM);
  834. TMC_REPORT("pwm scale auto", TMC_PWM_SCALE_AUTO);
  835. TMC_REPORT("pwm offset auto", TMC_PWM_OFS_AUTO);
  836. TMC_REPORT("pwm grad auto", TMC_PWM_GRAD_AUTO);
  837. #endif
  838. TMC_REPORT("off time", TMC_TOFF);
  839. TMC_REPORT("blank time", TMC_TBL);
  840. TMC_REPORT("hysteresis\n -end\t", TMC_HEND);
  841. TMC_REPORT(" -start\t", TMC_HSTRT);
  842. TMC_REPORT("Stallguard thrs", TMC_SGT);
  843. TMC_REPORT("uStep count", TMC_MSCNT);
  844. DRV_REPORT("DRVSTATUS", TMC_DRV_CODES);
  845. #if HAS_TMCX1X0 || HAS_TMC220x
  846. DRV_REPORT("sg_result", TMC_SG_RESULT);
  847. #endif
  848. #if HAS_TMCX1X0
  849. DRV_REPORT("stallguard", TMC_STALLGUARD);
  850. DRV_REPORT("fsactive", TMC_FSACTIVE);
  851. #endif
  852. DRV_REPORT("stst\t", TMC_STST);
  853. DRV_REPORT("olb\t", TMC_OLB);
  854. DRV_REPORT("ola\t", TMC_OLA);
  855. DRV_REPORT("s2gb\t", TMC_S2GB);
  856. DRV_REPORT("s2ga\t", TMC_S2GA);
  857. DRV_REPORT("otpw\t", TMC_DRV_OTPW);
  858. DRV_REPORT("ot\t", TMC_OT);
  859. #if HAS_TMC220x
  860. DRV_REPORT("157C\t", TMC_T157);
  861. DRV_REPORT("150C\t", TMC_T150);
  862. DRV_REPORT("143C\t", TMC_T143);
  863. DRV_REPORT("120C\t", TMC_T120);
  864. DRV_REPORT("s2vsa\t", TMC_S2VSA);
  865. DRV_REPORT("s2vsb\t", TMC_S2VSB);
  866. #endif
  867. DRV_REPORT("Driver registers:\n",TMC_DRV_STATUS_HEX);
  868. SERIAL_EOL();
  869. }
  870. #define PRINT_TMC_REGISTER(REG_CASE) case TMC_GET_##REG_CASE: print_hex_long(st.REG_CASE(), ':'); break
  871. #if HAS_TMCX1X0
  872. static void tmc_get_ic_registers(TMC2130Stepper &st, const TMC_get_registers_enum i) {
  873. switch (i) {
  874. PRINT_TMC_REGISTER(TCOOLTHRS);
  875. PRINT_TMC_REGISTER(THIGH);
  876. PRINT_TMC_REGISTER(COOLCONF);
  877. default: SERIAL_CHAR('\t'); break;
  878. }
  879. }
  880. #endif
  881. #if HAS_TMC220x
  882. static void tmc_get_ic_registers(TMC2208Stepper, const TMC_get_registers_enum) { SERIAL_CHAR('\t'); }
  883. #endif
  884. #if HAS_TRINAMIC_CONFIG
  885. template<class TMC>
  886. static void tmc_get_registers(TMC &st, const TMC_get_registers_enum i) {
  887. switch (i) {
  888. case TMC_AXIS_CODES: SERIAL_CHAR('\t'); st.printLabel(); break;
  889. PRINT_TMC_REGISTER(GCONF);
  890. PRINT_TMC_REGISTER(IHOLD_IRUN);
  891. PRINT_TMC_REGISTER(GSTAT);
  892. PRINT_TMC_REGISTER(IOIN);
  893. PRINT_TMC_REGISTER(TPOWERDOWN);
  894. PRINT_TMC_REGISTER(TSTEP);
  895. PRINT_TMC_REGISTER(TPWMTHRS);
  896. PRINT_TMC_REGISTER(CHOPCONF);
  897. PRINT_TMC_REGISTER(PWMCONF);
  898. PRINT_TMC_REGISTER(PWM_SCALE);
  899. PRINT_TMC_REGISTER(DRV_STATUS);
  900. default: tmc_get_ic_registers(st, i); break;
  901. }
  902. SERIAL_CHAR('\t');
  903. }
  904. #endif
  905. #if HAS_DRIVER(TMC2660)
  906. template <char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  907. static void tmc_get_registers(TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const TMC_get_registers_enum i) {
  908. switch (i) {
  909. case TMC_AXIS_CODES: SERIAL_CHAR('\t'); st.printLabel(); break;
  910. PRINT_TMC_REGISTER(DRVCONF);
  911. PRINT_TMC_REGISTER(DRVCTRL);
  912. PRINT_TMC_REGISTER(CHOPCONF);
  913. PRINT_TMC_REGISTER(DRVSTATUS);
  914. PRINT_TMC_REGISTER(SGCSCONF);
  915. PRINT_TMC_REGISTER(SMARTEN);
  916. default: SERIAL_CHAR('\t'); break;
  917. }
  918. SERIAL_CHAR('\t');
  919. }
  920. #endif
  921. 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) {
  922. if (print_x) {
  923. #if AXIS_IS_TMC(X)
  924. tmc_get_registers(stepperX, i);
  925. #endif
  926. #if AXIS_IS_TMC(X2)
  927. tmc_get_registers(stepperX2, i);
  928. #endif
  929. }
  930. if (print_y) {
  931. #if AXIS_IS_TMC(Y)
  932. tmc_get_registers(stepperY, i);
  933. #endif
  934. #if AXIS_IS_TMC(Y2)
  935. tmc_get_registers(stepperY2, i);
  936. #endif
  937. }
  938. if (print_z) {
  939. #if AXIS_IS_TMC(Z)
  940. tmc_get_registers(stepperZ, i);
  941. #endif
  942. #if AXIS_IS_TMC(Z2)
  943. tmc_get_registers(stepperZ2, i);
  944. #endif
  945. #if AXIS_IS_TMC(Z3)
  946. tmc_get_registers(stepperZ3, i);
  947. #endif
  948. #if AXIS_IS_TMC(Z4)
  949. tmc_get_registers(stepperZ4, i);
  950. #endif
  951. }
  952. if (print_e) {
  953. #if AXIS_IS_TMC(E0)
  954. tmc_get_registers(stepperE0, i);
  955. #endif
  956. #if AXIS_IS_TMC(E1)
  957. tmc_get_registers(stepperE1, i);
  958. #endif
  959. #if AXIS_IS_TMC(E2)
  960. tmc_get_registers(stepperE2, i);
  961. #endif
  962. #if AXIS_IS_TMC(E3)
  963. tmc_get_registers(stepperE3, i);
  964. #endif
  965. #if AXIS_IS_TMC(E4)
  966. tmc_get_registers(stepperE4, i);
  967. #endif
  968. #if AXIS_IS_TMC(E5)
  969. tmc_get_registers(stepperE5, i);
  970. #endif
  971. #if AXIS_IS_TMC(E6)
  972. tmc_get_registers(stepperE6, i);
  973. #endif
  974. #if AXIS_IS_TMC(E7)
  975. tmc_get_registers(stepperE7, i);
  976. #endif
  977. }
  978. SERIAL_EOL();
  979. }
  980. void tmc_get_registers(bool print_x, bool print_y, bool print_z, bool print_e) {
  981. #define _TMC_GET_REG(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); tmc_get_registers(ITEM, print_x, print_y, print_z, print_e); }while(0)
  982. #define TMC_GET_REG(NAME, TABS) _TMC_GET_REG(STRINGIFY(NAME) TABS, TMC_GET_##NAME)
  983. _TMC_GET_REG("\t", TMC_AXIS_CODES);
  984. TMC_GET_REG(GCONF, "\t\t");
  985. TMC_GET_REG(IHOLD_IRUN, "\t");
  986. TMC_GET_REG(GSTAT, "\t\t");
  987. TMC_GET_REG(IOIN, "\t\t");
  988. TMC_GET_REG(TPOWERDOWN, "\t");
  989. TMC_GET_REG(TSTEP, "\t\t");
  990. TMC_GET_REG(TPWMTHRS, "\t");
  991. TMC_GET_REG(TCOOLTHRS, "\t");
  992. TMC_GET_REG(THIGH, "\t\t");
  993. TMC_GET_REG(CHOPCONF, "\t");
  994. TMC_GET_REG(COOLCONF, "\t");
  995. TMC_GET_REG(PWMCONF, "\t");
  996. TMC_GET_REG(PWM_SCALE, "\t");
  997. TMC_GET_REG(DRV_STATUS, "\t");
  998. }
  999. #endif // TMC_DEBUG
  1000. #if USE_SENSORLESS
  1001. bool tmc_enable_stallguard(TMC2130Stepper &st) {
  1002. const bool stealthchop_was_enabled = st.en_pwm_mode();
  1003. st.TCOOLTHRS(0xFFFFF);
  1004. st.en_pwm_mode(false);
  1005. st.diag1_stall(true);
  1006. return stealthchop_was_enabled;
  1007. }
  1008. void tmc_disable_stallguard(TMC2130Stepper &st, const bool restore_stealth) {
  1009. st.TCOOLTHRS(0);
  1010. st.en_pwm_mode(restore_stealth);
  1011. st.diag1_stall(false);
  1012. }
  1013. bool tmc_enable_stallguard(TMC2209Stepper &st) {
  1014. const bool stealthchop_was_enabled = !st.en_spreadCycle();
  1015. st.TCOOLTHRS(0xFFFFF);
  1016. st.en_spreadCycle(false);
  1017. return stealthchop_was_enabled;
  1018. }
  1019. void tmc_disable_stallguard(TMC2209Stepper &st, const bool restore_stealth) {
  1020. st.en_spreadCycle(!restore_stealth);
  1021. st.TCOOLTHRS(0);
  1022. }
  1023. bool tmc_enable_stallguard(TMC2660Stepper) {
  1024. // TODO
  1025. return false;
  1026. }
  1027. void tmc_disable_stallguard(TMC2660Stepper, const bool) {};
  1028. #endif // USE_SENSORLESS
  1029. #if HAS_TMC_SPI
  1030. #define SET_CS_PIN(st) OUT_WRITE(st##_CS_PIN, HIGH)
  1031. void tmc_init_cs_pins() {
  1032. #if AXIS_HAS_SPI(X)
  1033. SET_CS_PIN(X);
  1034. #endif
  1035. #if AXIS_HAS_SPI(Y)
  1036. SET_CS_PIN(Y);
  1037. #endif
  1038. #if AXIS_HAS_SPI(Z)
  1039. SET_CS_PIN(Z);
  1040. #endif
  1041. #if AXIS_HAS_SPI(X2)
  1042. SET_CS_PIN(X2);
  1043. #endif
  1044. #if AXIS_HAS_SPI(Y2)
  1045. SET_CS_PIN(Y2);
  1046. #endif
  1047. #if AXIS_HAS_SPI(Z2)
  1048. SET_CS_PIN(Z2);
  1049. #endif
  1050. #if AXIS_HAS_SPI(Z3)
  1051. SET_CS_PIN(Z3);
  1052. #endif
  1053. #if AXIS_HAS_SPI(Z4)
  1054. SET_CS_PIN(Z4);
  1055. #endif
  1056. #if AXIS_HAS_SPI(E0)
  1057. SET_CS_PIN(E0);
  1058. #endif
  1059. #if AXIS_HAS_SPI(E1)
  1060. SET_CS_PIN(E1);
  1061. #endif
  1062. #if AXIS_HAS_SPI(E2)
  1063. SET_CS_PIN(E2);
  1064. #endif
  1065. #if AXIS_HAS_SPI(E3)
  1066. SET_CS_PIN(E3);
  1067. #endif
  1068. #if AXIS_HAS_SPI(E4)
  1069. SET_CS_PIN(E4);
  1070. #endif
  1071. #if AXIS_HAS_SPI(E5)
  1072. SET_CS_PIN(E5);
  1073. #endif
  1074. #if AXIS_HAS_SPI(E6)
  1075. SET_CS_PIN(E6);
  1076. #endif
  1077. #if AXIS_HAS_SPI(E7)
  1078. SET_CS_PIN(E7);
  1079. #endif
  1080. }
  1081. #endif // HAS_TMC_SPI
  1082. template<typename TMC>
  1083. static bool test_connection(TMC &st) {
  1084. SERIAL_ECHOPGM("Testing ");
  1085. st.printLabel();
  1086. SERIAL_ECHOPGM(" connection... ");
  1087. const uint8_t test_result = st.test_connection();
  1088. if (test_result > 0) SERIAL_ECHOPGM("Error: All ");
  1089. const char *stat;
  1090. switch (test_result) {
  1091. default:
  1092. case 0: stat = PSTR("OK"); break;
  1093. case 1: stat = PSTR("HIGH"); break;
  1094. case 2: stat = PSTR("LOW"); break;
  1095. }
  1096. serialprintPGM(stat);
  1097. SERIAL_EOL();
  1098. return test_result;
  1099. }
  1100. void test_tmc_connection(const bool test_x, const bool test_y, const bool test_z, const bool test_e) {
  1101. uint8_t axis_connection = 0;
  1102. if (test_x) {
  1103. #if AXIS_IS_TMC(X)
  1104. axis_connection += test_connection(stepperX);
  1105. #endif
  1106. #if AXIS_IS_TMC(X2)
  1107. axis_connection += test_connection(stepperX2);
  1108. #endif
  1109. }
  1110. if (test_y) {
  1111. #if AXIS_IS_TMC(Y)
  1112. axis_connection += test_connection(stepperY);
  1113. #endif
  1114. #if AXIS_IS_TMC(Y2)
  1115. axis_connection += test_connection(stepperY2);
  1116. #endif
  1117. }
  1118. if (test_z) {
  1119. #if AXIS_IS_TMC(Z)
  1120. axis_connection += test_connection(stepperZ);
  1121. #endif
  1122. #if AXIS_IS_TMC(Z2)
  1123. axis_connection += test_connection(stepperZ2);
  1124. #endif
  1125. #if AXIS_IS_TMC(Z3)
  1126. axis_connection += test_connection(stepperZ3);
  1127. #endif
  1128. #if AXIS_IS_TMC(Z4)
  1129. axis_connection += test_connection(stepperZ4);
  1130. #endif
  1131. }
  1132. if (test_e) {
  1133. #if AXIS_IS_TMC(E0)
  1134. axis_connection += test_connection(stepperE0);
  1135. #endif
  1136. #if AXIS_IS_TMC(E1)
  1137. axis_connection += test_connection(stepperE1);
  1138. #endif
  1139. #if AXIS_IS_TMC(E2)
  1140. axis_connection += test_connection(stepperE2);
  1141. #endif
  1142. #if AXIS_IS_TMC(E3)
  1143. axis_connection += test_connection(stepperE3);
  1144. #endif
  1145. #if AXIS_IS_TMC(E4)
  1146. axis_connection += test_connection(stepperE4);
  1147. #endif
  1148. #if AXIS_IS_TMC(E5)
  1149. axis_connection += test_connection(stepperE5);
  1150. #endif
  1151. #if AXIS_IS_TMC(E6)
  1152. axis_connection += test_connection(stepperE6);
  1153. #endif
  1154. #if AXIS_IS_TMC(E7)
  1155. axis_connection += test_connection(stepperE7);
  1156. #endif
  1157. }
  1158. if (axis_connection) LCD_MESSAGEPGM(MSG_ERROR_TMC);
  1159. }
  1160. #endif // HAS_TRINAMIC_CONFIG