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