Restore custom currents after homing end

This commit is contained in:
Alex Voinea 2024-02-19 13:29:31 +01:00
parent 40771fe7a4
commit dd4b8f7352
No known key found for this signature in database
GPG Key ID: 37EDFD565CB33BAD
2 changed files with 35 additions and 44 deletions

View File

@ -30,18 +30,25 @@ static constexpr uint8_t default_dedge_bit = 0;
//mode //mode
uint8_t tmc2130_mode = TMC2130_MODE_NORMAL; uint8_t tmc2130_mode = TMC2130_MODE_NORMAL;
uint8_t tmc2130_current_h[4] = TMC2130_CURRENTS_H;
//running currents
uint8_t tmc2130_current_r[4] = TMC2130_CURRENTS_R;
static constexpr uint8_t tmc2130_default_current_h[4] = TMC2130_CURRENTS_H;
//running currents
static constexpr uint8_t tmc2130_default_current_r[4] = TMC2130_CURRENTS_R;
//running currents for homing //running currents for homing
static uint8_t tmc2130_current_r_home[4] = TMC2130_CURRENTS_R_HOME; static constexpr uint8_t tmc2130_current_r_home[4] = TMC2130_CURRENTS_R_HOME;
static constexpr MotorCurrents homing_currents_P[NUM_AXIS] PROGMEM = {
MotorCurrents(tmc2130_current_r_home[0], tmc2130_current_r_home[0]),
MotorCurrents(tmc2130_current_r_home[1], tmc2130_current_r_home[1]),
MotorCurrents(tmc2130_current_r_home[2], tmc2130_current_r_home[2]),
MotorCurrents(tmc2130_current_r_home[3], tmc2130_current_r_home[3])
};
MotorCurrents currents[NUM_AXIS] = { MotorCurrents currents[NUM_AXIS] = {
MotorCurrents(tmc2130_current_r[0], tmc2130_current_h[0]), MotorCurrents(tmc2130_default_current_r[0], tmc2130_default_current_h[0]),
MotorCurrents(tmc2130_current_r[1], tmc2130_current_h[1]), MotorCurrents(tmc2130_default_current_r[1], tmc2130_default_current_h[1]),
MotorCurrents(tmc2130_current_r[2], tmc2130_current_h[2]), MotorCurrents(tmc2130_default_current_r[2], tmc2130_default_current_h[2]),
MotorCurrents(tmc2130_current_r[3], tmc2130_current_h[3]) MotorCurrents(tmc2130_default_current_r[3], tmc2130_default_current_h[3])
}; };
union ChopConfU { union ChopConfU {
@ -419,8 +426,8 @@ void tmc2130_home_enter(uint8_t axes_mask)
tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_NORMAL); tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_NORMAL);
tmc2130_wr(axis, TMC2130_REG_COOLCONF, (((uint32_t)tmc2130_sg_thr_home[axis]) << 16)); tmc2130_wr(axis, TMC2130_REG_COOLCONF, (((uint32_t)tmc2130_sg_thr_home[axis]) << 16));
tmc2130_wr(axis, TMC2130_REG_TCOOLTHRS, __tcoolthrs(axis)); tmc2130_wr(axis, TMC2130_REG_TCOOLTHRS, __tcoolthrs(axis));
currents[axis].setiRun(tmc2130_current_r_home[axis]); MotorCurrents curr(homing_currents_P[axis]);
tmc2130_setup_chopper(axis, tmc2130_mres[axis]); tmc2130_setup_chopper(axis, tmc2130_mres[axis], &curr);
tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_SGSENS); //stallguard output DIAG1, DIAG1 = pushpull tmc2130_wr(axis, TMC2130_REG_GCONF, TMC2130_GCONF_SGSENS); //stallguard output DIAG1, DIAG1 = pushpull
} }
} }
@ -439,9 +446,6 @@ void tmc2130_home_exit()
{ {
if (tmc2130_sg_homing_axes_mask & mask) { if (tmc2130_sg_homing_axes_mask & mask) {
tmc2130_XYZ_reg_init(axis); tmc2130_XYZ_reg_init(axis);
currents[axis].setiRun(tmc2130_current_r[axis]);
currents[axis].setiHold(tmc2130_current_h[axis]);
tmc2130_setup_chopper(axis, tmc2130_mres[axis]);
} }
} }
tmc2130_sg_homing_axes_mask = 0x00; tmc2130_sg_homing_axes_mask = 0x00;
@ -519,17 +523,12 @@ static constexpr bool getIntpolBit([[maybe_unused]]const uint8_t axis, const uin
return (mres != 0); // intpol to 256 only if microsteps aren't 256 return (mres != 0); // intpol to 256 only if microsteps aren't 256
} }
static void SetCurrents(const uint8_t axis) { static void SetCurrents(const uint8_t axis, const MotorCurrents &curr) {
uint8_t iHold = currents[axis].getiHold(); uint8_t iHold = curr.getiHold();
const uint8_t iRun = currents[axis].getiRun(); const uint8_t iRun = curr.getiRun();
// Make sure iHold never exceeds iRun at runtime // Make sure iHold never exceeds iRun at runtime
if (iHold > iRun) { if (curr.iHoldIsClamped()) {
iHold = iRun;
// Update global array such that M913 reports correct values
currents[axis].setiHold(iRun);
// Let user know firmware modified the value // Let user know firmware modified the value
SERIAL_ECHO_START; SERIAL_ECHO_START;
SERIAL_ECHOLNRPGM(_n("Hold current truncated to Run current")); SERIAL_ECHOLNRPGM(_n("Hold current truncated to Run current"));
@ -555,7 +554,7 @@ static void SetCurrents(const uint8_t axis) {
tmc2130_wr(axis, TMC2130_REG_IHOLD_IRUN, ihold_irun.dw); tmc2130_wr(axis, TMC2130_REG_IHOLD_IRUN, ihold_irun.dw);
} }
void tmc2130_setup_chopper(uint8_t axis, uint8_t mres) void tmc2130_setup_chopper(uint8_t axis, uint8_t mres, const MotorCurrents *curr /* = nullptr */)
{ {
// Initialise the chopper configuration // Initialise the chopper configuration
ChopConfU chopconf = ChopConfU(currents[axis].getvSense(), mres); ChopConfU chopconf = ChopConfU(currents[axis].getvSense(), mres);
@ -567,21 +566,10 @@ void tmc2130_setup_chopper(uint8_t axis, uint8_t mres)
chopconf.s.tbl = tmc2130_chopper_config[axis].tbl; //blanking time, original value = 2 chopconf.s.tbl = tmc2130_chopper_config[axis].tbl; //blanking time, original value = 2
tmc2130_wr(axis, TMC2130_REG_CHOPCONF, chopconf.dw); tmc2130_wr(axis, TMC2130_REG_CHOPCONF, chopconf.dw);
SetCurrents(axis); if (curr == nullptr) {
} curr = &currents[axis];
}
void tmc2130_set_current_h(uint8_t axis, uint8_t current) SetCurrents(axis, *curr);
{
// DBG(_n("tmc2130_set_current_h(axis=%d, current=%d\n"), axis, current);
currents[axis].setiHold(current);
tmc2130_setup_chopper(axis, tmc2130_mres[axis]);
}
void tmc2130_set_current_r(uint8_t axis, uint8_t current)
{
// DBG(_n("tmc2130_set_current_r(axis=%d, current=%d\n"), axis, current);
currents[axis].setiRun(current);
tmc2130_setup_chopper(axis, tmc2130_mres[axis]);
} }
void tmc2130_print_currents() void tmc2130_print_currents()

View File

@ -95,13 +95,17 @@ struct MotorCurrents {
} }
} }
// PROGMEM initializer
inline __attribute__((always_inline)) MotorCurrents(const MotorCurrents &curr_P) { memcpy_P(this, &curr_P, sizeof(*this)); }
constexpr inline __attribute__((always_inline)) MotorCurrents(uint8_t ir, uint8_t ih) constexpr inline __attribute__((always_inline)) MotorCurrents(uint8_t ir, uint8_t ih)
: vSense((ir < 32) ? 1 : 0) : vSense((ir < 32) ? 1 : 0)
, iRun((ir < 32) ? ir : (ir >> 1)) , iRun((ir < 32) ? ir : (ir >> 1))
, iHold((ir < 32) ? ih : (ih >> 1)) {} , iHold((ir < 32) ? ih : (ih >> 1)) {}
inline uint8_t getiRun() const { return iRun; } inline uint8_t getiRun() const { return iRun; }
inline uint8_t getiHold() const { return iHold; } inline uint8_t getiHold() const { return min(iHold, iRun); }
inline bool iHoldIsClamped() const { return iHold > iRun; }
inline uint8_t getvSense() const { return vSense; } inline uint8_t getvSense() const { return vSense; }
void __attribute__((noinline)) setiRun(uint8_t ir) { void __attribute__((noinline)) setiRun(uint8_t ir) {
@ -162,12 +166,11 @@ extern void tmc2130_sg_measure_start(uint8_t axis);
//stop current stallguard measuring and report result //stop current stallguard measuring and report result
extern uint16_t tmc2130_sg_measure_stop(); extern uint16_t tmc2130_sg_measure_stop();
extern void tmc2130_setup_chopper(uint8_t axis, uint8_t mres); // Enable or Disable crash detection according to EEPROM
void crashdet_use_eeprom_setting();
extern void tmc2130_setup_chopper(uint8_t axis, uint8_t mres, const MotorCurrents *curr = nullptr);
//set holding current for any axis (M911)
extern void tmc2130_set_current_h(uint8_t axis, uint8_t current);
//set running current for any axis (M912)
extern void tmc2130_set_current_r(uint8_t axis, uint8_t current);
//print currents (M913) //print currents (M913)
extern void tmc2130_print_currents(); extern void tmc2130_print_currents();