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) 2017-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, TIMER1, TIMER2, TIMER3.
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 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 u32Clk = 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 > (u32Clk / 2UL))
49 {
50 u32Cmpr = 2UL;
51 }
52 else
53 {
54 u32Cmpr = u32Clk / 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(u32Clk / (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, TIMER1, TIMER2, TIMER3.
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, TIMER1, TIMER2, TIMER3.
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 This API overwrites the register setting of the timer used to count the delay time.
91 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 u32Clk = TIMER_GetModuleClock(timer);
96 uint32_t u32Prescale = 0UL, u32Delay = (SystemCoreClock / u32Clk) + 1UL;
97 uint32_t u32Cmpr, u32NsecPerTick;
98
99 /* Clear current timer configuration */
100 timer->CTL = 0UL;
101 timer->EXTCTL = 0UL;
102
103 if(u32Clk <= 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(u32Clk <= 1000000UL)
127 {
128 u32Prescale = 0UL;
129 u32NsecPerTick = 1000000000UL / u32Clk;
130 u32Cmpr = (u32Usec * 1000UL) / u32NsecPerTick;
131 }
132 else
133 {
134 u32Cmpr = u32Usec * (u32Clk / 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) {}
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 * - \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 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, TIMER1, TIMER2, TIMER3.
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, TIMER1, TIMER2, TIMER3.
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 Timer compare value should be configured separately by using \ref TIMER_SET_CMP_VALUE macro or program registers directly.
208 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, TIMER1, TIMER2, TIMER3.
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, TIMER1, TIMER2, TIMER3.
234 *
235 * @return Timer clock frequency
236 *
237 * @details This API is used to get the timer clock frequency.
238 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 #if 1
243 return __HIRC;
244 #else
245 uint32_t u32Src, u32Clk = __HIRC;
246 const uint32_t au32Clk[] = {__HXT, __LXT, 0UL, 0UL, 0UL, __LIRC, 0UL, __HIRC};
247
248 if(timer == TIMER0)
249 {
250 u32Src = CLK_GetModuleClockSource(TMR0_MODULE);
251 }
252 else if(timer == TIMER1)
253 {
254 u32Src = CLK_GetModuleClockSource(TMR1_MODULE);
255 }
256 else if((timer == TIMER2) || (timer == TIMER2_NS))
257 {
258 u32Src = CLK_GetModuleClockSource(TMR2_MODULE);
259 }
260 else if((timer == TIMER3) || (timer == TIMER3_NS))
261 {
262 u32Src = CLK_GetModuleClockSource(TMR3_MODULE);
263 }
264 else
265 {
266 return 0UL;
267 }
268
269 if(u32Src == 2UL)
270 {
271 if((timer == TIMER0) || (timer == TIMER1))
272 {
273 u32Clk = CLK_GetPCLK0Freq();
274 }
275 else
276 {
277 u32Clk = CLK_GetPCLK1Freq();
278 }
279 }
280 else
281 {
282 u32Clk = au32Clk[u32Src];
283 }
284
285 return u32Clk;
286 #endif
287 }
288
289 /**
290 * @brief Enable Timer Frequency Counter Function
291 *
292 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
293 * @param[in] u32DropCount This parameter has no effect in this BSP
294 * @param[in] u32Timeout This parameter has no effect in this BSP
295 * @param[in] u32EnableInt Enable interrupt assertion after capture complete or not. Valid values are TRUE and FALSE
296 *
297 * @return None
298 *
299 * @details This function is used to calculate input event frequency. After enable
300 * this function, a pair of timers, TIMER0 and TIMER1, or TIMER2 and TIMER3
301 * will be configured for this function. The mode used to calculate input
302 * event frequency is mentioned as "Inter Timer Trigger Mode" in Technical
303 * Reference Manual.
304 */
TIMER_EnableFreqCounter(TIMER_T * timer,uint32_t u32DropCount,uint32_t u32Timeout,uint32_t u32EnableInt)305 void TIMER_EnableFreqCounter(TIMER_T *timer,
306 uint32_t u32DropCount,
307 uint32_t u32Timeout,
308 uint32_t u32EnableInt)
309 {
310 TIMER_T *t; /* store the timer base to configure compare value */
311
312 (void)u32DropCount;
313 (void)u32Timeout;
314
315 if(timer == TIMER0)
316 {
317 t = TIMER1;
318 }
319 else if(timer == TIMER2)
320 {
321 t = TIMER3;
322 }
323 else if(timer == TIMER2_NS)
324 {
325 t = TIMER3_NS;
326 }
327 else
328 {
329 t = 0UL;
330 }
331
332 if(t != 0)
333 {
334 t->CMP = 0xFFFFFFUL;
335 t->EXTCTL = u32EnableInt ? TIMER_EXTCTL_CAPIEN_Msk : 0UL;
336 timer->CTL = TIMER_CTL_INTRGEN_Msk | TIMER_CTL_CNTEN_Msk;
337 }
338 }
339
340 /**
341 * @brief Disable Timer Frequency Counter Function
342 *
343 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
344 *
345 * @return None
346 *
347 * @brief This function is used to disable the Timer frequency counter function.
348 */
TIMER_DisableFreqCounter(TIMER_T * timer)349 void TIMER_DisableFreqCounter(TIMER_T *timer)
350 {
351 timer->CTL &= ~TIMER_CTL_INTRGEN_Msk;
352 }
353
354 /**
355 * @brief Select Interrupt Source to Trigger others Module
356 *
357 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
358 * @param[in] u32Src Selects the interrupt source to trigger other modules. Could be:
359 * - \ref TIMER_TRGSRC_TIMEOUT_EVENT
360 * - \ref TIMER_TRGSRC_CAPTURE_EVENT
361 *
362 * @return None
363 *
364 * @brief This function is used to select the interrupt source used to trigger other modules.
365 */
TIMER_SetTriggerSource(TIMER_T * timer,uint32_t u32Src)366 void TIMER_SetTriggerSource(TIMER_T *timer, uint32_t u32Src)
367 {
368 timer->TRGCTL = (timer->TRGCTL & ~TIMER_TRGCTL_TRGSSEL_Msk) | u32Src;
369 }
370
371 /**
372 * @brief Set Modules Trigger by Timer Interrupt
373 *
374 * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
375 * @param[in] u32Mask The mask of modules (EPWM, EADC, DAC and PDMA) trigger by timer. Is the combination of
376 * - \ref TIMER_TRG_TO_EPWM,
377 * - \ref TIMER_TRG_TO_EADC,
378 * - \ref TIMER_TRG_TO_DAC and
379 * - \ref TIMER_TRG_TO_PDMA
380 *
381 * @return None
382 *
383 * @details This function is used to set EPWM, EADC, DAC and PDMA module triggered by timer interrupt event.
384 */
TIMER_SetTriggerTarget(TIMER_T * timer,uint32_t u32Mask)385 void TIMER_SetTriggerTarget(TIMER_T *timer, uint32_t u32Mask)
386 {
387 timer->TRGCTL = (timer->TRGCTL & ~(TIMER_TRGCTL_TRGEPWM_Msk | TIMER_TRGCTL_TRGDAC_Msk | TIMER_TRGCTL_TRGEADC_Msk | TIMER_TRGCTL_TRGPDMA_Msk)) | u32Mask;
388 }
389
390 /*@}*/ /* end of group TIMER_EXPORTED_FUNCTIONS */
391
392 /*@}*/ /* end of group TIMER_Driver */
393
394 /*@}*/ /* end of group Standard_Driver */
395
396