From a4c9f70878a02f9f3fcb3dd0785cd06e63e14cc5 Mon Sep 17 00:00:00 2001 From: teemuatlut Date: Wed, 3 Oct 2018 10:48:49 +0300 Subject: [PATCH] Migrate to a new TMC library (#11943) Backport commit c3229e1b3461b6da8373e7a24a7eeb131912a15b from Marlin 2.0 --- Marlin/Conditionals_post.h | 9 +- Marlin/Marlin_main.cpp | 83 +++---- Marlin/configuration_store.cpp | 382 ++++++++++++++++----------------- Marlin/drivers.h | 6 +- Marlin/language.h | 4 + Marlin/stepper_indirection.cpp | 372 +++++++++++++++----------------- Marlin/stepper_indirection.h | 109 ++++------ Marlin/tmc_util.cpp | 224 ++++++++----------- Marlin/tmc_util.h | 133 +++++++++--- platformio.ini | 3 +- 10 files changed, 651 insertions(+), 674 deletions(-) diff --git a/Marlin/Conditionals_post.h b/Marlin/Conditionals_post.h index 42f4733..3f979ad 100644 --- a/Marlin/Conditionals_post.h +++ b/Marlin/Conditionals_post.h @@ -423,6 +423,9 @@ * NOTE: Driver timing order is longest-to-shortest duration. * Preserve this ordering when adding new drivers. */ + +#define TRINAMICS (HAS_TRINAMIC || HAS_DRIVER(TMC2660) || HAS_DRIVER(TMC2130_STANDALONE) || HAS_DRIVER(TMC2208_STANDALONE) || HAS_DRIVER(TMC26X_STANDALONE) || HAS_DRIVER(TMC2660_STANDALONE)) + #ifndef MINIMUM_STEPPER_DIR_DELAY #if HAS_DRIVER(TB6560) #define MINIMUM_STEPPER_DIR_DELAY 15000 @@ -434,7 +437,7 @@ #define MINIMUM_STEPPER_DIR_DELAY 500 #elif HAS_DRIVER(A4988) #define MINIMUM_STEPPER_DIR_DELAY 200 - #elif HAS_TRINAMIC || HAS_DRIVER(TMC2660) || HAS_DRIVER(TMC2130_STANDALONE) || HAS_DRIVER(TMC2208_STANDALONE) || HAS_DRIVER(TMC26X_STANDALONE) || HAS_DRIVER(TMC2660_STANDALONE) + #elif TRINAMICS #define MINIMUM_STEPPER_DIR_DELAY 20 #else #define MINIMUM_STEPPER_DIR_DELAY 200 // Expect at least 10µS since one Stepper ISR must transpire @@ -450,7 +453,7 @@ #define MINIMUM_STEPPER_PULSE 2 #elif HAS_DRIVER(A4988) || HAS_DRIVER(LV8729) #define MINIMUM_STEPPER_PULSE 1 - #elif HAS_TRINAMIC || HAS_DRIVER(TMC2660) || HAS_DRIVER(TMC2130_STANDALONE) || HAS_DRIVER(TMC2208_STANDALONE) || HAS_DRIVER(TMC26X_STANDALONE) || HAS_DRIVER(TMC2660_STANDALONE) + #elif TRINAMICS #define MINIMUM_STEPPER_PULSE 0 #else #define MINIMUM_STEPPER_PULSE 1 @@ -466,7 +469,7 @@ #define MAXIMUM_STEPPER_RATE 150000 #elif HAS_DRIVER(DRV8825) #define MAXIMUM_STEPPER_RATE 250000 - #elif HAS_TRINAMIC || HAS_DRIVER(TMC2660) || HAS_DRIVER(TMC2130_STANDALONE) || HAS_DRIVER(TMC2208_STANDALONE) || HAS_DRIVER(TMC26X_STANDALONE) || HAS_DRIVER(TMC2660_STANDALONE) + #elif TRINAMICS #define MAXIMUM_STEPPER_RATE 400000 #elif HAS_DRIVER(A4988) #define MAXIMUM_STEPPER_RATE 500000 diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index 7cb98a7..02a8c11 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -11503,9 +11503,11 @@ inline void gcode_M502() { #if HAS_TRINAMIC #if ENABLED(TMC_DEBUG) inline void gcode_M122() { - if (parser.seen('S')) - tmc_set_report_status(parser.value_bool()); - else + #if ENABLED(MONITOR_DRIVER_STATUS) + if (parser.seen('S')) + tmc_set_report_status(parser.value_bool()); + else + #endif tmc_report_all(); } #endif // TMC_DEBUG @@ -11516,7 +11518,7 @@ inline void gcode_M502() { * Report driver currents when no axis specified */ inline void gcode_M906() { - #define TMC_SAY_CURRENT(Q) tmc_get_current(stepper##Q, TMC_##Q) + #define TMC_SAY_CURRENT(Q) tmc_get_current(stepper##Q) #define TMC_SET_CURRENT(Q) tmc_set_current(stepper##Q, value) bool report = true; @@ -11661,43 +11663,44 @@ inline void gcode_M502() { #define M91x_USE(ST) (AXIS_DRIVER_TYPE(ST, TMC2130) || (AXIS_DRIVER_TYPE(ST, TMC2208) && PIN_EXISTS(ST##_SERIAL_RX))) #define M91x_USE_E(N) (E_STEPPERS > N && M91x_USE(E##N)) +#if ENABLED(MONITOR_DRIVER_STATUS) /** * M911: Report TMC stepper driver overtemperature pre-warn flag * This flag is held by the library, persisting until cleared by M912 */ inline void gcode_M911() { #if M91x_USE(X) - tmc_report_otpw(stepperX, TMC_X); + tmc_report_otpw(stepperX); #endif #if M91x_USE(X2) - tmc_report_otpw(stepperX2, TMC_X2); + tmc_report_otpw(stepperX2); #endif #if M91x_USE(Y) - tmc_report_otpw(stepperY, TMC_Y); + tmc_report_otpw(stepperY); #endif #if M91x_USE(Y2) - tmc_report_otpw(stepperY2, TMC_Y2); + tmc_report_otpw(stepperY2); #endif #if M91x_USE(Z) - tmc_report_otpw(stepperZ, TMC_Z); + tmc_report_otpw(stepperZ); #endif #if M91x_USE(Z2) - tmc_report_otpw(stepperZ2, TMC_Z2); + tmc_report_otpw(stepperZ2); #endif #if M91x_USE_E(0) - tmc_report_otpw(stepperE0, TMC_E0); + tmc_report_otpw(stepperE0); #endif #if M91x_USE_E(1) - tmc_report_otpw(stepperE1, TMC_E1); + tmc_report_otpw(stepperE1); #endif #if M91x_USE_E(2) - tmc_report_otpw(stepperE2, TMC_E2); + tmc_report_otpw(stepperE2); #endif #if M91x_USE_E(3) - tmc_report_otpw(stepperE3, TMC_E3); + tmc_report_otpw(stepperE3); #endif #if M91x_USE_E(4) - tmc_report_otpw(stepperE4, TMC_E4); + tmc_report_otpw(stepperE4); #endif } @@ -11723,30 +11726,30 @@ inline void gcode_M502() { #if M91x_USE(X) || M91x_USE(X2) const uint8_t xval = parser.byteval(axis_codes[X_AXIS], 10); #if M91x_USE(X) - if (hasNone || xval == 1 || (hasX && xval == 10)) tmc_clear_otpw(stepperX, TMC_X); + if (hasNone || xval == 1 || (hasX && xval == 10)) tmc_clear_otpw(stepperX); #endif #if M91x_USE(X2) - if (hasNone || xval == 2 || (hasX && xval == 10)) tmc_clear_otpw(stepperX2, TMC_X2); + if (hasNone || xval == 2 || (hasX && xval == 10)) tmc_clear_otpw(stepperX2); #endif #endif #if M91x_USE(Y) || M91x_USE(Y2) const uint8_t yval = parser.byteval(axis_codes[Y_AXIS], 10); #if M91x_USE(Y) - if (hasNone || yval == 1 || (hasY && yval == 10)) tmc_clear_otpw(stepperY, TMC_Y); + if (hasNone || yval == 1 || (hasY && yval == 10)) tmc_clear_otpw(stepperY); #endif #if M91x_USE(Y2) - if (hasNone || yval == 2 || (hasY && yval == 10)) tmc_clear_otpw(stepperY2, TMC_Y2); + if (hasNone || yval == 2 || (hasY && yval == 10)) tmc_clear_otpw(stepperY2); #endif #endif #if M91x_USE(Z) || M91x_USE(Z2) const uint8_t zval = parser.byteval(axis_codes[Z_AXIS], 10); #if M91x_USE(Z) - if (hasNone || zval == 1 || (hasZ && zval == 10)) tmc_clear_otpw(stepperZ, TMC_Z); + if (hasNone || zval == 1 || (hasZ && zval == 10)) tmc_clear_otpw(stepperZ); #endif #if M91x_USE(Z2) - if (hasNone || zval == 2 || (hasZ && zval == 10)) tmc_clear_otpw(stepperZ2, TMC_Z2); + if (hasNone || zval == 2 || (hasZ && zval == 10)) tmc_clear_otpw(stepperZ2); #endif #endif @@ -11754,31 +11757,32 @@ inline void gcode_M502() { #if M91x_USE_E(0) || M91x_USE_E(1) || M91x_USE_E(2) || M91x_USE_E(3) || M91x_USE_E(4) const uint8_t eval = parser.byteval(axis_codes[E_AXIS], 10); #if M91x_USE_E(0) - if (hasNone || eval == 0 || (hasE && eval == 10)) tmc_clear_otpw(stepperE0, TMC_E0); + if (hasNone || eval == 0 || (hasE && eval == 10)) tmc_clear_otpw(stepperE0); #endif #if M91x_USE_E(1) - if (hasNone || eval == 1 || (hasE && eval == 10)) tmc_clear_otpw(stepperE1, TMC_E1); + if (hasNone || eval == 1 || (hasE && eval == 10)) tmc_clear_otpw(stepperE1); #endif #if M91x_USE_E(2) - if (hasNone || eval == 2 || (hasE && eval == 10)) tmc_clear_otpw(stepperE2, TMC_E2); + if (hasNone || eval == 2 || (hasE && eval == 10)) tmc_clear_otpw(stepperE2); #endif #if M91x_USE_E(3) - if (hasNone || eval == 3 || (hasE && eval == 10)) tmc_clear_otpw(stepperE3, TMC_E3); + if (hasNone || eval == 3 || (hasE && eval == 10)) tmc_clear_otpw(stepperE3); #endif #if M91x_USE_E(4) - if (hasNone || eval == 4 || (hasE && eval == 10)) tmc_clear_otpw(stepperE4, TMC_E4); + if (hasNone || eval == 4 || (hasE && eval == 10)) tmc_clear_otpw(stepperE4); #endif #endif } +#endif /** * M913: Set HYBRID_THRESHOLD speed. */ #if ENABLED(HYBRID_THRESHOLD) inline void gcode_M913() { - #define TMC_SAY_PWMTHRS(A,Q) tmc_get_pwmthrs(stepper##Q, TMC_##Q, planner.axis_steps_per_mm[_AXIS(A)]) + #define TMC_SAY_PWMTHRS(A,Q) tmc_get_pwmthrs(stepper##Q, planner.axis_steps_per_mm[_AXIS(A)]) #define TMC_SET_PWMTHRS(A,Q) tmc_set_pwmthrs(stepper##Q, value, planner.axis_steps_per_mm[_AXIS(A)]) - #define TMC_SAY_PWMTHRS_E(E) do{ const uint8_t extruder = E; tmc_get_pwmthrs(stepperE##E, TMC_E##E, planner.axis_steps_per_mm[E_AXIS_N]); }while(0) + #define TMC_SAY_PWMTHRS_E(E) do{ const uint8_t extruder = E; tmc_get_pwmthrs(stepperE##E, planner.axis_steps_per_mm[E_AXIS_N]); }while(0) #define TMC_SET_PWMTHRS_E(E) do{ const uint8_t extruder = E; tmc_set_pwmthrs(stepperE##E, value, planner.axis_steps_per_mm[E_AXIS_N]); }while(0) bool report = true; @@ -11876,7 +11880,7 @@ inline void gcode_M502() { */ #if ENABLED(SENSORLESS_HOMING) inline void gcode_M914() { - #define TMC_SAY_SGT(Q) tmc_get_sgt(stepper##Q, TMC_##Q) + #define TMC_SAY_SGT(Q) tmc_get_sgt(stepper##Q) #define TMC_SET_SGT(Q) tmc_set_sgt(stepper##Q, value) bool report = true; @@ -11961,12 +11965,12 @@ inline void gcode_M502() { } #if AXIS_IS_TMC(Z) - const uint16_t Z_current_1 = stepperZ.getCurrent(); - stepperZ.setCurrent(_rms, R_SENSE, HOLD_MULTIPLIER); + const uint16_t Z_current_1 = stepperZ.getMilliamps(); + stepperZ.rms_current(_rms); #endif #if AXIS_IS_TMC(Z2) - const uint16_t Z2_current_1 = stepperZ2.getCurrent(); - stepperZ2.setCurrent(_rms, R_SENSE, HOLD_MULTIPLIER); + const uint16_t Z2_current_1 = stepperZ2.getMilliamps(); + stepperZ2.rms_current(_rms); #endif SERIAL_ECHOPAIR("\nCalibration current: Z", _rms); @@ -11976,10 +11980,10 @@ inline void gcode_M502() { do_blocking_move_to_z(Z_MAX_POS+_z); #if AXIS_IS_TMC(Z) - stepperZ.setCurrent(Z_current_1, R_SENSE, HOLD_MULTIPLIER); + stepperZ.rms_current(Z_current_1); #endif #if AXIS_IS_TMC(Z2) - stepperZ2.setCurrent(Z2_current_1, R_SENSE, HOLD_MULTIPLIER); + stepperZ2.rms_current(Z2_current_1); #endif do_blocking_move_to_z(Z_MAX_POS); @@ -13233,8 +13237,10 @@ void process_parsed_command() { case 122: gcode_M122(); break; // M122: Debug TMC steppers #endif case 906: gcode_M906(); break; // M906: Set motor current in milliamps using axis codes X, Y, Z, E - case 911: gcode_M911(); break; // M911: Report TMC prewarn triggered flags - case 912: gcode_M912(); break; // M911: Clear TMC prewarn triggered flags + #if ENABLED(MONITOR_DRIVER_STATUS) + case 911: gcode_M911(); break; // M911: Report TMC prewarn triggered flags + case 912: gcode_M912(); break; // M911: Clear TMC prewarn triggered flags + #endif #if ENABLED(HYBRID_THRESHOLD) case 913: gcode_M913(); break; // M913: Set HYBRID_THRESHOLD speed. #endif @@ -15243,6 +15249,9 @@ void setup() { // Prepare communication for TMC drivers #if HAS_DRIVER(TMC2130) + #if DISABLED(TMC_USE_SW_SPI) + SPI.begin(); + #endif tmc_init_cs_pins(); #endif #if HAS_DRIVER(TMC2208) diff --git a/Marlin/configuration_store.cpp b/Marlin/configuration_store.cpp index bfaea61..1d8241e 100644 --- a/Marlin/configuration_store.cpp +++ b/Marlin/configuration_store.cpp @@ -64,6 +64,9 @@ #include "tmc_util.h" #define TMC_GET_PWMTHRS(A,Q) _tmc_thrs(stepper##Q.microsteps(), stepper##Q.TPWMTHRS(), planner.axis_steps_per_mm[_AXIS(A)]) #endif +typedef struct { uint16_t X, Y, Z, X2, Y2, Z2, E0, E1, E2, E3, E4; } tmc_stepper_current_t; +typedef struct { uint32_t X, Y, Z, X2, Y2, Z2, E0, E1, E2, E3, E4; } tmc_hybrid_threshold_t; +typedef struct { int16_t X, Y, Z; } tmc_sgt_t; #if ENABLED(AUTO_BED_LEVELING_UBL) #include "ubl.h" @@ -79,7 +82,7 @@ #pragma pack(push, 1) // No padding between variables -typedef struct PID { float Kp, Ki, Kd; } PID; +typedef struct PID { float Kp, Ki, Kd; } PID; typedef struct PIDC { float Kp, Ki, Kd, Kc; } PIDC; /** @@ -241,9 +244,9 @@ typedef struct SettingsDataStruct { // HAS_TRINAMIC // #define TMC_AXES (MAX_EXTRUDERS + 6) - uint16_t tmc_stepper_current[TMC_AXES]; // M906 X Y Z X2 Y2 Z2 E0 E1 E2 E3 E4 - uint32_t tmc_hybrid_threshold[TMC_AXES]; // M913 X Y Z X2 Y2 Z2 E0 E1 E2 E3 E4 - int16_t tmc_sgt[XYZ]; // M914 X Y Z + tmc_stepper_current_t tmc_stepper_current; // M906 X Y Z X2 Y2 Z2 E0 E1 E2 E3 E4 + tmc_hybrid_threshold_t tmc_hybrid_threshold; // M913 X Y Z X2 Y2 Z2 E0 E1 E2 E3 E4 + tmc_sgt_t tmc_sgt; // M914 X Y Z // // LIN_ADVANCE @@ -294,7 +297,7 @@ uint16_t MarlinSettings::datasize() { return sizeof(SettingsData); } #endif void MarlinSettings::postprocess() { - const float oldpos[] = { current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] }; + const float oldpos[XYZE] = { current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS] }; // steps per s2 needs to be updated to agree with units per s2 planner.reset_acceleration_rates(); @@ -452,10 +455,11 @@ void MarlinSettings::postprocess() { EEPROM_WRITE(planner.min_travel_feedrate_mm_s); #if ENABLED(JUNCTION_DEVIATION) - const float planner_max_jerk[] = { #if ENABLED(HANGPRINTER) + const float planner_max_jerk[ABCDE] = { float(DEFAULT_AJERK), float(DEFAULT_BJERK), float(DEFAULT_CJERK), float(DEFAULT_DJERK), float(DEFAULT_EJERK) #else + const float planner_max_jerk[XYZE] = { float(DEFAULT_XJERK), float(DEFAULT_YJERK), float(DEFAULT_ZJERK), float(DEFAULT_EJERK) #endif }; @@ -484,11 +488,13 @@ void MarlinSettings::postprocess() { // Global Leveling // - #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) - const float zfh = planner.z_fade_height; - #else - const float zfh = 10.0; - #endif + const float zfh = ( + #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT) + planner.z_fade_height + #else + 10.0 + #endif + ); EEPROM_WRITE(zfh); // @@ -498,7 +504,7 @@ void MarlinSettings::postprocess() { #if ENABLED(MESH_BED_LEVELING) // Compile time test that sizeof(mbl.z_values) is as expected static_assert( - sizeof(mbl.z_values) == GRID_MAX_POINTS * sizeof(mbl.z_values[0][0]), + sizeof(mbl.z_values) == (GRID_MAX_POINTS) * sizeof(mbl.z_values[0][0]), "MBL Z array is the wrong size." ); const uint8_t mesh_num_x = GRID_MAX_POINTS_X, mesh_num_y = GRID_MAX_POINTS_Y; @@ -540,7 +546,7 @@ void MarlinSettings::postprocess() { #if ENABLED(AUTO_BED_LEVELING_BILINEAR) // Compile time test that sizeof(z_values) is as expected static_assert( - sizeof(z_values) == GRID_MAX_POINTS * sizeof(z_values[0][0]), + sizeof(z_values) == (GRID_MAX_POINTS) * sizeof(z_values[0][0]), "Bilinear Z array is the wrong size." ); const uint8_t grid_max_x = GRID_MAX_POINTS_X, grid_max_y = GRID_MAX_POINTS_Y; @@ -731,169 +737,138 @@ void MarlinSettings::postprocess() { #endif // - // Save TMC2130 or TMC2208 Configuration, and placeholder values + // Save TMC Configuration, and placeholder values // _FIELD_TEST(tmc_stepper_current); - uint16_t tmc_stepper_current[TMC_AXES] = { - #if HAS_TRINAMIC - #if AXIS_IS_TMC(X) - stepperX.getCurrent(), - #else - 0, - #endif - #if AXIS_IS_TMC(Y) - stepperY.getCurrent(), - #else - 0, - #endif - #if AXIS_IS_TMC(Z) - stepperZ.getCurrent(), - #else - 0, - #endif - #if AXIS_IS_TMC(X2) - stepperX2.getCurrent(), - #else - 0, - #endif - #if AXIS_IS_TMC(Y2) - stepperY2.getCurrent(), - #else - 0, - #endif - #if AXIS_IS_TMC(Z2) - stepperZ2.getCurrent(), - #else - 0, - #endif - #if AXIS_IS_TMC(E0) - stepperE0.getCurrent(), - #else - 0, - #endif - #if AXIS_IS_TMC(E1) - stepperE1.getCurrent(), - #else - 0, - #endif - #if AXIS_IS_TMC(E2) - stepperE2.getCurrent(), - #else - 0, - #endif - #if AXIS_IS_TMC(E3) - stepperE3.getCurrent(), - #else - 0, - #endif - #if AXIS_IS_TMC(E4) - stepperE4.getCurrent() - #else - 0 - #endif - #else - 0 + tmc_stepper_current_t tmc_stepper_current = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + + #if HAS_TRINAMIC + #if AXIS_IS_TMC(X) + tmc_stepper_current.X = stepperX.getMilliamps(); #endif - }; + #if AXIS_IS_TMC(Y) + tmc_stepper_current.Y = stepperY.getMilliamps(); + #endif + #if AXIS_IS_TMC(Z) + tmc_stepper_current.Z = stepperZ.getMilliamps(); + #endif + #if AXIS_IS_TMC(X2) + tmc_stepper_current.X2 = stepperX2.getMilliamps(); + #endif + #if AXIS_IS_TMC(Y2) + tmc_stepper_current.Y2 = stepperY2.getMilliamps(); + #endif + #if AXIS_IS_TMC(Z2) + tmc_stepper_current.Z2 = stepperZ2.getMilliamps(); + #endif + #if MAX_EXTRUDERS + #if AXIS_IS_TMC(E0) + tmc_stepper_current.E0 = stepperE0.getMilliamps(); + #endif + #if MAX_EXTRUDERS > 1 + #if AXIS_IS_TMC(E1) + tmc_stepper_current.E1 = stepperE1.getMilliamps(); + #endif + #if MAX_EXTRUDERS > 2 + #if AXIS_IS_TMC(E2) + tmc_stepper_current.E2 = stepperE2.getMilliamps(); + #endif + #if MAX_EXTRUDERS > 3 + #if AXIS_IS_TMC(E3) + tmc_stepper_current.E3 = stepperE3.getMilliamps(); + #endif + #if MAX_EXTRUDERS > 4 + #if AXIS_IS_TMC(E4) + tmc_stepper_current.E4 = stepperE4.getMilliamps(); + #endif + #endif // MAX_EXTRUDERS > 4 + #endif // MAX_EXTRUDERS > 3 + #endif // MAX_EXTRUDERS > 2 + #endif // MAX_EXTRUDERS > 1 + #endif // MAX_EXTRUDERS + #endif EEPROM_WRITE(tmc_stepper_current); // - // Save TMC2130 or TMC2208 Hybrid Threshold, and placeholder values + // Save TMC Hybrid Threshold, and placeholder values // _FIELD_TEST(tmc_hybrid_threshold); - uint32_t tmc_hybrid_threshold[TMC_AXES] = { - #if ENABLED(HYBRID_THRESHOLD) - #if AXIS_HAS_STEALTHCHOP(X) - TMC_GET_PWMTHRS(X, X), - #else - X_HYBRID_THRESHOLD, - #endif - #if AXIS_HAS_STEALTHCHOP(Y) - TMC_GET_PWMTHRS(Y, Y), - #else - Y_HYBRID_THRESHOLD, - #endif - #if AXIS_HAS_STEALTHCHOP(Z) - TMC_GET_PWMTHRS(Z, Z), - #else - Z_HYBRID_THRESHOLD, - #endif - #if AXIS_HAS_STEALTHCHOP(X2) - TMC_GET_PWMTHRS(X, X2), - #else - X2_HYBRID_THRESHOLD, - #endif - #if AXIS_HAS_STEALTHCHOP(Y2) - TMC_GET_PWMTHRS(Y, Y2), - #else - Y2_HYBRID_THRESHOLD, - #endif - #if AXIS_HAS_STEALTHCHOP(Z2) - TMC_GET_PWMTHRS(Z, Z2), - #else - Z2_HYBRID_THRESHOLD, - #endif - #if AXIS_HAS_STEALTHCHOP(E0) - TMC_GET_PWMTHRS(E, E0), - #else - E0_HYBRID_THRESHOLD, - #endif - #if AXIS_HAS_STEALTHCHOP(E1) - TMC_GET_PWMTHRS(E, E1), - #else - E1_HYBRID_THRESHOLD, - #endif - #if AXIS_HAS_STEALTHCHOP(E2) - TMC_GET_PWMTHRS(E, E2), - #else - E2_HYBRID_THRESHOLD, - #endif - #if AXIS_HAS_STEALTHCHOP(E3) - TMC_GET_PWMTHRS(E, E3), - #else - E3_HYBRID_THRESHOLD, - #endif - #if AXIS_HAS_STEALTHCHOP(E4) - TMC_GET_PWMTHRS(E, E4) - #else - E4_HYBRID_THRESHOLD - #endif - #else - 100, 100, 3, // X, Y, Z - 100, 100, 3, // X2, Y2, Z2 - 30, 30, 30, 30, 30 // E0, E1, E2, E3, E4 + #if ENABLED(HYBRID_THRESHOLD) + tmc_hybrid_threshold_t tmc_hybrid_threshold = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + #if AXIS_HAS_STEALTHCHOP(X) + tmc_hybrid_threshold.X = TMC_GET_PWMTHRS(X, X); #endif - }; + #if AXIS_HAS_STEALTHCHOP(Y) + tmc_hybrid_threshold.Y = TMC_GET_PWMTHRS(Y, Y); + #endif + #if AXIS_HAS_STEALTHCHOP(Z) + tmc_hybrid_threshold.Z = TMC_GET_PWMTHRS(Z, Z); + #endif + #if AXIS_HAS_STEALTHCHOP(X2) + tmc_hybrid_threshold.X2 = TMC_GET_PWMTHRS(X, X2); + #endif + #if AXIS_HAS_STEALTHCHOP(Y2) + tmc_hybrid_threshold.Y2 = TMC_GET_PWMTHRS(Y, Y2); + #endif + #if AXIS_HAS_STEALTHCHOP(Z2) + tmc_hybrid_threshold.Z2 = TMC_GET_PWMTHRS(Z, Z2); + #endif + #if MAX_EXTRUDERS + #if AXIS_HAS_STEALTHCHOP(E0) + tmc_hybrid_threshold.E0 = TMC_GET_PWMTHRS(E, E0); + #endif + #if MAX_EXTRUDERS > 1 + #if AXIS_HAS_STEALTHCHOP(E1) + tmc_hybrid_threshold.E1 = TMC_GET_PWMTHRS(E, E1); + #endif + #if MAX_EXTRUDERS > 2 + #if AXIS_HAS_STEALTHCHOP(E2) + tmc_hybrid_threshold.E2 = TMC_GET_PWMTHRS(E, E2); + #endif + #if MAX_EXTRUDERS > 3 + #if AXIS_HAS_STEALTHCHOP(E3) + tmc_hybrid_threshold.E3 = TMC_GET_PWMTHRS(E, E3); + #endif + #if MAX_EXTRUDERS > 4 + #if AXIS_HAS_STEALTHCHOP(E4) + tmc_hybrid_threshold.E4 = TMC_GET_PWMTHRS(E, E4); + #endif + #endif // MAX_EXTRUDERS > 4 + #endif // MAX_EXTRUDERS > 3 + #endif // MAX_EXTRUDERS > 2 + #endif // MAX_EXTRUDERS > 1 + #endif // MAX_EXTRUDERS + #else + const tmc_hybrid_threshold_t tmc_hybrid_threshold = { + .X = 100, .Y = 100, .Z = 3, + .X2 = 100, .Y2 = 100, .Z2 = 3, + .E0 = 30, .E1 = 30, .E2 = 30, + .E3 = 30, .E4 = 30 + }; + #endif EEPROM_WRITE(tmc_hybrid_threshold); // - // TMC2130 Sensorless homing threshold + // TMC Sensorless homing threshold // - int16_t tmc_sgt[XYZ] = { - #if ENABLED(SENSORLESS_HOMING) - #if X_SENSORLESS - stepperX.sgt(), - #else - 0, - #endif - #if Y_SENSORLESS - stepperY.sgt(), - #else - 0, - #endif - #if Z_SENSORLESS - stepperZ.sgt() - #else - 0 - #endif - #else - 0 + + tmc_sgt_t tmc_sgt = { 0, 0, 0 }; + + #if USE_SENSORLESS + #if X_SENSORLESS + tmc_sgt.X = stepperX.sgt(); #endif - }; + #if Y_SENSORLESS + tmc_sgt.Y = stepperY.sgt(); + #endif + #if Z_SENSORLESS + tmc_sgt.Z = stepperZ.sgt(); + #endif + #endif EEPROM_WRITE(tmc_sgt); // @@ -1369,15 +1344,15 @@ void MarlinSettings::postprocess() { if (!validating) reset_stepper_drivers(); // - // TMC2130 Stepper Settings + // TMC Stepper Settings // _FIELD_TEST(tmc_stepper_current); #if HAS_TRINAMIC - #define SET_CURR(Q) stepper##Q.setCurrent(currents[TMC_##Q] ? currents[TMC_##Q] : Q##_CURRENT, R_SENSE, HOLD_MULTIPLIER) - uint16_t currents[TMC_AXES]; + #define SET_CURR(Q) stepper##Q.rms_current(currents.Q ? currents.Q : Q##_CURRENT) + tmc_stepper_current_t currents; EEPROM_READ(currents); if (!validating) { #if AXIS_IS_TMC(X) @@ -1420,8 +1395,8 @@ void MarlinSettings::postprocess() { #endif #if ENABLED(HYBRID_THRESHOLD) - #define TMC_SET_PWMTHRS(A,Q) tmc_set_pwmthrs(stepper##Q, tmc_hybrid_threshold[TMC_##Q], planner.axis_steps_per_mm[_AXIS(A)]) - uint32_t tmc_hybrid_threshold[TMC_AXES]; + #define TMC_SET_PWMTHRS(A,Q) tmc_set_pwmthrs(stepper##Q, tmc_hybrid_threshold.Q, planner.axis_steps_per_mm[_AXIS(A)]) + tmc_hybrid_threshold_t tmc_hybrid_threshold; EEPROM_READ(tmc_hybrid_threshold); if (!validating) { #if AXIS_HAS_STEALTHCHOP(X) @@ -1464,37 +1439,37 @@ void MarlinSettings::postprocess() { #endif /* - * TMC2130 Sensorless homing threshold. + * TMC Sensorless homing threshold. * X and X2 use the same value * Y and Y2 use the same value * Z and Z2 use the same value */ - int16_t tmc_sgt[XYZ]; + tmc_sgt_t tmc_sgt; EEPROM_READ(tmc_sgt); #if ENABLED(SENSORLESS_HOMING) if (!validating) { #ifdef X_HOMING_SENSITIVITY #if AXIS_HAS_STALLGUARD(X) - stepperX.sgt(tmc_sgt[0]); + stepperX.sgt(tmc_sgt.X); #endif #if AXIS_HAS_STALLGUARD(X2) - stepperX2.sgt(tmc_sgt[0]); + stepperX2.sgt(tmc_sgt.X); #endif #endif #ifdef Y_HOMING_SENSITIVITY #if AXIS_HAS_STALLGUARD(Y) - stepperY.sgt(tmc_sgt[1]); + stepperY.sgt(tmc_sgt.Y); #endif #if AXIS_HAS_STALLGUARD(Y2) - stepperY2.sgt(tmc_sgt[1]); + stepperY2.sgt(tmc_sgt.Y); #endif #endif #ifdef Z_HOMING_SENSITIVITY #if AXIS_HAS_STALLGUARD(Z) - stepperZ.sgt(tmc_sgt[2]); + stepperZ.sgt(tmc_sgt.Z); #endif #if AXIS_HAS_STALLGUARD(Z2) - stepperZ2.sgt(tmc_sgt[2]); + stepperZ2.sgt(tmc_sgt.Z); #endif #endif } @@ -1838,8 +1813,7 @@ void MarlinSettings::reset() { #endif #if ENABLED(DELTA) - const float adj[ABC] = DELTA_ENDSTOP_ADJ, - dta[ABC] = DELTA_TOWER_ANGLE_TRIM; + const float adj[ABC] = DELTA_ENDSTOP_ADJ, dta[ABC] = DELTA_TOWER_ANGLE_TRIM; delta_height = DELTA_HEIGHT; COPY(delta_endstop_adj, adj); delta_radius = DELTA_RADIUS; @@ -2552,7 +2526,7 @@ void MarlinSettings::reset() { #if HAS_TRINAMIC /** - * TMC2130 / TMC2208 stepper driver current + * TMC stepper driver current */ if (!forReplay) { CONFIG_ECHO_START; @@ -2563,57 +2537,59 @@ void MarlinSettings::reset() { say_M906(); #endif #if AXIS_IS_TMC(X) - SERIAL_ECHOPAIR(" X", stepperX.getCurrent()); + SERIAL_ECHOPAIR(" X", stepperX.getMilliamps()); #endif #if AXIS_IS_TMC(Y) - SERIAL_ECHOPAIR(" Y", stepperY.getCurrent()); + SERIAL_ECHOPAIR(" Y", stepperY.getMilliamps()); #endif #if AXIS_IS_TMC(Z) - SERIAL_ECHOPAIR(" Z", stepperZ.getCurrent()); + SERIAL_ECHOPAIR(" Z", stepperZ.getMilliamps()); #endif #if AXIS_IS_TMC(X) || AXIS_IS_TMC(Y) || AXIS_IS_TMC(Z) SERIAL_EOL(); #endif + #if AXIS_IS_TMC(X2) || AXIS_IS_TMC(Y2) || AXIS_IS_TMC(Z2) say_M906(); SERIAL_ECHOPGM(" I1"); #endif #if AXIS_IS_TMC(X2) - SERIAL_ECHOPAIR(" X", stepperX2.getCurrent()); + SERIAL_ECHOPAIR(" X", stepperX2.getMilliamps()); #endif #if AXIS_IS_TMC(Y2) - SERIAL_ECHOPAIR(" Y", stepperY2.getCurrent()); + SERIAL_ECHOPAIR(" Y", stepperY2.getMilliamps()); #endif #if AXIS_IS_TMC(Z2) - SERIAL_ECHOPAIR(" Z", stepperZ2.getCurrent()); + SERIAL_ECHOPAIR(" Z", stepperZ2.getMilliamps()); #endif #if AXIS_IS_TMC(X2) || AXIS_IS_TMC(Y2) || AXIS_IS_TMC(Z2) SERIAL_EOL(); #endif + #if AXIS_IS_TMC(E0) say_M906(); - SERIAL_ECHOLNPAIR(" T0 E", stepperE0.getCurrent()); + SERIAL_ECHOLNPAIR(" T0 E", stepperE0.getMilliamps()); #endif #if E_STEPPERS > 1 && AXIS_IS_TMC(E1) say_M906(); - SERIAL_ECHOLNPAIR(" T1 E", stepperE1.getCurrent()); + SERIAL_ECHOLNPAIR(" T1 E", stepperE1.getMilliamps()); #endif #if E_STEPPERS > 2 && AXIS_IS_TMC(E2) say_M906(); - SERIAL_ECHOLNPAIR(" T2 E", stepperE2.getCurrent()); + SERIAL_ECHOLNPAIR(" T2 E", stepperE2.getMilliamps()); #endif #if E_STEPPERS > 3 && AXIS_IS_TMC(E3) say_M906(); - SERIAL_ECHOLNPAIR(" T3 E", stepperE3.getCurrent()); + SERIAL_ECHOLNPAIR(" T3 E", stepperE3.getMilliamps()); #endif #if E_STEPPERS > 4 && AXIS_IS_TMC(E4) say_M906(); - SERIAL_ECHOLNPAIR(" T4 E", stepperE4.getCurrent()); + SERIAL_ECHOLNPAIR(" T4 E", stepperE4.getMilliamps()); #endif SERIAL_EOL(); /** - * TMC2130 / TMC2208 / TRAMS Hybrid Threshold + * TMC Hybrid Threshold */ #if ENABLED(HYBRID_THRESHOLD) if (!forReplay) { @@ -2621,54 +2597,56 @@ void MarlinSettings::reset() { SERIAL_ECHOLNPGM("Hybrid Threshold:"); } CONFIG_ECHO_START; - #if AXIS_IS_TMC(X) || AXIS_IS_TMC(Y) || AXIS_IS_TMC(Z) + #if AXIS_HAS_STEALTHCHOP(X) || AXIS_HAS_STEALTHCHOP(Y) || AXIS_HAS_STEALTHCHOP(Z) say_M913(); #endif - #if AXIS_IS_TMC(X) + #if AXIS_HAS_STEALTHCHOP(X) SERIAL_ECHOPAIR(" X", TMC_GET_PWMTHRS(X, X)); #endif - #if AXIS_IS_TMC(Y) + #if AXIS_HAS_STEALTHCHOP(Y) SERIAL_ECHOPAIR(" Y", TMC_GET_PWMTHRS(Y, Y)); #endif - #if AXIS_IS_TMC(Z) + #if AXIS_HAS_STEALTHCHOP(Z) SERIAL_ECHOPAIR(" Z", TMC_GET_PWMTHRS(Z, Z)); #endif - #if AXIS_IS_TMC(X) || AXIS_IS_TMC(Y) || AXIS_IS_TMC(Z) + #if AXIS_HAS_STEALTHCHOP(X) || AXIS_HAS_STEALTHCHOP(Y) || AXIS_HAS_STEALTHCHOP(Z) SERIAL_EOL(); #endif - #if AXIS_IS_TMC(X2) || AXIS_IS_TMC(Y2) || AXIS_IS_TMC(Z2) + + #if AXIS_HAS_STEALTHCHOP(X2) || AXIS_HAS_STEALTHCHOP(Y2) || AXIS_HAS_STEALTHCHOP(Z2) say_M913(); SERIAL_ECHOPGM(" I1"); #endif - #if AXIS_IS_TMC(X2) + #if AXIS_HAS_STEALTHCHOP(X2) SERIAL_ECHOPAIR(" X", TMC_GET_PWMTHRS(X, X2)); #endif - #if AXIS_IS_TMC(Y2) + #if AXIS_HAS_STEALTHCHOP(Y2) SERIAL_ECHOPAIR(" Y", TMC_GET_PWMTHRS(Y, Y2)); #endif - #if AXIS_IS_TMC(Z2) + #if AXIS_HAS_STEALTHCHOP(Z2) SERIAL_ECHOPAIR(" Z", TMC_GET_PWMTHRS(Z, Z2)); #endif - #if AXIS_IS_TMC(X2) || AXIS_IS_TMC(Y2) || AXIS_IS_TMC(Z2) + #if AXIS_HAS_STEALTHCHOP(X2) || AXIS_HAS_STEALTHCHOP(Y2) || AXIS_HAS_STEALTHCHOP(Z2) SERIAL_EOL(); #endif - #if AXIS_IS_TMC(E0) + + #if AXIS_HAS_STEALTHCHOP(E0) say_M913(); SERIAL_ECHOLNPAIR(" T0 E", TMC_GET_PWMTHRS(E, E0)); #endif - #if E_STEPPERS > 1 && AXIS_IS_TMC(E1) + #if E_STEPPERS > 1 && AXIS_HAS_STEALTHCHOP(E1) say_M913(); SERIAL_ECHOLNPAIR(" T1 E", TMC_GET_PWMTHRS(E, E1)); #endif - #if E_STEPPERS > 2 && AXIS_IS_TMC(E2) + #if E_STEPPERS > 2 && AXIS_HAS_STEALTHCHOP(E2) say_M913(); SERIAL_ECHOLNPAIR(" T2 E", TMC_GET_PWMTHRS(E, E2)); #endif - #if E_STEPPERS > 3 && AXIS_IS_TMC(E3) + #if E_STEPPERS > 3 && AXIS_HAS_STEALTHCHOP(E3) say_M913(); SERIAL_ECHOLNPAIR(" T3 E", TMC_GET_PWMTHRS(E, E3)); #endif - #if E_STEPPERS > 4 && AXIS_IS_TMC(E4) + #if E_STEPPERS > 4 && AXIS_HAS_STEALTHCHOP(E4) say_M913(); SERIAL_ECHOLNPAIR(" T4 E", TMC_GET_PWMTHRS(E, E4)); #endif @@ -2676,7 +2654,7 @@ void MarlinSettings::reset() { #endif // HYBRID_THRESHOLD /** - * TMC2130 Sensorless homing thresholds + * TMC Sensorless homing thresholds */ #if ENABLED(SENSORLESS_HOMING) if (!forReplay) { diff --git a/Marlin/drivers.h b/Marlin/drivers.h index 3a8ff4a..5efefe7 100644 --- a/Marlin/drivers.h +++ b/Marlin/drivers.h @@ -31,9 +31,9 @@ #define TB6560 0x005 #define TB6600 0x006 #define TMC2100 0x007 -#define TMC2130 0x108 +#define TMC2130 2130 #define TMC2130_STANDALONE 0x008 -#define TMC2208 0x109 +#define TMC2208 2208 #define TMC2208_STANDALONE 0x009 #define TMC26X 0x10A #define TMC26X_STANDALONE 0x00A @@ -45,7 +45,7 @@ #define AXIS_DRIVER_TYPE_X(T) _AXIS_DRIVER_TYPE(X,T) #define AXIS_DRIVER_TYPE_Y(T) _AXIS_DRIVER_TYPE(Y,T) #define AXIS_DRIVER_TYPE_Z(T) _AXIS_DRIVER_TYPE(Z,T) -#define AXIS_DRIVER_TYPE_X2(T) (ENABLED(X_DUAL_STEPPER_DRIVERS) || ENABLED(DUAL_X_CARRIAGE)) && _AXIS_DRIVER_TYPE(X2,T) +#define AXIS_DRIVER_TYPE_X2(T) ((ENABLED(X_DUAL_STEPPER_DRIVERS) || ENABLED(DUAL_X_CARRIAGE)) && _AXIS_DRIVER_TYPE(X2,T)) #define AXIS_DRIVER_TYPE_Y2(T) (ENABLED(Y_DUAL_STEPPER_DRIVERS) && _AXIS_DRIVER_TYPE(Y2,T)) #define AXIS_DRIVER_TYPE_Z2(T) (ENABLED(Z_DUAL_STEPPER_DRIVERS) && _AXIS_DRIVER_TYPE(Z2,T)) #define AXIS_DRIVER_TYPE_E0(T) (E_STEPPERS > 0 && _AXIS_DRIVER_TYPE(E0,T)) diff --git a/Marlin/language.h b/Marlin/language.h index d021a90..9dbcc0e 100644 --- a/Marlin/language.h +++ b/Marlin/language.h @@ -289,6 +289,9 @@ #define MSG_B "Y" #define MSG_C "Z" #endif +#define MSG_X2 "X2" +#define MSG_Y2 "Y2" +#define MSG_Z2 "Z2" #define MSG_H1 "1" #define MSG_H2 "2" #define MSG_H3 "3" @@ -299,6 +302,7 @@ #define MSG_N3 " 3" #define MSG_N4 " 4" #define MSG_N5 " 5" +#define MSG_E0 "E0" #define MSG_E1 "E1" #define MSG_E2 "E2" #define MSG_E3 "E3" diff --git a/Marlin/stepper_indirection.cpp b/Marlin/stepper_indirection.cpp index cde4c31..c75ae03 100644 --- a/Marlin/stepper_indirection.cpp +++ b/Marlin/stepper_indirection.cpp @@ -122,156 +122,98 @@ } #endif // TMC26X +#if HAS_TRINAMIC + #define _TMC_INIT(ST, SPMM) tmc_init(stepper##ST, ST##_CURRENT, ST##_MICROSTEPS, ST##_HYBRID_THRESHOLD, SPMM) +#endif + // // TMC2130 Driver objects and inits // #if HAS_DRIVER(TMC2130) #include - #include #include "planner.h" #include "enum.h" - #if TMC2130STEPPER_VERSION < 0x020201 - #error "Update TMC2130Stepper library to 2.2.1 or newer." - #endif - #if ENABLED(TMC_USE_SW_SPI) - #define _TMC2130_DEFINE(ST) TMC2130Stepper stepper##ST(ST##_ENABLE_PIN, ST##_DIR_PIN, ST##_STEP_PIN, ST##_CS_PIN, TMC_SW_MOSI, TMC_SW_MISO, TMC_SW_SCK) + #define _TMC2130_DEFINE(ST, L) TMCMarlin stepper##ST(ST##_CS_PIN, R_SENSE, TMC_SW_MOSI, TMC_SW_MISO, TMC_SW_SCK) + #define TMC2130_DEFINE(ST) _TMC2130_DEFINE(ST, TMC_##ST##_LABEL) #else - #define _TMC2130_DEFINE(ST) TMC2130Stepper stepper##ST(ST##_ENABLE_PIN, ST##_DIR_PIN, ST##_STEP_PIN, ST##_CS_PIN) + #define _TMC2130_DEFINE(ST, L) TMCMarlin stepper##ST(ST##_CS_PIN, R_SENSE) + #define TMC2130_DEFINE(ST) _TMC2130_DEFINE(ST, TMC_##ST##_LABEL) #endif - // Stepper objects of TMC2130 steppers used #if AXIS_DRIVER_TYPE(X, TMC2130) - _TMC2130_DEFINE(X); + TMC2130_DEFINE(X); #endif #if AXIS_DRIVER_TYPE(X2, TMC2130) - _TMC2130_DEFINE(X2); + TMC2130_DEFINE(X2); #endif #if AXIS_DRIVER_TYPE(Y, TMC2130) - _TMC2130_DEFINE(Y); + TMC2130_DEFINE(Y); #endif #if AXIS_DRIVER_TYPE(Y2, TMC2130) - _TMC2130_DEFINE(Y2); + TMC2130_DEFINE(Y2); #endif #if AXIS_DRIVER_TYPE(Z, TMC2130) - _TMC2130_DEFINE(Z); + TMC2130_DEFINE(Z); #endif #if AXIS_DRIVER_TYPE(Z2, TMC2130) - _TMC2130_DEFINE(Z2); + TMC2130_DEFINE(Z2); #endif #if AXIS_DRIVER_TYPE(E0, TMC2130) - _TMC2130_DEFINE(E0); + TMC2130_DEFINE(E0); #endif #if AXIS_DRIVER_TYPE(E1, TMC2130) - _TMC2130_DEFINE(E1); + TMC2130_DEFINE(E1); #endif #if AXIS_DRIVER_TYPE(E2, TMC2130) - _TMC2130_DEFINE(E2); + TMC2130_DEFINE(E2); #endif #if AXIS_DRIVER_TYPE(E3, TMC2130) - _TMC2130_DEFINE(E3); + TMC2130_DEFINE(E3); #endif #if AXIS_DRIVER_TYPE(E4, TMC2130) - _TMC2130_DEFINE(E4); + TMC2130_DEFINE(E4); #endif - // Use internal reference voltage for current calculations. This is the default. - // Following values from Trinamic's spreadsheet with values for a NEMA17 (42BYGHW609) - // https://www.trinamic.com/products/integrated-circuits/details/tmc2130/ - void tmc2130_init(TMC2130Stepper &st, const uint16_t mA, const uint16_t microsteps, const uint32_t thrs, const float spmm) { + template + void tmc_init(TMCMarlin &st, const uint16_t mA, const uint16_t microsteps, const uint32_t thrs, const float spmm) { #if DISABLED(STEALTHCHOP) || DISABLED(HYBRID_THRESHOLD) UNUSED(thrs); UNUSED(spmm); #endif st.begin(); - st.setCurrent(mA, R_SENSE, HOLD_MULTIPLIER); + + CHOPCONF_t chopconf{0}; + chopconf.tbl = 1; + chopconf.toff = 3; + chopconf.intpol = INTERPOLATE; + chopconf.hstrt = 2; + chopconf.hend = 5; + st.CHOPCONF(chopconf.sr); + + st.rms_current(mA, HOLD_MULTIPLIER); st.microsteps(microsteps); - st.blank_time(24); - st.off_time(5); // Only enables the driver if used with stealthChop - st.interpolate(INTERPOLATE); - st.power_down_delay(128); // ~2s until driver lowers to hold current - st.hysteresis_start(3); - st.hysteresis_end(2); + st.iholddelay(10); + st.TPOWERDOWN(128); // ~2s until driver lowers to hold current + #if ENABLED(STEALTHCHOP) - st.stealth_freq(1); // f_pwm = 2/683 f_clk - st.stealth_autoscale(1); - st.stealth_gradient(5); - st.stealth_amplitude(255); - st.stealthChop(1); - #if ENABLED(HYBRID_THRESHOLD) - st.stealth_max_speed(12650000UL*microsteps/(256*thrs*spmm)); + st.en_pwm_mode(true); + + PWMCONF_t pwmconf{0}; + pwmconf.pwm_freq = 0b01; // f_pwm = 2/683 f_clk + pwmconf.pwm_autoscale = true; + pwmconf.pwm_grad = 5; + pwmconf.pwm_ampl = 180; + st.PWMCONF(pwmconf.sr); + + #if ENABLED(HYBRID_THRESHOLD) + st.TPWMTHRS(12650000UL*microsteps/(256*thrs*spmm)); #endif #endif st.GSTAT(); // Clear GSTAT } - - #define _TMC2130_INIT(ST, SPMM) tmc2130_init(stepper##ST, ST##_CURRENT, ST##_MICROSTEPS, ST##_HYBRID_THRESHOLD, SPMM) - - void tmc2130_init_to_defaults() { - #if AXIS_DRIVER_TYPE(X, TMC2130) - _TMC2130_INIT( X, planner.axis_steps_per_mm[X_AXIS]); - #endif - #if AXIS_DRIVER_TYPE(X2, TMC2130) - _TMC2130_INIT(X2, planner.axis_steps_per_mm[X_AXIS]); - #endif - #if AXIS_DRIVER_TYPE(Y, TMC2130) - _TMC2130_INIT( Y, planner.axis_steps_per_mm[Y_AXIS]); - #endif - #if AXIS_DRIVER_TYPE(Y2, TMC2130) - _TMC2130_INIT(Y2, planner.axis_steps_per_mm[Y_AXIS]); - #endif - #if AXIS_DRIVER_TYPE(Z, TMC2130) - _TMC2130_INIT( Z, planner.axis_steps_per_mm[Z_AXIS]); - #endif - #if AXIS_DRIVER_TYPE(Z2, TMC2130) - _TMC2130_INIT(Z2, planner.axis_steps_per_mm[Z_AXIS]); - #endif - #if AXIS_DRIVER_TYPE(E0, TMC2130) - _TMC2130_INIT(E0, planner.axis_steps_per_mm[E_AXIS]); - #endif - #if AXIS_DRIVER_TYPE(E1, TMC2130) - { constexpr int extruder = 1; _TMC2130_INIT(E1, planner.axis_steps_per_mm[E_AXIS_N]); } - #endif - #if AXIS_DRIVER_TYPE(E2, TMC2130) - { constexpr int extruder = 2; _TMC2130_INIT(E2, planner.axis_steps_per_mm[E_AXIS_N]); } - #endif - #if AXIS_DRIVER_TYPE(E3, TMC2130) - { constexpr int extruder = 3; _TMC2130_INIT(E3, planner.axis_steps_per_mm[E_AXIS_N]); } - #endif - #if AXIS_DRIVER_TYPE(E4, TMC2130) - { constexpr int extruder = 4; _TMC2130_INIT(E4, planner.axis_steps_per_mm[E_AXIS_N]); } - #endif - - #if ENABLED(SENSORLESS_HOMING) - #define TMC_INIT_SGT(P,Q) stepper##Q.sgt(P##_HOMING_SENSITIVITY); - #if X_SENSORLESS - #if AXIS_DRIVER_TYPE(X, TMC2130) - stepperX.sgt(X_HOMING_SENSITIVITY); - #endif - #if AXIS_DRIVER_TYPE(X2, TMC2130) - stepperX2.sgt(X_HOMING_SENSITIVITY); - #endif - #endif - #if Y_SENSORLESS - #if AXIS_DRIVER_TYPE(Y, TMC2130) - stepperY.sgt(Y_HOMING_SENSITIVITY); - #endif - #if AXIS_DRIVER_TYPE(Y2, TMC2130) - stepperY2.sgt(Y_HOMING_SENSITIVITY); - #endif - #endif - #if Z_SENSORLESS - #if AXIS_DRIVER_TYPE(Z, TMC2130) - stepperZ.sgt(Z_HOMING_SENSITIVITY); - #endif - #if AXIS_DRIVER_TYPE(Z2, TMC2130) - stepperZ2.sgt(Z_HOMING_SENSITIVITY); - #endif - #endif - #endif - } #endif // TMC2130 // @@ -282,92 +224,90 @@ #undef HardwareSerial_h // undo Marlin trickery #include #include - #include #include "planner.h" - #if TMC2208STEPPER_VERSION < 0x000101 - #error "Update TMC2208Stepper library to 0.1.1 or newer." - #endif + #define _TMC2208_DEFINE_HARDWARE(ST, L) TMCMarlin stepper##ST(&ST##_HARDWARE_SERIAL, R_SENSE) + #define TMC2208_DEFINE_HARDWARE(ST) _TMC2208_DEFINE_HARDWARE(ST, TMC_##ST##_LABEL) - #define _TMC2208_DEFINE_HARDWARE(ST) TMC2208Stepper stepper##ST(&ST##_HARDWARE_SERIAL) - #define _TMC2208_DEFINE_SOFTWARE(ST) TMC2208Stepper stepper##ST(ST##_SERIAL_RX_PIN, ST##_SERIAL_TX_PIN, ST##_SERIAL_RX_PIN > -1) + #define _TMC2208_DEFINE_SOFTWARE(ST, L) TMCMarlin stepper##ST(ST##_SERIAL_RX_PIN, ST##_SERIAL_TX_PIN, R_SENSE, ST##_SERIAL_RX_PIN > -1) + #define TMC2208_DEFINE_SOFTWARE(ST) _TMC2208_DEFINE_SOFTWARE(ST, TMC_##ST##_LABEL) // Stepper objects of TMC2208 steppers used #if AXIS_DRIVER_TYPE(X, TMC2208) #ifdef X_HARDWARE_SERIAL - _TMC2208_DEFINE_HARDWARE(X); + TMC2208_DEFINE_HARDWARE(X); #else - _TMC2208_DEFINE_SOFTWARE(X); + TMC2208_DEFINE_SOFTWARE(X); #endif #endif #if AXIS_DRIVER_TYPE(X2, TMC2208) #ifdef X2_HARDWARE_SERIAL - _TMC2208_DEFINE_HARDWARE(X2); + TMC2208_DEFINE_HARDWARE(X2); #else - _TMC2208_DEFINE_SOFTWARE(X2); + TMC2208_DEFINE_SOFTWARE(X2); #endif #endif #if AXIS_DRIVER_TYPE(Y, TMC2208) #ifdef Y_HARDWARE_SERIAL - _TMC2208_DEFINE_HARDWARE(Y); + TMC2208_DEFINE_HARDWARE(Y); #else - _TMC2208_DEFINE_SOFTWARE(Y); + TMC2208_DEFINE_SOFTWARE(Y); #endif #endif #if AXIS_DRIVER_TYPE(Y2, TMC2208) #ifdef Y2_HARDWARE_SERIAL - _TMC2208_DEFINE_HARDWARE(Y2); + TMC2208_DEFINE_HARDWARE(Y2); #else - _TMC2208_DEFINE_SOFTWARE(Y2); + TMC2208_DEFINE_SOFTWARE(Y2); #endif #endif #if AXIS_DRIVER_TYPE(Z, TMC2208) #ifdef Z_HARDWARE_SERIAL - _TMC2208_DEFINE_HARDWARE(Z); + TMC2208_DEFINE_HARDWARE(Z); #else - _TMC2208_DEFINE_SOFTWARE(Z); + TMC2208_DEFINE_SOFTWARE(Z); #endif #endif #if AXIS_DRIVER_TYPE(Z2, TMC2208) #ifdef Z2_HARDWARE_SERIAL - _TMC2208_DEFINE_HARDWARE(Z2); + TMC2208_DEFINE_HARDWARE(Z2); #else - _TMC2208_DEFINE_SOFTWARE(Z2); + TMC2208_DEFINE_SOFTWARE(Z2); #endif #endif #if AXIS_DRIVER_TYPE(E0, TMC2208) #ifdef E0_HARDWARE_SERIAL - _TMC2208_DEFINE_HARDWARE(E0); + TMC2208_DEFINE_HARDWARE(E0); #else - _TMC2208_DEFINE_SOFTWARE(E0); + TMC2208_DEFINE_SOFTWARE(E0); #endif #endif #if AXIS_DRIVER_TYPE(E1, TMC2208) #ifdef E1_HARDWARE_SERIAL - _TMC2208_DEFINE_HARDWARE(E1); + TMC2208_DEFINE_HARDWARE(E1); #else - _TMC2208_DEFINE_SOFTWARE(E1); + TMC2208_DEFINE_SOFTWARE(E1); #endif #endif #if AXIS_DRIVER_TYPE(E2, TMC2208) #ifdef E2_HARDWARE_SERIAL - _TMC2208_DEFINE_HARDWARE(E2); + TMC2208_DEFINE_HARDWARE(E2); #else - _TMC2208_DEFINE_SOFTWARE(E2); + TMC2208_DEFINE_SOFTWARE(E2); #endif #endif #if AXIS_DRIVER_TYPE(E3, TMC2208) #ifdef E3_HARDWARE_SERIAL - _TMC2208_DEFINE_HARDWARE(E3); + TMC2208_DEFINE_HARDWARE(E3); #else - _TMC2208_DEFINE_SOFTWARE(E3); + TMC2208_DEFINE_SOFTWARE(E3); #endif #endif #if AXIS_DRIVER_TYPE(E4, TMC2208) #ifdef E4_HARDWARE_SERIAL - _TMC2208_DEFINE_HARDWARE(E4); + TMC2208_DEFINE_HARDWARE(E4); #else - _TMC2208_DEFINE_SOFTWARE(E4); + TMC2208_DEFINE_SOFTWARE(E4); #endif #endif @@ -451,79 +391,52 @@ #endif } - // Use internal reference voltage for current calculations. This is the default. - // Following values from Trinamic's spreadsheet with values for a NEMA17 (42BYGHW609) - void tmc2208_init(TMC2208Stepper &st, const uint16_t mA, const uint16_t microsteps, const uint32_t thrs, const float spmm) { - st.pdn_disable(true); // Use UART - st.mstep_reg_select(true); // Select microsteps with UART - st.I_scale_analog(false); - st.rms_current(mA, HOLD_MULTIPLIER, R_SENSE); + template + void tmc_init(TMCMarlin &st, const uint16_t mA, const uint16_t microsteps, const uint32_t thrs, const float spmm) { + #if DISABLED(STEALTHCHOP) || DISABLED(HYBRID_THRESHOLD) + UNUSED(thrs); + UNUSED(spmm); + #endif + + TMC2208_n::GCONF_t gconf{0}; + gconf.pdn_disable = true; // Use UART + gconf.mstep_reg_select = true; // Select microsteps with UART + gconf.i_scale_analog = false; + + TMC2208_n::CHOPCONF_t chopconf{0}; + chopconf.tbl = 0b01; // blank_time = 24 + chopconf.toff = 5; + chopconf.intpol = INTERPOLATE; + chopconf.hstrt = 2; + chopconf.hend = 5; + st.CHOPCONF(chopconf.sr); + + st.rms_current(mA, HOLD_MULTIPLIER); st.microsteps(microsteps); - st.blank_time(24); - st.toff(5); - st.intpol(INTERPOLATE); + st.iholddelay(10); st.TPOWERDOWN(128); // ~2s until driver lowers to hold current - st.hysteresis_start(3); - st.hysteresis_end(2); #if ENABLED(STEALTHCHOP) - st.pwm_lim(12); - st.pwm_reg(8); - st.pwm_autograd(1); - st.pwm_autoscale(1); - st.pwm_freq(1); - st.pwm_grad(14); - st.pwm_ofs(36); - st.en_spreadCycle(false); + gconf.en_spreadcycle = false; + + TMC2208_n::PWMCONF_t pwmconf{0}; + pwmconf.pwm_lim = 12; + pwmconf.pwm_reg = 8; + pwmconf.pwm_autograd = true; + pwmconf.pwm_autoscale = true; + pwmconf.pwm_freq = 0b01; + pwmconf.pwm_grad = 14; + pwmconf.pwm_ofs = 36; + st.PWMCONF(pwmconf.sr); #if ENABLED(HYBRID_THRESHOLD) st.TPWMTHRS(12650000UL*microsteps/(256*thrs*spmm)); - #else - UNUSED(thrs); - UNUSED(spmm); #endif #else - st.en_spreadCycle(true); + gconf.en_spreadcycle = true; #endif + st.GCONF(gconf.sr); st.GSTAT(0b111); // Clear delay(200); } - - #define _TMC2208_INIT(ST, SPMM) tmc2208_init(stepper##ST, ST##_CURRENT, ST##_MICROSTEPS, ST##_HYBRID_THRESHOLD, SPMM) - - void tmc2208_init_to_defaults() { - #if AXIS_DRIVER_TYPE(X, TMC2208) - _TMC2208_INIT(X, planner.axis_steps_per_mm[X_AXIS]); - #endif - #if AXIS_DRIVER_TYPE(X2, TMC2208) - _TMC2208_INIT(X2, planner.axis_steps_per_mm[X_AXIS]); - #endif - #if AXIS_DRIVER_TYPE(Y, TMC2208) - _TMC2208_INIT(Y, planner.axis_steps_per_mm[Y_AXIS]); - #endif - #if AXIS_DRIVER_TYPE(Y2, TMC2208) - _TMC2208_INIT(Y2, planner.axis_steps_per_mm[Y_AXIS]); - #endif - #if AXIS_DRIVER_TYPE(Z, TMC2208) - _TMC2208_INIT(Z, planner.axis_steps_per_mm[Z_AXIS]); - #endif - #if AXIS_DRIVER_TYPE(Z2, TMC2208) - _TMC2208_INIT(Z2, planner.axis_steps_per_mm[Z_AXIS]); - #endif - #if AXIS_DRIVER_TYPE(E0, TMC2208) - _TMC2208_INIT(E0, planner.axis_steps_per_mm[E_AXIS]); - #endif - #if AXIS_DRIVER_TYPE(E1, TMC2208) - { constexpr int extruder = 1; _TMC2208_INIT(E1, planner.axis_steps_per_mm[E_AXIS_N]); } - #endif - #if AXIS_DRIVER_TYPE(E2, TMC2208) - { constexpr int extruder = 2; _TMC2208_INIT(E2, planner.axis_steps_per_mm[E_AXIS_N]); } - #endif - #if AXIS_DRIVER_TYPE(E3, TMC2208) - { constexpr int extruder = 3; _TMC2208_INIT(E3, planner.axis_steps_per_mm[E_AXIS_N]); } - #endif - #if AXIS_DRIVER_TYPE(E4, TMC2208) - { constexpr int extruder = 4; _TMC2208_INIT(E4, planner.axis_steps_per_mm[E_AXIS_N]); } - #endif - } #endif // TMC2208 void restore_stepper_drivers() { @@ -566,20 +479,75 @@ void reset_stepper_drivers() { #if HAS_DRIVER(TMC26X) tmc26x_init_to_defaults(); #endif - #if HAS_DRIVER(TMC2130) - delay(100); - tmc2130_init_to_defaults(); + #if ENABLED(HAVE_L6470DRIVER) + L6470_init_to_defaults(); #endif - #if HAS_DRIVER(TMC2208) - delay(100); - tmc2208_init_to_defaults(); + + #if AXIS_IS_TMC(X) + _TMC_INIT(X, planner.axis_steps_per_mm[X_AXIS]); #endif + #if AXIS_IS_TMC(X2) + _TMC_INIT(X2, planner.axis_steps_per_mm[X_AXIS]); + #endif + #if AXIS_IS_TMC(Y) + _TMC_INIT(Y, planner.axis_steps_per_mm[Y_AXIS]); + #endif + #if AXIS_IS_TMC(Y2) + _TMC_INIT(Y2, planner.axis_steps_per_mm[Y_AXIS]); + #endif + #if AXIS_IS_TMC(Z) + _TMC_INIT(Z, planner.axis_steps_per_mm[Z_AXIS]); + #endif + #if AXIS_IS_TMC(Z2) + _TMC_INIT(Z2, planner.axis_steps_per_mm[Z_AXIS]); + #endif + #if AXIS_IS_TMC(E0) + _TMC_INIT(E0, planner.axis_steps_per_mm[E_AXIS]); + #endif + #if AXIS_IS_TMC(E1) + { constexpr uint8_t extruder = 1; _TMC_INIT(E1, planner.axis_steps_per_mm[E_AXIS_N]); UNUSED(extruder); } + #endif + #if AXIS_IS_TMC(E2) + { constexpr uint8_t extruder = 2; _TMC_INIT(E2, planner.axis_steps_per_mm[E_AXIS_N]); UNUSED(extruder); } + #endif + #if AXIS_IS_TMC(E3) + { constexpr uint8_t extruder = 3; _TMC_INIT(E3, planner.axis_steps_per_mm[E_AXIS_N]); UNUSED(extruder); } + #endif + #if AXIS_IS_TMC(E4) + { constexpr uint8_t extruder = 4; _TMC_INIT(E4, planner.axis_steps_per_mm[E_AXIS_N]); UNUSED(extruder); } + #endif + + #if ENABLED(SENSORLESS_HOMING) + #if X_SENSORLESS + #if AXIS_HAS_STALLGUARD(X) + stepperX.sgt(X_STALL_SENSITIVITY); + #endif + #if AXIS_HAS_STALLGUARD(X2) + stepperX2.sgt(X_STALL_SENSITIVITY); + #endif + #endif + #if Y_SENSORLESS + #if AXIS_HAS_STALLGUARD(Y) + stepperY.sgt(Y_STALL_SENSITIVITY); + #endif + #if AXIS_HAS_STALLGUARD(Y2) + stepperY2.sgt(Y_STALL_SENSITIVITY); + #endif + #endif + #if Z_SENSORLESS + #if AXIS_HAS_STALLGUARD(Z) + stepperZ.sgt(Z_STALL_SENSITIVITY); + #endif + #if AXIS_HAS_STALLGUARD(Z2) + stepperZ2.sgt(Z_STALL_SENSITIVITY); + #endif + #endif + #endif + #ifdef TMC_ADV TMC_ADV() #endif - #if HAS_DRIVER(L6470) - L6470_init_to_defaults(); - #endif + stepper.set_directions(); } diff --git a/Marlin/stepper_indirection.h b/Marlin/stepper_indirection.h index 54a17c5..83a2c9a 100644 --- a/Marlin/stepper_indirection.h +++ b/Marlin/stepper_indirection.h @@ -53,15 +53,20 @@ void tmc26x_init_to_defaults(); #endif -#if HAS_DRIVER(TMC2130) - #include - void tmc2130_init_to_defaults(); -#endif +#if HAS_TRINAMIC + #include + #include "tmc_util.h" + #if TMCSTEPPER_VERSION < 0x000001 + #error "Update TMCStepper library to 0.0.1 or newer." + #endif -#if HAS_DRIVER(TMC2208) - #include - void tmc2208_serial_begin(); - void tmc2208_init_to_defaults(); + #define __TMC_CLASS(MODEL, A, I) TMCMarlin + #define _TMC_CLASS(MODEL, L) __TMC_CLASS(MODEL, L) + #define TMC_CLASS(ST) _TMC_CLASS(ST##_DRIVER_TYPE, TMC_##ST##_LABEL) + + #if HAS_DRIVER(TMC2208) + void tmc2208_serial_begin(); + #endif #endif // L6470 has STEP on normal pins, but DIR/ENABLE via SPI @@ -84,17 +89,15 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset #define X_DIR_WRITE(STATE) stepperX.Step_Clock(STATE) #define X_DIR_READ (stepperX.getStatus() & STATUS_DIR) #else + #if AXIS_IS_TMC(X) + extern TMC_CLASS(X) stepperX; + #endif #if AXIS_DRIVER_TYPE(X, TMC26X) extern TMC26XStepper stepperX; #define X_ENABLE_INIT NOOP #define X_ENABLE_WRITE(STATE) stepperX.setEnabled(STATE) #define X_ENABLE_READ stepperX.isEnabled() #else - #if AXIS_DRIVER_TYPE(X, TMC2130) - extern TMC2130Stepper stepperX; - #elif AXIS_DRIVER_TYPE(X, TMC2208) - extern TMC2208Stepper stepperX; - #endif #define X_ENABLE_INIT SET_OUTPUT(X_ENABLE_PIN) #define X_ENABLE_WRITE(STATE) WRITE(X_ENABLE_PIN,STATE) #define X_ENABLE_READ READ(X_ENABLE_PIN) @@ -117,17 +120,15 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset #define Y_DIR_WRITE(STATE) stepperY.Step_Clock(STATE) #define Y_DIR_READ (stepperY.getStatus() & STATUS_DIR) #else + #if AXIS_IS_TMC(Y) + extern TMC_CLASS(Y) stepperY; + #endif #if AXIS_DRIVER_TYPE(Y, TMC26X) extern TMC26XStepper stepperY; #define Y_ENABLE_INIT NOOP #define Y_ENABLE_WRITE(STATE) stepperY.setEnabled(STATE) #define Y_ENABLE_READ stepperY.isEnabled() #else - #if AXIS_DRIVER_TYPE(Y, TMC2130) - extern TMC2130Stepper stepperY; - #elif AXIS_DRIVER_TYPE(Y, TMC2208) - extern TMC2208Stepper stepperY; - #endif #define Y_ENABLE_INIT SET_OUTPUT(Y_ENABLE_PIN) #define Y_ENABLE_WRITE(STATE) WRITE(Y_ENABLE_PIN,STATE) #define Y_ENABLE_READ READ(Y_ENABLE_PIN) @@ -150,17 +151,15 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset #define Z_DIR_WRITE(STATE) stepperZ.Step_Clock(STATE) #define Z_DIR_READ (stepperZ.getStatus() & STATUS_DIR) #else + #if AXIS_IS_TMC(Z) + extern TMC_CLASS(Z) stepperZ; + #endif #if AXIS_DRIVER_TYPE(Z, TMC26X) extern TMC26XStepper stepperZ; #define Z_ENABLE_INIT NOOP #define Z_ENABLE_WRITE(STATE) stepperZ.setEnabled(STATE) #define Z_ENABLE_READ stepperZ.isEnabled() #else - #if AXIS_DRIVER_TYPE(Z, TMC2130) - extern TMC2130Stepper stepperZ; - #elif AXIS_DRIVER_TYPE(Z, TMC2208) - extern TMC2208Stepper stepperZ; - #endif #define Z_ENABLE_INIT SET_OUTPUT(Z_ENABLE_PIN) #define Z_ENABLE_WRITE(STATE) WRITE(Z_ENABLE_PIN,STATE) #define Z_ENABLE_READ READ(Z_ENABLE_PIN) @@ -184,17 +183,15 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset #define X2_DIR_WRITE(STATE) stepperX2.Step_Clock(STATE) #define X2_DIR_READ (stepperX2.getStatus() & STATUS_DIR) #else + #if AXIS_IS_TMC(X2) + extern TMC_CLASS(X2) stepperX2; + #endif #if AXIS_DRIVER_TYPE(X2, TMC26X) extern TMC26XStepper stepperX2; #define X2_ENABLE_INIT NOOP #define X2_ENABLE_WRITE(STATE) stepperX2.setEnabled(STATE) #define X2_ENABLE_READ stepperX2.isEnabled() #else - #if AXIS_DRIVER_TYPE(X2, TMC2130) - extern TMC2130Stepper stepperX2; - #elif AXIS_DRIVER_TYPE(X2, TMC2208) - extern TMC2208Stepper stepperX2; - #endif #define X2_ENABLE_INIT SET_OUTPUT(X2_ENABLE_PIN) #define X2_ENABLE_WRITE(STATE) WRITE(X2_ENABLE_PIN,STATE) #define X2_ENABLE_READ READ(X2_ENABLE_PIN) @@ -219,17 +216,15 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset #define Y2_DIR_WRITE(STATE) stepperY2.Step_Clock(STATE) #define Y2_DIR_READ (stepperY2.getStatus() & STATUS_DIR) #else + #if AXIS_IS_TMC(Y2) + extern TMC_CLASS(Y2) stepperY2; + #endif #if AXIS_DRIVER_TYPE(Y2, TMC26X) extern TMC26XStepper stepperY2; #define Y2_ENABLE_INIT NOOP #define Y2_ENABLE_WRITE(STATE) stepperY2.setEnabled(STATE) #define Y2_ENABLE_READ stepperY2.isEnabled() #else - #if AXIS_DRIVER_TYPE(Y2, TMC2130) - extern TMC2130Stepper stepperY2; - #elif AXIS_DRIVER_TYPE(Y2, TMC2208) - extern TMC2208Stepper stepperY2; - #endif #define Y2_ENABLE_INIT SET_OUTPUT(Y2_ENABLE_PIN) #define Y2_ENABLE_WRITE(STATE) WRITE(Y2_ENABLE_PIN,STATE) #define Y2_ENABLE_READ READ(Y2_ENABLE_PIN) @@ -254,17 +249,15 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset #define Z2_DIR_WRITE(STATE) stepperZ2.Step_Clock(STATE) #define Z2_DIR_READ (stepperZ2.getStatus() & STATUS_DIR) #else + #if AXIS_IS_TMC(Z2) + extern TMC_CLASS(Z2) stepperZ2; + #endif #if AXIS_DRIVER_TYPE(Z2, TMC26X) extern TMC26XStepper stepperZ2; #define Z2_ENABLE_INIT NOOP #define Z2_ENABLE_WRITE(STATE) stepperZ2.setEnabled(STATE) #define Z2_ENABLE_READ stepperZ2.isEnabled() #else - #if AXIS_DRIVER_TYPE(Z2, TMC2130) - extern TMC2130Stepper stepperZ2; - #elif AXIS_DRIVER_TYPE(Z2, TMC2208) - extern TMC2208Stepper stepperZ2; - #endif #define Z2_ENABLE_INIT SET_OUTPUT(Z2_ENABLE_PIN) #define Z2_ENABLE_WRITE(STATE) WRITE(Z2_ENABLE_PIN,STATE) #define Z2_ENABLE_READ READ(Z2_ENABLE_PIN) @@ -288,17 +281,15 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset #define E0_DIR_WRITE(STATE) stepperE0.Step_Clock(STATE) #define E0_DIR_READ (stepperE0.getStatus() & STATUS_DIR) #else + #if AXIS_IS_TMC(E0) + extern TMC_CLASS(E0) stepperE0; + #endif #if AXIS_DRIVER_TYPE(E0, TMC26X) extern TMC26XStepper stepperE0; #define E0_ENABLE_INIT NOOP #define E0_ENABLE_WRITE(STATE) stepperE0.setEnabled(STATE) #define E0_ENABLE_READ stepperE0.isEnabled() #else - #if AXIS_DRIVER_TYPE(E0, TMC2130) - extern TMC2130Stepper stepperE0; - #elif AXIS_DRIVER_TYPE(E0, TMC2208) - extern TMC2208Stepper stepperE0; - #endif #define E0_ENABLE_INIT SET_OUTPUT(E0_ENABLE_PIN) #define E0_ENABLE_WRITE(STATE) WRITE(E0_ENABLE_PIN,STATE) #define E0_ENABLE_READ READ(E0_ENABLE_PIN) @@ -321,17 +312,15 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset #define E1_DIR_WRITE(STATE) stepperE1.Step_Clock(STATE) #define E1_DIR_READ (stepperE1.getStatus() & STATUS_DIR) #else + #if AXIS_IS_TMC(E1) + extern TMC_CLASS(E1) stepperE1; + #endif #if AXIS_DRIVER_TYPE(E1, TMC26X) extern TMC26XStepper stepperE1; #define E1_ENABLE_INIT NOOP #define E1_ENABLE_WRITE(STATE) stepperE1.setEnabled(STATE) #define E1_ENABLE_READ stepperE1.isEnabled() #else - #if AXIS_DRIVER_TYPE(E1, TMC2130) - extern TMC2130Stepper stepperE1; - #elif AXIS_DRIVER_TYPE(E1, TMC2208) - extern TMC2208Stepper stepperE1; - #endif #define E1_ENABLE_INIT SET_OUTPUT(E1_ENABLE_PIN) #define E1_ENABLE_WRITE(STATE) WRITE(E1_ENABLE_PIN,STATE) #define E1_ENABLE_READ READ(E1_ENABLE_PIN) @@ -354,17 +343,15 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset #define E2_DIR_WRITE(STATE) stepperE2.Step_Clock(STATE) #define E2_DIR_READ (stepperE2.getStatus() & STATUS_DIR) #else + #if AXIS_IS_TMC(E2) + extern TMC_CLASS(E2) stepperE2; + #endif #if AXIS_DRIVER_TYPE(E2, TMC26X) extern TMC26XStepper stepperE2; #define E2_ENABLE_INIT NOOP #define E2_ENABLE_WRITE(STATE) stepperE2.setEnabled(STATE) #define E2_ENABLE_READ stepperE2.isEnabled() #else - #if AXIS_DRIVER_TYPE(E2, TMC2130) - extern TMC2130Stepper stepperE2; - #elif AXIS_DRIVER_TYPE(E2, TMC2208) - extern TMC2208Stepper stepperE2; - #endif #define E2_ENABLE_INIT SET_OUTPUT(E2_ENABLE_PIN) #define E2_ENABLE_WRITE(STATE) WRITE(E2_ENABLE_PIN,STATE) #define E2_ENABLE_READ READ(E2_ENABLE_PIN) @@ -387,17 +374,15 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset #define E3_DIR_WRITE(STATE) stepperE3.Step_Clock(STATE) #define E3_DIR_READ (stepperE3.getStatus() & STATUS_DIR) #else + #if AXIS_IS_TMC(E3) + extern TMC_CLASS(E3) stepperE3; + #endif #if AXIS_DRIVER_TYPE(E3, TMC26X) extern TMC26XStepper stepperE3; #define E3_ENABLE_INIT NOOP #define E3_ENABLE_WRITE(STATE) stepperE3.setEnabled(STATE) #define E3_ENABLE_READ stepperE3.isEnabled() #else - #if AXIS_DRIVER_TYPE(E3, TMC2130) - extern TMC2130Stepper stepperE3; - #elif AXIS_DRIVER_TYPE(E3, TMC2208) - extern TMC2208Stepper stepperE3; - #endif #define E3_ENABLE_INIT SET_OUTPUT(E3_ENABLE_PIN) #define E3_ENABLE_WRITE(STATE) WRITE(E3_ENABLE_PIN,STATE) #define E3_ENABLE_READ READ(E3_ENABLE_PIN) @@ -420,17 +405,15 @@ void reset_stepper_drivers(); // Called by settings.load / settings.reset #define E4_DIR_WRITE(STATE) stepperE4.Step_Clock(STATE) #define E4_DIR_READ (stepperE4.getStatus() & STATUS_DIR) #else + #if AXIS_IS_TMC(E4) + extern TMC_CLASS(E4) stepperE4; + #endif #if AXIS_DRIVER_TYPE(E4, TMC26X) extern TMC26XStepper stepperE4; #define E4_ENABLE_INIT NOOP #define E4_ENABLE_WRITE(STATE) stepperE4.setEnabled(STATE) #define E4_ENABLE_READ stepperE4.isEnabled() #else - #if AXIS_DRIVER_TYPE(E4, TMC2130) - extern TMC2130Stepper stepperE4; - #elif AXIS_DRIVER_TYPE(E4, TMC2208) - extern TMC2208Stepper stepperE4; - #endif #define E4_ENABLE_INIT SET_OUTPUT(E4_ENABLE_PIN) #define E4_ENABLE_WRITE(STATE) WRITE(E4_ENABLE_PIN,STATE) #define E4_ENABLE_READ READ(E4_ENABLE_PIN) diff --git a/Marlin/tmc_util.cpp b/Marlin/tmc_util.cpp index 2e9ba55..20793a3 100644 --- a/Marlin/tmc_util.cpp +++ b/Marlin/tmc_util.cpp @@ -34,8 +34,6 @@ #include "planner.h" #endif -bool report_tmc_status = false; - /** * Check for over temperature or short to ground error flags. * Report and log warning of overtemperature condition. @@ -44,6 +42,8 @@ bool report_tmc_status = false; * and so we don't repeatedly report warning before the condition is cleared. */ #if ENABLED(MONITOR_DRIVER_STATUS) + static bool report_tmc_status = false; + struct TMC_driver_data { uint32_t drv_status; bool is_otpw; @@ -93,13 +93,13 @@ bool report_tmc_status = false; #endif template - void monitor_tmc_driver(TMC &st, const TMC_AxisEnum axis, uint8_t &otpw_cnt) { + void monitor_tmc_driver(TMC &st) { TMC_driver_data data = get_driver_data(st); #if ENABLED(STOP_ON_ERROR) if (data.is_error) { SERIAL_EOL(); - _tmc_say_axis(axis); + st.printLabel(); SERIAL_ECHOLNPGM(" driver error detected:"); if (data.is_ot) SERIAL_ECHOLNPGM("overtemperature"); if (st.s2ga()) SERIAL_ECHOLNPGM("short to ground (coil A)"); @@ -112,7 +112,7 @@ bool report_tmc_status = false; #endif // Report if a warning was triggered - if (data.is_otpw && otpw_cnt == 0) { + if (data.is_otpw && st.otpw_count == 0) { char timestamp[10]; duration_t elapsed = print_job_timer.duration(); const bool has_days = (elapsed.value > 60*60*24L); @@ -120,38 +120,38 @@ bool report_tmc_status = false; SERIAL_EOL(); SERIAL_ECHO(timestamp); SERIAL_ECHOPGM(": "); - _tmc_say_axis(axis); + st.printLabel(); SERIAL_ECHOPGM(" driver overtemperature warning! ("); - SERIAL_ECHO(st.getCurrent()); + SERIAL_ECHO(st.getMilliamps()); SERIAL_ECHOLNPGM("mA)"); } #if CURRENT_STEP_DOWN > 0 // Decrease current if is_otpw is true and driver is enabled and there's been more than 4 warnings - if (data.is_otpw && st.isEnabled() && otpw_cnt > 4) { - st.setCurrent(st.getCurrent() - CURRENT_STEP_DOWN, R_SENSE, HOLD_MULTIPLIER); + if (data.is_otpw && st.isEnabled() && st.otpw_cnt > 4) { + st.rms_current(st.getMilliamps() - (CURRENT_STEP_DOWN)); #if ENABLED(REPORT_CURRENT_CHANGE) - _tmc_say_axis(axis); - SERIAL_ECHOLNPAIR(" current decreased to ", st.getCurrent()); + st.printLabel(); + SERIAL_ECHOLNPAIR(" current decreased to ", st.getMilliamps()); #endif } #endif if (data.is_otpw) { - otpw_cnt++; + st.otpw_count++; st.flag_otpw = true; } - else if (otpw_cnt > 0) otpw_cnt = 0; + else if (st.otpw_count > 0) st.otpw_count = 0; if (report_tmc_status) { const uint32_t pwm_scale = get_pwm_scale(st); - _tmc_say_axis(axis); + st.printLabel(); SERIAL_ECHOPAIR(":", pwm_scale); SERIAL_ECHOPGM(" |0b"); SERIAL_PRINT(get_status_response(st), BIN); SERIAL_ECHOPGM("| "); if (data.is_error) SERIAL_CHAR('E'); else if (data.is_ot) SERIAL_CHAR('O'); else if (data.is_otpw) SERIAL_CHAR('W'); - else if (otpw_cnt > 0) SERIAL_PRINT(otpw_cnt, DEC); + else if (st.otpw_count > 0) SERIAL_PRINT(st.otpw_count, DEC); else if (st.flag_otpw) SERIAL_CHAR('F'); SERIAL_CHAR('\t'); } @@ -164,48 +164,37 @@ bool report_tmc_status = false; if (ELAPSED(millis(), next_cOT)) { next_cOT = millis() + 500; #if HAS_HW_COMMS(X) - static uint8_t x_otpw_cnt = 0; - monitor_tmc_driver(stepperX, TMC_X, x_otpw_cnt); + monitor_tmc_driver(stepperX); #endif #if HAS_HW_COMMS(Y) - static uint8_t y_otpw_cnt = 0; - monitor_tmc_driver(stepperY, TMC_Y, y_otpw_cnt); + monitor_tmc_driver(stepperY); #endif #if HAS_HW_COMMS(Z) - static uint8_t z_otpw_cnt = 0; - monitor_tmc_driver(stepperZ, TMC_Z, z_otpw_cnt); + monitor_tmc_driver(stepperZ); #endif #if HAS_HW_COMMS(X2) - static uint8_t x2_otpw_cnt = 0; - monitor_tmc_driver(stepperX2, TMC_X, x2_otpw_cnt); + monitor_tmc_driver(stepperX2); #endif #if HAS_HW_COMMS(Y2) - static uint8_t y2_otpw_cnt = 0; - monitor_tmc_driver(stepperY2, TMC_Y, y2_otpw_cnt); + monitor_tmc_driver(stepperY2); #endif #if HAS_HW_COMMS(Z2) - static uint8_t z2_otpw_cnt = 0; - monitor_tmc_driver(stepperZ2, TMC_Z, z2_otpw_cnt); + monitor_tmc_driver(stepperZ2); #endif #if HAS_HW_COMMS(E0) - static uint8_t e0_otpw_cnt = 0; - monitor_tmc_driver(stepperE0, TMC_E0, e0_otpw_cnt); + monitor_tmc_driver(stepperE0); #endif #if HAS_HW_COMMS(E1) - static uint8_t e1_otpw_cnt = 0; - monitor_tmc_driver(stepperE1, TMC_E1, e1_otpw_cnt); + monitor_tmc_driver(stepperE1); #endif #if HAS_HW_COMMS(E2) - static uint8_t e2_otpw_cnt = 0; - monitor_tmc_driver(stepperE2, TMC_E2, e2_otpw_cnt); + monitor_tmc_driver(stepperE2); #endif #if HAS_HW_COMMS(E3) - static uint8_t e3_otpw_cnt = 0; - monitor_tmc_driver(stepperE3, TMC_E3, e3_otpw_cnt); + monitor_tmc_driver(stepperE3); #endif #if HAS_HW_COMMS(E4) - static uint8_t e4_otpw_cnt = 0; - monitor_tmc_driver(stepperE4, TMC_E4, e4_otpw_cnt); + monitor_tmc_driver(stepperE4); #endif if (report_tmc_status) SERIAL_EOL(); @@ -214,40 +203,6 @@ bool report_tmc_status = false; #endif // MONITOR_DRIVER_STATUS -void _tmc_say_axis(const TMC_AxisEnum axis) { - static const char ext_X[] PROGMEM = "X", ext_Y[] PROGMEM = "Y", ext_Z[] PROGMEM = "Z", - ext_X2[] PROGMEM = "X2", ext_Y2[] PROGMEM = "Y2", ext_Z2[] PROGMEM = "Z2", - ext_E0[] PROGMEM = "E0", ext_E1[] PROGMEM = "E1", - ext_E2[] PROGMEM = "E2", ext_E3[] PROGMEM = "E3", - ext_E4[] PROGMEM = "E4"; - static const char* const tmc_axes[] PROGMEM = { ext_X, ext_Y, ext_Z, ext_X2, ext_Y2, ext_Z2, ext_E0, ext_E1, ext_E2, ext_E3, ext_E4 }; - serialprintPGM((char*)pgm_read_ptr(&tmc_axes[axis])); -} - -void _tmc_say_current(const TMC_AxisEnum axis, const uint16_t curr) { - _tmc_say_axis(axis); - SERIAL_ECHOLNPAIR(" driver current: ", curr); -} -void _tmc_say_otpw(const TMC_AxisEnum axis, const bool otpw) { - _tmc_say_axis(axis); - SERIAL_ECHOPGM(" temperature prewarn triggered: "); - serialprintPGM(otpw ? PSTR("true") : PSTR("false")); - SERIAL_EOL(); -} -void _tmc_say_otpw_cleared(const TMC_AxisEnum axis) { - _tmc_say_axis(axis); - SERIAL_ECHOLNPGM(" prewarn flag cleared"); -} -void _tmc_say_pwmthrs(const TMC_AxisEnum axis, const uint32_t thrs) { - _tmc_say_axis(axis); - SERIAL_ECHOLNPAIR(" stealthChop max speed: ", thrs); -} -void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt) { - _tmc_say_axis(axis); - SERIAL_ECHOPGM(" homing sensitivity: "); - SERIAL_PRINTLN(sgt, DEC); -} - #if ENABLED(TMC_DEBUG) enum TMC_debug_enum : char { @@ -296,9 +251,7 @@ void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt) { TMC_S2VSB, TMC_S2VSA }; - static void drv_status_print_hex(const TMC_AxisEnum axis, const uint32_t drv_status) { - _tmc_say_axis(axis); - SERIAL_ECHOPGM(" = 0x"); + static void drv_status_print_hex(const uint32_t drv_status) { for (int B = 24; B >= 8; B -= 8){ SERIAL_PRINT((drv_status >> (B + 4)) & 0xF, HEX); SERIAL_PRINT((drv_status >> B) & 0xF, HEX); @@ -309,17 +262,19 @@ void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt) { SERIAL_EOL(); } + template + static void print_vsense(TMC &st) { serialprintPGM(st.vsense() ? PSTR("1=.18") : PSTR("0=.325")); } + #if HAS_DRIVER(TMC2130) static void tmc_status(TMC2130Stepper &st, const TMC_debug_enum i) { switch (i) { case TMC_PWM_SCALE: SERIAL_PRINT(st.PWM_SCALE(), DEC); break; - case TMC_TSTEP: SERIAL_ECHO(st.TSTEP()); break; case TMC_SGT: SERIAL_PRINT(st.sgt(), DEC); break; - case TMC_STEALTHCHOP: serialprintPGM(st.stealthChop() ? PSTR("true") : PSTR("false")); break; + case TMC_STEALTHCHOP: serialprintPGM(st.en_pwm_mode() ? PSTR("true") : PSTR("false")); break; default: break; } } - static void tmc_parse_drv_status(TMC2130Stepper &st, const TMC_drv_status_enum i) { + static void _tmc_parse_drv_status(TMC2130Stepper &st, const TMC_drv_status_enum i) { switch (i) { case TMC_STALLGUARD: if (st.stallguard()) SERIAL_CHAR('X'); break; case TMC_SG_RESULT: SERIAL_PRINT(st.sg_result(), DEC); break; @@ -332,7 +287,6 @@ void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt) { #if HAS_DRIVER(TMC2208) static void tmc_status(TMC2208Stepper &st, const TMC_debug_enum i) { switch (i) { - case TMC_TSTEP: { uint32_t data = 0; st.TSTEP(&data); SERIAL_PROTOCOL(data); break; } case TMC_PWM_SCALE: SERIAL_PRINT(st.pwm_scale_sum(), DEC); break; case TMC_STEALTHCHOP: serialprintPGM(st.stealth() ? PSTR("true") : PSTR("false")); break; case TMC_S2VSA: if (st.s2vsa()) SERIAL_CHAR('X'); break; @@ -340,7 +294,7 @@ void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt) { default: break; } } - static void tmc_parse_drv_status(TMC2208Stepper &st, const TMC_drv_status_enum i) { + static void _tmc_parse_drv_status(TMC2208Stepper &st, const TMC_drv_status_enum i) { switch (i) { case TMC_T157: if (st.t157()) SERIAL_CHAR('X'); break; case TMC_T150: if (st.t150()) SERIAL_CHAR('X'); break; @@ -352,12 +306,12 @@ void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt) { #endif template - static void tmc_status(TMC &st, const TMC_AxisEnum axis, const TMC_debug_enum i, const float spmm) { + static void tmc_status(TMC &st, const TMC_debug_enum i, const float spmm) { SERIAL_ECHO('\t'); switch (i) { - case TMC_CODES: _tmc_say_axis(axis); break; + case TMC_CODES: st.printLabel(); break; case TMC_ENABLED: serialprintPGM(st.isEnabled() ? PSTR("true") : PSTR("false")); break; - case TMC_CURRENT: SERIAL_ECHO(st.getCurrent()); break; + case TMC_CURRENT: SERIAL_ECHO(st.getMilliamps()); break; case TMC_RMS_CURRENT: SERIAL_PROTOCOL(st.rms_current()); break; case TMC_MAX_CURRENT: SERIAL_PRINT((float)st.rms_current() * 1.41, 0); break; case TMC_IRUN: @@ -372,10 +326,9 @@ void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt) { SERIAL_PRINT(st.cs_actual(), DEC); SERIAL_ECHOPGM("/31"); break; - - case TMC_VSENSE: serialprintPGM(st.vsense() ? PSTR("1=.18") : PSTR("0=.325")); break; - + case TMC_VSENSE: print_vsense(st); break; case TMC_MICROSTEPS: SERIAL_ECHO(st.microsteps()); break; + case TMC_TSTEP: SERIAL_ECHO(st.TSTEP()); break; case TMC_TPWMTHRS: { uint32_t tpwmthrs_val = st.TPWMTHRS(); SERIAL_ECHO(tpwmthrs_val); @@ -390,7 +343,9 @@ void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt) { } break; case TMC_OTPW: serialprintPGM(st.otpw() ? PSTR("true") : PSTR("false")); break; - case TMC_OTPW_TRIGGERED: serialprintPGM(st.getOTPW() ? PSTR("true") : PSTR("false")); break; + #if ENABLED(MONITOR_DRIVER_STATUS) + case TMC_OTPW_TRIGGERED: serialprintPGM(st.getOTPW() ? PSTR("true") : PSTR("false")); break; + #endif case TMC_TOFF: SERIAL_PRINT(st.toff(), DEC); break; case TMC_TBL: SERIAL_PRINT(st.blank_time(), DEC); break; case TMC_HEND: SERIAL_PRINT(st.hysteresis_end(), DEC); break; @@ -400,10 +355,10 @@ void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt) { } template - static void tmc_parse_drv_status(TMC &st, const TMC_AxisEnum axis, const TMC_drv_status_enum i) { + static void tmc_parse_drv_status(TMC &st, const TMC_drv_status_enum i) { SERIAL_CHAR('\t'); switch (i) { - case TMC_DRV_CODES: _tmc_say_axis(axis); break; + case TMC_DRV_CODES: st.printLabel(); break; case TMC_STST: if (st.stst()) SERIAL_CHAR('X'); break; case TMC_OLB: if (st.olb()) SERIAL_CHAR('X'); break; case TMC_OLA: if (st.ola()) SERIAL_CHAR('X'); break; @@ -412,59 +367,63 @@ void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt) { case TMC_DRV_OTPW: if (st.otpw()) SERIAL_CHAR('X'); break; case TMC_OT: if (st.ot()) SERIAL_CHAR('X'); break; case TMC_DRV_CS_ACTUAL: SERIAL_PRINT(st.cs_actual(), DEC); break; - case TMC_DRV_STATUS_HEX:drv_status_print_hex(axis, st.DRV_STATUS()); break; - default: tmc_parse_drv_status(st, i); break; + case TMC_DRV_STATUS_HEX: + st.printLabel(); + SERIAL_ECHOPGM("\t0x"); + drv_status_print_hex(st.DRV_STATUS()); + break; + default: _tmc_parse_drv_status(st, i); break; } } static void tmc_debug_loop(const TMC_debug_enum i) { #if AXIS_IS_TMC(X) - tmc_status(stepperX, TMC_X, i, planner.axis_steps_per_mm[X_AXIS]); + tmc_status(stepperX, i, planner.axis_steps_per_mm[X_AXIS]); #endif #if AXIS_IS_TMC(X2) - tmc_status(stepperX2, TMC_X2, i, planner.axis_steps_per_mm[X_AXIS]); + tmc_status(stepperX2, i, planner.axis_steps_per_mm[X_AXIS]); #endif #if AXIS_IS_TMC(Y) - tmc_status(stepperY, TMC_Y, i, planner.axis_steps_per_mm[Y_AXIS]); + tmc_status(stepperY, i, planner.axis_steps_per_mm[Y_AXIS]); #endif #if AXIS_IS_TMC(Y2) - tmc_status(stepperY2, TMC_Y2, i, planner.axis_steps_per_mm[Y_AXIS]); + tmc_status(stepperY2, i, planner.axis_steps_per_mm[Y_AXIS]); #endif #if AXIS_IS_TMC(Z) - tmc_status(stepperZ, TMC_Z, i, planner.axis_steps_per_mm[Z_AXIS]); + tmc_status(stepperZ, i, planner.axis_steps_per_mm[Z_AXIS]); #endif #if AXIS_IS_TMC(Z2) - tmc_status(stepperZ2, TMC_Z2, i, planner.axis_steps_per_mm[Z_AXIS]); + tmc_status(stepperZ2, i, planner.axis_steps_per_mm[Z_AXIS]); #endif #if AXIS_IS_TMC(E0) - tmc_status(stepperE0, TMC_E0, i, planner.axis_steps_per_mm[E_AXIS]); + tmc_status(stepperE0, i, planner.axis_steps_per_mm[E_AXIS]); #endif #if AXIS_IS_TMC(E1) - tmc_status(stepperE1, TMC_E1, i, planner.axis_steps_per_mm[E_AXIS + tmc_status(stepperE1, i, planner.axis_steps_per_mm[E_AXIS #if ENABLED(DISTINCT_E_FACTORS) + 1 #endif ]); #endif #if AXIS_IS_TMC(E2) - tmc_status(stepperE2, TMC_E2, i, planner.axis_steps_per_mm[E_AXIS + tmc_status(stepperE2, i, planner.axis_steps_per_mm[E_AXIS #if ENABLED(DISTINCT_E_FACTORS) + 2 #endif ]); #endif #if AXIS_IS_TMC(E3) - tmc_status(stepperE3, TMC_E3, i, planner.axis_steps_per_mm[E_AXIS + tmc_status(stepperE3, i, planner.axis_steps_per_mm[E_AXIS #if ENABLED(DISTINCT_E_FACTORS) + 3 #endif ]); #endif #if AXIS_IS_TMC(E4) - tmc_status(stepperE4, TMC_E4, i, planner.axis_steps_per_mm[E_AXIS + tmc_status(stepperE4, i, planner.axis_steps_per_mm[E_AXIS #if ENABLED(DISTINCT_E_FACTORS) + 4 #endif @@ -476,40 +435,40 @@ void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt) { static void drv_status_loop(const TMC_drv_status_enum i) { #if AXIS_IS_TMC(X) - tmc_parse_drv_status(stepperX, TMC_X, i); + tmc_parse_drv_status(stepperX, i); #endif #if AXIS_IS_TMC(X2) - tmc_parse_drv_status(stepperX2, TMC_X2, i); + tmc_parse_drv_status(stepperX2, i); #endif #if AXIS_IS_TMC(Y) - tmc_parse_drv_status(stepperY, TMC_Y, i); + tmc_parse_drv_status(stepperY, i); #endif #if AXIS_IS_TMC(Y2) - tmc_parse_drv_status(stepperY2, TMC_Y2, i); + tmc_parse_drv_status(stepperY2, i); #endif #if AXIS_IS_TMC(Z) - tmc_parse_drv_status(stepperZ, TMC_Z, i); + tmc_parse_drv_status(stepperZ, i); #endif #if AXIS_IS_TMC(Z2) - tmc_parse_drv_status(stepperZ2, TMC_Z2, i); + tmc_parse_drv_status(stepperZ2, i); #endif #if AXIS_IS_TMC(E0) - tmc_parse_drv_status(stepperE0, TMC_E0, i); + tmc_parse_drv_status(stepperE0, i); #endif #if AXIS_IS_TMC(E1) - tmc_parse_drv_status(stepperE1, TMC_E1, i); + tmc_parse_drv_status(stepperE1, i); #endif #if AXIS_IS_TMC(E2) - tmc_parse_drv_status(stepperE2, TMC_E2, i); + tmc_parse_drv_status(stepperE2, i); #endif #if AXIS_IS_TMC(E3) - tmc_parse_drv_status(stepperE3, TMC_E3, i); + tmc_parse_drv_status(stepperE3, i); #endif #if AXIS_IS_TMC(E4) - tmc_parse_drv_status(stepperE4, TMC_E4, i); + tmc_parse_drv_status(stepperE4, i); #endif SERIAL_EOL(); @@ -518,10 +477,12 @@ void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt) { /** * M122 report functions */ - void tmc_set_report_status(const bool status) { - if ((report_tmc_status = status)) - SERIAL_ECHOLNPGM("axis:pwm_scale |status_response|"); - } + #if ENABLED(MONITOR_DRIVER_STATUS) + void tmc_set_report_status(const bool status) { + if ((report_tmc_status = status)) + SERIAL_ECHOLNPGM("axis:pwm_scale |status_response|"); + } + #endif void tmc_report_all() { #define TMC_REPORT(LABEL, ITEM) do{ SERIAL_ECHOPGM(LABEL); tmc_debug_loop(ITEM); }while(0) @@ -534,7 +495,7 @@ void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt) { TMC_REPORT("Run current", TMC_IRUN); TMC_REPORT("Hold current", TMC_IHOLD); TMC_REPORT("CS actual\t", TMC_CS_ACTUAL); - TMC_REPORT("PWM scale\t", TMC_PWM_SCALE); + TMC_REPORT("PWM scale", TMC_PWM_SCALE); TMC_REPORT("vsense\t", TMC_VSENSE); TMC_REPORT("stealthChop", TMC_STEALTHCHOP); TMC_REPORT("msteps\t", TMC_MICROSTEPS); @@ -571,7 +532,7 @@ void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt) { DRV_REPORT("s2vsa\t", TMC_S2VSA); DRV_REPORT("s2vsb\t", TMC_S2VSB); #endif - DRV_REPORT("Driver registers:", TMC_DRV_STATUS_HEX); + DRV_REPORT("Driver registers:\n",TMC_DRV_STATUS_HEX); SERIAL_EOL(); } @@ -580,9 +541,9 @@ void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt) { #if ENABLED(SENSORLESS_HOMING) void tmc_sensorless_homing(TMC2130Stepper &st, const bool enable/*=true*/) { - st.coolstep_min_speed(enable ? 1024UL * 1024UL - 1UL : 0); + st.TCOOLTHRS(enable ? 0xFFFFF : 0); #if ENABLED(STEALTHCHOP) - st.stealthChop(!enable); + st.en_pwm_mode(!enable); #endif st.diag1_stall(enable ? 1 : 0); } @@ -590,39 +551,40 @@ void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt) { #endif // SENSORLESS_HOMING #if HAS_DRIVER(TMC2130) + #define IS_TMC_SPI(ST) AXIS_DRIVER_TYPE(ST, TMC2130) #define SET_CS_PIN(st) OUT_WRITE(st##_CS_PIN, HIGH) void tmc_init_cs_pins() { - #if AXIS_DRIVER_TYPE(X, TMC2130) + #if IS_TMC_SPI(X) SET_CS_PIN(X); #endif - #if AXIS_DRIVER_TYPE(Y, TMC2130) + #if IS_TMC_SPI(Y) SET_CS_PIN(Y); #endif - #if AXIS_DRIVER_TYPE(Z, TMC2130) + #if IS_TMC_SPI(Z) SET_CS_PIN(Z); #endif - #if AXIS_DRIVER_TYPE(X2, TMC2130) + #if IS_TMC_SPI(X2) SET_CS_PIN(X2); #endif - #if AXIS_DRIVER_TYPE(Y2, TMC2130) + #if IS_TMC_SPI(Y2) SET_CS_PIN(Y2); #endif - #if AXIS_DRIVER_TYPE(Z2, TMC2130) + #if IS_TMC_SPI(Z2) SET_CS_PIN(Z2); #endif - #if AXIS_DRIVER_TYPE(E0, TMC2130) + #if IS_TMC_SPI(E0) SET_CS_PIN(E0); #endif - #if AXIS_DRIVER_TYPE(E1, TMC2130) + #if IS_TMC_SPI(E1) SET_CS_PIN(E1); #endif - #if AXIS_DRIVER_TYPE(E2, TMC2130) + #if IS_TMC_SPI(E2) SET_CS_PIN(E2); #endif - #if AXIS_DRIVER_TYPE(E3, TMC2130) + #if IS_TMC_SPI(E3) SET_CS_PIN(E3); #endif - #if AXIS_DRIVER_TYPE(E4, TMC2130) + #if IS_TMC_SPI(E4) SET_CS_PIN(E4); #endif } diff --git a/Marlin/tmc_util.h b/Marlin/tmc_util.h index 7efe67a..61591de 100644 --- a/Marlin/tmc_util.h +++ b/Marlin/tmc_util.h @@ -24,58 +24,129 @@ #define _TMC_UTIL_H_ #include "MarlinConfig.h" - -#if HAS_DRIVER(TMC2130) - #include +#if HAS_TRINAMIC + #include #endif -#if HAS_DRIVER(TMC2208) - #include -#endif +#define TMC_X_LABEL 'X', '0' +#define TMC_Y_LABEL 'Y', '0' +#define TMC_Z_LABEL 'Z', '0' -extern bool report_tmc_status; +#define TMC_X2_LABEL 'X', '2' +#define TMC_Y2_LABEL 'Y', '2' +#define TMC_Z2_LABEL 'Z', '2' -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 }; +#define TMC_E0_LABEL 'E', '0' +#define TMC_E1_LABEL 'E', '1' +#define TMC_E2_LABEL 'E', '2' +#define TMC_E3_LABEL 'E', '3' +#define TMC_E4_LABEL 'E', '4' + +template +class TMCStorage { + protected: + // Only a child class has access to constructor => Don't create on its own! "Poor man's abstract class" + TMCStorage() {} + + uint16_t val_mA = 0; + + public: + #if ENABLED(MONITOR_DRIVER_STATUS) + uint8_t otpw_count = 0; + bool flag_otpw = false; + bool getOTPW() { return flag_otpw; } + void clear_otpw() { flag_otpw = 0; } + #endif + + uint16_t getMilliamps() { return val_mA; } + + void printLabel() { + SERIAL_CHAR(AXIS_LETTER); + if (DRIVER_ID > '0') SERIAL_CHAR(DRIVER_ID); + } +}; + +template +class TMCMarlin : public TMC, public TMCStorage { + public: + TMCMarlin(uint16_t cs_pin, float RS) : + TMC(cs_pin, RS) + {} + TMCMarlin(uint16_t CS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK) : + TMC(CS, RS, pinMOSI, pinMISO, pinSCK) + {} + uint16_t rms_current() { return TMC::rms_current(); } + void rms_current(uint16_t mA) { + this->val_mA = mA; + TMC::rms_current(mA); + } + void rms_current(uint16_t mA, float mult) { + this->val_mA = mA; + TMC::rms_current(mA, mult); + } +}; +template +class TMCMarlin : public TMC2208Stepper, public TMCStorage { + public: + TMCMarlin(Stream * SerialPort, float RS, bool has_rx=true) : + TMC2208Stepper(SerialPort, RS, has_rx=true) + {} + TMCMarlin(uint16_t RX, uint16_t TX, float RS, bool has_rx=true) : + TMC2208Stepper(RX, TX, RS, has_rx=true) + {} + uint16_t rms_current() { return TMC2208Stepper::rms_current(); } + void rms_current(uint16_t mA) { + this->val_mA = mA; + TMC2208Stepper::rms_current(mA); + } + void rms_current(uint16_t mA, float mult) { + this->val_mA = mA; + TMC2208Stepper::rms_current(mA, mult); + } +}; constexpr uint32_t _tmc_thrs(const uint16_t msteps, const int32_t thrs, const uint32_t spmm) { return 12650000UL * msteps / (256 * thrs * spmm); } -void _tmc_say_axis(const TMC_AxisEnum axis); -void _tmc_say_current(const TMC_AxisEnum axis, const uint16_t curr); -void _tmc_say_otpw(const TMC_AxisEnum axis, const bool otpw); -void _tmc_say_otpw_cleared(const TMC_AxisEnum axis); -void _tmc_say_pwmthrs(const TMC_AxisEnum axis, const uint32_t thrs); -void _tmc_say_sgt(const TMC_AxisEnum axis, const int8_t sgt); - template -void tmc_get_current(TMC &st, const TMC_AxisEnum axis) { - _tmc_say_current(axis, st.getCurrent()); +void tmc_get_current(TMC &st) { + st.printLabel(); + SERIAL_ECHOLNPAIR(" driver current: ", st.getMilliamps()); } template void tmc_set_current(TMC &st, const int mA) { - st.setCurrent(mA, R_SENSE, HOLD_MULTIPLIER); + st.rms_current(mA); } +#if ENABLED(MONITOR_DRIVER_STATUS) + template + void tmc_report_otpw(TMC &st) { + st.printLabel(); + SERIAL_ECHOPGM(" temperature prewarn triggered: "); + serialprintPGM(st.getOTPW() ? PSTR("true") : PSTR("false")); + SERIAL_EOL(); + } + template + void tmc_clear_otpw(TMC &st) { + st.clear_otpw(); + st.printLabel(); + SERIAL_ECHOLNPGM(" prewarn flag cleared"); + } +#endif template -void tmc_report_otpw(TMC &st, const TMC_AxisEnum axis) { - _tmc_say_otpw(axis, st.getOTPW()); -} -template -void tmc_clear_otpw(TMC &st, const TMC_AxisEnum axis) { - st.clear_otpw(); - _tmc_say_otpw_cleared(axis); -} -template -void tmc_get_pwmthrs(TMC &st, const TMC_AxisEnum axis, const uint16_t spmm) { - _tmc_say_pwmthrs(axis, _tmc_thrs(st.microsteps(), st.TPWMTHRS(), spmm)); +void tmc_get_pwmthrs(TMC &st, const uint16_t spmm) { + st.printLabel(); + SERIAL_ECHOLNPAIR(" stealthChop max speed: ", _tmc_thrs(st.microsteps(), st.TPWMTHRS(), spmm)); } template void tmc_set_pwmthrs(TMC &st, const int32_t thrs, const uint32_t spmm) { st.TPWMTHRS(_tmc_thrs(st.microsteps(), thrs, spmm)); } template -void tmc_get_sgt(TMC &st, const TMC_AxisEnum axis) { - _tmc_say_sgt(axis, st.sgt()); +void tmc_get_sgt(TMC &st) { + st.printLabel(); + SERIAL_ECHOPGM(" homing sensitivity: "); + SERIAL_PRINTLN(st.sgt(), DEC); } template void tmc_set_sgt(TMC &st, const int8_t sgt_val) { diff --git a/platformio.ini b/platformio.ini index 9c2c71a..8b13608 100644 --- a/platformio.ini +++ b/platformio.ini @@ -29,8 +29,7 @@ build_flags = -fmax-errors=5 lib_deps = https://github.com/MarlinFirmware/U8glib-HAL/archive/dev.zip LiquidCrystal_I2C@1.1.2 - TMC2130Stepper - https://github.com/teemuatlut/TMC2208Stepper/archive/v0.1.1.zip + https://github.com/teemuatlut/TMCStepper.git Adafruit NeoPixel@1.1.3 https://github.com/lincomatic/LiquidTWI2/archive/30aa480.zip https://github.com/ameyer/Arduino-L6470/archive/master.zip