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.

tmc2130.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #include "../inc/MarlinConfig.h"
  23. #if ENABLED(HAVE_TMC2130)
  24. #include "tmc2130.h"
  25. #include "../Marlin.h"
  26. #include "../module/stepper_indirection.h"
  27. #include "../module/printcounter.h"
  28. #include "../libs/duration_t.h"
  29. #ifdef AUTOMATIC_CURRENT_CONTROL
  30. bool auto_current_control = 0;
  31. #endif
  32. void automatic_current_control(TMC2130Stepper &st, String axisID) {
  33. // Check otpw even if we don't use automatic control. Allows for flag inspection.
  34. const bool is_otpw = st.checkOT();
  35. // Report if a warning was triggered
  36. static bool previous_otpw = false;
  37. if (is_otpw && !previous_otpw) {
  38. char timestamp[10];
  39. duration_t elapsed = print_job_timer.duration();
  40. const bool has_days = (elapsed.value > 60*60*24L);
  41. (void)elapsed.toDigital(timestamp, has_days);
  42. SERIAL_ECHO(timestamp);
  43. SERIAL_ECHOPGM(": ");
  44. SERIAL_ECHO(axisID);
  45. SERIAL_ECHOLNPGM(" driver overtemperature warning!");
  46. }
  47. previous_otpw = is_otpw;
  48. #if ENABLED(AUTOMATIC_CURRENT_CONTROL) && CURRENT_STEP > 0
  49. // Return if user has not enabled current control start with M906 S1.
  50. if (!auto_current_control) return;
  51. /**
  52. * Decrease current if is_otpw is true.
  53. * Bail out if driver is disabled.
  54. * Increase current if OTPW has not been triggered yet.
  55. */
  56. uint16_t current = st.getCurrent();
  57. if (is_otpw) {
  58. st.setCurrent(current - CURRENT_STEP, R_SENSE, HOLD_MULTIPLIER);
  59. #if ENABLED(REPORT_CURRENT_CHANGE)
  60. SERIAL_ECHO(axisID);
  61. SERIAL_ECHOPAIR(" current decreased to ", st.getCurrent());
  62. #endif
  63. }
  64. else if (!st.isEnabled())
  65. return;
  66. else if (!is_otpw && !st.getOTPW()) {
  67. current += CURRENT_STEP;
  68. if (current <= AUTO_ADJUST_MAX) {
  69. st.setCurrent(current, R_SENSE, HOLD_MULTIPLIER);
  70. #if ENABLED(REPORT_CURRENT_CHANGE)
  71. SERIAL_ECHO(axisID);
  72. SERIAL_ECHOPAIR(" current increased to ", st.getCurrent());
  73. #endif
  74. }
  75. }
  76. SERIAL_EOL();
  77. #endif
  78. }
  79. void tmc2130_checkOverTemp(void) {
  80. static millis_t next_cOT = 0;
  81. if (ELAPSED(millis(), next_cOT)) {
  82. next_cOT = millis() + 5000;
  83. #if ENABLED(X_IS_TMC2130)
  84. automatic_current_control(stepperX, "X");
  85. #endif
  86. #if ENABLED(Y_IS_TMC2130)
  87. automatic_current_control(stepperY, "Y");
  88. #endif
  89. #if ENABLED(Z_IS_TMC2130)
  90. automatic_current_control(stepperZ, "Z");
  91. #endif
  92. #if ENABLED(X2_IS_TMC2130)
  93. automatic_current_control(stepperX2, "X2");
  94. #endif
  95. #if ENABLED(Y2_IS_TMC2130)
  96. automatic_current_control(stepperY2, "Y2");
  97. #endif
  98. #if ENABLED(Z2_IS_TMC2130)
  99. automatic_current_control(stepperZ2, "Z2");
  100. #endif
  101. #if ENABLED(E0_IS_TMC2130)
  102. automatic_current_control(stepperE0, "E0");
  103. #endif
  104. #if ENABLED(E1_IS_TMC2130)
  105. automatic_current_control(stepperE1, "E1");
  106. #endif
  107. #if ENABLED(E2_IS_TMC2130)
  108. automatic_current_control(stepperE2, "E2");
  109. #endif
  110. #if ENABLED(E3_IS_TMC2130)
  111. automatic_current_control(stepperE3, "E3");
  112. #endif
  113. #if ENABLED(E4_IS_TMC2130)
  114. automatic_current_control(stepperE4, "E4");
  115. #endif
  116. }
  117. }
  118. /**
  119. * TMC2130 specific sensorless homing using stallGuard2.
  120. * stallGuard2 only works when in spreadCycle mode.
  121. * spreadCycle and stealthChop are mutually exclusive.
  122. */
  123. #if ENABLED(SENSORLESS_HOMING)
  124. void tmc2130_sensorless_homing(TMC2130Stepper &st, bool enable/*=true*/) {
  125. #if ENABLED(STEALTHCHOP)
  126. if (enable) {
  127. st.coolstep_min_speed(1024UL * 1024UL - 1UL);
  128. st.stealthChop(0);
  129. }
  130. else {
  131. st.coolstep_min_speed(0);
  132. st.stealthChop(1);
  133. }
  134. #endif
  135. st.diag1_stall(enable ? 1 : 0);
  136. }
  137. #endif // SENSORLESS_HOMING
  138. #endif // HAVE_TMC2130