Mini51 BSP  V3.02.002
The Board Support Package for Mini51 Series
timer.c
Go to the documentation of this file.
1 /**************************************************************************/
12 #include "Mini51Series.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 |
151  u32CapMode | u32Edge | TIMER_TEXCON_TEXEN_Msk;
152 }
153 
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 
186 {
187  timer->TCSR &= ~TIMER_TCSR_CTB_Msk;
188 }
189 
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. ***/
#define CLK_CLKSEL1_TMR1_S_Msk
#define TIMER_TEXCON_RSTCAPSEL_Msk
#define CLK_CLKSEL1_TMR1_S_Pos
__IO uint32_t TEXCON
void TIMER_Close(TIMER_T *timer)
This API stops Timer counting and disable the Timer interrupt function.
Definition: timer.c:73
#define __XTAL12M
#define TIMER_TCSR_CACT_Msk
Mini51 series peripheral access layer header file. This file contains all the peripheral register's d...
#define TIMER_TCSR_CEN_Msk
#define TIMER_TEXCON_TEX_EDGE_Msk
void TIMER_DisableCapture(TIMER_T *timer)
This API is used to disable the Timer capture function.
Definition: timer.c:159
uint32_t TIMER_GetModuleClock(TIMER_T *timer)
This API is used to get the clock frequency of Timer.
Definition: timer.c:196
#define TIMER0
Pointer to Timer 0 register structure.
#define TIMER_TCSR_CTB_Msk
#define __IRC10K
#define TIMER_TEXCON_TX_PHASE_Msk
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
#define CLK_CLKSEL1_TMR0_S_Pos
#define CLK_PWRCON_XTLCLK_EN_Msk
#define __XTAL32K
__IO uint32_t TCMPR
#define TIMER_TEXCON_TEXEN_Msk
__IO uint32_t TCSR
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
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
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
#define CLK
Pointer to CLK register structure.
#define CLK_CLKSEL1_TMR0_S_Msk
#define __IRC22M
#define TIMER_TEXCON_CAP_MODE_Msk
uint32_t SystemCoreClock
void TIMER_DisableEventCounter(TIMER_T *timer)
This API is used to disable the Timer event counter function.
Definition: timer.c:185