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.

MarlinSerial.h 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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. /**
  23. * stepper/trinamic.cpp
  24. * Stepper driver indirection for Trinamic
  25. */
  26. #include "../../inc/MarlinConfig.h"
  27. #if HAS_TRINAMIC_CONFIG
  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], chopper_timing_##ST, ST##_INTERPOLATE)
  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, float(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, float(ST##_RSENSE), ST##_CHAIN_POS)
  43. #endif
  44. #if ENABLED(TMC_SERIAL_MULTIPLEXER)
  45. #define TMC_UART_HW_DEFINE(IC, ST, L, AI) TMCMarlin<IC##Stepper, L, AI> stepper##ST(&ST##_HARDWARE_SERIAL, float(ST##_RSENSE), ST##_SLAVE_ADDRESS, SERIAL_MUL_PIN1, SERIAL_MUL_PIN2)
  46. #else
  47. #define TMC_UART_HW_DEFINE(IC, ST, L, AI) TMCMarlin<IC##Stepper, L, AI> stepper##ST(&ST##_HARDWARE_SERIAL, float(ST##_RSENSE), ST##_SLAVE_ADDRESS)
  48. #endif
  49. #define TMC_UART_SW_DEFINE(IC, ST, L, AI) TMCMarlin<IC##Stepper, L, AI> stepper##ST(ST##_SERIAL_RX_PIN, ST##_SERIAL_TX_PIN, float(ST##_RSENSE), ST##_SLAVE_ADDRESS)
  50. #define _TMC_SPI_DEFINE(IC, ST, AI) __TMC_SPI_DEFINE(IC, ST, TMC_##ST##_LABEL, AI)
  51. #define TMC_SPI_DEFINE(ST, AI) _TMC_SPI_DEFINE(ST##_DRIVER_TYPE, ST, AI##_AXIS)
  52. #define _TMC_UART_DEFINE(SWHW, IC, ST, AI) TMC_UART_##SWHW##_DEFINE(IC, ST, TMC_##ST##_LABEL, AI)
  53. #define TMC_UART_DEFINE(SWHW, ST, AI) _TMC_UART_DEFINE(SWHW, ST##_DRIVER_TYPE, ST, AI##_AXIS)
  54. #if DISTINCT_E > 1
  55. #define TMC_SPI_DEFINE_E(AI) TMC_SPI_DEFINE(E##AI, E##AI)
  56. #define TMC_UART_DEFINE_E(SWHW, AI) TMC_UART_DEFINE(SWHW, E##AI, E##AI)
  57. #else
  58. #define TMC_SPI_DEFINE_E(AI) TMC_SPI_DEFINE(E##AI, E)
  59. #define TMC_UART_DEFINE_E(SWHW, AI) TMC_UART_DEFINE(SWHW, E##AI, E)
  60. #endif
  61. // Stepper objects of TMC2130/TMC2160/TMC2660/TMC5130/TMC5160 steppers used
  62. #if AXIS_HAS_SPI(X)
  63. TMC_SPI_DEFINE(X, X);
  64. #endif
  65. #if AXIS_HAS_SPI(X2)
  66. TMC_SPI_DEFINE(X2, X);
  67. #endif
  68. #if AXIS_HAS_SPI(Y)
  69. TMC_SPI_DEFINE(Y, Y);
  70. #endif
  71. #if AXIS_HAS_SPI(Y2)
  72. TMC_SPI_DEFINE(Y2, Y);
  73. #endif
  74. #if AXIS_HAS_SPI(Z)
  75. TMC_SPI_DEFINE(Z, Z);
  76. #endif
  77. #if AXIS_HAS_SPI(Z2)
  78. TMC_SPI_DEFINE(Z2, Z);
  79. #endif
  80. #if AXIS_HAS_SPI(Z3)
  81. TMC_SPI_DEFINE(Z3, Z);
  82. #endif
  83. #if AXIS_HAS_SPI(Z4)
  84. TMC_SPI_DEFINE(Z4, Z);
  85. #endif
  86. #if AXIS_HAS_SPI(E0)
  87. TMC_SPI_DEFINE_E(0);
  88. #endif
  89. #if AXIS_HAS_SPI(E1)
  90. TMC_SPI_DEFINE_E(1);
  91. #endif
  92. #if AXIS_HAS_SPI(E2)
  93. TMC_SPI_DEFINE_E(2);
  94. #endif
  95. #if AXIS_HAS_SPI(E3)
  96. TMC_SPI_DEFINE_E(3);
  97. #endif
  98. #if AXIS_HAS_SPI(E4)
  99. TMC_SPI_DEFINE_E(4);
  100. #endif
  101. #if AXIS_HAS_SPI(E5)
  102. TMC_SPI_DEFINE_E(5);
  103. #endif
  104. #if AXIS_HAS_SPI(E6)
  105. TMC_SPI_DEFINE_E(6);
  106. #endif
  107. #if AXIS_HAS_SPI(E7)
  108. TMC_SPI_DEFINE_E(7);
  109. #endif
  110. #ifndef TMC_BAUD_RATE
  111. // Reduce baud rate for boards not already overriding TMC_BAUD_RATE for software serial.
  112. // Testing has shown that 115200 is not 100% reliable on AVR platforms, occasionally
  113. // failing to read status properly. 32-bit platforms typically define an even lower
  114. // TMC_BAUD_RATE, due to differences in how SoftwareSerial libraries work on different
  115. // platforms.
  116. #define TMC_BAUD_RATE TERN(HAS_TMC_SW_SERIAL, 57600, 115200)
  117. #endif
  118. #if HAS_DRIVER(TMC2130)
  119. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  120. void tmc_init(TMCMarlin<TMC2130Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const uint16_t mA, const uint16_t microsteps, const uint32_t hyb_thrs, const bool stealth, const chopper_timing_t &chop_init, const bool interpolate) {
  121. st.begin();
  122. CHOPCONF_t chopconf{0};
  123. chopconf.tbl = 0b01;
  124. chopconf.toff = chop_init.toff;
  125. chopconf.intpol = interpolate;
  126. chopconf.hend = chop_init.hend + 3;
  127. chopconf.hstrt = chop_init.hstrt - 1;
  128. TERN_(SQUARE_WAVE_STEPPING, chopconf.dedge = true);
  129. st.CHOPCONF(chopconf.sr);
  130. st.rms_current(mA, HOLD_MULTIPLIER);
  131. st.microsteps(microsteps);
  132. st.iholddelay(10);
  133. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  134. st.en_pwm_mode(stealth);
  135. st.stored.stealthChop_enabled = stealth;
  136. PWMCONF_t pwmconf{0};
  137. pwmconf.pwm_freq = 0b01; // f_pwm = 2/683 f_clk
  138. pwmconf.pwm_autoscale = true;
  139. pwmconf.pwm_grad = 5;
  140. pwmconf.pwm_ampl = 180;
  141. st.PWMCONF(pwmconf.sr);
  142. TERN(HYBRID_THRESHOLD, st.set_pwm_thrs(hyb_thrs), UNUSED(hyb_thrs));
  143. st.GSTAT(); // Clear GSTAT
  144. }
  145. #endif // TMC2130
  146. #if HAS_DRIVER(TMC2160)
  147. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  148. void tmc_init(TMCMarlin<TMC2160Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const uint16_t mA, const uint16_t microsteps, const uint32_t hyb_thrs, const bool stealth, const chopper_timing_t &chop_init, const bool interpolate) {
  149. st.begin();
  150. CHOPCONF_t chopconf{0};
  151. chopconf.tbl = 0b01;
  152. chopconf.toff = chop_init.toff;
  153. chopconf.intpol = interpolate;
  154. chopconf.hend = chop_init.hend + 3;
  155. chopconf.hstrt = chop_init.hstrt - 1;
  156. TERN_(SQUARE_WAVE_STEPPING, chopconf.dedge = true);
  157. st.CHOPCONF(chopconf.sr);
  158. st.rms_current(mA, HOLD_MULTIPLIER);
  159. st.microsteps(microsteps);
  160. st.iholddelay(10);
  161. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  162. st.en_pwm_mode(stealth);
  163. st.stored.stealthChop_enabled = stealth;
  164. TMC2160_n::PWMCONF_t pwmconf{0};
  165. pwmconf.pwm_lim = 12;
  166. pwmconf.pwm_reg = 8;
  167. pwmconf.pwm_autograd = true;
  168. pwmconf.pwm_autoscale = true;
  169. pwmconf.pwm_freq = 0b01;
  170. pwmconf.pwm_grad = 14;
  171. pwmconf.pwm_ofs = 36;
  172. st.PWMCONF(pwmconf.sr);
  173. TERN(HYBRID_THRESHOLD, st.set_pwm_thrs(hyb_thrs), UNUSED(hyb_thrs));
  174. st.GSTAT(); // Clear GSTAT
  175. }
  176. #endif // TMC2160
  177. //
  178. // TMC2208/2209 Driver objects and inits
  179. //
  180. #if HAS_TMC220x
  181. #if AXIS_HAS_UART(X)
  182. #ifdef X_HARDWARE_SERIAL
  183. TMC_UART_DEFINE(HW, X, X);
  184. #define X_HAS_HW_SERIAL 1
  185. #else
  186. TMC_UART_DEFINE(SW, X, X);
  187. #define X_HAS_SW_SERIAL 1
  188. #endif
  189. #endif
  190. #if AXIS_HAS_UART(X2)
  191. #ifdef X2_HARDWARE_SERIAL
  192. TMC_UART_DEFINE(HW, X2, X);
  193. #define X2_HAS_HW_SERIAL 1
  194. #else
  195. TMC_UART_DEFINE(SW, X2, X);
  196. #define X2_HAS_SW_SERIAL 1
  197. #endif
  198. #endif
  199. #if AXIS_HAS_UART(Y)
  200. #ifdef Y_HARDWARE_SERIAL
  201. TMC_UART_DEFINE(HW, Y, Y);
  202. #define Y_HAS_HW_SERIAL 1
  203. #else
  204. TMC_UART_DEFINE(SW, Y, Y);
  205. #define Y_HAS_SW_SERIAL 1
  206. #endif
  207. #endif
  208. #if AXIS_HAS_UART(Y2)
  209. #ifdef Y2_HARDWARE_SERIAL
  210. TMC_UART_DEFINE(HW, Y2, Y);
  211. #define Y2_HAS_HW_SERIAL 1
  212. #else
  213. TMC_UART_DEFINE(SW, Y2, Y);
  214. #define Y2_HAS_SW_SERIAL 1
  215. #endif
  216. #endif
  217. #if AXIS_HAS_UART(Z)
  218. #ifdef Z_HARDWARE_SERIAL
  219. TMC_UART_DEFINE(HW, Z, Z);
  220. #define Z_HAS_HW_SERIAL 1
  221. #else
  222. TMC_UART_DEFINE(SW, Z, Z);
  223. #define Z_HAS_SW_SERIAL 1
  224. #endif
  225. #endif
  226. #if AXIS_HAS_UART(Z2)
  227. #ifdef Z2_HARDWARE_SERIAL
  228. TMC_UART_DEFINE(HW, Z2, Z);
  229. #define Z2_HAS_HW_SERIAL 1
  230. #else
  231. TMC_UART_DEFINE(SW, Z2, Z);
  232. #define Z2_HAS_SW_SERIAL 1
  233. #endif
  234. #endif
  235. #if AXIS_HAS_UART(Z3)
  236. #ifdef Z3_HARDWARE_SERIAL
  237. TMC_UART_DEFINE(HW, Z3, Z);
  238. #define Z3_HAS_HW_SERIAL 1
  239. #else
  240. TMC_UART_DEFINE(SW, Z3, Z);
  241. #define Z3_HAS_SW_SERIAL 1
  242. #endif
  243. #endif
  244. #if AXIS_HAS_UART(Z4)
  245. #ifdef Z4_HARDWARE_SERIAL
  246. TMC_UART_DEFINE(HW, Z4, Z);
  247. #define Z4_HAS_HW_SERIAL 1
  248. #else
  249. TMC_UART_DEFINE(SW, Z4, Z);
  250. #define Z4_HAS_SW_SERIAL 1
  251. #endif
  252. #endif
  253. #if AXIS_HAS_UART(E0)
  254. #ifdef E0_HARDWARE_SERIAL
  255. TMC_UART_DEFINE_E(HW, 0);
  256. #define E0_HAS_HW_SERIAL 1
  257. #else
  258. TMC_UART_DEFINE_E(SW, 0);
  259. #define E0_HAS_SW_SERIAL 1
  260. #endif
  261. #endif
  262. #if AXIS_HAS_UART(E1)
  263. #ifdef E1_HARDWARE_SERIAL
  264. TMC_UART_DEFINE_E(HW, 1);
  265. #define E1_HAS_HW_SERIAL 1
  266. #else
  267. TMC_UART_DEFINE_E(SW, 1);
  268. #define E1_HAS_SW_SERIAL 1
  269. #endif
  270. #endif
  271. #if AXIS_HAS_UART(E2)
  272. #ifdef E2_HARDWARE_SERIAL
  273. TMC_UART_DEFINE_E(HW, 2);
  274. #define E2_HAS_HW_SERIAL 1
  275. #else
  276. TMC_UART_DEFINE_E(SW, 2);
  277. #define E2_HAS_SW_SERIAL 1
  278. #endif
  279. #endif
  280. #if AXIS_HAS_UART(E3)
  281. #ifdef E3_HARDWARE_SERIAL
  282. TMC_UART_DEFINE_E(HW, 3);
  283. #define E3_HAS_HW_SERIAL 1
  284. #else
  285. TMC_UART_DEFINE_E(SW, 3);
  286. #define E3_HAS_SW_SERIAL 1
  287. #endif
  288. #endif
  289. #if AXIS_HAS_UART(E4)
  290. #ifdef E4_HARDWARE_SERIAL
  291. TMC_UART_DEFINE_E(HW, 4);
  292. #define E4_HAS_HW_SERIAL 1
  293. #else
  294. TMC_UART_DEFINE_E(SW, 4);
  295. #define E4_HAS_SW_SERIAL 1
  296. #endif
  297. #endif
  298. #if AXIS_HAS_UART(E5)
  299. #ifdef E5_HARDWARE_SERIAL
  300. TMC_UART_DEFINE_E(HW, 5);
  301. #define E5_HAS_HW_SERIAL 1
  302. #else
  303. TMC_UART_DEFINE_E(SW, 5);
  304. #define E5_HAS_SW_SERIAL 1
  305. #endif
  306. #endif
  307. #if AXIS_HAS_UART(E6)
  308. #ifdef E6_HARDWARE_SERIAL
  309. TMC_UART_DEFINE_E(HW, 6);
  310. #define E6_HAS_HW_SERIAL 1
  311. #else
  312. TMC_UART_DEFINE_E(SW, 6);
  313. #define E6_HAS_SW_SERIAL 1
  314. #endif
  315. #endif
  316. #if AXIS_HAS_UART(E7)
  317. #ifdef E7_HARDWARE_SERIAL
  318. TMC_UART_DEFINE_E(HW, 7);
  319. #define E7_HAS_HW_SERIAL 1
  320. #else
  321. TMC_UART_DEFINE_E(SW, 7);
  322. #define E7_HAS_SW_SERIAL 1
  323. #endif
  324. #endif
  325. enum TMCAxis : uint8_t { X, Y, Z, X2, Y2, Z2, Z3, Z4, E0, E1, E2, E3, E4, E5, E6, E7, TOTAL };
  326. void tmc_serial_begin() {
  327. #if HAS_TMC_HW_SERIAL
  328. struct {
  329. const void *ptr[TMCAxis::TOTAL];
  330. bool began(const TMCAxis a, const void * const p) {
  331. LOOP_L_N(i, a) if (p == ptr[i]) return true;
  332. ptr[a] = p; return false;
  333. };
  334. } sp_helper;
  335. #define HW_SERIAL_BEGIN(A) do{ if (!sp_helper.began(TMCAxis::A, &A##_HARDWARE_SERIAL)) \
  336. A##_HARDWARE_SERIAL.begin(TMC_BAUD_RATE); }while(0)
  337. #endif
  338. #if AXIS_HAS_UART(X)
  339. #ifdef X_HARDWARE_SERIAL
  340. HW_SERIAL_BEGIN(X);
  341. #else
  342. stepperX.beginSerial(TMC_BAUD_RATE);
  343. #endif
  344. #endif
  345. #if AXIS_HAS_UART(X2)
  346. #ifdef X2_HARDWARE_SERIAL
  347. HW_SERIAL_BEGIN(X2);
  348. #else
  349. stepperX2.beginSerial(TMC_BAUD_RATE);
  350. #endif
  351. #endif
  352. #if AXIS_HAS_UART(Y)
  353. #ifdef Y_HARDWARE_SERIAL
  354. HW_SERIAL_BEGIN(Y);
  355. #else
  356. stepperY.beginSerial(TMC_BAUD_RATE);
  357. #endif
  358. #endif
  359. #if AXIS_HAS_UART(Y2)
  360. #ifdef Y2_HARDWARE_SERIAL
  361. HW_SERIAL_BEGIN(Y2);
  362. #else
  363. stepperY2.beginSerial(TMC_BAUD_RATE);
  364. #endif
  365. #endif
  366. #if AXIS_HAS_UART(Z)
  367. #ifdef Z_HARDWARE_SERIAL
  368. HW_SERIAL_BEGIN(Z);
  369. #else
  370. stepperZ.beginSerial(TMC_BAUD_RATE);
  371. #endif
  372. #endif
  373. #if AXIS_HAS_UART(Z2)
  374. #ifdef Z2_HARDWARE_SERIAL
  375. HW_SERIAL_BEGIN(Z2);
  376. #else
  377. stepperZ2.beginSerial(TMC_BAUD_RATE);
  378. #endif
  379. #endif
  380. #if AXIS_HAS_UART(Z3)
  381. #ifdef Z3_HARDWARE_SERIAL
  382. HW_SERIAL_BEGIN(Z3);
  383. #else
  384. stepperZ3.beginSerial(TMC_BAUD_RATE);
  385. #endif
  386. #endif
  387. #if AXIS_HAS_UART(Z4)
  388. #ifdef Z4_HARDWARE_SERIAL
  389. HW_SERIAL_BEGIN(Z4);
  390. #else
  391. stepperZ4.beginSerial(TMC_BAUD_RATE);
  392. #endif
  393. #endif
  394. #if AXIS_HAS_UART(E0)
  395. #ifdef E0_HARDWARE_SERIAL
  396. HW_SERIAL_BEGIN(E0);
  397. #else
  398. stepperE0.beginSerial(TMC_BAUD_RATE);
  399. #endif
  400. #endif
  401. #if AXIS_HAS_UART(E1)
  402. #ifdef E1_HARDWARE_SERIAL
  403. HW_SERIAL_BEGIN(E1);
  404. #else
  405. stepperE1.beginSerial(TMC_BAUD_RATE);
  406. #endif
  407. #endif
  408. #if AXIS_HAS_UART(E2)
  409. #ifdef E2_HARDWARE_SERIAL
  410. HW_SERIAL_BEGIN(E2);
  411. #else
  412. stepperE2.beginSerial(TMC_BAUD_RATE);
  413. #endif
  414. #endif
  415. #if AXIS_HAS_UART(E3)
  416. #ifdef E3_HARDWARE_SERIAL
  417. HW_SERIAL_BEGIN(E3);
  418. #else
  419. stepperE3.beginSerial(TMC_BAUD_RATE);
  420. #endif
  421. #endif
  422. #if AXIS_HAS_UART(E4)
  423. #ifdef E4_HARDWARE_SERIAL
  424. HW_SERIAL_BEGIN(E4);
  425. #else
  426. stepperE4.beginSerial(TMC_BAUD_RATE);
  427. #endif
  428. #endif
  429. #if AXIS_HAS_UART(E5)
  430. #ifdef E5_HARDWARE_SERIAL
  431. HW_SERIAL_BEGIN(E5);
  432. #else
  433. stepperE5.beginSerial(TMC_BAUD_RATE);
  434. #endif
  435. #endif
  436. #if AXIS_HAS_UART(E6)
  437. #ifdef E6_HARDWARE_SERIAL
  438. HW_SERIAL_BEGIN(E6);
  439. #else
  440. stepperE6.beginSerial(TMC_BAUD_RATE);
  441. #endif
  442. #endif
  443. #if AXIS_HAS_UART(E7)
  444. #ifdef E7_HARDWARE_SERIAL
  445. HW_SERIAL_BEGIN(E7);
  446. #else
  447. stepperE7.beginSerial(TMC_BAUD_RATE);
  448. #endif
  449. #endif
  450. }
  451. #endif
  452. #if HAS_DRIVER(TMC2208)
  453. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  454. void tmc_init(TMCMarlin<TMC2208Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const uint16_t mA, const uint16_t microsteps, const uint32_t hyb_thrs, const bool stealth, const chopper_timing_t &chop_init, const bool interpolate) {
  455. TMC2208_n::GCONF_t gconf{0};
  456. gconf.pdn_disable = true; // Use UART
  457. gconf.mstep_reg_select = true; // Select microsteps with UART
  458. gconf.i_scale_analog = false;
  459. gconf.en_spreadcycle = !stealth;
  460. st.GCONF(gconf.sr);
  461. st.stored.stealthChop_enabled = stealth;
  462. TMC2208_n::CHOPCONF_t chopconf{0};
  463. chopconf.tbl = 0b01; // blank_time = 24
  464. chopconf.toff = chop_init.toff;
  465. chopconf.intpol = interpolate;
  466. chopconf.hend = chop_init.hend + 3;
  467. chopconf.hstrt = chop_init.hstrt - 1;
  468. TERN_(SQUARE_WAVE_STEPPING, chopconf.dedge = true);
  469. st.CHOPCONF(chopconf.sr);
  470. st.rms_current(mA, HOLD_MULTIPLIER);
  471. st.microsteps(microsteps);
  472. st.iholddelay(10);
  473. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  474. TMC2208_n::PWMCONF_t pwmconf{0};
  475. pwmconf.pwm_lim = 12;
  476. pwmconf.pwm_reg = 8;
  477. pwmconf.pwm_autograd = true;
  478. pwmconf.pwm_autoscale = true;
  479. pwmconf.pwm_freq = 0b01;
  480. pwmconf.pwm_grad = 14;
  481. pwmconf.pwm_ofs = 36;
  482. st.PWMCONF(pwmconf.sr);
  483. TERN(HYBRID_THRESHOLD, st.set_pwm_thrs(hyb_thrs), UNUSED(hyb_thrs));
  484. st.GSTAT(0b111); // Clear
  485. delay(200);
  486. }
  487. #endif // TMC2208
  488. #if HAS_DRIVER(TMC2209)
  489. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  490. void tmc_init(TMCMarlin<TMC2209Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const uint16_t mA, const uint16_t microsteps, const uint32_t hyb_thrs, const bool stealth, const chopper_timing_t &chop_init, const bool interpolate) {
  491. TMC2208_n::GCONF_t gconf{0};
  492. gconf.pdn_disable = true; // Use UART
  493. gconf.mstep_reg_select = true; // Select microsteps with UART
  494. gconf.i_scale_analog = false;
  495. gconf.en_spreadcycle = !stealth;
  496. st.GCONF(gconf.sr);
  497. st.stored.stealthChop_enabled = stealth;
  498. TMC2208_n::CHOPCONF_t chopconf{0};
  499. chopconf.tbl = 0b01; // blank_time = 24
  500. chopconf.toff = chop_init.toff;
  501. chopconf.intpol = interpolate;
  502. chopconf.hend = chop_init.hend + 3;
  503. chopconf.hstrt = chop_init.hstrt - 1;
  504. TERN_(SQUARE_WAVE_STEPPING, chopconf.dedge = true);
  505. st.CHOPCONF(chopconf.sr);
  506. st.rms_current(mA, HOLD_MULTIPLIER);
  507. st.microsteps(microsteps);
  508. st.iholddelay(10);
  509. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  510. TMC2208_n::PWMCONF_t pwmconf{0};
  511. pwmconf.pwm_lim = 12;
  512. pwmconf.pwm_reg = 8;
  513. pwmconf.pwm_autograd = true;
  514. pwmconf.pwm_autoscale = true;
  515. pwmconf.pwm_freq = 0b01;
  516. pwmconf.pwm_grad = 14;
  517. pwmconf.pwm_ofs = 36;
  518. st.PWMCONF(pwmconf.sr);
  519. TERN(HYBRID_THRESHOLD, st.set_pwm_thrs(hyb_thrs), UNUSED(hyb_thrs));
  520. st.GSTAT(0b111); // Clear
  521. delay(200);
  522. }
  523. #endif // TMC2209
  524. #if HAS_DRIVER(TMC2660)
  525. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  526. 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, const chopper_timing_t &chop_init, const bool interpolate) {
  527. st.begin();
  528. TMC2660_n::CHOPCONF_t chopconf{0};
  529. chopconf.tbl = 0b01;
  530. chopconf.toff = chop_init.toff;
  531. chopconf.hend = chop_init.hend + 3;
  532. chopconf.hstrt = chop_init.hstrt - 1;
  533. st.CHOPCONF(chopconf.sr);
  534. st.sdoff(0);
  535. st.rms_current(mA);
  536. st.microsteps(microsteps);
  537. TERN_(SQUARE_WAVE_STEPPING, st.dedge(true));
  538. st.intpol(interpolate);
  539. st.diss2g(true); // Disable short to ground protection. Too many false readings?
  540. TERN_(TMC_DEBUG, st.rdsel(0b01));
  541. }
  542. #endif // TMC2660
  543. #if HAS_DRIVER(TMC5130)
  544. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  545. void tmc_init(TMCMarlin<TMC5130Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const uint16_t mA, const uint16_t microsteps, const uint32_t hyb_thrs, const bool stealth, const chopper_timing_t &chop_init, const bool interpolate) {
  546. st.begin();
  547. CHOPCONF_t chopconf{0};
  548. chopconf.tbl = 0b01;
  549. chopconf.toff = chop_init.toff;
  550. chopconf.intpol = interpolate;
  551. chopconf.hend = chop_init.hend + 3;
  552. chopconf.hstrt = chop_init.hstrt - 1;
  553. TERN_(SQUARE_WAVE_STEPPING, chopconf.dedge = true);
  554. st.CHOPCONF(chopconf.sr);
  555. st.rms_current(mA, HOLD_MULTIPLIER);
  556. st.microsteps(microsteps);
  557. st.iholddelay(10);
  558. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  559. st.en_pwm_mode(stealth);
  560. st.stored.stealthChop_enabled = stealth;
  561. PWMCONF_t pwmconf{0};
  562. pwmconf.pwm_freq = 0b01; // f_pwm = 2/683 f_clk
  563. pwmconf.pwm_autoscale = true;
  564. pwmconf.pwm_grad = 5;
  565. pwmconf.pwm_ampl = 180;
  566. st.PWMCONF(pwmconf.sr);
  567. TERN(HYBRID_THRESHOLD, st.set_pwm_thrs(hyb_thrs), UNUSED(hyb_thrs));
  568. st.GSTAT(); // Clear GSTAT
  569. }
  570. #endif // TMC5130
  571. #if HAS_DRIVER(TMC5160)
  572. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  573. void tmc_init(TMCMarlin<TMC5160Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> &st, const uint16_t mA, const uint16_t microsteps, const uint32_t hyb_thrs, const bool stealth, const chopper_timing_t &chop_init, const bool interpolate) {
  574. st.begin();
  575. CHOPCONF_t chopconf{0};
  576. chopconf.tbl = 0b01;
  577. chopconf.toff = chop_init.toff;
  578. chopconf.intpol = interpolate;
  579. chopconf.hend = chop_init.hend + 3;
  580. chopconf.hstrt = chop_init.hstrt - 1;
  581. TERN_(SQUARE_WAVE_STEPPING, chopconf.dedge = true);
  582. st.CHOPCONF(chopconf.sr);
  583. st.rms_current(mA, HOLD_MULTIPLIER);
  584. st.microsteps(microsteps);
  585. st.iholddelay(10);
  586. st.TPOWERDOWN(128); // ~2s until driver lowers to hold current
  587. st.en_pwm_mode(stealth);
  588. st.stored.stealthChop_enabled = stealth;
  589. TMC2160_n::PWMCONF_t pwmconf{0};
  590. pwmconf.pwm_lim = 12;
  591. pwmconf.pwm_reg = 8;
  592. pwmconf.pwm_autograd = true;
  593. pwmconf.pwm_autoscale = true;
  594. pwmconf.pwm_freq = 0b01;
  595. pwmconf.pwm_grad = 14;
  596. pwmconf.pwm_ofs = 36;
  597. st.PWMCONF(pwmconf.sr);
  598. #if ENABLED(HYBRID_THRESHOLD)
  599. st.set_pwm_thrs(hyb_thrs);
  600. #else
  601. UNUSED(hyb_thrs);
  602. #endif
  603. st.GSTAT(); // Clear GSTAT
  604. }
  605. #endif // TMC5160
  606. void restore_trinamic_drivers() {
  607. #if AXIS_IS_TMC(X)
  608. stepperX.push();
  609. #endif
  610. #if AXIS_IS_TMC(X2)
  611. stepperX2.push();
  612. #endif
  613. #if AXIS_IS_TMC(Y)
  614. stepperY.push();
  615. #endif
  616. #if AXIS_IS_TMC(Y2)
  617. stepperY2.push();
  618. #endif
  619. #if AXIS_IS_TMC(Z)
  620. stepperZ.push();
  621. #endif
  622. #if AXIS_IS_TMC(Z2)
  623. stepperZ2.push();
  624. #endif
  625. #if AXIS_IS_TMC(Z3)
  626. stepperZ3.push();
  627. #endif
  628. #if AXIS_IS_TMC(Z4)
  629. stepperZ4.push();
  630. #endif
  631. #if AXIS_IS_TMC(E0)
  632. stepperE0.push();
  633. #endif
  634. #if AXIS_IS_TMC(E1)
  635. stepperE1.push();
  636. #endif
  637. #if AXIS_IS_TMC(E2)
  638. stepperE2.push();
  639. #endif
  640. #if AXIS_IS_TMC(E3)
  641. stepperE3.push();
  642. #endif
  643. #if AXIS_IS_TMC(E4)
  644. stepperE4.push();
  645. #endif
  646. #if AXIS_IS_TMC(E5)
  647. stepperE5.push();
  648. #endif
  649. #if AXIS_IS_TMC(E6)
  650. stepperE6.push();
  651. #endif
  652. #if AXIS_IS_TMC(E7)
  653. stepperE7.push();
  654. #endif
  655. }
  656. void reset_trinamic_drivers() {
  657. static constexpr bool stealthchop_by_axis[] = { ENABLED(STEALTHCHOP_XY), ENABLED(STEALTHCHOP_Z), ENABLED(STEALTHCHOP_E) };
  658. #if AXIS_IS_TMC(X)
  659. TMC_INIT(X, STEALTH_AXIS_XY);
  660. #endif
  661. #if AXIS_IS_TMC(X2)
  662. TMC_INIT(X2, STEALTH_AXIS_XY);
  663. #endif
  664. #if AXIS_IS_TMC(Y)
  665. TMC_INIT(Y, STEALTH_AXIS_XY);
  666. #endif
  667. #if AXIS_IS_TMC(Y2)
  668. TMC_INIT(Y2, STEALTH_AXIS_XY);
  669. #endif
  670. #if AXIS_IS_TMC(Z)
  671. TMC_INIT(Z, STEALTH_AXIS_Z);
  672. #endif
  673. #if AXIS_IS_TMC(Z2)
  674. TMC_INIT(Z2, STEALTH_AXIS_Z);
  675. #endif
  676. #if AXIS_IS_TMC(Z3)
  677. TMC_INIT(Z3, STEALTH_AXIS_Z);
  678. #endif
  679. #if AXIS_IS_TMC(Z4)
  680. TMC_INIT(Z4, STEALTH_AXIS_Z);
  681. #endif
  682. #if AXIS_IS_TMC(E0)
  683. TMC_INIT(E0, STEALTH_AXIS_E);
  684. #endif
  685. #if AXIS_IS_TMC(E1)
  686. TMC_INIT(E1, STEALTH_AXIS_E);
  687. #endif
  688. #if AXIS_IS_TMC(E2)
  689. TMC_INIT(E2, STEALTH_AXIS_E);
  690. #endif
  691. #if AXIS_IS_TMC(E3)
  692. TMC_INIT(E3, STEALTH_AXIS_E);
  693. #endif
  694. #if AXIS_IS_TMC(E4)
  695. TMC_INIT(E4, STEALTH_AXIS_E);
  696. #endif
  697. #if AXIS_IS_TMC(E5)
  698. TMC_INIT(E5, STEALTH_AXIS_E);
  699. #endif
  700. #if AXIS_IS_TMC(E6)
  701. TMC_INIT(E6, STEALTH_AXIS_E);
  702. #endif
  703. #if AXIS_IS_TMC(E7)
  704. TMC_INIT(E7, STEALTH_AXIS_E);
  705. #endif
  706. #if USE_SENSORLESS
  707. #if X_SENSORLESS
  708. stepperX.homing_threshold(X_STALL_SENSITIVITY);
  709. #if AXIS_HAS_STALLGUARD(X2)
  710. stepperX2.homing_threshold(CAT(TERN(X2_SENSORLESS, X2, X), _STALL_SENSITIVITY));
  711. #endif
  712. #endif
  713. #if Y_SENSORLESS
  714. stepperY.homing_threshold(Y_STALL_SENSITIVITY);
  715. #if AXIS_HAS_STALLGUARD(Y2)
  716. stepperY2.homing_threshold(CAT(TERN(Y2_SENSORLESS, Y2, Y), _STALL_SENSITIVITY));
  717. #endif
  718. #endif
  719. #if Z_SENSORLESS
  720. stepperZ.homing_threshold(Z_STALL_SENSITIVITY);
  721. #if AXIS_HAS_STALLGUARD(Z2)
  722. stepperZ2.homing_threshold(CAT(TERN(Z2_SENSORLESS, Z2, Z), _STALL_SENSITIVITY));
  723. #endif
  724. #if AXIS_HAS_STALLGUARD(Z3)
  725. stepperZ3.homing_threshold(CAT(TERN(Z3_SENSORLESS, Z3, Z), _STALL_SENSITIVITY));
  726. #endif
  727. #if AXIS_HAS_STALLGUARD(Z4)
  728. stepperZ4.homing_threshold(CAT(TERN(Z4_SENSORLESS, Z4, Z), _STALL_SENSITIVITY));
  729. #endif
  730. #endif
  731. #endif
  732. #ifdef TMC_ADV
  733. TMC_ADV()
  734. #endif
  735. stepper.set_directions();
  736. }
  737. // TMC Slave Address Conflict Detection
  738. //
  739. // Conflict detection is performed in the following way. Similar methods are used for
  740. // hardware and software serial, but the implementations are indepenent.
  741. //
  742. // 1. Populate a data structure with UART parameters and addresses for all possible axis.
  743. // If an axis is not in use, populate it with recognizable placeholder data.
  744. // 2. For each axis in use, static_assert using a constexpr function, which counts the
  745. // number of matching/conflicting axis. If the value is not exactly 1, fail.
  746. #if ANY_AXIS_HAS(HW_SERIAL)
  747. // Hardware serial names are compared as strings, since actually resolving them cannot occur in a constexpr.
  748. // Using a fixed-length character array for the port name allows this to be constexpr compatible.
  749. struct SanityHwSerialDetails { const char port[20]; uint32_t address; };
  750. #define TMC_HW_DETAIL_ARGS(A) TERN(A##_HAS_HW_SERIAL, STRINGIFY(A##_HARDWARE_SERIAL), ""), TERN0(A##_HAS_HW_SERIAL, A##_SLAVE_ADDRESS)
  751. #define TMC_HW_DETAIL(A) {TMC_HW_DETAIL_ARGS(A)}
  752. constexpr SanityHwSerialDetails sanity_tmc_hw_details[] = {
  753. TMC_HW_DETAIL(X), TMC_HW_DETAIL(X2),
  754. TMC_HW_DETAIL(Y), TMC_HW_DETAIL(Y2),
  755. TMC_HW_DETAIL(Z), TMC_HW_DETAIL(Z2), TMC_HW_DETAIL(Z3), TMC_HW_DETAIL(Z4),
  756. TMC_HW_DETAIL(E0), TMC_HW_DETAIL(E1), TMC_HW_DETAIL(E2), TMC_HW_DETAIL(E3), TMC_HW_DETAIL(E4), TMC_HW_DETAIL(E5), TMC_HW_DETAIL(E6), TMC_HW_DETAIL(E7)
  757. };
  758. // constexpr compatible string comparison
  759. constexpr bool str_eq_ce(const char * a, const char * b) {
  760. return *a == *b && (*a == '\0' || str_eq_ce(a+1,b+1));
  761. }
  762. constexpr bool sc_hw_done(size_t start, size_t end) { return start == end; }
  763. constexpr bool sc_hw_skip(const char* port_name) { return !(*port_name); }
  764. constexpr bool sc_hw_match(const char* port_name, uint32_t address, size_t start, size_t end) {
  765. return !sc_hw_done(start, end) && !sc_hw_skip(port_name) && (address == sanity_tmc_hw_details[start].address && str_eq_ce(port_name, sanity_tmc_hw_details[start].port));
  766. }
  767. constexpr int count_tmc_hw_serial_matches(const char* port_name, uint32_t address, size_t start, size_t end) {
  768. return sc_hw_done(start, end) ? 0 : ((sc_hw_skip(port_name) ? 0 : (sc_hw_match(port_name, address, start, end) ? 1 : 0)) + count_tmc_hw_serial_matches(port_name, address, start + 1, end));
  769. }
  770. #define TMC_HWSERIAL_CONFLICT_MSG(A) STRINGIFY(A) "_SLAVE_ADDRESS conflicts with another driver using the same " STRINGIFY(A) "_HARDWARE_SERIAL"
  771. #define SA_NO_TMC_HW_C(A) static_assert(1 >= count_tmc_hw_serial_matches(TMC_HW_DETAIL_ARGS(A), 0, COUNT(sanity_tmc_hw_details)), TMC_HWSERIAL_CONFLICT_MSG(A));
  772. SA_NO_TMC_HW_C(X);SA_NO_TMC_HW_C(X2);
  773. SA_NO_TMC_HW_C(Y);SA_NO_TMC_HW_C(Y2);
  774. SA_NO_TMC_HW_C(Z);SA_NO_TMC_HW_C(Z2);SA_NO_TMC_HW_C(Z3);SA_NO_TMC_HW_C(Z4);
  775. SA_NO_TMC_HW_C(E0);SA_NO_TMC_HW_C(E1);SA_NO_TMC_HW_C(E2);SA_NO_TMC_HW_C(E3);SA_NO_TMC_HW_C(E4);SA_NO_TMC_HW_C(E5);SA_NO_TMC_HW_C(E6);SA_NO_TMC_HW_C(E7);
  776. #endif
  777. #if ANY_AXIS_HAS(SW_SERIAL)
  778. struct SanitySwSerialDetails { int32_t txpin; int32_t rxpin; uint32_t address; };
  779. #define TMC_SW_DETAIL_ARGS(A) TERN(A##_HAS_SW_SERIAL, A##_SERIAL_TX_PIN, -1), TERN(A##_HAS_SW_SERIAL, A##_SERIAL_RX_PIN, -1), TERN0(A##_HAS_SW_SERIAL, A##_SLAVE_ADDRESS)
  780. #define TMC_SW_DETAIL(A) TMC_SW_DETAIL_ARGS(A)
  781. constexpr SanitySwSerialDetails sanity_tmc_sw_details[] = {
  782. TMC_SW_DETAIL(X), TMC_SW_DETAIL(X2),
  783. TMC_SW_DETAIL(Y), TMC_SW_DETAIL(Y2),
  784. TMC_SW_DETAIL(Z), TMC_SW_DETAIL(Z2), TMC_SW_DETAIL(Z3), TMC_SW_DETAIL(Z4),
  785. TMC_SW_DETAIL(E0), TMC_SW_DETAIL(E1), TMC_SW_DETAIL(E2), TMC_SW_DETAIL(E3), TMC_SW_DETAIL(E4), TMC_SW_DETAIL(E5), TMC_SW_DETAIL(E6), TMC_SW_DETAIL(E7)
  786. };
  787. constexpr bool sc_sw_done(size_t start, size_t end) { return start == end; }
  788. constexpr bool sc_sw_skip(int32_t txpin) { return txpin < 0; }
  789. constexpr bool sc_sw_match(int32_t txpin, int32_t rxpin, uint32_t address, size_t start, size_t end) {
  790. return !sc_sw_done(start, end) && !sc_sw_skip(txpin) && (txpin == sanity_tmc_sw_details[start].txpin || rxpin == sanity_tmc_sw_details[start].rxpin) && (address == sanity_tmc_sw_details[start].address);
  791. }
  792. constexpr int count_tmc_sw_serial_matches(int32_t txpin, int32_t rxpin, uint32_t address, size_t start, size_t end) {
  793. return sc_sw_done(start, end) ? 0 : ((sc_sw_skip(txpin) ? 0 : (sc_sw_match(txpin, rxpin, address, start, end) ? 1 : 0)) + count_tmc_sw_serial_matches(txpin, rxpin, address, start + 1, end));
  794. }
  795. #define TMC_SWSERIAL_CONFLICT_MSG(A) STRINGIFY(A) "_SLAVE_ADDRESS conflicts with another driver using the same " STRINGIFY(A) "_SERIAL_RX_PIN or " STRINGIFY(A) "_SERIAL_TX_PIN"
  796. #define SA_NO_TMC_SW_C(A) static_assert(1 >= count_tmc_sw_serial_matches(TMC_SW_DETAIL_ARGS(A), 0, COUNT(sanity_tmc_sw_details)), TMC_SWSERIAL_CONFLICT_MSG(A));
  797. SA_NO_TMC_SW_C(X);SA_NO_TMC_SW_C(X2);
  798. SA_NO_TMC_SW_C(Y);SA_NO_TMC_SW_C(Y2);
  799. SA_NO_TMC_SW_C(Z);SA_NO_TMC_SW_C(Z2);SA_NO_TMC_SW_C(Z3);SA_NO_TMC_SW_C(Z4);
  800. SA_NO_TMC_SW_C(E0);SA_NO_TMC_SW_C(E1);SA_NO_TMC_SW_C(E2);SA_NO_TMC_SW_C(E3);SA_NO_TMC_SW_C(E4);SA_NO_TMC_SW_C(E5);SA_NO_TMC_SW_C(E6);SA_NO_TMC_SW_C(E7);
  801. #endif
  802. #endif // HAS_TRINAMIC_CONFIG