nu_timer_pwm.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /**************************************************************************//**
  2. * @file nu_timer_pwm.h
  3. * @version V1.00
  4. * @brief M480 series Timer PWM Controller(Timer PWM) driver header file
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * @copyright (C) 2016-2020 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 ADC Source Select Constant Definitions */
  49. /*---------------------------------------------------------------------------------------------------------*/
  50. #define TPWM_TRIGGER_ADC_AT_ZERO_POINT (0UL << TIMER_PWMEADCTS_TRGSEL_Pos) /*!< Timer PWM trigger ADC while counter zero point event occurred \hideinitializer */
  51. #define TPWM_TRIGGER_ADC_AT_PERIOD_POINT (1UL << TIMER_PWMEADCTS_TRGSEL_Pos) /*!< Timer PWM trigger ADC while counter period point event occurred \hideinitializer */
  52. #define TPWM_TRIGGER_ADC_AT_ZERO_OR_PERIOD_POINT (2UL << TIMER_PWMEADCTS_TRGSEL_Pos) /*!< Timer PWM trigger ADC while counter zero or period point event occurred \hideinitializer */
  53. #define TPWM_TRIGGER_ADC_AT_COMPARE_UP_COUNT_POINT (3UL << TIMER_PWMEADCTS_TRGSEL_Pos) /*!< Timer PWM trigger ADC while counter up count compare point event occurred \hideinitializer */
  54. #define TPWM_TRIGGER_ADC_AT_COMPARE_DOWN_COUNT_POINT (4UL << TIMER_PWMEADCTS_TRGSEL_Pos) /*!< Timer PWM trigger ADC 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. /*@}*/ /* end of group TIMER_PWM_EXPORTED_CONSTANTS */
  115. /** @addtogroup TIMER_PWM_EXPORTED_FUNCTIONS TIMER PWM Exported Functions
  116. @{
  117. */
  118. /**
  119. * @brief Enable PWM Counter Mode
  120. *
  121. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  122. *
  123. * @return None
  124. *
  125. * @details This macro is used to enable specified Timer channel as PWM counter mode, then timer counter mode is invalid.
  126. * @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.
  127. * \hideinitializer
  128. */
  129. #define TPWM_ENABLE_PWM_MODE(timer) ((timer)->ALTCTL = (1 << TIMER_ALTCTL_FUNCSEL_Pos))
  130. /**
  131. * @brief Disable PWM Counter Mode
  132. *
  133. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  134. *
  135. * @return None
  136. *
  137. * @details This macro is used to disable specified Timer channel as PWM counter mode, then timer counter mode is available.
  138. * @note All registers about PWM counter function will be cleared to 0 after executing this macro.
  139. * \hideinitializer
  140. */
  141. #define TPWM_DISABLE_PWM_MODE(timer) ((timer)->ALTCTL = (0 << TIMER_ALTCTL_FUNCSEL_Pos))
  142. /**
  143. * @brief Enable Independent 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 independent mode of TIMER PWM module and complementary mode will be disabled.
  150. * \hideinitializer
  151. */
  152. #define TPWM_ENABLE_INDEPENDENT_MODE(timer) ((timer)->PWMCTL &= ~(1 << TIMER_PWMCTL_OUTMODE_Pos))
  153. /**
  154. * @brief Enable Complementary Mode
  155. *
  156. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  157. *
  158. * @return None
  159. *
  160. * @details This macro is used to enable complementary mode of Timer PWM module and independent mode will be disabled.
  161. * \hideinitializer
  162. */
  163. #define TPWM_ENABLE_COMPLEMENTARY_MODE(timer) ((timer)->PWMCTL |= (1 << TIMER_PWMCTL_OUTMODE_Pos))
  164. /**
  165. * @brief Set Counter Type
  166. *
  167. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  168. * @param[in] type Timer PWM count type, could be one of the following type
  169. * - \ref TPWM_UP_COUNT
  170. * - \ref TPWM_DOWN_COUNT
  171. * - \ref TPWM_UP_DOWN_COUNT
  172. *
  173. * @return None
  174. *
  175. * @details This macro is used to set Timer PWM counter type.
  176. * \hideinitializer
  177. */
  178. #define TPWM_SET_COUNTER_TYPE(timer, type) ((timer)->PWMCTL = ((timer)->PWMCTL & ~TIMER_PWMCTL_CNTTYPE_Msk) | (type))
  179. /**
  180. * @brief Start PWM Counter
  181. *
  182. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  183. *
  184. * @return None
  185. *
  186. * @details This macro is used to enable PWM generator and start counter counting.
  187. * \hideinitializer
  188. */
  189. #define TPWM_START_COUNTER(timer) ((timer)->PWMCTL |= TIMER_PWMCTL_CNTEN_Msk)
  190. /**
  191. * @brief Stop PWM Counter
  192. *
  193. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  194. *
  195. * @return None
  196. *
  197. * @details This macro is used to stop PWM counter after current period is completed.
  198. * \hideinitializer
  199. */
  200. #define TPWM_STOP_COUNTER(timer) ((timer)->PWMPERIOD = 0x0)
  201. /**
  202. * @brief Set Counter Clock Prescaler
  203. *
  204. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  205. *
  206. * @param[in] prescaler Clock prescaler of specified channel. Valid values are between 0x0~0xFFF.
  207. *
  208. * @return None
  209. *
  210. * @details This macro is used to set the prescaler of specified TIMER PWM.
  211. * @note If prescaler is 0, then there is no scaling in counter clock source.
  212. * \hideinitializer
  213. */
  214. #define TPWM_SET_PRESCALER(timer, prescaler) ((timer)->PWMCLKPSC = (prescaler))
  215. /**
  216. * @brief Get Counter Clock Prescaler
  217. *
  218. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  219. *
  220. * @return Target prescaler setting, CLKPSC (TIMERx_PWMCLKPSC[11:0])
  221. *
  222. * @details Get the prescaler setting, the target counter clock divider is (CLKPSC + 1).
  223. * \hideinitializer
  224. */
  225. #define TPWM_GET_PRESCALER(timer) ((timer)->PWMCLKPSC)
  226. /**
  227. * @brief Set Counter Period
  228. *
  229. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  230. *
  231. * @param[in] period Period of specified channel. Valid values are between 0x0~0xFFFF.
  232. *
  233. * @return None
  234. *
  235. * @details This macro is used to set the period of specified TIMER PWM.
  236. * \hideinitializer
  237. */
  238. #define TPWM_SET_PERIOD(timer, period) ((timer)->PWMPERIOD = (period))
  239. /**
  240. * @brief Get Counter Period
  241. *
  242. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  243. *
  244. * @return Target period setting, PERIOD (TIMERx_PWMPERIOD[15:0])
  245. *
  246. * @details This macro is used to get the period of specified TIMER PWM.
  247. * \hideinitializer
  248. */
  249. #define TPWM_GET_PERIOD(timer) ((timer)->PWMPERIOD)
  250. /**
  251. * @brief Set Comparator Value
  252. *
  253. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  254. *
  255. * @param[in] cmp Comparator of specified channel. Valid values are between 0x0~0xFFFF.
  256. *
  257. * @return None
  258. *
  259. * @details This macro is used to set the comparator value of specified TIMER PWM.
  260. * \hideinitializer
  261. */
  262. #define TPWM_SET_CMPDAT(timer, cmp) ((timer)->PWMCMPDAT = (cmp))
  263. /**
  264. * @brief Get Comparator Value
  265. *
  266. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  267. *
  268. * @return Target comparator setting, CMPDAT (TIMERx_PWMCMPDAT[15:0])
  269. *
  270. * @details This macro is used to get the comparator value of specified TIMER PWM.
  271. * \hideinitializer
  272. */
  273. #define TPWM_GET_CMPDAT(timer) ((timer)->PWMCMPDAT)
  274. /**
  275. * @brief Clear Counter
  276. *
  277. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  278. *
  279. * @return None
  280. *
  281. * @details This macro is used to clear counter of specified TIMER PWM.
  282. * \hideinitializer
  283. */
  284. #define TPWM_CLEAR_COUNTER(timer) ((timer)->PWMCNTCLR = TIMER_PWMCNTCLR_CNTCLR_Msk)
  285. /**
  286. * @brief Software Trigger Brake Event
  287. *
  288. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  289. *
  290. * @param[in] type Type of brake trigger. Valid values are:
  291. * - \ref TPWM_BRAKE_EDGE
  292. * - \ref TPWM_BRAKE_LEVEL
  293. *
  294. * @return None
  295. *
  296. * @details This macro is used to trigger brake event by writing PWMSWBRK register.
  297. * \hideinitializer
  298. */
  299. #define TPWM_SW_TRIGGER_BRAKE(timer, type) ((timer)->PWMSWBRK = (type))
  300. /**
  301. * @brief Enable Output Function
  302. *
  303. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  304. *
  305. * @param[in] ch Enable specified channel output function. Valid values are the combination of:
  306. * - \ref TPWM_CH0
  307. * - \ref TPWM_CH1
  308. *
  309. * @return None
  310. *
  311. * @details This macro is used to enable output function of specified output pins.
  312. * @note If the corresponding bit in ch parameter is 0, then output function will be disabled in this channel.
  313. * \hideinitializer
  314. */
  315. #define TPWM_ENABLE_OUTPUT(timer, ch) ((timer)->PWMPOEN = (ch))
  316. /**
  317. * @brief Set Output Inverse
  318. *
  319. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  320. *
  321. * @param[in] ch Set specified channel output is inversed or not. Valid values are the combination of:
  322. * - \ref TPWM_CH0
  323. * - \ref TPWM_CH1
  324. *
  325. * @return None
  326. *
  327. * @details This macro is used to enable output inverse of specified output pins.
  328. * @note If ch parameter is 0, then output inverse function will be disabled.
  329. * \hideinitializer
  330. */
  331. #define TPWM_SET_OUTPUT_INVERSE(timer, ch) ((timer)->PWMPOLCTL = (ch))
  332. /**
  333. * @brief Enable Output Function
  334. *
  335. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  336. *
  337. * @param[in] ch Enable specified channel output mask function. Valid values are the combination of:
  338. * - \ref TPWM_CH0
  339. * - \ref TPWM_CH1
  340. *
  341. * @param[in] level Output to high or low on specified mask channel.
  342. *
  343. * @return None
  344. *
  345. * @details This macro is used to enable output function of specified output pins.
  346. * @note If ch parameter is 0, then output mask function will be disabled.
  347. * \hideinitializer
  348. */
  349. #define TPWM_SET_MASK_OUTPUT(timer, ch, level) do {(timer)->PWMMSKEN = (ch); (timer)->PWMMSK = (level); }while(0)
  350. /**
  351. * @brief Set Counter Synchronous Mode
  352. *
  353. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  354. *
  355. * @param[in] mode Synchronous mode. Possible options are:
  356. * - \ref TPWM_CNTR_SYNC_DISABLE
  357. * - \ref TPWM_CNTR_SYNC_START_BY_TIMER0
  358. * - \ref TPWM_CNTR_SYNC_CLEAR_BY_TIMER0
  359. * - \ref TPWM_CNTR_SYNC_START_BY_TIMER2
  360. * - \ref TPWM_CNTR_SYNC_CLEAR_BY_TIMER2
  361. *
  362. * @return None
  363. *
  364. * @details This macro is used to set counter synchronous mode of specified Timer PWM module.
  365. * @note Only support all PWM counters are synchronous by TIMER0 PWM or TIMER0~1 PWM counter synchronous by TIMER0 PWM and
  366. * TIMER2~3 PWM counter synchronous by TIMER2 PWM.
  367. * \hideinitializer
  368. */
  369. #define TPWM_SET_COUNTER_SYNC_MODE(timer, mode) ((timer)->PWMSCTL = (mode))
  370. /**
  371. * @brief Trigger Counter Synchronous
  372. *
  373. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  374. *
  375. * @return None
  376. *
  377. * @details This macro is used to trigger synchronous event by specified TIMER PWM.
  378. * @note 1. This macro is only available for TIMER0 PWM and TIMER2 PWM. \n
  379. * 2. STRGEN (PWMSTRG[0]) is write only and always read as 0.
  380. * \hideinitializer
  381. */
  382. #define TPWM_TRIGGER_COUNTER_SYNC(timer) ((timer)->PWMSTRG = TIMER_PWMSTRG_STRGEN_Msk)
  383. /**
  384. * @brief Enable Zero Event Interrupt
  385. *
  386. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  387. *
  388. * @return None
  389. *
  390. * @details This macro is used to enable the zero event interrupt function.
  391. * \hideinitializer
  392. */
  393. #define TPWM_ENABLE_ZERO_INT(timer) ((timer)->PWMINTEN0 |= TIMER_PWMINTEN0_ZIEN_Msk)
  394. /**
  395. * @brief Disable Zero Event Interrupt
  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 disable the zero event interrupt function.
  402. * \hideinitializer
  403. */
  404. #define TPWM_DISABLE_ZERO_INT(timer) ((timer)->PWMINTEN0 &= ~TIMER_PWMINTEN0_ZIEN_Msk)
  405. /**
  406. * @brief Get Zero Event Interrupt Flag
  407. *
  408. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  409. *
  410. * @retval 0 Zero event interrupt did not occur
  411. * @retval 1 Zero event interrupt occurred
  412. *
  413. * @details This macro indicates zero event occurred or not.
  414. * \hideinitializer
  415. */
  416. #define TPWM_GET_ZERO_INT_FLAG(timer) (((timer)->PWMINTSTS0 & TIMER_PWMINTSTS0_ZIF_Msk)? 1 : 0)
  417. /**
  418. * @brief Clear Zero Event Interrupt Flag
  419. *
  420. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  421. *
  422. * @return None
  423. *
  424. * @details This macro clears zero event interrupt flag.
  425. * \hideinitializer
  426. */
  427. #define TPWM_CLEAR_ZERO_INT_FLAG(timer) ((timer)->PWMINTSTS0 = TIMER_PWMINTSTS0_ZIF_Msk)
  428. /**
  429. * @brief Enable Period Event Interrupt
  430. *
  431. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  432. *
  433. * @return None
  434. *
  435. * @details This macro is used to enable the period event interrupt function.
  436. * \hideinitializer
  437. */
  438. #define TPWM_ENABLE_PERIOD_INT(timer) ((timer)->PWMINTEN0 |= TIMER_PWMINTEN0_PIEN_Msk)
  439. /**
  440. * @brief Disable Period Event Interrupt
  441. *
  442. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  443. *
  444. * @return None
  445. *
  446. * @details This macro is used to disable the period event interrupt function.
  447. * \hideinitializer
  448. */
  449. #define TPWM_DISABLE_PERIOD_INT(timer) ((timer)->PWMINTEN0 &= ~TIMER_PWMINTEN0_PIEN_Msk)
  450. /**
  451. * @brief Get Period Event Interrupt Flag
  452. *
  453. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  454. *
  455. * @retval 0 Period event interrupt did not occur
  456. * @retval 1 Period event interrupt occurred
  457. *
  458. * @details This macro indicates period event occurred or not.
  459. * \hideinitializer
  460. */
  461. #define TPWM_GET_PERIOD_INT_FLAG(timer) (((timer)->PWMINTSTS0 & TIMER_PWMINTSTS0_PIF_Msk)? 1 : 0)
  462. /**
  463. * @brief Clear Period Event Interrupt Flag
  464. *
  465. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  466. *
  467. * @return None
  468. *
  469. * @details This macro clears period event interrupt flag.
  470. * \hideinitializer
  471. */
  472. #define TPWM_CLEAR_PERIOD_INT_FLAG(timer) ((timer)->PWMINTSTS0 = TIMER_PWMINTSTS0_PIF_Msk)
  473. /**
  474. * @brief Enable Compare Up Event Interrupt
  475. *
  476. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  477. *
  478. * @return None
  479. *
  480. * @details This macro is used to enable the compare up event interrupt function.
  481. * \hideinitializer
  482. */
  483. #define TPWM_ENABLE_CMP_UP_INT(timer) ((timer)->PWMINTEN0 |= TIMER_PWMINTEN0_CMPUIEN_Msk)
  484. /**
  485. * @brief Disable Compare Up Event Interrupt
  486. *
  487. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  488. *
  489. * @return None
  490. *
  491. * @details This macro is used to disable the compare up event interrupt function.
  492. * \hideinitializer
  493. */
  494. #define TPWM_DISABLE_CMP_UP_INT(timer) ((timer)->PWMINTEN0 &= ~TIMER_PWMINTEN0_CMPUIEN_Msk)
  495. /**
  496. * @brief Get Compare Up Event Interrupt Flag
  497. *
  498. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  499. *
  500. * @retval 0 Compare up event interrupt did not occur
  501. * @retval 1 Compare up event interrupt occurred
  502. *
  503. * @details This macro indicates compare up event occurred or not.
  504. * \hideinitializer
  505. */
  506. #define TPWM_GET_CMP_UP_INT_FLAG(timer) (((timer)->PWMINTSTS0 & TIMER_PWMINTSTS0_CMPUIF_Msk)? 1 : 0)
  507. /**
  508. * @brief Clear Compare Up Event Interrupt Flag
  509. *
  510. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  511. *
  512. * @return None
  513. *
  514. * @details This macro clears compare up event interrupt flag.
  515. * \hideinitializer
  516. */
  517. #define TPWM_CLEAR_CMP_UP_INT_FLAG(timer) ((timer)->PWMINTSTS0 = TIMER_PWMINTSTS0_CMPUIF_Msk)
  518. /**
  519. * @brief Enable Compare Down Event Interrupt
  520. *
  521. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  522. *
  523. * @return None
  524. *
  525. * @details This macro is used to enable the compare down event interrupt function.
  526. * \hideinitializer
  527. */
  528. #define TPWM_ENABLE_CMP_DOWN_INT(timer) ((timer)->PWMINTEN0 |= TIMER_PWMINTEN0_CMPDIEN_Msk)
  529. /**
  530. * @brief Disable Compare Down Event Interrupt
  531. *
  532. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  533. *
  534. * @return None
  535. *
  536. * @details This macro is used to disable the compare down event interrupt function.
  537. * \hideinitializer
  538. */
  539. #define TPWM_DISABLE_CMP_DOWN_INT(timer) ((timer)->PWMINTEN0 &= ~TIMER_PWMINTEN0_CMPDIEN_Msk)
  540. /**
  541. * @brief Get Compare Down Event Interrupt Flag
  542. *
  543. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  544. *
  545. * @retval 0 Compare down event interrupt did not occur
  546. * @retval 1 Compare down event interrupt occurred
  547. *
  548. * @details This macro indicates compare down event occurred or not.
  549. * \hideinitializer
  550. */
  551. #define TPWM_GET_CMP_DOWN_INT_FLAG(timer) (((timer)->PWMINTSTS0 & TIMER_PWMINTSTS0_CMPDIF_Msk)? 1 : 0)
  552. /**
  553. * @brief Clear Compare Down Event Interrupt Flag
  554. *
  555. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  556. *
  557. * @return None
  558. *
  559. * @details This macro clears compare down event interrupt flag.
  560. * \hideinitializer
  561. */
  562. #define TPWM_CLEAR_CMP_DOWN_INT_FLAG(timer) ((timer)->PWMINTSTS0 = TIMER_PWMINTSTS0_CMPDIF_Msk)
  563. /**
  564. * @brief Get Counter Reach Maximum Count Status
  565. *
  566. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  567. *
  568. * @retval 0 Timer PWM counter never counts to maximum value
  569. * @retval 1 Timer PWM counter counts to maximum value, 0xFFFF
  570. *
  571. * @details This macro indicates Timer PWM counter has count to 0xFFFF or not.
  572. * \hideinitializer
  573. */
  574. #define TPWM_GET_REACH_MAX_CNT_STATUS(timer) (((timer)->PWMSTATUS & TIMER_PWMSTATUS_CNTMAXF_Msk)? 1 : 0)
  575. /**
  576. * @brief Clear Counter Reach Maximum Count Status
  577. *
  578. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  579. *
  580. * @return None
  581. *
  582. * @details This macro clears reach maximum count status.
  583. * \hideinitializer
  584. */
  585. #define TPWM_CLEAR_REACH_MAX_CNT_STATUS(timer) ((timer)->PWMSTATUS = TIMER_PWMSTATUS_CNTMAXF_Msk)
  586. /**
  587. * @brief Get Trigger ADC Status
  588. *
  589. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  590. *
  591. * @retval 0 Trigger ADC start conversion is not occur
  592. * @retval 1 Specified counter compare event has trigger ADC start conversion
  593. *
  594. * @details This macro is used to indicate PWM counter compare event has triggered ADC start conversion.
  595. * \hideinitializer
  596. */
  597. #define TPWM_GET_TRG_ADC_STATUS(timer) (((timer)->PWMSTATUS & TIMER_PWMSTATUS_EADCTRGF_Msk)? 1 : 0)
  598. /**
  599. * @brief Clear Trigger ADC Status
  600. *
  601. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  602. *
  603. * @return None
  604. *
  605. * @details This macro is used to clear PWM counter compare event trigger ADC status.
  606. * \hideinitializer
  607. */
  608. #define TPWM_CLEAR_TRG_ADC_STATUS(timer) ((timer)->PWMSTATUS = TIMER_PWMSTATUS_EADCTRGF_Msk)
  609. /**
  610. * @brief Set Brake Event at Brake Pin High or Low-to-High
  611. *
  612. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  613. *
  614. * @return None
  615. *
  616. * @details This macro is used to set detect brake event when external brake pin at high level or transfer from low to high.
  617. * @note The default brake pin detection is high level or from low to high.
  618. * \hideinitializer
  619. */
  620. #define TPWM_SET_BRAKE_PIN_HIGH_DETECT(timer) ((timer)->PWMBNF &= ~TIMER_PWMBNF_BRKPINV_Msk)
  621. /**
  622. * @brief Set Brake Event at Brake Pin Low or High-to-Low
  623. *
  624. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  625. *
  626. * @return None
  627. *
  628. * @details This macro is used to set detect brake event when external brake pin at low level or transfer from high to low.
  629. * \hideinitializer
  630. */
  631. #define TPWM_SET_BRAKE_PIN_LOW_DETECT(timer) ((timer)->PWMBNF |= TIMER_PWMBNF_BRKPINV_Msk)
  632. /**
  633. * @brief Set External Brake Pin Source
  634. *
  635. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  636. * @param[in] pin The external brake pin source, could be one of following source
  637. * - \ref TPWM_TM_BRAKE0
  638. * - \ref TPWM_TM_BRAKE1
  639. * - \ref TPWM_TM_BRAKE2
  640. * - \ref TPWM_TM_BRAKE3
  641. *
  642. * @return None
  643. *
  644. * @details This macro is used to set detect brake event when external brake pin at high level or transfer from low to high.
  645. * \hideinitializer
  646. */
  647. #define TPWM_SET_BRAKE_PIN_SOURCE(timer, pin) ((timer)->PWMBNF = ((timer)->PWMBNF & ~TIMER_PWMBNF_BKPINSRC_Msk) | ((pin)<<TIMER_PWMBNF_BKPINSRC_Pos))
  648. void TPWM_SetCounterClockSource(TIMER_T *timer, uint32_t u32CntClkSrc);
  649. uint32_t TPWM_ConfigOutputFreqAndDuty(TIMER_T *timer, uint32_t u32Frequency, uint32_t u32DutyCycle);
  650. void TPWM_EnableDeadTime(TIMER_T *timer, uint32_t u32DTCount);
  651. void TPWM_EnableDeadTimeWithPrescale(TIMER_T *timer, uint32_t u32DTCount);
  652. void TPWM_DisableDeadTime(TIMER_T *timer);
  653. void TPWM_EnableCounter(TIMER_T *timer);
  654. void TPWM_DisableCounter(TIMER_T *timer);
  655. void TPWM_EnableTriggerADC(TIMER_T *timer, uint32_t u32Condition);
  656. void TPWM_DisableTriggerADC(TIMER_T *timer);
  657. void TPWM_EnableFaultBrake(TIMER_T *timer, uint32_t u32CH0Level, uint32_t u32CH1Level, uint32_t u32BrakeSource);
  658. void TPWM_EnableFaultBrakeInt(TIMER_T *timer, uint32_t u32IntSource);
  659. void TPWM_DisableFaultBrakeInt(TIMER_T *timer, uint32_t u32IntSource);
  660. uint32_t TPWM_GetFaultBrakeIntFlag(TIMER_T *timer, uint32_t u32IntSource);
  661. void TPWM_ClearFaultBrakeIntFlag(TIMER_T *timer, uint32_t u32IntSource);
  662. void TPWM_SetLoadMode(TIMER_T *timer, uint32_t u32LoadMode);
  663. void TPWM_EnableBrakePinDebounce(TIMER_T *timer, uint32_t u32BrakePinSrc, uint32_t u32DebounceCnt, uint32_t u32ClkSrcSel);
  664. void TPWM_DisableBrakePinDebounce(TIMER_T *timer);
  665. void TPWM_EnableBrakePinInverse(TIMER_T *timer);
  666. void TPWM_DisableBrakePinInverse(TIMER_T *timer);
  667. void TPWM_SetBrakePinSource(TIMER_T *timer, uint32_t u32BrakePinNum);
  668. /*@}*/ /* end of group TIMER_PWM_EXPORTED_FUNCTIONS */
  669. /*@}*/ /* end of group TIMER_PWM_Driver */
  670. /*@}*/ /* end of group Standard_Driver */
  671. #ifdef __cplusplus
  672. }
  673. #endif
  674. #endif /* __NU_TIMER_PWM_H__ */