Fix unit tests + rebase onto main + extract TMC parameters into config

pull/76/head
D.R.racer 2021-07-29 07:38:29 +02:00 committed by DRracer
parent aca2cb7e79
commit a8147be803
5 changed files with 60 additions and 37 deletions

View File

@ -31,6 +31,8 @@ static constexpr const uint16_t buttonsDebounceMs = 100;
static constexpr const uint16_t buttonADCLimits[buttonCount][2] = { { 0, 50 }, { 80, 100 }, { 160, 180 } }; static constexpr const uint16_t buttonADCLimits[buttonCount][2] = { { 0, 50 }, { 80, 100 }, { 160, 180 } };
static constexpr const uint8_t buttonsADCIndex = 5; ///< ADC index of buttons input static constexpr const uint8_t buttonsADCIndex = 5; ///< ADC index of buttons input
// Motion and planning
/// Do not plan moves equal or shorter than the requested steps /// Do not plan moves equal or shorter than the requested steps
static constexpr uint8_t dropSegments = 0; static constexpr uint8_t dropSegments = 0;
@ -104,4 +106,14 @@ static constexpr IdlerLimits idlerLimits = {
.accel = 1000.0_deg_s2, .accel = 1000.0_deg_s2,
}; };
// TMC2130 setup
static constexpr uint8_t tmc2130_sg_thrs = 3; // @todo 7bit two's complement for the sg_thrs
static constexpr uint32_t tmc2130_coolConf = (((uint32_t)tmc2130_sg_thrs) << 16U);
static constexpr uint16_t tmc2130_coolStepThreshold = 400;
static constexpr uint32_t tmc2130_PWM_AMPL = (uint32_t)(240U & 0xFFU) << 0U;
static constexpr uint32_t tmc2130_PWM_GRAD = (uint32_t)(4U & 0xFFU) << 8U;
static constexpr uint32_t tmc2130_PWM_FREQ = (uint32_t)(2U & 0x03U) << 16U;
static constexpr uint32_t tmc2130_PWM_AUTOSCALE = (uint32_t)(1U & 0x01U) << 18U;
} // namespace config } // namespace config

View File

