epwm.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**************************************************************************//**
  2. * @file epwm.h
  3. * @version V1.00
  4. * $Revision: 3 $
  5. * $Date: 15/12/02 5:23p $
  6. * @brief NUC472/NUC442 EPWM driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #ifndef __EPWM_H__
  12. #define __EPWM_H__
  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif
  17. /** @addtogroup NUC472_442_Device_Driver NUC472/NUC442 Device Driver
  18. @{
  19. */
  20. /** @addtogroup NUC472_442_EPWM_Driver EPWM Driver
  21. @{
  22. */
  23. /** @addtogroup NUC472_442_EPWM_EXPORTED_CONSTANTS EPWM Exported Constants
  24. @{
  25. */
  26. #define EPWM_CHANNEL_NUM (6) /*!< EPWM channel number \hideinitializer */
  27. #define EPWM_CH0 (0UL) /*!< EPWM channel 0 \hideinitializer */
  28. #define EPWM_CH1 (1UL) /*!< EPWM channel 1 \hideinitializer */
  29. #define EPWM_CH2 (2UL) /*!< EPWM channel 2 \hideinitializer */
  30. #define EPWM_CH3 (3UL) /*!< EPWM channel 3 \hideinitializer */
  31. #define EPWM_CH4 (4UL) /*!< EPWM channel 4 \hideinitializer */
  32. #define EPWM_CH5 (5UL) /*!< EPWM channel 5 \hideinitializer */
  33. #define EPWM_CH_0_MASK (1UL) /*!< EPWM channel 0 mask \hideinitializer */
  34. #define EPWM_CH_1_MASK (2UL) /*!< EPWM channel 1 mask \hideinitializer */
  35. #define EPWM_CH_2_MASK (4UL) /*!< EPWM channel 2 mask \hideinitializer */
  36. #define EPWM_CH_3_MASK (8UL) /*!< EPWM channel 3 mask \hideinitializer */
  37. #define EPWM_CH_4_MASK (16UL) /*!< EPWM channel 4 mask \hideinitializer */
  38. #define EPWM_CH_5_MASK (32UL) /*!< EPWM channel 5 mask \hideinitializer */
  39. #define EPWM_CH_6_MASK (64UL) /*!< EPWM channel 6 mask \hideinitializer */
  40. #define EPWM_CLK_DIV_1 (0UL) /*!< EPWM clock divide by 1 \hideinitializer */
  41. #define EPWM_CLK_DIV_2 (1UL) /*!< EPWM clock divide by 2 \hideinitializer */
  42. #define EPWM_CLK_DIV_4 (2UL) /*!< EPWM clock divide by 4 \hideinitializer */
  43. #define EPWM_CLK_DIV_16 (3UL) /*!< EPWM clock divide by 16 \hideinitializer */
  44. #define EPWM_EDGE_ALIGNED (0UL) /*!< EPWM working in edge aligned type \hideinitializer */
  45. #define EPWM_CENTER_ALIGNED (EPWM_CTL_CNTTYPE_Msk) /*!< EPWM working in center aligned type \hideinitializer */
  46. #define EPWM_BRK0_BKP0 (EPWM_CTL_BRKP0EN_Msk) /*!< Brake0 signal source from external pin BKP0 \hideinitializer */
  47. #define EPWM_BRK0_CPO0 (EPWM_CTL_CPO0BKEN_Msk) /*!< Brake0 signal source from analog comparator 0 output \hideinitializer */
  48. #define EPWM_BRK0_CPO1 (EPWM_CTL_CPO1BKEN_Msk) /*!< Brake0 signal source from analog comparator 1 output \hideinitializer */
  49. #define EPWM_BRK0_CPO2 (EPWM_CTL_CPO2BKEN_Msk) /*!< Brake0 signal source from analog comparator 2 output \hideinitializer */
  50. #define EPWM_BRK1_LVDBKEN (EPWM_CTL_LVDBKEN_Msk) /*!< Brake1 signal source from low level detection \hideinitializer */
  51. #define EPWM_BK1SEL_BKP1 (0UL << EPWM_CTL_BRK1SEL_Pos) /*!< Brake1 signal source from external pin BKP1 \hideinitializer */
  52. #define EPWM_BK1SEL_CPO0 (1UL << EPWM_CTL_BRK1SEL_Pos) /*!< Brake1 signal source from analog comparator 0 output \hideinitializer */
  53. #define EPWM_BK1SEL_CPO1 (2UL << EPWM_CTL_BRK1SEL_Pos) /*!< Brake1 signal source from analog comparator 1 output \hideinitializer */
  54. #define EPWM_BK1SEL_CPO2 (3UL << EPWM_CTL_BRK1SEL_Pos) /*!< Brake1 signal source from analog comparator 2 output \hideinitializer */
  55. #define EPWM_PERIOD_INT_UNDERFLOW (0) /*!< EPWM period interrupt trigger if counter underflow \hideinitializer */
  56. #define EPWM_PERIOD_INT_MATCH_CNR (EPWM_CTL_INTTYPE_Msk) /*!< EPWM period interrupt trigger if counter match CNR \hideinitializer */
  57. #define EPWM_MODE_INDEPENDENT (0UL) /*!< EPWM independent mode \hideinitializer */
  58. #define EPWM_MODE_COMPLEMENTARY (1UL) /*!< EPWM complementary mode \hideinitializer */
  59. #define EPWM_MODE_SYNCHRONIZED (2UL) /*!< EPWM synchronized mode \hideinitializer */
  60. /*@}*/ /* end of group NUC472_442_EPWM_EXPORTED_CONSTANTS */
  61. /** @addtogroup NUC472_442_EPWM_EXPORTED_FUNCTIONS EPWM Exported Functions
  62. @{
  63. */
  64. /**
  65. * @brief This macro enable complementary mode
  66. * @param[in] pwm The base address of PWM module
  67. * @return None
  68. * \hideinitializer
  69. */
  70. #define EPWM_ENABLE_COMPLEMENTARY_MODE(pwm) ((pwm)->CTL = ((pwm)->CTL & ~EPWM_CTL_MODE_Msk) | (EPWM_MODE_COMPLEMENTARY << EPWM_CTL_MODE_Pos))
  71. /**
  72. * @brief This macro disable complementary mode, and enable independent mode.
  73. * @param[in] pwm The base address of PWM module
  74. * @return None
  75. * \hideinitializer
  76. */
  77. #define EPWM_DISABLE_COMPLEMENTARY_MODE(pwm) ((pwm)->CTL &= ~EPWM_CTL_MODE_Msk)
  78. /**
  79. * @brief This macro enable group mode
  80. * @param[in] pwm The base address of PWM module
  81. * @return None
  82. * \hideinitializer
  83. */
  84. #define EPWM_ENABLE_GROUP_MODE(pwm) ((pwm)->CTL |= EPWM_CTL_GROUPEN_Msk)
  85. /**
  86. * @brief This macro disable group mode
  87. * @param[in] pwm The base address of PWM module
  88. * @return None
  89. * \hideinitializer
  90. */
  91. #define EPWM_DISABLE_GROUP_MODE(pwm) ((pwm)->CTL &= ~EPWM_CTL_GROUPEN_Msk)
  92. /**
  93. * @brief This macro enable synchronous mode
  94. * @param[in] pwm The base address of PWM module
  95. * @return None
  96. * \hideinitializer
  97. */
  98. #define EPWM_ENABLE_SYNC_MODE(pwm) ((pwm)->CTL = ((pwm)->CTL & ~EPWM_CTL_MODE_Msk) | (EPWM_MODE_SYNCHRONIZED << EPWM_CTL_MODE_Pos))
  99. /**
  100. * @brief This macro disable synchronous mode, and enable independent mode.
  101. * @param[in] pwm The base address of PWM module
  102. * @return None
  103. * \hideinitializer
  104. */
  105. #define EPWM_DISABLE_SYNC_MODE(pwm) ((pwm)->CTL = ((pwm)->CTL & ~EPWM_CTL_MODE_Msk))
  106. /**
  107. * @brief This macro enable output inverter of specified channel(s)
  108. * @param[in] pwm The base address of PWM module
  109. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  110. * Bit 0 represents channel 0, bit 1 represents channel 1...
  111. * @return None
  112. * \hideinitializer
  113. */
  114. #define EPWM_ENABLE_OUTPUT_INVERTER(pwm, u32ChannelMask) \
  115. do { \
  116. if ((u32ChannelMask)) \
  117. ((pwm)->CTL |= EPWM_CTL_PINV_Msk); \
  118. else \
  119. ((pwm)->CTL &= ~EPWM_CTL_PINV_Msk); \
  120. }while(0)
  121. /**
  122. * @brief This macro mask output output logic to high or low
  123. * @param[in] pwm The base address of PWM module
  124. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  125. * Bit 0 represents channel 0, bit 1 represents channel 1...
  126. * @param[in] u32LevelMask Output logic to high or low
  127. * @return None
  128. * \hideinitializer
  129. */
  130. #define EPWM_MASK_OUTPUT(pwm, u32ChannelMask, u32LevelMask) \
  131. do { \
  132. (pwm)->MSKEN = u32ChannelMask; \
  133. (pwm)->MSK = u32ChannelMask; \
  134. }while(0)
  135. /**
  136. * @brief This macro set the divider of the selected channel
  137. * @param[in] pwm The base address of PWM module
  138. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  139. * @param[in] u32Divider Clock divider of specified channel. Valid values are
  140. * - \ref EPWM_CLK_DIV_1
  141. * - \ref EPWM_CLK_DIV_2
  142. * - \ref EPWM_CLK_DIV_4
  143. * - \ref EPWM_CLK_DIV_16
  144. * @return None
  145. * \hideinitializer
  146. */
  147. #define EPWM_SET_DIVIDER(pwm, u32ChannelNum, u32Divider) \
  148. ((pwm)->CTL = ((pwm)->CTL & ~EPWM_CTL_CLKDIV_Msk) | ((u32Divider) << EPWM_CTL_CLKDIV_Pos))
  149. /**
  150. * @brief This macro set the duty of the selected channel
  151. * @param[in] pwm The base address of PWM module
  152. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  153. * @param[in] u32CMR Duty of specified channel. Valid values are between 0~0xFFFF
  154. * @return None
  155. * @note This new setting will take effect on next PWM period
  156. * \hideinitializer
  157. */
  158. #define EPWM_SET_CMR(pwm, u32ChannelNum, u32CMR) \
  159. do { \
  160. (pwm)->CMPDAT[(u32ChannelNum) >> 1] = (u32CMR); \
  161. (pwm)->CTL |= EPWM_CTL_LOAD_Msk; \
  162. }while(0)
  163. /**
  164. * @brief This macro set the period of the selected channel
  165. * @param[in] pwm The base address of PWM module
  166. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  167. * @param[in] u32CNR Period of specified channel. Valid values are between 0~0xFFFF
  168. * @return None
  169. * @note This new setting will take effect on next PWM period
  170. * @note PWM counter will stop if period length set to 0
  171. * \hideinitializer
  172. */
  173. #define EPWM_SET_CNR(pwm, u32ChannelNum, u32CNR) \
  174. do { \
  175. (pwm)->PERIOD = (u32CNR); \
  176. (pwm)->CTL |= EPWM_CTL_LOAD_Msk; \
  177. }while(0)
  178. /**
  179. * @brief This macro set the PWM aligned type
  180. * @param[in] pwm The base address of PWM module
  181. * @param[in] u32ChannelMask This parameter is not used
  182. * @param[in] u32AlignedType PWM aligned type, valid values are:
  183. * - \ref EPWM_EDGE_ALIGNED
  184. * - \ref EPWM_CENTER_ALIGNED
  185. * @return None
  186. * \hideinitializer
  187. */
  188. #define EPWM_SET_ALIGNED_TYPE(pwm, u32ChannelMask, u32AlignedType) \
  189. ((pwm)->CTL = ((pwm)->CTL & ~EPWM_CTL_CNTTYPE_Msk) | (u32AlignedType))
  190. uint32_t EPWM_ConfigOutputChannel(EPWM_T *pwm,
  191. uint32_t u32ChannelNum,
  192. uint32_t u32Frequency,
  193. uint32_t u32DutyCycle);
  194. void EPWM_Start (EPWM_T *pwm, uint32_t u32ChannelMask);
  195. void EPWM_Stop(EPWM_T *pwm, uint32_t u32ChannelMask);
  196. void EPWM_ForceStop(EPWM_T *pwm, uint32_t u32ChannelMask);
  197. void EPWM_EnableFaultBrake(EPWM_T *pwm,
  198. uint32_t u32ChannelMask,
  199. uint32_t u32LevelMask,
  200. uint32_t u32BrakeSource);
  201. void EPWM_ClearFaultBrakeFlag(EPWM_T *pwm, uint32_t u32BrakeSource);
  202. void EPWM_EnableOutput(EPWM_T *pwm, uint32_t u32ChannelMask);
  203. void EPWM_DisableOutput(EPWM_T *pwm, uint32_t u32ChannelMask);
  204. void EPWM_EnableDeadZone(EPWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Duration);
  205. void EPWM_DisableDeadZone(EPWM_T *pwm, uint32_t u32ChannelNum);
  206. void EPWM_EnableDutyInt(EPWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32IntDutyType);
  207. void EPWM_DisableDutyInt(EPWM_T *pwm, uint32_t u32ChannelNum);
  208. void EPWM_ClearDutyIntFlag(EPWM_T *pwm, uint32_t u32ChannelNum);
  209. uint32_t EPWM_GetDutyIntFlag(EPWM_T *pwm, uint32_t u32ChannelNum);
  210. void EPWM_EnableFaultBrakeInt(EPWM_T *pwm, uint32_t u32BrakeSource);
  211. void EPWM_DisableFaultBrakeInt(EPWM_T *pwm, uint32_t u32BrakeSource);
  212. void EPWM_ClearFaultBrakeIntFlag(EPWM_T *pwm, uint32_t u32BrakeSource);
  213. uint32_t EPWM_GetFaultBrakeIntFlag(EPWM_T *pwm, uint32_t u32BrakeSource);
  214. void EPWM_EnablePeriodInt(EPWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32IntPeriodType);
  215. void EPWM_DisablePeriodInt(EPWM_T *pwm, uint32_t u32ChannelNum);
  216. void EPWM_ClearPeriodIntFlag(EPWM_T *pwm, uint32_t u32ChannelNum);
  217. uint32_t EPWM_GetPeriodIntFlag(EPWM_T *pwm, uint32_t u32ChannelNum);
  218. /*@}*/ /* end of group NUC472_442_EPWM_EXPORTED_FUNCTIONS */
  219. /*@}*/ /* end of group NUC472_442_EPWM_Driver */
  220. /*@}*/ /* end of group NUC472_442_Device_Driver */
  221. #ifdef __cplusplus
  222. }
  223. #endif
  224. #endif //__EPWM_H__
  225. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/