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.

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