pwm.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /**************************************************************************//**
  2. * @file pwm.h
  3. * @version V1.00
  4. * $Revision: 22 $
  5. * $Date: 15/11/16 2:08p $
  6. * @brief NUC472/NUC442 PWM driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2013 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 NUC472_442_Device_Driver NUC472/NUC442 Device Driver
  18. @{
  19. */
  20. /** @addtogroup NUC472_442_PWM_Driver PWM Driver
  21. @{
  22. */
  23. /** @addtogroup NUC472_442_PWM_EXPORTED_CONSTANTS PWM Exported Constants
  24. @{
  25. */
  26. #define PWM_CHANNEL_NUM (6) /*!< PWM channel number \hideinitializer */
  27. #define PWM_CH0 (0UL) /*!< PWM channel 0 \hideinitializer */
  28. #define PWM_CH1 (1UL) /*!< PWM channel 1 \hideinitializer */
  29. #define PWM_CH2 (2UL) /*!< PWM channel 2 \hideinitializer */
  30. #define PWM_CH3 (3UL) /*!< PWM channel 3 \hideinitializer */
  31. #define PWM_CH4 (4UL) /*!< PWM channel 4 \hideinitializer */
  32. #define PWM_CH5 (5UL) /*!< PWM channel 5 \hideinitializer */
  33. #define PWM_CH_0_MASK (1UL) /*!< PWM channel 0 mask \hideinitializer */
  34. #define PWM_CH_1_MASK (2UL) /*!< PWM channel 1 mask \hideinitializer */
  35. #define PWM_CH_2_MASK (4UL) /*!< PWM channel 2 mask \hideinitializer */
  36. #define PWM_CH_3_MASK (8UL) /*!< PWM channel 3 mask \hideinitializer */
  37. #define PWM_CH_4_MASK (16UL) /*!< PWM channel 4 mask \hideinitializer */
  38. #define PWM_CH_5_MASK (32UL) /*!< PWM channel 5 mask \hideinitializer */
  39. #define PWM_CLK_DIV_1 (4UL) /*!< PWM clock divide by 1 \hideinitializer */
  40. #define PWM_CLK_DIV_2 (0UL) /*!< PWM clock divide by 2 \hideinitializer */
  41. #define PWM_CLK_DIV_4 (1UL) /*!< PWM clock divide by 4 \hideinitializer */
  42. #define PWM_CLK_DIV_8 (2UL) /*!< PWM clock divide by 8 \hideinitializer */
  43. #define PWM_CLK_DIV_16 (3UL) /*!< PWM clock divide by 16 \hideinitializer */
  44. #define PWM_EDGE_ALIGNED (0UL) /*!< PWM working in edge aligned type \hideinitializer */
  45. #define PWM_CENTER_ALIGNED (1UL) /*!< PWM working in center aligned type \hideinitializer */
  46. #define PWM_TRIGGER_ADC_RISING_EDGE_POINT (0x1000000UL) /*!< PWM trigger ADC while output rising edge is detected \hideinitializer */
  47. #define PWM_TRIGGER_ADC_FALLING_EDGE_POINT (0x10000UL) /*!< PWM trigger ADC while output falling edge is detected \hideinitializer */
  48. #define PWM_TRIGGER_ADC_CENTER_POINT (0x100UL) /*!< PWM trigger ADC while counter matches (CNR + 1) \hideinitializer */
  49. #define PWM_TRIGGER_ADC_PERIOD_POINT (0x1UL) /*!< PWM trigger ADC while counter down count to 0 \hideinitializer */
  50. #define PWM_BRK0_BKP0 (PWM_BRKCTL_BRK0EN_Msk) /*!< Brake0 signal source from external pin BKP0 \hideinitializer */
  51. #define PWM_BRK0_CPO0 (PWM_BRKCTL_CPO0BKEN_Msk) /*!< Brake0 signal source from analog comparator 0 output \hideinitializer */
  52. #define PWM_BRK0_CPO1 (PWM_BRKCTL_CPO1BKEN_Msk) /*!< Brake0 signal source from analog comparator 1 output \hideinitializer */
  53. #define PWM_BRK0_CPO2 (PWM_BRKCTL_CPO2BKEN_Msk) /*!< Brake0 signal source from analog comparator 2 output \hideinitializer */
  54. #define PWM_BRK1_LVDBK (PWM_BRKCTL_LVDBKEN_Msk) /*!< Brake1 signal source from level detect \hideinitializer */
  55. #define PWM_BK1SEL_BKP1 (0UL << PWM_BRKCTL_BK1SEL_Pos) /*!< Brake1 signal source from external pin BKP1 \hideinitializer */
  56. #define PWM_BK1SEL_CPO0 (1UL << PWM_BRKCTL_BK1SEL_Pos) /*!< Brake1 signal source from analog comparator 0 output \hideinitializer */
  57. #define PWM_BK1SEL_CPO1 (2UL << PWM_BRKCTL_BK1SEL_Pos) /*!< Brake1 signal source from analog comparator 1 output \hideinitializer */
  58. #define PWM_PERIOD_INT_UNDERFLOW (0) /*!< PWM period interrupt trigger if counter underflow \hideinitializer */
  59. #define PWM_PERIOD_INT_MATCH_CNR (1UL) /*!< PWM period interrupt trigger if counter match CNR \hideinitializer */
  60. #define PWM_DUTY_INT_MATCH_CMR_DN (0) /*!< PWM duty interrupt if counter match CNR during down counting \hideinitializer */
  61. #define PWM_DUTY_INT_MATCH_CMR_UP (0x100UL) /*!< PWM duty interrupt if counter match CNR during up counting \hideinitializer */
  62. #define PWM_FALLING_LATCH_INT_ENABLE (0x1000000UL) /*!< PWM falling latch interrupt enable \hideinitializer */
  63. #define PWM_RISING_LATCH_INT_ENABLE (0x10000UL) /*!< PWM rising latch interrupt enable \hideinitializer */
  64. #define PWM_RISING_FALLING_LATCH_INT_ENABLE (0x1010000UL) /*!< PWM rising latch interrupt enable \hideinitializer */
  65. #define PWM_FALLING_LATCH_INT_FLAG (PWM_FALLING_LATCH_INT_ENABLE) /*!< PWM falling latch condition happened \hideinitializer */
  66. #define PWM_RISING_LATCH_INT_FLAG (PWM_RISING_LATCH_INT_ENABLE) /*!< PWM rising latch condition happened \hideinitializer */
  67. #define PWM_RISING_FALLING_LATCH_INT_FLAG (PWM_RISING_FALLING_LATCH_INT_ENABLE) /*!< PWM rising latch condition happened \hideinitializer */
  68. /*@}*/ /* end of group NUC472_442_PWM_EXPORTED_CONSTANTS */
  69. /** @addtogroup NUC472_442_PWM_EXPORTED_FUNCTIONS PWM Exported Functions
  70. @{
  71. */
  72. /**
  73. * @brief This macro enable complementary mode
  74. * @param[in] pwm The base address of PWM module
  75. * @return None
  76. * \hideinitializer
  77. */
  78. #define PWM_ENABLE_COMPLEMENTARY_MODE(pwm) ((pwm)->CTL = (pwm)->CTL | PWM_CTL_OUTMODE_Msk)
  79. /**
  80. * @brief This macro disable complementary mode, and enable independent mode.
  81. * @param[in] pwm The base address of PWM module
  82. * @return None
  83. * \hideinitializer
  84. */
  85. #define PWM_DISABLE_COMPLEMENTARY_MODE(pwm) ((pwm)->CTL = (pwm)->CTL & ~PWM_CTL_OUTMODE_Msk)
  86. /**
  87. * @brief This macro enable group mode
  88. * @param[in] pwm The base address of PWM module
  89. * @return None
  90. * \hideinitializer
  91. */
  92. #define PWM_ENABLE_GROUP_MODE(pwm) ((pwm)->CTL = (pwm)->CTL | PWM_CTL_GROUPEN_Msk)
  93. /**
  94. * @brief This macro disable group mode
  95. * @param[in] pwm The base address of PWM module
  96. * @return None
  97. * \hideinitializer
  98. */
  99. #define PWM_DISABLE_GROUP_MODE(pwm) ((pwm)->CTL = (pwm)->CTL & ~PWM_CTL_GROUPEN_Msk)
  100. /**
  101. * @brief This macro enable synchronous mode
  102. * @param[in] pwm The base address of PWM module
  103. * @return None
  104. * \hideinitializer
  105. */
  106. #define PWM_ENABLE_SYNC_MODE(pwm) ((pwm)->CTL = (pwm)->CTL | PWM_CTL_SYNCEN_Msk)
  107. /**
  108. * @brief This macro disable synchronous mode, and enable independent mode.
  109. * @param[in] pwm The base address of PWM module
  110. * @return None
  111. * \hideinitializer
  112. */
  113. #define PWM_DISABLE_SYNC_MODE(pwm) ((pwm)->CTL = (pwm)->CTL & ~PWM_CTL_SYNCEN_Msk)
  114. /**
  115. * @brief This macro enable output inverter of specified channel(s)
  116. * @param[in] pwm The base address of PWM module
  117. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  118. * Bit 0 represents channel 0, bit 1 represents channel 1...
  119. * @return None
  120. * \hideinitializer
  121. */
  122. #define PWM_ENABLE_OUTPUT_INVERTER(pwm, u32ChannelMask) ((pwm)->CTL = (((pwm)->CTL & ~PWM_CTL_PINV_Msk) | ((u32ChannelMask) << PWM_CTL_PINV_Pos)))
  123. /**
  124. * @brief This macro get captured rising data
  125. * @param[in] pwm The base address of PWM module
  126. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  127. * @return None
  128. * \hideinitializer
  129. */
  130. #define PWM_GET_CAPTURE_RISING_DATA(pwm, u32ChannelNum) (*(__IO uint32_t *) (&(pwm)->RCAPDAT0 + 2 * (u32ChannelNum)))
  131. /**
  132. * @brief This macro get captured falling data
  133. * @param[in] pwm The base address of PWM module
  134. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  135. * @return None
  136. * \hideinitializer
  137. */
  138. #define PWM_GET_CAPTURE_FALLING_DATA(pwm, u32ChannelNum) (*(__IO uint32_t *) (&(pwm)->FCAPDAT0 + 2 * (u32ChannelNum)))
  139. /**
  140. * @brief This macro mask output output logic to high or low
  141. * @param[in] pwm The base address of PWM module
  142. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  143. * Bit 0 represents channel 0, bit 1 represents channel 1...
  144. * @param[in] u32LevelMask Output logic to high or low
  145. * @return None
  146. * \hideinitializer
  147. */
  148. #define PWM_MASK_OUTPUT(pwm, u32ChannelMask, u32LevelMask) ((pwm)->MSKEN |= (u32ChannelMask))
  149. /**
  150. * @brief This macro set the prescaler 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] u32Prescaler Clock prescaler of specified channel. Valid values are between 1 ~ 0xFF
  154. * @return None
  155. * @note Every even channel N, and channel (N + 1) share a prescaler. So if channel 0 prescaler changed,
  156. * channel 1 will also be affected.
  157. * \hideinitializer
  158. */
  159. #define PWM_SET_PRESCALER(pwm, u32ChannelNum, u32Prescaler) \
  160. (pwm->CLKPSC = ((pwm)->CLKPSC & ~(PWM_CLKPSC_CLKPSC01_Msk << (((u32ChannelNum) >> 1) * 8))) | ((u32Prescaler) << (((u32ChannelNum) >> 1) * 8)))
  161. /**
  162. * @brief This macro set the divider of the selected channel
  163. * @param[in] pwm The base address of PWM module
  164. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  165. * @param[in] u32Divider Clock divider of specified channel. Valid values are
  166. * - \ref PWM_CLK_DIV_1
  167. * - \ref PWM_CLK_DIV_2
  168. * - \ref PWM_CLK_DIV_4
  169. * - \ref PWM_CLK_DIV_8
  170. * - \ref PWM_CLK_DIV_16
  171. * @return None
  172. * \hideinitializer
  173. */
  174. #define PWM_SET_DIVIDER(pwm, u32ChannelNum, u32Divider) \
  175. ((pwm)->CLKDIV = ((pwm)->CLKDIV & ~(PWM_CLKDIV_CLKDIV0_Msk << ((u32ChannelNum) * 4))) | ((u32Divider) << ((u32ChannelNum) * 4)))
  176. /**
  177. * @brief This macro set the duty of the selected channel
  178. * @param[in] pwm The base address of PWM module
  179. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  180. * @param[in] u32CMR Duty of specified channel. Valid values are between 0~0xFFFF
  181. * @return None
  182. * @note This new setting will take effect on next PWM period
  183. * \hideinitializer
  184. */
  185. #define PWM_SET_CMR(pwm, u32ChannelNum, u32CMR) ((pwm)->CMPDAT[(u32ChannelNum)] = (u32CMR))
  186. /**
  187. * @brief This macro set the period of the selected channel
  188. * @param[in] pwm The base address of PWM module
  189. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  190. * @param[in] u32CNR Period of specified channel. Valid values are between 0~0xFFFF
  191. * @return None
  192. * @note This new setting will take effect on next PWM period
  193. * @note PWM counter will stop if period length set to 0
  194. * \hideinitializer
  195. */
  196. #define PWM_SET_CNR(pwm, u32ChannelNum, u32CNR) ((pwm)->PERIOD[(u32ChannelNum)] = (u32CNR))
  197. /**
  198. * @brief This macro set the PWM aligned type
  199. * @param[in] pwm The base address of PWM module
  200. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  201. * Bit 0 represents channel 0, bit 1 represents channel 1...
  202. * @param[in] u32AlignedType PWM aligned type, valid values are:
  203. * - \ref PWM_EDGE_ALIGNED
  204. * - \ref PWM_CENTER_ALIGNED
  205. * @return None
  206. * \hideinitializer
  207. */
  208. #define PWM_SET_ALIGNED_TYPE(pwm, u32ChannelMask, u32AlignedType) \
  209. do { \
  210. (pwm)->CTL = ((pwm)->CTL & ~PWM_CTL_CNTTYPE_Msk); \
  211. if ((u32AlignedType) == PWM_CENTER_ALIGNED) \
  212. (pwm)->CTL = ((pwm)->CTL | ((u32ChannelMask) << PWM_CTL_CNTTYPE_Pos)); \
  213. } while(0)
  214. uint32_t PWM_ConfigOutputChannel(PWM_T *pwm,
  215. uint32_t u32ChannelNum,
  216. uint32_t u32Frequency,
  217. uint32_t u32DutyCycle);
  218. uint32_t PWM_ConfigCaptureChannel (PWM_T *pwm,
  219. uint32_t u32ChannelNum,
  220. uint32_t u32UnitTimeNsec,
  221. uint32_t u32CaptureEdge);
  222. void PWM_Start(PWM_T *pwm, uint32_t u32ChannelMask);
  223. void PWM_Stop(PWM_T *pwm, uint32_t u32ChannelMask);
  224. void PWM_ForceStop(PWM_T *pwm, uint32_t u32ChannelMask);
  225. void PWM_EnableADCTrigger(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition);
  226. void PWM_DisableADCTrigger(PWM_T *pwm, uint32_t u32ChannelNum);
  227. void PWM_ClearADCTriggerFlag(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition);
  228. uint32_t PWM_GetADCTriggerFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  229. void PWM_EnableFaultBrake(PWM_T *pwm,
  230. uint32_t u32ChannelMask,
  231. uint32_t u32LevelMask,
  232. uint32_t u32BrakeSource);
  233. void PWM_ClearFaultBrakeFlag(PWM_T *pwm, uint32_t u32BrakeSource);
  234. void PWM_EnableCapture(PWM_T *pwm, uint32_t u32ChannelMask);
  235. void PWM_DisableCapture(PWM_T *pwm, uint32_t u32ChannelMask);
  236. void PWM_EnableOutput(PWM_T *pwm, uint32_t u32ChannelMask);
  237. void PWM_DisableOutput(PWM_T *pwm, uint32_t u32ChannelMask);
  238. void PWM_EnableDeadZone(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Duration);
  239. void PWM_DisableDeadZone(PWM_T *pwm, uint32_t u32ChannelNum);
  240. void PWM_EnableCaptureInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge);
  241. void PWM_DisableCaptureInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge);
  242. void PWM_ClearCaptureIntFlag(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge);
  243. uint32_t PWM_GetCaptureIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  244. void PWM_EnableDutyInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32IntDutyType);
  245. void PWM_DisableDutyInt(PWM_T *pwm, uint32_t u32ChannelNum);
  246. void PWM_ClearDutyIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  247. uint32_t PWM_GetDutyIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  248. void PWM_EnableFaultBrakeInt(PWM_T *pwm, uint32_t u32BrakeSource);
  249. void PWM_DisableFaultBrakeInt(PWM_T *pwm, uint32_t u32BrakeSource);
  250. void PWM_ClearFaultBrakeIntFlag(PWM_T *pwm, uint32_t u32BrakeSource);
  251. uint32_t PWM_GetFaultBrakeIntFlag(PWM_T *pwm, uint32_t u32BrakeSource);
  252. void PWM_EnablePeriodInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32IntPeriodType);
  253. void PWM_DisablePeriodInt(PWM_T *pwm, uint32_t u32ChannelNum);
  254. void PWM_ClearPeriodIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  255. uint32_t PWM_GetPeriodIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  256. /*@}*/ /* end of group NUC472_442_PWM_EXPORTED_FUNCTIONS */
  257. /*@}*/ /* end of group NUC472_442_PWM_Driver */
  258. /*@}*/ /* end of group NUC472_442_Device_Driver */
  259. #ifdef __cplusplus
  260. }
  261. #endif
  262. #endif //__PWM_H__
  263. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/