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.

tmc_util.h 14KB

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