nu_pwm.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /**************************************************************************//**
  2. * @file nu_pwm.h
  3. * @version V1.00
  4. * $Revision: 9 $
  5. * $Date: 18/06/07 3:47p $
  6. * @brief M031 series PWM driver header file
  7. *
  8. * SPDX-License-Identifier: Apache-2.0
  9. * @copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #ifndef __NU_PWM_H__
  12. #define __NU_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 (6UL) /*!< PWM channel number \hideinitializer */
  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 \hideinitializer */
  37. #define PWM_DOWN_COUNTER (1UL) /*!< Down counter type \hideinitializer */
  38. #define PWM_UP_DOWN_COUNTER (2UL) /*!< Up-Down counter type \hideinitializer */
  39. /*---------------------------------------------------------------------------------------------------------*/
  40. /* Aligned Type Constant Definitions */
  41. /*---------------------------------------------------------------------------------------------------------*/
  42. #define PWM_EDGE_ALIGNED (1UL) /*!< PWM working in edge aligned type(down count) \hideinitializer */
  43. #define PWM_CENTER_ALIGNED (2UL) /*!< PWM working in center aligned type \hideinitializer */
  44. /*---------------------------------------------------------------------------------------------------------*/
  45. /* Output Level Constant Definitions */
  46. /*---------------------------------------------------------------------------------------------------------*/
  47. #define PWM_OUTPUT_NOTHING (0UL) /*!< PWM output nothing \hideinitializer */
  48. #define PWM_OUTPUT_LOW (1UL) /*!< PWM output low \hideinitializer */
  49. #define PWM_OUTPUT_HIGH (2UL) /*!< PWM output high \hideinitializer */
  50. #define PWM_OUTPUT_TOGGLE (3UL) /*!< PWM output toggle \hideinitializer */
  51. /*---------------------------------------------------------------------------------------------------------*/
  52. /* Synchronous Start Function Control Constant Definitions */
  53. /*---------------------------------------------------------------------------------------------------------*/
  54. #define PWM_SSCTL_SSRC_PWM0 (0UL<<PWM_SSCTL_SSRC_Pos) /*!< Synchronous start source comes from PWM0 \hideinitializer */
  55. #define PWM_SSCTL_SSRC_PWM1 (1UL<<PWM_SSCTL_SSRC_Pos) /*!< Synchronous start source comes from PWM1 \hideinitializer */
  56. /*---------------------------------------------------------------------------------------------------------*/
  57. /* Trigger Source Select Constant Definitions */
  58. /*---------------------------------------------------------------------------------------------------------*/
  59. #define PWM_TRIGGER_ADC_EVEN_ZERO_POINT (0UL) /*!< PWM trigger ADC while counter of even channel matches zero point \hideinitializer */
  60. #define PWM_TRIGGER_ADC_EVEN_PERIOD_POINT (1UL) /*!< PWM trigger ADC while counter of even channel matches period point \hideinitializer */
  61. #define PWM_TRIGGER_ADC_EVEN_ZERO_OR_PERIOD_POINT (2UL) /*!< PWM trigger ADC while counter of even channel matches zero or period point \hideinitializer */
  62. #define PWM_TRIGGER_ADC_EVEN_COMPARE_UP_COUNT_POINT (3UL) /*!< PWM trigger ADC while counter of even channel matches up count to comparator point \hideinitializer */
  63. #define PWM_TRIGGER_ADC_EVEN_COMPARE_DOWN_COUNT_POINT (4UL) /*!< PWM trigger ADC while counter of even channel matches down count to comparator point \hideinitializer */
  64. #define PWM_TRIGGER_ADC_ODD_COMPARE_UP_COUNT_POINT (8UL) /*!< PWM trigger ADC while counter of odd channel matches up count to comparator point \hideinitializer */
  65. #define PWM_TRIGGER_ADC_ODD_COMPARE_DOWN_COUNT_POINT (9UL) /*!< PWM trigger ADC while counter of odd channel matches down count to comparator point \hideinitializer */
  66. /*---------------------------------------------------------------------------------------------------------*/
  67. /* Fail brake Control Constant Definitions */
  68. /*---------------------------------------------------------------------------------------------------------*/
  69. #define PWM_FB_EDGE_ACMP0 (PWM_BRKCTL0_1_CPO0EBEN_Msk) /*!< Comparator 0 as edge-detect fault brake source \hideinitializer */
  70. #define PWM_FB_EDGE_ACMP1 (PWM_BRKCTL0_1_CPO1EBEN_Msk) /*!< Comparator 1 as edge-detect fault brake source \hideinitializer */
  71. #define PWM_FB_EDGE_BKP0 (PWM_BRKCTL0_1_BRKP0EEN_Msk) /*!< BKP0 pin as edge-detect fault brake source \hideinitializer */
  72. #define PWM_FB_EDGE_BKP1 (PWM_BRKCTL0_1_BRKP1EEN_Msk) /*!< BKP1 pin as edge-detect fault brake source \hideinitializer */
  73. #define PWM_FB_EDGE_ADCRM (PWM_BRKCTL0_1_ADCEBEN_Msk) /*!< ADC Result Monitor (ADCRM) as edge-detect fault brake source \hideinitializer */
  74. #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 \hideinitializer */
  75. #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 \hideinitializer */
  76. #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 \hideinitializer */
  77. #define PWM_FB_LEVEL_ACMP0 (PWM_BRKCTL0_1_CPO0LBEN_Msk) /*!< Comparator 0 as level-detect fault brake source \hideinitializer */
  78. #define PWM_FB_LEVEL_ACMP1 (PWM_BRKCTL0_1_CPO1LBEN_Msk) /*!< Comparator 1 as level-detect fault brake source \hideinitializer */
  79. #define PWM_FB_LEVEL_BKP0 (PWM_BRKCTL0_1_BRKP0LEN_Msk) /*!< BKP0 pin as level-detect fault brake source \hideinitializer */
  80. #define PWM_FB_LEVEL_BKP1 (PWM_BRKCTL0_1_BRKP1LEN_Msk) /*!< BKP1 pin as level-detect fault brake source \hideinitializer */
  81. #define PWM_FB_LEVEL_ADCRM (PWM_BRKCTL0_1_ADCLBEN_Msk) /*!< ADC Result Monitor (ADCRM) as level-detect fault brake source \hideinitializer */
  82. #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 \hideinitializer */
  83. #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 \hideinitializer */
  84. #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 \hideinitializer */
  85. #define PWM_FB_EDGE (0UL) /*!< Edge-detect fault brake \hideinitializer */
  86. #define PWM_FB_LEVEL (8UL) /*!< Level-detect fault brake \hideinitializer */
  87. /*---------------------------------------------------------------------------------------------------------*/
  88. /* Capture Control Constant Definitions */
  89. /*---------------------------------------------------------------------------------------------------------*/
  90. #define PWM_CAPTURE_INT_RISING_LATCH (1UL) /*!< PWM capture interrupt if channel has rising transition \hideinitializer */
  91. #define PWM_CAPTURE_INT_FALLING_LATCH (0x100UL) /*!< PWM capture interrupt if channel has falling transition \hideinitializer */
  92. #define PWM_CAPTURE_PDMA_RISING_LATCH (0x2UL) /*!< PWM capture rising latched data transfer by PDMA \hideinitializer */
  93. #define PWM_CAPTURE_PDMA_FALLING_LATCH (0x4UL) /*!< PWM capture falling latched data transfer by PDMA \hideinitializer */
  94. #define PWM_CAPTURE_PDMA_RISING_FALLING_LATCH (0x6UL) /*!< PWM capture rising and falling latched data transfer by PDMA \hideinitializer */
  95. /*---------------------------------------------------------------------------------------------------------*/
  96. /* Duty Interrupt Type Constant Definitions */
  97. /*---------------------------------------------------------------------------------------------------------*/
  98. #define PWM_DUTY_INT_DOWN_COUNT_MATCH_CMP (PWM_INTEN0_CMPDIEN0_Msk) /*!< PWM duty interrupt triggered if down count match comparator \hideinitializer */
  99. #define PWM_DUTY_INT_UP_COUNT_MATCH_CMP (PWM_INTEN0_CMPUIEN0_Msk) /*!< PWM duty interrupt triggered if up count match comparator \hideinitializer */
  100. /*---------------------------------------------------------------------------------------------------------*/
  101. /* Load Mode Constant Definitions */
  102. /*---------------------------------------------------------------------------------------------------------*/
  103. #define PWM_LOAD_MODE_IMMEDIATE (PWM_CTL0_IMMLDEN0_Msk) /*!< PWM immediately load mode \hideinitializer */
  104. #define PWM_LOAD_MODE_CENTER (PWM_CTL0_CTRLD0_Msk) /*!< PWM center load mode \hideinitializer */
  105. /*---------------------------------------------------------------------------------------------------------*/
  106. /* Synchronize Control Constant Definitions */
  107. /*---------------------------------------------------------------------------------------------------------*/
  108. #define PWM_SYNC_OUT_FROM_SYNCIN_SWSYNC (0UL) /*!< Synchronize source from SYNC_IN or SWSYNC \hideinitializer */
  109. #define PWM_SYNC_OUT_FROM_COUNT_TO_ZERO (1UL) /*!< Synchronize source from counter equal to 0 \hideinitializer */
  110. #define PWM_SYNC_OUT_FROM_COUNT_TO_COMPARATOR (2UL) /*!< Synchronize source from counter equal to CMPDAT1, CMPDAT3, CMPDAT5 \hideinitializer */
  111. #define PWM_SYNC_OUT_DISABLE (3UL) /*!< SYNC_OUT will not be generated \hideinitializer */
  112. #define PWM_PHS_DIR_DECREMENT (0UL) /*!< PWM counter count decrement \hideinitializer */
  113. #define PWM_PHS_DIR_INCREMENT (1UL) /*!< PWM counter count increment \hideinitializer */
  114. /*---------------------------------------------------------------------------------------------------------*/
  115. /* Noise Filter Clock Divide Select Constant Definitions */
  116. /*---------------------------------------------------------------------------------------------------------*/
  117. #define PWM_NF_CLK_DIV_1 (0UL) /*!< Noise filter clock is HCLK divide by 1 \hideinitializer */
  118. #define PWM_NF_CLK_DIV_2 (1UL) /*!< Noise filter clock is HCLK divide by 2 \hideinitializer */
  119. #define PWM_NF_CLK_DIV_4 (2UL) /*!< Noise filter clock is HCLK divide by 4 \hideinitializer */
  120. #define PWM_NF_CLK_DIV_8 (3UL) /*!< Noise filter clock is HCLK divide by 8 \hideinitializer */
  121. #define PWM_NF_CLK_DIV_16 (4UL) /*!< Noise filter clock is HCLK divide by 16 \hideinitializer */
  122. #define PWM_NF_CLK_DIV_32 (5UL) /*!< Noise filter clock is HCLK divide by 32 \hideinitializer */
  123. #define PWM_NF_CLK_DIV_64 (6UL) /*!< Noise filter clock is HCLK divide by 64 \hideinitializer */
  124. #define PWM_NF_CLK_DIV_128 (7UL) /*!< Noise filter clock is HCLK divide by 128 \hideinitializer */
  125. /*---------------------------------------------------------------------------------------------------------*/
  126. /* Clock Source Select Constant Definitions */
  127. /*---------------------------------------------------------------------------------------------------------*/
  128. #define PWM_CLKSRC_PWM_CLK (0UL) /*!< PWM Clock source selects to PWM0_CLK or PWM1_CLK \hideinitializer */
  129. #define PWM_CLKSRC_TIMER0 (1UL) /*!< PWM Clock source selects to TIMER0 overflow \hideinitializer */
  130. #define PWM_CLKSRC_TIMER1 (2UL) /*!< PWM Clock source selects to TIMER1 overflow \hideinitializer */
  131. #define PWM_CLKSRC_TIMER2 (3UL) /*!< PWM Clock source selects to TIMER2 overflow \hideinitializer */
  132. #define PWM_CLKSRC_TIMER3 (4UL) /*!< PWM Clock source selects to TIMER3 overflow \hideinitializer */
  133. /*@}*/ /* end of group PWM_EXPORTED_CONSTANTS */
  134. /** @addtogroup PWM_EXPORTED_FUNCTIONS PWM Exported Functions
  135. @{
  136. */
  137. /**
  138. * @brief This macro enable complementary mode
  139. * @param[in] pwm The pointer of the specified PWM module
  140. * @return None
  141. * @details This macro is used to enable complementary mode of PWM module.
  142. * \hideinitializer
  143. */
  144. #define PWM_ENABLE_COMPLEMENTARY_MODE(pwm) ((pwm)->CTL1 = (pwm)->CTL1 | (0x7ul<<PWM_CTL1_OUTMODE0_Pos))
  145. /**
  146. * @brief This macro disable complementary mode, and enable independent mode.
  147. * @param[in] pwm The pointer of the specified PWM module
  148. * @return None
  149. * @details This macro is used to disable complementary mode of PWM module.
  150. * \hideinitializer
  151. */
  152. #define PWM_DISABLE_COMPLEMENTARY_MODE(pwm) ((pwm)->CTL1 = (pwm)->CTL1 & ~(0x7ul<<PWM_CTL1_OUTMODE0_Pos))
  153. /**
  154. * @brief Enable timer synchronous start counting function of specified channel(s)
  155. * @param[in] pwm The pointer of the specified PWM module
  156. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  157. * Bit 0 represents channel 0, bit 1 represents channel 1...
  158. * @param[in] u32SyncSrc Synchronous start source selection, valid values are:
  159. * - \ref PWM_SSCTL_SSRC_PWM0
  160. * - \ref PWM_SSCTL_SSRC_PWM1
  161. * @return None
  162. * @details This macro is used to enable timer synchronous start counting function of specified channel(s).
  163. * \hideinitializer
  164. */
  165. #define PWM_ENABLE_TIMER_SYNC(pwm, u32ChannelMask, u32SyncSrc) ((pwm)->SSCTL = ((pwm)->SSCTL & ~PWM_SSCTL_SSRC_Msk) | (u32SyncSrc) | (u32ChannelMask))
  166. /**
  167. * @brief Disable timer synchronous start counting function of specified channel(s)
  168. * @param[in] pwm The pointer of the specified PWM module
  169. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  170. * Bit 0 represents channel 0, bit 1 represents channel 1...
  171. * @return None
  172. * @details This macro is used to disable timer synchronous start counting function of specified channel(s).
  173. * \hideinitializer
  174. */
  175. #define PWM_DISABLE_TIMER_SYNC(pwm, u32ChannelMask) \
  176. do{ \
  177. int i;\
  178. for(i = 0; i < 6; i++) { \
  179. if((u32ChannelMask) & (1 << i)) \
  180. (pwm)->SSCTL &= ~(1UL << i); \
  181. } \
  182. }while(0)
  183. /**
  184. * @brief This macro enable PWM counter synchronous start counting function.
  185. * @param[in] pwm The pointer of the specified PWM module
  186. * @return None
  187. * @details This macro is used to make selected PWM0 and PWM1 channel(s) start counting at the same time.
  188. * To configure synchronous start counting channel(s) by PWM_ENABLE_TIMER_SYNC() and PWM_DISABLE_TIMER_SYNC().
  189. * \hideinitializer
  190. */
  191. #define PWM_TRIGGER_SYNC_START(pwm) ((pwm)->SSTRG = PWM_SSTRG_CNTSEN_Msk)
  192. /**
  193. * @brief This macro enable output inverter of specified channel(s)
  194. * @param[in] pwm The pointer of the specified PWM module
  195. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  196. * Bit 0 represents channel 0, bit 1 represents channel 1...
  197. * @return None
  198. * @details This macro is used to enable output inverter of specified channel(s).
  199. * \hideinitializer
  200. */
  201. #define PWM_ENABLE_OUTPUT_INVERTER(pwm, u32ChannelMask) ((pwm)->POLCTL = (u32ChannelMask))
  202. /**
  203. * @brief This macro get captured rising data
  204. * @param[in] pwm The pointer of the specified PWM module
  205. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  206. * @return None
  207. * @details This macro is used to get captured rising data of specified channel.
  208. * \hideinitializer
  209. */
  210. #define PWM_GET_CAPTURE_RISING_DATA(pwm, u32ChannelNum) (*(__IO uint32_t *) (&((pwm)->RCAPDAT0) + ((u32ChannelNum) << 1)))
  211. /**
  212. * @brief This macro get captured falling data
  213. * @param[in] pwm The pointer of the specified PWM module
  214. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  215. * @return None
  216. * @details This macro is used to get captured falling data of specified channel.
  217. * \hideinitializer
  218. */
  219. #define PWM_GET_CAPTURE_FALLING_DATA(pwm, u32ChannelNum) (*(__IO uint32_t *) (&((pwm)->FCAPDAT0) + ((u32ChannelNum) << 1)))
  220. /**
  221. * @brief This macro mask output logic to high or low
  222. * @param[in] pwm The pointer of the specified PWM module
  223. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  224. * Bit 0 represents channel 0, bit 1 represents channel 1...
  225. * @param[in] u32LevelMask Output logic to high or low
  226. * @return None
  227. * @details This macro is used to mask output logic to high or low of specified channel(s).
  228. * @note If u32ChannelMask parameter is 0, then mask function will be disabled.
  229. * \hideinitializer
  230. */
  231. #define PWM_MASK_OUTPUT(pwm, u32ChannelMask, u32LevelMask) \
  232. { \
  233. (pwm)->MSKEN = (u32ChannelMask); \
  234. (pwm)->MSK = (u32LevelMask); \
  235. }
  236. /**
  237. * @brief This macro set the prescaler of the selected channel
  238. * @param[in] pwm The pointer of the specified PWM module
  239. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  240. * @param[in] u32Prescaler Clock prescaler of specified channel. Valid values are between 0 ~ 0xFFF
  241. * @return None
  242. * @details This macro is used to set the prescaler of specified channel.
  243. * @note Every even channel N, and channel (N + 1) share a prescaler. So if channel 0 prescaler changed, channel 1 will also be affected.
  244. * The clock of PWM counter is divided by (u32Prescaler + 1).
  245. * \hideinitializer
  246. */
  247. #define PWM_SET_PRESCALER(pwm, u32ChannelNum, u32Prescaler) ((pwm)->CLKPSC[(u32ChannelNum) >> 1] = (u32Prescaler))
  248. /**
  249. * @brief This macro get the prescaler of the selected channel
  250. * @param[in] pwm The pointer of the specified PWM module
  251. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  252. * @return Return Clock prescaler of specified channel. Valid values are between 0 ~ 0xFFF
  253. * @details This macro is used to get the prescaler of specified channel.
  254. * @note Every even channel N, and channel (N + 1) share a prescaler. So if channel 0 prescaler changed, channel 1 will also be affected.
  255. * The clock of PWM counter is divided by (u32Prescaler + 1).
  256. * \hideinitializer
  257. */
  258. #define PWM_GET_PRESCALER(pwm, u32ChannelNum) (*(__IO uint32_t *) (&((pwm)->CLKPSC[0]) + ((u32ChannelNum) >> 1)))
  259. /**
  260. * @brief This macro set the comparator of the selected channel
  261. * @param[in] pwm The pointer of the specified PWM module
  262. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  263. * @param[in] u32CMR Comparator of specified channel. Valid values are between 0~0xFFFF
  264. * @return None
  265. * @details This macro is used to set the comparator of specified channel.
  266. * @note This new setting will take effect on next PWM period.
  267. * \hideinitializer
  268. */
  269. #define PWM_SET_CMR(pwm, u32ChannelNum, u32CMR) ((pwm)->CMPDAT[(u32ChannelNum)]= (u32CMR))
  270. /**
  271. * @brief This macro get the comparator of the selected channel
  272. * @param[in] pwm The pointer of the specified PWM module
  273. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  274. * @return Return the comparator of specified channel. Valid values are between 0~0xFFFF
  275. * @details This macro is used to get the comparator of specified channel.
  276. * \hideinitializer
  277. */
  278. #define PWM_GET_CMR(pwm, u32ChannelNum) ((pwm)->CMPDAT[(u32ChannelNum)])
  279. /**
  280. * @brief This macro set the period of the selected channel
  281. * @param[in] pwm The pointer of the specified PWM module
  282. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  283. * @param[in] u32CNR Period of specified channel. Valid values are between 0~0xFFFF
  284. * @return None
  285. * @details This macro is used to set the period of specified channel.
  286. * @note This new setting will take effect on next PWM period.
  287. * @note PWM counter will stop if period length set to 0.
  288. * \hideinitializer
  289. */
  290. #define PWM_SET_CNR(pwm, u32ChannelNum, u32CNR) ((pwm)->PERIOD[(u32ChannelNum>>1)<<1] = (u32CNR))
  291. /**
  292. * @brief This macro get the period of the selected channel
  293. * @param[in] pwm The pointer of the specified PWM module
  294. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  295. * @return Return the period of specified channel. Valid values are between 0~0xFFFF
  296. * @details This macro is used to get the period of specified channel.
  297. * \hideinitializer
  298. */
  299. #define PWM_GET_CNR(pwm, u32ChannelNum) ((pwm)->PERIOD[(u32ChannelNum>>1)<<1])
  300. /**
  301. * @brief This macro set the PWM aligned type
  302. * @param[in] pwm The pointer of the specified PWM module
  303. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  304. * Bit 0 represents channel 0, bit 1 represents channel 1...
  305. * @param[in] u32AlignedType PWM aligned type, valid values are:
  306. * - \ref PWM_EDGE_ALIGNED
  307. * - \ref PWM_CENTER_ALIGNED
  308. * @return None
  309. * @details This macro is used to set the PWM aligned type of specified channel(s).
  310. * \hideinitializer
  311. */
  312. #define PWM_SET_ALIGNED_TYPE(pwm, u32ChannelMask, u32AlignedType) \
  313. do{ \
  314. int i; \
  315. for(i = 0; i < 6; i++) { \
  316. if((u32ChannelMask) & (1 << i)) \
  317. (pwm)->CTL1 = (((pwm)->CTL1 & ~(3UL << (i << 1))) | ((u32AlignedType) << (i << 1))); \
  318. } \
  319. }while(0)
  320. /**
  321. * @brief Clear counter of specified channel(s)
  322. * @param[in] pwm The pointer of the specified PWM module
  323. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  324. * Bit 0 represents channel 0, bit 1 represents channel 1...
  325. * @return None
  326. * @details This macro is used to clear counter of specified channel(s).
  327. * \hideinitializer
  328. */
  329. #define PWM_CLR_COUNTER(pwm, u32ChannelMask) \
  330. do{ \
  331. uint32_t i; \
  332. for(i = 0UL; i < 6UL; i++) { \
  333. if((u32ChannelMask) & (1UL << i)) \
  334. ((pwm)->CNTCLR |= (1UL << ((i >> 1UL) << 1UL))); \
  335. } \
  336. }while(0)
  337. /**
  338. * @brief Set output level at zero, compare up, period(center) and compare down of specified channel(s)
  339. * @param[in] pwm The pointer of the specified PWM module
  340. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  341. * Bit 0 represents channel 0, bit 1 represents channel 1...
  342. * @param[in] u32ZeroLevel output level at zero point, valid values are:
  343. * - \ref PWM_OUTPUT_NOTHING
  344. * - \ref PWM_OUTPUT_LOW
  345. * - \ref PWM_OUTPUT_HIGH
  346. * - \ref PWM_OUTPUT_TOGGLE
  347. * @param[in] u32CmpUpLevel output level at compare up point, valid values are:
  348. * - \ref PWM_OUTPUT_NOTHING
  349. * - \ref PWM_OUTPUT_LOW
  350. * - \ref PWM_OUTPUT_HIGH
  351. * - \ref PWM_OUTPUT_TOGGLE
  352. * @param[in] u32PeriodLevel output level at period(center) point, valid values are:
  353. * - \ref PWM_OUTPUT_NOTHING
  354. * - \ref PWM_OUTPUT_LOW
  355. * - \ref PWM_OUTPUT_HIGH
  356. * - \ref PWM_OUTPUT_TOGGLE
  357. * @param[in] u32CmpDownLevel output level at compare down point, valid values are:
  358. * - \ref PWM_OUTPUT_NOTHING
  359. * - \ref PWM_OUTPUT_LOW
  360. * - \ref PWM_OUTPUT_HIGH
  361. * - \ref PWM_OUTPUT_TOGGLE
  362. * @return None
  363. * @details This macro is used to Set output level at zero, compare up, period(center) and compare down of specified channel(s).
  364. * \hideinitializer
  365. */
  366. #define PWM_SET_OUTPUT_LEVEL(pwm, u32ChannelMask, u32ZeroLevel, u32CmpUpLevel, u32PeriodLevel, u32CmpDownLevel) \
  367. do{ \
  368. int i; \
  369. for(i = 0; i < 6; i++) { \
  370. if((u32ChannelMask) & (1 << i)) { \
  371. (pwm)->WGCTL0 = (((pwm)->WGCTL0 & ~(3UL << (i << 1))) | ((u32ZeroLevel) << (i << 1))); \
  372. (pwm)->WGCTL0 = (((pwm)->WGCTL0 & ~(3UL << (PWM_WGCTL0_PRDPCTL0_Pos + (i << 1)))) | ((u32PeriodLevel) << (PWM_WGCTL0_PRDPCTL0_Pos + (i << 1)))); \
  373. (pwm)->WGCTL1 = (((pwm)->WGCTL1 & ~(3UL << (i << 1))) | ((u32CmpUpLevel) << (i << 1))); \
  374. (pwm)->WGCTL1 = (((pwm)->WGCTL1 & ~(3UL << (PWM_WGCTL1_CMPDCTL0_Pos + (i << 1)))) | ((u32CmpDownLevel) << (PWM_WGCTL1_CMPDCTL0_Pos + (i << 1)))); \
  375. } \
  376. } \
  377. }while(0)
  378. /**
  379. * @brief Trigger brake event from specified channel(s)
  380. * @param[in] pwm The pointer of the specified PWM module
  381. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  382. * Bit 0 represents channel 0, bit 1 represents channel 2 and bit 2 represents channel 4
  383. * @param[in] u32BrakeType Type of brake trigger. PWM_FB_EDGE of this macro is only supported in M45xD/M45xC.
  384. * - \ref PWM_FB_EDGE
  385. * - \ref PWM_FB_LEVEL
  386. * @return None
  387. * @details This macro is used to trigger brake event from specified channel(s).
  388. * \hideinitializer
  389. */
  390. #define PWM_TRIGGER_BRAKE(pwm, u32ChannelMask, u32BrakeType) ((pwm)->SWBRK |= ((u32ChannelMask) << (u32BrakeType)))
  391. /**
  392. * @brief Set Dead zone clock source
  393. * @param[in] pwm The pointer of the specified PWM module
  394. * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5
  395. * @param[in] u32AfterPrescaler Dead zone clock source is from prescaler output. Valid values are TRUE (after prescaler) or FALSE (before prescaler).
  396. * @return None
  397. * @details This macro is used to set Dead zone clock source. Every two channels share the same setting.
  398. * @note The write-protection function should be disabled before using this function.
  399. * @note This function is only supported in M45xD/M45xC.
  400. * \hideinitializer
  401. */
  402. #define PWM_SET_DEADZONE_CLK_SRC(pwm, u32ChannelNum, u32AfterPrescaler) \
  403. ((pwm)->DTCTL[(u32ChannelNum) >> 1] = (((pwm)->DTCTL[(u32ChannelNum) >> 1] & ~PWM_DTCTL0_1_DTCKSEL_Msk) | \
  404. ((u32AfterPrescaler) << PWM_DTCTL0_1_DTCKSEL_Pos)))
  405. /*---------------------------------------------------------------------------------------------------------*/
  406. /* Define PWM functions prototype */
  407. /*---------------------------------------------------------------------------------------------------------*/
  408. uint32_t PWM_ConfigCaptureChannel(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32UnitTimeNsec, uint32_t u32CaptureEdge);
  409. uint32_t PWM_ConfigOutputChannel(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Frequency, uint32_t u32DutyCycle);
  410. void PWM_Start(PWM_T *pwm, uint32_t u32ChannelMask);
  411. void PWM_Stop(PWM_T *pwm, uint32_t u32ChannelMask);
  412. void PWM_ForceStop(PWM_T *pwm, uint32_t u32ChannelMask);
  413. void PWM_EnableADCTrigger(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition);
  414. void PWM_DisableADCTrigger(PWM_T *pwm, uint32_t u32ChannelNum);
  415. void PWM_ClearADCTriggerFlag(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition);
  416. uint32_t PWM_GetADCTriggerFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  417. void PWM_EnableFaultBrake(PWM_T *pwm, uint32_t u32ChannelMask, uint32_t u32LevelMask, uint32_t u32BrakeSource);
  418. void PWM_EnableCapture(PWM_T *pwm, uint32_t u32ChannelMask);
  419. void PWM_DisableCapture(PWM_T *pwm, uint32_t u32ChannelMask);
  420. void PWM_EnableOutput(PWM_T *pwm, uint32_t u32ChannelMask);
  421. void PWM_DisableOutput(PWM_T *pwm, uint32_t u32ChannelMask);
  422. void PWM_EnablePDMA(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32RisingFirst, uint32_t u32Mode);
  423. void PWM_DisablePDMA(PWM_T *pwm, uint32_t u32ChannelNum);
  424. void PWM_EnableDeadZone(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Duration);
  425. void PWM_DisableDeadZone(PWM_T *pwm, uint32_t u32ChannelNum);
  426. void PWM_EnableCaptureInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge);
  427. void PWM_DisableCaptureInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge);
  428. void PWM_ClearCaptureIntFlag(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge);
  429. uint32_t PWM_GetCaptureIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  430. void PWM_EnableDutyInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32IntDutyType);
  431. void PWM_DisableDutyInt(PWM_T *pwm, uint32_t u32ChannelNum);
  432. void PWM_ClearDutyIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  433. uint32_t PWM_GetDutyIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  434. void PWM_EnableFaultBrakeInt(PWM_T *pwm, uint32_t u32BrakeSource);
  435. void PWM_DisableFaultBrakeInt(PWM_T *pwm, uint32_t u32BrakeSource);
  436. void PWM_ClearFaultBrakeIntFlag(PWM_T *pwm, uint32_t u32BrakeSource);
  437. uint32_t PWM_GetFaultBrakeIntFlag(PWM_T *pwm, uint32_t u32BrakeSource);
  438. void PWM_EnablePeriodInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32IntPeriodType);
  439. void PWM_DisablePeriodInt(PWM_T *pwm, uint32_t u32ChannelNum);
  440. void PWM_ClearPeriodIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  441. uint32_t PWM_GetPeriodIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  442. void PWM_EnableZeroInt(PWM_T *pwm, uint32_t u32ChannelNum);
  443. void PWM_DisableZeroInt(PWM_T *pwm, uint32_t u32ChannelNum);
  444. void PWM_ClearZeroIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  445. uint32_t PWM_GetZeroIntFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  446. void PWM_EnableLoadMode(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32LoadMode);
  447. void PWM_DisableLoadMode(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32LoadMode);
  448. void PWM_SetClockSource(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32ClkSrcSel);
  449. void PWM_EnableBrakeNoiseFilter(PWM_T *pwm, uint32_t u32BrakePinNum, uint32_t u32ClkCnt, uint32_t u32ClkDivSel);
  450. void PWM_DisableBrakeNoiseFilter(PWM_T *pwm, uint32_t u32BrakePinNum);
  451. void PWM_EnableBrakePinInverse(PWM_T *pwm, uint32_t u32BrakePinNum);
  452. void PWM_DisableBrakePinInverse(PWM_T *pwm, uint32_t u32BrakePinNum);
  453. void PWM_SetBrakePinSource(PWM_T *pwm, uint32_t u32BrakePinNum, uint32_t u32SelAnotherModule);
  454. uint32_t PWM_GetWrapAroundFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  455. void PWM_ClearWrapAroundFlag(PWM_T *pwm, uint32_t u32ChannelNum);
  456. /*@}*/ /* end of group PWM_EXPORTED_FUNCTIONS */
  457. /*@}*/ /* end of group PWM_Driver */
  458. /*@}*/ /* end of group Standard_Driver */
  459. #ifdef __cplusplus
  460. }
  461. #endif
  462. #endif //__NU_PWM_H__
  463. /*** (C) COPYRIGHT 2017 Nuvoton Technology Corp. ***/