lib_pwm.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. ******************************************************************************
  3. * @file lib_pwm.h
  4. * @author Application Team
  5. * @version V4.4.0
  6. * @date 2018-09-27
  7. * @brief PWM library.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. ******************************************************************************
  12. */
  13. #ifndef __LIB_PWM_H
  14. #define __LIB_PWM_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "target.h"
  19. typedef struct
  20. {
  21. uint32_t ClockDivision;
  22. uint32_t Mode;
  23. uint32_t ClockSource;
  24. } PWM_BaseInitType;
  25. //ClockDivision
  26. #define PWM_CLKDIV_2 PWM_CTL_ID_DIV2
  27. #define PWM_CLKDIV_4 PWM_CTL_ID_DIV4
  28. #define PWM_CLKDIV_8 PWM_CTL_ID_DIV8
  29. #define PWM_CLKDIV_16 PWM_CTL_ID_DIV16
  30. //Mode
  31. #define PWM_MODE_STOP PWM_CTL_MC_STOP
  32. #define PWM_MODE_UPCOUNT PWM_CTL_MC_UP
  33. #define PWM_MODE_CONTINUOUS PWM_CTL_MC_CONTINUE
  34. #define PWM_MODE_UPDOWN PWM_CTL_MC_UPDOWN
  35. //ClockSource
  36. #define PWM_CLKSRC_APB PWM_CTL_TESL_APBDIV1
  37. #define PWM_CLKSRC_APBD128 PWM_CTL_TESL_APBDIV128
  38. typedef struct
  39. {
  40. uint32_t Period;
  41. uint32_t OutMode;
  42. } PWM_OCInitType;
  43. //OUTMOD
  44. #define PWM_OUTMOD_CONST PWM_CCTL_OUTMOD_CONST
  45. #define PWM_OUTMOD_SET PWM_CCTL_OUTMOD_SET
  46. #define PWM_OUTMOD_TOGGLE_RESET PWM_CCTL_OUTMOD_TOGGLE_RESET
  47. #define PWM_OUTMOD_SET_RESET PWM_CCTL_OUTMOD_SET_RESET
  48. #define PWM_OUTMOD_TOGGLE PWM_CCTL_OUTMOD_TOGGLE
  49. #define PWM_OUTMOD_RESET PWM_CCTL_OUTMOD_RESET
  50. #define PWM_OUTMOD_TOGGLE_SET PWM_CCTL_OUTMOD_TOGGLE_SET
  51. #define PWM_OUTMOD_RESET_SET PWM_CCTL_OUTMOD_RESET_SET
  52. //PWM CHANNEL
  53. #define PWM_CHANNEL_0 0
  54. #define PWM_CHANNEL_1 1
  55. #define PWM_CHANNEL_2 2
  56. #define PWM_OSEL0_T0O0 (0<<0)
  57. #define PWM_OSEL0_T0O1 (1<<0)
  58. #define PWM_OSEL0_T0O2 (2<<0)
  59. #define PWM_OSEL0_T1O0 (4<<0)
  60. #define PWM_OSEL0_T1O1 (5<<0)
  61. #define PWM_OSEL0_T1O2 (6<<0)
  62. #define PWM_OSEL0_T2O0 (8<<0)
  63. #define PWM_OSEL0_T2O1 (9<<0)
  64. #define PWM_OSEL0_T2O2 (10<<0)
  65. #define PWM_OSEL0_T3O0 (12<<0)
  66. #define PWM_OSEL0_T3O1 (13<<0)
  67. #define PWM_OSEL0_T3O2 (14<<0)
  68. //outline
  69. #define PWM_OLINE_0 1
  70. #define PWM_OLINE_1 2
  71. #define PWM_OLINE_2 4
  72. #define PWM_OLINE_3 8
  73. #define PWM_OLINE_Msk 0xF
  74. //PWM output selection
  75. #define PWM0_OUT0 PWM_OSEL0_T0O0
  76. #define PWM0_OUT1 PWM_OSEL0_T0O1
  77. #define PWM0_OUT2 PWM_OSEL0_T0O2
  78. #define PWM1_OUT0 PWM_OSEL0_T1O0
  79. #define PWM1_OUT1 PWM_OSEL0_T1O1
  80. #define PWM1_OUT2 PWM_OSEL0_T1O2
  81. #define PWM2_OUT0 PWM_OSEL0_T2O0
  82. #define PWM2_OUT1 PWM_OSEL0_T2O1
  83. #define PWM2_OUT2 PWM_OSEL0_T2O2
  84. #define PWM3_OUT0 PWM_OSEL0_T3O0
  85. #define PWM3_OUT1 PWM_OSEL0_T3O1
  86. #define PWM3_OUT2 PWM_OSEL0_T3O2
  87. //Level
  88. #define PWM_LEVEL_HIGH PWM_CCTL_OUT
  89. #define PWM_LEVEL_LOW 0
  90. /* Private macros ------------------------------------------------------------*/
  91. #define IS_PWM_CLKDIV(__CLKDIV__) (((__CLKDIV__) == PWM_CLKDIV_2) ||\
  92. ((__CLKDIV__) == PWM_CLKDIV_4) ||\
  93. ((__CLKDIV__) == PWM_CLKDIV_8) ||\
  94. ((__CLKDIV__) == PWM_CLKDIV_16))
  95. #define IS_PWM_CNTMODE(__CNTMODE__) (((__CNTMODE__) == PWM_MODE_STOP) ||\
  96. ((__CNTMODE__) == PWM_MODE_UPCOUNT) ||\
  97. ((__CNTMODE__) == PWM_MODE_CONTINUOUS) ||\
  98. ((__CNTMODE__) == PWM_MODE_UPDOWN))
  99. #define IS_PWM_CLKSRC(__CLKSRC__) (((__CLKSRC__) == PWM_CLKSRC_APB) ||\
  100. ((__CLKSRC__) == PWM_CLKSRC_APBD128))
  101. #define IS_PWM_OUTMODE(__OUTMODE__) (((__OUTMODE__) == PWM_OUTMOD_CONST) ||\
  102. ((__OUTMODE__) == PWM_OUTMOD_SET) ||\
  103. ((__OUTMODE__) == PWM_OUTMOD_TOGGLE_RESET) ||\
  104. ((__OUTMODE__) == PWM_OUTMOD_SET_RESET) ||\
  105. ((__OUTMODE__) == PWM_OUTMOD_TOGGLE) ||\
  106. ((__OUTMODE__) == PWM_OUTMOD_RESET) ||\
  107. ((__OUTMODE__) == PWM_OUTMOD_TOGGLE_SET) ||\
  108. ((__OUTMODE__) == PWM_OUTMOD_RESET_SET))
  109. #define IS_PWM_CCR(__CCR__) ((__CCR__) < 0x10000U)
  110. #define IS_PWM_CHANNEL(__CHANNEL__) (((__CHANNEL__) == PWM_CHANNEL_0) ||\
  111. ((__CHANNEL__) == PWM_CHANNEL_1) ||\
  112. ((__CHANNEL__) == PWM_CHANNEL_2))
  113. #define IS_PWM_OUTLINE(__OUTLINE__) ((((__OUTLINE__) & PWM_OLINE_Msk) != 0U) &&\
  114. (((__OUTLINE__) & ~PWM_OLINE_Msk) == 0U))
  115. #define IS_PWM_OUTSEL(__OUTSEL__) (((__OUTSEL__) == PWM0_OUT0) ||\
  116. ((__OUTSEL__) == PWM0_OUT1) ||\
  117. ((__OUTSEL__) == PWM0_OUT2) ||\
  118. ((__OUTSEL__) == PWM1_OUT0) ||\
  119. ((__OUTSEL__) == PWM1_OUT1) ||\
  120. ((__OUTSEL__) == PWM1_OUT2) ||\
  121. ((__OUTSEL__) == PWM2_OUT0) ||\
  122. ((__OUTSEL__) == PWM2_OUT1) ||\
  123. ((__OUTSEL__) == PWM2_OUT2) ||\
  124. ((__OUTSEL__) == PWM3_OUT0) ||\
  125. ((__OUTSEL__) == PWM3_OUT1) ||\
  126. ((__OUTSEL__) == PWM3_OUT2))
  127. #define IS_PWM_OUTLVL(__OUTLVL__) (((__OUTLVL__) == PWM_LEVEL_HIGH) ||\
  128. ((__OUTLVL__) == PWM_LEVEL_LOW))
  129. /* Exported Functions ------------------------------------------------------- */
  130. /* PWM Exported Functions Group1:
  131. Initialization ----------------------------*/
  132. void PWM_BaseInit(PWM_TypeDef *PWMx, PWM_BaseInitType *InitStruct);
  133. void PWM_BaseStructInit(PWM_BaseInitType *InitStruct);
  134. void PWM_OC0Init(PWM_TypeDef *PWMx, PWM_OCInitType *OCInitType);
  135. void PWM_OC1Init(PWM_TypeDef *PWMx, PWM_OCInitType *OCInitType);
  136. void PWM_OC2Init(PWM_TypeDef *PWMx, PWM_OCInitType *OCInitType);
  137. void PWM_OCStructInit(PWM_OCInitType *OCInitType);
  138. /* PWM Exported Functions Group2:
  139. Interrupt ---------------------------------*/
  140. void PWM_BaseINTConfig(PWM_TypeDef *PWMx, uint32_t NewState);
  141. uint8_t PWM_GetBaseINTStatus(PWM_TypeDef *PWMx);
  142. void PWM_ClearBaseINTStatus(PWM_TypeDef *PWMx);
  143. void PWM_ChannelINTConfig(PWM_TypeDef *PWMx, uint32_t Channel, uint32_t NewState);
  144. uint8_t PWM_GetChannelINTStatus(PWM_TypeDef *PWMx, uint32_t Channel);
  145. void PWM_ClearChannelINTStatus(PWM_TypeDef *PWMx, uint32_t Channel);
  146. /* PWM Exported Functions Group3:
  147. MISC --------------------------------------*/
  148. void PWM_ClearCounter(PWM_TypeDef *PWMx);
  149. void PWM_CCRConfig(PWM_TypeDef *PWMx, uint32_t Channel, uint16_t Period);
  150. //Compare output
  151. void PWM_OLineConfig(uint32_t OutSelection, uint32_t OLine);
  152. void PWM_OutputCmd(PWM_TypeDef *PWMx, uint32_t Channel, uint32_t NewState);
  153. void PWM_SetOutLevel(PWM_TypeDef *PWMx, uint32_t Channel, uint32_t Level);
  154. #ifdef __cplusplus
  155. }
  156. #endif
  157. #endif /* __LIB_PWM_H */
  158. /*********************************** END OF FILE ******************************/