nu_timer_pwm.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /**************************************************************************//**
  2. * @file nu_timer_pwm.h
  3. * @version V3.00
  4. * @brief Timer PWM Controller(Timer PWM) driver header file
  5. *
  6. * @copyright SPDX-License-Identifier: Apache-2.0
  7. * @copyright Copyright (C) 2021 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #ifndef __NU_TIMER_PWM_H__
  10. #define __NU_TIMER_PWM_H__
  11. #ifdef __cplusplus
  12. extern "C"
  13. {
  14. #endif
  15. /** @addtogroup Standard_Driver Standard Driver
  16. @{
  17. */
  18. /** @addtogroup TIMER_PWM_Driver TIMER PWM Driver
  19. @{
  20. */
  21. /** @addtogroup TIMER_PWM_EXPORTED_CONSTANTS TIMER PWM Exported Constants
  22. @{
  23. */
  24. /*---------------------------------------------------------------------------------------------------------*/
  25. /* Output Channel Constant Definitions */
  26. /*---------------------------------------------------------------------------------------------------------*/
  27. #define TPWM_CH0 (BIT0) /*!< Indicate PWMx_CH0 \hideinitializer */
  28. #define TPWM_CH1 (BIT1) /*!< Indicate PWMx_CH1 \hideinitializer */
  29. /*---------------------------------------------------------------------------------------------------------*/
  30. /* Counter Type Constant Definitions */
  31. /*---------------------------------------------------------------------------------------------------------*/
  32. #define TPWM_UP_COUNT (0UL << TIMER_PWMCTL_CNTTYPE_Pos) /*!< Up count type \hideinitializer */
  33. #define TPWM_DOWN_COUNT (1UL << TIMER_PWMCTL_CNTTYPE_Pos) /*!< Down count type \hideinitializer */
  34. #define TPWM_UP_DOWN_COUNT (2UL << TIMER_PWMCTL_CNTTYPE_Pos) /*!< Up-Down count type \hideinitializer */
  35. /*---------------------------------------------------------------------------------------------------------*/
  36. /* Counter Mode Constant Definitions */
  37. /*---------------------------------------------------------------------------------------------------------*/
  38. #define TPWM_AUTO_RELOAD_MODE (0UL) /*!< Auto-reload mode \hideinitializer */
  39. #define TPWM_ONE_SHOT_MODE (TIMER_PWMCTL_CNTMODE_Msk) /*!< One-shot mode \hideinitializer */
  40. /*---------------------------------------------------------------------------------------------------------*/
  41. /* Output Level Constant Definitions */
  42. /*---------------------------------------------------------------------------------------------------------*/
  43. #define TPWM_OUTPUT_TOGGLE (0UL) /*!< Timer PWM output toggle \hideinitializer */
  44. #define TPWM_OUTPUT_NOTHING (1UL) /*!< Timer PWM output nothing \hideinitializer */
  45. #define TPWM_OUTPUT_LOW (2UL) /*!< Timer PWM output low \hideinitializer */
  46. #define TPWM_OUTPUT_HIGH (3UL) /*!< Timer PWM output high \hideinitializer */
  47. /*---------------------------------------------------------------------------------------------------------*/
  48. /* Trigger EADC Source Select Constant Definitions */
  49. /*---------------------------------------------------------------------------------------------------------*/
  50. #define TPWM_TRIGGER_EADC_AT_ZERO_POINT (0UL << TIMER_PWMTRGCTL_TRGSEL_Pos) /*!< Timer PWM trigger EADC while counter zero point event occurred \hideinitializer */
  51. #define TPWM_TRIGGER_EADC_AT_PERIOD_POINT (1UL << TIMER_PWMTRGCTL_TRGSEL_Pos) /*!< Timer PWM trigger EADC while counter period point event occurred \hideinitializer */
  52. #define TPWM_TRIGGER_EADC_AT_ZERO_OR_PERIOD_POINT (2UL << TIMER_PWMTRGCTL_TRGSEL_Pos) /*!< Timer PWM trigger EADC while counter zero or period point event occurred \hideinitializer */
  53. #define TPWM_TRIGGER_EADC_AT_COMPARE_UP_COUNT_POINT (3UL << TIMER_PWMTRGCTL_TRGSEL_Pos) /*!< Timer PWM trigger EADC while counter up count compare point event occurred \hideinitializer */
  54. #define TPWM_TRIGGER_EADC_AT_COMPARE_DOWN_COUNT_POINT (4UL << TIMER_PWMTRGCTL_TRGSEL_Pos) /*!< Timer PWM trigger EADC while counter down count compare point event occurred \hideinitializer */
  55. /*---------------------------------------------------------------------------------------------------------*/
  56. /* Brake Control Constant Definitions */
  57. /*---------------------------------------------------------------------------------------------------------*/
  58. #define TPWM_BRAKE_SOURCE_EDGE_ACMP0 (TIMER_PWMBRKCTL_CPO0EBEN_Msk) /*!< Comparator 0 as edge-detect fault brake source \hideinitializer */
  59. #define TPWM_BRAKE_SOURCE_EDGE_ACMP1 (TIMER_PWMBRKCTL_CPO1EBEN_Msk) /*!< Comparator 1 as edge-detect fault brake source \hideinitializer */
  60. #define TPWM_BRAKE_SOURCE_EDGE_BKPIN (TIMER_PWMBRKCTL_BRKPEEN_Msk) /*!< Brake pin as edge-detect fault brake source \hideinitializer */
  61. #define TPWM_BRAKE_SOURCE_EDGE_SYS_CSS (TIMER_PWMBRKCTL_SYSEBEN_Msk | (TIMER_PWMFAILBRK_CSSBRKEN_Msk << 16)) /*!< System fail condition: clock security system detection as edge-detect fault brake source \hideinitializer */
  62. #define TPWM_BRAKE_SOURCE_EDGE_SYS_BOD (TIMER_PWMBRKCTL_SYSEBEN_Msk | (TIMER_PWMFAILBRK_BODBRKEN_Msk << 16)) /*!< System fail condition: brown-out detection as edge-detect fault brake source \hideinitializer */
  63. #define TPWM_BRAKE_SOURCE_EDGE_SYS_COR (TIMER_PWMBRKCTL_SYSEBEN_Msk | (TIMER_PWMFAILBRK_CORBRKEN_Msk << 16)) /*!< System fail condition: core lockup detection as edge-detect fault brake source \hideinitializer */
  64. #define TPWM_BRAKE_SOURCE_EDGE_SYS_RAM (TIMER_PWMBRKCTL_SYSEBEN_Msk | (TIMER_PWMFAILBRK_RAMBRKEN_Msk << 16)) /*!< System fail condition: SRAM parity error detection as edge-detect fault brake source \hideinitializer */
  65. #define TPWM_BRAKE_SOURCE_LEVEL_ACMP0 (TIMER_PWMBRKCTL_CPO0LBEN_Msk) /*!< Comparator 0 as level-detect fault brake source \hideinitializer */
  66. #define TPWM_BRAKE_SOURCE_LEVEL_ACMP1 (TIMER_PWMBRKCTL_CPO1LBEN_Msk) /*!< Comparator 1 as level-detect fault brake source \hideinitializer */
  67. #define TPWM_BRAKE_SOURCE_LEVEL_BKPIN (TIMER_PWMBRKCTL_BRKPLEN_Msk) /*!< Brake pin as level-detect fault brake source \hideinitializer */
  68. #define TPWM_BRAKE_SOURCE_LEVEL_SYS_CSS (TIMER_PWMBRKCTL_SYSLBEN_Msk | (TIMER_PWMFAILBRK_CSSBRKEN_Msk << 16)) /*!< System fail condition: clock security system detection as level-detect fault brake source \hideinitializer */
  69. #define TPWM_BRAKE_SOURCE_LEVEL_SYS_BOD (TIMER_PWMBRKCTL_SYSLBEN_Msk | (TIMER_PWMFAILBRK_BODBRKEN_Msk << 16)) /*!< System fail condition: brown-out detection as level-detect fault brake source \hideinitializer */
  70. #define TPWM_BRAKE_SOURCE_LEVEL_SYS_COR (TIMER_PWMBRKCTL_SYSLBEN_Msk | (TIMER_PWMFAILBRK_CORBRKEN_Msk << 16)) /*!< System fail condition: core lockup detection as level-detect fault brake source \hideinitializer */
  71. #define TPWM_BRAKE_SOURCE_LEVEL_SYS_RAM (TIMER_PWMBRKCTL_SYSLBEN_Msk | (TIMER_PWMFAILBRK_RAMBRKEN_Msk << 16)) /*!< System fail condition: SRAM parity error detection as level-detect fault brake source \hideinitializer */
  72. #define TPWM_BRAKE_EDGE (TIMER_PWMSWBRK_BRKETRG_Msk) /*!< Edge-detect fault brake \hideinitializer */
  73. #define TPWM_BRAKE_LEVEL (TIMER_PWMSWBRK_BRKLTRG_Msk) /*!< Level-detect fault brake \hideinitializer */
  74. /*---------------------------------------------------------------------------------------------------------*/
  75. /* Load Mode Constant Definitions */
  76. /*---------------------------------------------------------------------------------------------------------*/
  77. #define TPWM_LOAD_MODE_PERIOD (0UL) /*!< Timer PWM period load mode \hideinitializer */
  78. #define TPWM_LOAD_MODE_IMMEDIATE (TIMER_PWMCTL_IMMLDEN_Msk) /*!< Timer PWM immediately load mode \hideinitializer */
  79. #define TPWM_LOAD_MODE_CENTER (TIMER_PWMCTL_CTRLD_Msk) /*!< Timer PWM center load mode \hideinitializer */
  80. /*---------------------------------------------------------------------------------------------------------*/
  81. /* Brake Pin De-bounce Clock Source Select Constant Definitions */
  82. /*---------------------------------------------------------------------------------------------------------*/
  83. #define TPWM_BKP_DBCLK_PCLK_DIV_1 (0UL) /*!< De-bounce clock is PCLK divide by 1 \hideinitializer */
  84. #define TPWM_BKP_DBCLK_PCLK_DIV_2 (1UL) /*!< De-bounce clock is PCLK divide by 2 \hideinitializer */
  85. #define TPWM_BKP_DBCLK_PCLK_DIV_4 (2UL) /*!< De-bounce clock is PCLK divide by 4 \hideinitializer */
  86. #define TPWM_BKP_DBCLK_PCLK_DIV_8 (3UL) /*!< De-bounce clock is PCLK divide by 8 \hideinitializer */
  87. #define TPWM_BKP_DBCLK_PCLK_DIV_16 (4UL) /*!< De-bounce clock is PCLK divide by 16 \hideinitializer */
  88. #define TPWM_BKP_DBCLK_PCLK_DIV_32 (5UL) /*!< De-bounce clock is PCLK divide by 32 \hideinitializer */
  89. #define TPWM_BKP_DBCLK_PCLK_DIV_64 (6UL) /*!< De-bounce clock is PCLK divide by 64 \hideinitializer */
  90. #define TPWM_BKP_DBCLK_PCLK_DIV_128 (7UL) /*!< De-bounce clock is PCLK divide by 128 \hideinitializer */
  91. /*---------------------------------------------------------------------------------------------------------*/
  92. /* Brake Pin Source Select Constant Definitions */
  93. /*---------------------------------------------------------------------------------------------------------*/
  94. #define TPWM_TM_BRAKE0 (0UL) /*!< Brake pin source comes from TM_BRAKE0 \hideinitializer */
  95. #define TPWM_TM_BRAKE1 (1UL) /*!< Brake pin source comes from TM_BRAKE1 \hideinitializer */
  96. #define TPWM_TM_BRAKE2 (2UL) /*!< Brake pin source comes from TM_BRAKE2 \hideinitializer */
  97. #define TPWM_TM_BRAKE3 (3UL) /*!< Brake pin source comes from TM_BRAKE3 \hideinitializer */
  98. /*---------------------------------------------------------------------------------------------------------*/
  99. /* Counter Clock Source Select Constant Definitions */
  100. /*---------------------------------------------------------------------------------------------------------*/
  101. #define TPWM_CNTR_CLKSRC_TMR_CLK (0UL) /*!< Timer PWM Clock source selects to TMR_CLK \hideinitializer */
  102. #define TPWM_CNTR_CLKSRC_TIMER0_INT (1UL) /*!< Timer PWM Clock source selects to TIMER0 interrupt event \hideinitializer */
  103. #define TPWM_CNTR_CLKSRC_TIMER1_INT (2UL) /*!< Timer PWM Clock source selects to TIMER1 interrupt event \hideinitializer */
  104. #define TPWM_CNTR_CLKSRC_TIMER2_INT (3UL) /*!< Timer PWM Clock source selects to TIMER2 interrupt event \hideinitializer */
  105. #define TPWM_CNTR_CLKSRC_TIMER3_INT (4UL) /*!< Timer PWM Clock source selects to TIMER3 interrupt event \hideinitializer */
  106. /*---------------------------------------------------------------------------------------------------------*/
  107. /* Counter Synchronous Mode Constant Definitions */
  108. /*---------------------------------------------------------------------------------------------------------*/
  109. #define TPWM_CNTR_SYNC_DISABLE (0UL) /*!< Disable TIMER PWM synchronous function \hideinitializer */
  110. #define TPWM_CNTR_SYNC_START_BY_TIMER0 ((0<<TIMER_PWMSCTL_SYNCSRC_Pos) | (1<<TIMER_PWMSCTL_SYNCMODE_Pos)) /*!< PWM counter synchronous start by TIMER0 PWM \hideinitializer */
  111. #define TPWM_CNTR_SYNC_CLEAR_BY_TIMER0 ((0<<TIMER_PWMSCTL_SYNCSRC_Pos) | (3<<TIMER_PWMSCTL_SYNCMODE_Pos)) /*!< PWM counter synchronous clear by TIMER0 PWM \hideinitializer */
  112. #define TPWM_CNTR_SYNC_START_BY_TIMER2 ((1<<TIMER_PWMSCTL_SYNCSRC_Pos) | (1<<TIMER_PWMSCTL_SYNCMODE_Pos)) /*!< PWM counter synchronous start by TIMER2 PWM \hideinitializer */
  113. #define TPWM_CNTR_SYNC_CLEAR_BY_TIMER2 ((1<<TIMER_PWMSCTL_SYNCSRC_Pos) | (3<<TIMER_PWMSCTL_SYNCMODE_Pos)) /*!< PWM counter synchronous clear by TIMER2 PWM \hideinitializer */
  114. /*---------------------------------------------------------------------------------------------------------*/
  115. /* Interrupt Flag Accumulator Constant Definitions */
  116. /*---------------------------------------------------------------------------------------------------------*/
  117. #define TPWM_IFA_ZERO_POINT (0U) /*!< Timer PWM counter equal to zero \hideinitializer */
  118. #define TPWM_IFA_PERIOD_POINT (1U) /*!< Timer PWM counter equal to period \hideinitializer */
  119. #define TPWM_IFA_COMPARE_UP_COUNT_POINT (2U) /*!< Timer PWM counter up count to comparator value \hideinitializer */
  120. #define TPWM_IFA_COMPARE_DOWN_COUNT_POINT (3U) /*!< Timer PWM counter down count to comparator value \hideinitializer */
  121. /*---------------------------------------------------------------------------------------------------------*/
  122. /* External Event Trigger Pin Constant Definitions */
  123. /*---------------------------------------------------------------------------------------------------------*/
  124. #define TPWM_EXT_TGR_PIN_INT0 (0U) /*!< Timer PWM external event trigger source form INT0 pin \hideinitializer */
  125. #define TPWM_EXT_TGR_PIN_INT1 (1U) /*!< Timer PWM external event trigger source form INT1 pin \hideinitializer */
  126. #define TPWM_EXT_TGR_PIN_INT2 (2U) /*!< Timer PWM external event trigger source form INT2 pin \hideinitializer */
  127. #define TPWM_EXT_TGR_PIN_INT3 (3U) /*!< Timer PWM external event trigger source form INT3 pin \hideinitializer */
  128. #define TPWM_EXT_TGR_PIN_INT4 (4U) /*!< Timer PWM external event trigger source form INT4 pin \hideinitializer */
  129. #define TPWM_EXT_TGR_PIN_INT5 (5U) /*!< Timer PWM external event trigger source form INT5 pin \hideinitializer */
  130. #define TPWM_EXT_TGR_PIN_INT6 (6U) /*!< Timer PWM external event trigger source form INT6 pin \hideinitializer */
  131. #define TPWM_EXT_TGR_PIN_INT7 (7U) /*!< Timer PWM external event trigger source form INT7 pin \hideinitializer */
  132. /*---------------------------------------------------------------------------------------------------------*/
  133. /* External Event Trigger Counter Action Constant Definitions */
  134. /*---------------------------------------------------------------------------------------------------------*/
  135. #define TPWM_EXT_TGR_COUNTER_RESET (0U) /*!< Timer PWM external event trigger counter reset \hideinitializer */
  136. #define TPWM_EXT_TGR_COUNTER_START (1U) /*!< Timer PWM external event trigger counter start \hideinitializer */
  137. #define TPWM_EXT_TGR_COUNTER_RESET_AND_START (2U) /*!< Timer PWM external event trigger counter reset and start \hideinitializer */
  138. /*@}*/ /* end of group TIMER_PWM_EXPORTED_CONSTANTS */
  139. /** @addtogroup TIMER_PWM_EXPORTED_FUNCTIONS TIMER PWM Exported Functions
  140. @{
  141. */
  142. /**
  143. * @brief Enable PWM Counter Mode
  144. *
  145. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  146. *
  147. * @return None
  148. *
  149. * @details This macro is used to enable specified Timer channel as PWM counter mode, then timer counter mode is invalid.
  150. * @note All registers about time counter function will be cleared to 0 and timer clock source will be changed to PCLKx automatically after executing this macro.
  151. * \hideinitializer
  152. */
  153. #define TPWM_ENABLE_PWM_MODE(timer) ((timer)->ALTCTL = (1 << TIMER_ALTCTL_FUNCSEL_Pos))
  154. /**
  155. * @brief Disable PWM Counter Mode
  156. *
  157. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  158. *
  159. * @return None
  160. *
  161. * @details This macro is used to disable specified Timer channel as PWM counter mode, then timer counter mode is available.
  162. * @note All registers about PWM counter function will be cleared to 0 after executing this macro.
  163. * \hideinitializer
  164. */
  165. #define TPWM_DISABLE_PWM_MODE(timer) ((timer)->ALTCTL = (0 << TIMER_ALTCTL_FUNCSEL_Pos))
  166. /**
  167. * @brief Enable Independent Mode
  168. *
  169. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  170. *
  171. * @return None
  172. *
  173. * @details This macro is used to enable independent mode of TIMER PWM module and complementary mode will be disabled.
  174. * \hideinitializer
  175. */
  176. #define TPWM_ENABLE_INDEPENDENT_MODE(timer) ((timer)->PWMCTL &= ~(1 << TIMER_PWMCTL_OUTMODE_Pos))
  177. /**
  178. * @brief Enable Complementary Mode
  179. *
  180. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  181. *
  182. * @return None
  183. *
  184. * @details This macro is used to enable complementary mode of Timer PWM module and independent mode will be disabled.
  185. * \hideinitializer
  186. */
  187. #define TPWM_ENABLE_COMPLEMENTARY_MODE(timer) ((timer)->PWMCTL |= (1 << TIMER_PWMCTL_OUTMODE_Pos))
  188. /**
  189. * @brief Set Counter Type
  190. *
  191. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  192. * @param[in] type Timer PWM count type, could be one of the following type
  193. * - \ref TPWM_UP_COUNT
  194. * - \ref TPWM_DOWN_COUNT
  195. * - \ref TPWM_UP_DOWN_COUNT
  196. *
  197. * @return None
  198. *
  199. * @details This macro is used to set Timer PWM counter type.
  200. * \hideinitializer
  201. */
  202. #define TPWM_SET_COUNTER_TYPE(timer, type) ((timer)->PWMCTL = ((timer)->PWMCTL & ~TIMER_PWMCTL_CNTTYPE_Msk) | (type))
  203. /**
  204. * @brief Start PWM Counter
  205. *
  206. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  207. *
  208. * @return None
  209. *
  210. * @details This macro is used to enable PWM generator and start counter counting.
  211. * \hideinitializer
  212. */
  213. #define TPWM_START_COUNTER(timer) ((timer)->PWMCTL |= TIMER_PWMCTL_CNTEN_Msk)
  214. /**
  215. * @brief Stop PWM Counter
  216. *
  217. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  218. *
  219. * @return None
  220. *
  221. * @details This macro is used to stop PWM counter after current period is completed.
  222. * \hideinitializer
  223. */
  224. #define TPWM_STOP_COUNTER(timer) ((timer)->PWMPERIOD = 0x0)
  225. /**
  226. * @brief Set Counter Clock Prescaler
  227. *
  228. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  229. *
  230. * @param[in] prescaler Clock prescaler of specified channel. Valid values are between 0x0~0xFFF.
  231. *
  232. * @return None
  233. *
  234. * @details This macro is used to set the prescaler of specified TIMER PWM.
  235. * @note If prescaler is 0, then there is no scaling in counter clock source.
  236. * \hideinitializer
  237. */
  238. #define TPWM_SET_PRESCALER(timer, prescaler) ((timer)->PWMCLKPSC = (prescaler))
  239. /**
  240. * @brief Get Counter Clock Prescaler
  241. *
  242. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  243. *
  244. * @return Target prescaler setting, CLKPSC (TIMERx_PWMCLKPSC[11:0])
  245. *
  246. * @details Get the prescaler setting, the target counter clock divider is (CLKPSC + 1).
  247. * \hideinitializer
  248. */
  249. #define TPWM_GET_PRESCALER(timer) ((timer)->PWMCLKPSC)
  250. /**
  251. * @brief Set Counter Period
  252. *
  253. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  254. *
  255. * @param[in] period Period of specified channel. Valid values are between 0x0~0xFFFF.
  256. *
  257. * @return None
  258. *
  259. * @details This macro is used to set the period of specified TIMER PWM.
  260. * \hideinitializer
  261. */
  262. #define TPWM_SET_PERIOD(timer, period) ((timer)->PWMPERIOD = (period))
  263. /**
  264. * @brief Get Counter Period
  265. *
  266. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  267. *
  268. * @return Target period setting, PERIOD (TIMERx_PWMPERIOD[15:0])
  269. *
  270. * @details This macro is used to get the period of specified TIMER PWM.
  271. * \hideinitializer
  272. */
  273. #define TPWM_GET_PERIOD(timer) ((timer)->PWMPERIOD)
  274. /**
  275. * @brief Set Comparator Value
  276. *
  277. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  278. *
  279. * @param[in] cmp Comparator of specified channel. Valid values are between 0x0~0xFFFF.
  280. *
  281. * @return None
  282. *
  283. * @details This macro is used to set the comparator value of specified TIMER PWM.
  284. * \hideinitializer
  285. */
  286. #define TPWM_SET_CMPDAT(timer, cmp) ((timer)->PWMCMPDAT = (cmp))
  287. /**
  288. * @brief Get Comparator Value
  289. *
  290. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  291. *
  292. * @return Target comparator setting, CMPDAT (TIMERx_PWMCMPDAT[15:0])
  293. *
  294. * @details This macro is used to get the comparator value of specified TIMER PWM.
  295. * \hideinitializer
  296. */
  297. #define TPWM_GET_CMPDAT(timer) ((timer)->PWMCMPDAT)
  298. /**
  299. * @brief Clear Counter
  300. *
  301. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  302. *
  303. * @return None
  304. *
  305. * @details This macro is used to clear counter of specified TIMER PWM.
  306. * \hideinitializer
  307. */
  308. #define TPWM_CLEAR_COUNTER(timer) ((timer)->PWMCNTCLR = TIMER_PWMCNTCLR_CNTCLR_Msk)
  309. /**
  310. * @brief Software Trigger Brake Event
  311. *
  312. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  313. *
  314. * @param[in] type Type of brake trigger. Valid values are:
  315. * - \ref TPWM_BRAKE_EDGE
  316. * - \ref TPWM_BRAKE_LEVEL
  317. *
  318. * @return None
  319. *
  320. * @details This macro is used to trigger brake event by writing PWMSWBRK register.
  321. * \hideinitializer
  322. */
  323. #define TPWM_SW_TRIGGER_BRAKE(timer, type) ((timer)->PWMSWBRK = (type))
  324. /**
  325. * @brief Enable Output Function
  326. *
  327. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  328. *
  329. * @param[in] ch Enable specified channel output function. Valid values are the combination of:
  330. * - \ref TPWM_CH0
  331. * - \ref TPWM_CH1
  332. *
  333. * @return None
  334. *
  335. * @details This macro is used to enable output function of specified output pins.
  336. * @note If the corresponding bit in ch parameter is 0, then output function will be disabled in this channel.
  337. * \hideinitializer
  338. */
  339. #define TPWM_ENABLE_OUTPUT(timer, ch) ((timer)->PWMPOEN = (ch))
  340. /**
  341. * @brief Set Output Inverse
  342. *
  343. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  344. *
  345. * @param[in] ch Set specified channel output is inversed or not. Valid values are the combination of:
  346. * - \ref TPWM_CH0
  347. * - \ref TPWM_CH1
  348. *
  349. * @return None
  350. *
  351. * @details This macro is used to enable output inverse of specified output pins.
  352. * @note If ch parameter is 0, then output inverse function will be disabled.
  353. * \hideinitializer
  354. */
  355. #define TPWM_SET_OUTPUT_INVERSE(timer, ch) ((timer)->PWMPOLCTL = (ch))
  356. /**
  357. * @brief Enable Output Function
  358. *
  359. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  360. *
  361. * @param[in] ch Enable specified channel output mask function. Valid values are the combination of:
  362. * - \ref TPWM_CH0
  363. * - \ref TPWM_CH1
  364. *
  365. * @param[in] level Output to high or low on specified mask channel.
  366. *
  367. * @return None
  368. *
  369. * @details This macro is used to enable output function of specified output pins.
  370. * @note If ch parameter is 0, then output mask function will be disabled.
  371. * \hideinitializer
  372. */
  373. #define TPWM_SET_MASK_OUTPUT(timer, ch, level) do {(timer)->PWMMSKEN = (ch); (timer)->PWMMSK = (level); }while(0)
  374. /**
  375. * @brief Set Counter Synchronous Mode
  376. *
  377. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  378. *
  379. * @param[in] mode Synchronous mode. Possible options are:
  380. * - \ref TPWM_CNTR_SYNC_DISABLE
  381. * - \ref TPWM_CNTR_SYNC_START_BY_TIMER0
  382. * - \ref TPWM_CNTR_SYNC_CLEAR_BY_TIMER0
  383. * - \ref TPWM_CNTR_SYNC_START_BY_TIMER2
  384. * - \ref TPWM_CNTR_SYNC_CLEAR_BY_TIMER2
  385. *
  386. * @return None
  387. *
  388. * @details This macro is used to set counter synchronous mode of specified Timer PWM module.
  389. * @note Only support all PWM counters are synchronous by TIMER0 PWM or TIMER0~1 PWM counter synchronous by TIMER0 PWM and
  390. * TIMER2~3 PWM counter synchronous by TIMER2 PWM.
  391. * \hideinitializer
  392. */
  393. #define TPWM_SET_COUNTER_SYNC_MODE(timer, mode) ((timer)->PWMSCTL = (mode))
  394. /**
  395. * @brief Trigger Counter Synchronous
  396. *
  397. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  398. *
  399. * @return None
  400. *
  401. * @details This macro is used to trigger synchronous event by specified TIMER PWM.
  402. * @note 1. This macro is only available for TIMER0 PWM and TIMER2 PWM. \n
  403. * 2. STRGEN (PWMSTRG[0]) is write only and always read as 0.
  404. * \hideinitializer
  405. */
  406. #define TPWM_TRIGGER_COUNTER_SYNC(timer) ((timer)->PWMSTRG = TIMER_PWMSTRG_STRGEN_Msk)
  407. /**
  408. * @brief Enable Zero Event Interrupt
  409. *
  410. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  411. *
  412. * @return None
  413. *
  414. * @details This macro is used to enable the zero event interrupt function.
  415. * \hideinitializer
  416. */
  417. #define TPWM_ENABLE_ZERO_INT(timer) ((timer)->PWMINTEN0 |= TIMER_PWMINTEN0_ZIEN_Msk)
  418. /**
  419. * @brief Disable Zero Event Interrupt
  420. *
  421. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  422. *
  423. * @return None
  424. *
  425. * @details This macro is used to disable the zero event interrupt function.
  426. * \hideinitializer
  427. */
  428. #define TPWM_DISABLE_ZERO_INT(timer) ((timer)->PWMINTEN0 &= ~TIMER_PWMINTEN0_ZIEN_Msk)
  429. /**
  430. * @brief Get Zero Event Interrupt Flag
  431. *
  432. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  433. *
  434. * @retval 0 Zero event interrupt did not occur
  435. * @retval 1 Zero event interrupt occurred
  436. *
  437. * @details This macro indicates zero event occurred or not.
  438. * \hideinitializer
  439. */
  440. #define TPWM_GET_ZERO_INT_FLAG(timer) (((timer)->PWMINTSTS0 & TIMER_PWMINTSTS0_ZIF_Msk)? 1 : 0)
  441. /**
  442. * @brief Clear Zero Event Interrupt Flag
  443. *
  444. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  445. *
  446. * @return None
  447. *
  448. * @details This macro clears zero event interrupt flag.
  449. * \hideinitializer
  450. */
  451. #define TPWM_CLEAR_ZERO_INT_FLAG(timer) ((timer)->PWMINTSTS0 = TIMER_PWMINTSTS0_ZIF_Msk)
  452. /**
  453. * @brief Enable Period Event Interrupt
  454. *
  455. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  456. *
  457. * @return None
  458. *
  459. * @details This macro is used to enable the period event interrupt function.
  460. * \hideinitializer
  461. */
  462. #define TPWM_ENABLE_PERIOD_INT(timer) ((timer)->PWMINTEN0 |= TIMER_PWMINTEN0_PIEN_Msk)
  463. /**
  464. * @brief Disable Period Event Interrupt
  465. *
  466. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  467. *
  468. * @return None
  469. *
  470. * @details This macro is used to disable the period event interrupt function.
  471. * \hideinitializer
  472. */
  473. #define TPWM_DISABLE_PERIOD_INT(timer) ((timer)->PWMINTEN0 &= ~TIMER_PWMINTEN0_PIEN_Msk)
  474. /**
  475. * @brief Get Period Event Interrupt Flag
  476. *
  477. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  478. *
  479. * @retval 0 Period event interrupt did not occur
  480. * @retval 1 Period event interrupt occurred
  481. *
  482. * @details This macro indicates period event occurred or not.
  483. * \hideinitializer
  484. */
  485. #define TPWM_GET_PERIOD_INT_FLAG(timer) (((timer)->PWMINTSTS0 & TIMER_PWMINTSTS0_PIF_Msk)? 1 : 0)
  486. /**
  487. * @brief Clear Period Event Interrupt Flag
  488. *
  489. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  490. *
  491. * @return None
  492. *
  493. * @details This macro clears period event interrupt flag.
  494. * \hideinitializer
  495. */
  496. #define TPWM_CLEAR_PERIOD_INT_FLAG(timer) ((timer)->PWMINTSTS0 = TIMER_PWMINTSTS0_PIF_Msk)
  497. /**
  498. * @brief Enable Compare Up Event Interrupt
  499. *
  500. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  501. *
  502. * @return None
  503. *
  504. * @details This macro is used to enable the compare up event interrupt function.
  505. * \hideinitializer
  506. */
  507. #define TPWM_ENABLE_CMP_UP_INT(timer) ((timer)->PWMINTEN0 |= TIMER_PWMINTEN0_CMPUIEN_Msk)
  508. /**
  509. * @brief Disable Compare Up Event Interrupt
  510. *
  511. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  512. *
  513. * @return None
  514. *
  515. * @details This macro is used to disable the compare up event interrupt function.
  516. * \hideinitializer
  517. */
  518. #define TPWM_DISABLE_CMP_UP_INT(timer) ((timer)->PWMINTEN0 &= ~TIMER_PWMINTEN0_CMPUIEN_Msk)
  519. /**
  520. * @brief Get Compare Up Event Interrupt Flag
  521. *
  522. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  523. *
  524. * @retval 0 Compare up event interrupt did not occur
  525. * @retval 1 Compare up event interrupt occurred
  526. *
  527. * @details This macro indicates compare up event occurred or not.
  528. * \hideinitializer
  529. */
  530. #define TPWM_GET_CMP_UP_INT_FLAG(timer) (((timer)->PWMINTSTS0 & TIMER_PWMINTSTS0_CMPUIF_Msk)? 1 : 0)
  531. /**
  532. * @brief Clear Compare Up Event Interrupt Flag
  533. *
  534. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  535. *
  536. * @return None
  537. *
  538. * @details This macro clears compare up event interrupt flag.
  539. * \hideinitializer
  540. */
  541. #define TPWM_CLEAR_CMP_UP_INT_FLAG(timer) ((timer)->PWMINTSTS0 = TIMER_PWMINTSTS0_CMPUIF_Msk)
  542. /**
  543. * @brief Enable Compare Down Event Interrupt
  544. *
  545. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  546. *
  547. * @return None
  548. *
  549. * @details This macro is used to enable the compare down event interrupt function.
  550. * \hideinitializer
  551. */
  552. #define TPWM_ENABLE_CMP_DOWN_INT(timer) ((timer)->PWMINTEN0 |= TIMER_PWMINTEN0_CMPDIEN_Msk)
  553. /**
  554. * @brief Disable Compare Down Event Interrupt
  555. *
  556. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  557. *
  558. * @return None
  559. *
  560. * @details This macro is used to disable the compare down event interrupt function.
  561. * \hideinitializer
  562. */
  563. #define TPWM_DISABLE_CMP_DOWN_INT(timer) ((timer)->PWMINTEN0 &= ~TIMER_PWMINTEN0_CMPDIEN_Msk)
  564. /**
  565. * @brief Get Compare Down Event Interrupt Flag
  566. *
  567. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  568. *
  569. * @retval 0 Compare down event interrupt did not occur
  570. * @retval 1 Compare down event interrupt occurred
  571. *
  572. * @details This macro indicates compare down event occurred or not.
  573. * \hideinitializer
  574. */
  575. #define TPWM_GET_CMP_DOWN_INT_FLAG(timer) (((timer)->PWMINTSTS0 & TIMER_PWMINTSTS0_CMPDIF_Msk)? 1 : 0)
  576. /**
  577. * @brief Clear Compare Down Event Interrupt Flag
  578. *
  579. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  580. *
  581. * @return None
  582. *
  583. * @details This macro clears compare down event interrupt flag.
  584. * \hideinitializer
  585. */
  586. #define TPWM_CLEAR_CMP_DOWN_INT_FLAG(timer) ((timer)->PWMINTSTS0 = TIMER_PWMINTSTS0_CMPDIF_Msk)
  587. /**
  588. * @brief Get Counter Reach Maximum Count Status
  589. *
  590. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  591. *
  592. * @retval 0 Timer PWM counter never counts to maximum value
  593. * @retval 1 Timer PWM counter counts to maximum value, 0xFFFF
  594. *
  595. * @details This macro indicates Timer PWM counter has count to 0xFFFF or not.
  596. * \hideinitializer
  597. */
  598. #define TPWM_GET_REACH_MAX_CNT_STATUS(timer) (((timer)->PWMSTATUS & TIMER_PWMSTATUS_CNTMAXF_Msk)? 1 : 0)
  599. /**
  600. * @brief Clear Counter Reach Maximum Count Status
  601. *
  602. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  603. *
  604. * @return None
  605. *
  606. * @details This macro clears reach maximum count status.
  607. * \hideinitializer
  608. */
  609. #define TPWM_CLEAR_REACH_MAX_CNT_STATUS(timer) ((timer)->PWMSTATUS = TIMER_PWMSTATUS_CNTMAXF_Msk)
  610. /**
  611. * @brief Get Trigger ADC Status
  612. *
  613. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  614. *
  615. * @retval 0 Trigger ADC start conversion is not occur
  616. * @retval 1 Specified counter compare event has trigger ADC start conversion
  617. *
  618. * @details This macro is used to indicate PWM counter compare event has triggered ADC start conversion.
  619. * \hideinitializer
  620. */
  621. #define TPWM_GET_TRG_ADC_STATUS(timer) (((timer)->PWMSTATUS & TIMER_PWMSTATUS_EADCTRGF_Msk)? 1 : 0)
  622. /**
  623. * @brief Clear Trigger ADC Status
  624. *
  625. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  626. *
  627. * @return None
  628. *
  629. * @details This macro is used to clear PWM counter compare event trigger ADC status.
  630. * \hideinitializer
  631. */
  632. #define TPWM_CLEAR_TRG_ADC_STATUS(timer) ((timer)->PWMSTATUS = TIMER_PWMSTATUS_EADCTRGF_Msk)
  633. /**
  634. * @brief Set Brake Event at Brake Pin High or Low-to-High
  635. *
  636. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  637. *
  638. * @return None
  639. *
  640. * @details This macro is used to set detect brake event when external brake pin at high level or transfer from low to high.
  641. * @note The default brake pin detection is high level or from low to high.
  642. * \hideinitializer
  643. */
  644. #define TPWM_SET_BRAKE_PIN_HIGH_DETECT(timer) ((timer)->PWMBNF &= ~TIMER_PWMBNF_BRKPINV_Msk)
  645. /**
  646. * @brief Set Brake Event at Brake Pin Low or High-to-Low
  647. *
  648. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  649. *
  650. * @return None
  651. *
  652. * @details This macro is used to set detect brake event when external brake pin at low level or transfer from high to low.
  653. * \hideinitializer
  654. */
  655. #define TPWM_SET_BRAKE_PIN_LOW_DETECT(timer) ((timer)->PWMBNF |= TIMER_PWMBNF_BRKPINV_Msk)
  656. /**
  657. * @brief Set External Brake Pin Source
  658. *
  659. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  660. * @param[in] pin The external brake pin source, could be one of following source
  661. * - \ref TPWM_TM_BRAKE0
  662. * - \ref TPWM_TM_BRAKE1
  663. * - \ref TPWM_TM_BRAKE2
  664. * - \ref TPWM_TM_BRAKE3
  665. *
  666. * @return None
  667. *
  668. * @details This macro is used to set detect brake event when external brake pin at high level or transfer from low to high.
  669. * \hideinitializer
  670. */
  671. #define TPWM_SET_BRAKE_PIN_SOURCE(timer, pin) ((timer)->PWMBNF = ((timer)->PWMBNF & ~TIMER_PWMBNF_BKPINSRC_Msk) | ((pin)<<TIMER_PWMBNF_BKPINSRC_Pos))
  672. void TPWM_SetCounterClockSource(TIMER_T *timer, uint32_t u32CntClkSrc);
  673. uint32_t TPWM_ConfigOutputFreqAndDuty(TIMER_T *timer, uint32_t u32Frequency, uint32_t u32DutyCycle);
  674. void TPWM_EnableDeadTime(TIMER_T *timer, uint32_t u32DTCount);
  675. void TPWM_EnableDeadTimeWithPrescale(TIMER_T *timer, uint32_t u32DTCount);
  676. void TPWM_DisableDeadTime(TIMER_T *timer);
  677. void TPWM_EnableCounter(TIMER_T *timer);
  678. void TPWM_DisableCounter(TIMER_T *timer);
  679. void TPWM_EnableTriggerADC(TIMER_T *timer, uint32_t u32Condition);
  680. void TPWM_DisableTriggerADC(TIMER_T *timer);
  681. void TPWM_EnableFaultBrake(TIMER_T *timer, uint32_t u32CH0Level, uint32_t u32CH1Level, uint32_t u32BrakeSource);
  682. void TPWM_EnableFaultBrakeInt(TIMER_T *timer, uint32_t u32IntSource);
  683. void TPWM_DisableFaultBrakeInt(TIMER_T *timer, uint32_t u32IntSource);
  684. uint32_t TPWM_GetFaultBrakeIntFlag(TIMER_T *timer, uint32_t u32IntSource);
  685. void TPWM_ClearFaultBrakeIntFlag(TIMER_T *timer, uint32_t u32IntSource);
  686. void TPWM_SetLoadMode(TIMER_T *timer, uint32_t u32LoadMode);
  687. void TPWM_EnableBrakePinDebounce(TIMER_T *timer, uint32_t u32BrakePinSrc, uint32_t u32DebounceCnt, uint32_t u32ClkSrcSel);
  688. void TPWM_DisableBrakePinDebounce(TIMER_T *timer);
  689. void TPWM_EnableBrakePinInverse(TIMER_T *timer);
  690. void TPWM_DisableBrakePinInverse(TIMER_T *timer);
  691. void TPWM_SetBrakePinSource(TIMER_T *timer, uint32_t u32BrakePinNum);
  692. void TPWM_EnableAcc(TIMER_T *timer, uint32_t u32IntFlagCnt, uint32_t u32IntAccSrc);
  693. void TPWM_DisableAcc(TIMER_T *timer);
  694. void TPWM_EnableAccInt(TIMER_T *timer);
  695. void TPWM_DisableAccInt(TIMER_T *timer);
  696. void TPWM_ClearAccInt(TIMER_T *timer);
  697. uint32_t TPWM_GetAccInt(TIMER_T *timer);
  698. void TPWM_EnableAccPDMA(TIMER_T *timer);
  699. void TPWM_DisableAccPDMA(TIMER_T *timer);
  700. void TPWM_EnableAccStopMode(TIMER_T *timer);
  701. void TPWM_DisableAccStopMode(TIMER_T *timer);
  702. void TPWM_EnableExtEventTrigger(TIMER_T *timer, uint32_t u32ExtEventSrc, uint32_t u32CounterAction);
  703. void TPWM_DisableExtEventTrigger(TIMER_T *timer);
  704. /*@}*/ /* end of group TIMER_PWM_EXPORTED_FUNCTIONS */
  705. /*@}*/ /* end of group TIMER_PWM_Driver */
  706. /*@}*/ /* end of group Standard_Driver */
  707. #ifdef __cplusplus
  708. }
  709. #endif
  710. #endif /* __NU_TIMER_PWM_H__ */