Browse Source

Cleanup, extend TMC2130 implementation

Scott Lahteine 7 years ago
parent
commit
4067d15c92
4 changed files with 275 additions and 86 deletions
  1. 1
    1
      .travis.yml
  2. 226
    51
      Marlin/Marlin_main.cpp
  3. 10
    10
      Marlin/configuration_store.cpp
  4. 38
    24
      Marlin/stepper_indirection.cpp

+ 1
- 1
.travis.yml View File

@@ -409,7 +409,7 @@ script:
409 409
   - restore_configs
410 410
   - opt_enable_adv HAVE_TMC2130 X_IS_TMC2130 Y_IS_TMC2130 Z_IS_TMC2130
411 411
   - build_marlin
412
-  - opt_enable_adv AUTOMATIC_CURRENT_CONTROL STEALTHCHOP
412
+  - opt_enable_adv AUTOMATIC_CURRENT_CONTROL STEALTHCHOP HYBRID_THRESHOLD SENSORLESS_HOMING
413 413
   - build_marlin
414 414
   #
415 415
   # tvrrug Config need to check board type for sanguino atmega644p

+ 226
- 51
Marlin/Marlin_main.cpp View File

@@ -198,6 +198,8 @@
198 198
  * M910 - Commit digipot/DAC value to external EEPROM via I2C. (Requires DAC_STEPPER_CURRENT)
199 199
  * M911 - Report stepper driver overtemperature pre-warn condition. (Requires HAVE_TMC2130)
200 200
  * M912 - Clear stepper driver overtemperature pre-warn condition flag. (Requires HAVE_TMC2130)
201
+ * M913 - Set HYBRID_THRESHOLD speed. (Requires HYBRID_THRESHOLD)
202
+ * M914 - Set SENSORLESS_HOMING sensitivity. (Requires SENSORLESS_HOMING)
201 203
  * M350 - Set microstepping mode. (Requires digital microstepping pins.)
202 204
  * M351 - Toggle MS1 MS2 pins directly. (Requires digital microstepping pins.)
203 205
  *
@@ -647,6 +649,10 @@ static bool send_ok[BUFSIZE];
647 649
   bool chdkActive = false;
648 650
 #endif
649 651
 
652
+#ifdef AUTOMATIC_CURRENT_CONTROL
653
+  bool auto_current_control = 0;
654
+#endif
655
+
650 656
 #if ENABLED(PID_EXTRUSION_SCALING)
651 657
   int lpq_len = 20;
652 658
 #endif
@@ -2757,6 +2763,28 @@ static void do_homing_move(const AxisEnum axis, float distance, float fr_mm_s=0.
2757 2763
 }
2758 2764
 
