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.

149 lines
5.2 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
  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. private:
  33. int32_t shadowRegister[TMC_REGISTER_COUNT];
  34. const uint8_t *m_registerAccessTable;
  35. const int32_t *m_defaultRegisterResetState;
  36. driver_ic_type_t m_driver_ic_type;
  37. uint32_t m_lastCallPeriodicJobTick;
  38. uint8_t m_status;
  39. SPI_HandleTypeDef *m_spi;
  40. ZGPIO *m_csgpio;
  41. ZGPIO *m_resetPin;
  42. ZGPIO *m_fREEZEPin;
  43. ZGPIO *m_ennPin;
  44. ZGPIO *m_driverIC_ennPin;
  45. ZGPIO *m_driverIC_resetPin;
  46. bool m_reachtarget;
  47. public:
  48. TMC4361A(/* args */);
  49. /*******************************************************************************
  50. * *
  51. *******************************************************************************/
  52. /**
  53. * @brief TMC4361A配置参数,使
  54. *
  55. * :
  56. * 1. 使
  57. * 2.
  58. * @param config
  59. */
  60. void initialize(cfg_t *cfg);
  61. void enableIC(bool enable);
  62. /*******************************************************************************
  63. * IStepperMotor impl *
  64. *******************************************************************************/
  65. // virtual void registerListener(MotorEventListener *listener);
  66. virtual void rotate(int32_t velocity);
  67. virtual void moveTo(int32_t position, uint32_t velocityMax);
  68. virtual void moveBy(int32_t relativePosition, uint32_t velocityMax);
  69. virtual void stop();
  70. virtual int32_t getXACTUAL();
  71. virtual int32_t getXTARGET();
  72. virtual void setXACTUAL(int32_t value);
  73. virtual int32_t getVACTUAL();
  74. virtual int32_t getENC_POS();
  75. virtual void setENC_POS(int32_t value);
  76. virtual void setAcceleration(float accelerationpps2); // 设置最大加速度
  77. virtual void setDeceleration(float accelerationpps2); // 设置最大减速度
  78. /*******************************************************************************
  79. * *
  80. *******************************************************************************/
  81. /**
  82. * @brief
  83. *
  84. * @param channel SPI通道号
  85. * @param driver_ic_type
  86. */
  87. int32_t readICVersion();
  88. uint8_t reset();
  89. uint8_t restore();
  90. /*******************************************************************************
  91. * *
  92. *******************************************************************************/
  93. /*******************************************************************************
  94. * *
  95. *******************************************************************************/
  96. void writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  97. void writeInt(uint8_t address, int32_t value);
  98. int32_t readInt(uint8_t address);
  99. void readWriteCover(uint8_t *data, size_t length);
  100. void writeSubRegister(uint8_t address, uint32_t mask, uint32_t shift, uint32_t value);
  101. int32_t getENC_POS_DEV(); // ENC_POS和XACTUAL的偏差值
  102. uint32_t readEVENTS(); // 读取事件寄存器
  103. /*******************************************************
  104. * driverIc function *
  105. *******************************************************/
  106. int32_t readSubICVersion();
  107. virtual bool isReachTarget();
  108. public:
  109. // only used in tmc4361A.cpp
  110. void tmc4361AConfigCallback(ConfigState state);
  111. void readWriteArray(uint8_t *data, size_t length);
  112. void setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay) { driverIC_setIHOLD_IRUN(ihold, irun, iholddelay); }
  113. void setMotorShaft(bool reverse) { driverIC_setMotorShaft(reverse); }
  114. private:
  115. uint32_t haspassedms(uint32_t now, uint32_t last);
  116. // void callOnEventCallback(StepperMotorEvent event);
  117. void driverIC_reset();
  118. void driverIC_enableIC(bool enable);
  119. void driverIC_setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay);
  120. void driverIC_writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  121. void driverIC_writeInt(uint8_t address, int32_t value);
  122. int32_t driverIC_readInt(uint8_t address);
  123. void driverIC_setMotorShaft(bool reverse);
  124. uint32_t driverIC_readICVersion();
  125. };
  126. } // namespace iflytop
  127. #endif