1 /**************************************************************************//**
\r
4 * @brief Timer Controller(Timer) driver source file
\r
6 * @copyright (C) 2017 Nuvoton Technology Corp. All rights reserved.
\r
7 *****************************************************************************/
\r
11 /** @addtogroup Standard_Driver Standard Driver
\r
15 /** @addtogroup TIMER_Driver TIMER Driver
\r
19 /** @addtogroup TIMER_EXPORTED_FUNCTIONS TIMER Exported Functions
\r
24 * @brief Open Timer with Operate Mode and Frequency
\r
26 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
27 * @param[in] u32Mode Operation mode. Possible options are
\r
28 * - \ref TIMER_ONESHOT_MODE
\r
29 * - \ref TIMER_PERIODIC_MODE
\r
30 * - \ref TIMER_TOGGLE_MODE
\r
31 * - \ref TIMER_CONTINUOUS_MODE
\r
32 * @param[in] u32Freq Target working frequency
\r
34 * @return Real timer working frequency
\r
36 * @details This API is used to configure timer to operate in specified mode and frequency.
\r
37 * If timer cannot work in target frequency, a closest frequency will be chose and returned.
\r
38 * @note After calling this API, Timer is \b NOT running yet. But could start timer running be calling
\r
39 * \ref TIMER_Start macro or program registers directly.
\r
41 uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq)
\r
43 uint32_t u32Clk = TIMER_GetModuleClock(timer);
\r
44 uint32_t u32Cmpr = 0UL, u32Prescale = 0UL;
\r
46 /* Fastest possible timer working freq is (u32Clk / 2). While cmpr = 2, prescaler = 0. */
\r
47 if(u32Freq > (u32Clk / 2UL))
\r
53 u32Cmpr = u32Clk / u32Freq;
\r
54 u32Prescale = (u32Cmpr >> 24); /* for 24 bits CMPDAT */
\r
55 if (u32Prescale > 0UL)
\r
56 u32Cmpr = u32Cmpr / (u32Prescale + 1UL);
\r
59 timer->CTL = u32Mode | u32Prescale;
\r
60 timer->CMP = u32Cmpr;
\r
62 return(u32Clk / (u32Cmpr * (u32Prescale + 1UL)));
\r
66 * @brief Stop Timer Counting
\r
68 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
72 * @details This API stops timer counting and disable all timer interrupt function.
\r
74 void TIMER_Close(TIMER_T *timer)
\r
77 timer->EXTCTL = 0UL;
\r
81 * @brief Create a specify Delay Time
\r
83 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
84 * @param[in] u32Usec Delay period in micro seconds. Valid values are between 100~1000000 (100 micro second ~ 1 second).
\r
88 * @details This API is used to create a delay loop for u32usec micro seconds by using timer one-shot mode.
\r
89 * @note This API overwrites the register setting of the timer used to count the delay time.
\r
90 * @note This API use polling mode. So there is no need to enable interrupt for the timer module used to generate delay.
\r
92 void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec)
\r
94 uint32_t u32Clk = TIMER_GetModuleClock(timer);
\r
95 uint32_t u32Prescale = 0UL, u32Delay = (SystemCoreClock / u32Clk) + 1UL;
\r
96 uint32_t u32Cmpr, u32NsecPerTick;
\r
98 /* Clear current timer configuration */
\r
100 timer->EXTCTL = 0UL;
\r
102 if(u32Clk <= 1000000UL) /* min delay is 1000 us if timer clock source is <= 1 MHz */
\r
104 if(u32Usec < 1000UL)
\r
108 if(u32Usec > 1000000UL)
\r
110 u32Usec = 1000000UL;
\r
115 if(u32Usec < 100UL)
\r
119 if(u32Usec > 1000000UL)
\r
121 u32Usec = 1000000UL;
\r
125 if(u32Clk <= 1000000UL)
\r
128 u32NsecPerTick = 1000000000UL / u32Clk;
\r
129 u32Cmpr = (u32Usec * 1000UL) / u32NsecPerTick;
\r
133 u32Cmpr = u32Usec * (u32Clk / 1000000UL);
\r
134 u32Prescale = (u32Cmpr >> 24); /* for 24 bits CMPDAT */
\r
135 if (u32Prescale > 0UL)
\r
136 u32Cmpr = u32Cmpr / (u32Prescale + 1UL);
\r
139 timer->CMP = u32Cmpr;
\r
140 timer->CTL = TIMER_CTL_CNTEN_Msk | TIMER_ONESHOT_MODE | u32Prescale;
\r
143 When system clock is faster than timer clock, it is possible timer active bit cannot set in time while we check it.
\r
144 And the while loop below return immediately, so put a tiny delay here allowing timer start counting and raise active flag.
\r
146 for(; u32Delay > 0UL; u32Delay--)
\r
151 while(timer->CTL & TIMER_CTL_ACTSTS_Msk) {}
\r
155 * @brief Enable Timer Capture Function
\r
157 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
158 * @param[in] u32CapMode Timer capture mode. Could be
\r
159 * - \ref TIMER_CAPTURE_FREE_COUNTING_MODE
\r
160 * - \ref TIMER_CAPTURE_COUNTER_RESET_MODE
\r
161 * @param[in] u32Edge Timer capture trigger edge. Possible values are
\r
162 * - \ref TIMER_CAPTURE_EVENT_FALLING
\r
163 * - \ref TIMER_CAPTURE_EVENT_RISING
\r
164 * - \ref TIMER_CAPTURE_EVENT_FALLING_RISING
\r
165 * - \ref TIMER_CAPTURE_EVENT_RISING_FALLING
\r
166 * - \ref TIMER_CAPTURE_EVENT_GET_LOW_PERIOD
\r
167 * - \ref TIMER_CAPTURE_EVENT_GET_HIGH_PERIOD
\r
171 * @details This API is used to enable timer capture function with specify capture trigger edge \n
\r
172 * to get current counter value or reset counter value to 0.
\r
173 * @note Timer frequency should be configured separately by using \ref TIMER_Open API, or program registers directly.
\r
175 void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge)
\r
177 timer->EXTCTL = (timer->EXTCTL & ~(TIMER_EXTCTL_CAPFUNCS_Msk | TIMER_EXTCTL_CAPEDGE_Msk)) |
\r
178 u32CapMode | u32Edge | TIMER_EXTCTL_CAPEN_Msk;
\r
182 * @brief Disable Timer Capture Function
\r
184 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
188 * @details This API is used to disable the timer capture function.
\r
190 void TIMER_DisableCapture(TIMER_T *timer)
\r
192 timer->EXTCTL &= ~TIMER_EXTCTL_CAPEN_Msk;
\r
196 * @brief Enable Timer Counter Function
\r
198 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
199 * @param[in] u32Edge Detection edge of counter pin. Could be ether
\r
200 * - \ref TIMER_COUNTER_EVENT_FALLING, or
\r
201 * - \ref TIMER_COUNTER_EVENT_RISING
\r
205 * @details This function is used to enable the timer counter function with specify detection edge.
\r
206 * @note Timer compare value should be configured separately by using \ref TIMER_SET_CMP_VALUE macro or program registers directly.
\r
207 * @note While using event counter function, \ref TIMER_TOGGLE_MODE cannot set as timer operation mode.
\r
209 void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge)
\r
211 timer->EXTCTL = (timer->EXTCTL & ~TIMER_EXTCTL_CNTPHASE_Msk) | u32Edge;
\r
212 timer->CTL |= TIMER_CTL_EXTCNTEN_Msk;
\r
216 * @brief Disable Timer Counter Function
\r
218 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
222 * @details This API is used to disable the timer event counter function.
\r
224 void TIMER_DisableEventCounter(TIMER_T *timer)
\r
226 timer->CTL &= ~TIMER_CTL_EXTCNTEN_Msk;
\r
230 * @brief Get Timer Clock Frequency
\r
232 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
234 * @return Timer clock frequency
\r
236 * @details This API is used to get the timer clock frequency.
\r
237 * @note This API cannot return correct clock rate if timer source is from external clock input.
\r
239 uint32_t TIMER_GetModuleClock(TIMER_T *timer)
\r
241 uint32_t u32Src, u32Clk = __HIRC;
\r
242 const uint32_t au32Clk[] = {__HXT, __LXT, 0UL, 0UL, 0UL, __LIRC, 0UL, __HIRC};
\r
244 if(timer == TIMER0)
\r
246 u32Src = CLK_GetModuleClockSource(TMR0_MODULE);
\r
248 else if(timer == TIMER1)
\r
250 u32Src = CLK_GetModuleClockSource(TMR1_MODULE);
\r
252 else if((timer == TIMER2) || (timer == TIMER2_NS))
\r
254 u32Src = CLK_GetModuleClockSource(TMR2_MODULE);
\r
256 else if((timer == TIMER3) || (timer == TIMER3_NS))
\r
258 u32Src = CLK_GetModuleClockSource(TMR3_MODULE);
\r
267 ; /* Invalid timer channel */
\r
273 if((timer == TIMER0) || (timer == TIMER1))
\r
275 u32Clk = CLK_GetPCLK0Freq();
\r
279 u32Clk = CLK_GetPCLK1Freq();
\r
284 u32Clk = au32Clk[u32Src];
\r
292 * @brief Enable Timer Frequency Counter Function
\r
294 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
295 * @param[in] u32DropCount This parameter has no effect in this BSP
\r
296 * @param[in] u32Timeout This parameter has no effect in this BSP
\r
297 * @param[in] u32EnableInt Enable interrupt assertion after capture complete or not. Valid values are TRUE and FALSE
\r
301 * @details This function is used to calculate input event frequency. After enable
\r
302 * this function, a pair of timers, TIMER0 and TIMER1, or TIMER2 and TIMER3
\r
303 * will be configured for this function. The mode used to calculate input
\r
304 * event frequency is mentioned as "Inter Timer Trigger Mode" in Technical
\r
305 * Reference Manual.
\r
307 void TIMER_EnableFreqCounter(TIMER_T *timer,
\r
308 uint32_t u32DropCount,
\r
309 uint32_t u32Timeout,
\r
310 uint32_t u32EnableInt)
\r
312 TIMER_T *t; /* store the timer base to configure compare value */
\r
314 if(timer == TIMER0)
\r
318 else if(timer == TIMER2)
\r
322 else if(timer == TIMER2_NS)
\r
333 t->CMP = 0xFFFFFFUL;
\r
334 t->EXTCTL = u32EnableInt ? TIMER_EXTCTL_CAPIEN_Msk : 0UL;
\r
335 timer->CTL = TIMER_CTL_INTRGEN_Msk | TIMER_CTL_CNTEN_Msk;
\r
340 * @brief Disable Timer Frequency Counter Function
\r
342 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
346 * @brief This function is used to disable the Timer frequency counter function.
\r
348 void TIMER_DisableFreqCounter(TIMER_T *timer)
\r
350 timer->CTL &= ~TIMER_CTL_INTRGEN_Msk;
\r
354 * @brief Select Interrupt Source to Trigger others Module
\r
356 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
357 * @param[in] u32Src Selects the interrupt source to trigger other modules. Could be:
\r
358 * - \ref TIMER_TRGSRC_TIMEOUT_EVENT
\r
359 * - \ref TIMER_TRGSRC_CAPTURE_EVENT
\r
363 * @brief This function is used to select the interrupt source used to trigger other modules.
\r
365 void TIMER_SetTriggerSource(TIMER_T *timer, uint32_t u32Src)
\r
367 timer->TRGCTL = (timer->TRGCTL & ~TIMER_TRGCTL_TRGSSEL_Msk) | u32Src;
\r
371 * @brief Set Modules Trigger by Timer Interrupt
\r
373 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
\r
374 * @param[in] u32Mask The mask of modules (EPWM, EADC, DAC and PDMA) trigger by timer. Is the combination of
\r
375 * - \ref TIMER_TRG_TO_EPWM,
\r
376 * - \ref TIMER_TRG_TO_EADC,
\r
377 * - \ref TIMER_TRG_TO_DAC and
\r
378 * - \ref TIMER_TRG_TO_PDMA
\r
382 * @details This function is used to set EPWM, EADC, DAC and PDMA module triggered by timer interrupt event.
\r
384 void TIMER_SetTriggerTarget(TIMER_T *timer, uint32_t u32Mask)
\r
386 timer->TRGCTL = (timer->TRGCTL & ~(TIMER_TRGCTL_TRGEPWM_Msk | TIMER_TRGCTL_TRGDAC_Msk | TIMER_TRGCTL_TRGEADC_Msk | TIMER_TRGCTL_TRGPDMA_Msk)) | u32Mask;
\r
389 /*@}*/ /* end of group TIMER_EXPORTED_FUNCTIONS */
\r
391 /*@}*/ /* end of group TIMER_Driver */
\r
393 /*@}*/ /* end of group Standard_Driver */
\r
395 /*** (C) COPYRIGHT 2017 Nuvoton Technology Corp. ***/
\r