Trinamic: Split stealthChop, improve driver monitoring, etc. (#12582)
This commit is contained in:
committed by
Scott Lahteine
parent
055cb2b956
commit
50b2fbd031
@ -48,35 +48,37 @@
|
||||
|
||||
struct TMC_driver_data {
|
||||
uint32_t drv_status;
|
||||
bool is_otpw;
|
||||
bool is_ot;
|
||||
bool is_error;
|
||||
bool is_otpw,
|
||||
is_ot,
|
||||
is_s2ga,
|
||||
is_s2gb,
|
||||
is_error;
|
||||
};
|
||||
#if HAS_DRIVER(TMC2130)
|
||||
#if ENABLED(TMC_DEBUG)
|
||||
static uint32_t get_pwm_scale(TMC2130Stepper &st) { return st.PWM_SCALE(); }
|
||||
static uint8_t get_status_response(TMC2130Stepper &st) { return st.status_response & 0xF; }
|
||||
static uint8_t get_status_response(TMC2130Stepper &st, uint32_t) { return st.status_response & 0xF; }
|
||||
#endif
|
||||
static TMC_driver_data get_driver_data(TMC2130Stepper &st) {
|
||||
constexpr uint32_t OTPW_bm = 0x4000000UL;
|
||||
constexpr uint8_t OTPW_bp = 26;
|
||||
constexpr uint32_t OT_bm = 0x2000000UL;
|
||||
constexpr uint8_t OT_bp = 25;
|
||||
constexpr uint8_t DRIVER_ERROR_bm = 0x2UL;
|
||||
constexpr uint8_t DRIVER_ERROR_bp = 1;
|
||||
constexpr uint8_t S2GA_bp = 27;
|
||||
constexpr uint8_t S2GB_bp = 28;
|
||||
TMC_driver_data data;
|
||||
data.drv_status = st.DRV_STATUS();
|
||||
data.is_otpw = (data.drv_status & OTPW_bm) >> OTPW_bp;
|
||||
data.is_ot = (data.drv_status & OT_bm) >> OT_bp;
|
||||
data.is_error = (st.status_response & DRIVER_ERROR_bm) >> DRIVER_ERROR_bp;
|
||||
data.is_s2ga = (data.drv_status >> S2GA_bp) & 0b1;
|
||||
data.is_s2gb = (data.drv_status >> S2GB_bp) & 0b1;
|
||||
return data;
|
||||
}
|
||||
#endif
|
||||
#if HAS_DRIVER(TMC2208)
|
||||
#if ENABLED(TMC_DEBUG)
|
||||
static uint32_t get_pwm_scale(TMC2208Stepper &st) { return st.pwm_scale_sum(); }
|
||||
static uint8_t get_status_response(TMC2208Stepper &st) {
|
||||
uint32_t drv_status = st.DRV_STATUS();
|
||||
static uint8_t get_status_response(TMC2208Stepper &st, uint32_t drv_status) {
|
||||
uint8_t gstat = st.GSTAT();
|
||||
uint8_t response = 0;
|
||||
response |= (drv_status >> (31-3)) & 0b1000;
|
||||
@ -89,75 +91,112 @@
|
||||
constexpr uint8_t OTPW_bp = 0;
|
||||
constexpr uint32_t OT_bm = 0b10ul;
|
||||
constexpr uint8_t OT_bp = 1;
|
||||
constexpr uint8_t S2GA_bp = 2;
|
||||
constexpr uint8_t S2GB_bp = 3;
|
||||
TMC_driver_data data;
|
||||
data.drv_status = st.DRV_STATUS();
|
||||
data.is_otpw = (data.drv_status & OTPW_bm) >> OTPW_bp;
|
||||
data.is_ot = (data.drv_status & OT_bm) >> OT_bp;
|
||||
data.is_error = st.drv_err();
|
||||
data.is_s2ga = (data.drv_status >> S2GA_bp) & 0b1;
|
||||
data.is_s2gb = (data.drv_status >> S2GB_bp) & 0b1;
|
||||
return data;
|
||||
}
|
||||
#endif
|
||||
#if HAS_DRIVER(TMC2660)
|
||||
#if ENABLED(TMC_DEBUG)
|
||||
static uint32_t get_pwm_scale(TMC2660Stepper) { return 0; }
|
||||
static uint8_t get_status_response(TMC2660Stepper) { return 0; }
|
||||
static uint8_t get_status_response(TMC2660Stepper, uint32_t drv_status) {
|
||||
return drv_status & 0xFF;
|
||||
}
|
||||
#endif
|
||||
static TMC_driver_data get_driver_data(TMC2660Stepper &st) {
|
||||
constexpr uint32_t OTPW_bm = 0x4UL;
|
||||
constexpr uint8_t OTPW_bp = 2;
|
||||
constexpr uint32_t OT_bm = 0x2UL;
|
||||
constexpr uint8_t OT_bp = 1;
|
||||
constexpr uint8_t DRIVER_ERROR_bm = 0x6;
|
||||
TMC_driver_data data;
|
||||
data.drv_status = st.DRVSTATUS();
|
||||
data.is_otpw = (data.drv_status & OTPW_bm) >> OTPW_bp;
|
||||
data.is_ot = (data.drv_status & OT_bm) >> OT_bp;
|
||||
data.is_error = data.drv_status & DRIVER_ERROR_bm;
|
||||
return data;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if ENABLED(STOP_ON_ERROR)
|
||||
void report_driver_error(const TMC_driver_data &data) {
|
||||
SERIAL_ECHOPGM(" driver error detected: 0x");
|
||||
SERIAL_PRINTLN(data.drv_status, HEX);
|
||||
if (data.is_ot) SERIAL_ECHOLNPGM("overtemperature");
|
||||
if (data.is_s2ga) SERIAL_ECHOLNPGM("short to ground (coil A)");
|
||||
if (data.is_s2gb) SERIAL_ECHOLNPGM("short to ground (coil B)");
|
||||
#if ENABLED(TMC_DEBUG)
|
||||
tmc_report_all();
|
||||
#endif
|
||||
kill(PSTR("Driver error"));
|
||||
}
|
||||
#endif
|
||||
|
||||
template<typename TMC>
|
||||
void report_driver_otpw(TMC &st) {
|
||||
char timestamp[14];
|
||||
duration_t elapsed = print_job_timer.duration();
|
||||
const bool has_days = (elapsed.value > 60*60*24L);
|
||||
(void)elapsed.toDigital(timestamp, has_days);
|
||||
SERIAL_EOL();
|
||||
SERIAL_ECHO(timestamp);
|
||||
SERIAL_ECHOPGM(": ");
|
||||
st.printLabel();
|
||||
SERIAL_ECHOPGM(" driver overtemperature warning! (");
|
||||
SERIAL_ECHO(st.getMilliamps());
|
||||
SERIAL_ECHOLNPGM("mA)");
|
||||
}
|
||||
|
||||
template<typename TMC>
|
||||
void report_polled_driver_data(TMC &st, const TMC_driver_data &data) {
|
||||
const uint32_t pwm_scale = get_pwm_scale(st);
|
||||
st.printLabel();
|
||||
SERIAL_ECHOPAIR(":", pwm_scale);
|
||||
SERIAL_ECHOPGM(" |0b"); SERIAL_PRINT(get_status_response(st, data.drv_status), BIN);
|
||||
SERIAL_ECHOPGM("| ");
|
||||
if (st.error_count) SERIAL_CHAR('E');
|
||||
else if (data.is_ot) SERIAL_CHAR('O');
|
||||
else if (data.is_otpw) SERIAL_CHAR('W');
|
||||
else if (st.otpw_count > 0) SERIAL_PRINT(st.otpw_count, DEC);
|
||||
else if (st.flag_otpw) SERIAL_CHAR('F');
|
||||
SERIAL_CHAR('\t');
|
||||
}
|
||||
|
||||
template<typename TMC>
|
||||
void monitor_tmc_driver(TMC &st) {
|
||||
TMC_driver_data data = get_driver_data(st);
|
||||
if ((data.drv_status == 0xFFFFFFFF) || (data.drv_status == 0x0)) return;
|
||||
|
||||
if (data.is_ot /* | data.s2ga | data.s2gb*/) st.error_count++;
|
||||
else if (st.error_count > 0) st.error_count--;
|
||||
|
||||
#if ENABLED(STOP_ON_ERROR)
|
||||
if (data.is_error) {
|
||||
if (st.error_count >= 10) {
|
||||
SERIAL_EOL();
|
||||
st.printLabel();
|
||||
SERIAL_ECHOLNPGM(" driver error detected:");
|
||||
if (data.is_ot) SERIAL_ECHOLNPGM("overtemperature");
|
||||
if (st.s2ga()) SERIAL_ECHOLNPGM("short to ground (coil A)");
|
||||
if (st.s2gb()) SERIAL_ECHOLNPGM("short to ground (coil B)");
|
||||
#if ENABLED(TMC_DEBUG)
|
||||
tmc_report_all();
|
||||
#endif
|
||||
kill(PSTR("Driver error"));
|
||||
report_driver_error(data);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Report if a warning was triggered
|
||||
if (data.is_otpw && st.otpw_count == 0) {
|
||||
char timestamp[14];
|
||||
duration_t elapsed = print_job_timer.duration();
|
||||
const bool has_days = (elapsed.value > 60*60*24L);
|
||||
(void)elapsed.toDigital(timestamp, has_days);
|
||||
SERIAL_EOL();
|
||||
SERIAL_ECHO(timestamp);
|
||||
SERIAL_ECHOPGM(": ");
|
||||
st.printLabel();
|
||||
SERIAL_ECHOPGM(" driver overtemperature warning! (");
|
||||
SERIAL_ECHO(st.getMilliamps());
|
||||
SERIAL_ECHOLNPGM("mA)");
|
||||
report_driver_otpw(st);
|
||||
}
|
||||
#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() && st.otpw_count > 4) {
|
||||
st.rms_current(MAX(int16_t(st.getMilliamps() - (CURRENT_STEP_DOWN)), 0));
|
||||
#if ENABLED(REPORT_CURRENT_CHANGE)
|
||||
st.printLabel();
|
||||
SERIAL_ECHOLNPAIR(" current decreased to ", st.getMilliamps());
|
||||
#endif
|
||||
if (data.is_otpw && st.otpw_count > 4) {
|
||||
uint16_t I_rms = st.getMilliamps();
|
||||
if (st.isEnabled() && I_rms > 100) {
|
||||
st.rms_current(I_rms - (CURRENT_STEP_DOWN));
|
||||
#if ENABLED(REPORT_CURRENT_CHANGE)
|
||||
st.printLabel();
|
||||
SERIAL_ECHOLNPAIR(" current decreased to ", st.getMilliamps());
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -169,17 +208,7 @@
|
||||
|
||||
#if ENABLED(TMC_DEBUG)
|
||||
if (report_tmc_status) {
|
||||
const uint32_t pwm_scale = get_pwm_scale(st);
|
||||
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 (st.otpw_count > 0) SERIAL_PRINT(st.otpw_count, DEC);
|
||||
else if (st.flag_otpw) SERIAL_CHAR('F');
|
||||
SERIAL_CHAR('\t');
|
||||
report_polled_driver_data(st, data);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@ -187,9 +216,10 @@
|
||||
#define HAS_HW_COMMS(ST) AXIS_DRIVER_TYPE(ST, TMC2130) || AXIS_DRIVER_TYPE(ST, TMC2660) || (AXIS_DRIVER_TYPE(ST, TMC2208) && defined(ST##_HARDWARE_SERIAL))
|
||||
|
||||
void monitor_tmc_driver() {
|
||||
static millis_t next_cOT = 0;
|
||||
if (ELAPSED(millis(), next_cOT)) {
|
||||
next_cOT = millis() + 500;
|
||||
static millis_t next_poll = 0;
|
||||
const millis_t ms = millis();
|
||||
if (ELAPSED(ms, next_poll)) {
|
||||
next_poll = ms + 500;
|
||||
#if HAS_HW_COMMS(X)
|
||||
monitor_tmc_driver(stepperX);
|
||||
#endif
|
||||
@ -379,7 +409,12 @@
|
||||
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_TSTEP: {
|
||||
uint32_t tstep_value = st.TSTEP();
|
||||
if (tstep_value == 0xFFFFF) SERIAL_ECHOPGM("max");
|
||||
else SERIAL_ECHO(tstep_value);
|
||||
}
|
||||
break;
|
||||
case TMC_TPWMTHRS: {
|
||||
uint32_t tpwmthrs_val = st.TPWMTHRS();
|
||||
SERIAL_ECHO(tpwmthrs_val);
|
||||
@ -630,16 +665,29 @@
|
||||
|
||||
#if USE_SENSORLESS
|
||||
|
||||
void tmc_stallguard(TMC2130Stepper &st, const bool enable/*=true*/) {
|
||||
st.TCOOLTHRS(enable ? 0xFFFFF : 0);
|
||||
#if ENABLED(STEALTHCHOP)
|
||||
st.en_pwm_mode(!enable);
|
||||
bool tmc_enable_stallguard(TMC2130Stepper &st) {
|
||||
bool stealthchop_was_enabled = st.en_pwm_mode();
|
||||
|
||||
st.TCOOLTHRS(0xFFFFF);
|
||||
#if STEALTHCHOP_ENABLED
|
||||
st.en_pwm_mode(false);
|
||||
#endif
|
||||
st.diag1_stall(enable ? 1 : 0);
|
||||
st.diag1_stall(true);
|
||||
|
||||
return stealthchop_was_enabled;
|
||||
}
|
||||
void tmc_sensorless_homing(TMC2660Stepper &st, const bool enable) {
|
||||
void tmc_disable_stallguard(TMC2130Stepper &st, const bool restore_stealth) {
|
||||
st.TCOOLTHRS(0);
|
||||
#if STEALTHCHOP_ENABLED
|
||||
st.en_pwm_mode(restore_stealth);
|
||||
#endif
|
||||
st.diag1_stall(false);
|
||||
}
|
||||
bool tmc_enable_stallguard(TMC2660Stepper) {
|
||||
// TODO
|
||||
return false;
|
||||
}
|
||||
void tmc_disable_stallguard(TMC2660Stepper, const bool) {};
|
||||
|
||||
#endif // USE_SENSORLESS
|
||||
|
||||
|
@ -52,7 +52,8 @@ class TMCStorage {
|
||||
|
||||
public:
|
||||
#if ENABLED(MONITOR_DRIVER_STATUS)
|
||||
uint8_t otpw_count = 0;
|
||||
uint8_t otpw_count = 0,
|
||||
error_count = 0;
|
||||
bool flag_otpw = false;
|
||||
bool getOTPW() { return flag_otpw; }
|
||||
void clear_otpw() { flag_otpw = 0; }
|
||||
@ -105,7 +106,7 @@ class TMCMarlin<TMC2208Stepper, AXIS_LETTER, DRIVER_ID> : public TMC2208Stepper,
|
||||
}
|
||||
};
|
||||
|
||||
constexpr uint32_t _tmc_thrs(const uint16_t msteps, const int32_t thrs, const uint32_t spmm) {
|
||||
constexpr uint16_t _tmc_thrs(const uint16_t msteps, const int32_t thrs, const uint32_t spmm) {
|
||||
return 12650000UL * msteps / (256 * thrs * spmm);
|
||||
}
|
||||
|
||||
@ -170,8 +171,16 @@ void monitor_tmc_driver();
|
||||
* Defined here because of limitations with templates and headers.
|
||||
*/
|
||||
#if USE_SENSORLESS
|
||||
void tmc_stallguard(TMC2130Stepper &st, const bool enable=true);
|
||||
void tmc_stallguard(TMC2660Stepper &st, const bool enable=true);
|
||||
// Track enabled status of stealthChop and only re-enable where applicable
|
||||
struct sensorless_t {
|
||||
bool x, y, z;
|
||||
};
|
||||
|
||||
bool tmc_enable_stallguard(TMC2130Stepper &st);
|
||||
void tmc_disable_stallguard(TMC2130Stepper &st, const bool restore_stealth);
|
||||
|
||||
bool tmc_enable_stallguard(TMC2660Stepper);
|
||||
void tmc_disable_stallguard(TMC2660Stepper, const bool);
|
||||
#endif
|
||||
|
||||
#if TMC_HAS_SPI
|
||||
|
Reference in New Issue
Block a user