2759 2765
 /**
2766
+ * TMC2130 specific sensorless homing using stallGuard2.
2767
+ * stallGuard2 only works when in spreadCycle mode.
2768
+ * spreadCycle and stealthChop are mutually exclusive.
2769
+ */
2770
+#if ENABLED(SENSORLESS_HOMING)
2771
+  void tmc2130_sensorless_homing(TMC2130Stepper &st, bool enable=true) {
2772
+    #if ENABLED(STEALTHCHOP)
2773
+      if (enable) {
2774
+        st.coolstep_min_speed(1024UL * 1024UL - 1UL);
2775
+        st.stealthChop(0);
2776
+      }
2777
+      else {
2778
+        st.coolstep_min_speed(0);
2779
+        st.stealthChop(1);
2780
+      }
2781
+    #endif
2782
+
2783
+    st.diag1_stall(enable ? 1 : 0);
2784
+  }
2785
+#endif
2786
+
2787
+/**
2760 2788
  * Home an individual "raw axis" to its endstop.
2761 2789
  * This applies to XYZ on Cartesian and Core robots, and
2762 2790
  * to the individual ABC steppers on DELTA and SCARA.
@@ -2804,6 +2832,16 @@ static void homeaxis(const AxisEnum axis) {
2804 2832
     if (axis == Z_AXIS) stepper.set_homing_flag(true);
2805 2833
   #endif
2806 2834
 
2835
+  // Disable stealthChop if used. Enable diag1 pin on driver.
2836
+  #if ENABLED(SENSORLESS_HOMING)
2837
+    #if ENABLED(X_IS_TMC2130)
2838
+      if (axis == X_AXIS) tmc2130_sensorless_homing(stepperX);
2839
+    #endif
2840
+    #if ENABLED(Y_IS_TMC2130)
2841
+      if (axis == Y_AXIS) tmc2130_sensorless_homing(stepperY);
2842
+    #endif
2843
+  #endif
2844
+
2807 2845
   // Fast move towards endstop until triggered
2808 2846
   #if ENABLED(DEBUG_LEVELING_FEATURE)
2809 2847
     if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Home 1 Fast:");
@@ -2888,6 +2926,16 @@ static void homeaxis(const AxisEnum axis) {
2888 2926
 
2889 2927
   #endif
2890 2928
 
2929
+  // Re-enable stealthChop if used. Disable diag1 pin on driver.
2930
+  #if ENABLED(SENSORLESS_HOMING)
2931
+    #if ENABLED(X_IS_TMC2130)
2932
+      if (axis == X_AXIS) tmc2130_sensorless_homing(stepperX, false);
2933
+    #endif
2934
+    #if ENABLED(Y_IS_TMC2130)
2935
+      if (axis == Y_AXIS) tmc2130_sensorless_homing(stepperY, false);
2936
+    #endif
2937
+  #endif
2938
+
2891 2939
   // Put away the Z probe
2892 2940
   #if HOMING_Z_WITH_PROBE
2893 2941
     if (axis == Z_AXIS && STOW_PROBE()) return;
@@ -6902,6 +6950,11 @@ inline void gcode_M140() {
6902 6950
       OUT_WRITE(SUICIDE_PIN, HIGH);
6903 6951
     #endif
6904 6952
 
6953
+    #if ENABLED(HAVE_TMC2130)
6954
+      delay(100);
6955
+      tmc2130_init(); // Settings only stick when the driver has power
6956
+    #endif
6957
+
6905 6958
     #if ENABLED(ULTIPANEL)
6906 6959
       powersupply = true;
6907 6960
       LCD_MESSAGEPGM(WELCOME_MSG);
@@ -8770,22 +8823,21 @@ inline void gcode_M503() {
8770 8823
 
8771 8824
 #if ENABLED(HAVE_TMC2130)
8772 8825
 
8773
-  static void tmc2130_print_current(const int mA, const char name) {
8826
+  static void tmc2130_get_current(TMC2130Stepper &st, const char name) {
8774 8827
     SERIAL_CHAR(name);
8775 8828
     SERIAL_ECHOPGM(" axis driver current: ");
8776
-    SERIAL_ECHOLN(mA);
8829
+    SERIAL_ECHOLN(st.getCurrent());
8777 8830
   }
8778
-  static void tmc2130_set_current(const int mA, TMC2130Stepper &st, const char name) {
8779
-    tmc2130_print_current(mA, name);
8780
-    st.setCurrent(mA, 0.11, 0.5);
8781
-  }
8782
-  static void tmc2130_get_current(TMC2130Stepper &st, const char name) {
8783
-    tmc2130_print_current(st.getCurrent(), name);
8831
+  static void tmc2130_set_current(TMC2130Stepper &st, const char name, const int mA) {
8832
+    st.setCurrent(mA, R_SENSE, HOLD_MULTIPLIER);
8833
+    tmc2130_get_current(st, name);
8784 8834
   }
8835
+
8785 8836
   static void tmc2130_report_otpw(TMC2130Stepper &st, const char name) {
8786 8837
     SERIAL_CHAR(name);
8787 8838
     SERIAL_ECHOPGM(" axis temperature prewarn triggered: ");
8788 8839
     serialprintPGM(st.getOTPW() ? PSTR("true") : PSTR("false"));
8840
+    SERIAL_EOL;
8789 8841
   }
8790 8842
   static void tmc2130_clear_otpw(TMC2130Stepper &st, const char name) {
8791 8843
     st.clear_otpw();
@@ -8793,10 +8845,32 @@ inline void gcode_M503() {
8793 8845
     SERIAL_ECHOLNPGM(" prewarn flag cleared");
8794 8846
   }
8795 8847
 
8848
+  static void tmc2130_get_pwmthrs(TMC2130Stepper &st, const char name, const uint16_t spmm) {
8849
+    SERIAL_CHAR(name);
8850
+    SERIAL_ECHOPGM(" stealthChop max speed set to ");
8851
+    SERIAL_ECHOLN(12650000UL * st.microsteps() / (256 * st.stealth_max_speed() * spmm));
8852
+  }
8853
+  static void tmc2130_set_pwmthrs(TMC2130Stepper &st, const char name, const int32_t thrs, const uint32_t spmm) {
8854
+    st.stealth_max_speed(12650000UL * st.microsteps() / (256 * thrs * spmm));
8855
+    tmc2130_get_pwmthrs(st, name, spmm);
8856
+  }
8857
+
8858
+  static void tmc2130_get_sgt(TMC2130Stepper &st, const char name) {
8859
+    SERIAL_CHAR(name);
8860
+    SERIAL_ECHOPGM(" driver homing sensitivity set to ");
8861
+    SERIAL_ECHOLN(st.sgt());
8862
+  }
8863
+  static void tmc2130_set_sgt(TMC2130Stepper &st, const char name, const int8_t sgt_val) {
8864
+    st.sgt(sgt_val);
8865
+    tmc2130_get_sgt(st, name);
8866
+  }
8867
+
8796 8868
   /**
8797 8869
    * M906: Set motor current in milliamps using axis codes X, Y, Z, E
8798
-   *
8799 8870
    * Report driver currents when no axis specified
8871
+   *
8872
+   * S1: Enable automatic current control
8873
+   * S0: Disable
8800 8874
    */
