1 /**************************************************************************//**
2  * @file     timer.c
3  * @brief    M480 Timer Controller(Timer) driver source file
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  * @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
7 *****************************************************************************/
8 #include "NuMicro.h"
9 
10 
11 /** @addtogroup Standard_Driver Standard Driver
12   @{
13 */
14 
15 /** @addtogroup TIMER_Driver TIMER Driver
16   @{
17 */
18 
19 /** @addtogroup TIMER_EXPORTED_FUNCTIONS TIMER Exported Functions
20   @{
21 */
22 
23 /**
24   * @brief      Open Timer with Operate Mode and Frequency
25   *
26   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
27   * @param[in]  u32Mode     Operation mode. Possible options are
28   *                         - \ref TIMER_ONESHOT_MODE
29   *                         - \ref TIMER_PERIODIC_MODE
30   *                         - \ref TIMER_TOGGLE_MODE
31   *                         - \ref TIMER_CONTINUOUS_MODE
32   * @param[in]  u32Freq     Target working frequency
33   *
34   * @return     Real timer working frequency
35   *
36   * @details    This API is used to configure timer to operate in specified mode and frequency.
37   *             If timer cannot work in target frequency, a closest frequency will be chose and returned.
38   * @note       After calling this API, Timer is \b NOT running yet. But could start timer running be calling
39   *             \ref TIMER_Start macro or program registers directly.
40   */
TIMER_Open(TIMER_T * timer,uint32_t u32Mode,uint32_t u32Freq)41 uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq)
42 {
43     uint32_t u32Clk = TIMER_GetModuleClock(timer);
44     uint32_t u32Cmpr = 0UL, u32Prescale = 0UL;
45 
46     /* Fastest possible timer working freq is (u32Clk / 2). While cmpr = 2, prescaler = 0. */
47     if(u32Freq > (u32Clk / 2UL))
48     {
49         u32Cmpr = 2UL;
50     }
51     else
52     {
53         u32Cmpr = u32Clk / u32Freq;
54         u32Prescale = (u32Cmpr >> 24);  /* for 24 bits CMPDAT */
55         if (u32Prescale > 0UL)
56             u32Cmpr = u32Cmpr / (u32Prescale + 1UL);
57     }
58 
59     timer->CTL = u32Mode | u32Prescale;
60     timer->CMP = u32Cmpr;
61 
62     return(u32Clk / (u32Cmpr * (u32Prescale + 1UL)));
63 }
64 
65 /**
66   * @brief      Stop Timer Counting
67   *
68   * @param[in]  timer   The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
69   *
70   * @return     None
71   *
72   * @details    This API stops timer counting and disable all timer interrupt function.
73   */
TIMER_Close(TIMER_T * timer)74 void TIMER_Close(TIMER_T *timer)
75 {
76     timer->CTL = 0UL;
77     timer->EXTCTL = 0UL;
78 }
79 
80 /**
81   * @brief      Create a specify Delay Time
82   *
83   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
84   * @param[in]  u32Usec     Delay period in micro seconds. Valid values are between 100~1000000 (100 micro second ~ 1 second).
85   *
86   * @return     None
87   *
88   * @details    This API is used to create a delay loop for u32usec micro seconds by using timer one-shot mode.
89   * @note       This API overwrites the register setting of the timer used to count the delay time.
90   * @note       This API use polling mode. So there is no need to enable interrupt for the timer module used to generate delay.
91   */
TIMER_Delay(TIMER_T * timer,uint32_t u32Usec)92 void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec)
93 {
94     uint32_t u32Clk = TIMER_GetModuleClock(timer);
95     uint32_t u32Prescale = 0UL, delay = (SystemCoreClock / u32Clk) + 1UL;
96     uint32_t u32Cmpr, u32NsecPerTick;
97 
98     /* Clear current timer configuration */
99     timer->CTL = 0UL;
100     timer->EXTCTL = 0UL;
101 
102     if(u32Clk <= 1000000UL)   /* min delay is 1000 us if timer clock source is <= 1 MHz */
103     {
104         if(u32Usec < 1000UL)
105         {
106             u32Usec = 1000UL;
107         }
108         if(u32Usec > 1000000UL)
109         {
110             u32Usec = 1000000UL;
111         }
112     }
113     else
114     {
115         if(u32Usec < 100UL)
116         {
117             u32Usec = 100UL;
118         }
119         if(u32Usec > 1000000UL)
120         {
121             u32Usec = 1000000UL;
122         }
123     }
124 
125     if(u32Clk <= 1000000UL)
126     {
127         u32Prescale = 0UL;
128         u32NsecPerTick = 1000000000UL / u32Clk;
129         u32Cmpr = (u32Usec * 1000UL) / u32NsecPerTick;
130     }
131     else
132     {
133         u32Cmpr = u32Usec * (u32Clk / 1000000UL);
134         u32Prescale = (u32Cmpr >> 24);  /* for 24 bits CMPDAT */
135         if (u32Prescale > 0UL)
136             u32Cmpr = u32Cmpr / (u32Prescale + 1UL);
137     }
138 
139     timer->CMP = u32Cmpr;
140     timer->CTL = TIMER_CTL_CNTEN_Msk | TIMER_ONESHOT_MODE | u32Prescale;
141 
142     /* When system clock is faster than timer clock, it is possible timer active bit cannot set in time while we check it.
143        And the while loop below return immediately, so put a tiny delay here allowing timer start counting and raise active flag. */
144     for(; delay > 0UL; delay--)
145     {
146         __NOP();
147     }
148 
149     while(timer->CTL & TIMER_CTL_ACTSTS_Msk)
150     {
151         ;
152     }
153 }
154 
155 /**
156   * @brief      Enable Timer Capture Function
157   *
158   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
159   * @param[in]  u32CapMode  Timer capture mode. Could be
160   *                         - \ref TIMER_CAPTURE_FREE_COUNTING_MODE
161   *                         - \ref TIMER_CAPTURE_COUNTER_RESET_MODE
162   * @param[in]  u32Edge     Timer capture trigger edge. Possible values are
163   *                         - \ref TIMER_CAPTURE_EVENT_FALLING
164   *                         - \ref TIMER_CAPTURE_EVENT_RISING
165   *                         - \ref TIMER_CAPTURE_EVENT_FALLING_RISING
166   *                         - \ref TIMER_CAPTURE_EVENT_RISING_FALLING
167   *
168   * @return     None
169   *
170   * @details    This API is used to enable timer capture function with specify capture trigger edge \n
171   *             to get current counter value or reset counter value to 0.
172   * @note       Timer frequency should be configured separately by using \ref TIMER_Open API, or program registers directly.
173   */
TIMER_EnableCapture(TIMER_T * timer,uint32_t u32CapMode,uint32_t u32Edge)174 void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge)
175 {
176     timer->EXTCTL = (timer->EXTCTL & ~(TIMER_EXTCTL_CAPFUNCS_Msk | TIMER_EXTCTL_CAPEDGE_Msk)) |
177                     u32CapMode | u32Edge | TIMER_EXTCTL_CAPEN_Msk;
178 }
179 
180 /**
181   * @brief      Disable Timer Capture Function
182   *
183   * @param[in]  timer   The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
184   *
185   * @return     None
186   *
187   * @details    This API is used to disable the timer capture function.
188   */
TIMER_DisableCapture(TIMER_T * timer)189 void TIMER_DisableCapture(TIMER_T *timer)
190 {
191     timer->EXTCTL &= ~TIMER_EXTCTL_CAPEN_Msk;
192 }
193 
194 /**
195   * @brief      Enable Timer Counter Function
196   *
197   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
198   * @param[in]  u32Edge     Detection edge of counter pin. Could be ether
199   *                         - \ref TIMER_COUNTER_EVENT_FALLING, or
200   *                         - \ref TIMER_COUNTER_EVENT_RISING
201   *
202   * @return     None
203   *
204   * @details    This function is used to enable the timer counter function with specify detection edge.
205   * @note       Timer compare value should be configured separately by using \ref TIMER_SET_CMP_VALUE macro or program registers directly.
206   * @note       While using event counter function, \ref TIMER_TOGGLE_MODE cannot set as timer operation mode.
207   */
TIMER_EnableEventCounter(TIMER_T * timer,uint32_t u32Edge)208 void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge)
209 {
210     timer->EXTCTL = (timer->EXTCTL & ~TIMER_EXTCTL_CNTPHASE_Msk) | u32Edge;
211     timer->CTL |= TIMER_CTL_EXTCNTEN_Msk;
212 }
213 
214 /**
215   * @brief      Disable Timer Counter Function
216   *
217   * @param[in]  timer   The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
218   *
219   * @return     None
220   *
221   * @details    This API is used to disable the timer event counter function.
222   */
TIMER_DisableEventCounter(TIMER_T * timer)223 void TIMER_DisableEventCounter(TIMER_T *timer)
224 {
225     timer->CTL &= ~TIMER_CTL_EXTCNTEN_Msk;
226 }
227 
228 /**
229   * @brief      Get Timer Clock Frequency
230   *
231   * @param[in]  timer   The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
232   *
233   * @return     Timer clock frequency
234   *
235   * @details    This API is used to get the timer clock frequency.
236   * @note       This API cannot return correct clock rate if timer source is from external clock input.
237   */
TIMER_GetModuleClock(TIMER_T * timer)238 uint32_t TIMER_GetModuleClock(TIMER_T *timer)
239 {
240     uint32_t u32Src, u32Clk;
241     const uint32_t au32Clk[] = {__HXT, __LXT, 0UL, 0UL, 0UL, __LIRC, 0UL, __HIRC};
242 
243     if(timer == TIMER0)
244     {
245         u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR0SEL_Msk) >> CLK_CLKSEL1_TMR0SEL_Pos;
246     }
247     else if(timer == TIMER1)
248     {
249         u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR1SEL_Msk) >> CLK_CLKSEL1_TMR1SEL_Pos;
250     }
251     else if(timer == TIMER2)
252     {
253         u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR2SEL_Msk) >> CLK_CLKSEL1_TMR2SEL_Pos;
254     }
255     else      /* Timer 3 */
256     {
257         u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR3SEL_Msk) >> CLK_CLKSEL1_TMR3SEL_Pos;
258     }
259 
260     if(u32Src == 2UL)
261     {
262         if((timer == TIMER0) || (timer == TIMER1))
263         {
264             u32Clk = CLK_GetPCLK0Freq();
265         }
266         else
267         {
268             u32Clk = CLK_GetPCLK1Freq();
269         }
270     }
271     else
272     {
273         u32Clk = au32Clk[u32Src];
274     }
275 
276     return u32Clk;
277 }
278 
279 
280 
281 /**
282   * @brief This function is used to enable the Timer frequency counter function
283   * @param[in] timer The base address of Timer module. Can be \ref TIMER0 or \ref TIMER2
284   * @param[in] u32DropCount This parameter has no effect in M480 series BSP
285   * @param[in] u32Timeout This parameter has no effect in M480 series BSP
286   * @param[in] u32EnableInt Enable interrupt assertion after capture complete or not. Valid values are TRUE and FALSE
287   * @return None
288   * @details This function is used to calculate input event frequency. After enable
289   *          this function, a pair of timers, TIMER0 and TIMER1, or TIMER2 and TIMER3
290   *          will be configured for this function. The mode used to calculate input
291   *          event frequency is mentioned as "Inter Timer Trigger Mode" in Technical
292   *          Reference Manual
293   */
TIMER_EnableFreqCounter(TIMER_T * timer,uint32_t u32DropCount,uint32_t u32Timeout,uint32_t u32EnableInt)294 void TIMER_EnableFreqCounter(TIMER_T *timer,
295                              uint32_t u32DropCount,
296                              uint32_t u32Timeout,
297                              uint32_t u32EnableInt)
298 {
299     TIMER_T *t;    /* store the timer base to configure compare value */
300 
301     t = (timer == TIMER0) ? TIMER1 : TIMER3;
302 
303     t->CMP = 0xFFFFFFUL;
304     t->EXTCTL = u32EnableInt ? TIMER_EXTCTL_CAPIEN_Msk : 0UL;
305     timer->CTL = TIMER_CTL_INTRGEN_Msk | TIMER_CTL_CNTEN_Msk;
306 
307     return;
308 }
309 /**
310   * @brief This function is used to disable the Timer frequency counter function.
311   * @param[in] timer The base address of Timer module
312   * @return None
313   */
TIMER_DisableFreqCounter(TIMER_T * timer)314 void TIMER_DisableFreqCounter(TIMER_T *timer)
315 {
316     timer->CTL &= ~TIMER_CTL_INTRGEN_Msk;
317 }
318 
319 
320 /**
321   * @brief This function is used to select the interrupt source used to trigger other modules.
322   * @param[in] timer The base address of Timer module
323   * @param[in] u32Src Selects the interrupt source to trigger other modules. Could be:
324   *              - \ref TIMER_TRGSRC_TIMEOUT_EVENT
325   *              - \ref TIMER_TRGSRC_CAPTURE_EVENT
326   * @return None
327   */
TIMER_SetTriggerSource(TIMER_T * timer,uint32_t u32Src)328 void TIMER_SetTriggerSource(TIMER_T *timer, uint32_t u32Src)
329 {
330     timer->TRGCTL = (timer->TRGCTL & ~TIMER_TRGCTL_TRGSSEL_Msk) | u32Src;
331 }
332 
333 /**
334   * @brief This function is used to set modules trigger by timer interrupt
335   * @param[in] timer The base address of Timer module
336   * @param[in] u32Mask The mask of modules (EPWM, EADC, DAC and PDMA) trigger by timer. Is the combination of
337   *             - \ref TIMER_TRG_TO_EPWM,
338   *             - \ref TIMER_TRG_TO_EADC,
339   *             - \ref TIMER_TRG_TO_DAC, and
340   *             - \ref TIMER_TRG_TO_PDMA
341   * @return None
342   */
TIMER_SetTriggerTarget(TIMER_T * timer,uint32_t u32Mask)343 void TIMER_SetTriggerTarget(TIMER_T *timer, uint32_t u32Mask)
344 {
345     timer->TRGCTL = (timer->TRGCTL & ~(TIMER_TRGCTL_TRGEPWM_Msk | TIMER_TRGCTL_TRGDAC_Msk | TIMER_TRGCTL_TRGEADC_Msk | TIMER_TRGCTL_TRGPDMA_Msk)) | u32Mask;
346 }
347 
348 /*@}*/ /* end of group TIMER_EXPORTED_FUNCTIONS */
349 
350 /*@}*/ /* end of group TIMER_Driver */
351 
352 /*@}*/ /* end of group Standard_Driver */
353 
354