1 /**************************************************************************//**
2  * @file     timer.h
3  * @version  V3.00
4  * @brief    Timer Controller(Timer) driver header file
5  *
6  * @copyright SPDX-License-Identifier: Apache-2.0
7  * @copyright Copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
8  *****************************************************************************/
9 #ifndef __TIMER_H__
10 #define __TIMER_H__
11 
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16 
17 
18 /** @addtogroup Standard_Driver Standard Driver
19   @{
20 */
21 
22 /** @addtogroup TIMER_Driver TIMER Driver
23   @{
24 */
25 
26 /** @addtogroup TIMER_EXPORTED_CONSTANTS TIMER Exported Constants
27   @{
28 */
29 /*---------------------------------------------------------------------------------------------------------*/
30 /*  TIMER Operation Mode, External Counter and Capture Mode Constant Definitions                           */
31 /*---------------------------------------------------------------------------------------------------------*/
32 #define TIMER_ONESHOT_MODE                      (0UL << TIMER_CTL_OPMODE_Pos)      /*!< Timer working in one-shot mode \hideinitializer */
33 #define TIMER_PERIODIC_MODE                     (1UL << TIMER_CTL_OPMODE_Pos)      /*!< Timer working in periodic mode \hideinitializer */
34 #define TIMER_TOGGLE_MODE                       (2UL << TIMER_CTL_OPMODE_Pos)      /*!< Timer working in toggle-output mode \hideinitializer */
35 #define TIMER_CONTINUOUS_MODE                   (3UL << TIMER_CTL_OPMODE_Pos)      /*!< Timer working in continuous counting mode \hideinitializer */
36 #define TIMER_TOUT_PIN_FROM_TMX                 (0UL << TIMER_CTL_TGLPINSEL_Pos)   /*!< Timer toggle-output pin is from TMx pin \hideinitializer */
37 #define TIMER_TOUT_PIN_FROM_TMX_EXT             (1UL << TIMER_CTL_TGLPINSEL_Pos)   /*!< Timer toggle-output pin is from TMx_EXT pin \hideinitializer */
38 
39 #define TIMER_COUNTER_EVENT_FALLING             (0UL << TIMER_EXTCTL_CNTPHASE_Pos) /*!< Counter increase on falling edge detection \hideinitializer */
40 #define TIMER_COUNTER_EVENT_RISING              (1UL << TIMER_EXTCTL_CNTPHASE_Pos) /*!< Counter increase on rising edge detection \hideinitializer */
41 #define TIMER_CAPTURE_FREE_COUNTING_MODE        (0UL << TIMER_EXTCTL_CAPFUNCS_Pos) /*!< Timer capture event to get timer counter value \hideinitializer */
42 #define TIMER_CAPTURE_COUNTER_RESET_MODE        (1UL << TIMER_EXTCTL_CAPFUNCS_Pos) /*!< Timer capture event to reset timer counter \hideinitializer */
43 
44 #define TIMER_CAPTURE_EVENT_FALLING             (0UL << TIMER_EXTCTL_CAPEDGE_Pos)  /*!< Falling edge detection to trigger capture event \hideinitializer */
45 #define TIMER_CAPTURE_EVENT_RISING              (1UL << TIMER_EXTCTL_CAPEDGE_Pos)  /*!< Rising edge detection to trigger capture event \hideinitializer */
46 #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 */
47 #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 */
48 #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 */
49 #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 */
50 
51 #define TIMER_CAPTURE_SOURCE_FROM_PIN           (0UL << TIMER_CTL_CAPSRC_Pos) /*!< The capture source is from TMx_EXT pin \hideinitializer */
52 #define TIMER_CAPTURE_SOURCE_FROM_INTERNAL      (1UL << TIMER_CTL_CAPSRC_Pos) /*!< The capture source is from internal ACMPx signal or clock source \hideinitializer */
53 
54 #define TIMER_CAPTURE_SOURCE_DIV_1              (0UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 1 \hideinitializer */
55 #define TIMER_CAPTURE_SOURCE_DIV_2              (1UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 2 \hideinitializer */
56 #define TIMER_CAPTURE_SOURCE_DIV_4              (2UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 4 \hideinitializer */
57 #define TIMER_CAPTURE_SOURCE_DIV_8              (3UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 8 \hideinitializer */
58 #define TIMER_CAPTURE_SOURCE_DIV_16             (4UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 16 \hideinitializer */
59 #define TIMER_CAPTURE_SOURCE_DIV_32             (5UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 32 \hideinitializer */
60 #define TIMER_CAPTURE_SOURCE_DIV_64             (6UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 64 \hideinitializer */
61 #define TIMER_CAPTURE_SOURCE_DIV_128            (7UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 128 \hideinitializer */
62 #define TIMER_CAPTURE_SOURCE_DIV_256            (8UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 256 \hideinitializer */
63 
64 #define TIMER_INTER_CAPTURE_SOURCE_ACMP0        (0UL << TIMER_EXTCTL_INTERCAPSEL_Pos) /*!< Capture source from internal ACMP0 output signal \hideinitializer */
65 #define TIMER_INTER_CAPTURE_SOURCE_ACMP1        (1UL << TIMER_EXTCTL_INTERCAPSEL_Pos) /*!< Capture source from internal ACMP1 output signal \hideinitializer */
66 #define TIMER_INTER_CAPTURE_SOURCE_HXT          (2UL << TIMER_EXTCTL_INTERCAPSEL_Pos) /*!< Capture source from HXT \hideinitializer */
67 #define TIMER_INTER_CAPTURE_SOURCE_LXT          (3UL << TIMER_EXTCTL_INTERCAPSEL_Pos) /*!< Capture source from LXT \hideinitializer */
68 #define TIMER_INTER_CAPTURE_SOURCE_HIRC         (4UL << TIMER_EXTCTL_INTERCAPSEL_Pos) /*!< Capture source from HIRC \hideinitializer */
69 #define TIMER_INTER_CAPTURE_SOURCE_LIRC         (5UL << TIMER_EXTCTL_INTERCAPSEL_Pos) /*!< Capture source from LIRC \hideinitializer */
70 #define TIMER_INTER_CAPTURE_SOURCE_MIRC         (6UL << TIMER_EXTCTL_INTERCAPSEL_Pos) /*!< Capture source from MIRC. Only available on TIMER4 and TIMER5 \hideinitializer */
71 
72 #define TIMER_TRGSRC_TIMEOUT_EVENT              (0UL << TIMER_TRGCTL_TRGSSEL_Pos) /*!< Select internal trigger source from timer time-out event \hideinitializer */
73 #define TIMER_TRGSRC_CAPTURE_EVENT              (1UL << TIMER_TRGCTL_TRGSSEL_Pos) /*!< Select internal trigger source from timer capture event \hideinitializer */
74 #define TIMER_TRG_TO_PWM                        (TIMER_TRGCTL_TRGPWM_Msk)         /*!< Each timer event as EPWM and BPWM counter clock source. NOT supported on TIMER4 and TIMER5 \hideinitializer */
75 #define TIMER_TRG_TO_EADC                       (TIMER_TRGCTL_TRGEADC_Msk)        /*!< Each timer event to start ADC conversion \hideinitializer */
76 #define TIMER_TRG_TO_DAC                        (TIMER_TRGCTL_TRGDAC_Msk)         /*!< Each timer event to start DAC conversion. NOT supported on TIMER4 and TIMER5 \hideinitializer */
77 #define TIMER_TRG_TO_PDMA                       (TIMER_TRGCTL_TRGPDMA_Msk)        /*!< Each timer event to trigger PDMA transfer \hideinitializer */
78 
79 /**@}*/ /* end of group TIMER_EXPORTED_CONSTANTS */
80 
81 
82 /** @addtogroup TIMER_EXPORTED_FUNCTIONS TIMER Exported Functions
83   @{
84 */
85 
86 /**
87   * @brief      Set Timer Compared Value
88   *
89   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
90   * @param[in]  u32Value    Timer compare value. Valid values are between 2 to 0xFFFFFF.
91   *
92   * @return     None
93   *
94   * @details    This macro is used to set timer compared value to adjust timer time-out interval.
95   * @note       1. Never write 0x0 or 0x1 in this field, or the core will run into unknown state. \n
96   *             2. If update timer compared value in continuous counting mode, timer counter value will keep counting continuously. \n
97   *                But if timer is operating at other modes, the timer up counter will restart counting and start from 0.
98   * \hideinitializer
99   */
100 #define TIMER_SET_CMP_VALUE(timer, u32Value)        ((timer)->CMP = (u32Value))
101 
102 /**
103   * @brief      Set Timer Prescale Value
104   *
105   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
106   * @param[in]  u32Value    Timer prescale value. Valid values are between 0 to 0xFF.
107   *
108   * @return     None
109   *
110   * @details    This macro is used to set timer prescale value and timer source clock will be divided by (prescale + 1) \n
111   *             before it is fed into timer.
112   * \hideinitializer
113   */
114 #define TIMER_SET_PRESCALE_VALUE(timer, u32Value)   ((timer)->CTL = ((timer)->CTL & ~TIMER_CTL_PSC_Msk) | (u32Value))
115 
116 /**
117   * @brief      Check specify Timer Status
118   *
119   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
120   *
121   * @retval     0   Timer 24-bit up counter is inactive
122   * @retval     1   Timer 24-bit up counter is active
123   *
124   * @details    This macro is used to check if specify Timer counter is inactive or active.
125   * \hideinitializer
126   */
127 #define TIMER_IS_ACTIVE(timer)                      ((((timer)->CTL & TIMER_CTL_ACTSTS_Msk) == TIMER_CTL_ACTSTS_Msk)? 1 : 0)
128 
129 /**
130   * @brief      Select Toggle-output Pin
131   *
132   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
133   * @param[in]  u32ToutSel  Toggle-output pin selection, valid values are:
134   *                         - \ref TIMER_TOUT_PIN_FROM_TMX
135   *                         - \ref TIMER_TOUT_PIN_FROM_TMX_EXT
136   *
137   * @return     None
138   *
139   * @details    This macro is used to select timer toggle-output pin is output on TMx or TMx_EXT pin.
140   * \hideinitializer
141   */
142 #define TIMER_SELECT_TOUT_PIN(timer, u32ToutSel)    ((timer)->CTL = ((timer)->CTL & ~TIMER_CTL_TGLPINSEL_Msk) | (u32ToutSel))
143 
144 /**
145   * @brief      Set Timer Operating Mode
146   *
147   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
148   * @param[in]  u32OpMode   Operation mode. Possible options are
149   *                         - \ref TIMER_ONESHOT_MODE
150   *                         - \ref TIMER_PERIODIC_MODE
151   *                         - \ref TIMER_TOGGLE_MODE
152   *                         - \ref TIMER_CONTINUOUS_MODE
153   *
154   * @return     None
155   * \hideinitializer
156   */
157 #define TIMER_SET_OPMODE(timer, u32OpMode)   ((timer)->CTL = ((timer)->CTL & ~TIMER_CTL_OPMODE_Msk) | (u32OpMode))
158 
159 
160 /*---------------------------------------------------------------------------------------------------------*/
161 /* static inline functions                                                                                 */
162 /*---------------------------------------------------------------------------------------------------------*/
163 /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
164 __STATIC_INLINE void TIMER_Start(TIMER_T *timer);
165 __STATIC_INLINE void TIMER_Stop(TIMER_T *timer);
166 __STATIC_INLINE void TIMER_EnableWakeup(TIMER_T *timer);
167 __STATIC_INLINE void TIMER_DisableWakeup(TIMER_T *timer);
168 __STATIC_INLINE void TIMER_StartCapture(TIMER_T *timer);
169 __STATIC_INLINE void TIMER_StopCapture(TIMER_T *timer);
170 __STATIC_INLINE void TIMER_EnableCaptureDebounce(TIMER_T *timer);
171 __STATIC_INLINE void TIMER_DisableCaptureDebounce(TIMER_T *timer);
172 __STATIC_INLINE void TIMER_EnableEventCounterDebounce(TIMER_T *timer);
173 __STATIC_INLINE void TIMER_DisableEventCounterDebounce(TIMER_T *timer);
174 __STATIC_INLINE void TIMER_EnableInt(TIMER_T *timer);
175 __STATIC_INLINE void TIMER_DisableInt(TIMER_T *timer);
176 __STATIC_INLINE void TIMER_EnableCaptureInt(TIMER_T *timer);
177 __STATIC_INLINE void TIMER_DisableCaptureInt(TIMER_T *timer);
178 __STATIC_INLINE uint32_t TIMER_GetIntFlag(TIMER_T *timer);
179 __STATIC_INLINE void TIMER_ClearIntFlag(TIMER_T *timer);
180 __STATIC_INLINE uint32_t TIMER_GetCaptureIntFlag(TIMER_T *timer);
181 __STATIC_INLINE void TIMER_ClearCaptureIntFlag(TIMER_T *timer);
182 __STATIC_INLINE uint32_t TIMER_GetWakeupFlag(TIMER_T *timer);
183 __STATIC_INLINE void TIMER_ClearWakeupFlag(TIMER_T *timer);
184 __STATIC_INLINE uint32_t TIMER_GetCaptureData(TIMER_T *timer);
185 __STATIC_INLINE uint32_t TIMER_GetCounter(TIMER_T *timer);
186 __STATIC_INLINE void TIMER_ResetCounter(TIMER_T *timer);
187 
188 
189 /**
190   * @brief      Start Timer Counting
191   *
192   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
193   *
194   * @return     None
195   *
196   * @details    This function is used to start Timer counting.
197   */
TIMER_Start(TIMER_T * timer)198 __STATIC_INLINE void TIMER_Start(TIMER_T *timer)
199 {
200     timer->CTL |= TIMER_CTL_CNTEN_Msk;
201 }
202 
203 /**
204   * @brief      Stop Timer Counting
205   *
206   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
207   *
208   * @return     None
209   *
210   * @details    This function is used to stop/suspend Timer counting.
211   */
TIMER_Stop(TIMER_T * timer)212 __STATIC_INLINE void TIMER_Stop(TIMER_T *timer)
213 {
214     timer->CTL &= ~TIMER_CTL_CNTEN_Msk;
215 }
216 
217 /**
218   * @brief      Enable Timer Interrupt Wake-up Function
219   *
220   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
221   *
222   * @return     None
223   *
224   * @details    This function is used to enable the timer interrupt wake-up function and interrupt source could be time-out interrupt, \n
225   *             counter event interrupt or capture trigger interrupt.
226   * @note       To wake the system from Power-down mode, timer clock source must be ether LXT or LIRC.
227   */
TIMER_EnableWakeup(TIMER_T * timer)228 __STATIC_INLINE void TIMER_EnableWakeup(TIMER_T *timer)
229 {
230     timer->CTL |= TIMER_CTL_WKEN_Msk;
231 }
232 
233 /**
234   * @brief      Disable Timer Wake-up Function
235   *
236   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
237   *
238   * @return     None
239   *
240   * @details    This function is used to disable the timer interrupt wake-up function.
241   */
TIMER_DisableWakeup(TIMER_T * timer)242 __STATIC_INLINE void TIMER_DisableWakeup(TIMER_T *timer)
243 {
244     timer->CTL &= ~TIMER_CTL_WKEN_Msk;
245 }
246 
247 /**
248   * @brief      Start Timer Capture Function
249   *
250   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
251   *
252   * @return     None
253   *
254   * @details    This function is used to start Timer capture function.
255   */
TIMER_StartCapture(TIMER_T * timer)256 __STATIC_INLINE void TIMER_StartCapture(TIMER_T *timer)
257 {
258     timer->EXTCTL |= TIMER_EXTCTL_CAPEN_Msk;
259 }
260 
261 /**
262   * @brief      Stop Timer Capture Function
263   *
264   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
265   *
266   * @return     None
267   *
268   * @details    This function is used to stop Timer capture function.
269   */
TIMER_StopCapture(TIMER_T * timer)270 __STATIC_INLINE void TIMER_StopCapture(TIMER_T *timer)
271 {
272     timer->EXTCTL &= ~TIMER_EXTCTL_CAPEN_Msk;
273 }
274 
275 /**
276   * @brief      Enable Capture Pin De-bounce
277   *
278   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
279   *
280   * @return     None
281   *
282   * @details    This function is used to enable the detect de-bounce function of capture pin.
283   */
TIMER_EnableCaptureDebounce(TIMER_T * timer)284 __STATIC_INLINE void TIMER_EnableCaptureDebounce(TIMER_T *timer)
285 {
286     timer->EXTCTL |= TIMER_EXTCTL_CAPDBEN_Msk;
287 }
288 
289 /**
290   * @brief      Disable Capture Pin De-bounce
291   *
292   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
293   *
294   * @return     None
295   *
296   * @details    This function is used to disable the detect de-bounce function of capture pin.
297   */
TIMER_DisableCaptureDebounce(TIMER_T * timer)298 __STATIC_INLINE void TIMER_DisableCaptureDebounce(TIMER_T *timer)
299 {
300     timer->EXTCTL &= ~TIMER_EXTCTL_CAPDBEN_Msk;
301 }
302 
303 /**
304   * @brief      Enable Counter Pin De-bounce
305   *
306   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
307   *
308   * @return     None
309   *
310   * @details    This function is used to enable the detect de-bounce function of counter pin.
311   */
TIMER_EnableEventCounterDebounce(TIMER_T * timer)312 __STATIC_INLINE void TIMER_EnableEventCounterDebounce(TIMER_T *timer)
313 {
314     timer->EXTCTL |= TIMER_EXTCTL_CNTDBEN_Msk;
315 }
316 
317 /**
318   * @brief      Disable Counter Pin De-bounce
319   *
320   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
321   *
322   * @return     None
323   *
324   * @details    This function is used to disable the detect de-bounce function of counter pin.
325   */
TIMER_DisableEventCounterDebounce(TIMER_T * timer)326 __STATIC_INLINE void TIMER_DisableEventCounterDebounce(TIMER_T *timer)
327 {
328     timer->EXTCTL &= ~TIMER_EXTCTL_CNTDBEN_Msk;
329 }
330 
331 /**
332   * @brief      Enable Timer Time-out Interrupt
333   *
334   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
335   *
336   * @return     None
337   *
338   * @details    This function is used to enable the timer time-out interrupt function.
339   */
TIMER_EnableInt(TIMER_T * timer)340 __STATIC_INLINE void TIMER_EnableInt(TIMER_T *timer)
341 {
342     timer->CTL |= TIMER_CTL_INTEN_Msk;
343 }
344 
345 /**
346   * @brief      Disable Timer Time-out Interrupt
347   *
348   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
349   *
350   * @return     None
351   *
352   * @details    This function is used to disable the timer time-out interrupt function.
353   */
TIMER_DisableInt(TIMER_T * timer)354 __STATIC_INLINE void TIMER_DisableInt(TIMER_T *timer)
355 {
356     timer->CTL &= ~TIMER_CTL_INTEN_Msk;
357 }
358 
359 /**
360   * @brief      Enable Capture Trigger Interrupt
361   *
362   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
363   *
364   * @return     None
365   *
366   * @details    This function is used to enable the timer capture trigger interrupt function.
367   */
TIMER_EnableCaptureInt(TIMER_T * timer)368 __STATIC_INLINE void TIMER_EnableCaptureInt(TIMER_T *timer)
369 {
370     timer->EXTCTL |= TIMER_EXTCTL_CAPIEN_Msk;
371 }
372 
373 /**
374   * @brief      Disable Capture Trigger Interrupt
375   *
376   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
377   *
378   * @return     None
379   *
380   * @details    This function is used to disable the timer capture trigger interrupt function.
381   */
TIMER_DisableCaptureInt(TIMER_T * timer)382 __STATIC_INLINE void TIMER_DisableCaptureInt(TIMER_T *timer)
383 {
384     timer->EXTCTL &= ~TIMER_EXTCTL_CAPIEN_Msk;
385 }
386 
387 /**
388   * @brief      Get Timer Time-out Interrupt Flag
389   *
390   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
391   *
392   * @retval     0   Timer time-out interrupt did not occur
393   * @retval     1   Timer time-out interrupt occurred
394   *
395   * @details    This function indicates timer time-out interrupt occurred or not.
396   */
TIMER_GetIntFlag(TIMER_T * timer)397 __STATIC_INLINE uint32_t TIMER_GetIntFlag(TIMER_T *timer)
398 {
399     return (((timer->INTSTS & TIMER_INTSTS_TIF_Msk) == TIMER_INTSTS_TIF_Msk) ? 1UL : 0UL);
400 }
401 
402 /**
403   * @brief      Clear Timer Time-out Interrupt Flag
404   *
405   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
406   *
407   * @return     None
408   *
409   * @details    This function clears timer time-out interrupt flag to 0.
410   */
TIMER_ClearIntFlag(TIMER_T * timer)411 __STATIC_INLINE void TIMER_ClearIntFlag(TIMER_T *timer)
412 {
413     timer->INTSTS = TIMER_INTSTS_TIF_Msk;
414 }
415 
416 /**
417   * @brief      Get Timer Capture Interrupt Flag
418   *
419   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
420   *
421   * @retval     0   Timer capture interrupt did not occur
422   * @retval     1   Timer capture interrupt occurred
423   *
424   * @details    This function indicates timer capture trigger interrupt occurred or not.
425   */
TIMER_GetCaptureIntFlag(TIMER_T * timer)426 __STATIC_INLINE uint32_t TIMER_GetCaptureIntFlag(TIMER_T *timer)
427 {
428     return timer->EINTSTS;
429 }
430 
431 /**
432   * @brief      Clear Timer Capture Interrupt Flag
433   *
434   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
435   *
436   * @return     None
437   *
438   * @details    This function clears timer capture trigger interrupt flag to 0.
439   */
TIMER_ClearCaptureIntFlag(TIMER_T * timer)440 __STATIC_INLINE void TIMER_ClearCaptureIntFlag(TIMER_T *timer)
441 {
442     timer->EINTSTS = TIMER_EINTSTS_CAPIF_Msk;
443 }
444 
445 /**
446   * @brief      Get Timer Wake-up Flag
447   *
448   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
449   *
450   * @retval     0   Timer does not cause CPU wake-up
451   * @retval     1   Timer interrupt event cause CPU wake-up
452   *
453   * @details    This function indicates timer interrupt event has waked up system or not.
454   */
TIMER_GetWakeupFlag(TIMER_T * timer)455 __STATIC_INLINE uint32_t TIMER_GetWakeupFlag(TIMER_T *timer)
456 {
457     return (((timer->INTSTS & TIMER_INTSTS_TWKF_Msk) == TIMER_INTSTS_TWKF_Msk) ? 1UL : 0UL);
458 }
459 
460 /**
461   * @brief      Clear Timer Wake-up Flag
462   *
463   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
464   *
465   * @return     None
466   *
467   * @details    This function clears the timer wake-up system flag to 0.
468   */
TIMER_ClearWakeupFlag(TIMER_T * timer)469 __STATIC_INLINE void TIMER_ClearWakeupFlag(TIMER_T *timer)
470 {
471     timer->INTSTS = TIMER_INTSTS_TWKF_Msk;
472 }
473 
474 /**
475   * @brief      Get Capture value
476   *
477   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
478   *
479   * @return     24-bit Capture Value
480   *
481   * @details    This function reports the current 24-bit timer capture value.
482   */
TIMER_GetCaptureData(TIMER_T * timer)483 __STATIC_INLINE uint32_t TIMER_GetCaptureData(TIMER_T *timer)
484 {
485     return timer->CAP;
486 }
487 
488 /**
489   * @brief      Get Counter value
490   *
491   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
492   *
493   * @return     24-bit Counter Value
494   *
495   * @details    This function reports the current 24-bit timer counter value.
496   */
TIMER_GetCounter(TIMER_T * timer)497 __STATIC_INLINE uint32_t TIMER_GetCounter(TIMER_T *timer)
498 {
499     return timer->CNT;
500 }
501 
502 /**
503   * @brief      Reset Counter
504   *
505   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
506   *
507   * @return     None
508   *
509   * @details    This function is used to reset current counter value and internal prescale counter value.
510   */
TIMER_ResetCounter(TIMER_T * timer)511 __STATIC_INLINE void TIMER_ResetCounter(TIMER_T *timer)
512 {
513     timer->CNT = 0UL;
514     while((timer->CNT & TIMER_CNT_RSTACT_Msk) == TIMER_CNT_RSTACT_Msk) {}
515 }
516 
517 
518 uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq);
519 void TIMER_Close(TIMER_T *timer);
520 void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec);
521 void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge);
522 void TIMER_DisableCapture(TIMER_T *timer);
523 void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge);
524 void TIMER_DisableEventCounter(TIMER_T *timer);
525 uint32_t TIMER_GetModuleClock(TIMER_T *timer);
526 void TIMER_EnableFreqCounter(TIMER_T *timer, uint32_t u32DropCount, uint32_t u32Timeout, uint32_t u32EnableInt);
527 void TIMER_DisableFreqCounter(TIMER_T *timer);
528 void TIMER_SetTriggerSource(TIMER_T *timer, uint32_t u32Src);
529 void TIMER_SetTriggerTarget(TIMER_T *timer, uint32_t u32Mask);
530 
531 /**@}*/ /* end of group TIMER_EXPORTED_FUNCTIONS */
532 
533 /**@}*/ /* end of group TIMER_Driver */
534 
535 /**@}*/ /* end of group Standard_Driver */
536 
537 #ifdef __cplusplus
538 }
539 #endif
540 
541 #endif /* __TIMER_H__ */
542