8801 8875
   inline void gcode_M906() {
8802 8876
     uint16_t values[XYZE];
@@ -8804,21 +8878,25 @@ inline void gcode_M503() {
8804 8878
       values[i] = code_seen(axis_codes[i]) ? code_value_int() : 0;
8805 8879
 
8806 8880
     #if ENABLED(X_IS_TMC2130)
8807
-      if (values[X_AXIS]) tmc2130_set_current(values[X_AXIS], stepperX, 'X');
8881
+      if (values[X_AXIS]) tmc2130_set_current(stepperX, 'X', values[X_AXIS]);
8808 8882
       else tmc2130_get_current(stepperX, 'X');
8809 8883
     #endif
8810 8884
     #if ENABLED(Y_IS_TMC2130)
8811
-      if (values[Y_AXIS]) tmc2130_set_current(values[Y_AXIS], stepperY, 'Y');
8885
+      if (values[Y_AXIS]) tmc2130_set_current(stepperY, 'Y', values[Y_AXIS]);
8812 8886
       else tmc2130_get_current(stepperY, 'Y');
8813 8887
     #endif
8814 8888
     #if ENABLED(Z_IS_TMC2130)
8815
-      if (values[Z_AXIS]) tmc2130_set_current(values[Z_AXIS], stepperZ, 'Z');
8889
+      if (values[Z_AXIS]) tmc2130_set_current(stepperZ, 'Z', values[Z_AXIS]);
8816 8890
       else tmc2130_get_current(stepperZ, 'Z');
8817 8891
     #endif
8818 8892
     #if ENABLED(E0_IS_TMC2130)
8819
-      if (values[E_AXIS]) tmc2130_set_current(values[E_AXIS], stepperE0, 'E');
8893
+      if (values[E_AXIS]) tmc2130_set_current(stepperE0, 'E', values[E_AXIS]);
8820 8894
       else tmc2130_get_current(stepperE0, 'E');
8821 8895
     #endif
8896
+
8897
+    #if ENABLED(AUTOMATIC_CURRENT_CONTROL)
8898
+      if (code_seen('S')) auto_current_control = code_value_bool();
8899
+    #endif
8822 8900
   }
8823 8901
 
8824 8902
   /**
@@ -8826,17 +8904,19 @@ inline void gcode_M503() {
8826 8904
    * The flag is held by the library and persist until manually cleared by M912
8827 8905
    */
8828 8906
   inline void gcode_M911() {
8907
+    const bool reportX = code_seen('X'), reportY = code_seen('Y'), reportZ = code_seen('Z'), reportE = code_seen('E'),
8908
+             reportAll = (!reportX && !reportY && !reportZ && !reportE) || (reportX && reportY && reportZ && reportE);
8829 8909
     #if ENABLED(X_IS_TMC2130)
8830
-      tmc2130_report_otpw(stepperX, 'X');
8910
+      if (reportX || reportAll) tmc2130_report_otpw(stepperX, 'X');
8831 8911
     #endif
8832 8912
     #if ENABLED(Y_IS_TMC2130)
8833
-      tmc2130_report_otpw(stepperY, 'Y');
8913
+      if (reportY || reportAll) tmc2130_report_otpw(stepperY, 'Y');
8834 8914
     #endif
8835 8915
     #if ENABLED(Z_IS_TMC2130)
8836
-      tmc2130_report_otpw(stepperZ, 'Z');
8916
+      if (reportZ || reportAll) tmc2130_report_otpw(stepperZ, 'Z');
8837 8917
     #endif
8838 8918
     #if ENABLED(E0_IS_TMC2130)
8839
-      tmc2130_report_otpw(stepperE0, 'E');
8919
+      if (reportE || reportAll) tmc2130_report_otpw(stepperE0, 'E');
8840 8920
     #endif
8841 8921
   }
8842 8922
 
@@ -8844,20 +8924,66 @@ inline void gcode_M503() {
8844 8924
    * M912: Clear TMC2130 stepper driver overtemperature pre-warn flag held by the library
8845 8925
    */
8846 8926
   inline void gcode_M912() {
8927
+    const bool clearX = code_seen('X'), clearY = code_seen('Y'), clearZ = code_seen('Z'), clearE = code_seen('E'),
8928
+             clearAll = (!clearX && !clearY && !clearZ && !clearE) || (clearX && clearY && clearZ && clearE);
8847 8929
     #if ENABLED(X_IS_TMC2130)
8848
-      if (code_seen('X')) tmc2130_clear_otpw(stepperX, 'X');
8930
+      if (clearX || clearAll) tmc2130_clear_otpw(stepperX, 'X');
8849 8931
     #endif
8850 8932
     #if ENABLED(Y_IS_TMC2130)
8851
-      if (code_seen('Y')) tmc2130_clear_otpw(stepperY, 'Y');
8933
+      if (clearY || clearAll) tmc2130_clear_otpw(stepperY, 'Y');
8852 8934
     #endif
8853 8935
     #if ENABLED(Z_IS_TMC2130)
8854
-      if (code_seen('Z')) tmc2130_clear_otpw(stepperZ, 'Z');
8936
+      if (clearZ || clearAll) tmc2130_clear_otpw(stepperZ, 'Z');
8855 8937
     #endif
8856 8938
     #if ENABLED(E0_IS_TMC2130)
8857
-      if (code_seen('E')) tmc2130_clear_otpw(stepperE0, 'E');
8939
+      if (clearE || clearAll) tmc2130_clear_otpw(stepperE0, 'E');
8858 8940
     #endif
8859 8941
   }
8860 8942
 
8943
+  /**
8944
+   * M913: Set HYBRID_THRESHOLD speed.
8945
+   */
8946
+  #if ENABLED(HYBRID_THRESHOLD)
8947
+    inline void gcode_M913() {
8948
+      uint16_t values[XYZE];
8949
+      LOOP_XYZE(i)
8950
+        values[i] = code_seen(axis_codes[i]) ? code_value_int() : 0;
8951
+
8952
+      #if ENABLED(X_IS_TMC2130)
8953
+        if (values[X_AXIS]) tmc2130_set_pwmthrs(stepperX, 'X', values[X_AXIS], planner.axis_steps_per_mm[X_AXIS]);
8954
+        else tmc2130_get_pwmthrs(stepperX, 'X', planner.axis_steps_per_mm[X_AXIS]);
8955
+      #endif
8956
+      #if ENABLED(Y_IS_TMC2130)
8957
+        if (values[Y_AXIS]) tmc2130_set_pwmthrs(stepperY, 'Y', values[Y_AXIS], planner.axis_steps_per_mm[Y_AXIS]);
8958
+        else tmc2130_get_pwmthrs(stepperY, 'Y', planner.axis_steps_per_mm[Y_AXIS]);
8959
+      #endif
8960
+      #if ENABLED(Z_IS_TMC2130)
8961
+        if (values[Z_AXIS]) tmc2130_set_pwmthrs(stepperZ, 'Z', values[Z_AXIS], planner.axis_steps_per_mm[Z_AXIS]);
8962
+        else tmc2130_get_pwmthrs(stepperZ, 'Z', planner.axis_steps_per_mm[Z_AXIS]);
8963
+      #endif
8964
+      #if ENABLED(E0_IS_TMC2130)
8965
+        if (values[E_AXIS]) tmc2130_set_pwmthrs(stepperE0, 'E', values[E_AXIS], planner.axis_steps_per_mm[E_AXIS]);
8966
+        else tmc2130_get_pwmthrs(stepperE0, 'E', planner.axis_steps_per_mm[E_AXIS]);
8967
+      #endif
8968
+    }
8969
+  #endif // HYBRID_THRESHOLD
8970
+
8971
+  /**
8972
+   * M914: Set SENSORLESS_HOMING sensitivity.
8973
+   */
8974
+  #if ENABLED(SENSORLESS_HOMING)
8975
+    inline void gcode_M914() {
8976
+      #if ENABLED(X_IS_TMC2130)
8977
+        if (code_seen(axis_codes[X_AXIS])) tmc2130_set_sgt(stepperX, 'X', code_value_int());
8978
+        else tmc2130_get_sgt(stepperX, 'X');
8979
+      #endif
8980
+      #if ENABLED(Y_IS_TMC2130)
8981
+        if (code_seen(axis_codes[Y_AXIS])) tmc2130_set_sgt(stepperY, 'Y', code_value_int());
8982
+        else tmc2130_get_sgt(stepperY, 'Y');
8983
+      #endif
8984
+    }
8985
+  #endif // SENSORLESS_HOMING
8986
+
8861 8987
 #endif // HAVE_TMC2130
8862 8988
 
8863 8989
 /**
@@ -8865,10 +8991,9 @@ inline void gcode_M503() {
8865 8991
  */
8866 8992
 inline void gcode_M907() {
8867 8993
   #if HAS_DIGIPOTSS
8868
-    LOOP_XYZE(i)
8869
-    if (code_seen(axis_codes[i])) stepper.digipot_current(i, code_value_int());
8994
+    LOOP_XYZE(i) if (code_seen(axis_codes[i])) stepper.digipot_current(i, code_value_int());
8870 8995
     if (code_seen('B')) stepper.digipot_current(4, code_value_int());
8871
-    if (code_seen('S')) for (int i = 0; i <= 4; i++) stepper.digipot_current(i, code_value_int());
8996
+    if (code_seen('S')) for (uint8_t i = 0; i <= 4; i++) stepper.digipot_current(i, code_value_int());
8872 8997
   #elif HAS_MOTOR_CURRENT_PWM
8873 8998
     #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
8874 8999
       if (code_seen('X')) stepper.digipot_current(0, code_value_int());
@@ -8884,11 +9009,11 @@ inline void gcode_M907() {
8884 9009
     // this one uses actual amps in floating point
8885 9010
     LOOP_XYZE(i) if (code_seen(axis_codes[i])) digipot_i2c_set_current(i, code_value_float());
8886 9011
     // for each additional extruder (named B,C,D,E..., channels 4,5,6,7...)
8887
-    for (int i = NUM_AXIS; i < DIGIPOT_I2C_NUM_CHANNELS; i++) if (code_seen('B' + i - (NUM_AXIS))) digipot_i2c_set_current(i, code_value_float());
9012
+    for (uint8_t i = NUM_AXIS; i < DIGIPOT_I2C_NUM_CHANNELS; i++) if (code_seen('B' + i - (NUM_AXIS))) digipot_i2c_set_current(i, code_value_float());
8888 9013
   #endif
8889 9014
   #if ENABLED(DAC_STEPPER_CURRENT)
8890 9015
     if (code_seen('S')) {
8891
-      float dac_percent = code_value_float();
9016
+      const float dac_percent = code_value_float();
8892 9017
       for (uint8_t i = 0; i <= 4; i++) dac_current_percent(i, dac_percent);
8893 9018
     }
8894 9019
     LOOP_XYZE(i) if (code_seen(axis_codes[i])) dac_current_percent(i, code_value_float());
@@ -10165,6 +10290,18 @@ void process_next_command() {
10165 10290
         case 912: // M911: Clear TMC2130 prewarn triggered flags
10166 10291
           gcode_M912();
10167 10292
           break;
10293
+
10294
+        #if ENABLED(HYBRID_THRESHOLD)
10295
+          case 913: // M913: Set HYBRID_THRESHOLD speed.
10296
+            gcode_M913();
10297
+            break;
10298
+        #endif
10299
+
10300
+        #if ENABLED(SENSORLESS_HOMING)
10301
+          case 914: // M914: Set SENSORLESS_HOMING sensitivity.
10302
+            gcode_M914();
10303
+            break;
10304
+        #endif
10168 10305
       #endif
10169 10306
 
10170 10307
       #if HAS_MICROSTEPS
@@ -11390,23 +11527,58 @@ void disable_all_steppers() {
11390 11527
   disable_e_steppers();
11391 11528
 }
11392 11529
 
11393
-#if ENABLED(AUTOMATIC_CURRENT_CONTROL)
11530
+#if ENABLED(HAVE_TMC2130)
11394 11531
 
11395
-  void automatic_current_control(const TMC2130Stepper &st) {
11396
-    #if CURRENT_STEP > 0
11397
-      const bool is_otpw = st.checkOT(), // Check otpw even if we don't adjust. Allows for flag inspection.
11398
-                 is_otpw_triggered = st.getOTPW();
11532
+  void automatic_current_control(TMC2130Stepper &st, String axisID) {
11533
+    // Check otpw even if we don't use automatic control. Allows for flag inspection.
11534
+    const bool is_otpw = st.checkOT();
11399 11535
 
11400
-      if (!is_otpw && !is_otpw_triggered) {
11401
-        // OTPW bit not triggered yet -> Increase current
11402
-        const uint16_t current = st.getCurrent() + CURRENT_STEP;
11403
-        if (current <= AUTO_ADJUST_MAX) st.SilentStepStick2130(current);
11536
+    // Report if a warning was triggered
11537
+    static bool previous_otpw = false;
11538
+    if (is_otpw && !previous_otpw) {
11539
+      char timestamp[10];
11540
+      duration_t elapsed = print_job_timer.duration();
11541
+      const bool has_days = (elapsed.value > 60*60*24L);
11542
+      (void)elapsed.toDigital(timestamp, has_days);
11543
+      SERIAL_ECHO(timestamp);
11544
+      SERIAL_ECHO(": ");
11545
+      SERIAL_ECHO(axisID);
11546
+      SERIAL_ECHOLNPGM(" driver overtemperature warning!");
11547
+    }
11548
+    previous_otpw = is_otpw;
11549
+
11550
+    #if CURRENT_STEP > 0 && ENABLED(AUTOMATIC_CURRENT_CONTROL)
11551
+      // Return if user has not enabled current control start with M906 S1.
11552
+      if (!auto_current_control) return;
11553
+
11554
+      /**
11555
+       * Decrease current if is_otpw is true.
11556
+       * Bail out if driver is disabled.
11557
+       * Increase current if OTPW has not been triggered yet.
11558
+       */
11559
+      uint16_t current = st.getCurrent();
11560
+      if (is_otpw) {
11561
+        st.setCurrent(current - CURRENT_STEP, R_SENSE, HOLD_MULTIPLIER);
11562
+        #if ENABLED(REPORT_CURRENT_CHANGE)
11563
+          SERIAL_ECHO(axisID);
11564
+          SERIAL_ECHOPAIR(" current decreased to ", st.getCurrent());
11565
+        #endif
11404 11566
       }
11405
-      else if (is_otpw && is_otpw_triggered) {
11406
-        // OTPW bit triggered, triggered flag raised -> Decrease current
11407
-        st.SilentStepStick2130((float)st.getCurrent() - CURRENT_STEP);
11567
+
11568
+      else if (!st.isEnabled())
11569
+        return;
11570
+
11571
+      else if (!is_otpw && !st.getOTPW()) {
11572
+        current += CURRENT_STEP;
11573
+        if (current <= AUTO_ADJUST_MAX) {
11574
+          st.setCurrent(current, R_SENSE, HOLD_MULTIPLIER);
11575
+          #if ENABLED(REPORT_CURRENT_CHANGE)
11576
+            SERIAL_ECHO(axisID);
11577
+            SERIAL_ECHOPAIR(" current increased to ", st.getCurrent());
11578
+          #endif
11579
+        }
11408 11580
       }
11409
-      // OTPW bit cleared (we've cooled down), triggered flag still raised until manually cleared -> Do nothing, we're good
11581
+      SERIAL_EOL;
11410 11582
     #endif
11411 11583
   }
11412 11584
 
@@ -11415,34 +11587,37 @@ void disable_all_steppers() {
11415 11587
     if (ELAPSED(millis(), next_cOT)) {
11416 11588
       next_cOT = millis() + 5000;
11417 11589
       #if ENABLED(X_IS_TMC2130)
11418
-        automatic_current_control(stepperX);
11590
+        automatic_current_control(stepperX, "X");
11419 11591
       #endif
11420 11592
       #if ENABLED(Y_IS_TMC2130)
11421
-        automatic_current_control(stepperY);
11593
+        automatic_current_control(stepperY, "Y");
11422 11594
       #endif
11423 11595
       #if ENABLED(Z_IS_TMC2130)
11424
-        automatic_current_control(stepperZ);
11596
+        automatic_current_control(stepperZ, "Z");
11425 11597
       #endif
11426 11598
       #if ENABLED(X2_IS_TMC2130)
11427
-        automatic_current_control(stepperX2);
11599
+        automatic_current_control(stepperX2, "X2");
11428 11600
       #endif
11429 11601
       #if ENABLED(Y2_IS_TMC2130)
11430
-        automatic_current_control(stepperY2);
11602
+        automatic_current_control(stepperY2, "Y2");
11431 11603
       #endif
11432 11604
       #if ENABLED(Z2_IS_TMC2130)
11433
-        automatic_current_control(stepperZ2);
11605
+        automatic_current_control(stepperZ2, "Z2");
11434 11606
       #endif
11435 11607
       #if ENABLED(E0_IS_TMC2130)
11436
-        automatic_current_control(stepperE0);
11608
+        automatic_current_control(stepperE0, "E0");
11437 11609
       #endif
11438 11610
       #if ENABLED(E1_IS_TMC2130)
11439
-        automatic_current_control(stepperE1);
11611
+        automatic_current_control(stepperE1, "E1");
11440 11612
       #endif
11441 11613
       #if ENABLED(E2_IS_TMC2130)
11442
-        automatic_current_control(stepperE2);
11614
+        automatic_current_control(stepperE2, "E2");
11443 11615
       #endif
11444 11616
       #if ENABLED(E3_IS_TMC2130)
11445
-        automatic_current_control(stepperE3);
11617
+        automatic_current_control(stepperE3, "E3");
11618
+      #endif
11619
+      #if ENABLED(E4_IS_TMC2130)
11620
+        automatic_current_control(stepperE4, "E4");
11446 11621
       #endif
11447 11622
       #if ENABLED(E4_IS_TMC2130)
11448 11623
         automatic_current_control(stepperE4);
@@ -11450,7 +11625,7 @@ void disable_all_steppers() {
11450 11625
     }
11451 11626
   }
11452 11627
 
11453
-#endif // AUTOMATIC_CURRENT_CONTROL
11628
+#endif // HAVE_TMC2130
11454 11629
 
11455 11630
 /**
11456 11631
  * Manage several activities:
@@ -11648,7 +11823,7 @@ void manage_inactivity(bool ignore_stepper_queue/*=false*/) {
11648 11823
     handle_status_leds();
11649 11824
   #endif
11650 11825
 
11651
-  #if ENABLED(AUTOMATIC_CURRENT_CONTROL)
11826
+  #if ENABLED(HAVE_TMC2130)
11652 11827
     checkOverTemp();
11653 11828
   #endif
11654 11829
 

+ 10
- 10
Marlin/configuration_store.cpp View File

@@ -1124,34 +1124,34 @@ void MarlinSettings::reset() {
1124 1124
 
1125 1125
   #if ENABLED(HAVE_TMC2130)
1126 1126
     #if ENABLED(X_IS_TMC2130)
1127
-      stepperX.setCurrent(X_MAX_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1127
+      stepperX.setCurrent(X_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1128 1128
     #endif
1129 1129
     #if ENABLED(Y_IS_TMC2130)
1130
-      stepperY.setCurrent(Y_MAX_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1130
+      stepperY.setCurrent(Y_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1131 1131
     #endif
1132 1132
     #if ENABLED(Z_IS_TMC2130)
1133
-      stepperZ.setCurrent(Z_MAX_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1133
+      stepperZ.setCurrent(Z_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1134 1134
     #endif
1135 1135
     #if ENABLED(X2_IS_TMC2130)
1136
-      stepperX2.setCurrent(X2_MAX_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1136
+      stepperX2.setCurrent(X2_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1137 1137
     #endif
1138 1138
     #if ENABLED(Y2_IS_TMC2130)
1139
-      stepperY2.setCurrent(Y2_MAX_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1139
+      stepperY2.setCurrent(Y2_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1140 1140
     #endif
1141 1141
     #if ENABLED(Z2_IS_TMC2130)
1142
-      stepperZ2.setCurrent(Z2_MAX_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1142
+      stepperZ2.setCurrent(Z2_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1143 1143
     #endif
1144 1144
     #if ENABLED(E0_IS_TMC2130)
1145
-      stepperE0.setCurrent(E0_MAX_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1145
+      stepperE0.setCurrent(E0_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1146 1146
     #endif
1147 1147
     #if ENABLED(E1_IS_TMC2130)
1148
-      stepperE1.setCurrent(E1_MAX_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1148
+      stepperE1.setCurrent(E1_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1149 1149
     #endif
1150 1150
     #if ENABLED(E2_IS_TMC2130)
1151
-      stepperE2.setCurrent(E2_MAX_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1151
+      stepperE2.setCurrent(E2_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1152 1152
     #endif
1153 1153
     #if ENABLED(E3_IS_TMC2130)
1154
-      stepperE3.setCurrent(E3_MAX_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1154
+      stepperE3.setCurrent(E3_CURRENT, R_SENSE, HOLD_MULTIPLIER);
1155 1155
     #endif
1156 1156
   #endif
1157 1157
 

+ 38
- 24
Marlin/stepper_indirection.cpp View File

@@ -129,8 +129,9 @@
129 129
 
130 130
   #include <SPI.h>
131 131
   #include <TMC2130Stepper.h>
132
+  #include "enum.h"
132 133
 
133
-  #define _TMC2130_DEFINE(ST) TMC2130Stepper stepper##ST(ST##_ENABLE_PIN, ST##_DIR_PIN, ST##_STEP_PIN, ST##_CHIP_SELECT)
134
+  #define _TMC2130_DEFINE(ST) TMC2130Stepper stepper##ST(ST##_ENABLE_PIN, ST##_DIR_PIN, ST##_STEP_PIN, ST##_CS_PIN)
134 135
 
135 136
   // Stepper objects of TMC2130 steppers used
136 137
   #if ENABLED(X_IS_TMC2130)
@@ -169,61 +170,74 @@
169 170
 
170 171
   // Use internal reference voltage for current calculations. This is the default.
171 172
   // Following values from Trinamic's spreadsheet with values for a NEMA17 (42BYGHW609)
172
-  void tmc2130_init(TMC2130Stepper &st, const uint16_t max_current, const uint16_t microsteps) {
173
+  // https://www.trinamic.com/products/integrated-circuits/details/tmc2130/
174
+  void tmc2130_init(TMC2130Stepper &st, const uint16_t microsteps, const uint32_t thrs, const uint32_t spmm) {
173 175
     st.begin();
174 176
     st.setCurrent(st.getCurrent(), R_SENSE, HOLD_MULTIPLIER);
175 177
     st.microsteps(microsteps);
176
-    st.blank_time(24);
177
-    st.off_time(8);
178
+    st.blank_time(36);
179
+    st.off_time(5); // Only enables the driver if used with stealthChop
178 180
     st.interpolate(INTERPOLATE);
181
+    st.power_down_delay(128); // ~2s until driver lowers to hold current
182
+    st.hysterisis_start(0); // HSTRT = 1
183
+    st.hysterisis_low(1); // HEND = -2
184
+    st.diag1_active_high(1); // For sensorless homing
179 185
     #if ENABLED(STEALTHCHOP)
186
+      st.stealth_freq(1); // f_pwm = 2/683 f_clk
187
+      st.stealth_autoscale(1);
188
+      st.stealth_gradient(5);
189
+      st.stealth_amplitude(255);
180 190
       st.stealthChop(1);
181
-    #endif
182
-    #if ENABLED(SENSORLESS_HOMING)
183
-      st.coolstep_min_speed(1048575);
184
-      st.sg_stall_value(STALL_THRESHOLD);
185
-      st.sg_filter(1);
186
-      st.diag1_stall(1);
187
-      st.diag1_active_high(1);
191
+      #if ENABLED(HYBRID_THRESHOLD)
192
+        st.stealth_max_speed(12650000UL*st.microsteps()/(256*thrs*spmm));
193
+      #endif
194
+    #elif ENABLED(SENSORLESS_HOMING)
195
+      st.coolstep_min_speed(1024UL * 1024UL - 1UL);
188 196
     #endif
189 197
   }
190 198
 
191
-  #define _TMC2130_INIT(ST) tmc2130_init(stepper##ST, ST##_MAX_CURRENT, ST##_MICROSTEPS)
199
+  #define _TMC2130_INIT(ST, SPMM) tmc2130_init(stepper##ST, ST##_MICROSTEPS, ST##_HYBRID_THRESHOLD, SPMM)
192 200
 
193 201
   void tmc2130_init() {
194
-    delay(500); // Let power stabilize before configuring the steppers
202
+    constexpr uint16_t steps_per_mm[] = DEFAULT_AXIS_STEPS_PER_UNIT;
195 203
     #if ENABLED(X_IS_TMC2130)
196
-      _TMC2130_INIT(X);
204
+      _TMC2130_INIT( X, steps_per_mm[X_AXIS]);
205
+      #if ENABLED(SENSORLESS_HOMING)
206
+        stepperX.sg_stall_value(X_HOMING_SENSITIVITY);
207
+      #endif
197 208
     #endif
198 209
     #if ENABLED(X2_IS_TMC2130)
199
-      _TMC2130_INIT(X2);
210
+      _TMC2130_INIT(X2, steps_per_mm[X_AXIS]);
200 211
     #endif
201 212
     #if ENABLED(Y_IS_TMC2130)
202
-      _TMC2130_INIT(Y);
213
+      _TMC2130_INIT( Y, steps_per_mm[Y_AXIS]);
214
+      #if ENABLED(SENSORLESS_HOMING)
215
+        stepperY.sg_stall_value(Y_HOMING_SENSITIVITY);
216
+      #endif
203 217
     #endif
204 218
     #if ENABLED(Y2_IS_TMC2130)
205
-      _TMC2130_INIT(Y2);
219
+      _TMC2130_INIT(Y2, steps_per_mm[Y_AXIS]);
206 220
     #endif
207 221
     #if ENABLED(Z_IS_TMC2130)
208
-      _TMC2130_INIT(Z);
222
+      _TMC2130_INIT( Z, steps_per_mm[Z_AXIS]);
209 223
     #endif
210 224
     #if ENABLED(Z2_IS_TMC2130)
211
-      _TMC2130_INIT(Z2);
225
+      _TMC2130_INIT(Z2, steps_per_mm[Z_AXIS]);
212 226
     #endif
213 227
     #if ENABLED(E0_IS_TMC2130)
214
-      _TMC2130_INIT(E0);
228
+      _TMC2130_INIT(E0, steps_per_mm[E_AXIS]);
215 229
     #endif
216 230
     #if ENABLED(E1_IS_TMC2130)
217
-      _TMC2130_INIT(E1);
231
+      { constexpr int extruder = 1; _TMC2130_INIT(E1, steps_per_mm[E_AXIS_N]); }
218 232
     #endif
219 233
     #if ENABLED(E2_IS_TMC2130)
220
-      _TMC2130_INIT(E2);
234
+      { constexpr int extruder = 2; _TMC2130_INIT(E2, steps_per_mm[E_AXIS_N]); }
221 235
     #endif
222 236
     #if ENABLED(E3_IS_TMC2130)
223
-      _TMC2130_INIT(E3);
237
+      { constexpr int extruder = 3; _TMC2130_INIT(E3, steps_per_mm[E_AXIS_N]); }
224 238
     #endif
225 239
     #if ENABLED(E4_IS_TMC2130)
226
-      _TMC2130_INIT(E4);
240
+      { constexpr int extruder = 4; _TMC2130_INIT(E4, steps_per_mm[E_AXIS_N]); }
227 241
     #endif
228 242
 
229 243
     TMC2130_ADV()

Loading…
Cancel
Save