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