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.

stepper_indirection.cpp 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #define _TMC2130_DEFINE(ST) TMC2130Stepper stepper##ST(ST##_ENABLE_PIN, ST##_DIR_PIN, ST##_STEP_PIN, ST##_CHIP_SELECT)
  121. // Stepper objects of TMC2130 steppers used
  122. #if ENABLED(X_IS_TMC2130)
  123. _TMC2130_DEFINE(X);
  124. #endif
  125. #if ENABLED(X2_IS_TMC2130)
  126. _TMC2130_DEFINE(X2);
  127. #endif
  128. #if ENABLED(Y_IS_TMC2130)
  129. _TMC2130_DEFINE(Y);
  130. #endif
  131. #if ENABLED(Y2_IS_TMC2130)
  132. _TMC2130_DEFINE(Y2);
  133. #endif
  134. #if ENABLED(Z_IS_TMC2130)
  135. _TMC2130_DEFINE(Z);
  136. #endif
  137. #if ENABLED(Z2_IS_TMC2130)
  138. _TMC2130_DEFINE(Z2);
  139. #endif
  140. #if ENABLED(E0_IS_TMC2130)
  141. _TMC2130_DEFINE(E0);
  142. #endif
  143. #if ENABLED(E1_IS_TMC2130)
  144. _TMC2130_DEFINE(E1);
  145. #endif
  146. #if ENABLED(E2_IS_TMC2130)
  147. _TMC2130_DEFINE(E2);
  148. #endif
  149. #if ENABLED(E3_IS_TMC2130)
  150. _TMC2130_DEFINE(E3);
  151. #endif
  152. #if ENABLED(E4_IS_TMC2130)
  153. _TMC2130_DEFINE(E4);
  154. #endif
  155. // Use internal reference voltage for current calculations. This is the default.
  156. // Following values from Trinamic's spreadsheet with values for a NEMA17 (42BYGHW609)
  157. void tmc2130_init(TMC2130Stepper &st, const uint16_t max_current, const uint16_t microsteps) {
  158. st.begin();
  159. st.setCurrent(st.getCurrent(), R_SENSE, HOLD_MULTIPLIER);
  160. st.microsteps(microsteps);
  161. st.blank_time(24);
  162. st.off_time(8);
  163. st.interpolate(INTERPOLATE);
  164. #if ENABLED(STEALTHCHOP)
  165. st.stealthChop(1);
  166. #endif
  167. #if ENABLED(SENSORLESS_HOMING)
  168. st.coolstep_min_speed(1048575);
  169. st.sg_stall_value(STALL_THRESHOLD);
  170. st.sg_filter(1);
  171. st.diag1_stall(1);
  172. st.diag1_active_high(1);
  173. #endif
  174. }
  175. #define _TMC2130_INIT(ST) tmc2130_init(stepper##ST, ST##_MAX_CURRENT, ST##_MICROSTEPS)
  176. void tmc2130_init() {
  177. delay(500); // Let power stabilize before configuring the steppers
  178. #if ENABLED(X_IS_TMC2130)
  179. _TMC2130_INIT(X);
  180. #endif
  181. #if ENABLED(X2_IS_TMC2130)
  182. _TMC2130_INIT(X2);
  183. #endif
  184. #if ENABLED(Y_IS_TMC2130)
  185. _TMC2130_INIT(Y);
  186. #endif
  187. #if ENABLED(Y2_IS_TMC2130)
  188. _TMC2130_INIT(Y2);
  189. #endif
  190. #if ENABLED(Z_IS_TMC2130)
  191. _TMC2130_INIT(Z);
  192. #endif
  193. #if ENABLED(Z2_IS_TMC2130)
  194. _TMC2130_INIT(Z2);
  195. #endif
  196. #if ENABLED(E0_IS_TMC2130)
  197. _TMC2130_INIT(E0);
  198. #endif
  199. #if ENABLED(E1_IS_TMC2130)
  200. _TMC2130_INIT(E1);
  201. #endif
  202. #if ENABLED(E2_IS_TMC2130)
  203. _TMC2130_INIT(E2);
  204. #endif
  205. #if ENABLED(E3_IS_TMC2130)
  206. _TMC2130_INIT(E3);
  207. #endif
  208. #if ENABLED(E4_IS_TMC2130)
  209. _TMC2130_INIT(E4);
  210. #endif
  211. TMC2130_ADV()
  212. }
  213. #endif // HAVE_TMC2130
  214. //
  215. // L6470 Driver objects and inits
  216. //
  217. #if ENABLED(HAVE_L6470DRIVER)
  218. #include <SPI.h>
  219. #include <L6470.h>
  220. #define _L6470_DEFINE(ST) L6470 stepper##ST(ST##_ENABLE_PIN)
  221. // L6470 Stepper objects
  222. #if ENABLED(X_IS_L6470)
  223. _L6470_DEFINE(X);
  224. #endif
  225. #if ENABLED(X2_IS_L6470)
  226. _L6470_DEFINE(X2);
  227. #endif
  228. #if ENABLED(Y_IS_L6470)
  229. _L6470_DEFINE(Y);
  230. #endif
  231. #if ENABLED(Y2_IS_L6470)
  232. _L6470_DEFINE(Y2);
  233. #endif
  234. #if ENABLED(Z_IS_L6470)
  235. _L6470_DEFINE(Z);
  236. #endif
  237. #if ENABLED(Z2_IS_L6470)
  238. _L6470_DEFINE(Z2);
  239. #endif
  240. #if ENABLED(E0_IS_L6470)
  241. _L6470_DEFINE(E0);
  242. #endif
  243. #if ENABLED(E1_IS_L6470)
  244. _L6470_DEFINE(E1);
  245. #endif
  246. #if ENABLED(E2_IS_L6470)
  247. _L6470_DEFINE(E2);
  248. #endif
  249. #if ENABLED(E3_IS_L6470)
  250. _L6470_DEFINE(E3);
  251. #endif
  252. #if ENABLED(E4_IS_L6470)
  253. _L6470_DEFINE(E4);
  254. #endif
  255. #define _L6470_INIT(A) do{ \
  256. stepper##A.init(A##_K_VAL); \
  257. stepper##A.softFree(); \
  258. stepper##A.setMicroSteps(A##_MICROSTEPS); \
  259. stepper##A.setOverCurrent(A##_OVERCURRENT); \
  260. stepper##A.setStallCurrent(A##_STALLCURRENT); \
  261. } while(0)
  262. void L6470_init() {
  263. #if ENABLED(X_IS_L6470)
  264. _L6470_INIT(X);
  265. #endif
  266. #if ENABLED(X2_IS_L6470)
  267. _L6470_INIT(X2);
  268. #endif
  269. #if ENABLED(Y_IS_L6470)
  270. _L6470_INIT(Y);
  271. #endif
  272. #if ENABLED(Y2_IS_L6470)
  273. _L6470_INIT(Y2);
  274. #endif
  275. #if ENABLED(Z_IS_L6470)
  276. _L6470_INIT(Z);
  277. #endif
  278. #if ENABLED(Z2_IS_L6470)
  279. _L6470_INIT(Z2);
  280. #endif
  281. #if ENABLED(E0_IS_L6470)
  282. _L6470_INIT(E0);
  283. #endif
  284. #if ENABLED(E1_IS_L6470)
  285. _L6470_INIT(E1);
  286. #endif
  287. #if ENABLED(E2_IS_L6470)
  288. _L6470_INIT(E2);
  289. #endif
  290. #if ENABLED(E3_IS_L6470)
  291. _L6470_INIT(E3);
  292. #endif
  293. #if ENABLED(E4_IS_L6470)
  294. _L6470_INIT(E4);
  295. #endif
  296. }
  297. #endif // HAVE_L6470DRIVER