| M480 BSP V3.05.001 The Board Support Package for M480 Series

timer.c
Go to the documentation of this file.
1 /**************************************************************************/
8 #include "NuMicro.h"
9
10
41 uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq)
42 {
43 uint32_t u32Clk = TIMER_GetModuleClock(timer);
44 uint32_t u32Cmpr = 0UL, u32Prescale = 0UL;
45
46 /* Fastest possible timer working freq is (u32Clk / 2). While cmpr = 2, prescaler = 0. */
47 if(u32Freq > (u32Clk / 2UL))
48 {
49 u32Cmpr = 2UL;
50 }
51 else
52 {
53 u32Cmpr = u32Clk / u32Freq;
54 u32Prescale = (u32Cmpr >> 24); /* for 24 bits CMPDAT */
55 if (u32Prescale > 0UL)
56 u32Cmpr = u32Cmpr / (u32Prescale + 1UL);
57 }
58
59 timer->CTL = u32Mode | u32Prescale;
60 timer->CMP = u32Cmpr;
61
62 return(u32Clk / (u32Cmpr * (u32Prescale + 1UL)));
63 }
64
74 void TIMER_Close(TIMER_T *timer)
75 {
76 timer->CTL = 0UL;
77 timer->EXTCTL = 0UL;
78 }
79
92 void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec)
93 {
94 uint32_t u32Clk = TIMER_GetModuleClock(timer);
95 uint32_t u32Prescale = 0UL, delay = (SystemCoreClock / u32Clk) + 1UL;
96 uint32_t u32Cmpr, u32NsecPerTick;
97
98 /* Clear current timer configuration */
99 timer->CTL = 0UL;
100 timer->EXTCTL = 0UL;
101
102 if(u32Clk <= 1000000UL) /* min delay is 1000 us if timer clock source is <= 1 MHz */
103 {
104 if(u32Usec < 1000UL)
105 {
106 u32Usec = 1000UL;
107 }
108 if(u32Usec > 1000000UL)
109 {
110 u32Usec = 1000000UL;
111 }
112 }
113 else
114 {
115 if(u32Usec < 100UL)
116 {
117 u32Usec = 100UL;
118 }
119 if(u32Usec > 1000000UL)
120 {
121 u32Usec = 1000000UL;
122 }
123 }
124
125 if(u32Clk <= 1000000UL)
126 {
127 u32Prescale = 0UL;
128 u32NsecPerTick = 1000000000UL / u32Clk;
129 u32Cmpr = (u32Usec * 1000UL) / u32NsecPerTick;
130 }
131 else
132 {
133 u32Cmpr = u32Usec * (u32Clk / 1000000UL);
134 u32Prescale = (u32Cmpr >> 24); /* for 24 bits CMPDAT */
135 if (u32Prescale > 0UL)
136 u32Cmpr = u32Cmpr / (u32Prescale + 1UL);
137 }
138
139 timer->CMP = u32Cmpr;
140 timer->CTL = TIMER_CTL_CNTEN_Msk | TIMER_ONESHOT_MODE | u32Prescale;
141
142 /* When system clock is faster than timer clock, it is possible timer active bit cannot set in time while we check it.
143 And the while loop below return immediately, so put a tiny delay here allowing timer start counting and raise active flag. */
144 for(; delay > 0UL; delay--)
145 {
146 __NOP();
147 }
148
149 while(timer->CTL & TIMER_CTL_ACTSTS_Msk)
150 {
151 ;
152 }
153 }
154
174 void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge)
175 {
176 timer->EXTCTL = (timer->EXTCTL & ~(TIMER_EXTCTL_CAPFUNCS_Msk | TIMER_EXTCTL_CAPEDGE_Msk)) |
177 u32CapMode | u32Edge | TIMER_EXTCTL_CAPEN_Msk;
178 }
179
189 void TIMER_DisableCapture(TIMER_T *timer)
190 {
191 timer->EXTCTL &= ~TIMER_EXTCTL_CAPEN_Msk;
192 }
193
208 void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge)
209 {
210 timer->EXTCTL = (timer->EXTCTL & ~TIMER_EXTCTL_CNTPHASE_Msk) | u32Edge;
211 timer->CTL |= TIMER_CTL_EXTCNTEN_Msk;
212 }
213
223 void TIMER_DisableEventCounter(TIMER_T *timer)
224 {
225 timer->CTL &= ~TIMER_CTL_EXTCNTEN_Msk;
226 }
227
238 uint32_t TIMER_GetModuleClock(TIMER_T *timer)
239 {
240 uint32_t u32Src, u32Clk;
241 const uint32_t au32Clk[] = {__HXT, __LXT, 0UL, 0UL, 0UL, __LIRC, 0UL, __HIRC};
242
243 if(timer == TIMER0)
244 {
245 u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR0SEL_Msk) >> CLK_CLKSEL1_TMR0SEL_Pos;
246 }
247 else if(timer == TIMER1)
248 {
249 u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR1SEL_Msk) >> CLK_CLKSEL1_TMR1SEL_Pos;
250 }
251 else if(timer == TIMER2)
252 {
253 u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR2SEL_Msk) >> CLK_CLKSEL1_TMR2SEL_Pos;
254 }
255 else /* Timer 3 */
256 {
257 u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR3SEL_Msk) >> CLK_CLKSEL1_TMR3SEL_Pos;
258 }
259
260 if(u32Src == 2UL)
261 {
262 if((timer == TIMER0) || (timer == TIMER1))
263 {
264 u32Clk = CLK_GetPCLK0Freq();
265 }
266 else
267 {
268 u32Clk = CLK_GetPCLK1Freq();
269 }
270 }
271 else
272 {
273 u32Clk = au32Clk[u32Src];
274 }
275
276 return u32Clk;
277 }
278
279
280
294 void TIMER_EnableFreqCounter(TIMER_T *timer,
295 uint32_t u32DropCount,
296 uint32_t u32Timeout,
297 uint32_t u32EnableInt)
298 {
299 TIMER_T *t; /* store the timer base to configure compare value */
300
301 t = (timer == TIMER0) ? TIMER1 : TIMER3;
302
303 t->CMP = 0xFFFFFFUL;
304 t->EXTCTL = u32EnableInt ? TIMER_EXTCTL_CAPIEN_Msk : 0UL;
305 timer->CTL = TIMER_CTL_INTRGEN_Msk | TIMER_CTL_CNTEN_Msk;
306
307 return;
308 }
314 void TIMER_DisableFreqCounter(TIMER_T *timer)
315 {
316 timer->CTL &= ~TIMER_CTL_INTRGEN_Msk;
317 }
318
319
328 void TIMER_SetTriggerSource(TIMER_T *timer, uint32_t u32Src)
329 {
330 timer->TRGCTL = (timer->TRGCTL & ~TIMER_TRGCTL_TRGSSEL_Msk) | u32Src;
331 }
332
343 void TIMER_SetTriggerTarget(TIMER_T *timer, uint32_t u32Mask)
344 {
345 timer->TRGCTL = (timer->TRGCTL & ~(TIMER_TRGCTL_TRGEPWM_Msk | TIMER_TRGCTL_TRGDAC_Msk | TIMER_TRGCTL_TRGEADC_Msk | TIMER_TRGCTL_TRGPDMA_Msk)) | u32Mask;
346 }
347 /* end of group TIMER_EXPORTED_FUNCTIONS */
349 /* end of group TIMER_Driver */
351 /* end of group Standard_Driver */
353
TIMER_EXTCTL_CAPIEN_Msk
#define TIMER_EXTCTL_CAPIEN_Msk
Definition: timer_reg.h:1716
TIMER_T::CTL
__IO uint32_t CTL
Definition: timer_reg.h:1606
CLK_CLKSEL1_TMR3SEL_Msk
#define CLK_CLKSEL1_TMR3SEL_Msk
Definition: clk_reg.h:2538
CLK
#define CLK
Definition: M480.h:368
TIMER_EnableEventCounter
void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge)
Enable Timer Counter Function.
Definition: timer.c:208
TIMER_SetTriggerSource
void TIMER_SetTriggerSource(TIMER_T *timer, uint32_t u32Src)
This function is used to select the interrupt source used to trigger other modules.
Definition: timer.c:328
TIMER1
#define TIMER1
Definition: M480.h:412
TIMER_CTL_INTRGEN_Msk
#define TIMER_CTL_INTRGEN_Msk
Definition: timer_reg.h:1656
TIMER_SetTriggerTarget
void TIMER_SetTriggerTarget(TIMER_T *timer, uint32_t u32Mask)
This function is used to set modules trigger by timer interrupt.
Definition: timer.c:343
TIMER_CTL_EXTCNTEN_Msk
#define TIMER_CTL_EXTCNTEN_Msk
Definition: timer_reg.h:1671
TIMER_Open
uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq)
Open Timer with Operate Mode and Frequency.
Definition: timer.c:41
TIMER3
#define TIMER3
Definition: M480.h:414
TIMER_GetModuleClock
uint32_t TIMER_GetModuleClock(TIMER_T *timer)
Get Timer Clock Frequency.
Definition: timer.c:238
CLK_CLKSEL1_TMR2SEL_Msk
#define CLK_CLKSEL1_TMR2SEL_Msk
Definition: clk_reg.h:2535
TIMER_Delay
void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec)
Create a specify Delay Time.
Definition: timer.c:92
TIMER_CTL_ACTSTS_Msk
#define TIMER_CTL_ACTSTS_Msk
Definition: timer_reg.h:1674
TIMER0
#define TIMER0
Definition: M480.h:411
NuMicro.h
NuMicro peripheral access layer header file.
TIMER_TRGCTL_TRGSSEL_Msk
#define TIMER_TRGCTL_TRGSSEL_Msk
Definition: timer_reg.h:1740
TIMER_EnableFreqCounter
void TIMER_EnableFreqCounter(TIMER_T *timer, uint32_t u32DropCount, uint32_t u32Timeout, uint32_t u32EnableInt)
This function is used to enable the Timer frequency counter function.
Definition: timer.c:294
TIMER_DisableFreqCounter
void TIMER_DisableFreqCounter(TIMER_T *timer)
This function is used to disable the Timer frequency counter function.
Definition: timer.c:314
TIMER_ONESHOT_MODE
#define TIMER_ONESHOT_MODE
Definition: timer.h:32
TIMER_EnableCapture
void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge)
Enable Timer Capture Function.
Definition: timer.c:174
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_M480.c:21
TIMER_T::TRGCTL
__IO uint32_t TRGCTL
Definition: timer_reg.h:1613
TIMER_T::EXTCTL
__IO uint32_t EXTCTL
Definition: timer_reg.h:1611
CLK_GetPCLK1Freq
uint32_t CLK_GetPCLK1Freq(void)
Get PCLK1 frequency.
Definition: clk.c:204
TIMER_EXTCTL_CAPFUNCS_Msk
#define TIMER_EXTCTL_CAPFUNCS_Msk
Definition: timer_reg.h:1713
CLK_CLKSEL1_TMR2SEL_Pos
#define CLK_CLKSEL1_TMR2SEL_Pos
Definition: clk_reg.h:2534
CLK_GetPCLK0Freq
uint32_t CLK_GetPCLK0Freq(void)
Get PCLK0 frequency.
Definition: clk.c:164
TIMER_T::CMP
__IO uint32_t CMP
Definition: timer_reg.h:1607
CLK_CLKSEL1_TMR1SEL_Msk
#define CLK_CLKSEL1_TMR1SEL_Msk
Definition: clk_reg.h:2532
TIMER_DisableEventCounter
void TIMER_DisableEventCounter(TIMER_T *timer)
Disable Timer Counter Function.
Definition: timer.c:223
CLK_CLKSEL1_TMR3SEL_Pos
#define CLK_CLKSEL1_TMR3SEL_Pos
Definition: clk_reg.h:2537
__LIRC
#define __LIRC
Definition: system_M480.h:37
TIMER_EXTCTL_CAPEN_Msk
#define TIMER_EXTCTL_CAPEN_Msk
Definition: timer_reg.h:1710
TIMER_DisableCapture
void TIMER_DisableCapture(TIMER_T *timer)
Disable Timer Capture Function.
Definition: timer.c:189
CLK_CLKSEL1_TMR0SEL_Msk
#define CLK_CLKSEL1_TMR0SEL_Msk
Definition: clk_reg.h:2529
TIMER_Close
void TIMER_Close(TIMER_T *timer)
Stop Timer Counting.
Definition: timer.c:74
TIMER_TRGCTL_TRGPDMA_Msk
#define TIMER_TRGCTL_TRGPDMA_Msk
Definition: timer_reg.h:1752
TIMER_EXTCTL_CNTPHASE_Msk
#define TIMER_EXTCTL_CNTPHASE_Msk
Definition: timer_reg.h:1707
TIMER_T
Definition: timer_reg.h:26
__LXT
#define __LXT
Definition: system_M480.h:33
__HIRC
#define __HIRC
Definition: system_M480.h:36
CLK_CLKSEL1_TMR0SEL_Pos
#define CLK_CLKSEL1_TMR0SEL_Pos
Definition: clk_reg.h:2528
TIMER2
#define TIMER2
Definition: M480.h:413
TIMER_TRGCTL_TRGEADC_Msk
#define TIMER_TRGCTL_TRGEADC_Msk
Definition: timer_reg.h:1746
TIMER_TRGCTL_TRGEPWM_Msk
#define TIMER_TRGCTL_TRGEPWM_Msk
Definition: timer_reg.h:1743
TIMER_TRGCTL_TRGDAC_Msk
#define TIMER_TRGCTL_TRGDAC_Msk
Definition: timer_reg.h:1749
TIMER_CTL_CNTEN_Msk
#define TIMER_CTL_CNTEN_Msk
Definition: timer_reg.h:1683
CLK_CLKSEL1_TMR1SEL_Pos
#define CLK_CLKSEL1_TMR1SEL_Pos
Definition: clk_reg.h:2531
__HXT
#define __HXT
Definition: system_M480.h:29
TIMER_EXTCTL_CAPEDGE_Msk
#define TIMER_EXTCTL_CAPEDGE_Msk
Definition: timer_reg.h:1728