1 /**************************************************************************//**
\r
4 * @brief Timer Controller(Timer) driver header file
\r
6 * @copyright (C) 2017 Nuvoton Technology Corp. All rights reserved.
\r
7 *****************************************************************************/
\r
17 /** @addtogroup Standard_Driver Standard Driver
\r
21 /** @addtogroup TIMER_Driver TIMER Driver
\r
25 /** @addtogroup TIMER_EXPORTED_CONSTANTS TIMER Exported Constants
\r
28 /*---------------------------------------------------------------------------------------------------------*/
\r
29 /* TIMER Operation Mode, External Counter and Capture Mode Constant Definitions */
\r
30 /*---------------------------------------------------------------------------------------------------------*/
\r
31 #define TIMER_ONESHOT_MODE (0UL << TIMER_CTL_OPMODE_Pos) /*!< Timer working in one-shot mode \hideinitializer */
\r
32 #define TIMER_PERIODIC_MODE (1UL << TIMER_CTL_OPMODE_Pos) /*!< Timer working in periodic mode \hideinitializer */
\r
33 #define TIMER_TOGGLE_MODE (2UL << TIMER_CTL_OPMODE_Pos) /*!< Timer working in toggle-output mode \hideinitializer */
\r
34 #define TIMER_CONTINUOUS_MODE (3UL << TIMER_CTL_OPMODE_Pos) /*!< Timer working in continuous counting mode \hideinitializer */
\r
35 #define TIMER_TOUT_PIN_FROM_TMX (0UL << TIMER_CTL_TGLPINSEL_Pos) /*!< Timer toggle-output pin is from TMx pin \hideinitializer */
\r
36 #define TIMER_TOUT_PIN_FROM_TMX_EXT (1UL << TIMER_CTL_TGLPINSEL_Pos) /*!< Timer toggle-output pin is from TMx_EXT pin \hideinitializer */
\r
38 #define TIMER_COUNTER_EVENT_FALLING (0UL << TIMER_EXTCTL_CNTPHASE_Pos) /*!< Counter increase on falling edge detection \hideinitializer */
\r
39 #define TIMER_COUNTER_EVENT_RISING (1UL << TIMER_EXTCTL_CNTPHASE_Pos) /*!< Counter increase on rising edge detection \hideinitializer */
\r
40 #define TIMER_CAPTURE_FREE_COUNTING_MODE (0UL << TIMER_EXTCTL_CAPFUNCS_Pos) /*!< Timer capture event to get timer counter value \hideinitializer */
\r
41 #define TIMER_CAPTURE_COUNTER_RESET_MODE (1UL << TIMER_EXTCTL_CAPFUNCS_Pos) /*!< Timer capture event to reset timer counter \hideinitializer */
\r
43 #define TIMER_CAPTURE_EVENT_FALLING (0UL << TIMER_EXTCTL_CAPEDGE_Pos) /*!< Falling edge detection to trigger capture event \hideinitializer */
\r
44 #define TIMER_CAPTURE_EVENT_RISING (1UL << TIMER_EXTCTL_CAPEDGE_Pos) /*!< Rising edge detection to trigger capture event \hideinitializer */
\r
45 #define TIMER_CAPTURE_EVENT_FALLING_RISING (2UL << TIMER_EXTCTL_CAPEDGE_Pos) /*!< Both falling and rising edge detection to trigger capture event, and first event at falling edge \hideinitializer */
\r
46 #define TIMER_CAPTURE_EVENT_RISING_FALLING (3UL << TIMER_EXTCTL_CAPEDGE_Pos) /*!< Both rising and falling edge detection to trigger capture event, and first event at rising edge \hideinitializer */
\r
47 #define TIMER_CAPTURE_EVENT_GET_LOW_PERIOD (6UL << TIMER_EXTCTL_CAPEDGE_Pos) /*!< First capture event is at falling edge, follows are at at rising edge \hideinitializer */
\r
48 #define TIMER_CAPTURE_EVENT_GET_HIGH_PERIOD (7UL << TIMER_EXTCTL_CAPEDGE_Pos) /*!< First capture event is at rising edge, follows are at at falling edge \hideinitializer */
\r
50 #define TIMER_TRGSRC_TIMEOUT_EVENT (0UL << TIMER_TRGCTL_TRGSSEL_Pos) /*!< Select internal trigger source from timer time-out event \hideinitializer */
\r
51 #define TIMER_TRGSRC_CAPTURE_EVENT (1UL << TIMER_TRGCTL_TRGSSEL_Pos) /*!< Select internal trigger source from timer capture event \hideinitializer */
\r
52 #define TIMER_TRG_TO_EPWM (TIMER_TRGCTL_TRGEPWM_Msk) /*!< Each timer event as EPWM counter clock source \hideinitializer */
\r
53 #define TIMER_TRG_TO_EADC (TIMER_TRGCTL_TRGEADC_Msk) /*!< Each timer event to start ADC conversion \hideinitializer */
\r
54 #define TIMER_TRG_TO_DAC (TIMER_TRGCTL_TRGDAC_Msk) /*!< Each timer event to start DAC conversion \hideinitializer */
\r
55 #define TIMER_TRG_TO_PDMA (TIMER_TRGCTL_TRGPDMA_Msk) /*!< Each timer event to trigger PDMA transfer \hideinitializer */
\r
57 /*@}*/ /* end of group TIMER_EXPORTED_CONSTANTS */
\r
60 /** @addtogroup TIMER_EXPORTED_FUNCTIONS TIMER Exported Functions
\r
65 * @brief Set Timer Compared Value
\r
67 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
68 * @param[in] u32Value Timer compare value. Valid values are between 2 to 0xFFFFFF.
\r
72 * @details This macro is used to set timer compared value to adjust timer time-out interval.
\r
73 * @note 1. Never write 0x0 or 0x1 in this field, or the core will run into unknown state. \n
\r
74 * 2. If update timer compared value in continuous counting mode, timer counter value will keep counting continuously. \n
\r
75 * But if timer is operating at other modes, the timer up counter will restart counting and start from 0.
\r
78 #define TIMER_SET_CMP_VALUE(timer, u32Value) ((timer)->CMP = (u32Value))
\r
81 * @brief Set Timer Prescale Value
\r
83 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
84 * @param[in] u32Value Timer prescale value. Valid values are between 0 to 0xFF.
\r
88 * @details This macro is used to set timer prescale value and timer source clock will be divided by (prescale + 1) \n
\r
89 * before it is fed into timer.
\r
92 #define TIMER_SET_PRESCALE_VALUE(timer, u32Value) ((timer)->CTL = ((timer)->CTL & ~TIMER_CTL_PSC_Msk) | (u32Value))
\r
95 * @brief Check specify Timer Status
\r
97 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
99 * @retval 0 Timer 24-bit up counter is inactive
\r
100 * @retval 1 Timer 24-bit up counter is active
\r
102 * @details This macro is used to check if specify Timer counter is inactive or active.
\r
105 #define TIMER_IS_ACTIVE(timer) (((timer)->CTL & TIMER_CTL_ACTSTS_Msk)? 1 : 0)
\r
108 * @brief Select Toggle-output Pin
\r
110 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
111 * @param[in] u32ToutSel Toggle-output pin selection, valid values are:
\r
112 * - \ref TIMER_TOUT_PIN_FROM_TMX
\r
113 * - \ref TIMER_TOUT_PIN_FROM_TMX_EXT
\r
117 * @details This macro is used to select timer toggle-output pin is output on TMx or TMx_EXT pin.
\r
120 #define TIMER_SELECT_TOUT_PIN(timer, u32ToutSel) ((timer)->CTL = ((timer)->CTL & ~TIMER_CTL_TGLPINSEL_Msk) | (u32ToutSel))
\r
123 /*---------------------------------------------------------------------------------------------------------*/
\r
124 /* static inline functions */
\r
125 /*---------------------------------------------------------------------------------------------------------*/
\r
126 /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
\r
127 __STATIC_INLINE void TIMER_Start(TIMER_T *timer);
\r
128 __STATIC_INLINE void TIMER_Stop(TIMER_T *timer);
\r
129 __STATIC_INLINE void TIMER_EnableWakeup(TIMER_T *timer);
\r
130 __STATIC_INLINE void TIMER_DisableWakeup(TIMER_T *timer);
\r
131 __STATIC_INLINE void TIMER_StartCapture(TIMER_T *timer);
\r
132 __STATIC_INLINE void TIMER_StopCapture(TIMER_T *timer);
\r
133 __STATIC_INLINE void TIMER_EnableCaptureDebounce(TIMER_T *timer);
\r
134 __STATIC_INLINE void TIMER_DisableCaptureDebounce(TIMER_T *timer);
\r
135 __STATIC_INLINE void TIMER_EnableEventCounterDebounce(TIMER_T *timer);
\r
136 __STATIC_INLINE void TIMER_DisableEventCounterDebounce(TIMER_T *timer);
\r
137 __STATIC_INLINE void TIMER_EnableInt(TIMER_T *timer);
\r
138 __STATIC_INLINE void TIMER_DisableInt(TIMER_T *timer);
\r
139 __STATIC_INLINE void TIMER_EnableCaptureInt(TIMER_T *timer);
\r
140 __STATIC_INLINE void TIMER_DisableCaptureInt(TIMER_T *timer);
\r
141 __STATIC_INLINE uint32_t TIMER_GetIntFlag(TIMER_T *timer);
\r
142 __STATIC_INLINE void TIMER_ClearIntFlag(TIMER_T *timer);
\r
143 __STATIC_INLINE uint32_t TIMER_GetCaptureIntFlag(TIMER_T *timer);
\r
144 __STATIC_INLINE void TIMER_ClearCaptureIntFlag(TIMER_T *timer);
\r
145 __STATIC_INLINE uint32_t TIMER_GetWakeupFlag(TIMER_T *timer);
\r
146 __STATIC_INLINE void TIMER_ClearWakeupFlag(TIMER_T *timer);
\r
147 __STATIC_INLINE uint32_t TIMER_GetCaptureData(TIMER_T *timer);
\r
148 __STATIC_INLINE uint32_t TIMER_GetCounter(TIMER_T *timer);
\r
149 __STATIC_INLINE void TIMER_ResetCounter(TIMER_T *timer);
\r
153 * @brief Start Timer Counting
\r
155 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
159 * @details This function is used to start Timer counting.
\r
161 __STATIC_INLINE void TIMER_Start(TIMER_T *timer)
\r
163 timer->CTL |= TIMER_CTL_CNTEN_Msk;
\r
167 * @brief Stop Timer Counting
\r
169 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
173 * @details This function is used to stop/suspend Timer counting.
\r
175 __STATIC_INLINE void TIMER_Stop(TIMER_T *timer)
\r
177 timer->CTL &= ~TIMER_CTL_CNTEN_Msk;
\r
181 * @brief Enable Timer Interrupt Wake-up Function
\r
183 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
187 * @details This function is used to enable the timer interrupt wake-up function and interrupt source could be time-out interrupt, \n
\r
188 * counter event interrupt or capture trigger interrupt.
\r
189 * @note To wake the system from Power-down mode, timer clock source must be ether LXT or LIRC.
\r
191 __STATIC_INLINE void TIMER_EnableWakeup(TIMER_T *timer)
\r
193 timer->CTL |= TIMER_CTL_WKEN_Msk;
\r
197 * @brief Disable Timer Wake-up Function
\r
199 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
203 * @details This function is used to disable the timer interrupt wake-up function.
\r
205 __STATIC_INLINE void TIMER_DisableWakeup(TIMER_T *timer)
\r
207 timer->CTL &= ~TIMER_CTL_WKEN_Msk;
\r
211 * @brief Start Timer Capture Function
\r
213 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
217 * @details This function is used to start Timer capture function.
\r
219 __STATIC_INLINE void TIMER_StartCapture(TIMER_T *timer)
\r
221 timer->EXTCTL |= TIMER_EXTCTL_CAPEN_Msk;
\r
225 * @brief Stop Timer Capture Function
\r
227 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
231 * @details This function is used to stop Timer capture function.
\r
233 __STATIC_INLINE void TIMER_StopCapture(TIMER_T *timer)
\r
235 timer->EXTCTL &= ~TIMER_EXTCTL_CAPEN_Msk;
\r
239 * @brief Enable Capture Pin De-bounce
\r
241 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
245 * @details This function is used to enable the detect de-bounce function of capture pin.
\r
247 __STATIC_INLINE void TIMER_EnableCaptureDebounce(TIMER_T *timer)
\r
249 timer->EXTCTL |= TIMER_EXTCTL_CAPDBEN_Msk;
\r
253 * @brief Disable Capture Pin De-bounce
\r
255 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
259 * @details This function is used to disable the detect de-bounce function of capture pin.
\r
261 __STATIC_INLINE void TIMER_DisableCaptureDebounce(TIMER_T *timer)
\r
263 timer->EXTCTL &= ~TIMER_EXTCTL_CAPDBEN_Msk;
\r
267 * @brief Enable Counter Pin De-bounce
\r
269 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
273 * @details This function is used to enable the detect de-bounce function of counter pin.
\r
275 __STATIC_INLINE void TIMER_EnableEventCounterDebounce(TIMER_T *timer)
\r
277 timer->EXTCTL |= TIMER_EXTCTL_CNTDBEN_Msk;
\r
281 * @brief Disable Counter Pin De-bounce
\r
283 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
287 * @details This function is used to disable the detect de-bounce function of counter pin.
\r
289 __STATIC_INLINE void TIMER_DisableEventCounterDebounce(TIMER_T *timer)
\r
291 timer->EXTCTL &= ~TIMER_EXTCTL_CNTDBEN_Msk;
\r
295 * @brief Enable Timer Time-out Interrupt
\r
297 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
301 * @details This function is used to enable the timer time-out interrupt function.
\r
303 __STATIC_INLINE void TIMER_EnableInt(TIMER_T *timer)
\r
305 timer->CTL |= TIMER_CTL_INTEN_Msk;
\r
309 * @brief Disable Timer Time-out Interrupt
\r
311 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
315 * @details This function is used to disable the timer time-out interrupt function.
\r
317 __STATIC_INLINE void TIMER_DisableInt(TIMER_T *timer)
\r
319 timer->CTL &= ~TIMER_CTL_INTEN_Msk;
\r
323 * @brief Enable Capture Trigger Interrupt
\r
325 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
329 * @details This function is used to enable the timer capture trigger interrupt function.
\r
331 __STATIC_INLINE void TIMER_EnableCaptureInt(TIMER_T *timer)
\r
333 timer->EXTCTL |= TIMER_EXTCTL_CAPIEN_Msk;
\r
337 * @brief Disable Capture Trigger Interrupt
\r
339 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
343 * @details This function is used to disable the timer capture trigger interrupt function.
\r
345 __STATIC_INLINE void TIMER_DisableCaptureInt(TIMER_T *timer)
\r
347 timer->EXTCTL &= ~TIMER_EXTCTL_CAPIEN_Msk;
\r
351 * @brief Get Timer Time-out Interrupt Flag
\r
353 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
355 * @retval 0 Timer time-out interrupt did not occur
\r
356 * @retval 1 Timer time-out interrupt occurred
\r
358 * @details This function indicates timer time-out interrupt occurred or not.
\r
360 __STATIC_INLINE uint32_t TIMER_GetIntFlag(TIMER_T *timer)
\r
362 return ((timer->INTSTS & TIMER_INTSTS_TIF_Msk) ? 1UL : 0UL);
\r
366 * @brief Clear Timer Time-out Interrupt Flag
\r
368 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
372 * @details This function clears timer time-out interrupt flag to 0.
\r
374 __STATIC_INLINE void TIMER_ClearIntFlag(TIMER_T *timer)
\r
376 timer->INTSTS = TIMER_INTSTS_TIF_Msk;
\r
380 * @brief Get Timer Capture Interrupt Flag
\r
382 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
384 * @retval 0 Timer capture interrupt did not occur
\r
385 * @retval 1 Timer capture interrupt occurred
\r
387 * @details This function indicates timer capture trigger interrupt occurred or not.
\r
389 __STATIC_INLINE uint32_t TIMER_GetCaptureIntFlag(TIMER_T *timer)
\r
391 return timer->EINTSTS;
\r
395 * @brief Clear Timer Capture Interrupt Flag
\r
397 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
401 * @details This function clears timer capture trigger interrupt flag to 0.
\r
403 __STATIC_INLINE void TIMER_ClearCaptureIntFlag(TIMER_T *timer)
\r
405 timer->EINTSTS = TIMER_EINTSTS_CAPIF_Msk;
\r
409 * @brief Get Timer Wake-up Flag
\r
411 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
413 * @retval 0 Timer does not cause CPU wake-up
\r
414 * @retval 1 Timer interrupt event cause CPU wake-up
\r
416 * @details This function indicates timer interrupt event has waked up system or not.
\r
418 __STATIC_INLINE uint32_t TIMER_GetWakeupFlag(TIMER_T *timer)
\r
420 return (timer->INTSTS & TIMER_INTSTS_TWKF_Msk ? 1UL : 0UL);
\r
424 * @brief Clear Timer Wake-up Flag
\r
426 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
430 * @details This function clears the timer wake-up system flag to 0.
\r
432 __STATIC_INLINE void TIMER_ClearWakeupFlag(TIMER_T *timer)
\r
434 timer->INTSTS = TIMER_INTSTS_TWKF_Msk;
\r
438 * @brief Get Capture value
\r
440 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
442 * @return 24-bit Capture Value
\r
444 * @details This function reports the current 24-bit timer capture value.
\r
446 __STATIC_INLINE uint32_t TIMER_GetCaptureData(TIMER_T *timer)
\r
452 * @brief Get Counter value
\r
454 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
456 * @return 24-bit Counter Value
\r
458 * @details This function reports the current 24-bit timer counter value.
\r
460 __STATIC_INLINE uint32_t TIMER_GetCounter(TIMER_T *timer)
\r
466 * @brief Reset Counter
\r
468 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
472 * @details This function is used to reset current counter value and internal prescale counter value.
\r
474 __STATIC_INLINE void TIMER_ResetCounter(TIMER_T *timer)
\r
477 while((timer->CNT & TIMER_CNT_RSTACT_Msk) == TIMER_CNT_RSTACT_Msk) {}
\r
481 uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq);
\r
482 void TIMER_Close(TIMER_T *timer);
\r
483 void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec);
\r
484 void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge);
\r
485 void TIMER_DisableCapture(TIMER_T *timer);
\r
486 void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge);
\r
487 void TIMER_DisableEventCounter(TIMER_T *timer);
\r
488 uint32_t TIMER_GetModuleClock(TIMER_T *timer);
\r
489 void TIMER_EnableFreqCounter(TIMER_T *timer, uint32_t u32DropCount, uint32_t u32Timeout, uint32_t u32EnableInt);
\r
490 void TIMER_DisableFreqCounter(TIMER_T *timer);
\r
491 void TIMER_SetTriggerSource(TIMER_T *timer, uint32_t u32Src);
\r
492 void TIMER_SetTriggerTarget(TIMER_T *timer, uint32_t u32Mask);
\r
494 /*@}*/ /* end of group TIMER_EXPORTED_FUNCTIONS */
\r
496 /*@}*/ /* end of group TIMER_Driver */
\r
498 /*@}*/ /* end of group Standard_Driver */
\r
504 #endif /* __TIMER_H__ */
\r
506 /*** (C) COPYRIGHT 2017 Nuvoton Technology Corp. ***/
\r