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.

168 lines
5.7 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
  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. public:
  65. TMC4361A(/* args */);
  66. /*******************************************************************************
  67. * ʼط *
  68. *******************************************************************************/
  69. /**
  70. * @brief ̬ĬϵTMC4361Aò,ʹʱֻ޸ԼҪIJ
  71. *
  72. * ע:
  73. * 1. ÷ڲʹõһ̬ÿεø÷ʱصĶͬһĵַ
  74. * 2. ÷صֵҪͷ
  75. * @param config
  76. */
  77. void initialize(cfg_t *cfg);
  78. void enableIC(bool enable);
  79. virtual void enable(bool enable) { enableIC(enable); }
  80. /*******************************************************************************
  81. * IStepperMotor impl *
  82. *******************************************************************************/
  83. // virtual void registerListener(MotorEventListener *listener);
  84. virtual void rotate(int32_t velocity);
  85. virtual void moveTo(int32_t position, uint32_t velocityMax);
  86. virtual void moveBy(int32_t relativePosition, uint32_t velocityMax);
  87. virtual void stop();
  88. virtual int32_t getXACTUAL();
  89. virtual int32_t getXTARGET();
  90. virtual void setXACTUAL(int32_t value);
  91. virtual int32_t getVACTUAL();
  92. virtual int32_t getENC_POS();
  93. virtual void setENC_POS(int32_t value);
  94. virtual void setAcceleration(float accelerationpps2); // �����������ٶ�
  95. virtual void setDeceleration(float accelerationpps2); // �����������ٶ�
  96. virtual void setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay) { driverIC_setIHOLD_IRUN(ihold, irun, iholddelay); }
  97. virtual void setMotorShaft(bool reverse) { driverIC_setMotorShaft(reverse); }
  98. /*******************************************************************************
  99. * ʼ *
  100. *******************************************************************************/
  101. /**
  102. * @brief ʼ
  103. *
  104. * @param channel SPIͨ
  105. * @param driver_ic_type оƬ
  106. *
  107. * TMC4361A:0x2 DriverIC:0x30
  108. */
  109. int32_t readICVersion();
  110. uint8_t reset();
  111. uint8_t restore();
  112. /*******************************************************************************
  113. * Ĵд *
  114. *******************************************************************************/
  115. void writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  116. void writeInt(uint8_t address, int32_t value);
  117. int32_t readInt(uint8_t address);
  118. void readWriteCover(uint8_t *data, size_t length);
  119. void writeSubRegister(uint8_t address, uint32_t mask, uint32_t shift, uint32_t value);
  120. int32_t getENC_POS_DEV(); // ENC_POS��XACTUAL��ƫ��
  121. uint32_t readEVENTS(); // ��ȡ�¼��Ĵ�
  122. /*******************************************************
  123. * driverIc function *
  124. *******************************************************/
  125. int32_t readSubICVersion();
  126. virtual bool isReachTarget();
  127. virtual bool isStoped() { return isReachTarget(); }
  128. public:
  129. // only used in tmc4361A.cpp
  130. void tmc4361AConfigCallback(ConfigState state);
  131. void readWriteArray(uint8_t *data, size_t length);
  132. private:
  133. uint32_t haspassedms(uint32_t now, uint32_t last);
  134. void driverIC_reset();
  135. void driverIC_enableIC(bool enable);
  136. void driverIC_setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay);
  137. void driverIC_writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  138. void driverIC_writeInt(uint8_t address, int32_t value);
  139. int32_t driverIC_readInt(uint8_t address);
  140. void driverIC_setMotorShaft(bool reverse);
  141. uint32_t driverIC_readICVersion();
  142. };
  143. } // namespace iflytop
  144. #endif
  145. #endif