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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. #pragma once
  23. #include "../inc/MarlinConfig.h"
  24. #include "../lcd/marlinui.h"
  25. #if HAS_TRINAMIC_CONFIG
  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. #define CHOPPER_09STEP_24V { 3, -1, 5 }
  35. #if ENABLED(MONITOR_DRIVER_STATUS) && !defined(MONITOR_DRIVER_STATUS_INTERVAL_MS)
  36. #define MONITOR_DRIVER_STATUS_INTERVAL_MS 500U
  37. #endif
  38. constexpr uint16_t _tmc_thrs(const uint16_t msteps, const uint32_t thrs, const uint32_t spmm) {
  39. return 12650000UL * msteps / (256 * thrs * spmm);
  40. }
  41. typedef struct {
  42. uint8_t toff;
  43. int8_t hend;
  44. uint8_t hstrt;
  45. } chopper_timing_t;
  46. template<char AXIS_LETTER, char DRIVER_ID>
  47. class TMCStorage {
  48. protected:
  49. // Only a child class has access to constructor => Don't create on its own! "Poor man's abstract class"
  50. TMCStorage() {}
  51. public:
  52. uint16_t val_mA = 0;
  53. #if ENABLED(MONITOR_DRIVER_STATUS)
  54. uint8_t otpw_count = 0,
  55. error_count = 0;
  56. bool flag_otpw = false;
  57. bool getOTPW() { return flag_otpw; }
  58. void clear_otpw() { flag_otpw = 0; }
  59. #endif
  60. uint16_t getMilliamps() { return val_mA; }
  61. void printLabel() {
  62. SERIAL_CHAR(AXIS_LETTER);
  63. if (DRIVER_ID > '0') SERIAL_CHAR(DRIVER_ID);
  64. }
  65. struct {
  66. OPTCODE(HAS_STEALTHCHOP, bool stealthChop_enabled = false)
  67. OPTCODE(HYBRID_THRESHOLD, uint8_t hybrid_thrs = 0)
  68. OPTCODE(USE_SENSORLESS, int16_t homing_thrs = 0)
  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. uint16_t rms_current() { return TMC::rms_current(); }
  87. void rms_current(uint16_t mA) {
  88. this->val_mA = mA;
  89. TMC::rms_current(mA);
  90. }
  91. void rms_current(const uint16_t mA, const float mult) {
  92. this->val_mA = mA;
  93. TMC::rms_current(mA, mult);
  94. }
  95. uint16_t get_microstep_counter() { return TMC::MSCNT(); }
  96. #if HAS_STEALTHCHOP
  97. bool get_stealthChop() { return this->en_pwm_mode(); }
  98. bool get_stored_stealthChop() { return this->stored.stealthChop_enabled; }
  99. void refresh_stepping_mode() { this->en_pwm_mode(this->stored.stealthChop_enabled); }
  100. void set_stealthChop(const bool stch) { this->stored.stealthChop_enabled = stch; refresh_stepping_mode(); }
  101. bool toggle_stepping_mode() { set_stealthChop(!this->stored.stealthChop_enabled); return get_stealthChop(); }
  102. #endif
  103. void set_chopper_times(const chopper_timing_t &ct) {
  104. TMC::toff(ct.toff);
  105. TMC::hysteresis_end(ct.hend);
  106. TMC::hysteresis_start(ct.hstrt);
  107. }
  108. #if ENABLED(HYBRID_THRESHOLD)
  109. uint32_t get_pwm_thrs() {
  110. return _tmc_thrs(this->microsteps(), this->TPWMTHRS(), planner.settings.axis_steps_per_mm[AXIS_ID]);
  111. }
  112. void set_pwm_thrs(const uint32_t thrs) {
  113. TMC::TPWMTHRS(_tmc_thrs(this->microsteps(), thrs, planner.settings.axis_steps_per_mm[AXIS_ID]));
  114. TERN_(HAS_MARLINUI_MENU, this->stored.hybrid_thrs = thrs);
  115. }
  116. #endif
  117. #if USE_SENSORLESS
  118. 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. TERN_(HAS_MARLINUI_MENU, this->stored.homing_thrs = sgt_val);
  123. }
  124. #if ENABLED(SPI_ENDSTOPS)
  125. bool test_stall_status();
  126. #endif
  127. #endif
  128. #if HAS_MARLINUI_MENU
  129. void refresh_stepper_current() { rms_current(this->val_mA); }
  130. #if ENABLED(HYBRID_THRESHOLD)
  131. void refresh_hybrid_thrs() { set_pwm_thrs(this->stored.hybrid_thrs); }
  132. #endif
  133. #if USE_SENSORLESS
  134. void refresh_homing_thrs() { homing_threshold(this->stored.homing_thrs); }
  135. #endif
  136. #endif
  137. static constexpr int8_t sgt_min = -64,
  138. sgt_max = 63;
  139. };
  140. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  141. class TMCMarlin<TMC2208Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> : public TMC2208Stepper, public TMCStorage<AXIS_LETTER, DRIVER_ID> {
  142. public:
  143. TMCMarlin(Stream * SerialPort, const float RS, const uint8_t) :
  144. TMC2208Stepper(SerialPort, RS)
  145. {}
  146. TMCMarlin(Stream * SerialPort, const float RS, uint8_t addr, const uint16_t mul_pin1, const uint16_t mul_pin2) :
  147. TMC2208Stepper(SerialPort, RS, addr, mul_pin1, mul_pin2)
  148. {}
  149. TMCMarlin(const uint16_t RX, const uint16_t TX, const float RS, const uint8_t) :
  150. TMC2208Stepper(RX, TX, RS)
  151. {}
  152. uint16_t rms_current() { return TMC2208Stepper::rms_current(); }
  153. void rms_current(const uint16_t mA) {
  154. this->val_mA = mA;
  155. TMC2208Stepper::rms_current(mA);
  156. }
  157. void rms_current(const uint16_t mA, const float mult) {
  158. this->val_mA = mA;
  159. TMC2208Stepper::rms_current(mA, mult);
  160. }
  161. uint16_t get_microstep_counter() { return TMC2208Stepper::MSCNT(); }
  162. #if HAS_STEALTHCHOP
  163. bool get_stealthChop() { return !this->en_spreadCycle(); }
  164. bool get_stored_stealthChop() { return this->stored.stealthChop_enabled; }
  165. void refresh_stepping_mode() { this->en_spreadCycle(!this->stored.stealthChop_enabled); }
  166. void set_stealthChop(const bool stch) { this->stored.stealthChop_enabled = stch; refresh_stepping_mode(); }
  167. bool toggle_stepping_mode() { set_stealthChop(!this->stored.stealthChop_enabled); return get_stealthChop(); }
  168. #endif
  169. void set_chopper_times(const chopper_timing_t &ct) {
  170. TMC2208Stepper::toff(ct.toff);
  171. TMC2208Stepper::hysteresis_end(ct.hend);
  172. TMC2208Stepper::hysteresis_start(ct.hstrt);
  173. }
  174. #if ENABLED(HYBRID_THRESHOLD)
  175. uint32_t get_pwm_thrs() {
  176. return _tmc_thrs(this->microsteps(), this->TPWMTHRS(), planner.settings.axis_steps_per_mm[AXIS_ID]);
  177. }
  178. void set_pwm_thrs(const uint32_t thrs) {
  179. TMC2208Stepper::TPWMTHRS(_tmc_thrs(this->microsteps(), thrs, planner.settings.axis_steps_per_mm[AXIS_ID]));
  180. TERN_(HAS_MARLINUI_MENU, this->stored.hybrid_thrs = thrs);
  181. }
  182. #endif
  183. #if HAS_MARLINUI_MENU
  184. void refresh_stepper_current() { rms_current(this->val_mA); }
  185. #if ENABLED(HYBRID_THRESHOLD)
  186. void refresh_hybrid_thrs() { set_pwm_thrs(this->stored.hybrid_thrs); }
  187. #endif
  188. #endif
  189. };
  190. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  191. class TMCMarlin<TMC2209Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> : public TMC2209Stepper, public TMCStorage<AXIS_LETTER, DRIVER_ID> {
  192. public:
  193. TMCMarlin(Stream * SerialPort, const float RS, const uint8_t addr) :
  194. TMC2209Stepper(SerialPort, RS, addr)
  195. {}
  196. TMCMarlin(const uint16_t RX, const uint16_t TX, const float RS, const uint8_t addr) :
  197. TMC2209Stepper(RX, TX, RS, addr)
  198. {}
  199. uint8_t get_address() { return slave_address; }
  200. uint16_t rms_current() { return TMC2209Stepper::rms_current(); }
  201. void rms_current(const uint16_t mA) {
  202. this->val_mA = mA;
  203. TMC2209Stepper::rms_current(mA);
  204. }
  205. void rms_current(const uint16_t mA, const float mult) {
  206. this->val_mA = mA;
  207. TMC2209Stepper::rms_current(mA, mult);
  208. }
  209. uint16_t get_microstep_counter() { return TMC2209Stepper::MSCNT(); }
  210. #if HAS_STEALTHCHOP
  211. bool get_stealthChop() { return !this->en_spreadCycle(); }
  212. bool get_stored_stealthChop() { return this->stored.stealthChop_enabled; }
  213. void refresh_stepping_mode() { this->en_spreadCycle(!this->stored.stealthChop_enabled); }
  214. void set_stealthChop(const bool stch) { this->stored.stealthChop_enabled = stch; refresh_stepping_mode(); }
  215. bool toggle_stepping_mode() { set_stealthChop(!this->stored.stealthChop_enabled); return get_stealthChop(); }
  216. #endif
  217. void set_chopper_times(const chopper_timing_t &ct) {
  218. TMC2209Stepper::toff(ct.toff);
  219. TMC2209Stepper::hysteresis_end(ct.hend);
  220. TMC2209Stepper::hysteresis_start(ct.hstrt);
  221. }
  222. #if ENABLED(HYBRID_THRESHOLD)
  223. uint32_t get_pwm_thrs() {
  224. return _tmc_thrs(this->microsteps(), this->TPWMTHRS(), planner.settings.axis_steps_per_mm[AXIS_ID]);
  225. }
  226. void set_pwm_thrs(const uint32_t thrs) {
  227. TMC2209Stepper::TPWMTHRS(_tmc_thrs(this->microsteps(), thrs, planner.settings.axis_steps_per_mm[AXIS_ID]));
  228. TERN_(HAS_MARLINUI_MENU, this->stored.hybrid_thrs = thrs);
  229. }
  230. #endif
  231. #if USE_SENSORLESS
  232. int16_t homing_threshold() { return TMC2209Stepper::SGTHRS(); }
  233. void homing_threshold(int16_t sgt_val) {
  234. sgt_val = (int16_t)constrain(sgt_val, sgt_min, sgt_max);
  235. TMC2209Stepper::SGTHRS(sgt_val);
  236. TERN_(HAS_MARLINUI_MENU, this->stored.homing_thrs = sgt_val);
  237. }
  238. #endif
  239. #if HAS_MARLINUI_MENU
  240. void refresh_stepper_current() { rms_current(this->val_mA); }
  241. #if ENABLED(HYBRID_THRESHOLD)
  242. void refresh_hybrid_thrs() { set_pwm_thrs(this->stored.hybrid_thrs); }
  243. #endif
  244. #if USE_SENSORLESS
  245. void refresh_homing_thrs() { homing_threshold(this->stored.homing_thrs); }
  246. #endif
  247. #endif
  248. static constexpr uint8_t sgt_min = 0,
  249. sgt_max = 255;
  250. };
  251. template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  252. class TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> : public TMC2660Stepper, public TMCStorage<AXIS_LETTER, DRIVER_ID> {
  253. public:
  254. TMCMarlin(const uint16_t cs_pin, const float RS, const uint8_t) :
  255. TMC2660Stepper(cs_pin, RS)
  256. {}
  257. TMCMarlin(const uint16_t CS, const float RS, const uint16_t pinMOSI, const uint16_t pinMISO, const uint16_t pinSCK, const uint8_t) :
  258. TMC2660Stepper(CS, RS, pinMOSI, pinMISO, pinSCK)
  259. {}
  260. uint16_t rms_current() { return TMC2660Stepper::rms_current(); }
  261. void rms_current(const uint16_t mA) {
  262. this->val_mA = mA;
  263. TMC2660Stepper::rms_current(mA);
  264. }
  265. uint16_t get_microstep_counter() { return TMC2660Stepper::mstep(); }
  266. void set_chopper_times(const chopper_timing_t &ct) {
  267. TMC2660Stepper::toff(ct.toff);
  268. TMC2660Stepper::hysteresis_end(ct.hend);
  269. TMC2660Stepper::hysteresis_start(ct.hstrt);
  270. }
  271. #if USE_SENSORLESS
  272. int16_t homing_threshold() { return TMC2660Stepper::sgt(); }
  273. void homing_threshold(int16_t sgt_val) {
  274. sgt_val = (int16_t)constrain(sgt_val, sgt_min, sgt_max);
  275. TMC2660Stepper::sgt(sgt_val);
  276. TERN_(HAS_MARLINUI_MENU, this->stored.homing_thrs = sgt_val);
  277. }
  278. #endif
  279. #if HAS_MARLINUI_MENU
  280. void refresh_stepper_current() { rms_current(this->val_mA); }
  281. #if USE_SENSORLESS
  282. void refresh_homing_thrs() { homing_threshold(this->stored.homing_thrs); }
  283. #endif
  284. #endif
  285. static constexpr int8_t sgt_min = -64,
  286. sgt_max = 63;
  287. };
  288. void monitor_tmc_drivers();
  289. void test_tmc_connection(LOGICAL_AXIS_DECL(const bool, true));
  290. #if ENABLED(TMC_DEBUG)
  291. #if ENABLED(MONITOR_DRIVER_STATUS)
  292. void tmc_set_report_interval(const uint16_t update_interval);
  293. #endif
  294. void tmc_report_all(LOGICAL_AXIS_DECL(const bool, true));
  295. void tmc_get_registers(LOGICAL_AXIS_ARGS(const bool));
  296. #endif
  297. /**
  298. * TMC2130-specific sensorless homing using stallGuard2.
  299. * stallGuard2 only works when in spreadCycle mode.
  300. * spreadCycle and stealthChop are mutually-exclusive.
  301. *
  302. * Defined here because of limitations with templates and headers.
  303. */
  304. #if USE_SENSORLESS
  305. // Track enabled status of stealthChop and only re-enable where applicable
  306. struct sensorless_t { bool NUM_AXIS_ARGS(), x2, y2, z2, z3, z4; };
  307. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  308. extern millis_t sg_guard_period;
  309. constexpr uint16_t default_sg_guard_duration = 400;
  310. #endif
  311. bool tmc_enable_stallguard(TMC2130Stepper &st);
  312. void tmc_disable_stallguard(TMC2130Stepper &st, const bool restore_stealth);
  313. bool tmc_enable_stallguard(TMC2209Stepper &st);
  314. void tmc_disable_stallguard(TMC2209Stepper &st, const bool restore_stealth);
  315. bool tmc_enable_stallguard(TMC2660Stepper);
  316. void tmc_disable_stallguard(TMC2660Stepper, const bool);
  317. #if ENABLED(SPI_ENDSTOPS)
  318. template<class TMC, char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
  319. bool TMCMarlin<TMC, AXIS_LETTER, DRIVER_ID, AXIS_ID>::test_stall_status() {
  320. this->switchCSpin(LOW);
  321. // read stallGuard flag from TMC library, will handle HW and SW SPI
  322. TMC2130_n::DRV_STATUS_t drv_status{0};
  323. drv_status.sr = this->DRV_STATUS();
  324. this->switchCSpin(HIGH);
  325. return drv_status.stallGuard;
  326. }
  327. #endif // SPI_ENDSTOPS
  328. #endif // USE_SENSORLESS
  329. #endif // HAS_TRINAMIC_CONFIG
  330. #if HAS_TMC_SPI
  331. void tmc_init_cs_pins();
  332. #endif