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.

155 lines
5.4 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
  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/hal/zhal.hpp"
  9. #include "tmc_driver_ic.hpp"
  10. extern "C" {
  11. #include "TMC2160\TMC2160.h"
  12. #include "TMC4361A\TMC4361A.h"
  13. }
  14. namespace iflytop {
  15. #define TMC4361A_LISTENER_MAX 5
  16. class TMC4361A : public IStepperMotor {
  17. public:
  18. typedef enum {
  19. IC_TMC2130 = 0, //
  20. IC_TMC2160,
  21. IC_TMC2660,
  22. } driver_ic_type_t;
  23. typedef struct {
  24. SPI_HandleTypeDef *spi;
  25. ZGPIO *csgpio;
  26. ZGPIO *resetPin;
  27. ZGPIO *fREEZEPin;
  28. ZGPIO *ennPin;
  29. ZGPIO *driverIC_ennPin;
  30. ZGPIO *driverIC_resetPin;
  31. } cfg_t;
  32. typedef enum {
  33. kvelmode,
  34. kposmode,
  35. } motor_mode_t;
  36. private:
  37. int32_t shadowRegister[TMC_REGISTER_COUNT];
  38. const uint8_t *m_registerAccessTable;
  39. const int32_t *m_defaultRegisterResetState;
  40. driver_ic_type_t m_driver_ic_type;
  41. uint32_t m_lastCallPeriodicJobTick;
  42. uint8_t m_status;
  43. SPI_HandleTypeDef *m_spi;
  44. ZGPIO *m_csgpio;
  45. ZGPIO *m_resetPin;
  46. ZGPIO *m_fREEZEPin;
  47. ZGPIO *m_ennPin;
  48. ZGPIO *m_driverIC_ennPin;
  49. ZGPIO *m_driverIC_resetPin;
  50. motor_mode_t m_motor_mode = kvelmode;
  51. public:
  52. TMC4361A(/* args */);
  53. /*******************************************************************************
  54. * *
  55. *******************************************************************************/
  56. /**
  57. * @brief TMC4361A配置参数,使
  58. *
  59. * :
  60. * 1. 使
  61. * 2.
  62. * @param config
  63. */
  64. void initialize(cfg_t *cfg);
  65. void enableIC(bool enable);
  66. /*******************************************************************************
  67. * IStepperMotor impl *
  68. *******************************************************************************/
  69. // virtual void registerListener(MotorEventListener *listener);
  70. virtual void rotate(int32_t velocity);
  71. virtual void moveTo(int32_t position, uint32_t velocityMax);
  72. virtual void moveBy(int32_t relativePosition, uint32_t velocityMax);
  73. virtual void stop();
  74. virtual int32_t getXACTUAL();
  75. virtual int32_t getXTARGET();
  76. virtual void setXACTUAL(int32_t value);
  77. virtual int32_t getVACTUAL();
  78. virtual int32_t getENC_POS();
  79. virtual void setENC_POS(int32_t value);
  80. virtual void setAcceleration(float accelerationpps2); // 设置最大加速度
  81. virtual void setDeceleration(float accelerationpps2); // 设置最大减速度
  82. /*******************************************************************************
  83. * *
  84. *******************************************************************************/
  85. /**
  86. * @brief
  87. *
  88. * @param channel SPI通道号
  89. * @param driver_ic_type
  90. */
  91. int32_t readICVersion();
  92. uint8_t reset();
  93. uint8_t restore();
  94. /*******************************************************************************
  95. * *
  96. *******************************************************************************/
  97. /*******************************************************************************
  98. * *
  99. *******************************************************************************/
  100. void writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  101. void writeInt(uint8_t address, int32_t value);
  102. int32_t readInt(uint8_t address);
  103. void readWriteCover(uint8_t *data, size_t length);
  104. void writeSubRegister(uint8_t address, uint32_t mask, uint32_t shift, uint32_t value);
  105. int32_t getENC_POS_DEV(); // ENC_POS和XACTUAL的偏差值
  106. uint32_t readEVENTS(); // 读取事件寄存器
  107. /*******************************************************
  108. * driverIc function *
  109. *******************************************************/
  110. int32_t readSubICVersion();
  111. virtual bool isReachTarget();
  112. virtual bool isStoped() { return isReachTarget(); }
  113. public:
  114. // only used in tmc4361A.cpp
  115. void tmc4361AConfigCallback(ConfigState state);
  116. void readWriteArray(uint8_t *data, size_t length);
  117. void setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay) { driverIC_setIHOLD_IRUN(ihold, irun, iholddelay); }
  118. void setMotorShaft(bool reverse) { driverIC_setMotorShaft(reverse); }
  119. private:
  120. uint32_t haspassedms(uint32_t now, uint32_t last);
  121. // void callOnEventCallback(StepperMotorEvent event);
  122. void driverIC_reset();
  123. void driverIC_enableIC(bool enable);
  124. void driverIC_setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay);
  125. void driverIC_writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  126. void driverIC_writeInt(uint8_t address, int32_t value);
  127. int32_t driverIC_readInt(uint8_t address);
  128. void driverIC_setMotorShaft(bool reverse);
  129. uint32_t driverIC_readICVersion();
  130. };
  131. } // namespace iflytop
  132. #endif