pwm.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**************************************************************************//**
  2. * @file pwm.h
  3. * @version V1.00
  4. * $Revision: 8 $
  5. * $Date: 14/01/28 10:49a $
  6. * @brief M051 series PWM driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #ifndef __PWM_H__
  12. #define __PWM_H__
  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif
  17. /** @addtogroup M051_Device_Driver M051 Device Driver
  18. @{
  19. */
  20. /** @addtogroup M051_PWM_Driver PWM Driver
  21. @{
  22. */
  23. /** @addtogroup M051_PWM_EXPORTED_CONSTANTS PWM Exported Constants
  24. @{
  25. */
  26. #define PWM_CHANNEL_NUM (4) /*!< PWM channel number */
  27. #define PWM_CLK_DIV_1 (4UL) /*!< PWM clock divide by 1 */
  28. #define PWM_CLK_DIV_2 (0UL) /*!< PWM clock divide by 2 */
  29. #define PWM_CLK_DIV_4 (1UL) /*!< PWM clock divide by 4 */
  30. #define PWM_CLK_DIV_8 (2UL) /*!< PWM clock divide by 8 */
  31. #define PWM_CLK_DIV_16 (3UL) /*!< PWM clock divide by 16 */
  32. #define PWM_EDGE_ALIGNED (0UL) /*!< PWM working in edge aligned type */
  33. #define PWM_CENTER_ALIGNED (1UL) /*!< PWM working in center aligned type */
  34. #define PWM_DUTY_TRIGGER_ADC (PWM_TCON_PWM0DTEN_Msk) /*!< PWM trigger ADC while counter matches CMR in edge-aligned or center-aligned mode */
  35. #define PWM_PERIOD_TRIGGER_ADC (PWM_TCON_PWM0TEN_Msk) /*!< PWM trigger ADC while counter matches 0 in edge-aligned mode or matches (CNR+1) or zero in center-aligned mode */
  36. #define PWM_DUTY_INT_DOWN_COUNT_MATCH_CMR (0) /*!< PWM duty interrupt triggered if down count match CMR */
  37. #define PWM_DUTY_INT_UP_COUNT_MATCH_CMR (PWM_PIER_INT01DTYPE_Msk) /*!< PWM duty interrupt triggered if up down match CMR */
  38. #define PWM_PERIOD_INT_UNDERFLOW (0) /*!< PWM period interrupt triggered if counter underflow */
  39. #define PWM_PERIOD_INT_MATCH_CNR (PWM_PIER_INT01TYPE_Msk) /*!< PWM period interrupt triggered if counter match CNR */
  40. #define PWM_CAPTURE_INT_RISING_LATCH (PWM_CCR0_CRL_IE0_Msk) /*!< PWM capture interrupt if channel has rising transition */
  41. #define PWM_CAPTURE_INT_FALLING_LATCH (PWM_CCR0_CFL_IE0_Msk) /*!< PWM capture interrupt if channel has falling transition */
  42. /*---------------------------------------------------------------------------------------------------------*/
  43. /* PWM Group channel number constants definitions */
  44. /*---------------------------------------------------------------------------------------------------------*/
  45. #define PWM_CH0 0x0 /*!< PWM Group A/B channel 0 */
  46. #define PWM_CH1 0x1 /*!< PWM Group A/B channel 1 */
  47. #define PWM_CH2 0x2 /*!< PWM Group A/B channel 2 */
  48. #define PWM_CH3 0x3 /*!< PWM Group A/B channel 3 */
  49. #define PWM_CCR_MASK 0x000F000F /*!< PWM CCR0/CCR2 bit0~3 and bit16~19 mask */
  50. /*@}*/ /* end of group M051_PWM_EXPORTED_CONSTANTS */
  51. /** @addtogroup M051_PWM_EXPORTED_FUNCTIONS PWM Exported Functions
  52. @{
  53. */
  54. /**
  55. * @brief Enable timer synchronous mode of specified channel(s)
  56. * @param[in] pwm The base address of PWM module
  57. * - PWMA : PWM Group A
  58. * - PWMB : PWM Group B
  59. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  60. * Bit 0 represents channel 0, bit 1 represents channel 1...
  61. * @return None
  62. * @details This macro is used to enable timer synchronous mode of specified channel(s)
  63. */
  64. #define PWM_ENABLE_TIMER_SYNC(pwm, u32ChannelMask) \
  65. do{ \
  66. int i;\
  67. for(i = 0; i < 4; i++) { \
  68. if((u32ChannelMask) & (1 << i)) \
  69. (pwm)->PSCR |= (PWM_PSCR_PSSEN0_Msk << (i * 8)); \
  70. } \
  71. }while(0)
  72. /**
  73. * @brief Disable timer synchronous mode of specified channel(s)
  74. * @param[in] pwm The base address of PWM module
  75. * - PWMA : PWM Group A
  76. * - PWMB : PWM Group B
  77. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  78. * Bit 0 represents channel 0, bit 1 represents channel 1...
  79. * @return None
  80. * @details This macro is used to disable timer synchronous mode of specified channel(s)
  81. */
  82. #define PWM_DISABLE_TIMER_SYNC(pwm, u32ChannelMask) \
  83. do{ \
  84. int i;\
  85. for(i = 0; i < 4; i++) { \
  86. if((u32ChannelMask) & (1 << i)) \
  87. (pwm)->PSCR &= ~(PWM_PSCR_PSSEN0_Msk << (i * 8)); \
  88. } \
  89. }while(0)
  90. /**
  91. * @brief Enable output inverter of specified channel(s)
  92. * @param[in] pwm The base address of PWM module
  93. * - PWMA : PWM Group A
  94. * - PWMB : PWM Group B
  95. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  96. * Bit 0 represents channel 0, bit 1 represents channel 1...
  97. * @return None
  98. * @details This macro is used to enable capture input inverter for specified channel(s)
  99. */
  100. #define PWM_ENABLE_OUTPUT_INVERTER(pwm, u32ChannelMask) \
  101. do{ \
  102. int i;\
  103. (pwm)->PCR &= ~(PWM_PCR_CH0INV_Msk|PWM_PCR_CH1INV_Msk|PWM_PCR_CH2INV_Msk|PWM_PCR_CH3INV_Msk);\
  104. for(i = 0; i < 4; i++) { \
  105. if((u32ChannelMask) & (1 << i)) \
  106. (pwm)->PCR |= (PWM_PCR_CH0INV_Msk << (PWM_PCR_CH0INV_Pos * (i * 4))); \
  107. } \
  108. }while(0)
  109. /**
  110. * @brief Get captured rising data of specified channel
  111. * @param[in] pwm The base address of PWM module
  112. * - PWMA : PWM Group A
  113. * - PWMB : PWM Group B
  114. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3
  115. * @return uint32_t Return the timer counter, 0~0xFFFF
  116. * @details This macro is used to get captured rising data for specified channel
  117. */
  118. #define PWM_GET_CAPTURE_RISING_DATA(pwm, u32ChannelNum) (*((__IO uint32_t *) ((((uint32_t)&((pwm)->CRLR0)) + (u32ChannelNum) * 8))))
  119. /**
  120. * @brief Get captured falling data of specified channel
  121. * @param[in] pwm The base address of PWM module
  122. * - PWMA : PWM Group A
  123. * - PWMB : PWM Group B
  124. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3
  125. * @return uint32_t Return the timer counter, 0~0xFFFF
  126. * @details This macro is used to get captured falling data for specified channel
  127. */
  128. #define PWM_GET_CAPTURE_FALLING_DATA(pwm, u32ChannelNum) (*((__IO uint32_t *) ((((uint32_t)&((pwm)->CFLR0)) + (u32ChannelNum) * 8))))
  129. /**
  130. * @brief Set the prescaler of the selected channel
  131. * @param[in] pwm The base address of PWM module
  132. * - PWMA : PWM Group A
  133. * - PWMB : PWM Group B
  134. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3
  135. * @param[in] u32Prescaler Clock prescaler of specified channel. Valid values are between 1 ~ 0xFF
  136. * @return None
  137. * @details This macro is used to set timer pre-scale for specified channel
  138. * @note - If u32Prescaler = 0, corresponding PWM-timer will be stopped
  139. * - If u32Prescaler = x (x not equal to 0), it means Clock input is divided by (x + 1) before it is fed to the corresponding PWM counter.
  140. */
  141. #define PWM_SET_PRESCALER(pwm, u32ChannelNum, u32Prescaler) \
  142. ((pwm)->PPR = ((pwm)->PPR & ~(PWM_PPR_CP01_Msk << (((u32ChannelNum) >> 1) * 8))) | ((u32Prescaler) << (((u32ChannelNum) >> 1) * 8)))
  143. /**
  144. * @brief Set the divider of the selected channel
  145. * @param[in] pwm The base address of PWM module
  146. * - PWMA : PWM Group A
  147. * - PWMB : PWM Group B
  148. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3
  149. * @param[in] u32Divider Clock divider of specified channel. Valid values are
  150. * - \ref PWM_CLK_DIV_1
  151. * - \ref PWM_CLK_DIV_2
  152. * - \ref PWM_CLK_DIV_4
  153. * - \ref PWM_CLK_DIV_8
  154. * - \ref PWM_CLK_DIV_16
  155. * @return None
  156. * @details This macro is used to set Timer clock source divider selection for specified channel
  157. */
  158. #define PWM_SET_DIVIDER(pwm, u32ChannelNum, u32Divider) \
  159. ((pwm)->CSR = ((pwm)->CSR & ~(PWM_CSR_CSR0_Msk << ((u32ChannelNum) * 4))) | ((u32Divider) << ((u32ChannelNum) * 4)))
  160. /**
  161. * @brief Set the duty of the selected channel
  162. * @param[in] pwm The base address of PWM module
  163. * - PWMA : PWM Group A
  164. * - PWMB : PWM Group B
  165. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3
  166. * @param[in] u32CMR Duty of specified channel. Valid values are between 0~0xFFFF
  167. * @return None
  168. * @details This macro is used to set PWM Comparator value for specified channel
  169. * @note This new setting will take effect on next PWM period
  170. */
  171. #define PWM_SET_CMR(pwm, u32ChannelNum, u32CMR) (*((__IO uint32_t *) ((((uint32_t)&((pwm)->CMR0)) + (u32ChannelNum) * 12))) = u32CMR)
  172. /**
  173. * @brief Set the period of the selected channel
  174. * @param[in] pwm The base address of PWM module
  175. * - PWMA : PWM Group A
  176. * - PWMB : PWM Group B
  177. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3
  178. * @param[in] u32CNR Period of specified channel. Valid values are between 0~0xFFFF
  179. * @return None
  180. * @details This macro is used to set timer loaded value(CNR) for specified channel.\n
  181. * Loaded value determines the PWM period.
  182. * @note This new setting will take effect on next PWM period
  183. * @note PWM counter will stop if period length set to 0
  184. */
  185. #define PWM_SET_CNR(pwm, u32ChannelNum, u32CNR) (*((__IO uint32_t *) ((((uint32_t)&((pwm)->CNR0)) + (u32ChannelNum) * 12))) = u32CNR)
  186. /**
  187. * @brief Set the PWM aligned type
  188. * @param[in] pwm The base address of PWM module
  189. * - PWMA : PWM Group A
  190. * - PWMB : PWM Group B
  191. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  192. * Bit 0 represents channel 0, bit 1 represents channel 1...
  193. * @param[in] u32AlignedType PWM aligned type, valid values are:
  194. * - \ref PWM_EDGE_ALIGNED
  195. * - \ref PWM_CENTER_ALIGNED
  196. * @return None
  197. * @details This macro is used to set the PWM aligned type
  198. */
  199. #define PWM_SET_ALIGNED_TYPE(pwm, u32ChannelMask, u32AlignedType) \
  200. do{ \
  201. int i; \
  202. for(i = 0; i < 4; i++) { \
  203. if((u32ChannelMask) & (1 << i)) \
  204. (pwm)->PCR = ((pwm)->PCR & ~(PWM_PCR_PWM01TYPE_Msk << (i >> 1))) | (u32AlignedType << (PWM_PCR_PWM01TYPE_Pos + (i >> 1))); \
  205. } \
  206. }while(0)
  207. uint32_t PWM_ConfigCaptureChannel(PWM_T *pwm,
  208. uint32_t u32ChannelNum,
  209. uint32_t u32UnitTimeNsec,
  210. uint32_t u32CaptureEdge);
  211. uint32_t PWM_ConfigOutputChannel(PWM_T *pwm,
  212. uint32_t u32ChannelNum,
  213. uint32_t u32Frequncy,
  214. uint32_t u32DutyCycle);
  215. void PWM_Start(PWM_T *pwm, uint32_t u32ChannelMask);
  216. void PWM_Stop(PWM_T *pwm, uint32_t u32ChannelMask);
  217. void PWM_ForceStop(PWM_T *pwm, uint32_t u32ChannelMask);
  218. void PWM_EnableADCTrigger(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition);
  219. void PWM_DisableADCTrigger(PWM_T *pwm, uint32_t u32ChannelNum);
  220. void PWM_ClearADCTriggerFlag(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition);
  221. uint32_t PWM_GetADCTriggerFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  222. void PWM_EnableCapture(PWM_T *pwm, uint32_t u32ChannelMask);
  223. void PWM_DisableCapture(PWM_T *pwm, uint32_t u32ChannelMask);
  224. void PWM_EnableOutput(PWM_T *pwm, uint32_t u32ChannelMask);
  225. void PWM_DisableOutput(PWM_T *pwm, uint32_t u32ChannelMask);
  226. void PWM_EnableDeadZone(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Duration);
  227. void PWM_DisableDeadZone(PWM_T *pwm, uint32_t u32ChannelNum);
  228. void PWM_EnableCaptureInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge);
  229. void PWM_DisableCaptureInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge);
  230. void PWM_ClearCaptureIntFlag(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge);
  231. uint32_t PWM_GetCaptureIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  232. void PWM_EnableDutyInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32IntDutyType);
  233. void PWM_DisableDutyInt(PWM_T *pwm, uint32_t u32ChannelNum);
  234. void PWM_ClearDutyIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  235. uint32_t PWM_GetDutyIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  236. void PWM_EnablePeriodInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32IntPeriodType);
  237. void PWM_DisablePeriodInt(PWM_T *pwm, uint32_t u32ChannelNum);
  238. void PWM_ClearPeriodIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  239. uint32_t PWM_GetPeriodIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  240. /*@}*/ /* end of group M051_PWM_EXPORTED_FUNCTIONS */
  241. /*@}*/ /* end of group M051_PWM_Driver */
  242. /*@}*/ /* end of group M051_Device_Driver */
  243. #ifdef __cplusplus
  244. }
  245. #endif
  246. #endif //__PWM_H__
  247. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/