You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
6.9 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "ztmc5130.hpp"
  2. using namespace iflytop;
  3. /**
  4. * @brief TMC5130配置参数,使
  5. *
  6. * :
  7. * 1. 使
  8. * 2.
  9. * @param config
  10. */
  11. #define PRV_FIELD_WRITE(address, mask, shift, value) (writeInt(address, FIELD_SET(readInt(address), mask, shift, value)))
  12. #define PRV_FIELD_READ(address, mask, shift) FIELD_GET(readInt(address), mask, shift)
  13. #define SET_PIN(pin, val) \
  14. if (pin) { \
  15. pin->setState(val); \
  16. }
  17. TMC5130::TMC5130(/* args */) {}
  18. void TMC5130::initialize(cfg_t *cfg) {
  19. m_cfg = *cfg;
  20. // m_channel = channel;
  21. // m_config = config;
  22. // m_port = config->m_port;
  23. m_registerAccessTable = &tmc5130_defaultRegisterAccess[0];
  24. m_defaultRegisterResetState = &tmc5130_defaultRegisterResetState[0];
  25. if (cfg->csn_pin != PinNull) {
  26. m_csnpin = new ZGPIO();
  27. m_csnpin->initAsOutput(cfg->csn_pin, ZGPIO::kMode_nopull, false, true);
  28. }
  29. if (cfg->enn_pin != PinNull) {
  30. m_ennpin = new ZGPIO();
  31. m_ennpin->initAsOutput(cfg->enn_pin, ZGPIO::kMode_nopull, false, true);
  32. }
  33. ZGPIO *m_csnpin;
  34. ZGPIO *m_ennpin;
  35. enableIC(false);
  36. // tmc5130_init(&m_TMC5130, channel, &m_tmc_api_config, &tmc5130_defaultRegisterResetState[0]);
  37. // tmc5130_setCallback(&m_TMC5130, pri_tmc4361A_callback);
  38. reset();
  39. writeInt(TMC5130_PWMCONF, 0x000500C8);
  40. // writeInt( TMC5130_GCONF, 0x00000004);
  41. writeInt(TMC5130_CHOPCONF, 0x000100c3);
  42. writeInt(TMC5130_IHOLD_IRUN, 0x00051A00);
  43. writeInt(TMC5130_PWMCONF, 0x000401c8);
  44. writeInt(TMC5130_XTARGET, 0);
  45. writeInt(TMC5130_XACTUAL, 0x00000000);
  46. writeInt(TMC5130_VACTUAL, 0x00000000);
  47. writeInt(TMC5130_VSTART, 5);
  48. writeInt(TMC5130_A1, 1000);
  49. writeInt(TMC5130_V1, 0);
  50. writeInt(TMC5130_D1, 1000);
  51. writeInt(TMC5130_VSTOP, 10);
  52. writeInt(TMC5130_TZEROWAIT, 1000);
  53. setAcceleration(100000);
  54. setDeceleration(100000);
  55. setIHOLD_IRUN(2, 10, 1);
  56. enableIC(true);
  57. }
  58. void TMC5130::enableIC(bool enable) {
  59. // m_port->TMC5130Port_setENNPinState(m_channel, !enable);
  60. SET_PIN(m_ennpin, !enable);
  61. }
  62. uint8_t TMC5130::reset() {
  63. stop();
  64. // m_port->TMC5130Port_setResetNPinState(m_channel, false);
  65. SET_PIN(m_csnpin, false);
  66. // m_port->TMC5130Port_sleepus(1000);
  67. chip_delay_us(1000);
  68. // m_port->TMC5130Port_setResetNPinState(m_channel, true);
  69. SET_PIN(m_csnpin, true);
  70. for (uint32_t add = 0; add < TMC5130_REGISTER_COUNT; add++) {
  71. if (!TMC_IS_RESETTABLE(m_registerAccessTable[add])) {
  72. continue;
  73. }
  74. writeInt(add, m_defaultRegisterResetState[add]);
  75. }
  76. return 0;
  77. }
  78. int32_t TMC5130::getXACTUAL() { return readInt(TMC5130_XACTUAL); }
  79. void TMC5130::setXACTUAL(int32_t value) { writeInt(TMC5130_XACTUAL, value); }
  80. int32_t TMC5130::getVACTUAL() { return readInt(TMC5130_VACTUAL); }
  81. void TMC5130::setAcceleration(float accelerationpps2) { writeInt(TMC5130_AMAX, (int32_t)(accelerationpps2)); } // 设置最大加速度
  82. void TMC5130::setDeceleration(float accelerationpps2) { writeInt(TMC5130_DMAX, (int32_t)(accelerationpps2)); } // 设置最大减速度
  83. void TMC5130::setMotorShaft(bool reverse) { PRV_FIELD_WRITE(TMC5130_GCONF, TMC5130_SHAFT_MASK, TMC5130_SHAFT_SHIFT, reverse); }
  84. void TMC5130::setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay) {
  85. writeInt(TMC5130_IHOLD_IRUN, (iholddelay << TMC5130_IHOLDDELAY_SHIFT) | (irun << TMC5130_IRUN_SHIFT) | (ihold << TMC5130_IHOLD_SHIFT));
  86. }
  87. uint32_t TMC5130::readChipVERSION() {
  88. uint32_t chipID = PRV_FIELD_READ(TMC5130_IOIN, TMC5130_VERSION_MASK, TMC5130_VERSION_SHIFT);
  89. return chipID;
  90. }
  91. uint32_t TMC5130::getTMC5130_RAMPSTAT() { return readInt(TMC5130_RAMPSTAT); }
  92. Tmc5130RampStat TMC5130::getTMC5130_RAMPSTAT2() {
  93. uint32_t value = getTMC5130_RAMPSTAT();
  94. return Tmc5130RampStat(value);
  95. }
  96. void TMC5130::stop() { rotate(0); }
  97. void TMC5130::rotate(int32_t velocity) {
  98. writeInt(TMC5130_VMAX, abs(velocity));
  99. writeInt(TMC5130_RAMPMODE, (velocity >= 0) ? TMC5130_MODE_VELPOS : TMC5130_MODE_VELNEG);
  100. }
  101. void TMC5130::right(int32_t velocity) { rotate(velocity); }
  102. void TMC5130::left(int32_t velocity) { rotate(-velocity); }
  103. void TMC5130::moveTo(int32_t position, uint32_t velocityMax) {
  104. writeInt(TMC5130_RAMPMODE, TMC5130_MODE_POSITION);
  105. writeInt(TMC5130_VMAX, velocityMax);
  106. writeInt(TMC5130_XTARGET, position);
  107. }
  108. void TMC5130::moveBy(int32_t relativePosition, uint32_t velocityMax) { // determine actual position and add numbers of ticks to move
  109. relativePosition += readInt(TMC5130_XACTUAL);
  110. moveTo(relativePosition, velocityMax);
  111. }
  112. uint32_t TMC5130::readXTARGET() { return readInt(TMC5130_XTARGET); }
  113. uint32_t TMC5130::haspassedms(uint32_t now, uint32_t last) {
  114. if (now >= last) {
  115. return now - last;
  116. } else {
  117. return 0xFFFFFFFF - last + now;
  118. }
  119. }
  120. bool TMC5130::isReachTarget() {
  121. uint32_t state = getTMC5130_RAMPSTAT();
  122. Tmc5130RampStat event = Tmc5130RampStat(state);
  123. if (event.isSetted(Tmc5130RampStat::ktmc5130_rs_posreached)) {
  124. return true;
  125. }
  126. return false;
  127. }
  128. /*******************************************************************************
  129. * basic *
  130. *******************************************************************************/
  131. void TMC5130::writeSubRegister(uint8_t address, uint32_t mask, uint32_t shift, uint32_t value) {
  132. CriticalContext cc;
  133. writeInt(address, readInt(address) & ~mask | value << shift);
  134. }
  135. void TMC5130::readWriteArray(uint8_t *data, size_t length) {
  136. CriticalContext cc;
  137. // m_port->TMC5130Port_readWriteArray(m_channel, data, length);
  138. HAL_SPI_TransmitReceive(m_hspi, data, data, length, 100);
  139. }
  140. void TMC5130::writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4) {
  141. CriticalContext cc;
  142. uint8_t data[5] = {static_cast<uint8_t>(address | static_cast<uint8_t>(TMC5130_WRITE_BIT)), x1, x2, x3, x4};
  143. readWriteArray(&data[0], 5);
  144. int32_t value = ((uint32_t)x1 << 24) | ((uint32_t)x2 << 16) | (x3 << 8) | x4;
  145. // Write to the shadow register and mark the register dirty
  146. address = TMC_ADDRESS(address);
  147. m_shadowRegister[address] = value;
  148. }
  149. void TMC5130::writeInt(uint8_t address, int32_t value) {
  150. CriticalContext cc;
  151. writeDatagram(address, BYTE(value, 3), BYTE(value, 2), BYTE(value, 1), BYTE(value, 0));
  152. }
  153. int32_t TMC5130::readInt(uint8_t address) {
  154. CriticalContext cc;
  155. address = TMC_ADDRESS(address);
  156. // register not readable -> shadow register copy
  157. if (!TMC_IS_READABLE(tmc5130_defaultRegisterAccess[address])) return m_shadowRegister[address];
  158. uint8_t data[5] = {0, 0, 0, 0, 0};
  159. data[0] = address;
  160. readWriteArray(&data[0], 5);
  161. data[0] = address;
  162. readWriteArray(&data[0], 5);
  163. return ((uint32_t)data[1] << 24) | ((uint32_t)data[2] << 16) | (data[3] << 8) | data[4];
  164. }