| NUC029FAE BSP V3.01.002 The Board Support Package for NUC029FAE

timer.c
Go to the documentation of this file.
1 /**************************************************************************/
12 #include "NUC029FAE.h"
13
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 = 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 while(timer->TCSR & TIMER_TCSR_CACT_Msk);
127
128 }
129
145 void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge)
146 {
147
148 timer->TEXCON = (timer->TEXCON & ~(TIMER_TEXCON_CAP_MODE_Msk |
149 TIMER_TEXCON_RSTCAPSEL_Msk |
150 TIMER_TEXCON_TEX_EDGE_Msk)) |
151 u32CapMode | u32Edge | TIMER_TEXCON_TEXEN_Msk;
152 }
153
159 void TIMER_DisableCapture(TIMER_T *timer)
160 {
161 timer->TEXCON &= ~TIMER_TEXCON_TEXEN_Msk;
162
163 }
164
174 void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge)
175 {
176 timer->TEXCON = (timer->TEXCON & ~TIMER_TEXCON_TX_PHASE_Msk) | u32Edge;
177 timer->TCSR |= TIMER_TCSR_CTB_Msk;
178 }
179
185 void TIMER_DisableEventCounter(TIMER_T *timer)
186 {
187 timer->TCSR &= ~TIMER_TCSR_CTB_Msk;
188 }
189
196 uint32_t TIMER_GetModuleClock(TIMER_T *timer)
197 {
198 uint32_t u32Src;
199 if(timer == TIMER0)
200 u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR0_S_Msk) >> CLK_CLKSEL1_TMR0_S_Pos;
201 else
202 u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR1_S_Msk) >> CLK_CLKSEL1_TMR1_S_Pos;
203
204 if(u32Src == 0)
205 return((CLK->PWRCON & CLK_PWRCON_XTLCLK_EN_Msk) == 1 ? __XTAL12M : __XTAL32K);
206 else if(u32Src == 1)
207 return __IRC10K;
208 else if(u32Src == 2)
209 return SystemCoreClock;
210 else
211 return __IRC22M;
212
213 }
214 /* end of group NUC029FAE_TIMER_EXPORTED_FUNCTIONS */
216 /* end of group NUC029FAE_TIMER_Driver */
218 /* end of group NUC029FAE_Device_Driver */
220
221 /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
NUC029FAE.h
NUC029FAE peripheral access layer header file. This file contains all the peripheral register's defin...
__IRC10K
#define __IRC10K
Definition: system_NUC029FAE.h:32
TIMER_T::TEXCON
__IO uint32_t TEXCON
Definition: NUC029FAE.h:2766
TIMER_GetModuleClock
uint32_t TIMER_GetModuleClock(TIMER_T *timer)
This API is used to get the clock frequency of Timer.
Definition: timer.c:196
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:145
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_NUC029FAE.c:20
__IRC22M
#define __IRC22M
Definition: system_NUC029FAE.h:31
__XTAL12M
#define __XTAL12M
Definition: system_NUC029FAE.h:29
__XTAL32K
#define __XTAL32K
Definition: system_NUC029FAE.h:30
TIMER_T::TCMPR
__IO uint32_t TCMPR
Definition: NUC029FAE.h:2762
TIMER0
#define TIMER0
Pointer to Timer 0 register structure.
Definition: NUC029FAE.h:3218
TIMER_DisableCapture
void TIMER_DisableCapture(TIMER_T *timer)
This API is used to disable the Timer capture function.
Definition: timer.c:159
CLK
#define CLK
Pointer to CLK register structure.
Definition: NUC029FAE.h:3228
TIMER_DisableEventCounter
void TIMER_DisableEventCounter(TIMER_T *timer)
This API is used to disable the Timer event counter function.
Definition: timer.c:185
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_T
Definition: NUC029FAE.h:2759
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:174
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_T::TCSR
__IO uint32_t TCSR
Definition: NUC029FAE.h:2761
TIMER_Close
void TIMER_Close(TIMER_T *timer)
This API stops Timer counting and disable the Timer interrupt function.
Definition: timer.c:73