1 /******************************************************************************
2 * Filename: systick.h
3 * Revised: 2017-05-23 12:08:52 +0200 (Tue, 23 May 2017)
4 * Revision: 49048
5 *
6 * Description: Prototypes for the SysTick driver.
7 *
8 * Copyright (c) 2015 - 2020, 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 "debug.h"
68 #include "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 // Enable SysTick.
98 HWREG(NVIC_ST_CTRL) |= NVIC_ST_CTRL_CLK_SRC | NVIC_ST_CTRL_ENABLE;
99 }
100
101 //*****************************************************************************
102 //
103 //! \brief Disables the SysTick counter.
104 //!
105 //! This will stop the SysTick counter. If an interrupt handler has been
106 //! registered, it will no longer be called until SysTick is restarted.
107 //!
108 //! \return None
109 //
110 //*****************************************************************************
111 __STATIC_INLINE void
SysTickDisable(void)112 SysTickDisable(void)
113 {
114 // Disable SysTick.
115 HWREG(NVIC_ST_CTRL) &= ~(NVIC_ST_CTRL_ENABLE);
116 }
117
118 //*****************************************************************************
119 //
120 //! \brief Registers an interrupt handler for the SysTick interrupt in the dynamic interrupt table.
121 //!
122 //! \note Only use this function if you want to use the dynamic vector table (in SRAM)!
123 //!
124 //! This function registers a function as the interrupt handler for a specific
125 //! interrupt and enables the corresponding interrupt in the interrupt controller.
126 //!
127 //! \param pfnHandler is a pointer to the function to be called when the
128 //! SysTick interrupt occurs.
129 //!
130 //! \return None
131 //!
132 //! \sa \ref IntRegister() for important information about registering interrupt
133 //! handlers.
134 //
135 //*****************************************************************************
136 __STATIC_INLINE void
SysTickIntRegister(void (* pfnHandler)(void))137 SysTickIntRegister(void (*pfnHandler)(void))
138 {
139 // Register the interrupt handler, returning an error if an error occurs.
140 IntRegister(INT_SYSTICK, pfnHandler);
141
142 // Enable the SysTick interrupt.
143 HWREG(NVIC_ST_CTRL) |= NVIC_ST_CTRL_INTEN;
144 }
145
146 //*****************************************************************************
147 //
148 //! \brief Unregisters the interrupt handler for the SysTick interrupt in the dynamic interrupt table.
149 //!
150 //! This function will clear the handler to be called when a SysTick interrupt
151 //! occurs.
152 //!
153 //! \return None
154 //!
155 //! \sa \ref IntRegister() for important information about registering interrupt
156 //! handlers.
157 //
158 //*****************************************************************************
159 __STATIC_INLINE void
SysTickIntUnregister(void)160 SysTickIntUnregister(void)
161 {
162 // Disable the SysTick interrupt.
163 HWREG(NVIC_ST_CTRL) &= ~(NVIC_ST_CTRL_INTEN);
164
165 // Unregister the interrupt handler.
166 IntUnregister(INT_SYSTICK);
167 }
168
169 //*****************************************************************************
170 //
171 //! \brief Enables the SysTick interrupt.
172 //!
173 //! This function will enable the SysTick interrupt, allowing it to be
174 //! reflected to the processor.
175 //!
176 //! \note The SysTick interrupt handler does not need to clear the SysTick
177 //! interrupt source as this is done automatically by NVIC when the interrupt
178 //! handler is called.
179 //!
180 //! \return None
181 //
182 //*****************************************************************************
183 __STATIC_INLINE void
SysTickIntEnable(void)184 SysTickIntEnable(void)
185 {
186 // Enable the SysTick interrupt.
187 HWREG(NVIC_ST_CTRL) |= NVIC_ST_CTRL_INTEN;
188 }
189
190 //*****************************************************************************
191 //
192 //! \brief Disables the SysTick interrupt.
193 //!
194 //! This function will disable the SysTick interrupt, preventing it from being
195 //! reflected to the processor.
196 //!
197 //! \return None
198 //
199 //*****************************************************************************
200 __STATIC_INLINE void
SysTickIntDisable(void)201 SysTickIntDisable(void)
202 {
203 // Disable the SysTick interrupt.
204 HWREG(NVIC_ST_CTRL) &= ~(NVIC_ST_CTRL_INTEN);
205 }
206
207 //*****************************************************************************
208 //
209 //! \brief Sets the period of the SysTick counter.
210 //!
211 //! This function sets the rate at which the SysTick counter wraps; this
212 //! equals to the number of processor clocks between interrupts.
213 //!
214 //! \note Calling this function does not cause the SysTick counter to reload
215 //! immediately. If an immediate reload is required, the NVIC_ST_CURRENT
216 //! register must be written. Any write to this register clears the SysTick
217 //! counter to 0 and will cause a reload with the \c ui32Period supplied here
218 //! on the next clock after the SysTick is enabled.
219 //!
220 //! \param ui32Period is the number of clock ticks in each period of the
221 //! SysTick counter; must be between 1 and 16,777,216 (0x1000000), both included.
222 //!
223 //! \return None
224 //
225 //*****************************************************************************
226 __STATIC_INLINE void
SysTickPeriodSet(uint32_t ui32Period)227 SysTickPeriodSet(uint32_t ui32Period)
228 {
229 // Check the arguments.
230 ASSERT((ui32Period > 0) && (ui32Period <= 16777216));
231
232 // Set the period of the SysTick counter.
233 HWREG(NVIC_ST_RELOAD) = ui32Period - 1;
234 }
235
236 //*****************************************************************************
237 //
238 //! \brief Gets the period of the SysTick counter.
239 //!
240 //! This function returns the rate at which the SysTick counter wraps; this
241 //! equals to the number of processor clocks between interrupts.
242 //!
243 //! \return Returns the period of the SysTick counter.
244 //
245 //*****************************************************************************
246 __STATIC_INLINE uint32_t
SysTickPeriodGet(void)247 SysTickPeriodGet(void)
248 {
249 // Return the period of the SysTick counter.
250 return(HWREG(NVIC_ST_RELOAD) + 1);
251 }
252
253 //*****************************************************************************
254 //
255 //! \brief Gets the current value of the SysTick counter.
256 //!
257 //! This function returns the current value of the SysTick counter; this will
258 //! be a value between the (period - 1) and zero, both included.
259 //!
260 //! \return Returns the current value of the SysTick counter
261 //
262 //*****************************************************************************
263 __STATIC_INLINE uint32_t
SysTickValueGet(void)264 SysTickValueGet(void)
265 {
266 // Return the current value of the SysTick counter.
267 return(HWREG(NVIC_ST_CURRENT));
268 }
269
270 //*****************************************************************************
271 //
272 // Mark the end of the C bindings section for C++ compilers.
273 //
274 //*****************************************************************************
275 #ifdef __cplusplus
276 }
277 #endif
278
279 #endif // __SYSTICK_H__
280
281 //*****************************************************************************
282 //
283 //! Close the Doxygen group
284 //! @}
285 //! @}
286 //
287 //*****************************************************************************
288