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

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