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.

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