MOTOR_STEP_ATY.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @file MOTOR_STEP_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 "MOTOR_STEP_ATY"
  28. ********************************************************************************
  29. */
  30. #ifndef __MOTOR_STEP_ATY_H
  31. #define __MOTOR_STEP_ATY_H
  32. #include "INCLUDE_ATY.h"
  33. #include "HW_GPIO_ATY.h"
  34. #include "HW_PWM_ATY.h"
  35. /******************************* For user *************************************/
  36. // #define __DEBUG_MOTOR_STEP_ATY
  37. // #define S_PARAM_C 0.5 // 0-1
  38. // #define Motor_StartLine_2
  39. #define Motor_StartLine_T
  40. // #define Motor_StartLine_S
  41. // #define Motor_Channel PWM_T2C2
  42. // 3 max, if need more, add codes at every function
  43. #define MOTOR_COUNT 2
  44. // pin defination must include all motors
  45. // diag pin is selectable
  46. #define MOTOR_EN_PORT_0 0
  47. #define MOTOR_EN_PIN_0 P37
  48. #define MOTOR_DIR_PORT_0 0
  49. #define MOTOR_DIR_PIN_0 P21
  50. #define MOTOR_STEP_PORT_0 0
  51. #define MOTOR_STEP_PIN_0 P23
  52. #define MOTOR_DIAG_PORT_0 0
  53. #define MOTOR_DIAG_PIN_0 0
  54. #define MOTOR_PWM_Channel_0 PWM_CHANNEL_8P
  55. #define MOTOR_EN_PORT_1 0
  56. #define MOTOR_EN_PIN_1 P36
  57. #define MOTOR_DIR_PORT_1 0
  58. #define MOTOR_DIR_PIN_1 P20
  59. #define MOTOR_STEP_PORT_1 0
  60. #define MOTOR_STEP_PIN_1 P22
  61. #define MOTOR_DIAG_PORT_1 0
  62. #define MOTOR_DIAG_PIN_1 0
  63. #define MOTOR_PWM_Channel_1 PWM_CHANNEL_7P
  64. #define STATIC_STEP_LEVEL 0
  65. /******************************************************************************/
  66. typedef struct{
  67. uint8_t motorEn;
  68. uint8_t motorDir;
  69. uint8_t motorStepLevel;
  70. uint8_t motorDiag;
  71. uint32_t motorSoftSpeed;
  72. uint32_t motorSpeed;
  73. uint32_t motorSpeedCounter;
  74. uint32_t motorSoftSteps;
  75. uint32_t motorSteps;
  76. uint32_t motorStepCounter;
  77. uint32_t motorStopCounter; // delay for stall detection or driver error(TMC2209)
  78. 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
  79. float speedIncreaseStep;
  80. float S_paramA;
  81. #ifdef S_PARAM_C
  82. float S_paramC;
  83. #else
  84. float S_paramC;
  85. #endif
  86. }motorStep_t;
  87. extern motorStep_t motorStep_t_Group[MOTOR_COUNT];
  88. #define MOTOR_PARAM_SET(dir, softSpeed, speed, softSteps, steps, setState, channel) \
  89. do \
  90. { \
  91. motorStep_t_Group[channel].motorDir = (uint8_t)dir; \
  92. motorStep_t_Group[channel].motorSoftSpeed = (uint32_t)softSpeed; \
  93. motorStep_t_Group[channel].motorSpeed = (uint32_t)speed; \
  94. motorStep_t_Group[channel].motorSoftSteps = (uint32_t)softSteps; \
  95. motorStep_t_Group[channel].motorSteps = (uint32_t)steps; \
  96. motorStep_t_Group[channel].motorSetState = (uint8_t)setState; \
  97. } while(0)
  98. #define MOTOR_PARAM_GET(dir, softSpeed, speed, softSteps, steps, setState, channel) \
  99. do \
  100. { \
  101. dir = motorStep_t_Group[(uint8_t)channel].motorDir; \
  102. softSpeed = motorStep_t_Group[(uint8_t)channel].motorSoftSpeed; \
  103. speed = motorStep_t_Group[(uint8_t)channel].motorSpeed; \
  104. softSteps = motorStep_t_Group[(uint8_t)channel].motorSoftSteps; \
  105. steps = motorStep_t_Group[(uint8_t)channel].motorSteps; \
  106. setState = motorStep_t_Group[(uint8_t)channel].motorSetState; \
  107. } while(0)
  108. #define MOTOR_DIR_IN 1
  109. #define MOTOR_DIR_OUT 0
  110. void MotorSetAble(uint8_t enable, uint8_t channel);
  111. void MotorSetDirection(uint8_t dir, uint8_t channel);
  112. void MotorSetStepLevel(uint8_t level, uint8_t channel);
  113. uint8_t MotorGetDiagLevel(uint8_t channel);
  114. void MotorStart(uint8_t dir, uint32_t speed, uint8_t channel);
  115. void MotorStartSpeed(uint32_t speed, uint8_t channel);
  116. void MotorStop(uint8_t channel);
  117. void MotorStateMachine_Step(void);
  118. void MotorStateMachine_Step_PWM(void);
  119. #endif /* __MOTOR_STEP_ATY_H */
  120. /******************************** End Of File *********************************/