uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lpc17xx_systick.c
Go to the documentation of this file.
1 /***********************************************************************/
20 /* Peripheral group ----------------------------------------------------------- */
25 /* Includes ------------------------------------------------------------------- */
26 #include "lpc17xx_systick.h"
27 #include "lpc17xx_clkpwr.h"
28 
29 /* If this source file built with example, the LPC17xx FW library configuration
30  * file in each example directory ("lpc17xx_libcfg.h") must be included,
31  * otherwise the default FW library configuration file must be included instead
32  */
33 #ifdef __BUILD_WITH_EXAMPLE__
34 #include "lpc17xx_libcfg.h"
35 #else
36 #include "lpc17xx_libcfg_default.h"
37 #endif /* __BUILD_WITH_EXAMPLE__ */
38 
39 
40 #ifdef _SYSTICK
41 
42 /* Public Functions ----------------------------------------------------------- */
46 /*********************************************************************/
51 void SYSTICK_InternalInit(uint32_t time)
52 {
53  uint32_t cclk;
54  float maxtime;
55 
56  cclk = SystemCoreClock;
57  /* With internal CPU clock frequency for LPC17xx is 'SystemCoreClock'
58  * And limit 24 bit for RELOAD value
59  * So the maximum time can be set:
60  * 1/SystemCoreClock * (2^24) * 1000 (ms)
61  */
62  //check time value is available or not
63  maxtime = (1<<24)/(SystemCoreClock / 1000) ;
64  if(time > maxtime)
65  //Error loop
66  while(1);
67  else
68  {
69  //Select CPU clock is System Tick clock source
70  SysTick->CTRL |= ST_CTRL_CLKSOURCE;
71  /* Set RELOAD value
72  * RELOAD = (SystemCoreClock/1000) * time - 1
73  * with time base is millisecond
74  */
75  SysTick->RELOAD = (cclk/1000)*time - 1;
76  }
77 }
78 
79 /*********************************************************************/
85 void SYSTICK_ExternalInit(uint32_t freq, uint32_t time)
86 {
87  float maxtime;
88 
89  /* With external clock frequency for LPC17xx is 'freq'
90  * And limit 24 bit for RELOAD value
91  * So the maximum time can be set:
92  * 1/freq * (2^24) * 1000 (ms)
93  */
94  //check time value is available or not
95  maxtime = (1<<24)/(freq / 1000) ;
96  if (time>maxtime)
97  //Error Loop
98  while(1);
99  else
100  {
101  //Select external clock is System Tick clock source
102  SysTick->CTRL &= ~ ST_CTRL_CLKSOURCE;
103  /* Set RELOAD value
104  * RELOAD = (freq/1000) * time - 1
105  * with time base is millisecond
106  */
107  maxtime = (freq/1000)*time - 1;
108  SysTick->RELOAD = (freq/1000)*time - 1;
109  }
110 }
111 
112 /*********************************************************************/
119 void SYSTICK_Cmd(FunctionalState NewState)
120 {
122 
123  if(NewState == ENABLE)
124  //Enable System Tick counter
125  SysTick->CTRL |= ST_CTRL_ENABLE;
126  else
127  //Disable System Tick counter
128  SysTick->CTRL &= ~ST_CTRL_ENABLE;
129 }
130 
131 /*********************************************************************/
138 void SYSTICK_IntCmd(FunctionalState NewState)
139 {
141 
142  if(NewState == ENABLE)
143  //Enable System Tick counter
144  SysTick->CTRL |= ST_CTRL_TICKINT;
145  else
146  //Disable System Tick counter
147  SysTick->CTRL &= ~ST_CTRL_TICKINT;
148 }
149 
150 /*********************************************************************/
155 uint32_t SYSTICK_GetCurrentValue(void)
156 {
157  return (SysTick->CURR);
158 }
159 
160 /*********************************************************************/
165 void SYSTICK_ClearCounterFlag(void)
166 {
167  SysTick->CTRL &= ~ST_CTRL_COUNTFLAG;
168 }
173 #endif /* _SYSTICK */
174 
179 /* --------------------------------- End Of File ------------------------------ */
180