My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

trinamic.cpp 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  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_1(ST, STEALTH_INDEX) tmc_init(stepper##ST, ST##_CURRENT, ST##_MICROSTEPS)
  34. #define _TMC_INIT_2(ST, STEALTH_INDEX) tmc_init(stepper##ST, ST##_CURRENT, ST##_MICROSTEPS, stealthchop_by_axis[STEALTH_INDEX], ST##_HYBRID_THRESHOLD)
  35. #define _TMC_INIT_3(ST, STEALTH_INDEX) tmc_init(stepper##ST, ST##_CURRENT, ST##_MICROSTEPS, stealthchop_by_axis[STEALTH_INDEX], ST##_HYBRID_THRESHOLD, ST##_COOLSTEP_SPEED_THRESHOLD, ST##_COOLSTEP_LOWER_LOAD_THRESHOLD, ST##_COOLSTEP_UPPER_LOAD_THRESHOLD, ST##_COOLSTEP_SEUP, ST##_COOLSTEP_SEDN, ST##_COOLSTEP_SEIMIN)
  36. #define _TMC_INIT_TMC2660(ST, STEALTH_INDEX) _TMC_INIT_1(ST, STEALTH_INDEX)
  37. #define _TMC_INIT_TMC2160(ST, STEALTH_INDEX) _TMC_INIT_2(ST, STEALTH_INDEX)
  38. #define _TMC_INIT_TMC2208(ST, STEALTH_INDEX) _TMC_INIT_2(ST, STEALTH_INDEX)
  39. #define _TMC_INIT_TMC5130(ST, STEALTH_INDEX) _TMC_INIT_3(ST, STEALTH_INDEX)
  40. #define _TMC_INIT_TMC5160(ST, STEALTH_INDEX) _TMC_INIT_3(ST, STEALTH_INDEX)
  41. #define _TMC_INIT_TMC2130(ST, STEALTH_INDEX) _TMC_INIT_3(ST, STEALTH_INDEX)
  42. #define _TMC_INIT_TMC2209(ST, STEALTH_INDEX) _TMC_INIT_3(ST, STEALTH_INDEX)
  43. #define __TMC_INIT(DRV, ST, STEALTH_INDEX) _TMC_INIT_##DRV(ST, STEALTH_INDEX)
  44. #define _TMC_INIT(DRV, ST, STEALTH_INDEX) __TMC_INIT(DRV, ST, STEALTH_INDEX)
  45. #define TMC_INIT(ST, STEALTH_INDEX) _TMC_INIT(ST##_DRIVER_TYPE, ST, STEALTH_INDEX)
  46. // IC = TMC model number
  47. // ST = Stepper object letter
  48. // L = Label characters
  49. // AI = Axis Enum Index
  50. // SWHW = SW/SH UART selection
  51. #if ENABLED(TMC_USE_SW_SPI)
  52. #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)
  53. #else
  54. #define __TMC_SPI_DEFINE(IC, ST, L, AI) TMCMarlin<IC##Stepper, L, AI> stepper##ST(ST##_CS_PIN, ST##_RSENSE, ST##_CHAIN_POS)
  55. #endif
  56. #define TMC_UART_HW_DEFINE(IC, ST, L, AI) TMCMarlin<IC##Stepper, L, AI> stepper##ST(&ST##_HARDWARE_SERIAL, ST##_RSENSE, ST##_SLAVE_ADDRESS)
  57. #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)
  58. #define _TMC_SPI_DEFINE(IC, ST, AI) __TMC_SPI_DEFINE(IC, ST, TMC_##ST##_LABEL, AI)
  59. #define TMC_SPI_DEFINE(ST, AI) _TMC_SPI_DEFINE(ST##_DRIVER_TYPE, ST, AI##_AXIS)
  60. #define _TMC_UART_DEFINE(SWHW, IC, ST, AI) TMC_UART_##SWHW##_DEFINE(IC, ST, TMC_##ST##_LABEL, AI)
  61. #define TMC_UART_DEFINE(SWHW, ST, AI) _TMC_UART_DEFINE(SWHW, ST##_DRIVER_TYPE, ST, AI##_AXIS)
  62. #if ENABLED(DISTINCT_E_FACTORS) && E_STEPPERS > 1
  63. #define TMC_SPI_DEFINE_E(AI) TMC_SPI_DEFINE(E##AI, E##AI)
  64. #define TMC_UART_DEFINE_E(SWHW, AI) TMC_UART_DEFINE(SWHW, E##AI, E##AI)
  65. #else
  66. #define TMC_SPI_DEFINE_E(AI) TMC_SPI_DEFINE(E##AI, E)
  67. #define TMC_UART_DEFINE_E(SWHW, AI) TMC_UART_DEFINE(SWHW, E##AI, E)
  68. #endif
  69. // Stepper objects of TMC2130/TMC2160/TMC2660/TMC5130/TMC5160 steppers used
  70. #if AXIS_HAS_SPI(X)
  71. TMC_SPI_DEFINE(X, X);
  72. #endif
  73. #if AXIS_HAS_SPI(X2)
  74. TMC_SPI_DEFINE(X2, X);
  75. #endif
  76. #if AXIS_HAS_SPI(Y)
  77. TMC_SPI_DEFINE(Y, Y);
  78. #endif
  79. #if AXIS_HAS_SPI(Y2)
  80. TMC_SPI_DEFINE(Y2, Y);
  81. #endif
  82. #if AXIS_HAS_SPI(Z)
  83. TMC_SPI_DEFINE(Z, Z);
  84. #endif
  85. #if AXIS_HAS_SPI(Z2)
  86. TMC_SPI_DEFINE(Z2, Z);
  87. #endif
  88. #if AXIS_HAS_SPI(Z3)
  89. TMC_SPI_DEFINE(Z3, Z);
  90. #endif
  91. #if AXIS_HAS_SPI(Z4)
  92. TMC_SPI_DEFINE(Z4, Z);
  93. #endif
  94. #if AXIS_HAS_SPI(E0)
  95. TMC_SPI_DEFINE_E(0);
  96. #endif
  97. #if AXIS_HAS_SPI(E1)
  98. TMC_SPI_DEFINE_E(1);
  99. #endif
  100. #if AXIS_HAS_SPI(E2)
  101. TMC_SPI_DEFINE_E(2);
  102. #endif
  103. #if AXIS_HAS_SPI(E3)
  104. TMC_SPI_DEFINE_E(3);
  105. #endif
  106. #if AXIS_HAS_SPI(E4)
  107. TMC_SPI_DEFINE_E(4);
  108. #endif
  109. #if AXIS_HAS_SPI(E5)
  110. TMC_SPI_DEFINE_E(5);
  111. #endif
  112. #if AXIS_HAS_SPI(E6)
  113. TMC_SPI_DEFINE_E(6);
  114. #endif
  115. #if AXIS_HAS_SPI(E7)
  116. TMC_SPI_DEFINE_E(7);
  117. #endif
  118. #ifndef TMC_BAUD_RATE
  119. #define TMC_BAUD_RATE 115200
  120. #endif
  121. #if HAS_DRIVER(TMC2130)
  122. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  123. void tmc_init(
  124. TMCMarlin<TMC2130Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st,
  125. const uint16_t mA,
  126. const uint16_t microsteps,
  127. const bool stealth,
  128. const uint32_t hyb_thrs,
  129. const uint32_t cool_thrs,
  130. const uint8_t cool_semin,
  131. const uint8_t cool_semax,
  132. const uint8_t cool_seup,
  133. const uint8_t cool_sedn,
  134. const bool cool_seimin
  135. ) {
  136. st.begin();
  137. CHOPCONF_t chopconf{0};
  138. chopconf.tbl = 1;
  139. chopconf.toff = chopper_timing.toff;
  140. chopconf.intpol = INTERPOLATE;
  141. chopconf.hend = chopper_timing.hend + 3;
  142. chopconf.hstrt = chopper_timing.hstrt - 1;
  143. #if ENABLED(SQUARE_WAVE_STEPPING)
  144. chopconf.dedge = true;
  145. #endif
  146. st.CHOPCONF(chopconf.sr);
  147. st.rms_current(mA, HOLD_MULTIPLIER);
  148. st.microsteps(microsteps);
  149. st.iholddelay(10);
  150. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  151. st.en_pwm_mode(stealth);
  152. st.stored.stealthChop_enabled = stealth;
  153. PWMCONF_t pwmconf{0};
  154. pwmconf.pwm_freq = 0b01; // f_pwm = 2/683 f_clk
  155. pwmconf.pwm_autoscale = true;
  156. pwmconf.pwm_grad = 5;
  157. pwmconf.pwm_ampl = 180;
  158. st.PWMCONF(pwmconf.sr);
  159. #if ENABLED(HYBRID_THRESHOLD)
  160. st.set_pwm_thrs(hyb_thrs);
  161. #else
  162. UNUSED(hyb_thrs);
  163. #endif
  164. st.set_cool_thrs(cool_thrs); // (mm/s)
  165. COOLCONF_t coolconf{0};
  166. coolconf.semin = cool_semin;
  167. coolconf.semax = cool_semax;
  168. coolconf.seup = cool_seup;
  169. coolconf.sedn = cool_sedn;
  170. coolconf.seimin = cool_seimin;
  171. st.COOLCONF(coolconf.sr);
  172. st.GSTAT(); // Clear GSTAT
  173. }
  174. #endif // TMC2130
  175. #if HAS_DRIVER(TMC2160)
  176. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  177. void tmc_init(
  178. TMCMarlin<TMC2160Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st,
  179. const uint16_t mA,
  180. const uint16_t microsteps,
  181. const bool stealth,
  182. const uint32_t hyb_thrs
  183. ) {
  184. st.begin();
  185. CHOPCONF_t chopconf{0};
  186. chopconf.tbl = 1;
  187. chopconf.toff = chopper_timing.toff;
  188. chopconf.intpol = INTERPOLATE;
  189. chopconf.hend = chopper_timing.hend + 3;
  190. chopconf.hstrt = chopper_timing.hstrt - 1;
  191. #if ENABLED(SQUARE_WAVE_STEPPING)
  192. chopconf.dedge = true;
  193. #endif
  194. st.CHOPCONF(chopconf.sr);
  195. st.rms_current(mA, HOLD_MULTIPLIER);
  196. st.microsteps(microsteps);
  197. st.iholddelay(10);
  198. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  199. st.en_pwm_mode(stealth);
  200. st.stored.stealthChop_enabled = stealth;
  201. TMC2160_n::PWMCONF_t pwmconf{0};
  202. pwmconf.pwm_lim = 12;
  203. pwmconf.pwm_reg = 8;
  204. pwmconf.pwm_autograd = true;
  205. pwmconf.pwm_autoscale = true;
  206. pwmconf.pwm_freq = 0b01;
  207. pwmconf.pwm_grad = 14;
  208. pwmconf.pwm_ofs = 36;
  209. st.PWMCONF(pwmconf.sr);
  210. #if ENABLED(HYBRID_THRESHOLD)
  211. st.set_pwm_thrs(hyb_thrs);
  212. #else
  213. UNUSED(hyb_thrs);
  214. #endif
  215. st.GSTAT(); // Clear GSTAT
  216. }
  217. #endif // TMC2160
  218. //
  219. // TMC2208/2209 Driver objects and inits
  220. //
  221. #if HAS_TMC220x
  222. #if AXIS_HAS_UART(X)
  223. #ifdef X_HARDWARE_SERIAL
  224. TMC_UART_DEFINE(HW, X, X);
  225. #else
  226. TMC_UART_DEFINE(SW, X, X);
  227. #endif
  228. #endif
  229. #if AXIS_HAS_UART(X2)
  230. #ifdef X2_HARDWARE_SERIAL
  231. TMC_UART_DEFINE(HW, X2, X);
  232. #else
  233. TMC_UART_DEFINE(SW, X2, X);
  234. #endif
  235. #endif
  236. #if AXIS_HAS_UART(Y)
  237. #ifdef Y_HARDWARE_SERIAL
  238. TMC_UART_DEFINE(HW, Y, Y);
  239. #else
  240. TMC_UART_DEFINE(SW, Y, Y);
  241. #endif
  242. #endif
  243. #if AXIS_HAS_UART(Y2)
  244. #ifdef Y2_HARDWARE_SERIAL
  245. TMC_UART_DEFINE(HW, Y2, Y);
  246. #else
  247. TMC_UART_DEFINE(SW, Y2, Y);
  248. #endif
  249. #endif
  250. #if AXIS_HAS_UART(Z)
  251. #ifdef Z_HARDWARE_SERIAL
  252. TMC_UART_DEFINE(HW, Z, Z);
  253. #else
  254. TMC_UART_DEFINE(SW, Z, Z);
  255. #endif
  256. #endif
  257. #if AXIS_HAS_UART(Z2)
  258. #ifdef Z2_HARDWARE_SERIAL
  259. TMC_UART_DEFINE(HW, Z2, Z);
  260. #else
  261. TMC_UART_DEFINE(SW, Z2, Z);
  262. #endif
  263. #endif
  264. #if AXIS_HAS_UART(Z3)
  265. #ifdef Z3_HARDWARE_SERIAL
  266. TMC_UART_DEFINE(HW, Z3, Z);
  267. #else
  268. TMC_UART_DEFINE(SW, Z3, Z);
  269. #endif
  270. #endif
  271. #if AXIS_HAS_UART(Z4)
  272. #ifdef Z4_HARDWARE_SERIAL
  273. TMC_UART_DEFINE(HW, Z4, Z);
  274. #else
  275. TMC_UART_DEFINE(SW, Z4, Z);
  276. #endif
  277. #endif
  278. #if AXIS_HAS_UART(E0)
  279. #ifdef E0_HARDWARE_SERIAL
  280. TMC_UART_DEFINE_E(HW, 0);
  281. #else
  282. TMC_UART_DEFINE_E(SW, 0);
  283. #endif
  284. #endif
  285. #if AXIS_HAS_UART(E1)
  286. #ifdef E1_HARDWARE_SERIAL
  287. TMC_UART_DEFINE_E(HW, 1);
  288. #else
  289. TMC_UART_DEFINE_E(SW, 1);
  290. #endif
  291. #endif
  292. #if AXIS_HAS_UART(E2)
  293. #ifdef E2_HARDWARE_SERIAL
  294. TMC_UART_DEFINE_E(HW, 2);
  295. #else
  296. TMC_UART_DEFINE_E(SW, 2);
  297. #endif
  298. #endif
  299. #if AXIS_HAS_UART(E3)
  300. #ifdef E3_HARDWARE_SERIAL
  301. TMC_UART_DEFINE_E(HW, 3);
  302. #else
  303. TMC_UART_DEFINE_E(SW, 3);
  304. #endif
  305. #endif
  306. #if AXIS_HAS_UART(E4)
  307. #ifdef E4_HARDWARE_SERIAL
  308. TMC_UART_DEFINE_E(HW, 4);
  309. #else
  310. TMC_UART_DEFINE_E(SW, 4);
  311. #endif
  312. #endif
  313. #if AXIS_HAS_UART(E5)
  314. #ifdef E5_HARDWARE_SERIAL
  315. TMC_UART_DEFINE_E(HW, 5);
  316. #else
  317. TMC_UART_DEFINE_E(SW, 5);
  318. #endif
  319. #endif
  320. #if AXIS_HAS_UART(E6)
  321. #ifdef E6_HARDWARE_SERIAL
  322. TMC_UART_DEFINE_E(HW, 6);
  323. #else
  324. TMC_UART_DEFINE_E(SW, 6);
  325. #endif
  326. #endif
  327. #if AXIS_HAS_UART(E7)
  328. #ifdef E7_HARDWARE_SERIAL
  329. TMC_UART_DEFINE_E(HW, 7);
  330. #else
  331. TMC_UART_DEFINE_E(SW, 7);
  332. #endif
  333. #endif
  334. void tmc_serial_begin() {
  335. #if AXIS_HAS_UART(X)
  336. #ifdef X_HARDWARE_SERIAL
  337. X_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  338. #else
  339. stepperX.beginSerial(TMC_BAUD_RATE);
  340. #endif
  341. #endif
  342. #if AXIS_HAS_UART(X2)
  343. #ifdef X2_HARDWARE_SERIAL
  344. X2_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  345. #else
  346. stepperX2.beginSerial(TMC_BAUD_RATE);
  347. #endif
  348. #endif
  349. #if AXIS_HAS_UART(Y)
  350. #ifdef Y_HARDWARE_SERIAL
  351. Y_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  352. #else
  353. stepperY.beginSerial(TMC_BAUD_RATE);
  354. #endif
  355. #endif
  356. #if AXIS_HAS_UART(Y2)
  357. #ifdef Y2_HARDWARE_SERIAL
  358. Y2_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  359. #else
  360. stepperY2.beginSerial(TMC_BAUD_RATE);
  361. #endif
  362. #endif
  363. #if AXIS_HAS_UART(Z)
  364. #ifdef Z_HARDWARE_SERIAL
  365. Z_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  366. #else
  367. stepperZ.beginSerial(TMC_BAUD_RATE);
  368. #endif
  369. #endif
  370. #if AXIS_HAS_UART(Z2)
  371. #ifdef Z2_HARDWARE_SERIAL
  372. Z2_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  373. #else
  374. stepperZ2.beginSerial(TMC_BAUD_RATE);
  375. #endif
  376. #endif
  377. #if AXIS_HAS_UART(Z3)
  378. #ifdef Z3_HARDWARE_SERIAL
  379. Z3_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  380. #else
  381. stepperZ3.beginSerial(TMC_BAUD_RATE);
  382. #endif
  383. #endif
  384. #if AXIS_HAS_UART(Z4)
  385. #ifdef Z4_HARDWARE_SERIAL
  386. Z4_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  387. #else
  388. stepperZ4.beginSerial(TMC_BAUD_RATE);
  389. #endif
  390. #endif
  391. #if AXIS_HAS_UART(E0)
  392. #ifdef E0_HARDWARE_SERIAL
  393. E0_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  394. #else
  395. stepperE0.beginSerial(TMC_BAUD_RATE);
  396. #endif
  397. #endif
  398. #if AXIS_HAS_UART(E1)
  399. #ifdef E1_HARDWARE_SERIAL
  400. E1_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  401. #else
  402. stepperE1.beginSerial(TMC_BAUD_RATE);
  403. #endif
  404. #endif
  405. #if AXIS_HAS_UART(E2)
  406. #ifdef E2_HARDWARE_SERIAL
  407. E2_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  408. #else
  409. stepperE2.beginSerial(TMC_BAUD_RATE);
  410. #endif
  411. #endif
  412. #if AXIS_HAS_UART(E3)
  413. #ifdef E3_HARDWARE_SERIAL
  414. E3_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  415. #else
  416. stepperE3.beginSerial(TMC_BAUD_RATE);
  417. #endif
  418. #endif
  419. #if AXIS_HAS_UART(E4)
  420. #ifdef E4_HARDWARE_SERIAL
  421. E4_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  422. #else
  423. stepperE4.beginSerial(TMC_BAUD_RATE);
  424. #endif
  425. #endif
  426. #if AXIS_HAS_UART(E5)
  427. #ifdef E5_HARDWARE_SERIAL
  428. E5_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  429. #else
  430. stepperE5.beginSerial(TMC_BAUD_RATE);
  431. #endif
  432. #endif
  433. #if AXIS_HAS_UART(E6)
  434. #ifdef E6_HARDWARE_SERIAL
  435. E6_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  436. #else
  437. stepperE6.beginSerial(TMC_BAUD_RATE);
  438. #endif
  439. #endif
  440. #if AXIS_HAS_UART(E7)
  441. #ifdef E7_HARDWARE_SERIAL
  442. E7_HARDWARE_SERIAL.begin(TMC_BAUD_RATE);
  443. #else
  444. stepperE7.beginSerial(TMC_BAUD_RATE);
  445. #endif
  446. #endif
  447. }
  448. #endif
  449. #if HAS_DRIVER(TMC2208)
  450. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  451. void tmc_init(
  452. TMCMarlin<TMC2208Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st,
  453. const uint16_t mA,
  454. const uint16_t microsteps,
  455. const bool stealth,
  456. const uint32_t hyb_thrs
  457. ) {
  458. TMC2208_n::GCONF_t gconf{0};
  459. gconf.pdn_disable = true; // Use UART
  460. gconf.mstep_reg_select = true; // Select microsteps with UART
  461. gconf.i_scale_analog = false;
  462. gconf.en_spreadcycle = !stealth;
  463. st.GCONF(gconf.sr);
  464. st.stored.stealthChop_enabled = stealth;
  465. TMC2208_n::CHOPCONF_t chopconf{0};
  466. chopconf.tbl = 0b01; // blank_time = 24
  467. chopconf.toff = chopper_timing.toff;
  468. chopconf.intpol = INTERPOLATE;
  469. chopconf.hend = chopper_timing.hend + 3;
  470. chopconf.hstrt = chopper_timing.hstrt - 1;
  471. #if ENABLED(SQUARE_WAVE_STEPPING)
  472. chopconf.dedge = true;
  473. #endif
  474. st.CHOPCONF(chopconf.sr);
  475. st.rms_current(mA, HOLD_MULTIPLIER);
  476. st.microsteps(microsteps);
  477. st.iholddelay(10);
  478. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  479. TMC2208_n::PWMCONF_t pwmconf{0};
  480. pwmconf.pwm_lim = 12;
  481. pwmconf.pwm_reg = 8;
  482. pwmconf.pwm_autograd = true;
  483. pwmconf.pwm_autoscale = true;
  484. pwmconf.pwm_freq = 0b01;
  485. pwmconf.pwm_grad = 14;
  486. pwmconf.pwm_ofs = 36;
  487. st.PWMCONF(pwmconf.sr);
  488. #if ENABLED(HYBRID_THRESHOLD)
  489. st.set_pwm_thrs(hyb_thrs);
  490. #else
  491. UNUSED(hyb_thrs);
  492. #endif
  493. st.GSTAT(0b111); // Clear
  494. delay(200);
  495. }
  496. #endif // TMC2208
  497. #if HAS_DRIVER(TMC2209)
  498. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  499. void tmc_init(
  500. TMCMarlin<TMC2209Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st,
  501. const uint16_t mA,
  502. const uint16_t microsteps,
  503. const bool stealth,
  504. const uint32_t hyb_thrs,
  505. const uint32_t cool_thrs,
  506. const uint8_t cool_semin,
  507. const uint8_t cool_semax,
  508. const uint8_t cool_seup,
  509. const uint8_t cool_sedn,
  510. const bool cool_seimin
  511. ) {
  512. TMC2208_n::GCONF_t gconf{0};
  513. gconf.pdn_disable = true; // Use UART
  514. gconf.mstep_reg_select = true; // Select microsteps with UART
  515. gconf.i_scale_analog = false;
  516. gconf.en_spreadcycle = !stealth;
  517. st.GCONF(gconf.sr);
  518. st.stored.stealthChop_enabled = stealth;
  519. TMC2208_n::CHOPCONF_t chopconf{0};
  520. chopconf.tbl = 0b01; // blank_time = 24
  521. chopconf.toff = chopper_timing.toff;
  522. chopconf.intpol = INTERPOLATE;
  523. chopconf.hend = chopper_timing.hend + 3;
  524. chopconf.hstrt = chopper_timing.hstrt - 1;
  525. #if ENABLED(SQUARE_WAVE_STEPPING)
  526. chopconf.dedge = true;
  527. #endif
  528. st.CHOPCONF(chopconf.sr);
  529. st.rms_current(mA, HOLD_MULTIPLIER);
  530. st.microsteps(microsteps);
  531. st.iholddelay(10);
  532. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  533. TMC2208_n::PWMCONF_t pwmconf{0};
  534. pwmconf.pwm_lim = 12;
  535. pwmconf.pwm_reg = 8;
  536. pwmconf.pwm_autograd = true;
  537. pwmconf.pwm_autoscale = true;
  538. pwmconf.pwm_freq = 0b01;
  539. pwmconf.pwm_grad = 14;
  540. pwmconf.pwm_ofs = 36;
  541. st.PWMCONF(pwmconf.sr);
  542. #if ENABLED(HYBRID_THRESHOLD)
  543. st.set_pwm_thrs(hyb_thrs);
  544. #else
  545. UNUSED(hyb_thrs);
  546. #endif
  547. st.set_cool_thrs(cool_thrs); // (mm/s)
  548. COOLCONF_t coolconf{0};
  549. coolconf.semin = cool_semin;
  550. coolconf.semax = cool_semax;
  551. coolconf.seup = cool_seup;
  552. coolconf.sedn = cool_sedn;
  553. coolconf.seimin = cool_seimin;
  554. st.COOLCONF(coolconf.sr);
  555. st.GSTAT(0b111); // Clear
  556. delay(200);
  557. }
  558. #endif // TMC2209
  559. #if HAS_DRIVER(TMC2660)
  560. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  561. void tmc_init(
  562. TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st,
  563. const uint16_t mA,
  564. const uint16_t microsteps
  565. ) {
  566. st.begin();
  567. TMC2660_n::CHOPCONF_t chopconf{0};
  568. chopconf.tbl = 1;
  569. chopconf.toff = chopper_timing.toff;
  570. chopconf.hend = chopper_timing.hend + 3;
  571. chopconf.hstrt = chopper_timing.hstrt - 1;
  572. st.CHOPCONF(chopconf.sr);
  573. st.sdoff(0);
  574. st.rms_current(mA);
  575. st.microsteps(microsteps);
  576. #if ENABLED(SQUARE_WAVE_STEPPING)
  577. st.dedge(true);
  578. #endif
  579. st.intpol(INTERPOLATE);
  580. st.diss2g(true); // Disable short to ground protection. Too many false readings?
  581. #if ENABLED(TMC_DEBUG)
  582. st.rdsel(0b01);
  583. #endif
  584. }
  585. #endif // TMC2660
  586. #if HAS_DRIVER(TMC5130)
  587. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  588. void tmc_init(
  589. TMCMarlin<TMC5130Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st,
  590. const uint16_t mA,
  591. const uint16_t microsteps,
  592. const bool stealth,
  593. const uint32_t hyb_thrs,
  594. const uint32_t cool_thrs,
  595. const uint8_t cool_semin,
  596. const uint8_t cool_semax,
  597. const uint8_t cool_seup,
  598. const uint8_t cool_sedn,
  599. const bool cool_seimin
  600. ) {
  601. st.begin();
  602. CHOPCONF_t chopconf{0};
  603. chopconf.tbl = 1;
  604. chopconf.toff = chopper_timing.toff;
  605. chopconf.intpol = INTERPOLATE;
  606. chopconf.hend = chopper_timing.hend + 3;
  607. chopconf.hstrt = chopper_timing.hstrt - 1;
  608. #if ENABLED(SQUARE_WAVE_STEPPING)
  609. chopconf.dedge = true;
  610. #endif
  611. st.CHOPCONF(chopconf.sr);
  612. st.rms_current(mA, HOLD_MULTIPLIER);
  613. st.microsteps(microsteps);
  614. st.iholddelay(10);
  615. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  616. st.en_pwm_mode(stealth);
  617. st.stored.stealthChop_enabled = stealth;
  618. PWMCONF_t pwmconf{0};
  619. pwmconf.pwm_freq = 0b01; // f_pwm = 2/683 f_clk
  620. pwmconf.pwm_autoscale = true;
  621. pwmconf.pwm_grad = 5;
  622. pwmconf.pwm_ampl = 180;
  623. st.PWMCONF(pwmconf.sr);
  624. #if ENABLED(HYBRID_THRESHOLD)
  625. st.set_pwm_thrs(hyb_thrs);
  626. #else
  627. UNUSED(hyb_thrs);
  628. #endif
  629. st.set_cool_thrs(cool_thrs); // (mm/s)
  630. COOLCONF_t coolconf{0};
  631. coolconf.semin = cool_semin;
  632. coolconf.semax = cool_semax;
  633. coolconf.seup = cool_seup;
  634. coolconf.sedn = cool_sedn;
  635. coolconf.seimin = cool_seimin;
  636. st.COOLCONF(coolconf.sr);
  637. st.GSTAT(); // Clear GSTAT
  638. }
  639. #endif // TMC5130
  640. #if HAS_DRIVER(TMC5160)
  641. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  642. void tmc_init(
  643. TMCMarlin<TMC5160Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st,
  644. const uint16_t mA,
  645. const uint16_t microsteps,
  646. const bool stealth,
  647. const uint32_t hyb_thrs,
  648. const uint32_t cool_thrs,
  649. const uint8_t cool_semin,
  650. const uint8_t cool_semax,
  651. const uint8_t cool_seup,
  652. const uint8_t cool_sedn,
  653. const bool cool_seimin
  654. ) {
  655. st.begin();
  656. CHOPCONF_t chopconf{0};
  657. chopconf.tbl = 1;
  658. chopconf.toff = chopper_timing.toff;
  659. chopconf.intpol = INTERPOLATE;
  660. chopconf.hend = chopper_timing.hend + 3;
  661. chopconf.hstrt = chopper_timing.hstrt - 1;
  662. #if ENABLED(SQUARE_WAVE_STEPPING)
  663. chopconf.dedge = true;
  664. #endif
  665. st.CHOPCONF(chopconf.sr);
  666. st.rms_current(mA, HOLD_MULTIPLIER);
  667. st.microsteps(microsteps);
  668. st.iholddelay(10);
  669. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  670. st.en_pwm_mode(stealth);
  671. st.stored.stealthChop_enabled = stealth;
  672. TMC2160_n::PWMCONF_t pwmconf{0};
  673. pwmconf.pwm_lim = 12;
  674. pwmconf.pwm_reg = 8;
  675. pwmconf.pwm_autograd = true;
  676. pwmconf.pwm_autoscale = true;
  677. pwmconf.pwm_freq = 0b01;
  678. pwmconf.pwm_grad = 14;
  679. pwmconf.pwm_ofs = 36;
  680. st.PWMCONF(pwmconf.sr);
  681. #if ENABLED(HYBRID_THRESHOLD)
  682. st.set_pwm_thrs(hyb_thrs);
  683. #else
  684. UNUSED(hyb_thrs);
  685. #endif
  686. st.set_cool_thrs(cool_thrs); // (mm/s)
  687. COOLCONF_t coolconf{0};
  688. coolconf.semin = cool_semin;
  689. coolconf.semax = cool_semax;
  690. coolconf.seup = cool_seup;
  691. coolconf.sedn = cool_sedn;
  692. coolconf.seimin = cool_seimin;
  693. st.COOLCONF(coolconf.sr);
  694. st.GSTAT(); // Clear GSTAT
  695. }
  696. #endif // TMC5160
  697. void restore_trinamic_drivers() {
  698. #if AXIS_IS_TMC(X)
  699. stepperX.push();
  700. #endif
  701. #if AXIS_IS_TMC(X2)
  702. stepperX2.push();
  703. #endif
  704. #if AXIS_IS_TMC(Y)
  705. stepperY.push();
  706. #endif
  707. #if AXIS_IS_TMC(Y2)
  708. stepperY2.push();
  709. #endif
  710. #if AXIS_IS_TMC(Z)
  711. stepperZ.push();
  712. #endif
  713. #if AXIS_IS_TMC(Z2)
  714. stepperZ2.push();
  715. #endif
  716. #if AXIS_IS_TMC(Z3)
  717. stepperZ3.push();
  718. #endif
  719. #if AXIS_IS_TMC(Z4)
  720. stepperZ4.push();
  721. #endif
  722. #if AXIS_IS_TMC(E0)
  723. stepperE0.push();
  724. #endif
  725. #if AXIS_IS_TMC(E1)
  726. stepperE1.push();
  727. #endif
  728. #if AXIS_IS_TMC(E2)
  729. stepperE2.push();
  730. #endif
  731. #if AXIS_IS_TMC(E3)
  732. stepperE3.push();
  733. #endif
  734. #if AXIS_IS_TMC(E4)
  735. stepperE4.push();
  736. #endif
  737. #if AXIS_IS_TMC(E5)
  738. stepperE5.push();
  739. #endif
  740. #if AXIS_IS_TMC(E6)
  741. stepperE6.push();
  742. #endif
  743. #if AXIS_IS_TMC(E7)
  744. stepperE7.push();
  745. #endif
  746. }
  747. void reset_trinamic_drivers() {
  748. static constexpr bool stealthchop_by_axis[] = {
  749. #if ENABLED(STEALTHCHOP_XY)
  750. true
  751. #else
  752. false
  753. #endif
  754. ,
  755. #if ENABLED(STEALTHCHOP_Z)
  756. true
  757. #else
  758. false
  759. #endif
  760. ,
  761. #if ENABLED(STEALTHCHOP_E)
  762. true
  763. #else
  764. false
  765. #endif
  766. };
  767. #if AXIS_IS_TMC(X)
  768. TMC_INIT(X, STEALTH_AXIS_XY);
  769. #endif
  770. #if AXIS_IS_TMC(X2)
  771. TMC_INIT(X2, STEALTH_AXIS_XY);
  772. #endif
  773. #if AXIS_IS_TMC(Y)
  774. TMC_INIT(Y, STEALTH_AXIS_XY);
  775. #endif
  776. #if AXIS_IS_TMC(Y2)
  777. TMC_INIT(Y2, STEALTH_AXIS_XY);
  778. #endif
  779. #if AXIS_IS_TMC(Z)
  780. TMC_INIT(Z, STEALTH_AXIS_Z);
  781. #endif
  782. #if AXIS_IS_TMC(Z2)
  783. TMC_INIT(Z2, STEALTH_AXIS_Z);
  784. #endif
  785. #if AXIS_IS_TMC(Z3)
  786. TMC_INIT(Z3, STEALTH_AXIS_Z);
  787. #endif
  788. #if AXIS_IS_TMC(Z4)
  789. TMC_INIT(Z4, STEALTH_AXIS_Z);
  790. #endif
  791. #if AXIS_IS_TMC(E0)
  792. TMC_INIT(E0, STEALTH_AXIS_E);
  793. #endif
  794. #if AXIS_IS_TMC(E1)
  795. TMC_INIT(E1, STEALTH_AXIS_E);
  796. #endif
  797. #if AXIS_IS_TMC(E2)
  798. TMC_INIT(E2, STEALTH_AXIS_E);
  799. #endif
  800. #if AXIS_IS_TMC(E3)
  801. TMC_INIT(E3, STEALTH_AXIS_E);
  802. #endif
  803. #if AXIS_IS_TMC(E4)
  804. TMC_INIT(E4, STEALTH_AXIS_E);
  805. #endif
  806. #if AXIS_IS_TMC(E5)
  807. TMC_INIT(E5, STEALTH_AXIS_E);
  808. #endif
  809. #if AXIS_IS_TMC(E6)
  810. TMC_INIT(E6, STEALTH_AXIS_E);
  811. #endif
  812. #if AXIS_IS_TMC(E7)
  813. TMC_INIT(E7, STEALTH_AXIS_E);
  814. #endif
  815. #if USE_SENSORLESS
  816. #if X_SENSORLESS
  817. #if AXIS_HAS_STALLGUARD(X)
  818. stepperX.homing_threshold(X_STALL_SENSITIVITY);
  819. #endif
  820. #if AXIS_HAS_STALLGUARD(X2) && !X2_SENSORLESS
  821. stepperX2.homing_threshold(X_STALL_SENSITIVITY);
  822. #endif
  823. #endif
  824. #if X2_SENSORLESS
  825. stepperX2.homing_threshold(X2_STALL_SENSITIVITY);
  826. #endif
  827. #if Y_SENSORLESS
  828. #if AXIS_HAS_STALLGUARD(Y)
  829. stepperY.homing_threshold(Y_STALL_SENSITIVITY);
  830. #endif
  831. #if AXIS_HAS_STALLGUARD(Y2)
  832. stepperY2.homing_threshold(Y_STALL_SENSITIVITY);
  833. #endif
  834. #endif
  835. #if Z_SENSORLESS
  836. #if AXIS_HAS_STALLGUARD(Z)
  837. stepperZ.homing_threshold(Z_STALL_SENSITIVITY);
  838. #endif
  839. #if AXIS_HAS_STALLGUARD(Z2)
  840. stepperZ2.homing_threshold(Z_STALL_SENSITIVITY);
  841. #endif
  842. #if AXIS_HAS_STALLGUARD(Z3)
  843. stepperZ3.homing_threshold(Z_STALL_SENSITIVITY);
  844. #endif
  845. #if AXIS_HAS_STALLGUARD(Z4)
  846. stepperZ4.homing_threshold(Z_STALL_SENSITIVITY);
  847. #endif
  848. #endif
  849. #endif
  850. #ifdef TMC_ADV
  851. TMC_ADV()
  852. #endif
  853. stepper.set_directions();
  854. }
  855. #endif // HAS_TRINAMIC