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.

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