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.

160 lines
5.6 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
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "../basic/tmc_ic_interface.hpp"
  7. #include "sdk/os/zos.hpp"
  8. extern "C" {
  9. #include "TMC5130\TMC5130.h"
  10. }
  11. #ifdef HAL_SPI_MODULE_ENABLED
  12. namespace iflytop {
  13. #define TMC5130_LISTENER_MAX 5
  14. /**
  15. * @brief
  16. *
  17. * ע
  18. * 1. ٶȵλ
  19. * velocityĵλ:p/t
  20. * t = 2^24/fin (finΪʱӣʱƵΪ16MHzʱt = 1.048576s)
  21. * ʱ pptԼpps
  22. *
  23. * οTMC5130A_datasheet_rev1.20%20(1)%20(3).pdf 38ҳ е
  24. *
  25. * 2. оƬλ
  26. * reset(); λоƬͬʱоƬļĴΪĬֵһ²Ҫûø÷ΪڳʼʱԶø÷
  27. *
  28. */
  29. class Tmc5130RampStat {
  30. public:
  31. uint32_t m_state;
  32. // �ο�TMC5130A_datasheet_rev1.20%20(1).pdf 40ҳ
  33. typedef enum {
  34. /**
  35. * @brief
  36. * R ֻ
  37. * R+CֻԶ
  38. */
  39. ktmc5130_rs_stopl = TMC5130_RS_STOPL, // (R )
  40. ktmc5130_rs_stopr = TMC5130_RS_STOPR, // (R )
  41. ktmc5130_rs_latchl = TMC5130_RS_LATCHL, // (R+C)
  42. ktmc5130_rs_latchr = TMC5130_RS_LATCHR, // (R+C)
  43. ktmc5130_rs_ev_stopl = TMC5130_RS_EV_STOPL, // (R )
  44. ktmc5130_rs_ev_stopr = TMC5130_RS_EV_STOPR, // (R )
  45. ktmc5130_rs_ev_stop_sg = TMC5130_RS_EV_STOP_SG, // (R+C)
  46. ktmc5130_rs_ev_posreached = TMC5130_RS_EV_POSREACHED, // (R+C)
  47. ktmc5130_rs_velreached = TMC5130_RS_VELREACHED, // (R )
  48. ktmc5130_rs_posreached = TMC5130_RS_POSREACHED, // (R ) �Ƿ񵽴�Ŀ��λ��
  49. ktmc5130_rs_vzero = TMC5130_RS_VZERO, // (R )
  50. ktmc5130_rs_zerowait = TMC5130_RS_ZEROWAIT, // (R )
  51. ktmc5130_rs_secondmove = TMC5130_RS_SECONDMOVE, // (R+C)
  52. ktmc5130_rs_sg = TMC5130_RS_SG, // (R )
  53. } ramp_stat_bit_t;
  54. Tmc5130RampStat(uint32_t state) : m_state(state) {}
  55. bool isSetted(ramp_stat_bit_t bit) { return (m_state & bit) != 0; }
  56. };
  57. class TMC5130 : public IStepperMotor {
  58. public:
  59. typedef struct {
  60. SPI_HandleTypeDef *spi;
  61. Pin_t csgpio = PinNull; //
  62. Pin_t resetPin = PinNull; //
  63. Pin_t fREEZEPin = PinNull; //
  64. Pin_t ennPin = PinNull; //
  65. Pin_t spi_mode_select = PinNull; //
  66. } cfg_t;
  67. protected:
  68. // TMC5130Port *m_port;
  69. // TMC5130Config_t *m_config;
  70. cfg_t m_cfg;
  71. ZGPIO *m_csnpin = NULL;
  72. ZGPIO *m_ennpin = NULL;
  73. ZGPIO *m_spi_mode_select_gpio = NULL;
  74. SPI_HandleTypeDef *m_hspi = NULL;
  75. // uint8_t m_channel;
  76. uint32_t m_lastCallPeriodicJobTick;
  77. int32_t m_shadowRegister[TMC_REGISTER_COUNT];
  78. const uint8_t *m_registerAccessTable;
  79. const int32_t *m_defaultRegisterResetState;
  80. float m_scale = 1.0;
  81. public:
  82. TMC5130(/* args */);
  83. // static void createDeafultTMC5130Config(TMC5130Config_t *config, TMC5130Port *m_port);
  84. void initialize(cfg_t *cfg);
  85. void enableIC(bool enable);
  86. virtual void enable(bool enable) { enableIC(enable); }
  87. uint8_t reset();
  88. /*******************************************************************************
  89. * üĴд *
  90. *******************************************************************************/
  91. uint32_t readICVersion(); // 5130:0x11 5160:0x30
  92. virtual int32_t getXACTUAL(); // ��ȡ������ǰλ��,����������λ��ֵ��ͬ����ֵ�ǵ����ڲ������������µ�λ��
  93. virtual void setXACTUAL(int32_t value); // ���õ�����ǰλ��,����������λ��ֵ��ͬ����ֵ�ǵ����ڲ������������µ�λ��
  94. virtual int32_t getVACTUAL(); // ��ȡ������ǰλ��,����������λ��ֵ��ͬ����ֵ�ǵ����ڲ������������µ�λ��
  95. uint32_t readXTARGET(); // ��ȡ������Ŀ��λ��
  96. virtual int32_t getENC_POS() { return 0; } // TODO impl it
  97. virtual void setENC_POS(int32_t value) {} // TODO impl it
  98. virtual void setScale(float scale);
  99. virtual void setAcceleration(float accelerationpps2);
  100. virtual void setDeceleration(float accelerationpps2);
  101. void setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay);
  102. void setMotorShaft(bool reverse); // ���õ�����ת����
  103. uint32_t getTMC5130_RAMPSTAT(); // �����Ĵ�����ȡ����TMC5130��״̬�Ĵ���
  104. Tmc5130RampStat getTMC5130_RAMPSTAT2();
  105. virtual void rotate(int32_t velocity);
  106. virtual void right(int32_t velocity);
  107. virtual void left(int32_t velocity);
  108. virtual void moveTo(int32_t position, uint32_t velocityMax);
  109. virtual void moveBy(int32_t relativePosition, uint32_t velocityMax);
  110. virtual void stop();
  111. virtual bool isReachTarget(); // �Ƿ񵽴�Ŀ��λ��
  112. virtual bool isStoped() { return isReachTarget(); }
  113. /*******************************************************************************
  114. * Ĵд *
  115. *******************************************************************************/
  116. // void writeSubRegister(uint8_t address, uint32_t mask, uint32_t shift, uint32_t value);
  117. void readWriteArray(uint8_t *data, size_t length);
  118. void writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  119. void writeInt(uint8_t address, int32_t value);
  120. int32_t readInt(uint8_t address);
  121. private:
  122. uint32_t haspassedms(uint32_t now, uint32_t last);
  123. int32_t to_motor_acc(int32_t acc); // rpm/s^2
  124. int32_t to_motor_vel(int32_t vel); // rpm
  125. int32_t to_motor_pos(int32_t pos); //
  126. int32_t to_user_pos(int32_t pos); //
  127. int32_t to_user_vel(int32_t vel);
  128. };
  129. } // namespace iflytop
  130. #endif