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.

172 lines
5.8 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
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. #pragma once
  2. #if 1
  3. #include <stdbool.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "../basic/tmc_ic_interface.hpp"
  8. #include "sdk/os/zos.hpp"
  9. #include "tmc_driver_ic.hpp"
  10. extern "C" {
  11. #include "TMC2160\TMC2160.h"
  12. #include "TMC4361A\TMC4361A.h"
  13. }
  14. #ifdef HAL_SPI_MODULE_ENABLED
  15. namespace iflytop {
  16. #define TMC4361A_LISTENER_MAX 5
  17. class TMC4361A : public IStepperMotor {
  18. public:
  19. typedef enum {
  20. IC_TMC2130 = 0, //
  21. IC_TMC2160,
  22. IC_TMC2660,
  23. } driver_ic_type_t;
  24. #if 0
  25. typedef struct {
  26. SPI_HandleTypeDef *spi; //
  27. ZGPIO *csgpio; //
  28. ZGPIO *resetPin; //
  29. ZGPIO *fREEZEPin; //
  30. ZGPIO *ennPin; //
  31. ZGPIO *driverIC_ennPin; //
  32. ZGPIO *driverIC_resetPin; //
  33. } cfg_t;
  34. #endif
  35. class cfg_t {
  36. public:
  37. SPI_HandleTypeDef *spi = nullptr; //
  38. Pin_t csgpio = PinNull; //
  39. Pin_t resetPin = PinNull; //
  40. Pin_t fREEZEPin = PinNull; //
  41. Pin_t ennPin = PinNull; //
  42. Pin_t driverIC_ennPin = PinNull; //
  43. Pin_t driverIC_resetPin = PinNull; //
  44. };
  45. typedef enum {
  46. kvelmode,
  47. kposmode,
  48. } motor_mode_t;
  49. private:
  50. int32_t shadowRegister[TMC_REGISTER_COUNT];
  51. const uint8_t *m_registerAccessTable = nullptr;
  52. const int32_t *m_defaultRegisterResetState = nullptr;
  53. driver_ic_type_t m_driver_ic_type;
  54. uint32_t m_lastCallPeriodicJobTick;
  55. uint8_t m_status;
  56. SPI_HandleTypeDef *m_spi = nullptr;
  57. ZGPIO *m_csgpio = nullptr;
  58. ZGPIO *m_resetPin = nullptr;
  59. ZGPIO *m_fREEZEPin = nullptr;
  60. ZGPIO *m_ennPin = nullptr;
  61. ZGPIO *m_driverIC_ennPin = nullptr;
  62. ZGPIO *m_driverIC_resetPin = nullptr;
  63. motor_mode_t m_motor_mode = kvelmode;
  64. float m_distanceK = 1.0;
  65. public:
  66. TMC4361A(/* args */);
  67. /*******************************************************************************
  68. * ʼط *
  69. *******************************************************************************/
  70. /**
  71. * @brief ̬ĬϵTMC4361Aò,ʹʱֻ޸ԼҪIJ
  72. *
  73. * ע:
  74. * 1. ÷ڲʹõһ̬ÿεø÷ʱصĶͬһĵַ
  75. * 2. ÷صֵҪͷ
  76. * @param config
  77. */
  78. void initialize(cfg_t *cfg);
  79. void enableIC(bool enable);
  80. virtual void enable(bool enable) { enableIC(enable); }
  81. /*******************************************************************************
  82. * IStepperMotor impl *
  83. *******************************************************************************/
  84. // virtual void registerListener(MotorEventListener *listener);
  85. virtual void rotate(int32_t velocity);
  86. virtual void moveTo(int32_t position, uint32_t velocityMax);
  87. virtual void moveBy(int32_t relativePosition, uint32_t velocityMax);
  88. virtual void stop();
  89. virtual int32_t getXACTUAL();
  90. virtual int32_t getXTARGET();
  91. virtual void setXACTUAL(int32_t value);
  92. virtual int32_t getVACTUAL();
  93. virtual int32_t getENC_POS();
  94. virtual void setENC_POS(int32_t value);
  95. virtual void setAcceleration(float accelerationpps2); // �����������ٶ�,����ֵ4000000
  96. virtual void setDeceleration(float accelerationpps2); // �����������ٶ�,����ֵ4000000
  97. virtual void setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay) { driverIC_setIHOLD_IRUN(ihold, irun, iholddelay); }
  98. virtual void setMotorShaft(bool reverse) { driverIC_setMotorShaft(reverse); }
  99. virtual void setDistanceK(float distanceK);
  100. /*******************************************************************************
  101. * ʼ *
  102. *******************************************************************************/
  103. /**
  104. * @brief ʼ
  105. *
  106. * @param channel SPIͨ
  107. * @param driver_ic_type оƬ
  108. *
  109. * TMC4361A:0x2 DriverIC:0x30
  110. */
  111. int32_t readICVersion();
  112. uint8_t reset();
  113. uint8_t restore();
  114. /*******************************************************************************
  115. * Ĵд *
  116. *******************************************************************************/
  117. void writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  118. void writeInt(uint8_t address, int32_t value);
  119. int32_t readInt(uint8_t address);
  120. void readWriteCover(uint8_t *data, size_t length);
  121. void writeSubRegister(uint8_t address, uint32_t mask, uint32_t shift, uint32_t value);
  122. int32_t getENC_POS_DEV(); // ENC_POS��XACTUAL��ƫ��
  123. uint32_t readEVENTS(); // ��ȡ�¼��Ĵ�
  124. /*******************************************************
  125. * driverIc function *
  126. *******************************************************/
  127. int32_t readSubICVersion();
  128. virtual bool isReachTarget();
  129. virtual bool isStoped() { return isReachTarget(); }
  130. public:
  131. // only used in tmc4361A.cpp
  132. void tmc4361AConfigCallback(ConfigState state);
  133. void readWriteArray(uint8_t *data, size_t length);
  134. private:
  135. uint32_t haspassedms(uint32_t now, uint32_t last);
  136. void driverIC_reset();
  137. void driverIC_enableIC(bool enable);
  138. void driverIC_setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay);
  139. void driverIC_writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  140. void driverIC_writeInt(uint8_t address, int32_t value);
  141. int32_t driverIC_readInt(uint8_t address);
  142. void driverIC_setMotorShaft(bool reverse);
  143. uint32_t driverIC_readICVersion();
  144. };
  145. } // namespace iflytop
  146. #endif
  147. #endif