TMC2209_ATY.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @file TMC2209_ATY.h
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2025 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of SteppingMotor control
  20. *
  21. * @version
  22. * - 1_01_221231 > ATY
  23. * -# Preliminary version, first Release
  24. * - 1_02_230408 > ATY
  25. * -# Add State Machine and so
  26. * - 1_03_230426 > ATY
  27. * -# Change name "SteppingMotor_ATY" to "TMC2209_ATY"
  28. * - 1_01_240111 > ATY
  29. * -# add multy addr and channel
  30. * -# add lock
  31. ********************************************************************************
  32. */
  33. #ifndef __TMC2209_ATY_H
  34. #define __TMC2209_ATY_H
  35. #include "INCLUDE_ATY.h"
  36. /******************************* For user *************************************/
  37. /* set step pin vol level when motor is not enable */
  38. // #define STATIC_STEP_LEVEL 0
  39. /******************************************************************************/
  40. struct TMC2209_ATY_Dev
  41. {
  42. uint8_t id;
  43. uint8_t motorEn;
  44. uint8_t motorDir;
  45. uint8_t motorDiag;
  46. uint32_t motorSoftSpeed;
  47. uint32_t motorSpeed;
  48. uint32_t motorSpeedCounter;
  49. uint32_t motorSoftSteps;
  50. uint32_t motorSteps;
  51. uint32_t motorStepCounter;
  52. uint32_t motorStopCounter; // delay for stall detection or driver error(TMC2209)
  53. uint8_t motorSetState; // 0: stop, 1: start hold, 2: start soft, 3: run speed, 4: change dir, 5: stop state, soft set 0 after use, 10: scram motor
  54. float speedIncreaseStep;
  55. float lineProParam;
  56. uint8_t lineProType;
  57. void (*enSet)(uint8_t level);
  58. void (*dirSet)(uint8_t level);
  59. uint8_t (*diagGet)();
  60. void (*freqSet)(uint16_t freq);
  61. uint8_t lock;
  62. uint8_t debugEnable;
  63. void (*LOG)(const char*, ...);
  64. };
  65. #define TMC2209_PARAM_SET(dir, softSpeed, speed, softSteps, steps, setState, dev) \
  66. do \
  67. { \
  68. dev->motorDir = (uint8_t)dir; \
  69. dev->motorSoftSpeed = (uint32_t)softSpeed; \
  70. dev->motorSpeed = (uint32_t)speed; \
  71. dev->motorSoftSteps = (uint32_t)softSteps; \
  72. dev->motorSteps = (uint32_t)steps; \
  73. dev->motorSetState = (uint8_t)setState; \
  74. } while(0)
  75. #define TMC2209_PARAM_GET(dir, softSpeed, speed, softSteps, steps, setState, dev) \
  76. do \
  77. { \
  78. dir = dev.motorDir; \
  79. softSpeed = dev.motorSoftSpeed; \
  80. speed = dev.motorSpeed; \
  81. softSteps = dev.motorSoftSteps; \
  82. steps = dev.motorSteps; \
  83. setState = dev.motorSetState; \
  84. } while(0)
  85. uint8_t TMC2209_SetAble(uint8_t enable, struct TMC2209_ATY_Dev *dev);
  86. uint8_t TMC2209_SetDirection(uint8_t dir, struct TMC2209_ATY_Dev *dev);
  87. uint8_t TMC2209_GetDiagLevel(struct TMC2209_ATY_Dev *dev);
  88. void TMC2209_Start(uint8_t dir, uint32_t speed, struct TMC2209_ATY_Dev *dev);
  89. void TMC2209_Stop(struct TMC2209_ATY_Dev *dev);
  90. void TMC2209_StateMachine_PWM(struct TMC2209_ATY_Dev *dev);
  91. #define TMC2209_SET_REVERSE 4
  92. #define TMC2209_SET_START_DIRECTLY 3
  93. #define TMC2209_SET_START_SOFT 2
  94. #define TMC2209_SET_STOP 0
  95. #define TMC2209_SET_SCRAM 10
  96. #define TMC2209_SET_FINISHED 5
  97. #define TMC2209_SET_RUNNING 1
  98. /* for motor soft start */
  99. #define TMC2209_S_PARAM_C 0.5 // 0-1
  100. #endif /* __TMC2209_ATY_H */
  101. /******************************** End Of File *********************************/