🧑‍💻 Add stepper/control.cpp (2)

This commit is contained in:
Scott Lahteine 2025-04-20 11:27:15 -05:00
parent 4b32be9df0
commit d3687d933c
4 changed files with 51 additions and 52 deletions

View file

@ -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

View file

@ -508,7 +508,7 @@ typedef struct SettingsDataStruct {
#endif
//
// HAS_MOTOR_CURRENT_PWM
// Stepper Motors Current
//
#ifndef MOTOR_CURRENT_COUNT
#if HAS_MOTOR_CURRENT_PWM

View file

@ -120,10 +120,6 @@ Stepper stepper; // Singleton
#include "../feature/dac/dac_dac084s085.h"
#endif
#if HAS_MOTOR_CURRENT_SPI
#include <SPI.h>
#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:

View file

@ -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 <SPI.h>
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