| MINI51DE_BSP V3.02.003 The Board Support Package for Mini51DE Series MCU

timer.c
Go to the documentation of this file.
1/**************************************************************************/
12#include "Mini51Series.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 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
196uint32_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 MINI51_TIMER_EXPORTED_FUNCTIONS */
216 /* end of group MINI51_TIMER_Driver */
218 /* end of group MINI51_Device_Driver */
220
221/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
Mini51Series.h
Mini51 series peripheral access layer header file. This file contains all the peripheral register's d...
TIMER_TEXCON_RSTCAPSEL_Msk
#define TIMER_TEXCON_RSTCAPSEL_Msk
Definition: Mini51Series.h:4902
TIMER_TCSR_CTB_Msk
#define TIMER_TCSR_CTB_Msk
Definition: Mini51Series.h:4857
TIMER_TEXCON_TEX_EDGE_Msk
#define TIMER_TEXCON_TEX_EDGE_Msk
Definition: Mini51Series.h:4896
TIMER_TCSR_CACT_Msk
#define TIMER_TCSR_CACT_Msk
Definition: Mini51Series.h:4860
TIMER_TEXCON_CAP_MODE_Msk
#define TIMER_TEXCON_CAP_MODE_Msk
Definition: Mini51Series.h:4914
TIMER_TCSR_CEN_Msk
#define TIMER_TCSR_CEN_Msk
Definition: Mini51Series.h:4872
TIMER_TEXCON_TEXEN_Msk
#define TIMER_TEXCON_TEXEN_Msk
Definition: Mini51Series.h:4899
CLK_PWRCON_XTLCLK_EN_Msk
#define CLK_PWRCON_XTLCLK_EN_Msk
Definition: Mini51Series.h:867
CLK_CLKSEL1_TMR1_S_Msk
#define CLK_CLKSEL1_TMR1_S_Msk
Definition: Mini51Series.h:980
CLK_CLKSEL1_TMR0_S_Pos
#define CLK_CLKSEL1_TMR0_S_Pos
Definition: Mini51Series.h:976
CLK_CLKSEL1_TMR0_S_Msk
#define CLK_CLKSEL1_TMR0_S_Msk
Definition: Mini51Series.h:977
CLK_CLKSEL1_TMR1_S_Pos
#define CLK_CLKSEL1_TMR1_S_Pos
Definition: Mini51Series.h:979
CLK
#define CLK
Pointer to CLK register structure.
Definition: Mini51Series.h:5736
TIMER0
#define TIMER0
Pointer to Timer 0 register structure.
Definition: Mini51Series.h:5723
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:159
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:185
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_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_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
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: Mini51Series.h:4820
TIMER_T::TEXCON
__IO uint32_t TEXCON
Definition: Mini51Series.h:4824
TIMER_T::TCSR
__IO uint32_t TCSR
Definition: Mini51Series.h:4819
TIMER_T
Definition: Mini51Series.h:4662
__IRC10K
#define __IRC10K
Definition: system_Mini51Series.h:32
__IRC22M
#define __IRC22M
Definition: system_Mini51Series.h:31
__XTAL32K
#define __XTAL32K
Definition: system_Mini51Series.h:30
__XTAL12M
#define __XTAL12M
Definition: system_Mini51Series.h:29
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_Mini51Series.c:20