@ -1,13 +1,14 @@
#include "../tmc2130.h" #include "../tmc2130.h"
#include "../../config/config.h"
namespace hal { namespace hal {
namespace tmc2130 { namespace tmc2130 {
TMC2130::TMC2130(const MotorParams &params, TMC2130::TMC2130(const MotorParams &params, const MotorCurrents &currents, MotorMode mode)
const MotorCurrents &currents, : mode(mode)
MotorMode mode) , currents(currents)
: currents(currents) { , sg_counter(0) {
// TODO Init(params);
} }
bool TMC2130::Init(const MotorParams &params) { bool TMC2130::Init(const MotorParams &params) {
@ -17,22 +18,24 @@ bool TMC2130::Init(const MotorParams &params) {
///check for compatible tmc driver (IOIN version field) ///check for compatible tmc driver (IOIN version field)
uint32_t IOIN = ReadRegister(params, Registers::IOIN); uint32_t IOIN = ReadRegister(params, Registers::IOIN);
if (((IOIN >> 24) != 0x11) | !(IOIN & (1 << 6))) ///if the version is incorrect or an always 1 bit is 0 (the supposed SD_MODE pin that doesn't exist on this driver variant)
// if the version is incorrect or an always 1st bit is 0
// (the supposed SD_MODE pin that doesn't exist on this driver variant)
if (((IOIN >> 24U) != 0x11) | !(IOIN & (1U << 6U)))
return true; // @todo return some kind of failure return true; // @todo return some kind of failure
///clear reset_flag as we are (re)initializing ///clear reset_flag as we are (re)initializing
errorFlags.reset_flag = false; errorFlags.reset_flag = false;
///apply chopper parameters ///apply chopper parameters
uint32_t chopconf = 0; const uint32_t chopconf = (uint32_t)(3U & 0x0FU) << 0U //toff
chopconf |= (uint32_t)(3 & 0x0F) << 0; //toff | (uint32_t)(5U & 0x07U) << 4U //hstrt
chopconf |= (uint32_t)(5 & 0x07) << 4; //hstrt | (uint32_t)(1U & 0x0FU) << 7U //hend
chopconf |= (uint32_t)(1 & 0x0F) << 7; //hend | (uint32_t)(2U & 0x03U) << 15U //tbl
chopconf |= (uint32_t)(2 & 0x03) << 15; //tbl | (uint32_t)(currents.vSense & 0x01U) << 17U //vsense
chopconf |= (uint32_t)(currents.vSense & 0x01) << 17; //vsense | (uint32_t)(params.uSteps & 0x0FU) << 24U //mres
chopconf |= (uint32_t)(params.uSteps & 0x0F) << 24; //mres | (uint32_t)((bool)params.uSteps) << 28U //intpol
chopconf |= (uint32_t)((bool)params.uSteps) << 28; //intpol | (uint32_t)(1U & 0x01) << 29U; //dedge
chopconf |= (uint32_t)(1 & 0x01) << 29; //dedge
WriteRegister(params, Registers::CHOPCONF, chopconf); WriteRegister(params, Registers::CHOPCONF, chopconf);
///apply currents ///apply currents
@ -42,25 +45,21 @@ bool TMC2130::Init(const MotorParams &params) {
WriteRegister(params, Registers::TPOWERDOWN, 0); WriteRegister(params, Registers::TPOWERDOWN, 0);
///Stallguard parameters ///Stallguard parameters
int8_t sg_thrs = 3; // @todo 7bit two's complement for the sg_thrs WriteRegister(params, Registers::COOLCONF, config::tmc2130_coolConf);
WriteRegister(params, Registers::COOLCONF, (((uint32_t)sg_thrs) << 16)); // @todo should be configurable WriteRegister(params, Registers::TCOOLTHRS, config::tmc2130_coolStepThreshold);
WriteRegister(params, Registers::TCOOLTHRS, 400); // @todo should be configurable
///Write stealth mode config and setup diag0 output ///Write stealth mode config and setup diag0 output
uint32_t gconf = 0; constexpr uint32_t gconf = (uint32_t)(1U & 0x01U) << 2U //en_pwm_mode - always enabled since we can control it's effect with TPWMTHRS (0=only stealthchop, 0xFFFFF=only spreadcycle)
gconf |= (uint32_t)(1 & 0x01) << 2; //en_pwm_mode - always enabled since we can control it's effect with TPWMTHRS (0=only stealthchop, 0xFFFFF=only spreadcycle) | (uint32_t)(1U & 0x01U) << 7U; //diag0_stall - diag0 is open collector => active low with external pullups
gconf |= (uint32_t)(1 & 0x01) << 7; //diag0_stall - diag0 is open collector => active low with external pullups
WriteRegister(params, Registers::GCONF, gconf); WriteRegister(params, Registers::GCONF, gconf);
///stealthChop parameters ///stealthChop parameters
uint32_t pwmconf = 0; /// @todo All of these parameters should be configurable constexpr uint32_t pwmconf = config::tmc2130_PWM_AMPL | config::tmc2130_PWM_GRAD | config::tmc2130_PWM_FREQ | config::tmc2130_PWM_AUTOSCALE;
pwmconf |= (uint32_t)(240 & 0xFF) << 0; //PWM_AMPL
pwmconf |= (uint32_t)(4 & 0xFF) << 8; //PWM_GRAD
pwmconf |= (uint32_t)(2 & 0x03) << 16; //pwm_freq
pwmconf |= (uint32_t)(1 & 0x01) << 18; //pwm_autoscale
WriteRegister(params, Registers::PWMCONF, pwmconf); WriteRegister(params, Registers::PWMCONF, pwmconf);
///TPWMTHRS: switching velocity between stealthChop and spreadCycle. Stallguard is also disabled if the velocity falls below this. Should be set as high as possible when homing. /// TPWMTHRS: switching velocity between stealthChop and spreadCycle.
/// Stallguard is also disabled if the velocity falls below this.
/// Should be set as high as possible when homing.
SetMode(params, mode); SetMode(params, mode);
return false; return false;
} }
@ -75,10 +74,9 @@ void TMC2130::SetMode(const MotorParams &params, MotorMode mode) {
void TMC2130::SetCurrents(const MotorParams &params, const MotorCurrents &currents) { void TMC2130::SetCurrents(const MotorParams &params, const MotorCurrents &currents) {
this->currents = currents; this->currents = currents;
uint32_t ihold_irun = 0; uint32_t ihold_irun = (uint32_t)(currents.iHold & 0x1F) << 0 //ihold
ihold_irun |= (uint32_t)(currents.iHold & 0x1F) << 0; //ihold | (uint32_t)(currents.iRun & 0x1F) << 8 //irun
ihold_irun |= (uint32_t)(currents.iRun & 0x1F) << 8; //irun | (uint32_t)(15 & 0x0F) << 16; //IHOLDDELAY
ihold_irun |= (uint32_t)(15 & 0x0F) << 16; //IHOLDDELAY
WriteRegister(params, Registers::IHOLD_IRUN, ihold_irun); WriteRegister(params, Registers::IHOLD_IRUN, ihold_irun);
} }
@ -97,11 +95,11 @@ void TMC2130::ClearStallguard(const MotorParams &params) {
bool TMC2130::CheckForErrors(const MotorParams &params) { bool TMC2130::CheckForErrors(const MotorParams &params) {
uint32_t GSTAT = ReadRegister(params, Registers::GSTAT); uint32_t GSTAT = ReadRegister(params, Registers::GSTAT);
uint32_t DRV_STATUS = ReadRegister(params, Registers::DRV_STATUS); uint32_t DRV_STATUS = ReadRegister(params, Registers::DRV_STATUS);
errorFlags.reset_flag |= GSTAT & (1 << 0); errorFlags.reset_flag |= GSTAT & (1U << 0U);
errorFlags.uv_cp = GSTAT & (1 << 2); errorFlags.uv_cp = GSTAT & (1U << 2U);
errorFlags.s2g = DRV_STATUS & (3ul << 27); errorFlags.s2g = DRV_STATUS & (3UL << 27U);
errorFlags.otpw = DRV_STATUS & (1ul << 26); errorFlags.otpw = DRV_STATUS & (1UL << 26U);
errorFlags.ot = DRV_STATUS & (1ul << 25); errorFlags.ot = DRV_STATUS & (1UL << 25U);
return GSTAT || errorFlags.reset_flag; //any bit in gstat is an error return GSTAT || errorFlags.reset_flag; //any bit in gstat is an error
} }

View File

@ -41,6 +41,13 @@ class TMC2130 {
uint8_t s2g : 1; uint8_t s2g : 1;
uint8_t otpw : 1; uint8_t otpw : 1;
uint8_t ot : 1; uint8_t ot : 1;
inline ErrorFlags()
: reset_flag(0)
, uv_cp(0)
, s2g(0)
, otpw(0)
, ot(0) {}
} errorFlags; } errorFlags;
bool enabled = false; bool enabled = false;
uint8_t sg_counter; uint8_t sg_counter;

View File

@ -26,7 +26,7 @@ bool Motion::StallGuard(Axis axis) {
return axes[axis].stallGuard; return axes[axis].stallGuard;
} }
void Motion::ClearStallGuardFlag(Axis axis) { void Motion::StallGuardReset(Axis axis) {
axes[axis].stallGuard = false; axes[axis].stallGuard = false;
} }

View File

@ -22,5 +22,11 @@ void TMC2130::SetEnabled(const MotorParams &params, bool enabled) {
this->enabled = enabled; this->enabled = enabled;
} }
void TMC2130::ClearStallguard(const MotorParams &params) {
}
void TMC2130::Isr(const MotorParams &params) {
}
} // namespace tmc2130 } // namespace tmc2130
} // namespace hal } // namespace hal