1 //*****************************************************************************
2 //
3 // systick.c - Driver for the SysTick timer in NVIC.
4 //
5 // Copyright (c) 2005-2012 Texas Instruments Incorporated.  All rights reserved.
6 // Software License Agreement
7 //
8 //   Redistribution and use in source and binary forms, with or without
9 //   modification, are permitted provided that the following conditions
10 //   are met:
11 //
12 //   Redistributions of source code must retain the above copyright
13 //   notice, this list of conditions and the following disclaimer.
14 //
15 //   Redistributions in binary form must reproduce the above copyright
16 //   notice, this list of conditions and the following disclaimer in the
17 //   documentation and/or other materials provided with the
18 //   distribution.
19 //
20 //   Neither the name of Texas Instruments Incorporated nor the names of
21 //   its contributors may be used to endorse or promote products derived
22 //   from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 //
36 // This is part of revision 9453 of the Stellaris Peripheral Driver Library.
37 //
38 //*****************************************************************************
39 
40 //*****************************************************************************
41 //
42 //! \addtogroup systick_api
43 //! @{
44 //
45 //*****************************************************************************
46 
47 #include "inc/hw_ints.h"
48 #include "inc/hw_nvic.h"
49 #include "inc/hw_types.h"
50 #include "driverlib/debug.h"
51 #include "driverlib/interrupt.h"
52 #include "driverlib/systick.h"
53 
54 //*****************************************************************************
55 //
56 //! Enables the SysTick counter.
57 //!
58 //! This function starts the SysTick counter.  If an interrupt handler has been
59 //! registered, it is called when the SysTick counter rolls over.
60 //!
61 //! \note Calling this function causes the SysTick counter to (re)commence
62 //! counting from its current value.  The counter is not automatically reloaded
63 //! with the period as specified in a previous call to SysTickPeriodSet().  If
64 //! an immediate reload is required, the \b NVIC_ST_CURRENT register must be
65 //! written to force the reload.  Any write to this register clears the SysTick
66 //! counter to 0 and causes a reload with the supplied period on the next
67 //! clock.
68 //!
69 //! \return None.
70 //
71 //*****************************************************************************
72 void
SysTickEnable(void)73 SysTickEnable(void)
74 {
75     //
76     // Enable SysTick.
77     //
78     HWREG(NVIC_ST_CTRL) |= NVIC_ST_CTRL_CLK_SRC | NVIC_ST_CTRL_ENABLE;
79 }
80 
81 //*****************************************************************************
82 //
83 //! Disables the SysTick counter.
84 //!
85 //! This function stops the SysTick counter.  If an interrupt handler has been
86 //! registered, it is not called until SysTick is restarted.
87 //!
88 //! \return None.
89 //
90 //*****************************************************************************
91 void
SysTickDisable(void)92 SysTickDisable(void)
93 {
94     //
95     // Disable SysTick.
96     //
97     HWREG(NVIC_ST_CTRL) &= ~(NVIC_ST_CTRL_ENABLE);
98 }
99 
100 //*****************************************************************************
101 //
102 //! Registers an interrupt handler for the SysTick interrupt.
103 //!
104 //! \param pfnHandler is a pointer to the function to be called when the
105 //! SysTick interrupt occurs.
106 //!
107 //! This function registers the handler to be called when a SysTick interrupt
108 //! occurs.
109 //!
110 //! \sa IntRegister() for important information about registering interrupt
111 //! handlers.
112 //!
113 //! \return None.
114 //
115 //*****************************************************************************
116 void
SysTickIntRegister(void (* pfnHandler)(void))117 SysTickIntRegister(void (*pfnHandler)(void))
118 {
119     //
120     // Register the interrupt handler, returning an error if an error occurs.
121     //
122     IntRegister(FAULT_SYSTICK, pfnHandler);
123 
124     //
125     // Enable the SysTick interrupt.
126     //
127     HWREG(NVIC_ST_CTRL) |= NVIC_ST_CTRL_INTEN;
128 }
129 
130 //*****************************************************************************
131 //
132 //! Unregisters the interrupt handler for the SysTick interrupt.
133 //!
134 //! This function unregisters the handler to be called when a SysTick interrupt
135 //! occurs.
136 //!
137 //! \sa IntRegister() for important information about registering interrupt
138 //! handlers.
139 //!
140 //! \return None.
141 //
142 //*****************************************************************************
143 void
SysTickIntUnregister(void)144 SysTickIntUnregister(void)
145 {
146     //
147     // Disable the SysTick interrupt.
148     //
149     HWREG(NVIC_ST_CTRL) &= ~(NVIC_ST_CTRL_INTEN);
150 
151     //
152     // Unregister the interrupt handler.
153     //
154     IntUnregister(FAULT_SYSTICK);
155 }
156 
157 //*****************************************************************************
158 //
159 //! Enables the SysTick interrupt.
160 //!
161 //! This function enables the SysTick interrupt, allowing it to be
162 //! reflected to the processor.
163 //!
164 //! \note The SysTick interrupt handler is not required to clear the SysTick
165 //! interrupt source because it is cleared automatically by the NVIC when the
166 //! interrupt handler is called.
167 //!
168 //! \return None.
169 //
170 //*****************************************************************************
171 void
SysTickIntEnable(void)172 SysTickIntEnable(void)
173 {
174     //
175     // Enable the SysTick interrupt.
176     //
177     HWREG(NVIC_ST_CTRL) |= NVIC_ST_CTRL_INTEN;
178 }
179 
180 //*****************************************************************************
181 //
182 //! Disables the SysTick interrupt.
183 //!
184 //! This function disables the SysTick interrupt, preventing it from being
185 //! reflected to the processor.
186 //!
187 //! \return None.
188 //
189 //*****************************************************************************
190 void
SysTickIntDisable(void)191 SysTickIntDisable(void)
192 {
193     //
194     // Disable the SysTick interrupt.
195     //
196     HWREG(NVIC_ST_CTRL) &= ~(NVIC_ST_CTRL_INTEN);
197 }
198 
199 //*****************************************************************************
200 //
201 //! Sets the period of the SysTick counter.
202 //!
203 //! \param ulPeriod is the number of clock ticks in each period of the SysTick
204 //! counter and must be between 1 and 16,777,216, inclusive.
205 //!
206 //! This function sets the rate at which the SysTick counter wraps, which
207 //! equates to the number of processor clocks between interrupts.
208 //!
209 //! \note Calling this function does not cause the SysTick counter to reload
210 //! immediately.  If an immediate reload is required, the \b NVIC_ST_CURRENT
211 //! register must be written.  Any write to this register clears the SysTick
212 //! counter to 0 and causes a reload with the \e ulPeriod supplied here on
213 //! the next clock after SysTick is enabled.
214 //!
215 //! \return None.
216 //
217 //*****************************************************************************
218 void
SysTickPeriodSet(unsigned long ulPeriod)219 SysTickPeriodSet(unsigned long ulPeriod)
220 {
221     //
222     // Check the arguments.
223     //
224     ASSERT((ulPeriod > 0) && (ulPeriod <= 16777216));
225 
226     //
227     // Set the period of the SysTick counter.
228     //
229     HWREG(NVIC_ST_RELOAD) = ulPeriod - 1;
230 }
231 
232 //*****************************************************************************
233 //
234 //! Gets the period of the SysTick counter.
235 //!
236 //! This function returns the rate at which the SysTick counter wraps, which
237 //! equates to the number of processor clocks between interrupts.
238 //!
239 //! \return Returns the period of the SysTick counter.
240 //
241 //*****************************************************************************
242 unsigned long
SysTickPeriodGet(void)243 SysTickPeriodGet(void)
244 {
245     //
246     // Return the period of the SysTick counter.
247     //
248     return(HWREG(NVIC_ST_RELOAD) + 1);
249 }
250 
251 //*****************************************************************************
252 //
253 //! Gets the current value of the SysTick counter.
254 //!
255 //! This function returns the current value of the SysTick counter, which is
256 //! a value between the period - 1 and zero, inclusive.
257 //!
258 //! \return Returns the current value of the SysTick counter.
259 //
260 //*****************************************************************************
261 unsigned long
SysTickValueGet(void)262 SysTickValueGet(void)
263 {
264     //
265     // Return the current value of the SysTick counter.
266     //
267     return(HWREG(NVIC_ST_CURRENT));
268 }
269 
270 //*****************************************************************************
271 //
272 // Close the Doxygen group.
273 //! @}
274 //
275 //*****************************************************************************
276