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.

trinamic.cpp 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. /**
  23. * stepper/trinamic.cpp
  24. * Stepper driver indirection for Trinamic
  25. */
  26. #include "../../inc/MarlinConfig.h"
  27. #if HAS_TRINAMIC
  28. #include "trinamic.h"
  29. #include "../stepper.h"
  30. #include <HardwareSerial.h>
  31. #include <SPI.h>
  32. enum StealthIndex : uint8_t { STEALTH_AXIS_XY, STEALTH_AXIS_Z, STEALTH_AXIS_E };
  33. #define _TMC_INIT(ST, STEALTH_INDEX) tmc_init(stepper##ST, ST##_CURRENT, ST##_MICROSTEPS, ST##_HYBRID_THRESHOLD, stealthchop_by_axis[STEALTH_INDEX])
  34. // IC = TMC model number
  35. // ST = Stepper object letter
  36. // L = Label characters
  37. // AI = Axis Enum Index
  38. // SWHW = SW/SH UART selection
  39. #if ENABLED(TMC_USE_SW_SPI)
  40. #define __TMC_SPI_DEFINE(IC, ST, L, AI) TMCMarlin<IC##Stepper, L, AI> stepper##ST(ST##_CS_PIN, ST##_RSENSE, TMC_SW_MOSI, TMC_SW_MISO, TMC_SW_SCK, ST##_CHAIN_POS)
  41. #else
  42. #define __TMC_SPI_DEFINE(IC, ST, L, AI) TMCMarlin<IC##Stepper, L, AI> stepper##ST(ST##_CS_PIN, ST##_RSENSE, ST##_CHAIN_POS)
  43. #endif
  44. #define TMC_UART_HW_DEFINE(IC, ST, L, AI) TMCMarlin<IC##Stepper, L, AI> stepper##ST(&ST##_HARDWARE_SERIAL, ST##_RSENSE, ST##_SLAVE_ADDRESS)
  45. #define TMC_UART_SW_DEFINE(IC, ST, L, AI) TMCMarlin<IC##Stepper, L, AI> stepper##ST(ST##_SERIAL_RX_PIN, ST##_SERIAL_TX_PIN, ST##_RSENSE, ST##_SLAVE_ADDRESS, ST##_SERIAL_RX_PIN > -1)
  46. #define _TMC_SPI_DEFINE(IC, ST, AI) __TMC_SPI_DEFINE(IC, ST, TMC_##ST##_LABEL, AI)
  47. #define TMC_SPI_DEFINE(ST, AI) _TMC_SPI_DEFINE(ST##_DRIVER_TYPE, ST, AI##_AXIS)
  48. #define _TMC_UART_DEFINE(SWHW, IC, ST, AI) TMC_UART_##SWHW##_DEFINE(IC, ST, TMC_##ST##_LABEL, AI)
  49. #define TMC_UART_DEFINE(SWHW, ST, AI) _TMC_UART_DEFINE(SWHW, ST##_DRIVER_TYPE, ST, AI##_AXIS)
  50. #if ENABLED(DISTINCT_E_FACTORS) && E_STEPPERS > 1
  51. #define TMC_SPI_DEFINE_E(AI) TMC_SPI_DEFINE(E##AI, E##AI)
  52. #define TMC_UART_DEFINE_E(SWHW, AI) TMC_UART_DEFINE(SWHW, E##AI, E##AI)
  53. #else
  54. #define TMC_SPI_DEFINE_E(AI) TMC_SPI_DEFINE(E##AI, E)
  55. #define TMC_UART_DEFINE_E(SWHW, AI) TMC_UART_DEFINE(SWHW, E##AI, E)
  56. #endif
  57. // Stepper objects of TMC2130/TMC2160/TMC2660/TMC5130/TMC5160 steppers used
  58. #if AXIS_HAS_SPI(X)
  59. TMC_SPI_DEFINE(X, X);
  60. #endif
  61. #if AXIS_HAS_SPI(X2)
  62. TMC_SPI_DEFINE(X2, X);
  63. #endif
  64. #if AXIS_HAS_SPI(Y)
  65. TMC_SPI_DEFINE(Y, Y);
  66. #endif
  67. #if AXIS_HAS_SPI(Y2)
  68. TMC_SPI_DEFINE(Y2, Y);
  69. #endif
  70. #if AXIS_HAS_SPI(Z)
  71. TMC_SPI_DEFINE(Z, Z);
  72. #endif
  73. #if AXIS_HAS_SPI(Z2)
  74. TMC_SPI_DEFINE(Z2, Z);
  75. #endif
  76. #if AXIS_HAS_SPI(Z3)
  77. TMC_SPI_DEFINE(Z3, Z);
  78. #endif
  79. #if AXIS_HAS_SPI(Z4)
  80. TMC_SPI_DEFINE(Z4, Z);
  81. #endif
  82. #if AXIS_HAS_SPI(E0)
  83. TMC_SPI_DEFINE_E(0);
  84. #endif
  85. #if AXIS_HAS_SPI(E1)
  86. TMC_SPI_DEFINE_E(1);
  87. #endif
  88. #if AXIS_HAS_SPI(E2)
  89. TMC_SPI_DEFINE_E(2);
  90. #endif
  91. #if AXIS_HAS_SPI(E3)
  92. TMC_SPI_DEFINE_E(3);
  93. #endif
  94. #if AXIS_HAS_SPI(E4)
  95. TMC_SPI_DEFINE_E(4);
  96. #endif
  97. #if AXIS_HAS_SPI(E5)
  98. TMC_SPI_DEFINE_E(5);
  99. #endif
  100. #ifndef TMC_BAUD_RATE
  101. #define TMC_BAUD_RATE 115200
  102. #endif
  103. #if HAS_DRIVER(TMC2130)
  104. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  105. void tmc_init(TMCMarlin<TMC2130Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const uint16_t mA, const uint16_t microsteps, const uint32_t thrs, const bool stealth) {
  106. st.begin();
  107. CHOPCONF_t chopconf{0};
  108. chopconf.tbl = 1;
  109. chopconf.toff = chopper_timing.toff;
  110. chopconf.intpol = INTERPOLATE;
  111. chopconf.hend = chopper_timing.hend + 3;
  112. chopconf.hstrt = chopper_timing.hstrt - 1;
  113. #if ENABLED(SQUARE_WAVE_STEPPING)
  114. chopconf.dedge = true;
  115. #endif
  116. st.CHOPCONF(chopconf.sr);
  117. st.rms_current(mA, HOLD_MULTIPLIER);
  118. st.microsteps(microsteps);
  119. st.iholddelay(10);
  120. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  121. st.en_pwm_mode(stealth);
  122. st.stored.stealthChop_enabled = stealth;
  123. PWMCONF_t pwmconf{0};
  124. pwmconf.pwm_freq = 0b01; // f_pwm = 2/683 f_clk
  125. pwmconf.pwm_autoscale = true;
  126. pwmconf.pwm_grad = 5;
  127. pwmconf.pwm_ampl = 180;
  128. st.PWMCONF(pwmconf.sr);
  129. #if ENABLED(HYBRID_THRESHOLD)
  130. st.set_pwm_thrs(thrs);
  131. #else
  132. UNUSED(thrs);
  133. #endif
  134. st.GSTAT(); // Clear GSTAT
  135. }
  136. #endif // TMC2130
  137. #if HAS_DRIVER(TMC2160)
  138. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  139. void tmc_init(TMCMarlin<TMC2160Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const uint16_t mA, const uint16_t microsteps, const uint32_t thrs, const bool stealth) {
  140. st.begin();
  141. CHOPCONF_t chopconf{0};
  142. chopconf.tbl = 1;
  143. chopconf.toff = chopper_timing.toff;
  144. chopconf.intpol = INTERPOLATE;
  145. chopconf.hend = chopper_timing.hend + 3;
  146. chopconf.hstrt = chopper_timing.hstrt - 1;
  147. #if ENABLED(SQUARE_WAVE_STEPPING)
  148. chopconf.dedge = true;
  149. #endif
  150. st.CHOPCONF(chopconf.sr);
  151. st.rms_current(mA, HOLD_MULTIPLIER);
  152. st.microsteps(microsteps);
  153. st.iholddelay(10);
  154. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  155. st.en_pwm_mode(stealth);
  156. st.stored.stealthChop_enabled = stealth;
  157. TMC2160_n::PWMCONF_t pwmconf{0};
  158. pwmconf.pwm_lim = 12;
  159. pwmconf.pwm_reg = 8;
  160. pwmconf.pwm_autograd = true;
  161. pwmconf.pwm_autoscale = true;
  162. pwmconf.pwm_freq = 0b01;
  163. pwmconf.pwm_grad = 14;
  164. pwmconf.pwm_ofs = 36;
  165. st.PWMCONF(pwmconf.sr);
  166. #if ENABLED(HYBRID_THRESHOLD)
  167. st.set_pwm_thrs(thrs);
  168. #else
  169. UNUSED(thrs);
  170. #endif
  171. st.GSTAT(); // Clear GSTAT
  172. }
  173. #endif // TMC2160
  174. //
  175. // TMC2208/2209 Driver objects and inits
  176. //
  177. #if HAS_TMC220x
  178. #if AXIS_HAS_UART(X)
  179. #ifdef X_HARDWARE_SERIAL
  180. TMC_UART_DEFINE(HW, X, X);
  181. #else
  182. TMC_UART_DEFINE(SW, X, X);
  183. #endif
  184. #endif
  185. #if AXIS_HAS_UART(X2)
  186. #ifdef X2_HARDWARE_SERIAL
  187. TMC_UART_DEFINE(HW, X2, X);
  188. #else
  189. TMC_UART_DEFINE(SW, X2, X);
  190. #endif
  191. #endif
  192. #if AXIS_HAS_UART(Y)
  193. #ifdef Y_HARDWARE_SERIAL
  194. TMC_UART_DEFINE(HW, Y, Y);
  195. #else
  196. TMC_UART_DEFINE(SW, Y, Y);
  197. #endif
  198. #endif
  199. #if AXIS_HAS_UART(Y2)
  200. #ifdef Y2_HARDWARE_SERIAL
  201. TMC_UART_DEFINE(HW, Y2, Y);
  202. #else
  203. TMC_UART_DEFINE(SW, Y2, Y);
  204. #endif
  205. #endif
  206. #if AXIS_HAS_UART(Z)
  207. #ifdef Z_HARDWARE_SERIAL
  208. TMC_UART_DEFINE(HW, Z, Z);
  209. #else
  210. TMC_UART_DEFINE(SW, Z, Z);
  211. #endif
  212. #endif
  213. #if AXIS_HAS_UART(Z2)
  214. #ifdef Z2_HARDWARE_SERIAL
  215. TMC_UART_DEFINE(HW, Z2, Z);
  216. #else
  217. TMC_UART_DEFINE(SW, Z2, Z);
  218. #endif
  219. #endif
  220. #if AXIS_HAS_UART(Z3)
  221. #ifdef Z3_HARDWARE_SERIAL
  222. TMC_UART_DEFINE(HW, Z3, Z);
  223. #else
  224. TMC_UART_DEFINE(SW, Z3, Z);
  225. #endif
  226. #endif
  227. #if AXIS_HAS_UART(Z4)
  228. #ifdef Z4_HARDWARE_SERIAL
  229. TMC_UART_DEFINE(HW, Z4, Z);
  230. #else
  231. TMC_UART_DEFINE(SW, Z4, Z);
  232. #endif
  233. #endif
  234. #if AXIS_HAS_UART(E0)
  235. #ifdef E0_HARDWARE_SERIAL
  236. TMC_UART_DEFINE_E(HW, 0);
  237. #else
  238. TMC_UART_DEFINE_E(SW, 0);
  239. #endif
  240. #endif
  241. #if AXIS_HAS_UART(E1)
  242. #ifdef E1_HARDWARE_SERIAL
  243. TMC_UART_DEFINE_E(HW, 1);
  244. #else
  245. TMC_UART_DEFINE_E(SW, 1);
  246. #endif
  247. #endif
  248. #if AXIS_HAS_UART(E2)
  249. #ifdef E2_HARDWARE_SERIAL
  250. TMC_UART_DEFINE_E(HW, 2);
  251. #else
  252. TMC_UART_DEFINE_E(SW, 2);
  253. #endif
  254. #endif
  255. #if AXIS_HAS_UART(E3)
  256. #ifdef E3_HARDWARE_SERIAL
  257. TMC_UART_DEFINE_E(HW, 3);
  258. #else
  259. TMC_UART_DEFINE_E(SW, 3);
  260. #endif
  261. #endif
  262. #if AXIS_HAS_UART(E4)
  263. #ifdef E4_HARDWARE_SERIAL
  264. TMC_UART_DEFINE_E(HW, 4);
  265. #else
  266. TMC_UART_DEFINE_E(SW, 4);
  267. #endif
  268. #endif
  269. #if AXIS_HAS_UART(E5)
  270. #ifdef E5_HARDWARE_SERIAL
  271. TMC_UART_DEFINE_E(HW, 5);
  272. #else
  273. TMC_UART_DEFINE_E(SW, 5);
  274. #endif
  275. #endif
  276. void tmc_serial_begin() {
  277. #if AXIS_HAS_UART(X)
  278. #ifdef X_HARDWARE_SERIAL
  279. X_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  280. #else
  281. stepperX.beginSerial(TMC_BAUD_RATE);
  282. #endif
  283. #endif
  284. #if AXIS_HAS_UART(X2)
  285. #ifdef X2_HARDWARE_SERIAL
  286. X2_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  287. #else
  288. stepperX2.beginSerial(TMC_BAUD_RATE);
  289. #endif
  290. #endif
  291. #if AXIS_HAS_UART(Y)
  292. #ifdef Y_HARDWARE_SERIAL
  293. Y_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  294. #else
  295. stepperY.beginSerial(TMC_BAUD_RATE);
  296. #endif
  297. #endif
  298. #if AXIS_HAS_UART(Y2)
  299. #ifdef Y2_HARDWARE_SERIAL
  300. Y2_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  301. #else
  302. stepperY2.beginSerial(TMC_BAUD_RATE);
  303. #endif
  304. #endif
  305. #if AXIS_HAS_UART(Z)
  306. #ifdef Z_HARDWARE_SERIAL
  307. Z_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  308. #else
  309. stepperZ.beginSerial(TMC_BAUD_RATE);
  310. #endif
  311. #endif
  312. #if AXIS_HAS_UART(Z2)
  313. #ifdef Z2_HARDWARE_SERIAL
  314. Z2_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  315. #else
  316. stepperZ2.beginSerial(TMC_BAUD_RATE);
  317. #endif
  318. #endif
  319. #if AXIS_HAS_UART(Z3)
  320. #ifdef Z3_HARDWARE_SERIAL
  321. Z3_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  322. #else
  323. stepperZ3.beginSerial(TMC_BAUD_RATE);
  324. #endif
  325. #endif
  326. #if AXIS_HAS_UART(Z4)
  327. #ifdef Z4_HARDWARE_SERIAL
  328. Z4_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  329. #else
  330. stepperZ4.beginSerial(TMC_BAUD_RATE);
  331. #endif
  332. #endif
  333. #if AXIS_HAS_UART(E0)
  334. #ifdef E0_HARDWARE_SERIAL
  335. E0_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  336. #else
  337. stepperE0.beginSerial(TMC_BAUD_RATE);
  338. #endif
  339. #endif
  340. #if AXIS_HAS_UART(E1)
  341. #ifdef E1_HARDWARE_SERIAL
  342. E1_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  343. #else
  344. stepperE1.beginSerial(TMC_BAUD_RATE);
  345. #endif
  346. #endif
  347. #if AXIS_HAS_UART(E2)
  348. #ifdef E2_HARDWARE_SERIAL
  349. E2_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  350. #else
  351. stepperE2.beginSerial(TMC_BAUD_RATE);
  352. #endif
  353. #endif
  354. #if AXIS_HAS_UART(E3)
  355. #ifdef E3_HARDWARE_SERIAL
  356. E3_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  357. #else
  358. stepperE3.beginSerial(TMC_BAUD_RATE);
  359. #endif
  360. #endif
  361. #if AXIS_HAS_UART(E4)
  362. #ifdef E4_HARDWARE_SERIAL
  363. E4_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  364. #else
  365. stepperE4.beginSerial(TMC_BAUD_RATE);
  366. #endif
  367. #endif
  368. #if AXIS_HAS_UART(E5)
  369. #ifdef E5_HARDWARE_SERIAL
  370. E5_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  371. #else
  372. stepperE5.beginSerial(TMC_BAUD_RATE);
  373. #endif
  374. #endif
  375. }
  376. #endif
  377. #if HAS_DRIVER(TMC2208)
  378. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  379. void tmc_init(TMCMarlin<TMC2208Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const uint16_t mA, const uint16_t microsteps, const uint32_t thrs, const bool stealth) {
  380. TMC2208_n::GCONF_t gconf{0};
  381. gconf.pdn_disable = true; // Use UART
  382. gconf.mstep_reg_select = true; // Select microsteps with UART
  383. gconf.i_scale_analog = false;
  384. gconf.en_spreadcycle = !stealth;
  385. st.GCONF(gconf.sr);
  386. st.stored.stealthChop_enabled = stealth;
  387. TMC2208_n::CHOPCONF_t chopconf{0};
  388. chopconf.tbl = 0b01; // blank_time = 24
  389. chopconf.toff = chopper_timing.toff;
  390. chopconf.intpol = INTERPOLATE;
  391. chopconf.hend = chopper_timing.hend + 3;
  392. chopconf.hstrt = chopper_timing.hstrt - 1;
  393. #if ENABLED(SQUARE_WAVE_STEPPING)
  394. chopconf.dedge = true;
  395. #endif
  396. st.CHOPCONF(chopconf.sr);
  397. st.rms_current(mA, HOLD_MULTIPLIER);
  398. st.microsteps(microsteps);
  399. st.iholddelay(10);
  400. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  401. TMC2208_n::PWMCONF_t pwmconf{0};
  402. pwmconf.pwm_lim = 12;
  403. pwmconf.pwm_reg = 8;
  404. pwmconf.pwm_autograd = true;
  405. pwmconf.pwm_autoscale = true;
  406. pwmconf.pwm_freq = 0b01;
  407. pwmconf.pwm_grad = 14;
  408. pwmconf.pwm_ofs = 36;
  409. st.PWMCONF(pwmconf.sr);
  410. #if ENABLED(HYBRID_THRESHOLD)
  411. st.set_pwm_thrs(thrs);
  412. #else
  413. UNUSED(thrs);
  414. #endif
  415. st.GSTAT(0b111); // Clear
  416. delay(200);
  417. }
  418. #endif // TMC2208
  419. #if HAS_DRIVER(TMC2209)
  420. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  421. void tmc_init(TMCMarlin<TMC2209Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const uint16_t mA, const uint16_t microsteps, const uint32_t thrs, const bool stealth) {
  422. TMC2208_n::GCONF_t gconf{0};
  423. gconf.pdn_disable = true; // Use UART
  424. gconf.mstep_reg_select = true; // Select microsteps with UART
  425. gconf.i_scale_analog = false;
  426. gconf.en_spreadcycle = !stealth;
  427. st.GCONF(gconf.sr);
  428. st.stored.stealthChop_enabled = stealth;
  429. TMC2208_n::CHOPCONF_t chopconf{0};
  430. chopconf.tbl = 0b01; // blank_time = 24
  431. chopconf.toff = chopper_timing.toff;
  432. chopconf.intpol = INTERPOLATE;
  433. chopconf.hend = chopper_timing.hend + 3;
  434. chopconf.hstrt = chopper_timing.hstrt - 1;
  435. #if ENABLED(SQUARE_WAVE_STEPPING)
  436. chopconf.dedge = true;
  437. #endif
  438. st.CHOPCONF(chopconf.sr);
  439. st.rms_current(mA, HOLD_MULTIPLIER);
  440. st.microsteps(microsteps);
  441. st.iholddelay(10);
  442. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  443. TMC2208_n::PWMCONF_t pwmconf{0};
  444. pwmconf.pwm_lim = 12;
  445. pwmconf.pwm_reg = 8;
  446. pwmconf.pwm_autograd = true;
  447. pwmconf.pwm_autoscale = true;
  448. pwmconf.pwm_freq = 0b01;
  449. pwmconf.pwm_grad = 14;
  450. pwmconf.pwm_ofs = 36;
  451. st.PWMCONF(pwmconf.sr);
  452. #if ENABLED(HYBRID_THRESHOLD)
  453. st.set_pwm_thrs(thrs);
  454. #else
  455. UNUSED(thrs);
  456. #endif
  457. st.GSTAT(0b111); // Clear
  458. delay(200);
  459. }
  460. #endif // TMC2209
  461. #if HAS_DRIVER(TMC2660)
  462. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  463. void tmc_init(TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const uint16_t mA, const uint16_t microsteps, const uint32_t, const bool) {
  464. st.begin();
  465. TMC2660_n::CHOPCONF_t chopconf{0};
  466. chopconf.tbl = 1;
  467. chopconf.toff = chopper_timing.toff;
  468. chopconf.hend = chopper_timing.hend + 3;
  469. chopconf.hstrt = chopper_timing.hstrt - 1;
  470. st.CHOPCONF(chopconf.sr);
  471. st.sdoff(0);
  472. st.rms_current(mA);
  473. st.microsteps(microsteps);
  474. #if ENABLED(SQUARE_WAVE_STEPPING)
  475. st.dedge(true);
  476. #endif
  477. st.intpol(INTERPOLATE);
  478. st.diss2g(true); // Disable short to ground protection. Too many false readings?
  479. #if ENABLED(TMC_DEBUG)
  480. st.rdsel(0b01);
  481. #endif
  482. }
  483. #endif // TMC2660
  484. #if HAS_DRIVER(TMC5130)
  485. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  486. void tmc_init(TMCMarlin<TMC5130Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const uint16_t mA, const uint16_t microsteps, const uint32_t thrs, const bool stealth) {
  487. st.begin();
  488. CHOPCONF_t chopconf{0};
  489. chopconf.tbl = 1;
  490. chopconf.toff = chopper_timing.toff;
  491. chopconf.intpol = INTERPOLATE;
  492. chopconf.hend = chopper_timing.hend + 3;
  493. chopconf.hstrt = chopper_timing.hstrt - 1;
  494. #if ENABLED(SQUARE_WAVE_STEPPING)
  495. chopconf.dedge = true;
  496. #endif
  497. st.CHOPCONF(chopconf.sr);
  498. st.rms_current(mA, HOLD_MULTIPLIER);
  499. st.microsteps(microsteps);
  500. st.iholddelay(10);
  501. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  502. st.en_pwm_mode(stealth);
  503. st.stored.stealthChop_enabled = stealth;
  504. PWMCONF_t pwmconf{0};
  505. pwmconf.pwm_freq = 0b01; // f_pwm = 2/683 f_clk
  506. pwmconf.pwm_autoscale = true;
  507. pwmconf.pwm_grad = 5;
  508. pwmconf.pwm_ampl = 180;
  509. st.PWMCONF(pwmconf.sr);
  510. #if ENABLED(HYBRID_THRESHOLD)
  511. st.set_pwm_thrs(thrs);
  512. #else
  513. UNUSED(thrs);
  514. #endif
  515. st.GSTAT(); // Clear GSTAT
  516. }
  517. #endif // TMC5130
  518. #if HAS_DRIVER(TMC5160)
  519. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  520. void tmc_init(TMCMarlin<TMC5160Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const uint16_t mA, const uint16_t microsteps, const uint32_t thrs, const bool stealth) {
  521. st.begin();
  522. CHOPCONF_t chopconf{0};
  523. chopconf.tbl = 1;
  524. chopconf.toff = chopper_timing.toff;
  525. chopconf.intpol = INTERPOLATE;
  526. chopconf.hend = chopper_timing.hend + 3;
  527. chopconf.hstrt = chopper_timing.hstrt - 1;
  528. #if ENABLED(SQUARE_WAVE_STEPPING)
  529. chopconf.dedge = true;
  530. #endif
  531. st.CHOPCONF(chopconf.sr);
  532. st.rms_current(mA, HOLD_MULTIPLIER);
  533. st.microsteps(microsteps);
  534. st.iholddelay(10);
  535. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  536. st.en_pwm_mode(stealth);
  537. st.stored.stealthChop_enabled = stealth;
  538. TMC2160_n::PWMCONF_t pwmconf{0};
  539. pwmconf.pwm_lim = 12;
  540. pwmconf.pwm_reg = 8;
  541. pwmconf.pwm_autograd = true;
  542. pwmconf.pwm_autoscale = true;
  543. pwmconf.pwm_freq = 0b01;
  544. pwmconf.pwm_grad = 14;
  545. pwmconf.pwm_ofs = 36;
  546. st.PWMCONF(pwmconf.sr);
  547. #if ENABLED(HYBRID_THRESHOLD)
  548. st.set_pwm_thrs(thrs);
  549. #else
  550. UNUSED(thrs);
  551. #endif
  552. st.GSTAT(); // Clear GSTAT
  553. }
  554. #endif // TMC5160
  555. void restore_trinamic_drivers() {
  556. #if AXIS_IS_TMC(X)
  557. stepperX.push();
  558. #endif
  559. #if AXIS_IS_TMC(X2)
  560. stepperX2.push();
  561. #endif
  562. #if AXIS_IS_TMC(Y)
  563. stepperY.push();
  564. #endif
  565. #if AXIS_IS_TMC(Y2)
  566. stepperY2.push();
  567. #endif
  568. #if AXIS_IS_TMC(Z)
  569. stepperZ.push();
  570. #endif
  571. #if AXIS_IS_TMC(Z2)
  572. stepperZ2.push();
  573. #endif
  574. #if AXIS_IS_TMC(Z3)
  575. stepperZ3.push();
  576. #endif
  577. #if AXIS_IS_TMC(Z4)
  578. stepperZ4.push();
  579. #endif
  580. #if AXIS_IS_TMC(E0)
  581. stepperE0.push();
  582. #endif
  583. #if AXIS_IS_TMC(E1)
  584. stepperE1.push();
  585. #endif
  586. #if AXIS_IS_TMC(E2)
  587. stepperE2.push();
  588. #endif
  589. #if AXIS_IS_TMC(E3)
  590. stepperE3.push();
  591. #endif
  592. #if AXIS_IS_TMC(E4)
  593. stepperE4.push();
  594. #endif
  595. #if AXIS_IS_TMC(E5)
  596. stepperE5.push();
  597. #endif
  598. }
  599. void reset_trinamic_drivers() {
  600. static constexpr bool stealthchop_by_axis[] = {
  601. #if ENABLED(STEALTHCHOP_XY)
  602. true
  603. #else
  604. false
  605. #endif
  606. ,
  607. #if ENABLED(STEALTHCHOP_Z)
  608. true
  609. #else
  610. false
  611. #endif
  612. ,
  613. #if ENABLED(STEALTHCHOP_E)
  614. true
  615. #else
  616. false
  617. #endif
  618. };
  619. #if AXIS_IS_TMC(X)
  620. _TMC_INIT(X, STEALTH_AXIS_XY);
  621. #endif
  622. #if AXIS_IS_TMC(X2)
  623. _TMC_INIT(X2, STEALTH_AXIS_XY);
  624. #endif
  625. #if AXIS_IS_TMC(Y)
  626. _TMC_INIT(Y, STEALTH_AXIS_XY);
  627. #endif
  628. #if AXIS_IS_TMC(Y2)
  629. _TMC_INIT(Y2, STEALTH_AXIS_XY);
  630. #endif
  631. #if AXIS_IS_TMC(Z)
  632. _TMC_INIT(Z, STEALTH_AXIS_Z);
  633. #endif
  634. #if AXIS_IS_TMC(Z2)
  635. _TMC_INIT(Z2, STEALTH_AXIS_Z);
  636. #endif
  637. #if AXIS_IS_TMC(Z3)
  638. _TMC_INIT(Z3, STEALTH_AXIS_Z);
  639. #endif
  640. #if AXIS_IS_TMC(Z4)
  641. _TMC_INIT(Z4, STEALTH_AXIS_Z);
  642. #endif
  643. #if AXIS_IS_TMC(E0)
  644. _TMC_INIT(E0, STEALTH_AXIS_E);
  645. #endif
  646. #if AXIS_IS_TMC(E1)
  647. _TMC_INIT(E1, STEALTH_AXIS_E);
  648. #endif
  649. #if AXIS_IS_TMC(E2)
  650. _TMC_INIT(E2, STEALTH_AXIS_E);
  651. #endif
  652. #if AXIS_IS_TMC(E3)
  653. _TMC_INIT(E3, STEALTH_AXIS_E);
  654. #endif
  655. #if AXIS_IS_TMC(E4)
  656. _TMC_INIT(E4, STEALTH_AXIS_E);
  657. #endif
  658. #if AXIS_IS_TMC(E5)
  659. _TMC_INIT(E5, STEALTH_AXIS_E);
  660. #endif
  661. #if USE_SENSORLESS
  662. #if X_SENSORLESS
  663. #if AXIS_HAS_STALLGUARD(X)
  664. stepperX.homing_threshold(X_STALL_SENSITIVITY);
  665. #endif
  666. #if AXIS_HAS_STALLGUARD(X2) && !X2_SENSORLESS
  667. stepperX2.homing_threshold(X_STALL_SENSITIVITY);
  668. #endif
  669. #endif
  670. #if X2_SENSORLESS
  671. stepperX2.homing_threshold(X2_STALL_SENSITIVITY);
  672. #endif
  673. #if Y_SENSORLESS
  674. #if AXIS_HAS_STALLGUARD(Y)
  675. stepperY.homing_threshold(Y_STALL_SENSITIVITY);
  676. #endif
  677. #if AXIS_HAS_STALLGUARD(Y2)
  678. stepperY2.homing_threshold(Y_STALL_SENSITIVITY);
  679. #endif
  680. #endif
  681. #if Z_SENSORLESS
  682. #if AXIS_HAS_STALLGUARD(Z)
  683. stepperZ.homing_threshold(Z_STALL_SENSITIVITY);
  684. #endif
  685. #if AXIS_HAS_STALLGUARD(Z2)
  686. stepperZ2.homing_threshold(Z_STALL_SENSITIVITY);
  687. #endif
  688. #if AXIS_HAS_STALLGUARD(Z3)
  689. stepperZ3.homing_threshold(Z_STALL_SENSITIVITY);
  690. #endif
  691. #if AXIS_HAS_STALLGUARD(Z4)
  692. stepperZ4.homing_threshold(Z_STALL_SENSITIVITY);
  693. #endif
  694. #endif
  695. #endif
  696. #ifdef TMC_ADV
  697. TMC_ADV()
  698. #endif
  699. stepper.set_directions();
  700. }
  701. #endif // HAS_TRINAMIC