]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MPU_M23_Nuvoton_NuMaker_PFM_M2351_IAR_GCC/Nuvoton_Code/StdDriver/src/timer.c
Add Cortex M23 GCC and IAR ports. Add demo projects for Nuvoton NuMaker-PFM-2351.
[freertos] / FreeRTOS / Demo / CORTEX_MPU_M23_Nuvoton_NuMaker_PFM_M2351_IAR_GCC / Nuvoton_Code / StdDriver / src / timer.c
1 /**************************************************************************//**\r
2  * @file     timer.c\r
3  * @version  V3.00\r
4  * @brief    Timer Controller(Timer) driver source file\r
5  *\r
6  * @copyright (C) 2017 Nuvoton Technology Corp. All rights reserved.\r
7 *****************************************************************************/\r
8 #include "NuMicro.h"\r
9 \r
10 \r
11 /** @addtogroup Standard_Driver Standard Driver\r
12   @{\r
13 */\r
14 \r
15 /** @addtogroup TIMER_Driver TIMER Driver\r
16   @{\r
17 */\r
18 \r
19 /** @addtogroup TIMER_EXPORTED_FUNCTIONS TIMER Exported Functions\r
20   @{\r
21 */\r
22 \r
23 /**\r
24   * @brief      Open Timer with Operate Mode and Frequency\r
25   *\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
33   *\r
34   * @return     Real timer working frequency\r
35   *\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
40   */\r
41 uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq)\r
42 {\r
43     uint32_t u32Clk = TIMER_GetModuleClock(timer);\r
44     uint32_t u32Cmpr = 0UL, u32Prescale = 0UL;\r
45 \r
46     /* Fastest possible timer working freq is (u32Clk / 2). While cmpr = 2, prescaler = 0. */\r
47     if(u32Freq > (u32Clk / 2UL))\r
48     {\r
49         u32Cmpr = 2UL;\r
50     }\r
51     else\r
52     {\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
57     }\r
58 \r
59     timer->CTL = u32Mode | u32Prescale;\r
60     timer->CMP = u32Cmpr;\r
61 \r
62     return(u32Clk / (u32Cmpr * (u32Prescale + 1UL)));\r
63 }\r
64 \r
65 /**\r
66   * @brief      Stop Timer Counting\r
67   *\r
68   * @param[in]  timer   The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.\r
69   *\r
70   * @return     None\r
71   *\r
72   * @details    This API stops timer counting and disable all timer interrupt function.\r
73   */\r
74 void TIMER_Close(TIMER_T *timer)\r
75 {\r
76     timer->CTL = 0UL;\r
77     timer->EXTCTL = 0UL;\r
78 }\r
79 \r
80 /**\r
81   * @brief      Create a specify Delay Time\r
82   *\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
85   *\r
86   * @return     None\r
87   *\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
91   */\r
92 void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec)\r
93 {\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
97 \r
98     /* Clear current timer configuration */\r
99     timer->CTL = 0UL;\r
100     timer->EXTCTL = 0UL;\r
101 \r
102     if(u32Clk <= 1000000UL)   /* min delay is 1000 us if timer clock source is <= 1 MHz */\r
103     {\r
104         if(u32Usec < 1000UL)\r
105         {\r
106             u32Usec = 1000UL;\r
107         }\r
108         if(u32Usec > 1000000UL)\r
109         {\r
110             u32Usec = 1000000UL;\r
111         }\r
112     }\r
113     else\r
114     {\r
115         if(u32Usec < 100UL)\r
116         {\r
117             u32Usec = 100UL;\r
118         }\r
119         if(u32Usec > 1000000UL)\r
120         {\r
121             u32Usec = 1000000UL;\r
122         }\r
123     }\r
124 \r
125     if(u32Clk <= 1000000UL)\r
126     {\r
127         u32Prescale = 0UL;\r
128         u32NsecPerTick = 1000000000UL / u32Clk;\r
129         u32Cmpr = (u32Usec * 1000UL) / u32NsecPerTick;\r
130     }\r
131     else\r
132     {\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
137     }\r
138 \r
139     timer->CMP = u32Cmpr;\r
140     timer->CTL = TIMER_CTL_CNTEN_Msk | TIMER_ONESHOT_MODE | u32Prescale;\r
141 \r
142     /*\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
145     */\r
146     for(; u32Delay > 0UL; u32Delay--)\r
147     {\r
148         __NOP();\r
149     }\r
150 \r
151     while(timer->CTL & TIMER_CTL_ACTSTS_Msk) {}\r
152 }\r
153 \r
154 /**\r
155   * @brief      Enable Timer Capture Function\r
156   *\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
168   *\r
169   * @return     None\r
170   *\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
174   */\r
175 void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge)\r
176 {\r
177     timer->EXTCTL = (timer->EXTCTL & ~(TIMER_EXTCTL_CAPFUNCS_Msk | TIMER_EXTCTL_CAPEDGE_Msk)) |\r
178                     u32CapMode | u32Edge | TIMER_EXTCTL_CAPEN_Msk;\r
179 }\r
180 \r
181 /**\r
182   * @brief      Disable Timer Capture Function\r
183   *\r
184   * @param[in]  timer   The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.\r
185   *\r
186   * @return     None\r
187   *\r
188   * @details    This API is used to disable the timer capture function.\r
189   */\r
190 void TIMER_DisableCapture(TIMER_T *timer)\r
191 {\r
192     timer->EXTCTL &= ~TIMER_EXTCTL_CAPEN_Msk;\r
193 }\r
194 \r
195 /**\r
196   * @brief      Enable Timer Counter Function\r
197   *\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
202   *\r
203   * @return     None\r
204   *\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
208   */\r
209 void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge)\r
210 {\r
211     timer->EXTCTL = (timer->EXTCTL & ~TIMER_EXTCTL_CNTPHASE_Msk) | u32Edge;\r
212     timer->CTL |= TIMER_CTL_EXTCNTEN_Msk;\r
213 }\r
214 \r
215 /**\r
216   * @brief      Disable Timer Counter Function\r
217   *\r
218   * @param[in]  timer   The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.\r
219   *\r
220   * @return     None\r
221   *\r
222   * @details    This API is used to disable the timer event counter function.\r
223   */\r
224 void TIMER_DisableEventCounter(TIMER_T *timer)\r
225 {\r
226     timer->CTL &= ~TIMER_CTL_EXTCNTEN_Msk;\r
227 }\r
228 \r
229 /**\r
230   * @brief      Get Timer Clock Frequency\r
231   *\r
232   * @param[in]  timer   The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.\r
233   *\r
234   * @return     Timer clock frequency\r
235   *\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
238   */\r
239 uint32_t TIMER_GetModuleClock(TIMER_T *timer)\r
240 {\r
241     uint32_t u32Src, u32Clk = __HIRC;\r
242     const uint32_t au32Clk[] = {__HXT, __LXT, 0UL, 0UL, 0UL, __LIRC, 0UL, __HIRC};\r
243 \r
244     if(timer == TIMER0)\r
245     {\r
246         u32Src = CLK_GetModuleClockSource(TMR0_MODULE);\r
247     }\r
248     else if(timer == TIMER1)\r
249     {\r
250         u32Src = CLK_GetModuleClockSource(TMR1_MODULE);\r
251     }\r
252     else if((timer == TIMER2) || (timer == TIMER2_NS))\r
253     {\r
254         u32Src = CLK_GetModuleClockSource(TMR2_MODULE);\r
255     }\r
256     else if((timer == TIMER3) || (timer == TIMER3_NS))\r
257     {\r
258         u32Src = CLK_GetModuleClockSource(TMR3_MODULE);\r
259     }\r
260     else\r
261     {\r
262         u32Clk = 0UL;\r
263     }\r
264 \r
265     if(u32Clk == 0UL)\r
266     {\r
267         ; /* Invalid timer channel */\r
268     }\r
269     else\r
270     {\r
271         if(u32Src == 2UL)\r
272         {\r
273             if((timer == TIMER0) || (timer == TIMER1))\r
274             {\r
275                 u32Clk = CLK_GetPCLK0Freq();\r
276             }\r
277             else\r
278             {\r
279                 u32Clk = CLK_GetPCLK1Freq();\r
280             }\r
281         }\r
282         else\r
283         {\r
284             u32Clk = au32Clk[u32Src];\r
285         }\r
286     }\r
287 \r
288     return u32Clk;\r
289 }\r
290 \r
291 /**\r
292   * @brief      Enable Timer Frequency Counter Function\r
293   *\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
298   *\r
299   * @return     None\r
300   *\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
306   */\r
307 void TIMER_EnableFreqCounter(TIMER_T *timer,\r
308                              uint32_t u32DropCount,\r
309                              uint32_t u32Timeout,\r
310                              uint32_t u32EnableInt)\r
311 {\r
312     TIMER_T *t;    /* store the timer base to configure compare value */\r
313 \r
314     if(timer == TIMER0)\r
315     {\r
316         t = TIMER1;\r
317     }\r
318     else if(timer == TIMER2)\r
319     {\r
320         t = TIMER3;\r
321     }\r
322     else if(timer == TIMER2_NS)\r
323     {\r
324         t = TIMER3_NS;\r
325     }\r
326     else\r
327     {\r
328         t = 0UL ;\r
329     }\r
330 \r
331     if(t != 0UL)\r
332     {\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
336     }\r
337 }\r
338 \r
339 /**\r
340   * @brief      Disable Timer Frequency Counter Function\r
341   *\r
342   * @param[in]  timer   The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.\r
343   *\r
344   * @return     None\r
345   *\r
346   * @brief      This function is used to disable the Timer frequency counter function.\r
347   */\r
348 void TIMER_DisableFreqCounter(TIMER_T *timer)\r
349 {\r
350     timer->CTL &= ~TIMER_CTL_INTRGEN_Msk;\r
351 }\r
352 \r
353 /**\r
354   * @brief      Select Interrupt Source to Trigger others Module\r
355   *\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
360   *\r
361   * @return     None\r
362   *\r
363   * @brief      This function is used to select the interrupt source used to trigger other modules.\r
364   */\r
365 void TIMER_SetTriggerSource(TIMER_T *timer, uint32_t u32Src)\r
366 {\r
367     timer->TRGCTL = (timer->TRGCTL & ~TIMER_TRGCTL_TRGSSEL_Msk) | u32Src;\r
368 }\r
369 \r
370 /**\r
371   * @brief      Set Modules Trigger by Timer Interrupt\r
372   *\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
379   *\r
380   * @return     None\r
381   *\r
382   * @details    This function is used to set EPWM, EADC, DAC and PDMA module triggered by timer interrupt event.\r
383   */\r
384 void TIMER_SetTriggerTarget(TIMER_T *timer, uint32_t u32Mask)\r
385 {\r
386     timer->TRGCTL = (timer->TRGCTL & ~(TIMER_TRGCTL_TRGEPWM_Msk | TIMER_TRGCTL_TRGDAC_Msk | TIMER_TRGCTL_TRGEADC_Msk | TIMER_TRGCTL_TRGPDMA_Msk)) | u32Mask;\r
387 }\r
388 \r
389 /*@}*/ /* end of group TIMER_EXPORTED_FUNCTIONS */\r
390 \r
391 /*@}*/ /* end of group TIMER_Driver */\r
392 \r
393 /*@}*/ /* end of group Standard_Driver */\r
394 \r
395 /*** (C) COPYRIGHT 2017 Nuvoton Technology Corp. ***/\r