tmc2130: Stallguard and Isr
parent
3d1880c006
commit
8577852b09
|
|
@ -84,9 +84,16 @@ void TMC2130::SetCurrents(const MotorParams ¶ms, const MotorCurrents ¤
|
||||||
|
|
||||||
void TMC2130::SetEnabled(const MotorParams ¶ms, bool enabled) {
|
void TMC2130::SetEnabled(const MotorParams ¶ms, bool enabled) {
|
||||||
hal::shr16::shr16.SetTMCDir(params.idx, enabled);
|
hal::shr16::shr16.SetTMCDir(params.idx, enabled);
|
||||||
|
if (this->enabled != enabled)
|
||||||
|
ClearStallguard(params);
|
||||||
this->enabled = enabled;
|
this->enabled = enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TMC2130::ClearStallguard(const MotorParams ¶ms) {
|
||||||
|
// @todo: maximum resolution right now is x256/4 (uint8_t / 4)
|
||||||
|
sg_counter = 4 * (1 << (8 - params.uSteps)) - 1; /// one electrical full step (4 steps when fullstepping)
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t TMC2130::ReadRegister(const MotorParams ¶ms, Registers reg) {
|
uint32_t TMC2130::ReadRegister(const MotorParams ¶ms, Registers reg) {
|
||||||
uint8_t pData[5] = { (uint8_t)reg };
|
uint8_t pData[5] = { (uint8_t)reg };
|
||||||
_spi_tx_rx(params, pData);
|
_spi_tx_rx(params, pData);
|
||||||
|
|
@ -102,6 +109,15 @@ void TMC2130::WriteRegister(const MotorParams ¶ms, Registers reg, uint32_t d
|
||||||
_handle_spi_status(params, pData[0]);
|
_handle_spi_status(params, pData[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TMC2130::Isr(const MotorParams ¶ms) {
|
||||||
|
if (sg_counter) {
|
||||||
|
if (SampleDiag(params))
|
||||||
|
sg_counter--;
|
||||||
|
else if (sg_counter < (4 * (1 << (8 - params.uSteps)) - 1))
|
||||||
|
sg_counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void TMC2130::_spi_tx_rx(const MotorParams ¶ms, uint8_t (&pData)[5]) {
|
void TMC2130::_spi_tx_rx(const MotorParams ¶ms, uint8_t (&pData)[5]) {
|
||||||
hal::gpio::WritePin(params.csPin, hal::gpio::Level::low);
|
hal::gpio::WritePin(params.csPin, hal::gpio::Level::low);
|
||||||
for (uint8_t i = 0; i < sizeof(pData); i++) {
|
for (uint8_t i = 0; i < sizeof(pData); i++) {
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ class TMC2130 {
|
||||||
uint8_t driver_error : 1;
|
uint8_t driver_error : 1;
|
||||||
} errorFlags;
|
} errorFlags;
|
||||||
bool enabled = false;
|
bool enabled = false;
|
||||||
|
uint8_t sg_counter;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
enum class Registers : uint8_t {
|
enum class Registers : uint8_t {
|
||||||
|
|
@ -107,16 +108,25 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return SG state
|
/// Return SG state
|
||||||
static inline bool Stall(const MotorParams ¶ms) {
|
static inline bool SampleDiag(const MotorParams ¶ms) {
|
||||||
return gpio::ReadPin(params.sgPin) == gpio::Level::low;
|
return gpio::ReadPin(params.sgPin) == gpio::Level::low;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline bool Stalled() const {
|
||||||
|
return sg_counter == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClearStallguard(const MotorParams ¶ms);
|
||||||
|
|
||||||
/// Reads a driver register and updates the status flags
|
/// Reads a driver register and updates the status flags
|
||||||
uint32_t ReadRegister(const MotorParams ¶ms, Registers reg);
|
uint32_t ReadRegister(const MotorParams ¶ms, Registers reg);
|
||||||
|
|
||||||
/// Writes a driver register and updates the status flags
|
/// Writes a driver register and updates the status flags
|
||||||
void WriteRegister(const MotorParams ¶ms, Registers reg, uint32_t data);
|
void WriteRegister(const MotorParams ¶ms, Registers reg, uint32_t data);
|
||||||
|
|
||||||
|
/// Used for polling the DIAG pin. Should be called from the stepper isr periodically when moving.
|
||||||
|
void Isr(const MotorParams ¶ms);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void _spi_tx_rx(const MotorParams ¶ms, uint8_t (&pData)[5]);
|
void _spi_tx_rx(const MotorParams ¶ms, uint8_t (&pData)[5]);
|
||||||
void _handle_spi_status(const MotorParams ¶ms, uint8_t status);
|
void _handle_spi_status(const MotorParams ¶ms, uint8_t status);
|
||||||
|
|
|
||||||
|
|
@ -18,12 +18,6 @@ void Motion::SetEnabled(Axis axis, bool enabled) {
|
||||||
if (enabled != axisData[axis].enabled) {
|
if (enabled != axisData[axis].enabled) {
|
||||||
axisData[axis].drv.SetEnabled(axisParams[axis].params, enabled);
|
axisData[axis].drv.SetEnabled(axisParams[axis].params, enabled);
|
||||||
axisData[axis].enabled = enabled;
|
axisData[axis].enabled = enabled;
|
||||||
|
|
||||||
if (!axisData[axis].enabled) {
|
|
||||||
// axis is powered off, clear internal StallGuard counters
|
|
||||||
axisData[axis].stall_trig = false;
|
|
||||||
axisData[axis].stall_cnt = 0;
|
|
||||||
}
|
|
||||||
} // else skip unnecessary Enable/Disable operations on an axis if already in the desired state
|
} // else skip unnecessary Enable/Disable operations on an axis if already in the desired state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -33,11 +27,11 @@ void Motion::SetMode(Axis axis, MotorMode mode) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Motion::StallGuard(Axis axis) {
|
bool Motion::StallGuard(Axis axis) {
|
||||||
return axisData[axis].stall_trig;
|
return axisData[axis].drv.Stalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Motion::ClearStallGuardFlag(Axis axis) {
|
void Motion::StallGuardReset(Axis axis) {
|
||||||
axisData[axis].stall_trig = false;
|
axisData[axis].drv.ClearStallguard(axisParams[axis].params);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: not implemented
|
// TODO: not implemented
|
||||||
|
|
@ -77,13 +71,8 @@ st_timer_t Motion::Step() {
|
||||||
if (timers[i] <= config::stepTimerQuantum) {
|
if (timers[i] <= config::stepTimerQuantum) {
|
||||||
timers[i] += axisData[i].ctrl.Step(axisParams[i].params);
|
timers[i] += axisData[i].ctrl.Step(axisParams[i].params);
|
||||||
|
|
||||||
// axis has been moved, sample StallGuard
|
// axis has been moved, run the tmc2130 Isr for this axis
|
||||||
if (hal::tmc2130::TMC2130::Stall(axisParams[i].params)) {
|
axisData[i].drv.Isr(axisParams[i].params);
|
||||||
// TODO: on the MK3 a stall is marked as such as 1/2 of a full step is
|
|
||||||
// lost: this is too simplistic for production
|
|
||||||
++axisData[i].stall_cnt;
|
|
||||||
axisData[i].stall_trig = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -93,7 +93,7 @@ public:
|
||||||
bool StallGuard(Axis axis);
|
bool StallGuard(Axis axis);
|
||||||
|
|
||||||
/// clear stall guard flag reported on an axis
|
/// clear stall guard flag reported on an axis
|
||||||
void ClearStallGuardFlag(Axis axis);
|
void StallGuardReset(Axis axis);
|
||||||
|
|
||||||
/// Enqueue performing of homing of an axis
|
/// Enqueue performing of homing of an axis
|
||||||
void Home(Axis axis, bool direction);
|
void Home(Axis axis, bool direction);
|
||||||
|
|
@ -220,8 +220,6 @@ private:
|
||||||
pulse_gen::PulseGen ctrl; ///< Motor controller
|
pulse_gen::PulseGen ctrl; ///< Motor controller
|
||||||
bool enabled; ///< Axis enabled
|
bool enabled; ///< Axis enabled
|
||||||
st_timer_t residual; ///< Axis timer residual
|
st_timer_t residual; ///< Axis timer residual
|
||||||
uint8_t stall_cnt; ///< Underlying StallGuard lost ustep count
|
|
||||||
bool stall_trig; ///< StallGuard trigger flag
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Helper to initialize AxisData members
|
/// Helper to initialize AxisData members
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue