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.

125 lines
4.7 KiB

12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
11 months ago
12 months ago
11 months ago
12 months ago
11 months ago
12 months ago
11 months ago
12 months ago
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. //
  8. #include "../tmc/tmc_reg_cache.hpp"
  9. #include "../tmc/tmc_type.h"
  10. #include "reg/TMC5130_Type.h"
  11. #include "stm32basic/stm32basic.hpp"
  12. namespace iflytop {
  13. /**
  14. * @brief
  15. *
  16. */
  17. class TMC51X0Cfg {
  18. public:
  19. SPI_HandleTypeDef *hspi = NULL;
  20. Pin_t csnpin;
  21. Pin_t ennpin;
  22. TMC51X0Cfg(SPI_HandleTypeDef *hspi, Pin_t csnpin, Pin_t ennpin) : hspi(hspi), csnpin(csnpin), ennpin(ennpin) {}
  23. TMC51X0Cfg() {}
  24. };
  25. class TMC51X0 {
  26. protected:
  27. TMC51X0Cfg m_cfg;
  28. TMCRegCache m_cache;
  29. SPI_HandleTypeDef *m_hspi = NULL;
  30. ZGPIO m_csnpin;
  31. ZGPIO m_ennpin;
  32. int m_mid = 0;
  33. int32_t m_scale = 10000;
  34. int32_t m_scale_deceleration = 1;
  35. mres_type_t m_MRES = kmres_256;
  36. double m_onecirclepulse = 51200;
  37. int32_t m_enc_resolution = 0; //
  38. zmutex m_mutex = {"TMC51X0"};
  39. bool m_driErr = false;
  40. bool m_isMoveToEnd = false;
  41. public:
  42. void initialize(int mid, TMC51X0Cfg cfg);
  43. private:
  44. void readWriteArray(uint8_t *data, size_t length);
  45. void writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4);
  46. void writeInt(uint8_t address, int32_t value);
  47. void writeField(uint8_t add, uint32_t mask, uint32_t shift, uint32_t value);
  48. int32_t readInt(uint8_t address);
  49. uint32_t readUInt(uint8_t address);
  50. public:
  51. bool ping();
  52. void enable(bool enable);
  53. void refreshcfg();
  54. void setdriErr(bool val) { m_driErr = val; }
  55. bool getdriErr() { return m_driErr; }
  56. /***********************************************************************************************************************
  57. * ctrl *
  58. ***********************************************************************************************************************/
  59. void rotate(int32_t velocity);
  60. void moveTo(int32_t position, uint32_t velocityMax);
  61. void moveToEnd(int32_t direction, uint32_t velocityMax);
  62. void refreshMoveToEnd();
  63. bool isMoveToEnd();
  64. void moveBy(int32_t relativePosition, uint32_t velocityMax);
  65. void stop();
  66. /***********************************************************************************************************************
  67. * get state *
  68. ***********************************************************************************************************************/
  69. int32_t getXACTUAL(); // now pos
  70. int32_t getVACTUAL(); // now vel
  71. int32_t getEncVal(); // ENCVAL
  72. TMC5130RampStat getRampStat();
  73. TMC5130DevStatusReg_t getDevStatus(); // R
  74. TMC5130GState_t getGState(); // R+C read and clear
  75. int32_t readICVersion();
  76. int32_t getXactualRAW(); // now pos 51200
  77. bool isReachTarget(TMC5130RampStat *state); // is reach target
  78. bool isTMC5130();
  79. /***********************************************************************************************************************
  80. * set state *
  81. ***********************************************************************************************************************/
  82. void setXACTUAL(int32_t value); // set pos
  83. void setMotorShaft(bool reverse);
  84. void setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay);
  85. void setGlobalScale(uint8_t globalscale); // ONLY for 5160
  86. void setScale(int32_t scale);
  87. void setScaleDenominator(int32_t scale);
  88. void setVstart(int32_t val);
  89. void setA1(int32_t val);
  90. void setAmax(int32_t val);
  91. void setV1(int32_t val);
  92. void setDmax(int32_t val);
  93. void setD1(int32_t val);
  94. void setVstop(int32_t val);
  95. void setTzerowait(int32_t val);
  96. bool setEncResolution(int32_t enc_resolution);
  97. void setEncVal(int32_t enc_val);
  98. /***********************************************************************************************************************
  99. * reg operation *
  100. ***********************************************************************************************************************/
  101. void writeIntExt(uint8_t address, int32_t value);
  102. int32_t readIntExt(uint8_t address);
  103. private:
  104. int32_t to_motor_acc(int32_t acc); // rpm/s^2
  105. int32_t to_motor_vel(int32_t vel); // rpm
  106. int32_t to_motor_pos(int32_t pos); //
  107. int32_t to_user_pos(int32_t pos); //
  108. int32_t to_user_vel(int32_t vel);
  109. };
  110. } // namespace iflytop