diff --git a/components/step_motor_ctrl_module/step_motor_ctrl_module.cpp b/components/step_motor_ctrl_module/step_motor_ctrl_module.cpp index 0262d00..4626d17 100644 --- a/components/step_motor_ctrl_module/step_motor_ctrl_module.cpp +++ b/components/step_motor_ctrl_module/step_motor_ctrl_module.cpp @@ -371,7 +371,7 @@ bool StepMotorCtrlModule::check_when_run() { } TMC51X0* tmc5130 = dynamic_cast(m_stepM1); - if (tmc5130) { + if (tmc5130 && tmc5130->isTMC5130()) { auto state = tmc5130->getGState(); auto devStatus = tmc5130->getDevStatus(); if (state.reset) { diff --git a/components/tmc/basic/tmc_ic_interface.hpp b/components/tmc/basic/tmc_ic_interface.hpp index b019714..0a01f52 100644 --- a/components/tmc/basic/tmc_ic_interface.hpp +++ b/components/tmc/basic/tmc_ic_interface.hpp @@ -20,6 +20,7 @@ typedef enum { kTMC4361A = 0x2, kTMC2160 = 0x30, kTMC5130 = 0x11, + // kTMC5160 = 0x30, } tmcic_id_t; class IStepperMotor { diff --git a/components/tmc/ic/ztmc4361A.cpp b/components/tmc/ic/ztmc4361A.cpp index 6e52d81..9e9a338 100644 --- a/components/tmc/ic/ztmc4361A.cpp +++ b/components/tmc/ic/ztmc4361A.cpp @@ -202,69 +202,30 @@ void TMC4361A::init() { driverIC_writeInt(0x81, 0x07); driverIC_writeInt(0x80, 0x12200); - driverIC_writeInt(0x90, 0x00071600); - driverIC_writeInt(0x91, 0x0a); - driverIC_writeInt(0x94, 200); - driverIC_writeInt(0x95, 50); - driverIC_writeInt(0xEE, 0x70025); - driverIC_writeInt(0xF0, 0x404FF); - driverIC_writeInt(0xEC, 0x03010223); - driverIC_writeInt(0xED, 0x100a220); - - tmc4361_writeInt(0x24, 0); - tmc4361_writeInt(0x21, 0); - tmc4361_writeInt(0x37, 0); - - driverIC_writeInt(0x6D, 0x6d); - driverIC_writeInt(0x6c, 0); - -#if 0 - tmc4361_writeInt(0x54, 4000); // Encoder resolution = 4000/rev - tmc4361_writeInt(0x1C, 0xff00ff); // CL_BETA=CL_GAMMA=255 - tmc4361_writeInt(0x5C, 0x10000); // cl_p = 1.0 - - // cl calibration: - tmc4361_writeInt(0x20, 0x06); // hold+pos mode - tmc4361_writeInt(0x24, 0x100000); // slow vel - tmc4361_writeInt(0x07, 0x400000); // turn on cl - tmc4361_writeInt(0x37, 0x80); // move to FS position - tmc4361_writeInt(0x07, 0x1400000); // turn on cl calibration - osDelay(100); - tmc4361_writeInt(0x07, 0x400000); // turn off cl calibration - tmc4361_writeInt(0x24, 0); // v=0 -#endif - - // S-shaped Ramps setup: - tmc4361_writeInt(0x20, 0x06); // s-ramp + pos mode - tmc4361_writeInt(0x24, 0x31A8000); // vmax=800kpps - tmc4361_writeInt(0x28, 30000); // amax - tmc4361_writeInt(0x29, 15000); // dmax - tmc4361_writeInt(0x2D, 0x100); // bow1 - tmc4361_writeInt(0x2E, 0x100); // bow2 - tmc4361_writeInt(0x2F, 0x100); // bow3 - tmc4361_writeInt(0x30, 0x100); // bow4 - -// cl operation setup: -#if 0 - tmc4361_writeInt(0x60, 250000); // emf_vmin = - tmc4361_writeInt(0x61, 450000); // emf_vadd = -> emf_vmax = - tmc4361_writeInt(0x62, 0xffffff); // emf_vel0_timer - tmc4361_writeInt(0x63, 0x2000864); // enc vel filter settings - - tmc4361_writeInt(0x5F, 5); // cl_tolerance=20 - tmc4361_writeInt(0x5C, 0x10000); // cl_p=1.0 - tmc4361_writeInt(0x5A, 3000); // cl_vlimit_p=200 - tmc4361_writeInt(0x5B, 300); // cl_vlimit_i=50 - tmc4361_writeInt(0x5D, 1000); // cl_vlimit_diclip=1000 - tmc4361_writeInt(0x5E, 200000); // cl_vlimit=200kpps - - tmc4361_writeInt(0x06, 0x32ff64); // cl scaling values - tmc4361_writeInt(0x18, 600); // cl_upscale=1000 - tmc4361_writeInt(0x19, 200000); // cl_dnscale=10000 - - tmc4361_writeInt(0x07, 0xa400000); // cl with gamma correction and vlimit - tmc4361_writeInt(0x05, 0x80); // cl scaling on -#endif + driverIC_writeInt(0x90, 0x00070700); // ihold=20, iholddelay=5, irun=20 + driverIC_writeInt(0x91, 0x0a); // TZEROWAIT=10 + driverIC_writeInt(0x94, 200); // TCOOLTHRS + driverIC_writeInt(0x95, 50); // THIGH + driverIC_writeInt(0xEE, 0x70025); // DCCTRLSG=5, DC_TIME=37 + driverIC_writeInt(0xF0, 0x404FF); // PWMCONF + driverIC_writeInt(0xEC, 0x00010223); // CHOPCONF + driverIC_writeInt(0xED, 0x100a220); // COOLCONF + driverIC_writeInt(0x8b, 0x00); // global scaler = 0 + + tmc4361_writeInt(0x24, 0); // v=0 + tmc4361_writeInt(0x21, 0); // xact=0 + tmc4361_writeInt(0x37, 0); // xtarg=0 + + tmc4361_writeInt(0x6D, 0x80); // GCONF + tmc4361_writeInt(0x6C, 0x00); // 0x12200 diag1=index, pushpull, direct_mode = off --> SD mode + + tmc4361_writeInt(0x20, 0x06); // s-ramp + pos mode + tmc4361_writeInt(0x2D, 0x100); // bow1 + tmc4361_writeInt(0x2E, 0x100); // bow2 + tmc4361_writeInt(0x2F, 0x100); // bow3 + tmc4361_writeInt(0x30, 0x100); // bow4 + tmc4361_writeInt(TMC4361A_AMAX, 10000); // amax + tmc4361_writeInt(TMC4361A_DMAX, 10000); // dmax } uint8_t TMC4361A::reset() { @@ -279,41 +240,87 @@ uint8_t TMC4361A::reset() { zchip_clock_early_delayus(1000); tmc4361_writeInt(TMC4361A_RESET_REG, 0); + init(); + + // 发送 0x4440128C to SPI_OUT_CONF 寄存器 0x04。 + // 发送 0xEC 到 COVER_HIGH 寄存器 0x6D 及 0x000100C3 到 COVER_LOW 寄存器 0x6C。 + // 发送 0x90 到 COVER_HIGH 寄存器 0x6D 及 0x00061F0A 到 COVER_LOW 寄存器 0x6C。 + // 发送 0x91 到 COVER_HIGH 寄存器 0x6D 及 0x0000000A 到 COVER_LOW 寄存器 0x6C。 + // 发送 0x80 到 COVER_HIGH 寄存器 0x6D 及 0x00000004 到 COVER_LOW 寄存器 0x6C。 + // 发送 0x93 到 COVER_HIGH 寄存器 0x6D 及 0x000001F4 到 COVER_LOW 寄存器 0x6C。 +#if 0 + tmc4361_writeInt(0x04, 0x4440128C); + + tmc4361_writeInt(0x6D, 0xEC); + tmc4361_writeInt(0x6C, 0x000100C3); + + tmc4361_writeInt(0x6D, 0x90); + tmc4361_writeInt(0x6C, 0x00061F0A); + + tmc4361_writeInt(0x6D, 0x91); + tmc4361_writeInt(0x6C, 0x0000000A); + + tmc4361_writeInt(0x6D, 0x80); + tmc4361_writeInt(0x6C, 0x00000004); + + tmc4361_writeInt(0x6D, 0x93); + tmc4361_writeInt(0x6C, 0x000001F4); +#endif + +#if 0 + //  将 0x4440128D 发送至 SPI_OUT_CONF 寄存器 0x04。 + //  发送 0x80 至 COVER_HIGH 寄存器 0x6D,发送 0x00010000 至 COVER_LOW 寄存器 0x6C。 + //  发送 0xEC 至 COVER_HIGH 寄存器 0x6D,发送 0x000100C3 至 COVER_LOW 寄存器 0x6C。 + //  发送 0x90 至 COVER_HIGH 寄存器 0x6D,发送 0x00000A0A 至 COVER_LOW 寄存器 0x6C。 + tmc4361_writeInt(0x04, 0x4440128D); + + tmc4361_writeInt(0x6D, 0x80); + tmc4361_writeInt(0x6C, 0x00010000); + + tmc4361_writeInt(0x6D, 0xEC); + tmc4361_writeInt(0x6C, 0x000100C3); + + tmc4361_writeInt(0x6D, 0x90); + tmc4361_writeInt(0x6C, 0x00000A0A); +#endif + +#if 0 /** * @brief 重置芯片镜像寄存?? * */ - // for (uint32_t add = 0; add < TMC4361A_REGISTER_COUNT; add++) { - // if (!TMC_IS_RESETTABLE(m_registerAccessTable[add])) { - // continue; - // } - // tmc4361_writeInt(add, m_defaultRegisterResetState[add]); - // } + for (uint32_t add = 0; add < TMC4361A_REGISTER_COUNT; add++) { + if (!TMC_IS_RESETTABLE(m_registerAccessTable[add])) { + continue; + } + tmc4361_writeInt(add, m_defaultRegisterResetState[add]); + } - init(); + // init(); - // uint8_t driver, dataLength; - // uint32_t value; - // // Setup SPI - // switch (m_driver_ic_type) { - // case IC_TMC2130: - // case IC_TMC2160: - // driver = 0x0C; - // dataLength = 0; - // break; - // case IC_TMC2660: - // driver = 0x0B; - // dataLength = 0; - // break; - // default: - // driver = 0x0F; - // dataLength = 40; - // break; - // } - // value = 0x44400040 | (dataLength << 13) | (driver << 0); - // tmc4361_writeInt(TMC4361A_SPIOUT_CONF, value); - // tmc4361_writeInt(TMC4361A_CURRENT_CONF, 0x00000003); - // tmc4361_writeInt(TMC4361A_SCALE_VALUES, 0x00000000); + uint8_t driver, dataLength; + uint32_t value; + // Setup SPI + switch (m_driver_ic_type) { + case IC_TMC2130: + case IC_TMC2160: + driver = 0x0C; + dataLength = 0; + break; + case IC_TMC2660: + driver = 0x0B; + dataLength = 0; + break; + default: + driver = 0x0F; + dataLength = 40; + break; + } + value = 0x44400040 | (dataLength << 13) | (driver << 0); + tmc4361_writeInt(TMC4361A_SPIOUT_CONF, value); + tmc4361_writeInt(TMC4361A_CURRENT_CONF, 0x00000003); + tmc4361_writeInt(TMC4361A_SCALE_VALUES, 0x00000000); +#endif return 1; } uint8_t TMC4361A::restore() { return 1; } diff --git a/components/tmc/ic/ztmc5130.cpp b/components/tmc/ic/ztmc5130.cpp index cd2eea7..0a6370e 100644 --- a/components/tmc/ic/ztmc5130.cpp +++ b/components/tmc/ic/ztmc5130.cpp @@ -136,6 +136,10 @@ void TMC51X0::setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay) #define TMC5160_GLOBAL_SCALER 0x0B #define TMC5160_GLOBAL_SCALER_MASK 0xFF #define TMC5160_GLOBAL_SCALER_SHIFT 0 +bool TMC51X0::isTMC5130() { + uint32_t chipId = readICVersion(); + return chipId == kTMC5130; +} void TMC51X0::setGlobalScale(uint8_t globalscale) { // diff --git a/components/tmc/ic/ztmc5130.hpp b/components/tmc/ic/ztmc5130.hpp index 6a7b980..43b7495 100644 --- a/components/tmc/ic/ztmc5130.hpp +++ b/components/tmc/ic/ztmc5130.hpp @@ -41,7 +41,6 @@ class Tmc5130RampStat { Tmc5130RampStat(uint32_t state) : m_state(state) {} bool isSetted(ramp_stat_bit_t bit) { return (m_state & bit) != 0; } - }; class TMC51X0 : public IStepperMotor { @@ -138,6 +137,8 @@ class TMC51X0 : public IStepperMotor { return *(GState_t *)&value; } + bool isTMC5130(); + public: uint8_t reset(); uint32_t readICVersion(); // 5130:0x11 5160:0x30