diff --git a/Marlin/src/inc/Conditionals-5-post.h b/Marlin/src/inc/Conditionals-5-post.h index 920031dcdb..fd06285bca 100644 --- a/Marlin/src/inc/Conditionals-5-post.h +++ b/Marlin/src/inc/Conditionals-5-post.h @@ -3012,7 +3012,7 @@ #undef MICROSTEP_MODES #endif -#if MB(PRINTRBOARD_G2) || ANY(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_PWM, HAS_MICROSTEPS) +#if ANY(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_PWM, HAS_MICROSTEPS) #define HAS_STEPPER_CONTROL 1 #endif diff --git a/Marlin/src/module/settings.cpp b/Marlin/src/module/settings.cpp index feb433041e..8c99c54323 100644 --- a/Marlin/src/module/settings.cpp +++ b/Marlin/src/module/settings.cpp @@ -508,7 +508,7 @@ typedef struct SettingsDataStruct { #endif // - // HAS_MOTOR_CURRENT_PWM + // Stepper Motors Current // #ifndef MOTOR_CURRENT_COUNT #if HAS_MOTOR_CURRENT_PWM diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index cc042181c6..1d02620e6f 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -120,10 +120,6 @@ Stepper stepper; // Singleton #include "../feature/dac/dac_dac084s085.h" #endif -#if HAS_MOTOR_CURRENT_SPI - #include -#endif - #if ENABLED(MIXING_EXTRUDER) #include "../feature/mixing.h" #endif @@ -158,14 +154,6 @@ Stepper stepper; // Singleton bool Stepper::separate_multi_axis = false; #endif -#if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM - bool Stepper::initialized; // = false - uint32_t Stepper::motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load - #if HAS_MOTOR_CURRENT_SPI - constexpr uint32_t Stepper::digipot_count[]; - #endif -#endif - stepper_flags_t Stepper::axis_enabled; // {0} // private: diff --git a/Marlin/src/module/stepper/control.cpp b/Marlin/src/module/stepper/control.cpp index 0d523c8ed5..4d4b6b351f 100644 --- a/Marlin/src/module/stepper/control.cpp +++ b/Marlin/src/module/stepper/control.cpp @@ -30,14 +30,24 @@ #if MB(PRINTRBOARD_G2) #include HAL_PATH(../.., fastio/G2_PWM.h) +#elif HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM + #define HAS_NON_G2_MOTOR_CURRENT 1 +#endif + +#if HAS_MOTOR_CURRENT_PWM || HAS_NON_G2_MOTOR_CURRENT + bool Stepper::initialized; // = false + uint32_t Stepper::motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load #endif /** - * Software-controlled Stepper Motor Current + * SPI-controlled Stepper Motor Current */ #if HAS_MOTOR_CURRENT_SPI + #include + constexpr uint32_t Stepper::digipot_count[]; + // From Arduino DigitalPotControl example void Stepper::set_digipot_value_spi(const int16_t address, const int16_t value) { WRITE(DIGIPOTSS_PIN, LOW); // Take the SS pin low to select the chip @@ -47,8 +57,36 @@ //delay(10); } + #if HAS_NON_G2_MOTOR_CURRENT + + void Stepper::set_digipot_current(const uint8_t driver, const int16_t current) { + if (WITHIN(driver, 0, COUNT(motor_current_setting) - 1)) + motor_current_setting[driver] = current; // update motor_current_setting + + if (!initialized) return; + + //SERIAL_ECHOLNPGM("Digipotss current ", current); + + const uint8_t digipot_ch[] = DIGIPOT_CHANNELS; + set_digipot_value_spi(digipot_ch[driver], current); + } + + void Stepper::digipot_init() { + SPI.begin(); + SET_OUTPUT(DIGIPOTSS_PIN); + + for (uint8_t i = 0; i < COUNT(motor_current_setting); ++i) + set_digipot_current(i, motor_current_setting[i]); + } + + #endif // HAS_NON_G2_MOTOR_CURRENT + #endif // HAS_MOTOR_CURRENT_SPI +/** + * PWM-controlled Stepper Motor Current + */ + #if HAS_MOTOR_CURRENT_PWM void Stepper::refresh_motor_power() { @@ -70,28 +108,13 @@ } } -#endif // HAS_MOTOR_CURRENT_PWM + #if HAS_NON_G2_MOTOR_CURRENT -/** - * PWM-controlled Stepper Motor Current - */ + void Stepper::set_digipot_current(const uint8_t driver, const int16_t current) { + if (WITHIN(driver, 0, COUNT(motor_current_setting) - 1)) + motor_current_setting[driver] = current; // update motor_current_setting -#if !MB(PRINTRBOARD_G2) && (HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM) - - void Stepper::set_digipot_current(const uint8_t driver, const int16_t current) { - if (WITHIN(driver, 0, COUNT(motor_current_setting) - 1)) - motor_current_setting[driver] = current; // update motor_current_setting - - if (!initialized) return; - - #if HAS_MOTOR_CURRENT_SPI - - //SERIAL_ECHOLNPGM("Digipotss current ", current); - - const uint8_t digipot_ch[] = DIGIPOT_CHANNELS; - set_digipot_value_spi(digipot_ch[driver], current); - - #elif HAS_MOTOR_CURRENT_PWM + if (!initialized) return; #define _WRITE_CURRENT_PWM(P) hal.set_pwm_duty(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE)) switch (driver) { @@ -141,21 +164,9 @@ #endif break; } - #endif - } - - void Stepper::digipot_init() { - - #if HAS_MOTOR_CURRENT_SPI - - SPI.begin(); - SET_OUTPUT(DIGIPOTSS_PIN); - - for (uint8_t i = 0; i < COUNT(motor_current_setting); ++i) - set_digipot_current(i, motor_current_setting[i]); - - #elif HAS_MOTOR_CURRENT_PWM + } + void Stepper::digipot_init() { #ifdef __SAM3X8E__ #define _RESET_CURRENT_PWM_FREQ(P) NOOP #else @@ -204,11 +215,11 @@ #endif refresh_motor_power(); + } - #endif - } + #endif // HAS_NON_G2_MOTOR_CURRENT -#endif +#endif // HAS_MOTOR_CURRENT_PWM /** * Software-controlled Microstepping with digital pins