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