My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

stepper_indirection.cpp 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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_indirection.cpp
  24. *
  25. * Stepper motor driver indirection to allow some stepper functions to
  26. * be done via SPI/I2c instead of direct pin manipulation.
  27. *
  28. * Part of Marlin
  29. *
  30. * Copyright (c) 2015 Dominik Wenger
  31. */
  32. #include "stepper_indirection.h"
  33. #include "MarlinConfig.h"
  34. //
  35. // TMC26X Driver objects and inits
  36. //
  37. #if ENABLED(HAVE_TMCDRIVER)
  38. #include <SPI.h>
  39. #include <TMC26XStepper.h>
  40. #define _TMC_DEFINE(ST) TMC26XStepper stepper##ST(200, ST##_ENABLE_PIN, ST##_STEP_PIN, ST##_DIR_PIN, ST##_MAX_CURRENT, ST##_SENSE_RESISTOR)
  41. #if ENABLED(X_IS_TMC)
  42. _TMC_DEFINE(X);
  43. #endif
  44. #if ENABLED(X2_IS_TMC)
  45. _TMC_DEFINE(X2);
  46. #endif
  47. #if ENABLED(Y_IS_TMC)
  48. _TMC_DEFINE(Y);
  49. #endif
  50. #if ENABLED(Y2_IS_TMC)
  51. _TMC_DEFINE(Y2);
  52. #endif
  53. #if ENABLED(Z_IS_TMC)
  54. _TMC_DEFINE(Z);
  55. #endif
  56. #if ENABLED(Z2_IS_TMC)
  57. _TMC_DEFINE(Z2);
  58. #endif
  59. #if ENABLED(E0_IS_TMC)
  60. _TMC_DEFINE(E0);
  61. #endif
  62. #if ENABLED(E1_IS_TMC)
  63. _TMC_DEFINE(E1);
  64. #endif
  65. #if ENABLED(E2_IS_TMC)
  66. _TMC_DEFINE(E2);
  67. #endif
  68. #if ENABLED(E3_IS_TMC)
  69. _TMC_DEFINE(E3);
  70. #endif
  71. #if ENABLED(E4_IS_TMC)
  72. _TMC_DEFINE(E4);
  73. #endif
  74. #define _TMC_INIT(A) do{ \
  75. stepper##A.setMicrosteps(A##_MICROSTEPS); \
  76. stepper##A.start(); \
  77. }while(0)
  78. void tmc_init() {
  79. #if ENABLED(X_IS_TMC)
  80. _TMC_INIT(X);
  81. #endif
  82. #if ENABLED(X2_IS_TMC)
  83. _TMC_INIT(X2);
  84. #endif
  85. #if ENABLED(Y_IS_TMC)
  86. _TMC_INIT(Y);
  87. #endif
  88. #if ENABLED(Y2_IS_TMC)
  89. _TMC_INIT(Y2);
  90. #endif
  91. #if ENABLED(Z_IS_TMC)
  92. _TMC_INIT(Z);
  93. #endif
  94. #if ENABLED(Z2_IS_TMC)
  95. _TMC_INIT(Z2);
  96. #endif
  97. #if ENABLED(E0_IS_TMC)
  98. _TMC_INIT(E0);
  99. #endif
  100. #if ENABLED(E1_IS_TMC)
  101. _TMC_INIT(E1);
  102. #endif
  103. #if ENABLED(E2_IS_TMC)
  104. _TMC_INIT(E2);
  105. #endif
  106. #if ENABLED(E3_IS_TMC)
  107. _TMC_INIT(E3);
  108. #endif
  109. #if ENABLED(E4_IS_TMC)
  110. _TMC_INIT(E4);
  111. #endif
  112. }
  113. #endif // HAVE_TMCDRIVER
  114. //
  115. // TMC2130 Driver objects and inits
  116. //
  117. #if ENABLED(HAVE_TMC2130)
  118. #include <SPI.h>
  119. #include <TMC2130Stepper.h>
  120. #include "enum.h"
  121. #define _TMC2130_DEFINE(ST) TMC2130Stepper stepper##ST(ST##_ENABLE_PIN, ST##_DIR_PIN, ST##_STEP_PIN, ST##_CS_PIN)
  122. // Stepper objects of TMC2130 steppers used
  123. #if ENABLED(X_IS_TMC2130)
  124. _TMC2130_DEFINE(X);
  125. #endif
  126. #if ENABLED(X2_IS_TMC2130)
  127. _TMC2130_DEFINE(X2);
  128. #endif
  129. #if ENABLED(Y_IS_TMC2130)
  130. _TMC2130_DEFINE(Y);
  131. #endif
  132. #if ENABLED(Y2_IS_TMC2130)
  133. _TMC2130_DEFINE(Y2);
  134. #endif
  135. #if ENABLED(Z_IS_TMC2130)
  136. _TMC2130_DEFINE(Z);
  137. #endif
  138. #if ENABLED(Z2_IS_TMC2130)
  139. _TMC2130_DEFINE(Z2);
  140. #endif
  141. #if ENABLED(E0_IS_TMC2130)
  142. _TMC2130_DEFINE(E0);
  143. #endif
  144. #if ENABLED(E1_IS_TMC2130)
  145. _TMC2130_DEFINE(E1);
  146. #endif
  147. #if ENABLED(E2_IS_TMC2130)
  148. _TMC2130_DEFINE(E2);
  149. #endif
  150. #if ENABLED(E3_IS_TMC2130)
  151. _TMC2130_DEFINE(E3);
  152. #endif
  153. #if ENABLED(E4_IS_TMC2130)
  154. _TMC2130_DEFINE(E4);
  155. #endif
  156. // Use internal reference voltage for current calculations. This is the default.
  157. // Following values from Trinamic's spreadsheet with values for a NEMA17 (42BYGHW609)
  158. // https://www.trinamic.com/products/integrated-circuits/details/tmc2130/
  159. void tmc2130_init(TMC2130Stepper &st, const uint16_t microsteps, const uint32_t thrs, const uint32_t spmm) {
  160. st.begin();
  161. st.setCurrent(st.getCurrent(), R_SENSE, HOLD_MULTIPLIER);
  162. st.microsteps(microsteps);
  163. st.blank_time(36);
  164. st.off_time(5); // Only enables the driver if used with stealthChop
  165. st.interpolate(INTERPOLATE);
  166. st.power_down_delay(128); // ~2s until driver lowers to hold current
  167. st.hysterisis_start(0); // HSTRT = 1
  168. st.hysterisis_low(1); // HEND = -2
  169. st.diag1_active_high(1); // For sensorless homing
  170. #if ENABLED(STEALTHCHOP)
  171. st.stealth_freq(1); // f_pwm = 2/683 f_clk
  172. st.stealth_autoscale(1);
  173. st.stealth_gradient(5);
  174. st.stealth_amplitude(255);
  175. st.stealthChop(1);
  176. #if ENABLED(HYBRID_THRESHOLD)
  177. st.stealth_max_speed(12650000UL*st.microsteps()/(256*thrs*spmm));
  178. #endif
  179. #elif ENABLED(SENSORLESS_HOMING)
  180. st.coolstep_min_speed(1024UL * 1024UL - 1UL);
  181. #endif
  182. }
  183. #define _TMC2130_INIT(ST, SPMM) tmc2130_init(stepper##ST, ST##_MICROSTEPS, ST##_HYBRID_THRESHOLD, SPMM)
  184. void tmc2130_init() {
  185. constexpr uint16_t steps_per_mm[] = DEFAULT_AXIS_STEPS_PER_UNIT;
  186. #if ENABLED(X_IS_TMC2130)
  187. _TMC2130_INIT( X, steps_per_mm[X_AXIS]);
  188. #if ENABLED(SENSORLESS_HOMING)
  189. stepperX.sg_stall_value(X_HOMING_SENSITIVITY);
  190. #endif
  191. #endif
  192. #if ENABLED(X2_IS_TMC2130)
  193. _TMC2130_INIT(X2, steps_per_mm[X_AXIS]);
  194. #endif
  195. #if ENABLED(Y_IS_TMC2130)
  196. _TMC2130_INIT( Y, steps_per_mm[Y_AXIS]);
  197. #if ENABLED(SENSORLESS_HOMING)
  198. stepperY.sg_stall_value(Y_HOMING_SENSITIVITY);
  199. #endif
  200. #endif
  201. #if ENABLED(Y2_IS_TMC2130)
  202. _TMC2130_INIT(Y2, steps_per_mm[Y_AXIS]);
  203. #endif
  204. #if ENABLED(Z_IS_TMC2130)
  205. _TMC2130_INIT( Z, steps_per_mm[Z_AXIS]);
  206. #endif
  207. #if ENABLED(Z2_IS_TMC2130)
  208. _TMC2130_INIT(Z2, steps_per_mm[Z_AXIS]);
  209. #endif
  210. #if ENABLED(E0_IS_TMC2130)
  211. _TMC2130_INIT(E0, steps_per_mm[E_AXIS]);
  212. #endif
  213. #if ENABLED(E1_IS_TMC2130)
  214. { constexpr int extruder = 1; _TMC2130_INIT(E1, steps_per_mm[E_AXIS_N]); }
  215. #endif
  216. #if ENABLED(E2_IS_TMC2130)
  217. { constexpr int extruder = 2; _TMC2130_INIT(E2, steps_per_mm[E_AXIS_N]); }
  218. #endif
  219. #if ENABLED(E3_IS_TMC2130)
  220. { constexpr int extruder = 3; _TMC2130_INIT(E3, steps_per_mm[E_AXIS_N]); }
  221. #endif
  222. #if ENABLED(E4_IS_TMC2130)
  223. { constexpr int extruder = 4; _TMC2130_INIT(E4, steps_per_mm[E_AXIS_N]); }
  224. #endif
  225. TMC2130_ADV()
  226. }
  227. #endif // HAVE_TMC2130
  228. //
  229. // L6470 Driver objects and inits
  230. //
  231. #if ENABLED(HAVE_L6470DRIVER)
  232. #include <SPI.h>
  233. #include <L6470.h>
  234. #define _L6470_DEFINE(ST) L6470 stepper##ST(ST##_ENABLE_PIN)
  235. // L6470 Stepper objects
  236. #if ENABLED(X_IS_L6470)
  237. _L6470_DEFINE(X);
  238. #endif
  239. #if ENABLED(X2_IS_L6470)
  240. _L6470_DEFINE(X2);
  241. #endif
  242. #if ENABLED(Y_IS_L6470)
  243. _L6470_DEFINE(Y);
  244. #endif
  245. #if ENABLED(Y2_IS_L6470)
  246. _L6470_DEFINE(Y2);
  247. #endif
  248. #if ENABLED(Z_IS_L6470)
  249. _L6470_DEFINE(Z);
  250. #endif
  251. #if ENABLED(Z2_IS_L6470)
  252. _L6470_DEFINE(Z2);
  253. #endif
  254. #if ENABLED(E0_IS_L6470)
  255. _L6470_DEFINE(E0);
  256. #endif
  257. #if ENABLED(E1_IS_L6470)
  258. _L6470_DEFINE(E1);
  259. #endif
  260. #if ENABLED(E2_IS_L6470)
  261. _L6470_DEFINE(E2);
  262. #endif
  263. #if ENABLED(E3_IS_L6470)
  264. _L6470_DEFINE(E3);
  265. #endif
  266. #if ENABLED(E4_IS_L6470)
  267. _L6470_DEFINE(E4);
  268. #endif
  269. #define _L6470_INIT(A) do{ \
  270. stepper##A.init(A##_K_VAL); \
  271. stepper##A.softFree(); \
  272. stepper##A.setMicroSteps(A##_MICROSTEPS); \
  273. stepper##A.setOverCurrent(A##_OVERCURRENT); \
  274. stepper##A.setStallCurrent(A##_STALLCURRENT); \
  275. } while(0)
  276. void L6470_init() {
  277. #if ENABLED(X_IS_L6470)
  278. _L6470_INIT(X);
  279. #endif
  280. #if ENABLED(X2_IS_L6470)
  281. _L6470_INIT(X2);
  282. #endif
  283. #if ENABLED(Y_IS_L6470)
  284. _L6470_INIT(Y);
  285. #endif
  286. #if ENABLED(Y2_IS_L6470)
  287. _L6470_INIT(Y2);
  288. #endif
  289. #if ENABLED(Z_IS_L6470)
  290. _L6470_INIT(Z);
  291. #endif
  292. #if ENABLED(Z2_IS_L6470)
  293. _L6470_INIT(Z2);
  294. #endif
  295. #if ENABLED(E0_IS_L6470)
  296. _L6470_INIT(E0);
  297. #endif
  298. #if ENABLED(E1_IS_L6470)
  299. _L6470_INIT(E1);
  300. #endif
  301. #if ENABLED(E2_IS_L6470)
  302. _L6470_INIT(E2);
  303. #endif
  304. #if ENABLED(E3_IS_L6470)
  305. _L6470_INIT(E3);
  306. #endif
  307. #if ENABLED(E4_IS_L6470)
  308. _L6470_INIT(E4);
  309. #endif
  310. }
  311. #endif // HAVE_L6470DRIVER