pwm.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /**************************************************************************//**
  2. * @file pwm.h
  3. * @version V1.00
  4. * $Revision: 26 $
  5. * $Date: 15/08/11 10:26a $
  6. * @brief M451 series PWM driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2014~2015 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 Standard_Driver Standard Driver
  18. @{
  19. */
  20. /** @addtogroup PWM_Driver PWM Driver
  21. @{
  22. */
  23. /** @addtogroup PWM_EXPORTED_CONSTANTS PWM Exported Constants
  24. @{
  25. */
  26. #define PWM_CHANNEL_NUM (6) /*!< PWM channel number */
  27. #define PWM_CH_0_MASK (0x1UL) /*!< PWM channel 0 mask \hideinitializer */
  28. #define PWM_CH_1_MASK (0x2UL) /*!< PWM channel 1 mask \hideinitializer */
  29. #define PWM_CH_2_MASK (0x4UL) /*!< PWM channel 2 mask \hideinitializer */
  30. #define PWM_CH_3_MASK (0x8UL) /*!< PWM channel 3 mask \hideinitializer */
  31. #define PWM_CH_4_MASK (0x10UL) /*!< PWM channel 4 mask \hideinitializer */
  32. #define PWM_CH_5_MASK (0x20UL) /*!< PWM channel 5 mask \hideinitializer */
  33. /*---------------------------------------------------------------------------------------------------------*/
  34. /* Counter Type Constant Definitions */
  35. /*---------------------------------------------------------------------------------------------------------*/
  36. #define PWM_UP_COUNTER (0UL) /*!< Up counter type */
  37. #define PWM_DOWN_COUNTER (1UL) /*!< Down counter type */
  38. #define PWM_UP_DOWN_COUNTER (2UL) /*!< Up-Down counter type */
  39. /*---------------------------------------------------------------------------------------------------------*/
  40. /* Aligned Type Constant Definitions */
  41. /*---------------------------------------------------------------------------------------------------------*/
  42. #define PWM_EDGE_ALIGNED (1UL) /*!< PWM working in edge aligned type(down count) */
  43. #define PWM_CENTER_ALIGNED (2UL) /*!< PWM working in center aligned type */
  44. /*---------------------------------------------------------------------------------------------------------*/
  45. /* Output Level Constant Definitions */
  46. /*---------------------------------------------------------------------------------------------------------*/
  47. #define PWM_OUTPUT_NOTHING (0UL) /*!< PWM output nothing */
  48. #define PWM_OUTPUT_LOW (1UL) /*!< PWM output low */
  49. #define PWM_OUTPUT_HIGH (2UL) /*!< PWM output high */
  50. #define PWM_OUTPUT_TOGGLE (3UL) /*!< PWM output toggle */
  51. /*---------------------------------------------------------------------------------------------------------*/
  52. /* Trigger Source Select Constant Definitions */
  53. /*---------------------------------------------------------------------------------------------------------*/
  54. #define PWM_TRIGGER_ADC_EVEN_ZERO_POINT (0UL) /*!< PWM trigger ADC while counter of even channel matches zero point */
  55. #define PWM_TRIGGER_ADC_EVEN_PERIOD_POINT (1UL) /*!< PWM trigger ADC while counter of even channel matches period point */
  56. #define PWM_TRIGGER_ADC_EVEN_ZERO_OR_PERIOD_POINT (2UL) /*!< PWM trigger ADC while counter of even channel matches zero or period point */
  57. #define PWM_TRIGGER_ADC_EVEN_COMPARE_UP_COUNT_POINT (3UL) /*!< PWM trigger ADC while counter of even channel matches up count to comparator point */
  58. #define PWM_TRIGGER_ADC_EVEN_COMPARE_DOWN_COUNT_POINT (4UL) /*!< PWM trigger ADC while counter of even channel matches down count to comparator point */
  59. #define PWM_TRIGGER_ADC_ODD_ZERO_POINT (5UL) /*!< PWM trigger ADC while counter of odd channel matches zero point */
  60. #define PWM_TRIGGER_ADC_ODD_PERIOD_POINT (6UL) /*!< PWM trigger ADC while counter of odd channel matches period point */
  61. #define PWM_TRIGGER_ADC_ODD_ZERO_OR_PERIOD_POINT (7UL) /*!< PWM trigger ADC while counter of odd channel matches zero or period point */
  62. #define PWM_TRIGGER_ADC_ODD_COMPARE_UP_COUNT_POINT (8UL) /*!< PWM trigger ADC while counter of odd channel matches up count to comparator point */
  63. #define PWM_TRIGGER_ADC_ODD_COMPARE_DOWN_COUNT_POINT (9UL) /*!< PWM trigger ADC while counter of odd channel matches down count to comparator point */
  64. #define PWM_TRIGGER_ADC_CH_0_FREE_COMPARE_UP_COUNT_POINT (10UL) /*!< PWM trigger ADC while counter of channel 0 matches up count to free comparator point */
  65. #define PWM_TRIGGER_ADC_CH_0_FREE_COMPARE_DOWN_COUNT_POINT (11UL) /*!< PWM trigger ADC while counter of channel 0 matches down count to free comparator point */
  66. #define PWM_TRIGGER_ADC_CH_2_FREE_COMPARE_UP_COUNT_POINT (12UL) /*!< PWM trigger ADC while counter of channel 2 matches up count to free comparator point */
  67. #define PWM_TRIGGER_ADC_CH_2_FREE_COMPARE_DOWN_COUNT_POINT (13UL) /*!< PWM trigger ADC while counter of channel 2 matches down count to free comparator point */
  68. #define PWM_TRIGGER_ADC_CH_4_FREE_COMPARE_UP_COUNT_POINT (14UL) /*!< PWM trigger ADC while counter of channel 4 matches up count to free comparator point */
  69. #define PWM_TRIGGER_ADC_CH_4_FREE_COMPARE_DOWN_COUNT_POINT (15UL) /*!< PWM trigger ADC while counter of channel 4 matches down count to free comparator point */
  70. #define PWM_TRIGGER_DAC_ZERO_POINT (0x1UL) /*!< PWM trigger ADC while counter down count to 0 \hideinitializer */
  71. #define PWM_TRIGGER_DAC_PERIOD_POINT (0x100UL) /*!< PWM trigger ADC while counter matches (PERIOD + 1) \hideinitializer */
  72. #define PWM_TRIGGER_DAC_COMPARE_UP_COUNT_POINT (0x10000UL) /*!< PWM trigger ADC while counter up count to CMPDAT \hideinitializer */
  73. #define PWM_TRIGGER_DAC_COMPARE_DOWN_COUNT_POINT (0x1000000UL) /*!< PWM trigger ADC while counter down count to CMPDAT \hideinitializer */
  74. /*---------------------------------------------------------------------------------------------------------*/
  75. /* Fail brake Control Constant Definitions */
  76. /*---------------------------------------------------------------------------------------------------------*/
  77. #define PWM_FB_EDGE_ACMP0 (PWM_BRKCTL0_1_CPO0EBEN_Msk) /*!< Comparator 0 as edge-detect fault brake source */
  78. #define PWM_FB_EDGE_ACMP1 (PWM_BRKCTL0_1_CPO1EBEN_Msk) /*!< Comparator 1 as edge-detect fault brake source */
  79. #define PWM_FB_EDGE_BKP0 (PWM_BRKCTL0_1_BRKP0EEN_Msk) /*!< BKP0 pin as edge-detect fault brake source */
  80. #define PWM_FB_EDGE_BKP1 (PWM_BRKCTL0_1_BRKP1EEN_Msk) /*!< BKP1 pin as edge-detect fault brake source */
  81. #define PWM_FB_EDGE_SYS_CSS (PWM_BRKCTL0_1_SYSEBEN_Msk | PWM_FAILBRK_CSSBRKEN_Msk) /*!< System fail condition: clock security system detection as edge-detect fault brake source */
  82. #define PWM_FB_EDGE_SYS_BOD (PWM_BRKCTL0_1_SYSEBEN_Msk | PWM_FAILBRK_BODBRKEN_Msk) /*!< System fail condition: brown-out detection as edge-detect fault brake source */
  83. #define PWM_FB_EDGE_SYS_RAM (PWM_BRKCTL0_1_SYSEBEN_Msk | PWM_FAILBRK_RAMBRKEN_Msk) /*!< System fail condition: SRAM parity error detection as edge-detect fault brake source */
  84. #define PWM_FB_EDGE_SYS_COR (PWM_BRKCTL0_1_SYSEBEN_Msk | PWM_FAILBRK_CORBRKEN_Msk) /*!< System fail condition: core lockup detection as edge-detect fault brake source */
  85. #define PWM_FB_LEVEL_ACMP0 (PWM_BRKCTL0_1_CPO0LBEN_Msk) /*!< Comparator 0 as level-detect fault brake source */
  86. #define PWM_FB_LEVEL_ACMP1 (PWM_BRKCTL0_1_CPO1LBEN_Msk) /*!< Comparator 1 as level-detect fault brake source */
  87. #define PWM_FB_LEVEL_BKP0 (PWM_BRKCTL0_1_BRKP0LEN_Msk) /*!< BKP0 pin as level-detect fault brake source */
  88. #define PWM_FB_LEVEL_BKP1 (PWM_BRKCTL0_1_BRKP1LEN_Msk) /*!< BKP1 pin as level-detect fault brake source */
  89. #define PWM_FB_LEVEL_SYS_CSS (PWM_BRKCTL0_1_SYSLBEN_Msk | PWM_FAILBRK_CSSBRKEN_Msk) /*!< System fail condition: clock security system detection as level-detect fault brake source */
  90. #define PWM_FB_LEVEL_SYS_BOD (PWM_BRKCTL0_1_SYSLBEN_Msk | PWM_FAILBRK_BODBRKEN_Msk) /*!< System fail condition: brown-out detection as level-detect fault brake source */
  91. #define PWM_FB_LEVEL_SYS_RAM (PWM_BRKCTL0_1_SYSLBEN_Msk | PWM_FAILBRK_RAMBRKEN_Msk) /*!< System fail condition: SRAM parity error detection as level-detect fault brake source */
  92. #define PWM_FB_LEVEL_SYS_COR (PWM_BRKCTL0_1_SYSLBEN_Msk | PWM_FAILBRK_CORBRKEN_Msk) /*!< System fail condition: core lockup detection as level-detect fault brake source */
  93. #define PWM_FB_EDGE (0UL) /*!< edge-detect fault brake */
  94. #define PWM_FB_LEVEL (8UL) /*!< level-detect fault brake */
  95. /*---------------------------------------------------------------------------------------------------------*/
  96. /* Capture Control Constant Definitions */
  97. /*---------------------------------------------------------------------------------------------------------*/
  98. #define PWM_CAPTURE_INT_RISING_LATCH (1UL) /*!< PWM capture interrupt if channel has rising transition */
  99. #define PWM_CAPTURE_INT_FALLING_LATCH (0x100UL) /*!< PWM capture interrupt if channel has falling transition */
  100. #define PWM_CAPTURE_PDMA_RISING_LATCH (0x2UL) /*!< PWM capture rising latched data transfer by PDMA */
  101. #define PWM_CAPTURE_PDMA_FALLING_LATCH (0x4UL) /*!< PWM capture falling latched data transfer by PDMA */
  102. #define PWM_CAPTURE_PDMA_RISING_FALLING_LATCH (0x6UL) /*!< PWM capture rising and falling latched data transfer by PDMA */
  103. /*---------------------------------------------------------------------------------------------------------*/
  104. /* Duty Interrupt Type Constant Definitions */
  105. /*---------------------------------------------------------------------------------------------------------*/
  106. #define PWM_DUTY_INT_DOWN_COUNT_MATCH_CMP (PWM_INTEN0_CMPDIEN0_Msk) /*!< PWM duty interrupt triggered if down count match comparator */
  107. #define PWM_DUTY_INT_UP_COUNT_MATCH_CMP (PWM_INTEN0_CMPUIEN0_Msk) /*!< PWM duty interrupt triggered if up down match comparator */
  108. /*---------------------------------------------------------------------------------------------------------*/
  109. /* Interrupt Flag Accumulator Constant Definitions */
  110. /*---------------------------------------------------------------------------------------------------------*/
  111. #define PWM_IFA_EVEN_ZERO_POINT (0UL) /*!< PWM counter equal to zero in even channel \hideinitializer */
  112. #define PWM_IFA_EVEN_PERIOD_POINT (1UL) /*!< PWM counter equal to period in even channel \hideinitializer */
  113. #define PWM_IFA_EVEN_COMPARE_UP_COUNT_POINT (2UL) /*!< PWM counter up count to comparator value in even channel \hideinitializer */
  114. #define PWM_IFA_EVEN_COMPARE_DOWN_COUNT_POINT (3UL) /*!< PWM counter down count to comparator value in even channel \hideinitializer */
  115. #define PWM_IFA_ODD_ZERO_POINT (4UL) /*!< PWM counter equal to zero in odd channel \hideinitializer */
  116. #define PWM_IFA_ODD_PERIOD_POINT (5UL) /*!< PWM counter equal to period in odd channel \hideinitializer */
  117. #define PWM_IFA_ODD_COMPARE_UP_COUNT_POINT (6UL) /*!< PWM counter up count to comparator value in odd channel \hideinitializer */
  118. #define PWM_IFA_ODD_COMPARE_DOWN_COUNT_POINT (7UL) /*!< PWM counter down count to comparator value in odd channel \hideinitializer */
  119. /*---------------------------------------------------------------------------------------------------------*/
  120. /* Load Mode Constant Definitions */
  121. /*---------------------------------------------------------------------------------------------------------*/
  122. #define PWM_LOAD_MODE_IMMEDIATE (PWM_CTL0_IMMLDEN0_Msk) /*!< PWM immediately load mode \hideinitializer */
  123. #define PWM_LOAD_MODE_WINDOW (PWM_CTL0_WINLDEN0_Msk) /*!< PWM window load mode \hideinitializer */
  124. #define PWM_LOAD_MODE_CENTER (PWM_CTL0_CTRLD0_Msk) /*!< PWM center load mode \hideinitializer */
  125. /*---------------------------------------------------------------------------------------------------------*/
  126. /* Synchronize Control Constant Definitions */
  127. /*---------------------------------------------------------------------------------------------------------*/
  128. #define PWM_SYNC_OUT_FROM_SYNCIN_SWSYNC (0UL) /*!< Synchronize source from SYNC_IN or SWSYNC \hideinitializer */
  129. #define PWM_SYNC_OUT_FROM_COUNT_TO_ZERO (1UL) /*!< Synchronize source from counter equal to 0 \hideinitializer */
  130. #define PWM_SYNC_OUT_FROM_COUNT_TO_COMPARATOR (2UL) /*!< Synchronize source from counter equal to CMPDAT1, CMPDAT3, CMPDAT5 \hideinitializer */
  131. #define PWM_SYNC_OUT_DISABLE (3UL) /*!< SYNC_OUT will not be generated \hideinitializer */
  132. #define PWM_PHS_DIR_DECREMENT (0UL) /*!< PWM counter count decrement \hideinitializer */
  133. #define PWM_PHS_DIR_INCREMENT (1UL) /*!< PWM counter count increment \hideinitializer */
  134. /*---------------------------------------------------------------------------------------------------------*/
  135. /* Noise Filter Clock Divide Select Constant Definitions */
  136. /*---------------------------------------------------------------------------------------------------------*/
  137. #define PWM_NF_CLK_DIV_1 (0UL) /*!< Noise filter clock is HCLK divide by 1 \hideinitializer */
  138. #define PWM_NF_CLK_DIV_2 (1UL) /*!< Noise filter clock is HCLK divide by 2 \hideinitializer */
  139. #define PWM_NF_CLK_DIV_4 (2UL) /*!< Noise filter clock is HCLK divide by 4 \hideinitializer */
  140. #define PWM_NF_CLK_DIV_8 (3UL) /*!< Noise filter clock is HCLK divide by 8 \hideinitializer */
  141. #define PWM_NF_CLK_DIV_16 (4UL) /*!< Noise filter clock is HCLK divide by 16 \hideinitializer */
  142. #define PWM_NF_CLK_DIV_32 (5UL) /*!< Noise filter clock is HCLK divide by 32 \hideinitializer */
  143. #define PWM_NF_CLK_DIV_64 (6UL) /*!< Noise filter clock is HCLK divide by 64 \hideinitializer */
  144. #define PWM_NF_CLK_DIV_128 (7UL) /*!< Noise filter clock is HCLK divide by 128 \hideinitializer */
  145. /*---------------------------------------------------------------------------------------------------------*/
  146. /* Clock Source Select Constant Definitions */
  147. /*---------------------------------------------------------------------------------------------------------*/
  148. #define PWM_CLKSRC_PWM_CLK (0UL) /*!< PWM Clock source selects to PWM0_CLK or PWM1_CLK \hideinitializer */
  149. #define PWM_CLKSRC_TIMER0 (1UL) /*!< PWM Clock source selects to TIMER0 overflow \hideinitializer */
  150. #define PWM_CLKSRC_TIMER1 (2UL) /*!< PWM Clock source selects to TIMER1 overflow \hideinitializer */
  151. #define PWM_CLKSRC_TIMER2 (3UL) /*!< PWM Clock source selects to TIMER2 overflow \hideinitializer */
  152. #define PWM_CLKSRC_TIMER3 (4UL) /*!< PWM Clock source selects to TIMER3 overflow \hideinitializer */
  153. /*@}*/ /* end of group PWM_EXPORTED_CONSTANTS */
  154. /** @addtogroup PWM_EXPORTED_FUNCTIONS PWM Exported Functions
  155. @{
  156. */
  157. /**
  158. * @brief This macro enable complementary mode
  159. * @param[in] pwm The pointer of the specified PWM module
  160. * @return None
  161. * @details This macro is used to enable complementary mode of PWM module.
  162. * \hideinitializer
  163. */
  164. #define PWM_ENABLE_COMPLEMENTARY_MODE(pwm) ((pwm)->CTL1 = (pwm)->CTL1 | PWM_CTL1_OUTMODEn_Msk)
  165. /**
  166. * @brief This macro disable complementary mode, and enable independent mode.
  167. * @param[in] pwm The pointer of the specified PWM module
  168. * @return None
  169. * @details This macro is used to disable complementary mode of PWM module.
  170. * \hideinitializer
  171. */
  172. #define PWM_DISABLE_COMPLEMENTARY_MODE(pwm) ((pwm)->CTL1 = (pwm)->CTL1 & ~PWM_CTL1_OUTMODEn_Msk)
  173. /**
  174. * @brief This macro enable group mode
  175. * @param[in] pwm The pointer of the specified PWM module
  176. * @return None
  177. * @details This macro is used to enable group mode of PWM module.
  178. * \hideinitializer
  179. */
  180. #define PWM_ENABLE_GROUP_MODE(pwm) ((pwm)->CTL0 = (pwm)->CTL0 | PWM_CTL0_GROUPEN_Msk)
  181. /**
  182. * @brief This macro disable group mode
  183. * @param[in] pwm The pointer of the specified PWM module
  184. * @return None
  185. * @details This macro is used to disable group mode of PWM module.
  186. * \hideinitializer
  187. */
  188. #define PWM_DISABLE_GROUP_MODE(pwm) ((pwm)->CTL0 = (pwm)->CTL0 & ~PWM_CTL0_GROUPEN_Msk)
  189. /**
  190. * @brief Enable timer synchronous mode of specified channel(s)
  191. * @param[in] pwm The pointer of the specified PWM module
  192. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  193. * Bit 0 represents channel 0, bit 1 represents channel 1...
  194. * @return None
  195. * @details This macro is used to enable timer synchronous mode of specified channel(s).
  196. * \hideinitializer
  197. */
  198. #define PWM_ENABLE_TIMER_SYNC(pwm, u32ChannelMask) ((pwm)->SSCTL |= (u32ChannelMask))
  199. /**
  200. * @brief Disable timer synchronous mode of specified channel(s)
  201. * @param[in] pwm The pointer of the specified PWM module
  202. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  203. * Bit 0 represents channel 0, bit 1 represents channel 1...
  204. * @return None
  205. * @details This macro is used to disable timer synchronous mode of specified channel(s).
  206. * \hideinitializer
  207. */
  208. #define PWM_DISABLE_TIMER_SYNC(pwm, u32ChannelMask) \
  209. do{ \
  210. int i;\
  211. for(i = 0; i < 6; i++) { \
  212. if((u32ChannelMask) & (1 << i)) \
  213. (pwm)->SSCTL &= ~(1UL << i); \
  214. } \
  215. }while(0)
  216. /**
  217. * @brief This macro enable output inverter of specified channel(s)
  218. * @param[in] pwm The pointer of the specified PWM module
  219. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  220. * Bit 0 represents channel 0, bit 1 represents channel 1...
  221. * @return None
  222. * @details This macro is used to enable output inverter of specified channel(s).
  223. * \hideinitializer
  224. */
  225. #define PWM_ENABLE_OUTPUT_INVERTER(pwm, u32ChannelMask) ((pwm)->POLCTL = (u32ChannelMask))
  226. /**
  227. * @brief This macro get captured rising data
  228. * @param[in] pwm The pointer of the specified PWM module
  229. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  230. * @return None
  231. * @details This macro is used to get captured rising data of specified channel.
  232. * \hideinitializer
  233. */
  234. #define PWM_GET_CAPTURE_RISING_DATA(pwm, u32ChannelNum) (*(__IO uint32_t *) (&((pwm)->RCAPDAT0) + 2 * (u32ChannelNum)))
  235. /**
  236. * @brief This macro get captured falling data
  237. * @param[in] pwm The pointer of the specified PWM module
  238. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  239. * @return None
  240. * @details This macro is used to get captured falling data of specified channel.
  241. * \hideinitializer
  242. */
  243. #define PWM_GET_CAPTURE_FALLING_DATA(pwm, u32ChannelNum) (*(__IO uint32_t *) (&((pwm)->FCAPDAT0) + 2 * (u32ChannelNum)))
  244. /**
  245. * @brief This macro mask output logic to high or low
  246. * @param[in] pwm The pointer of the specified PWM module
  247. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  248. * Bit 0 represents channel 0, bit 1 represents channel 1...
  249. * @param[in] u32LevelMask Output logic to high or low
  250. * @return None
  251. * @details This macro is used to mask output logic to high or low of specified channel(s).
  252. * @note If u32ChannelMask parameter is 0, then mask function will be disabled.
  253. * \hideinitializer
  254. */
  255. #define PWM_MASK_OUTPUT(pwm, u32ChannelMask, u32LevelMask) \
  256. { \
  257. (pwm)->MSKEN = (u32ChannelMask); \
  258. (pwm)->MSK = (u32LevelMask); \
  259. }
  260. /**
  261. * @brief This macro set the prescaler of the selected channel
  262. * @param[in] pwm The pointer of the specified PWM module
  263. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  264. * @param[in] u32Prescaler Clock prescaler of specified channel. Valid values are between 1 ~ 0xFFF
  265. * @return None
  266. * @details This macro is used to set the prescaler of specified channel.
  267. * @note Every even channel N, and channel (N + 1) share a prescaler. So if channel 0 prescaler changed,
  268. * channel 1 will also be affected.
  269. * \hideinitializer
  270. */
  271. #define PWM_SET_PRESCALER(pwm, u32ChannelNum, u32Prescaler) (*(__IO uint32_t *) (&((pwm)->CLKPSC0_1) + ((u32ChannelNum) >> 1)) = (u32Prescaler))
  272. /**
  273. * @brief This macro set the comparator of the selected channel
  274. * @param[in] pwm The pointer of the specified PWM module
  275. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  276. * @param[in] u32CMR Comparator of specified channel. Valid values are between 0~0xFFFF
  277. * @return None
  278. * @details This macro is used to set the comparator of specified channel.
  279. * @note This new setting will take effect on next PWM period.
  280. * \hideinitializer
  281. */
  282. #define PWM_SET_CMR(pwm, u32ChannelNum, u32CMR) ((pwm)->CMPDAT[(u32ChannelNum)]= (u32CMR))
  283. /**
  284. * @brief This macro set the free trigger comparator of the selected channel
  285. * @param[in] pwm The pointer of the specified PWM module
  286. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  287. * @param[in] u32FTCMR Free trigger comparator of specified channel. Valid values are between 0~0xFFFF
  288. * @return None
  289. * @details This macro is used to set the free trigger comparator of specified channel.
  290. * @note This new setting will take effect on next PWM period.
  291. * \hideinitializer
  292. */
  293. #define PWM_SET_FTCMR(pwm, u32ChannelNum, u32FTCMR) (*(__IO uint32_t *) (&((pwm)->FTCMPDAT0_1) + ((u32ChannelNum) >> 1)) = (u32FTCMR))
  294. /**
  295. * @brief This macro set the period of the selected channel
  296. * @param[in] pwm The pointer of the specified PWM module
  297. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  298. * @param[in] u32CNR Period of specified channel. Valid values are between 0~0xFFFF
  299. * @return None
  300. * @details This macro is used to set the period of specified channel.
  301. * @note This new setting will take effect on next PWM period.
  302. * @note PWM counter will stop if period length set to 0.
  303. * \hideinitializer
  304. */
  305. #define PWM_SET_CNR(pwm, u32ChannelNum, u32CNR) ((pwm)->PERIOD[(u32ChannelNum)] = (u32CNR))
  306. /**
  307. * @brief This macro set the PWM aligned type
  308. * @param[in] pwm The pointer of the specified PWM module
  309. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  310. * Bit 0 represents channel 0, bit 1 represents channel 1...
  311. * @param[in] u32AlignedType PWM aligned type, valid values are:
  312. * - \ref PWM_EDGE_ALIGNED
  313. * - \ref PWM_CENTER_ALIGNED
  314. * @return None
  315. * @details This macro is used to set the PWM aligned type of specified channel(s).
  316. * \hideinitializer
  317. */
  318. #define PWM_SET_ALIGNED_TYPE(pwm, u32ChannelMask, u32AlignedType) \
  319. do{ \
  320. int i; \
  321. for(i = 0; i < 6; i++) { \
  322. if((u32ChannelMask) & (1 << i)) \
  323. (pwm)->CTL1 = (((pwm)->CTL1 & ~(3UL << (2 * i))) | ((u32AlignedType) << ( 2 * i))); \
  324. } \
  325. }while(0)
  326. /**
  327. * @brief Set load window of window loading mode for specified channel(s)
  328. * @param[in] pwm The pointer of the specified PWM module
  329. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  330. * Bit 0 represents channel 0, bit 1 represents channel 1...
  331. * @return None
  332. * @details This macro is used to set load window of window loading mode for specified channel(s).
  333. * \hideinitializer
  334. */
  335. #define PWM_SET_LOAD_WINDOW(pwm, u32ChannelMask) ((pwm)->LOAD |= (u32ChannelMask))
  336. /**
  337. * @brief Trigger synchronous event from specified channel(s)
  338. * @param[in] pwm The pointer of the specified PWM module
  339. * @param[in] u32ChannelNum PWM channel number. Valid values are 0, 2, 4
  340. * Bit 0 represents channel 0, bit 1 represents channel 2 and bit 2 represents channel 4
  341. * @return None
  342. * @details This macro is used to trigger synchronous event from specified channel(s).
  343. * \hideinitializer
  344. */
  345. #define PWM_TRIGGER_SYNC(pwm, u32ChannelNum) ((pwm)->SWSYNC |= (1 << ((u32ChannelNum) >> 1)))
  346. /**
  347. * @brief Clear counter of specified channel(s)
  348. * @param[in] pwm The pointer of the specified PWM module
  349. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  350. * Bit 0 represents channel 0, bit 1 represents channel 1...
  351. * @return None
  352. * @details This macro is used to clear counter of specified channel(s).
  353. * \hideinitializer
  354. */
  355. #define PWM_CLR_COUNTER(pwm, u32ChannelMask) ((pwm)->CNTCLR |= (u32ChannelMask))
  356. /**
  357. * @brief Set output level at zero, compare up, period(center) and compare down of specified channel(s)
  358. * @param[in] pwm The pointer of the specified PWM module
  359. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  360. * Bit 0 represents channel 0, bit 1 represents channel 1...
  361. * @param[in] u32ZeroLevel output level at zero point, valid values are:
  362. * - \ref PWM_OUTPUT_NOTHING
  363. * - \ref PWM_OUTPUT_LOW
  364. * - \ref PWM_OUTPUT_HIGH
  365. * - \ref PWM_OUTPUT_TOGGLE
  366. * @param[in] u32CmpUpLevel output level at compare up point, valid values are:
  367. * - \ref PWM_OUTPUT_NOTHING
  368. * - \ref PWM_OUTPUT_LOW
  369. * - \ref PWM_OUTPUT_HIGH
  370. * - \ref PWM_OUTPUT_TOGGLE
  371. * @param[in] u32PeriodLevel output level at period(center) point, valid values are:
  372. * - \ref PWM_OUTPUT_NOTHING
  373. * - \ref PWM_OUTPUT_LOW
  374. * - \ref PWM_OUTPUT_HIGH
  375. * - \ref PWM_OUTPUT_TOGGLE
  376. * @param[in] u32CmpDownLevel output level at compare down point, valid values are:
  377. * - \ref PWM_OUTPUT_NOTHING
  378. * - \ref PWM_OUTPUT_LOW
  379. * - \ref PWM_OUTPUT_HIGH
  380. * - \ref PWM_OUTPUT_TOGGLE
  381. * @return None
  382. * @details This macro is used to Set output level at zero, compare up, period(center) and compare down of specified channel(s).
  383. * \hideinitializer
  384. */
  385. #define PWM_SET_OUTPUT_LEVEL(pwm, u32ChannelMask, u32ZeroLevel, u32CmpUpLevel, u32PeriodLevel, u32CmpDownLevel) \
  386. do{ \
  387. int i; \
  388. for(i = 0; i < 6; i++) { \
  389. if((u32ChannelMask) & (1 << i)) { \
  390. (pwm)->WGCTL0 = (((pwm)->WGCTL0 & ~(3UL << (2 * i))) | ((u32ZeroLevel) << (2 * i))); \
  391. (pwm)->WGCTL0 = (((pwm)->WGCTL0 & ~(3UL << (PWM_WGCTL0_PRDPCTLn_Pos + (2 * i)))) | ((u32PeriodLevel) << (PWM_WGCTL0_PRDPCTLn_Pos + (2 * i)))); \
  392. (pwm)->WGCTL1 = (((pwm)->WGCTL1 & ~(3UL << (2 * i))) | ((u32CmpUpLevel) << (2 * i))); \
  393. (pwm)->WGCTL1 = (((pwm)->WGCTL1 & ~(3UL << (PWM_WGCTL1_CMPDCTLn_Pos + (2 * i)))) | ((u32CmpDownLevel) << (PWM_WGCTL1_CMPDCTLn_Pos + (2 * i)))); \
  394. } \
  395. } \
  396. }while(0)
  397. /**
  398. * @brief Trigger brake event from specified channel(s)
  399. * @param[in] pwm The pointer of the specified PWM module
  400. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  401. * Bit 0 represents channel 0, bit 1 represents channel 2 and bit 2 represents channel 4
  402. * @param[in] u32BrakeType Type of brake trigger. PWM_FB_EDGE of this macro is only supported in M45xD/M45xC.
  403. * - \ref PWM_FB_EDGE
  404. * - \ref PWM_FB_LEVEL
  405. * @return None
  406. * @details This macro is used to trigger brake event from specified channel(s).
  407. * \hideinitializer
  408. */
  409. #define PWM_TRIGGER_BRAKE(pwm, u32ChannelMask, u32BrakeType) ((pwm)->SWBRK |= ((u32ChannelMask) << (u32BrakeType)))
  410. /**
  411. * @brief Set Dead zone clock source
  412. * @param[in] pwm The pointer of the specified PWM module
  413. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  414. * @param[in] u32AfterPrescaler Dead zone clock source is from prescaler output. Valid values are TRUE (after prescaler) or FALSE (before prescaler).
  415. * @return None
  416. * @details This macro is used to set Dead zone clock source. Every two channels share the same setting.
  417. * @note The write-protection function should be disabled before using this function.
  418. * @note This function is only supported in M45xD/M45xC.
  419. * \hideinitializer
  420. */
  421. #define PWM_SET_DEADZONE_CLK_SRC(pwm, u32ChannelNum, u32AfterPrescaler) \
  422. (*(__IO uint32_t *) (&((pwm)->DTCTL0_1) + ((u32ChannelNum) >> 1)) = (*(__IO uint32_t *) (&((pwm)->DTCTL0_1) + ((u32ChannelNum) >> 1)) & ~PWM_DTCTL0_1_DTCKSEL_Msk) | \
  423. ((u32AfterPrescaler) << PWM_DTCTL0_1_DTCKSEL_Pos))
  424. /*---------------------------------------------------------------------------------------------------------*/
  425. /* Define PWM functions prototype */
  426. /*---------------------------------------------------------------------------------------------------------*/
  427. uint32_t PWM_ConfigCaptureChannel(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32UnitTimeNsec, uint32_t u32CaptureEdge);
  428. uint32_t PWM_ConfigOutputChannel(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Frequency, uint32_t u32DutyCycle);
  429. void PWM_Start(PWM_T *pwm, uint32_t u32ChannelMask);
  430. void PWM_Stop(PWM_T *pwm, uint32_t u32ChannelMask);
  431. void PWM_ForceStop(PWM_T *pwm, uint32_t u32ChannelMask);
  432. void PWM_EnableADCTrigger(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition);
  433. void PWM_DisableADCTrigger(PWM_T *pwm, uint32_t u32ChannelNum);
  434. void PWM_ClearADCTriggerFlag(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition);
  435. uint32_t PWM_GetADCTriggerFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  436. void PWM_EnableDACTrigger(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition);
  437. void PWM_DisableDACTrigger(PWM_T *pwm, uint32_t u32ChannelNum);
  438. void PWM_ClearDACTriggerFlag(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition);
  439. uint32_t PWM_GetDACTriggerFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  440. void PWM_EnableFaultBrake(PWM_T *pwm, uint32_t u32ChannelMask, uint32_t u32LevelMask, uint32_t u32BrakeSource);
  441. void PWM_EnableCapture(PWM_T *pwm, uint32_t u32ChannelMask);
  442. void PWM_DisableCapture(PWM_T *pwm, uint32_t u32ChannelMask);
  443. void PWM_EnableOutput(PWM_T *pwm, uint32_t u32ChannelMask);
  444. void PWM_DisableOutput(PWM_T *pwm, uint32_t u32ChannelMask);
  445. void PWM_EnablePDMA(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32RisingFirst, uint32_t u32Mode);
  446. void PWM_DisablePDMA(PWM_T *pwm, uint32_t u32ChannelNum);
  447. void PWM_EnableDeadZone(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Duration);
  448. void PWM_DisableDeadZone(PWM_T *pwm, uint32_t u32ChannelNum);
  449. void PWM_EnableCaptureInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge);
  450. void PWM_DisableCaptureInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge);
  451. void PWM_ClearCaptureIntFlag(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge);
  452. uint32_t PWM_GetCaptureIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  453. void PWM_EnableDutyInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32IntDutyType);
  454. void PWM_DisableDutyInt(PWM_T *pwm, uint32_t u32ChannelNum);
  455. void PWM_ClearDutyIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  456. uint32_t PWM_GetDutyIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  457. void PWM_EnableFaultBrakeInt(PWM_T *pwm, uint32_t u32BrakeSource);
  458. void PWM_DisableFaultBrakeInt(PWM_T *pwm, uint32_t u32BrakeSource);
  459. void PWM_ClearFaultBrakeIntFlag(PWM_T *pwm, uint32_t u32BrakeSource);
  460. uint32_t PWM_GetFaultBrakeIntFlag(PWM_T *pwm, uint32_t u32BrakeSource);
  461. void PWM_EnablePeriodInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32IntPeriodType);
  462. void PWM_DisablePeriodInt(PWM_T *pwm, uint32_t u32ChannelNum);
  463. void PWM_ClearPeriodIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  464. uint32_t PWM_GetPeriodIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  465. void PWM_EnableZeroInt(PWM_T *pwm, uint32_t u32ChannelNum);
  466. void PWM_DisableZeroInt(PWM_T *pwm, uint32_t u32ChannelNum);
  467. void PWM_ClearZeroIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  468. uint32_t PWM_GetZeroIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  469. void PWM_EnableAcc(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32IntFlagCnt, uint32_t u32IntAccSrc);
  470. void PWM_DisableAcc(PWM_T *pwm, uint32_t u32ChannelNum);
  471. void PWM_EnableAccInt(PWM_T *pwm, uint32_t u32ChannelNum);
  472. void PWM_DisableAccInt(PWM_T *pwm, uint32_t u32ChannelNum);
  473. void PWM_ClearAccInt(PWM_T *pwm, uint32_t u32ChannelNum);
  474. uint32_t PWM_GetAccInt(PWM_T *pwm, uint32_t u32ChannelNum);
  475. void PWM_ClearFTDutyIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  476. uint32_t PWM_GetFTDutyIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  477. void PWM_EnableLoadMode(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32LoadMode);
  478. void PWM_DisableLoadMode(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32LoadMode);
  479. void PWM_ConfigSyncPhase(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32SyncSrc, uint32_t u32Direction, uint32_t u32StartPhase);
  480. void PWM_EnableSyncPhase(PWM_T *pwm, uint32_t u32ChannelMask);
  481. void PWM_DisableSyncPhase(PWM_T *pwm, uint32_t u32ChannelMask);
  482. void PWM_EnableSyncNoiseFilter(PWM_T *pwm, uint32_t u32ClkCnt, uint32_t u32ClkDivSel);
  483. void PWM_DisableSyncNoiseFilter(PWM_T *pwm);
  484. void PWM_EnableSyncPinInverse(PWM_T *pwm);
  485. void PWM_DisableSyncPinInverse(PWM_T *pwm);
  486. void PWM_SetClockSource(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32ClkSrcSel);
  487. void PWM_EnableBrakeNoiseFilter(PWM_T *pwm, uint32_t u32BrakePinNum, uint32_t u32ClkCnt, uint32_t u32ClkDivSel);
  488. void PWM_DisableBrakeNoiseFilter(PWM_T *pwm, uint32_t u32BrakePinNum);
  489. void PWM_EnableBrakePinInverse(PWM_T *pwm, uint32_t u32BrakePinNum);
  490. void PWM_DisableBrakePinInverse(PWM_T *pwm, uint32_t u32BrakePinNum);
  491. void PWM_SetBrakePinSource(PWM_T *pwm, uint32_t u32BrakePinNum, uint32_t u32SelAnotherModule);
  492. uint32_t PWM_GetWrapAroundFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  493. void PWM_ClearWrapAroundFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  494. /*@}*/ /* end of group PWM_EXPORTED_FUNCTIONS */
  495. /*@}*/ /* end of group PWM_Driver */
  496. /*@}*/ /* end of group Standard_Driver */
  497. #ifdef __cplusplus
  498. }
  499. #endif
  500. #endif //__PWM_H__
  501. /*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/