tmc_util.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifndef _TMC_UTIL_H_
  23. #define _TMC_UTIL_H_
  24. #include "MarlinConfig.h"
  25. #if HAS_DRIVER(TMC2130)
  26. #include <TMC2130Stepper.h>
  27. #endif
  28. #if HAS_DRIVER(TMC2208)
  29. #include <TMC2208Stepper.h>
  30. #endif
  31. extern bool report_tmc_status;
  32. enum TMC_AxisEnum : char { TMC_X, TMC_Y, TMC_Z, TMC_X2, TMC_Y2, TMC_Z2, TMC_E0, TMC_E1, TMC_E2, TMC_E3, TMC_E4 };
  33. constexpr uint32_t _tmc_thrs(const uint16_t msteps, const int32_t thrs, const uint32_t spmm) {
  34. return 12650000UL * msteps / (256 * thrs * spmm);
  35. }
  36. void _tmc_say_axis(const TMC_AxisEnum axis);
  37. void _tmc_say_current(const TMC_AxisEnum axis, const uint16_t curr);
  38. void _tmc_say_otpw(const TMC_AxisEnum axis, const bool otpw);
  39. void _tmc_say_otpw_cleared(const TMC_AxisEnum axis);
  40. void _tmc_say_pwmthrs(const TMC_AxisEnum axis, const uint32_t thrs);
  41. void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt);
  42. template<typename TMC>
  43. void tmc_get_current(TMC &st, const TMC_AxisEnum axis) {
  44. _tmc_say_current(axis, st.getCurrent());
  45. }
  46. template<typename TMC>
  47. void tmc_set_current(TMC &st, const int mA) {
  48. st.setCurrent(mA, R_SENSE, HOLD_MULTIPLIER);
  49. }
  50. template<typename TMC>
  51. void tmc_report_otpw(TMC &st, const TMC_AxisEnum axis) {
  52. _tmc_say_otpw(axis, st.getOTPW());
  53. }
  54. template<typename TMC>
  55. void tmc_clear_otpw(TMC &st, const TMC_AxisEnum axis) {
  56. st.clear_otpw();
  57. _tmc_say_otpw_cleared(axis);
  58. }
  59. template<typename TMC>
  60. void tmc_get_pwmthrs(TMC &st, const TMC_AxisEnum axis, const uint16_t spmm) {
  61. _tmc_say_pwmthrs(axis, _tmc_thrs(st.microsteps(), st.TPWMTHRS(), spmm));
  62. }
  63. template<typename TMC>
  64. void tmc_set_pwmthrs(TMC &st, const int32_t thrs, const uint32_t spmm) {
  65. st.TPWMTHRS(_tmc_thrs(st.microsteps(), thrs, spmm));
  66. }
  67. template<typename TMC>
  68. void tmc_get_sgt(TMC &st, const TMC_AxisEnum axis) {
  69. _tmc_say_sgt(axis, st.sgt());
  70. }
  71. template<typename TMC>
  72. void tmc_set_sgt(TMC &st, const int8_t sgt_val) {
  73. st.sgt(sgt_val);
  74. }
  75. void monitor_tmc_driver();
  76. #if ENABLED(TMC_DEBUG)
  77. void tmc_set_report_status(const bool status);
  78. void tmc_report_all();
  79. #endif
  80. /**
  81. * TMC2130 specific sensorless homing using stallGuard2.
  82. * stallGuard2 only works when in spreadCycle mode.
  83. * spreadCycle and stealthChop are mutually exclusive.
  84. *
  85. * Defined here because of limitations with templates and headers.
  86. */
  87. #if ENABLED(SENSORLESS_HOMING)
  88. void tmc_sensorless_homing(TMC2130Stepper &st, const bool enable=true);
  89. #endif
  90. #if HAS_DRIVER(TMC2130)
  91. void tmc_init_cs_pins();
  92. #endif
  93. #endif // _TMC_UTIL_H_