| NUC029FAE_BSP V3.02.000 The Board Support Package for NUC029FAE MCU

timer.c
Go to the documentation of this file.
1/**************************************************************************/
12#include "NUC029FAE.h"
13
42uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq)
43{
44 uint32_t u32Clk = TIMER_GetModuleClock(timer);
45 uint32_t u32Cmpr = 0, u32Prescale = 0;
46
47 // Fastest possible timer working freq is u32Clk / 2. While cmpr = 2, pre-scale = 0
48 if(u32Freq > (u32Clk / 2))
49 {
50 u32Cmpr = 2;
51 }
52 else
53 {
54 if(u32Clk > 0xFFFFFF) // For Mini, only needs to consider 24MHz at most
55 {
56 u32Prescale = 1;
57 u32Clk >>= 1;
58 }
59 u32Cmpr = u32Clk / u32Freq;
60 }
61
62 timer->TCSR = u32Mode | u32Prescale;
63 timer->TCMPR = u32Cmpr;
64
65 return(u32Clk / (u32Cmpr * (u32Prescale + 1)));
66}
67
73 void TIMER_Close(TIMER_T *timer)
74{
75 timer->TCSR = 0;
76 timer->TEXCON = 0;
77
78}
79
88 void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec)
89{
90 uint32_t u32Clk = TIMER_GetModuleClock(timer);
91 uint32_t u32Prescale = 0, delay = SystemCoreClock / u32Clk;
92 float fCmpr;
93
94 // Clear current timer configuration
95 timer->TCSR = 0;
96 timer->TEXCON = 0;
97
98 if(u32Clk == 10000) // min delay is 100us if timer clock source is LIRC 10k
99 {
100 u32Usec = ((u32Usec + 99) / 100) * 100;
101 }
102 else // 10 usec every step
103 {
104 u32Usec = ((u32Usec + 9) / 10) * 10;
105 }
106
107 if(u32Clk > 0xFFFFFF) // For Mini, only needs to consider 24MHz at most
108 {
109 u32Prescale = 1;
110 u32Clk >>= 1;
111 }
112
113 // u32Usec * u32Clk might overflow if using uint32_t
114 fCmpr = ((float)u32Usec * (float)u32Clk) / 1000000.0;
115
116 timer->TCMPR = (uint32_t)fCmpr;
117 timer->TCSR = TIMER_TCSR_CEN_Msk | u32Prescale; // one shot mode
118
119 // When system clock is faster than timer clock, it is possible timer active bit cannot set in time while we check it.
120 // And the while loop below return immediately, so put a tiny delay here allowing timer start counting and raise active flag.
121 for(; delay > 0; delay--)
122 {
123 __NOP();
124 }
125
126 delay = (SystemCoreClock / 1000000) * u32Usec * 10;
127 while((timer->TCSR & TIMER_TCSR_CACT_Msk) && (delay-- > 0));
128
129}
130
146 void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge)
147{
148
149 timer->TEXCON = (timer->TEXCON & ~(TIMER_TEXCON_CAP_MODE_Msk |
150 TIMER_TEXCON_RSTCAPSEL_Msk |
151 TIMER_TEXCON_TEX_EDGE_Msk)) |
152 u32CapMode | u32Edge | TIMER_TEXCON_TEXEN_Msk;
153}
154
160 void TIMER_DisableCapture(TIMER_T *timer)
161{
162 timer->TEXCON &= ~TIMER_TEXCON_TEXEN_Msk;
163
164}
165
175 void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge)
176{
177 timer->TEXCON = (timer->TEXCON & ~TIMER_TEXCON_TX_PHASE_Msk) | u32Edge;
178 timer->TCSR |= TIMER_TCSR_CTB_Msk;
179}
180
186 void TIMER_DisableEventCounter(TIMER_T *timer)
187{
188 timer->TCSR &= ~TIMER_TCSR_CTB_Msk;
189}
190
197uint32_t TIMER_GetModuleClock(TIMER_T *timer)
198{
199 uint32_t u32Src;
200 if(timer == TIMER0)
201 u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR0_S_Msk) >> CLK_CLKSEL1_TMR0_S_Pos;
202 else
203 u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR1_S_Msk) >> CLK_CLKSEL1_TMR1_S_Pos;
204
205 if(u32Src == 0)
206 return((CLK->PWRCON & CLK_PWRCON_XTLCLK_EN_Msk) == 1 ? __XTAL12M : __XTAL32K);
207 else if(u32Src == 1)
208 return __IRC10K;
209 else if(u32Src == 2)
210 return SystemCoreClock;
211 else
212 return __IRC22M;
213
214}
215 /* end of group NUC029FAE_TIMER_EXPORTED_FUNCTIONS */
217 /* end of group NUC029FAE_TIMER_Driver */
219 /* end of group NUC029FAE_Device_Driver */
221
222/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
NUC029FAE.h
NUC029FAE peripheral access layer header file. This file contains all the peripheral register's defin...
CLK
#define CLK
Pointer to CLK register structure.
Definition: NUC029FAE.h:3228
TIMER0
#define TIMER0
Pointer to Timer 0 register structure.
Definition: NUC029FAE.h:3218
TIMER_Delay
void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec)
This API is used to create a delay loop for u32usec micro seconds.
Definition: timer.c:88
TIMER_DisableCapture
void TIMER_DisableCapture(TIMER_T *timer)
This API is used to disable the Timer capture function.
Definition: timer.c:160
TIMER_Open
uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq)
This API is used to configure timer to operate in specified mode and frequency. If timer cannot work ...
Definition: timer.c:42
TIMER_DisableEventCounter
void TIMER_DisableEventCounter(TIMER_T *timer)
This API is used to disable the Timer event counter function.
Definition: timer.c:186
TIMER_GetModuleClock
uint32_t TIMER_GetModuleClock(TIMER_T *timer)
This API is used to get the clock frequency of Timer.
Definition: timer.c:197
TIMER_EnableEventCounter
void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge)
This function is used to enable the Timer counter function with specify detection edge.
Definition: timer.c:175
TIMER_EnableCapture
void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge)
This API is used to enable timer capture function with specified mode and capture edge.
Definition: timer.c:146
TIMER_Close
void TIMER_Close(TIMER_T *timer)
This API stops Timer counting and disable the Timer interrupt function.
Definition: timer.c:73
TIMER_T::TCMPR
__IO uint32_t TCMPR
Definition: NUC029FAE.h:2762
TIMER_T::TCSR
__IO uint32_t TCSR
Definition: NUC029FAE.h:2761
TIMER_T::TEXCON
__IO uint32_t TEXCON
Definition: NUC029FAE.h:2766
TIMER_T
Definition: NUC029FAE.h:2760
__IRC10K
#define __IRC10K
Definition: system_NUC029FAE.h:32
__IRC22M
#define __IRC22M
Definition: system_NUC029FAE.h:31
__XTAL32K
#define __XTAL32K
Definition: system_NUC029FAE.h:30
__XTAL12M
#define __XTAL12M
Definition: system_NUC029FAE.h:29
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_NUC029FAE.c:20