My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

tmc_util.cpp 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../inc/MarlinConfig.h"
  23. #if HAS_TRINAMIC
  24. #include "tmc_util.h"
  25. #include "../Marlin.h"
  26. #include "../module/stepper_indirection.h"
  27. #include "../module/printcounter.h"
  28. #include "../libs/duration_t.h"
  29. #include "../gcode/gcode.h"
  30. #if ENABLED(TMC_DEBUG)
  31. #include "../module/planner.h"
  32. #include "../libs/hex_print_routines.h"
  33. #if ENABLED(MONITOR_DRIVER_STATUS)
  34. static uint16_t report_tmc_status_interval; // = 0
  35. #endif
  36. #endif
  37. #if HAS_LCD_MENU
  38. #include "../module/stepper.h"
  39. #endif
  40. /**
  41. * Check for over temperature or short to ground error flags.
  42. * Report and log warning of overtemperature condition.
  43. * Reduce driver current in a persistent otpw condition.
  44. * Keep track of otpw counter so we don't reduce current on a single instance,
  45. * and so we don't repeatedly report warning before the condition is cleared.
  46. */
  47. #if ENABLED(MONITOR_DRIVER_STATUS)
  48. struct TMC_driver_data {
  49. uint32_t drv_status;
  50. bool is_otpw:1,
  51. is_ot:1,
  52. is_s2g:1,
  53. is_error:1
  54. #if ENABLED(TMC_DEBUG)
  55. , is_stall:1
  56. , is_stealth:1
  57. , is_standstill:1
  58. #if HAS_STALLGUARD
  59. , sg_result_reasonable:1
  60. #endif
  61. #endif
  62. ;
  63. #if ENABLED(TMC_DEBUG)
  64. #if HAS_TMCX1X0 || HAS_DRIVER(TMC2208)
  65. uint8_t cs_actual;
  66. #endif
  67. #if HAS_STALLGUARD
  68. uint16_t sg_result;
  69. #endif
  70. #endif
  71. };
  72. #if HAS_TMCX1X0
  73. #if ENABLED(TMC_DEBUG)
  74. static uint32_t get_pwm_scale(TMC2130Stepper &st) { return st.PWM_SCALE(); }
  75. #endif
  76. static TMC_driver_data get_driver_data(TMC2130Stepper &st) {
  77. constexpr uint16_t SG_RESULT_bm = 0x3FF; // 0:9
  78. constexpr uint8_t STEALTH_bp = 14, CS_ACTUAL_sb = 16;
  79. constexpr uint32_t CS_ACTUAL_bm = 0x1F0000; // 16:20
  80. constexpr uint8_t STALL_GUARD_bp = 24, OT_bp = 25, OTPW_bp = 26;
  81. constexpr uint32_t S2G_bm = 0x18000000;
  82. constexpr uint8_t STST_bp = 31;
  83. TMC_driver_data data;
  84. data.drv_status = st.DRV_STATUS();
  85. #ifdef __AVR__
  86. // 8-bit optimization saves up to 70 bytes of PROGMEM per axis
  87. uint8_t spart;
  88. #if ENABLED(TMC_DEBUG)
  89. data.sg_result = data.drv_status & SG_RESULT_bm;
  90. spart = data.drv_status >> 8;
  91. data.is_stealth = !!(spart & _BV(STEALTH_bp - 8));
  92. spart = data.drv_status >> 16;
  93. data.cs_actual = spart & (CS_ACTUAL_bm >> 16);
  94. #endif
  95. spart = data.drv_status >> 24;
  96. data.is_ot = !!(spart & _BV(OT_bp - 24));
  97. data.is_otpw = !!(spart & _BV(OTPW_bp - 24));
  98. data.is_s2g = !!(spart & (S2G_bm >> 24));
  99. #if ENABLED(TMC_DEBUG)
  100. data.is_stall = !!(spart & _BV(STALL_GUARD_bp - 24));
  101. data.is_standstill = !!(spart & _BV(STST_bp - 24));
  102. data.sg_result_reasonable = !data.is_standstill; // sg_result has no reasonable meaning while standstill
  103. #endif
  104. #else // !__AVR__
  105. data.is_ot = !!(data.drv_status & _BV(OT_bp));
  106. data.is_otpw = !!(data.drv_status & _BV(OTPW_bp));
  107. data.is_s2g = !!(data.drv_status & S2G_bm);
  108. #if ENABLED(TMC_DEBUG)
  109. data.sg_result = data.drv_status & SG_RESULT_bm;
  110. data.is_stealth = !!(data.drv_status & _BV(STEALTH_bp));
  111. data.cs_actual = (data.drv_status & CS_ACTUAL_bm) >> CS_ACTUAL_sb;
  112. data.is_stall = !!(data.drv_status & _BV(STALL_GUARD_bp));
  113. data.is_standstill = !!(data.drv_status & _BV(STST_bp));
  114. data.sg_result_reasonable = !data.is_standstill; // sg_result has no reasonable meaning while standstill
  115. #endif
  116. #endif // !__AVR__
  117. return data;
  118. }
  119. #endif // HAS_TMCX1X0
  120. #if HAS_DRIVER(TMC2208)
  121. #if ENABLED(TMC_DEBUG)
  122. static uint32_t get_pwm_scale(TMC2208Stepper &st) { return st.pwm_scale_sum(); }
  123. #endif
  124. static TMC_driver_data get_driver_data(TMC2208Stepper &st) {
  125. constexpr uint8_t OTPW_bp = 0, OT_bp = 1;
  126. constexpr uint8_t S2G_bm = 0b11110; // 2..5
  127. constexpr uint8_t CS_ACTUAL_sb = 16;
  128. constexpr uint32_t CS_ACTUAL_bm = 0x1F0000; // 16:20
  129. constexpr uint8_t STEALTH_bp = 30, STST_bp = 31;
  130. TMC_driver_data data;
  131. data.drv_status = st.DRV_STATUS();
  132. data.is_otpw = !!(data.drv_status & _BV(OTPW_bp));
  133. data.is_ot = !!(data.drv_status & _BV(OT_bp));
  134. data.is_s2g = !!(data.drv_status & S2G_bm);
  135. #if ENABLED(TMC_DEBUG)
  136. #ifdef __AVR__
  137. // 8-bit optimization saves up to 12 bytes of PROGMEM per axis
  138. uint8_t spart = data.drv_status >> 16;
  139. data.cs_actual = spart & (CS_ACTUAL_bm >> 16);
  140. spart = data.drv_status >> 24;
  141. data.is_stealth = !!(spart & _BV(STEALTH_bp - 24));
  142. data.is_standstill = !!(spart & _BV(STST_bp - 24));
  143. #else
  144. data.cs_actual = (data.drv_status & CS_ACTUAL_bm) >> CS_ACTUAL_sb;
  145. data.is_stealth = !!(data.drv_status & _BV(STEALTH_bp));
  146. data.is_standstill = !!(data.drv_status & _BV(STST_bp));
  147. #endif
  148. #if HAS_STALLGUARD
  149. data.sg_result_reasonable = false;
  150. #endif
  151. #endif
  152. return data;
  153. }
  154. #endif // TMC2208
  155. #if HAS_DRIVER(TMC2660)
  156. #if ENABLED(TMC_DEBUG)
  157. static uint32_t get_pwm_scale(TMC2660Stepper) { return 0; }
  158. #endif
  159. static TMC_driver_data get_driver_data(TMC2660Stepper &st) {
  160. constexpr uint8_t STALL_GUARD_bp = 0;
  161. constexpr uint8_t OT_bp = 1, OTPW_bp = 2;
  162. constexpr uint8_t S2G_bm = 0b11000;
  163. constexpr uint8_t STST_bp = 7, SG_RESULT_sp = 10;
  164. constexpr uint32_t SG_RESULT_bm = 0xFFC00; // 10:19
  165. TMC_driver_data data;
  166. data.drv_status = st.DRVSTATUS();
  167. uint8_t spart = data.drv_status & 0xFF;
  168. data.is_otpw = !!(spart & _BV(OTPW_bp));
  169. data.is_ot = !!(spart & _BV(OT_bp));
  170. data.is_s2g = !!(data.drv_status & S2G_bm);
  171. #if ENABLED(TMC_DEBUG)
  172. data.is_stall = !!(spart & _BV(STALL_GUARD_bp));
  173. data.is_standstill = !!(spart & _BV(STST_bp));
  174. data.sg_result = (data.drv_status & SG_RESULT_bm) >> SG_RESULT_sp;
  175. data.sg_result_reasonable = true;
  176. #endif
  177. return data;
  178. }
  179. #endif // TMC2660
  180. #if ENABLED(STOP_ON_ERROR)
  181. void report_driver_error(const TMC_driver_data &data) {
  182. SERIAL_ECHOPGM(" driver error detected: 0x");
  183. SERIAL_PRINTLN(data.drv_status, HEX);
  184. if (data.is_ot) SERIAL_ECHOLNPGM("overtemperature");
  185. if (data.is_s2g) SERIAL_ECHOLNPGM("coil short circuit");
  186. #if ENABLED(TMC_DEBUG)
  187. tmc_report_all(true, true, true, true);
  188. #endif
  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_ECHOPGM(" driver overtemperature warning! (");
  203. SERIAL_ECHO(st.getMilliamps());
  204. SERIAL_ECHOLNPGM("mA)");
  205. }
  206. template<typename TMC>
  207. void report_polled_driver_data(TMC &st, const TMC_driver_data &data) {
  208. const uint32_t pwm_scale = get_pwm_scale(st);
  209. st.printLabel();
  210. SERIAL_CHAR(':'); SERIAL_PRINT(pwm_scale, DEC);
  211. #if ENABLED(TMC_DEBUG)
  212. #if HAS_TMCX1X0 || HAS_DRIVER(TMC2208)
  213. SERIAL_CHAR('/'); SERIAL_PRINT(data.cs_actual, DEC);
  214. #endif
  215. #if HAS_STALLGUARD
  216. SERIAL_CHAR('/');
  217. if (data.sg_result_reasonable)
  218. SERIAL_ECHO(data.sg_result);
  219. else
  220. SERIAL_CHAR('-');
  221. #endif
  222. #endif
  223. SERIAL_CHAR('|');
  224. if (st.error_count) SERIAL_CHAR('E'); // Error
  225. if (data.is_ot) SERIAL_CHAR('O'); // Over-temperature
  226. if (data.is_otpw) SERIAL_CHAR('W'); // over-temperature pre-Warning
  227. #if ENABLED(TMC_DEBUG)
  228. if (data.is_stall) SERIAL_CHAR('G'); // stallGuard
  229. if (data.is_stealth) SERIAL_CHAR('T'); // stealthChop
  230. if (data.is_standstill) SERIAL_CHAR('I'); // standstIll
  231. #endif
  232. if (st.flag_otpw) SERIAL_CHAR('F'); // otpw Flag
  233. SERIAL_CHAR('|');
  234. if (st.otpw_count > 0) SERIAL_PRINT(st.otpw_count, DEC);
  235. SERIAL_CHAR('\t');
  236. }
  237. template<typename TMC>
  238. void monitor_tmc_driver(TMC &st, const bool need_update_error_counters, const bool need_debug_reporting) {
  239. TMC_driver_data data = get_driver_data(st);
  240. if (data.drv_status == 0xFFFFFFFF || data.drv_status == 0x0) return;
  241. if (need_update_error_counters) {
  242. if (data.is_ot /* | data.s2ga | data.s2gb*/) st.error_count++;
  243. else if (st.error_count > 0) st.error_count--;
  244. #if ENABLED(STOP_ON_ERROR)
  245. if (st.error_count >= 10) {
  246. SERIAL_EOL();
  247. st.printLabel();
  248. report_driver_error(data);
  249. }
  250. #endif
  251. // Report if a warning was triggered
  252. if (data.is_otpw && st.otpw_count == 0)
  253. report_driver_otpw(st);
  254. #if CURRENT_STEP_DOWN > 0
  255. // Decrease current if is_otpw is true and driver is enabled and there's been more than 4 warnings
  256. if (data.is_otpw && st.otpw_count > 4) {
  257. uint16_t I_rms = st.getMilliamps();
  258. if (st.isEnabled() && I_rms > 100) {
  259. st.rms_current(I_rms - (CURRENT_STEP_DOWN));
  260. #if ENABLED(REPORT_CURRENT_CHANGE)
  261. st.printLabel();
  262. SERIAL_ECHOLNPAIR(" current decreased to ", st.getMilliamps());
  263. #endif
  264. }
  265. }
  266. #endif
  267. if (data.is_otpw) {
  268. st.otpw_count++;
  269. st.flag_otpw = true;
  270. }
  271. else if (st.otpw_count > 0) st.otpw_count = 0;
  272. }
  273. #if ENABLED(TMC_DEBUG)
  274. if (need_debug_reporting)
  275. report_polled_driver_data(st, data);
  276. #endif
  277. }
  278. #define HAS_HW_COMMS(ST) AXIS_DRIVER_TYPE(ST, TMC2130) || AXIS_DRIVER_TYPE(ST, TMC2160) || AXIS_DRIVER_TYPE(ST, TMC2660) || AXIS_DRIVER_TYPE(ST, TMC5130) || AXIS_DRIVER_TYPE(ST, TMC5160) || (AXIS_DRIVER_TYPE(ST, TMC2208) && defined(ST##_HARDWARE_SERIAL))
  279. void monitor_tmc_driver() {
  280. static millis_t next_poll = 0;
  281. const millis_t ms = millis();
  282. bool need_update_error_counters = ELAPSED(ms, next_poll);
  283. bool need_debug_reporting = false;
  284. if (need_update_error_counters)
  285. next_poll = ms + MONITOR_DRIVER_STATUS_INTERVAL_MS;
  286. #if ENABLED(TMC_DEBUG)
  287. static millis_t next_debug_reporting = 0;
  288. if (report_tmc_status_interval && ELAPSED(ms, next_debug_reporting)) {
  289. need_debug_reporting = true;
  290. next_debug_reporting = ms + report_tmc_status_interval;
  291. }
  292. #endif
  293. if (need_update_error_counters || need_debug_reporting) {
  294. #if HAS_HW_COMMS(X)
  295. monitor_tmc_driver(stepperX, need_update_error_counters, need_debug_reporting);
  296. #endif
  297. #if HAS_HW_COMMS(Y)
  298. monitor_tmc_driver(stepperY, need_update_error_counters, need_debug_reporting);
  299. #endif
  300. #if HAS_HW_COMMS(Z)
  301. monitor_tmc_driver(stepperZ, need_update_error_counters, need_debug_reporting);
  302. #endif
  303. #if HAS_HW_COMMS(X2)
  304. monitor_tmc_driver(stepperX2, need_update_error_counters, need_debug_reporting);
  305. #endif
  306. #if HAS_HW_COMMS(Y2)
  307. monitor_tmc_driver(stepperY2, need_update_error_counters, need_debug_reporting);
  308. #endif
  309. #if HAS_HW_COMMS(Z2)
  310. monitor_tmc_driver(stepperZ2, need_update_error_counters, need_debug_reporting);
  311. #endif
  312. #if HAS_HW_COMMS(Z3)
  313. monitor_tmc_driver(stepperZ3, need_update_error_counters, need_debug_reporting);
  314. #endif
  315. #if HAS_HW_COMMS(E0)
  316. monitor_tmc_driver(stepperE0, need_update_error_counters, need_debug_reporting);
  317. #endif
  318. #if HAS_HW_COMMS(E1)
  319. monitor_tmc_driver(stepperE1, need_update_error_counters, need_debug_reporting);
  320. #endif
  321. #if HAS_HW_COMMS(E2)
  322. monitor_tmc_driver(stepperE2, need_update_error_counters, need_debug_reporting);
  323. #endif
  324. #if HAS_HW_COMMS(E3)
  325. monitor_tmc_driver(stepperE3, need_update_error_counters, need_debug_reporting);
  326. #endif
  327. #if HAS_HW_COMMS(E4)
  328. monitor_tmc_driver(stepperE4, need_update_error_counters, need_debug_reporting);
  329. #endif
  330. #if HAS_HW_COMMS(E5)
  331. monitor_tmc_driver(stepperE5, need_update_error_counters, need_debug_reporting);
  332. #endif
  333. #if ENABLED(TMC_DEBUG)
  334. if (need_debug_reporting) SERIAL_EOL();
  335. #endif
  336. }
  337. }
  338. #endif // MONITOR_DRIVER_STATUS
  339. #if ENABLED(TMC_DEBUG)
  340. /**
  341. * M122 [S<0|1>] [Pnnn] Enable periodic status reports
  342. */
  343. #if ENABLED(MONITOR_DRIVER_STATUS)
  344. void tmc_set_report_interval(const uint16_t update_interval) {
  345. if ((report_tmc_status_interval = update_interval))
  346. SERIAL_ECHOLNPGM("axis:pwm_scale"
  347. #if HAS_STEALTHCHOP
  348. "/current_scale"
  349. #endif
  350. #if HAS_STALLGUARD
  351. "/mech_load"
  352. #endif
  353. "|flags|warncount"
  354. );
  355. }
  356. #endif
  357. enum TMC_debug_enum : char {
  358. TMC_CODES,
  359. TMC_ENABLED,
  360. TMC_CURRENT,
  361. TMC_RMS_CURRENT,
  362. TMC_MAX_CURRENT,
  363. TMC_IRUN,
  364. TMC_IHOLD,
  365. TMC_GLOBAL_SCALER,
  366. TMC_CS_ACTUAL,
  367. TMC_PWM_SCALE,
  368. TMC_VSENSE,
  369. TMC_STEALTHCHOP,
  370. TMC_MICROSTEPS,
  371. TMC_TSTEP,
  372. TMC_TPWMTHRS,
  373. TMC_TPWMTHRS_MMS,
  374. TMC_OTPW,
  375. TMC_OTPW_TRIGGERED,
  376. TMC_TOFF,
  377. TMC_TBL,
  378. TMC_HEND,
  379. TMC_HSTRT,
  380. TMC_SGT
  381. };
  382. enum TMC_drv_status_enum : char {
  383. TMC_DRV_CODES,
  384. TMC_STST,
  385. TMC_OLB,
  386. TMC_OLA,
  387. TMC_S2GB,
  388. TMC_S2GA,
  389. TMC_DRV_OTPW,
  390. TMC_OT,
  391. TMC_STALLGUARD,
  392. TMC_DRV_CS_ACTUAL,
  393. TMC_FSACTIVE,
  394. TMC_SG_RESULT,
  395. TMC_DRV_STATUS_HEX,
  396. TMC_T157,
  397. TMC_T150,
  398. TMC_T143,
  399. TMC_T120,
  400. TMC_STEALTH,
  401. TMC_S2VSB,
  402. TMC_S2VSA
  403. };
  404. enum TMC_get_registers_enum : char {
  405. TMC_AXIS_CODES,
  406. TMC_GET_GCONF,
  407. TMC_GET_IHOLD_IRUN,
  408. TMC_GET_GSTAT,
  409. TMC_GET_IOIN,
  410. TMC_GET_TPOWERDOWN,
  411. TMC_GET_TSTEP,
  412. TMC_GET_TPWMTHRS,
  413. TMC_GET_TCOOLTHRS,
  414. TMC_GET_THIGH,
  415. TMC_GET_CHOPCONF,
  416. TMC_GET_COOLCONF,
  417. TMC_GET_PWMCONF,
  418. TMC_GET_PWM_SCALE,
  419. TMC_GET_DRV_STATUS,
  420. TMC_GET_DRVCONF,
  421. TMC_GET_DRVCTRL,
  422. TMC_GET_DRVSTATUS,
  423. TMC_GET_SGCSCONF,
  424. TMC_GET_SMARTEN
  425. };
  426. template<class TMC>
  427. static void print_vsense(TMC &st) { serialprintPGM(st.vsense() ? PSTR("1=.18") : PSTR("0=.325")); }
  428. #if HAS_TMCX1X0
  429. static void tmc_status(TMC2130Stepper &st, const TMC_debug_enum i) {
  430. switch (i) {
  431. case TMC_PWM_SCALE: SERIAL_PRINT(st.PWM_SCALE(), DEC); break;
  432. case TMC_SGT: SERIAL_PRINT(st.sgt(), DEC); break;
  433. case TMC_STEALTHCHOP: serialprintPGM(st.en_pwm_mode() ? PSTR("true") : PSTR("false")); break;
  434. default: break;
  435. }
  436. }
  437. static void _tmc_parse_drv_status(TMC2130Stepper &st, const TMC_drv_status_enum i) {
  438. switch (i) {
  439. case TMC_STALLGUARD: if (st.stallguard()) SERIAL_CHAR('X'); break;
  440. case TMC_SG_RESULT: SERIAL_PRINT(st.sg_result(), DEC); break;
  441. case TMC_FSACTIVE: if (st.fsactive()) SERIAL_CHAR('X'); break;
  442. case TMC_DRV_CS_ACTUAL: SERIAL_PRINT(st.cs_actual(), DEC); break;
  443. default: break;
  444. }
  445. }
  446. #endif
  447. #if HAS_DRIVER(TMC2160) || HAS_DRIVER(TMC5160)
  448. template<char AXIS_LETTER, char DRIVER_ID> void print_vsense(TMCMarlin<TMC2160Stepper, AXIS_LETTER, DRIVER_ID> &st) { UNUSED(st); }
  449. template<char AXIS_LETTER, char DRIVER_ID> void print_vsense(TMCMarlin<TMC5160Stepper, AXIS_LETTER, DRIVER_ID> &st) { UNUSED(st); }
  450. static void tmc_status(TMC2160Stepper &st, const TMC_debug_enum i) {
  451. switch (i) {
  452. case TMC_PWM_SCALE: SERIAL_PRINT(st.PWM_SCALE(), DEC); break;
  453. case TMC_SGT: SERIAL_PRINT(st.sgt(), DEC); break;
  454. case TMC_STEALTHCHOP: serialprintPGM(st.en_pwm_mode() ? PSTR("true") : PSTR("false")); break;
  455. case TMC_GLOBAL_SCALER:
  456. {
  457. uint16_t value = st.GLOBAL_SCALER();
  458. SERIAL_PRINT(value ? value : 256, DEC);
  459. SERIAL_ECHOPGM("/256");
  460. }
  461. break;
  462. default: break;
  463. }
  464. }
  465. #endif
  466. #if HAS_DRIVER(TMC2208)
  467. static void tmc_status(TMC2208Stepper &st, const TMC_debug_enum i) {
  468. switch (i) {
  469. case TMC_PWM_SCALE: SERIAL_PRINT(st.pwm_scale_sum(), DEC); break;
  470. case TMC_STEALTHCHOP: serialprintPGM(st.stealth() ? PSTR("true") : PSTR("false")); break;
  471. case TMC_S2VSA: if (st.s2vsa()) SERIAL_CHAR('X'); break;
  472. case TMC_S2VSB: if (st.s2vsb()) SERIAL_CHAR('X'); break;
  473. default: break;
  474. }
  475. }
  476. static void _tmc_parse_drv_status(TMC2208Stepper &st, const TMC_drv_status_enum i) {
  477. switch (i) {
  478. case TMC_T157: if (st.t157()) SERIAL_CHAR('X'); break;
  479. case TMC_T150: if (st.t150()) SERIAL_CHAR('X'); break;
  480. case TMC_T143: if (st.t143()) SERIAL_CHAR('X'); break;
  481. case TMC_T120: if (st.t120()) SERIAL_CHAR('X'); break;
  482. case TMC_DRV_CS_ACTUAL: SERIAL_PRINT(st.cs_actual(), DEC); break;
  483. default: break;
  484. }
  485. }
  486. #endif
  487. #if HAS_DRIVER(TMC2660)
  488. static void _tmc_parse_drv_status(TMC2660Stepper, const TMC_drv_status_enum) { }
  489. #endif
  490. template <typename TMC>
  491. static void tmc_status(TMC &st, const TMC_debug_enum i, const float spmm) {
  492. SERIAL_CHAR('\t');
  493. switch (i) {
  494. case TMC_CODES: st.printLabel(); break;
  495. case TMC_ENABLED: serialprintPGM(st.isEnabled() ? PSTR("true") : PSTR("false")); break;
  496. case TMC_CURRENT: SERIAL_ECHO(st.getMilliamps()); break;
  497. case TMC_RMS_CURRENT: SERIAL_ECHO(st.rms_current()); break;
  498. case TMC_MAX_CURRENT: SERIAL_PRINT((float)st.rms_current() * 1.41, 0); break;
  499. case TMC_IRUN:
  500. SERIAL_PRINT(st.irun(), DEC);
  501. SERIAL_ECHOPGM("/31");
  502. break;
  503. case TMC_IHOLD:
  504. SERIAL_PRINT(st.ihold(), DEC);
  505. SERIAL_ECHOPGM("/31");
  506. break;
  507. case TMC_CS_ACTUAL:
  508. SERIAL_PRINT(st.cs_actual(), DEC);
  509. SERIAL_ECHOPGM("/31");
  510. break;
  511. case TMC_VSENSE: print_vsense(st); break;
  512. case TMC_MICROSTEPS: SERIAL_ECHO(st.microsteps()); break;
  513. case TMC_TSTEP: {
  514. uint32_t tstep_value = st.TSTEP();
  515. if (tstep_value == 0xFFFFF) SERIAL_ECHOPGM("max");
  516. else SERIAL_ECHO(tstep_value);
  517. }
  518. break;
  519. case TMC_TPWMTHRS: {
  520. uint32_t tpwmthrs_val = st.TPWMTHRS();
  521. SERIAL_ECHO(tpwmthrs_val);
  522. }
  523. break;
  524. case TMC_TPWMTHRS_MMS: {
  525. uint32_t tpwmthrs_val = st.TPWMTHRS();
  526. if (tpwmthrs_val)
  527. SERIAL_ECHO(12650000UL * st.microsteps() / (256 * tpwmthrs_val * spmm));
  528. else
  529. SERIAL_CHAR('-');
  530. }
  531. break;
  532. case TMC_OTPW: serialprintPGM(st.otpw() ? PSTR("true") : PSTR("false")); break;
  533. #if ENABLED(MONITOR_DRIVER_STATUS)
  534. case TMC_OTPW_TRIGGERED: serialprintPGM(st.getOTPW() ? PSTR("true") : PSTR("false")); break;
  535. #endif
  536. case TMC_TOFF: SERIAL_PRINT(st.toff(), DEC); break;
  537. case TMC_TBL: SERIAL_PRINT(st.blank_time(), DEC); break;
  538. case TMC_HEND: SERIAL_PRINT(st.hysteresis_end(), DEC); break;
  539. case TMC_HSTRT: SERIAL_PRINT(st.hysteresis_start(), DEC); break;
  540. default: tmc_status(st, i); break;
  541. }
  542. }
  543. #if HAS_DRIVER(TMC2660)
  544. template<char AXIS_LETTER, char DRIVER_ID>
  545. void tmc_status(TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID> &st, const TMC_debug_enum i, const float) {
  546. SERIAL_CHAR('\t');
  547. switch (i) {
  548. case TMC_CODES: st.printLabel(); break;
  549. case TMC_ENABLED: serialprintPGM(st.isEnabled() ? PSTR("true") : PSTR("false")); break;
  550. case TMC_CURRENT: SERIAL_ECHO(st.getMilliamps()); break;
  551. case TMC_RMS_CURRENT: SERIAL_ECHO(st.rms_current()); break;
  552. case TMC_MAX_CURRENT: SERIAL_PRINT((float)st.rms_current() * 1.41, 0); break;
  553. case TMC_IRUN:
  554. SERIAL_PRINT(st.cs(), DEC);
  555. SERIAL_ECHOPGM("/31");
  556. break;
  557. case TMC_VSENSE: serialprintPGM(st.vsense() ? PSTR("1=.165") : PSTR("0=.310")); break;
  558. case TMC_MICROSTEPS: SERIAL_ECHO(st.microsteps()); break;
  559. //case TMC_OTPW: serialprintPGM(st.otpw() ? PSTR("true") : PSTR("false")); break;
  560. //case TMC_OTPW_TRIGGERED: serialprintPGM(st.getOTPW() ? PSTR("true") : PSTR("false")); break;
  561. case TMC_SGT: SERIAL_PRINT(st.sgt(), DEC); break;
  562. case TMC_TOFF: SERIAL_PRINT(st.toff(), DEC); break;
  563. case TMC_TBL: SERIAL_PRINT(st.blank_time(), DEC); break;
  564. case TMC_HEND: SERIAL_PRINT(st.hysteresis_end(), DEC); break;
  565. case TMC_HSTRT: SERIAL_PRINT(st.hysteresis_start(), DEC); break;
  566. default: break;
  567. }
  568. }
  569. #endif
  570. template <typename TMC>
  571. static void tmc_parse_drv_status(TMC &st, const TMC_drv_status_enum i) {
  572. SERIAL_CHAR('\t');
  573. switch (i) {
  574. case TMC_DRV_CODES: st.printLabel(); break;
  575. case TMC_STST: if (st.stst()) SERIAL_CHAR('X'); break;
  576. case TMC_OLB: if (st.olb()) SERIAL_CHAR('X'); break;
  577. case TMC_OLA: if (st.ola()) SERIAL_CHAR('X'); break;
  578. case TMC_S2GB: if (st.s2gb()) SERIAL_CHAR('X'); break;
  579. case TMC_S2GA: if (st.s2ga()) SERIAL_CHAR('X'); break;
  580. case TMC_DRV_OTPW: if (st.otpw()) SERIAL_CHAR('X'); break;
  581. case TMC_OT: if (st.ot()) SERIAL_CHAR('X'); break;
  582. case TMC_DRV_STATUS_HEX: {
  583. const uint32_t drv_status = st.DRV_STATUS();
  584. SERIAL_CHAR('\t');
  585. st.printLabel();
  586. SERIAL_CHAR('\t');
  587. print_hex_long(drv_status, ':');
  588. if (drv_status == 0xFFFFFFFF || drv_status == 0) SERIAL_ECHOPGM("\t Bad response!");
  589. SERIAL_EOL();
  590. break;
  591. }
  592. default: _tmc_parse_drv_status(st, i); break;
  593. }
  594. }
  595. 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) {
  596. if (print_x) {
  597. #if AXIS_IS_TMC(X)
  598. tmc_status(stepperX, i, planner.settings.axis_steps_per_mm[X_AXIS]);
  599. #endif
  600. #if AXIS_IS_TMC(X2)
  601. tmc_status(stepperX2, i, planner.settings.axis_steps_per_mm[X_AXIS]);
  602. #endif
  603. }
  604. if (print_y) {
  605. #if AXIS_IS_TMC(Y)
  606. tmc_status(stepperY, i, planner.settings.axis_steps_per_mm[Y_AXIS]);
  607. #endif
  608. #if AXIS_IS_TMC(Y2)
  609. tmc_status(stepperY2, i, planner.settings.axis_steps_per_mm[Y_AXIS]);
  610. #endif
  611. }
  612. if (print_z) {
  613. #if AXIS_IS_TMC(Z)
  614. tmc_status(stepperZ, i, planner.settings.axis_steps_per_mm[Z_AXIS]);
  615. #endif
  616. #if AXIS_IS_TMC(Z2)
  617. tmc_status(stepperZ2, i, planner.settings.axis_steps_per_mm[Z_AXIS]);
  618. #endif
  619. #if AXIS_IS_TMC(Z3)
  620. tmc_status(stepperZ3, i, planner.settings.axis_steps_per_mm[Z_AXIS]);
  621. #endif
  622. }
  623. if (print_e) {
  624. #if AXIS_IS_TMC(E0)
  625. tmc_status(stepperE0, i, planner.settings.axis_steps_per_mm[E_AXIS]);
  626. #endif
  627. #if AXIS_IS_TMC(E1)
  628. tmc_status(stepperE1, i, planner.settings.axis_steps_per_mm[E_AXIS
  629. #if ENABLED(DISTINCT_E_FACTORS)
  630. + 1
  631. #endif
  632. ]);
  633. #endif
  634. #if AXIS_IS_TMC(E2)
  635. tmc_status(stepperE2, i, planner.settings.axis_steps_per_mm[E_AXIS
  636. #if ENABLED(DISTINCT_E_FACTORS)
  637. + 2
  638. #endif
  639. ]);
  640. #endif
  641. #if AXIS_IS_TMC(E3)
  642. tmc_status(stepperE3, i, planner.settings.axis_steps_per_mm[E_AXIS
  643. #if ENABLED(DISTINCT_E_FACTORS)
  644. + 3
  645. #endif
  646. ]);
  647. #endif
  648. #if AXIS_IS_TMC(E4)
  649. tmc_status(stepperE4, i, planner.settings.axis_steps_per_mm[E_AXIS
  650. #if ENABLED(DISTINCT_E_FACTORS)
  651. + 4
  652. #endif
  653. ]);
  654. #endif
  655. #if AXIS_IS_TMC(E5)
  656. tmc_status(stepperE5, i, planner.settings.axis_steps_per_mm[E_AXIS
  657. #if ENABLED(DISTINCT_E_FACTORS)
  658. + 5
  659. #endif
  660. ]);
  661. #endif
  662. }
  663. SERIAL_EOL();
  664. }
  665. 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) {
  666. if (print_x) {
  667. #if AXIS_IS_TMC(X)
  668. tmc_parse_drv_status(stepperX, i);
  669. #endif
  670. #if AXIS_IS_TMC(X2)
  671. tmc_parse_drv_status(stepperX2, i);
  672. #endif
  673. }
  674. if (print_y) {
  675. #if AXIS_IS_TMC(Y)
  676. tmc_parse_drv_status(stepperY, i);
  677. #endif
  678. #if AXIS_IS_TMC(Y2)
  679. tmc_parse_drv_status(stepperY2, i);
  680. #endif
  681. }
  682. if (print_z) {
  683. #if AXIS_IS_TMC(Z)
  684. tmc_parse_drv_status(stepperZ, i);
  685. #endif
  686. #if AXIS_IS_TMC(Z2)
  687. tmc_parse_drv_status(stepperZ2, i);
  688. #endif
  689. #if AXIS_IS_TMC(Z3)
  690. tmc_parse_drv_status(stepperZ3, i);
  691. #endif
  692. }
  693. if (print_e) {
  694. #if AXIS_IS_TMC(E0)
  695. tmc_parse_drv_status(stepperE0, i);
  696. #endif
  697. #if AXIS_IS_TMC(E1)
  698. tmc_parse_drv_status(stepperE1, i);
  699. #endif
  700. #if AXIS_IS_TMC(E2)
  701. tmc_parse_drv_status(stepperE2, i);
  702. #endif
  703. #if AXIS_IS_TMC(E3)
  704. tmc_parse_drv_status(stepperE3, i);
  705. #endif
  706. #if AXIS_IS_TMC(E4)
  707. tmc_parse_drv_status(stepperE4, i);
  708. #endif
  709. #if AXIS_IS_TMC(E5)
  710. tmc_parse_drv_status(stepperE5, i);
  711. #endif
  712. }
  713. SERIAL_EOL();
  714. }
  715. /**
  716. * M122 report functions
  717. */
  718. void tmc_report_all(bool print_x, const bool print_y, const bool print_z, const bool print_e) {
  719. #define TMC_REPORT(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); tmc_debug_loop(ITEM, print_x, print_y, print_z, print_e); }while(0)
  720. #define DRV_REPORT(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); drv_status_loop(ITEM, print_x, print_y, print_z, print_e); }while(0)
  721. TMC_REPORT("\t", TMC_CODES);
  722. TMC_REPORT("Enabled\t", TMC_ENABLED);
  723. TMC_REPORT("Set current", TMC_CURRENT);
  724. TMC_REPORT("RMS current", TMC_RMS_CURRENT);
  725. TMC_REPORT("MAX current", TMC_MAX_CURRENT);
  726. TMC_REPORT("Run current", TMC_IRUN);
  727. TMC_REPORT("Hold current", TMC_IHOLD);
  728. #if HAS_DRIVER(TMC2160) || HAS_DRIVER(TMC5160)
  729. TMC_REPORT("Global scaler", TMC_GLOBAL_SCALER);
  730. #endif
  731. TMC_REPORT("CS actual\t", TMC_CS_ACTUAL);
  732. TMC_REPORT("PWM scale", TMC_PWM_SCALE);
  733. TMC_REPORT("vsense\t", TMC_VSENSE);
  734. TMC_REPORT("stealthChop", TMC_STEALTHCHOP);
  735. TMC_REPORT("msteps\t", TMC_MICROSTEPS);
  736. TMC_REPORT("tstep\t", TMC_TSTEP);
  737. TMC_REPORT("pwm\nthreshold\t", TMC_TPWMTHRS);
  738. TMC_REPORT("[mm/s]\t", TMC_TPWMTHRS_MMS);
  739. TMC_REPORT("OT prewarn", TMC_OTPW);
  740. #if ENABLED(MONITOR_DRIVER_STATUS)
  741. TMC_REPORT("OT prewarn has\n"
  742. "been triggered", TMC_OTPW_TRIGGERED);
  743. #endif
  744. TMC_REPORT("off time\t", TMC_TOFF);
  745. TMC_REPORT("blank time", TMC_TBL);
  746. TMC_REPORT("hysteresis\n-end\t", TMC_HEND);
  747. TMC_REPORT("-start\t", TMC_HSTRT);
  748. TMC_REPORT("Stallguard thrs", TMC_SGT);
  749. DRV_REPORT("DRVSTATUS", TMC_DRV_CODES);
  750. #if HAS_TMCX1X0
  751. DRV_REPORT("stallguard\t", TMC_STALLGUARD);
  752. DRV_REPORT("sg_result\t", TMC_SG_RESULT);
  753. DRV_REPORT("fsactive\t", TMC_FSACTIVE);
  754. #endif
  755. DRV_REPORT("stst\t", TMC_STST);
  756. DRV_REPORT("olb\t", TMC_OLB);
  757. DRV_REPORT("ola\t", TMC_OLA);
  758. DRV_REPORT("s2gb\t", TMC_S2GB);
  759. DRV_REPORT("s2ga\t", TMC_S2GA);
  760. DRV_REPORT("otpw\t", TMC_DRV_OTPW);
  761. DRV_REPORT("ot\t", TMC_OT);
  762. #if HAS_DRIVER(TMC2208)
  763. DRV_REPORT("157C\t", TMC_T157);
  764. DRV_REPORT("150C\t", TMC_T150);
  765. DRV_REPORT("143C\t", TMC_T143);
  766. DRV_REPORT("120C\t", TMC_T120);
  767. DRV_REPORT("s2vsa\t", TMC_S2VSA);
  768. DRV_REPORT("s2vsb\t", TMC_S2VSB);
  769. #endif
  770. DRV_REPORT("Driver registers:\n",TMC_DRV_STATUS_HEX);
  771. SERIAL_EOL();
  772. }
  773. #define PRINT_TMC_REGISTER(REG_CASE) case TMC_GET_##REG_CASE: print_hex_long(st.REG_CASE(), ':'); break
  774. #if HAS_TMCX1X0
  775. static void tmc_get_ic_registers(TMC2130Stepper &st, const TMC_get_registers_enum i) {
  776. switch (i) {
  777. PRINT_TMC_REGISTER(TCOOLTHRS);
  778. PRINT_TMC_REGISTER(THIGH);
  779. PRINT_TMC_REGISTER(COOLCONF);
  780. default: SERIAL_CHAR('\t'); break;
  781. }
  782. }
  783. #endif
  784. #if HAS_DRIVER(TMC2208)
  785. static void tmc_get_ic_registers(TMC2208Stepper, const TMC_get_registers_enum) { SERIAL_CHAR('\t'); }
  786. #endif
  787. #if HAS_TRINAMIC
  788. template<class TMC>
  789. static void tmc_get_registers(TMC &st, const TMC_get_registers_enum i) {
  790. switch (i) {
  791. case TMC_AXIS_CODES: SERIAL_CHAR('\t'); st.printLabel(); break;
  792. PRINT_TMC_REGISTER(GCONF);
  793. PRINT_TMC_REGISTER(IHOLD_IRUN);
  794. PRINT_TMC_REGISTER(GSTAT);
  795. PRINT_TMC_REGISTER(IOIN);
  796. PRINT_TMC_REGISTER(TPOWERDOWN);
  797. PRINT_TMC_REGISTER(TSTEP);
  798. PRINT_TMC_REGISTER(TPWMTHRS);
  799. PRINT_TMC_REGISTER(CHOPCONF);
  800. PRINT_TMC_REGISTER(PWMCONF);
  801. PRINT_TMC_REGISTER(PWM_SCALE);
  802. PRINT_TMC_REGISTER(DRV_STATUS);
  803. default: tmc_get_ic_registers(st, i); break;
  804. }
  805. SERIAL_CHAR('\t');
  806. }
  807. #endif
  808. #if HAS_DRIVER(TMC2660)
  809. template <char AXIS_LETTER, char DRIVER_ID>
  810. static void tmc_get_registers(TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID> &st, const TMC_get_registers_enum i) {
  811. switch (i) {
  812. case TMC_AXIS_CODES: SERIAL_CHAR('\t'); st.printLabel(); break;
  813. PRINT_TMC_REGISTER(DRVCONF);
  814. PRINT_TMC_REGISTER(DRVCTRL);
  815. PRINT_TMC_REGISTER(CHOPCONF);
  816. PRINT_TMC_REGISTER(DRVSTATUS);
  817. PRINT_TMC_REGISTER(SGCSCONF);
  818. PRINT_TMC_REGISTER(SMARTEN);
  819. default: SERIAL_CHAR('\t'); break;
  820. }
  821. SERIAL_CHAR('\t');
  822. }
  823. #endif
  824. 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) {
  825. if (print_x) {
  826. #if AXIS_IS_TMC(X)
  827. tmc_get_registers(stepperX, i);
  828. #endif
  829. #if AXIS_IS_TMC(X2)
  830. tmc_get_registers(stepperX2, i);
  831. #endif
  832. }
  833. if (print_y) {
  834. #if AXIS_IS_TMC(Y)
  835. tmc_get_registers(stepperY, i);
  836. #endif
  837. #if AXIS_IS_TMC(Y2)
  838. tmc_get_registers(stepperY2, i);
  839. #endif
  840. }
  841. if (print_z) {
  842. #if AXIS_IS_TMC(Z)
  843. tmc_get_registers(stepperZ, i);
  844. #endif
  845. #if AXIS_IS_TMC(Z2)
  846. tmc_get_registers(stepperZ2, i);
  847. #endif
  848. #if AXIS_IS_TMC(Z3)
  849. tmc_get_registers(stepperZ3, i);
  850. #endif
  851. }
  852. if (print_e) {
  853. #if AXIS_IS_TMC(E0)
  854. tmc_get_registers(stepperE0, i);
  855. #endif
  856. #if AXIS_IS_TMC(E1)
  857. tmc_get_registers(stepperE1, i);
  858. #endif
  859. #if AXIS_IS_TMC(E2)
  860. tmc_get_registers(stepperE2, i);
  861. #endif
  862. #if AXIS_IS_TMC(E3)
  863. tmc_get_registers(stepperE3, i);
  864. #endif
  865. #if AXIS_IS_TMC(E4)
  866. tmc_get_registers(stepperE4, i);
  867. #endif
  868. #if AXIS_IS_TMC(E5)
  869. tmc_get_registers(stepperE5, i);
  870. #endif
  871. }
  872. SERIAL_EOL();
  873. }
  874. void tmc_get_registers(bool print_x, bool print_y, bool print_z, bool print_e) {
  875. #define _TMC_GET_REG(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); tmc_get_registers(ITEM, print_x, print_y, print_z, print_e); }while(0)
  876. #define TMC_GET_REG(NAME, TABS) _TMC_GET_REG(STRINGIFY(NAME) TABS, TMC_GET_##NAME)
  877. _TMC_GET_REG("\t", TMC_AXIS_CODES);
  878. TMC_GET_REG(GCONF, "\t\t");
  879. TMC_GET_REG(IHOLD_IRUN, "\t");
  880. TMC_GET_REG(GSTAT, "\t\t");
  881. TMC_GET_REG(IOIN, "\t\t");
  882. TMC_GET_REG(TPOWERDOWN, "\t");
  883. TMC_GET_REG(TSTEP, "\t\t");
  884. TMC_GET_REG(TPWMTHRS, "\t");
  885. TMC_GET_REG(TCOOLTHRS, "\t");
  886. TMC_GET_REG(THIGH, "\t\t");
  887. TMC_GET_REG(CHOPCONF, "\t");
  888. TMC_GET_REG(COOLCONF, "\t");
  889. TMC_GET_REG(PWMCONF, "\t");
  890. TMC_GET_REG(PWM_SCALE, "\t");
  891. TMC_GET_REG(DRV_STATUS, "\t");
  892. }
  893. #endif // TMC_DEBUG
  894. #if USE_SENSORLESS
  895. bool tmc_enable_stallguard(TMC2130Stepper &st) {
  896. bool stealthchop_was_enabled = st.en_pwm_mode();
  897. st.TCOOLTHRS(0xFFFFF);
  898. st.en_pwm_mode(false);
  899. st.diag1_stall(true);
  900. return stealthchop_was_enabled;
  901. }
  902. void tmc_disable_stallguard(TMC2130Stepper &st, const bool restore_stealth) {
  903. st.TCOOLTHRS(0);
  904. st.en_pwm_mode(restore_stealth);
  905. st.diag1_stall(false);
  906. }
  907. bool tmc_enable_stallguard(TMC2660Stepper) {
  908. // TODO
  909. return false;
  910. }
  911. void tmc_disable_stallguard(TMC2660Stepper, const bool) {};
  912. #endif // USE_SENSORLESS
  913. #if TMC_HAS_SPI
  914. #define SET_CS_PIN(st) OUT_WRITE(st##_CS_PIN, HIGH)
  915. void tmc_init_cs_pins() {
  916. #if AXIS_HAS_SPI(X)
  917. SET_CS_PIN(X);
  918. #endif
  919. #if AXIS_HAS_SPI(Y)
  920. SET_CS_PIN(Y);
  921. #endif
  922. #if AXIS_HAS_SPI(Z)
  923. SET_CS_PIN(Z);
  924. #endif
  925. #if AXIS_HAS_SPI(X2)
  926. SET_CS_PIN(X2);
  927. #endif
  928. #if AXIS_HAS_SPI(Y2)
  929. SET_CS_PIN(Y2);
  930. #endif
  931. #if AXIS_HAS_SPI(Z2)
  932. SET_CS_PIN(Z2);
  933. #endif
  934. #if AXIS_HAS_SPI(Z3)
  935. SET_CS_PIN(Z3);
  936. #endif
  937. #if AXIS_HAS_SPI(E0)
  938. SET_CS_PIN(E0);
  939. #endif
  940. #if AXIS_HAS_SPI(E1)
  941. SET_CS_PIN(E1);
  942. #endif
  943. #if AXIS_HAS_SPI(E2)
  944. SET_CS_PIN(E2);
  945. #endif
  946. #if AXIS_HAS_SPI(E3)
  947. SET_CS_PIN(E3);
  948. #endif
  949. #if AXIS_HAS_SPI(E4)
  950. SET_CS_PIN(E4);
  951. #endif
  952. #if AXIS_HAS_SPI(E5)
  953. SET_CS_PIN(E5);
  954. #endif
  955. }
  956. #endif // TMC_HAS_SPI
  957. template<typename TMC>
  958. static bool test_connection(TMC &st) {
  959. SERIAL_ECHOPGM("Testing ");
  960. st.printLabel();
  961. SERIAL_ECHOPGM(" connection... ");
  962. const uint8_t test_result = st.test_connection();
  963. if (test_result > 0) SERIAL_ECHOPGM("Error: All ");
  964. const char *stat;
  965. switch (test_result) {
  966. default:
  967. case 0: stat = PSTR("OK"); break;
  968. case 1: stat = PSTR("HIGH"); break;
  969. case 2: stat = PSTR("LOW"); break;
  970. }
  971. serialprintPGM(stat);
  972. SERIAL_EOL();
  973. return test_result;
  974. }
  975. void test_tmc_connection(const bool test_x, const bool test_y, const bool test_z, const bool test_e) {
  976. uint8_t axis_connection = 0;
  977. if (test_x) {
  978. #if AXIS_IS_TMC(X)
  979. axis_connection += test_connection(stepperX);
  980. #endif
  981. #if AXIS_IS_TMC(X2)
  982. axis_connection += test_connection(stepperX2);
  983. #endif
  984. }
  985. if (test_y) {
  986. #if AXIS_IS_TMC(Y)
  987. axis_connection += test_connection(stepperY);
  988. #endif
  989. #if AXIS_IS_TMC(Y2)
  990. axis_connection += test_connection(stepperY2);
  991. #endif
  992. }
  993. if (test_z) {
  994. #if AXIS_IS_TMC(Z)
  995. axis_connection += test_connection(stepperZ);
  996. #endif
  997. #if AXIS_IS_TMC(Z2)
  998. axis_connection += test_connection(stepperZ2);
  999. #endif
  1000. #if AXIS_IS_TMC(Z3)
  1001. axis_connection += test_connection(stepperZ3);
  1002. #endif
  1003. }
  1004. if (test_e) {
  1005. #if AXIS_IS_TMC(E0)
  1006. axis_connection += test_connection(stepperE0);
  1007. #endif
  1008. #if AXIS_IS_TMC(E1)
  1009. axis_connection += test_connection(stepperE1);
  1010. #endif
  1011. #if AXIS_IS_TMC(E2)
  1012. axis_connection += test_connection(stepperE2);
  1013. #endif
  1014. #if AXIS_IS_TMC(E3)
  1015. axis_connection += test_connection(stepperE3);
  1016. #endif
  1017. #if AXIS_IS_TMC(E4)
  1018. axis_connection += test_connection(stepperE4);
  1019. #endif
  1020. #if AXIS_IS_TMC(E5)
  1021. axis_connection += test_connection(stepperE5);
  1022. #endif
  1023. }
  1024. if (axis_connection) ui.set_status_P(PSTR("TMC CONNECTION ERROR"));
  1025. }
  1026. #if HAS_LCD_MENU
  1027. void init_tmc_section() {
  1028. #if AXIS_IS_TMC(X)
  1029. stepperX.init_lcd_variables(X_AXIS);
  1030. #endif
  1031. #if AXIS_IS_TMC(Y)
  1032. stepperY.init_lcd_variables(Y_AXIS);
  1033. #endif
  1034. #if AXIS_IS_TMC(Z)
  1035. stepperZ.init_lcd_variables(Z_AXIS);
  1036. #endif
  1037. #if AXIS_IS_TMC(X2)
  1038. stepperX2.init_lcd_variables(X_AXIS);
  1039. #endif
  1040. #if AXIS_IS_TMC(Y2)
  1041. stepperY2.init_lcd_variables(Y_AXIS);
  1042. #endif
  1043. #if AXIS_IS_TMC(Z2)
  1044. stepperZ2.init_lcd_variables(Z_AXIS);
  1045. #endif
  1046. #if AXIS_IS_TMC(Z3)
  1047. stepperZ3.init_lcd_variables(Z_AXIS);
  1048. #endif
  1049. #if AXIS_IS_TMC(E0)
  1050. stepperE0.init_lcd_variables(E_AXIS);
  1051. #endif
  1052. #if AXIS_IS_TMC(E1)
  1053. stepperE1.init_lcd_variables(E_AXIS_N(1));
  1054. #endif
  1055. #if AXIS_IS_TMC(E2)
  1056. stepperE2.init_lcd_variables(E_AXIS_N(2));
  1057. #endif
  1058. #if AXIS_IS_TMC(E3)
  1059. stepperE3.init_lcd_variables(E_AXIS_N(3));
  1060. #endif
  1061. #if AXIS_IS_TMC(E4)
  1062. stepperE4.init_lcd_variables(E_AXIS_N(4));
  1063. #endif
  1064. #if AXIS_IS_TMC(E5)
  1065. stepperE5.init_lcd_variables(E_AXIS_N(5));
  1066. #endif
  1067. }
  1068. #endif
  1069. #endif // HAS_TRINAMIC