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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. #pragma once
  23. #include "../inc/MarlinConfig.h"
  24. #include "../lcd/ultralcd.h"
  25. #if HAS_TRINAMIC
  26. #include <TMCStepper.h>
  27. #include "../module/planner.h"
  28. #define CHOPPER_DEFAULT_12V { 3, -1, 1 }
  29. #define CHOPPER_DEFAULT_19V { 4, 1, 1 }
  30. #define CHOPPER_DEFAULT_24V { 4, 2, 1 }
  31. #define CHOPPER_DEFAULT_36V { 5, 2, 4 }
  32. #define CHOPPER_PRUSAMK3_24V { 3, -2, 6 }
  33. #define CHOPPER_MARLIN_119 { 5, 2, 3 }
  34. #if ENABLED(MONITOR_DRIVER_STATUS) && !defined(MONITOR_DRIVER_STATUS_INTERVAL_MS)
  35. #define MONITOR_DRIVER_STATUS_INTERVAL_MS 500u
  36. #endif
  37. constexpr uint16_t _tmc_thrs(const uint16_t msteps, const uint32_t thrs, const uint32_t spmm) {
  38. return 12650000UL * msteps / (256 * thrs * spmm);
  39. }
  40. template<char AXIS_LETTER, char DRIVER_ID>
  41. class TMCStorage {
  42. protected:
  43. // Only a child class has access to constructor => Don't create on its own! "Poor man's abstract class"
  44. TMCStorage() {}
  45. public:
  46. uint16_t val_mA = 0;
  47. #if ENABLED(MONITOR_DRIVER_STATUS)
  48. uint8_t otpw_count = 0,
  49. error_count = 0;
  50. bool flag_otpw = false;
  51. inline bool getOTPW() { return flag_otpw; }
  52. inline void clear_otpw() { flag_otpw = 0; }
  53. #endif
  54. inline uint16_t getMilliamps() { return val_mA; }
  55. inline void printLabel() {
  56. SERIAL_CHAR(AXIS_LETTER);
  57. if (DRIVER_ID > '0') SERIAL_CHAR(DRIVER_ID);
  58. }
  59. struct {
  60. #if HAS_STEALTHCHOP
  61. bool stealthChop_enabled = false;
  62. #endif
  63. #if ENABLED(HYBRID_THRESHOLD)
  64. uint8_t hybrid_thrs = 0;
  65. #endif
  66. #if USE_SENSORLESS
  67. int16_t homing_thrs = 0;
  68. #endif
  69. } stored;
  70. };
  71. template<class TMC, char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  72. class TMCMarlin : public TMC, public TMCStorage<AXIS_LETTER, DRIVER_ID> {
  73. public:
  74. TMCMarlin(const uint16_t cs_pin, const float RS) :
  75. TMC(cs_pin, RS)
  76. {}
  77. TMCMarlin(const uint16_t cs_pin, const float RS, const uint8_t axis_chain_index) :
  78. TMC(cs_pin, RS, axis_chain_index)
  79. {}
  80. TMCMarlin(const uint16_t CS, const float RS, const uint16_t pinMOSI, const uint16_t pinMISO, const uint16_t pinSCK) :
  81. TMC(CS, RS, pinMOSI, pinMISO, pinSCK)
  82. {}
  83. TMCMarlin(const uint16_t CS, const float RS, const uint16_t pinMOSI, const uint16_t pinMISO, const uint16_t pinSCK, const uint8_t axis_chain_index) :
  84. TMC(CS, RS, pinMOSI, pinMISO, pinSCK, axis_chain_index)
  85. {}
  86. inline uint16_t rms_current() { return TMC::rms_current(); }
  87. inline void rms_current(uint16_t mA) {
  88. this->val_mA = mA;
  89. TMC::rms_current(mA);
  90. }
  91. inline void rms_current(const uint16_t mA, const float mult) {
  92. this->val_mA = mA;
  93. TMC::rms_current(mA, mult);
  94. }
  95. #if HAS_STEALTHCHOP
  96. inline void refresh_stepping_mode() { this->en_pwm_mode(this->stored.stealthChop_enabled); }
  97. inline bool get_stealthChop_status() { return this->en_pwm_mode(); }
  98. #endif
  99. #if ENABLED(HYBRID_THRESHOLD)
  100. uint32_t get_pwm_thrs() {
  101. return _tmc_thrs(this->microsteps(), this->TPWMTHRS(), planner.settings.axis_steps_per_mm[AXIS_ID]);
  102. }
  103. void set_pwm_thrs(const uint32_t thrs) {
  104. TMC::TPWMTHRS(_tmc_thrs(this->microsteps(), thrs, planner.settings.axis_steps_per_mm[AXIS_ID]));
  105. #if HAS_LCD_MENU
  106. this->stored.hybrid_thrs = thrs;
  107. #endif
  108. }
  109. #endif
  110. #if USE_SENSORLESS
  111. inline int16_t homing_threshold() { return TMC::sgt(); }
  112. void homing_threshold(int16_t sgt_val) {
  113. sgt_val = (int16_t)constrain(sgt_val, sgt_min, sgt_max);
  114. TMC::sgt(sgt_val);
  115. #if HAS_LCD_MENU
  116. this->stored.homing_thrs = sgt_val;
  117. #endif
  118. }
  119. #if ENABLED(SPI_ENDSTOPS)
  120. bool test_stall_status();
  121. #endif
  122. #endif
  123. #if HAS_LCD_MENU
  124. inline void refresh_stepper_current() { rms_current(this->val_mA); }
  125. #if ENABLED(HYBRID_THRESHOLD)
  126. inline void refresh_hybrid_thrs() { set_pwm_thrs(this->stored.hybrid_thrs); }
  127. #endif
  128. #if USE_SENSORLESS
  129. inline void refresh_homing_thrs() { homing_threshold(this->stored.homing_thrs); }
  130. #endif
  131. #endif
  132. static constexpr int8_t sgt_min = -64,
  133. sgt_max = 63;
  134. };
  135. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  136. class TMCMarlin<TMC2208Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> : public TMC2208Stepper, public TMCStorage<AXIS_LETTER, DRIVER_ID> {
  137. public:
  138. TMCMarlin(Stream * SerialPort, const float RS, const uint8_t) :
  139. TMC2208Stepper(SerialPort, RS)
  140. {}
  141. TMCMarlin(const uint16_t RX, const uint16_t TX, const float RS, const uint8_t, const bool has_rx=true) :
  142. TMC2208Stepper(RX, TX, RS, has_rx)
  143. {}
  144. uint16_t rms_current() { return TMC2208Stepper::rms_current(); }
  145. inline void rms_current(const uint16_t mA) {
  146. this->val_mA = mA;
  147. TMC2208Stepper::rms_current(mA);
  148. }
  149. inline void rms_current(const uint16_t mA, const float mult) {
  150. this->val_mA = mA;
  151. TMC2208Stepper::rms_current(mA, mult);
  152. }
  153. #if HAS_STEALTHCHOP
  154. inline void refresh_stepping_mode() { en_spreadCycle(!this->stored.stealthChop_enabled); }
  155. inline bool get_stealthChop_status() { return !this->en_spreadCycle(); }
  156. #endif
  157. #if ENABLED(HYBRID_THRESHOLD)
  158. uint32_t get_pwm_thrs() {
  159. return _tmc_thrs(this->microsteps(), this->TPWMTHRS(), planner.settings.axis_steps_per_mm[AXIS_ID]);
  160. }
  161. void set_pwm_thrs(const uint32_t thrs) {
  162. TMC2208Stepper::TPWMTHRS(_tmc_thrs(this->microsteps(), thrs, planner.settings.axis_steps_per_mm[AXIS_ID]));
  163. #if HAS_LCD_MENU
  164. this->stored.hybrid_thrs = thrs;
  165. #endif
  166. }
  167. #endif
  168. #if HAS_LCD_MENU
  169. inline void refresh_stepper_current() { rms_current(this->val_mA); }
  170. #if ENABLED(HYBRID_THRESHOLD)
  171. inline void refresh_hybrid_thrs() { set_pwm_thrs(this->stored.hybrid_thrs); }
  172. #endif
  173. #endif
  174. };
  175. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  176. class TMCMarlin<TMC2209Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> : public TMC2209Stepper, public TMCStorage<AXIS_LETTER, DRIVER_ID> {
  177. public:
  178. TMCMarlin(Stream * SerialPort, const float RS, const uint8_t addr) :
  179. TMC2209Stepper(SerialPort, RS, addr)
  180. {}
  181. TMCMarlin(const uint16_t RX, const uint16_t TX, const float RS, const uint8_t addr, const bool) :
  182. TMC2209Stepper(RX, TX, RS, addr)
  183. {}
  184. uint8_t get_address() { return slave_address; }
  185. uint16_t rms_current() { return TMC2209Stepper::rms_current(); }
  186. inline void rms_current(const uint16_t mA) {
  187. this->val_mA = mA;
  188. TMC2209Stepper::rms_current(mA);
  189. }
  190. inline void rms_current(const uint16_t mA, const float mult) {
  191. this->val_mA = mA;
  192. TMC2209Stepper::rms_current(mA, mult);
  193. }
  194. #if HAS_STEALTHCHOP
  195. inline void refresh_stepping_mode() { en_spreadCycle(!this->stored.stealthChop_enabled); }
  196. inline bool get_stealthChop_status() { return !this->en_spreadCycle(); }
  197. #endif
  198. #if ENABLED(HYBRID_THRESHOLD)
  199. uint32_t get_pwm_thrs() {
  200. return _tmc_thrs(this->microsteps(), this->TPWMTHRS(), planner.settings.axis_steps_per_mm[AXIS_ID]);
  201. }
  202. void set_pwm_thrs(const uint32_t thrs) {
  203. TMC2209Stepper::TPWMTHRS(_tmc_thrs(this->microsteps(), thrs, planner.settings.axis_steps_per_mm[AXIS_ID]));
  204. #if HAS_LCD_MENU
  205. this->stored.hybrid_thrs = thrs;
  206. #endif
  207. }
  208. #endif
  209. #if USE_SENSORLESS
  210. inline int16_t homing_threshold() { return TMC2209Stepper::SGTHRS(); }
  211. void homing_threshold(int16_t sgt_val) {
  212. sgt_val = (int16_t)constrain(sgt_val, sgt_min, sgt_max);
  213. TMC2209Stepper::SGTHRS(sgt_val);
  214. #if HAS_LCD_MENU
  215. this->stored.homing_thrs = sgt_val;
  216. #endif
  217. }
  218. #endif
  219. #if HAS_LCD_MENU
  220. inline void refresh_stepper_current() { rms_current(this->val_mA); }
  221. #if ENABLED(HYBRID_THRESHOLD)
  222. inline void refresh_hybrid_thrs() { set_pwm_thrs(this->stored.hybrid_thrs); }
  223. #endif
  224. #if USE_SENSORLESS
  225. inline void refresh_homing_thrs() { homing_threshold(this->stored.homing_thrs); }
  226. #endif
  227. #endif
  228. static constexpr uint8_t sgt_min = 0,
  229. sgt_max = 255;
  230. };
  231. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  232. class TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> : public TMC2660Stepper, public TMCStorage<AXIS_LETTER, DRIVER_ID> {
  233. public:
  234. TMCMarlin(const uint16_t cs_pin, const float RS, const uint8_t) :
  235. TMC2660Stepper(cs_pin, RS)
  236. {}
  237. TMCMarlin(const uint16_t CS, const float RS, const uint16_t pinMOSI, const uint16_t pinMISO, const uint16_t pinSCK, const uint8_t) :
  238. TMC2660Stepper(CS, RS, pinMOSI, pinMISO, pinSCK)
  239. {}
  240. inline uint16_t rms_current() { return TMC2660Stepper::rms_current(); }
  241. inline void rms_current(const uint16_t mA) {
  242. this->val_mA = mA;
  243. TMC2660Stepper::rms_current(mA);
  244. }
  245. #if USE_SENSORLESS
  246. inline int16_t homing_threshold() { return TMC2660Stepper::sgt(); }
  247. void homing_threshold(int16_t sgt_val) {
  248. sgt_val = (int16_t)constrain(sgt_val, sgt_min, sgt_max);
  249. TMC2660Stepper::sgt(sgt_val);
  250. #if HAS_LCD_MENU
  251. this->stored.homing_thrs = sgt_val;
  252. #endif
  253. }
  254. #endif
  255. #if HAS_LCD_MENU
  256. inline void refresh_stepper_current() { rms_current(this->val_mA); }
  257. #if USE_SENSORLESS
  258. inline void refresh_homing_thrs() { homing_threshold(this->stored.homing_thrs); }
  259. #endif
  260. #endif
  261. static constexpr int8_t sgt_min = -64,
  262. sgt_max = 63;
  263. };
  264. template<typename TMC>
  265. void tmc_print_current(TMC &st) {
  266. st.printLabel();
  267. SERIAL_ECHOLNPAIR(" driver current: ", st.getMilliamps());
  268. }
  269. #if ENABLED(MONITOR_DRIVER_STATUS)
  270. template<typename TMC>
  271. void tmc_report_otpw(TMC &st) {
  272. st.printLabel();
  273. SERIAL_ECHOPGM(" temperature prewarn triggered: ");
  274. serialprint_truefalse(st.getOTPW());
  275. SERIAL_EOL();
  276. }
  277. template<typename TMC>
  278. void tmc_clear_otpw(TMC &st) {
  279. st.clear_otpw();
  280. st.printLabel();
  281. SERIAL_ECHOLNPGM(" prewarn flag cleared");
  282. }
  283. #endif
  284. #if ENABLED(HYBRID_THRESHOLD)
  285. template<typename TMC>
  286. void tmc_print_pwmthrs(TMC &st) {
  287. st.printLabel();
  288. SERIAL_ECHOLNPAIR(" stealthChop max speed: ", st.get_pwm_thrs());
  289. }
  290. #endif
  291. #if USE_SENSORLESS
  292. template<typename TMC>
  293. void tmc_print_sgt(TMC &st) {
  294. st.printLabel();
  295. SERIAL_ECHOPGM(" homing sensitivity: ");
  296. SERIAL_PRINTLN(st.homing_threshold(), DEC);
  297. }
  298. #endif
  299. void monitor_tmc_drivers();
  300. void test_tmc_connection(const bool test_x, const bool test_y, const bool test_z, const bool test_e);
  301. #if ENABLED(TMC_DEBUG)
  302. #if ENABLED(MONITOR_DRIVER_STATUS)
  303. void tmc_set_report_interval(const uint16_t update_interval);
  304. #endif
  305. void tmc_report_all(const bool print_x, const bool print_y, const bool print_z, const bool print_e);
  306. void tmc_get_registers(const bool print_x, const bool print_y, const bool print_z, const bool print_e);
  307. #endif
  308. /**
  309. * TMC2130-specific sensorless homing using stallGuard2.
  310. * stallGuard2 only works when in spreadCycle mode.
  311. * spreadCycle and stealthChop are mutually-exclusive.
  312. *
  313. * Defined here because of limitations with templates and headers.
  314. */
  315. #if USE_SENSORLESS
  316. // Track enabled status of stealthChop and only re-enable where applicable
  317. struct sensorless_t { bool x, y, z, x2, y2, z2, z3, z4; };
  318. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  319. extern millis_t sg_guard_period;
  320. constexpr uint16_t default_sg_guard_duration = 400;
  321. struct slow_homing_t {
  322. xy_ulong_t acceleration;
  323. #if HAS_CLASSIC_JERK
  324. xy_float_t jerk_xy;
  325. #endif
  326. };
  327. #endif
  328. bool tmc_enable_stallguard(TMC2130Stepper &st);
  329. void tmc_disable_stallguard(TMC2130Stepper &st, const bool restore_stealth);
  330. bool tmc_enable_stallguard(TMC2209Stepper &st);
  331. void tmc_disable_stallguard(TMC2209Stepper &st, const bool restore_stealth);
  332. bool tmc_enable_stallguard(TMC2660Stepper);
  333. void tmc_disable_stallguard(TMC2660Stepper, const bool);
  334. #if ENABLED(SPI_ENDSTOPS)
  335. template<class TMC, char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  336. bool TMCMarlin<TMC, AXIS_LETTER, DRIVER_ID, AXIS_ID>::test_stall_status() {
  337. this->switchCSpin(LOW);
  338. // read stallGuard flag from TMC library, will handle HW and SW SPI
  339. TMC2130_n::DRV_STATUS_t drv_status{0};
  340. drv_status.sr = this->DRV_STATUS();
  341. this->switchCSpin(HIGH);
  342. return drv_status.stallGuard;
  343. }
  344. #endif // SPI_ENDSTOPS
  345. #endif // USE_SENSORLESS
  346. #if TMC_HAS_SPI
  347. void tmc_init_cs_pins();
  348. #endif
  349. #endif // HAS_TRINAMIC