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