1 /***************************************************************************//**
2 * \file cy_systick.h
3 * \version 1.70.1
4 *
5 * Provides the API declarations of the SysTick driver.
6 *
7 ********************************************************************************
8 * \copyright
9 * Copyright (c) (2016-2022), Cypress Semiconductor Corporation (an Infineon company) or
10 * an affiliate of Cypress Semiconductor Corporation.
11 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 *     http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *******************************************************************************/
25 
26 #ifndef CY_SYSTICK_H
27 #define CY_SYSTICK_H
28 
29 /**
30 * \addtogroup group_arm_system_timer
31 * \{
32 * Provides vendor-specific SysTick API.
33 *
34 * The functions and other declarations used in this driver are in cy_systick.h.
35 * You can include cy_pdl.h to get access to all functions and declarations in the PDL.
36 *
37 * The SysTick timer is part of the CPU. The timer is a down counter with a 24-bit reload/tick value that is clocked by
38 * the FastClk/SlowClk. The timer has the capability to generate an interrupt when the set number of ticks expires and
39 * the counter is reloaded. This interrupt is available as part of the Nested Vectored Interrupt Controller (NVIC) for
40 * service by the CPU and can be used for general-purpose timing control in user code.
41 *
42 * The timer is independent of the CPU (except for the clock), which is useful in applications requiring
43 * precise timing that do not have a dedicated timer/counter available for the job.
44 *
45 * \section group_systick_configuration Configuration Considerations
46 *
47 * The \ref Cy_SysTick_Init() performs all required driver's initialization and enables the timer. The function accepts
48 * two parameters: clock source \ref cy_en_systick_clock_source_t and the timer interval. You must ensure
49 * the selected clock source for SysTick is enabled.
50 * The callbacks can be registered/unregistered any time after \ref Cy_SysTick_Init() by calling
51 * \ref Cy_SysTick_SetCallback().
52 *
53 * Changing the SysTick clock source and/or its frequency will change the interrupt interval and therefore
54 * \ref Cy_SysTick_SetReload() should be called to compensate for this change.
55 *
56 * \section group_systick_more_information More Information
57 *
58 * Refer to the SysTick section of the ARM reference guide for complete details on the registers and their use.
59 * See also the "CPU Subsystem (CPUSS)" chapter of the device technical reference manual (TRM).
60 *
61 * \section group_systick_changelog Changelog
62 *
63 * <table class="doxtable">
64 * <tr><th>Version</th><th>Changes</th><th>Reason for Change</th></tr>
65 *    <tr>
66 *     <td>1.70.1</td>
67 *     <td>Updated driver guards.<br>
68 *     <td>Bug fixes.</td>
69 *   </tr>
70 *   <tr>
71 *     <td>1.70</td>
72 *     <td>Macro value change and enhancements.<br>
73 *     <td>Bug fix and Enhancements.</td>
74 *   </tr>
75 *   <tr>
76 *     <td>1.60</td>
77 *     <td>Bug fix and Enhancements.<br>
78 *     <td>Bug fix and Enhancements.</td>
79 *   </tr>
80 *   <tr>
81 *     <td>1.50</td>
82 *     <td>CAT1C, CAT1D devices support.<br>
83 *         Updated Cy_SysTick_Init() to use Cy_SysInt_SetVector() API to register the callback function and removed invalid define CY_SYSTICK_IRQ_NUM.</td>
84 *     <td>Support for new devices.</td>
85 *   </tr>
86 *   <tr>
87 *     <td>1.40</td>
88 *     <td>Support for CM33.</td>
89 *     <td>New devices support.</td>
90 *   </tr>
91 * <tr>
92 *   <td rowspan="2">1.30</td>
93 *     <td>Added function parameter checks.</td>
94 *     <td>Improved the debugging capability.</td>
95 * </tr>
96 * <tr>
97 *     <td>Minor documentation updates.</td>
98 *     <td>Documentation enhancement.</td>
99 * </tr>
100 * <tr>
101 *   <td rowspan="2">1.20</td>
102 *     <td>Updated Cy_SysTick_SetClockSource() for the PSoC 64 devices,
103 *         so that passing any other value than CY_SYSTICK_CLOCK_SOURCE_CLK_CPU
104 *         will not affect clock source and it will be as
105 *         \ref Cy_SysTick_GetClockSource() reports.</td>
106 *     <td>Added PSoC 64 devices support.</td>
107 * </tr>
108 * <tr>
109 *     <td>Minor documentation updates.</td>
110 *     <td>Documentation enhancement.</td>
111 * </tr>
112 * <tr>
113 *   <td>1.10.1</td>
114 *     <td>Updated include files.</td>
115 *     <td>Improve pdl usability.</td>
116 * </tr>
117 * <tr>
118 *   <td rowspan="2">1.10</td>
119 *     <td>Flattened the organization of the driver source code into the single
120 *         source directory and the single include directory.
121 *     </td>
122 *     <td>Driver library directory-structure simplification.</td>
123 * </tr>
124 *   <tr>
125 *     <td>Added register access layer. Use register access macros instead
126 *         of direct register access using dereferenced pointers.</td>
127 *     <td>Makes register access device-independent, so that the PDL does
128 *         not need to be recompiled for each supported part number.</td>
129 *   </tr>
130 * <tr>
131 * <td>1.0.1</td>
132 * <td>Fixed a warning issued when the compilation of C++ source code was
133 *     enabled.</td>
134 * <td></td>
135 * </tr>
136 * <tr>
137 * <td>1.0</td>
138 * <td>Initial version</td>
139 * <td></td>
140 * </tr>
141 * </table>
142 *
143 * \defgroup group_systick_macros Macros
144 * \defgroup group_systick_functions Functions
145 * \defgroup group_systick_data_structures Data Structures
146 */
147 
148 #include "cy_device.h"
149 
150 #if defined (CY_IP_M33SYSCPUSS) || defined (CY_IP_M4CPUSS) || defined (CY_IP_M7CPUSS) || defined(CY_IP_M55APPCPUSS)
151 
152 #include <stdint.h>
153 #include "cy_syslib.h"
154 
155 #ifdef __cplusplus
156 extern "C" {
157 #endif
158 
159 /** \cond */
160 typedef void (*Cy_SysTick_Callback)(void);
161 /** \endcond */
162 
163 /**
164 * \addtogroup group_systick_data_structures
165 * \{
166 */
167 /** SysTick clocks sources */
168 typedef enum
169 {
170     CY_SYSTICK_CLOCK_SOURCE_CLK_LF    = 0u,     /**< The low frequency clock clk_lf is selected. */
171     CY_SYSTICK_CLOCK_SOURCE_CLK_IMO   = 1u,     /**< The internal main oscillator (IMO) clock clk_imo is selected. */
172     CY_SYSTICK_CLOCK_SOURCE_CLK_ECO   = 2u,     /**< The external crystal oscillator (ECO) clock clk_eco is selected. */
173     CY_SYSTICK_CLOCK_SOURCE_CLK_TIMER = 3u,     /**< The SRSS clk_timer is selected. */
174     CY_SYSTICK_CLOCK_SOURCE_CLK_CPU   = 4u,     /**< The CPU clock is selected. */
175 } cy_en_systick_clock_source_t;
176 
177 /** \} group_systick_data_structures */
178 
179 
180 /**
181 * \addtogroup group_systick_macros
182 * \{
183 */
184 
185 /** Driver major version */
186 #define CY_SYSTICK_DRV_VERSION_MAJOR       1
187 
188 /** Driver minor version */
189 #define CY_SYSTICK_DRV_VERSION_MINOR       70
190 
191 /** SysTick driver ID */
192 #define CY_SYSTICK_ID           CY_PDL_DRV_ID(0x79U)
193 
194 /** Number of the callbacks assigned to the SysTick interrupt */
195 #define CY_SYS_SYST_NUM_OF_CALLBACKS         (5u)
196 
197 /** \} group_systick_macros */
198 
199 /** \cond */
200 /** Macros for the conditions used by CY_ASSERT calls */
201 #define CY_SYSTICK_IS_RELOAD_VALID(load)     ((load) <= 0xFFFFFFUL)
202 /** \endcond */
203 
204 /** \cond */
205 /** Interrupt number in the vector table */
206 #define CY_SYSTICK_IRQ_NUM                   (SysTick_IRQn)
207 /** \endcond */
208 
209 
210 /**
211 * \addtogroup group_systick_functions
212 * \{
213 */
214 
215 
216 /*******************************************************************************
217 * Function Name: Cy_SysTick_Init
218 ****************************************************************************//**
219 *
220 * Initializes the SysTick driver:
221 * - Initializes the callback addresses with pointers to NULL
222 * - Associates the SysTick system vector with the callback functions
223 * - Sets the SysTick clock by calling \ref Cy_SysTick_SetClockSource()
224 * - Sets the SysTick reload interval by calling \ref Cy_SysTick_SetReload()
225 * - Clears the SysTick counter value by calling \ref Cy_SysTick_Clear()
226 * - Enables the SysTick by calling \ref Cy_SysTick_Enable(). Note the \ref
227 *   Cy_SysTick_Enable() function also enables the SysTick interrupt by calling
228 *   \ref Cy_SysTick_EnableInterrupt().
229 *
230 * \param clockSource The SysTick clock source \ref cy_en_systick_clock_source_t
231 * \param interval The SysTick reload value.
232 *
233 * \sideeffect Clears the SysTick count flag if it was set.
234 *
235 *******************************************************************************/
236 void Cy_SysTick_Init(cy_en_systick_clock_source_t clockSource, uint32_t interval);
237 
238 
239 /*******************************************************************************
240 * Function Name: Cy_SysTick_Enable
241 ****************************************************************************//**
242 *
243 * Enables the SysTick timer and its interrupt.
244 *
245 * \sideeffect Clears the SysTick count flag if it was set
246 *
247 *******************************************************************************/
248 void Cy_SysTick_Enable(void);
249 
250 
251 /*******************************************************************************
252 * Function Name: Cy_SysTick_Disable
253 ****************************************************************************//**
254 *
255 * Disables the SysTick timer and its interrupt.
256 *
257 * \sideeffect Clears the SysTick count flag if it was set
258 *
259 *******************************************************************************/
260 void Cy_SysTick_Disable(void);
261 
262 
263 /*******************************************************************************
264 * Function Name: Cy_SysTick_SetCallback
265 ****************************************************************************//**
266 *
267 * Sets the callback function to the specified callback number.
268 *
269 * \param number The number of the callback function addresses to be set.
270 * The valid range is from 0 to \ref CY_SYS_SYST_NUM_OF_CALLBACKS - 1.
271 *
272 * \param function The pointer to the function that will be associated with the
273 * SysTick ISR for the specified number.
274 *
275 * \return Returns the address of the previous callback function.
276 * The NULL is returned if the specified address in not set or incorrect
277 * parameter is specified.
278 
279 * \sideeffect
280 * The registered callback functions will be executed in the interrupt.
281 *
282 *******************************************************************************/
283 Cy_SysTick_Callback Cy_SysTick_SetCallback(uint32_t number, Cy_SysTick_Callback function);
284 
285 
286 /*******************************************************************************
287 * Function Name: Cy_SysTick_GetCallback
288 ****************************************************************************//**
289 *
290 * Gets the specified callback function address.
291 *
292 * \param number The number of the callback function address to get. The valid
293 * range is from 0 to \ref CY_SYS_SYST_NUM_OF_CALLBACKS - 1.
294 *
295 * \return Returns the address of the specified callback function.
296 * The NULL is returned if the specified address in not initialized or incorrect
297 * parameter is specified.
298 *
299 *******************************************************************************/
300 Cy_SysTick_Callback Cy_SysTick_GetCallback(uint32_t number);
301 
302 
303 /*******************************************************************************
304 * Function Name: Cy_SysTick_SetClockSource
305 ****************************************************************************//**
306 *
307 * Sets the clock source for the SysTick counter.
308 *
309 * Clears the SysTick count flag if it was set. If the clock source is not ready
310 * this function call will have no effect. After changing the clock source to the
311 * low frequency clock, the counter and reload register values will remain
312 * unchanged so the time to the interrupt will be significantly longer and vice
313 * versa.
314 *
315 * Changing the SysTick clock source and/or its frequency will change
316 * the interrupt interval and Cy_SysTick_SetReload() should be
317 * called to compensate this change.
318 *
319 * \param clockSource \ref cy_en_systick_clock_source_t Clock source.
320 * For the PSoC 64 devices, passing any other value than
321 * CY_SYSTICK_CLOCK_SOURCE_CLK_CPU will not affect clock source
322 * and it will be as \ref Cy_SysTick_GetClockSource() reports.
323 *
324 *******************************************************************************/
325 void Cy_SysTick_SetClockSource(cy_en_systick_clock_source_t clockSource);
326 
327 
328 /*******************************************************************************
329 * Function Name: Cy_SysTick_GetClockSource
330 ****************************************************************************//**
331 *
332 * Gets the clock source for the SysTick counter.
333 *
334 * \returns \ref cy_en_systick_clock_source_t Clock source
335 *
336 *******************************************************************************/
337 cy_en_systick_clock_source_t Cy_SysTick_GetClockSource(void);
338 
339 
340 #if (defined(CY_PDL_TZ_ENABLED) && (defined(__SAUREGION_PRESENT) && (__SAUREGION_PRESENT == 1))) || defined (CY_DOXYGEN)
341 /*******************************************************************************
342 * Function Name: Cy_NssysTick_Enable
343 ****************************************************************************//**
344 *
345 * Enables the Non-Secure SysTick timer and its interrupt.
346 *
347 * \sideeffect Clears the SysTick count flag if it was set
348 *
349 * \note
350 * This macro is available for devices having M33SYSCPUSS IP.
351 *
352 *******************************************************************************/
353 void Cy_NsSysTick_Enable(void);
354 
355 
356 /*******************************************************************************
357 * Function Name: Cy_NsSysTick_Disable
358 ****************************************************************************//**
359 *
360 * Disables the Non-Secure SysTick timer and its interrupt.
361 *
362 * \sideeffect Clears the SysTick count flag if it was set
363 *
364 * \note
365 * This macro is available for devices having M33SYSCPUSS IP.
366 *
367 *******************************************************************************/
368 void Cy_NsSysTick_Disable(void);
369 
370 
371 #endif
372 
373 /** \} group_systick_functions */
374 
375 
376 /**
377 * \addtogroup group_systick_functions
378 * \{
379 */
380 
381 /*******************************************************************************
382 * Function Name: Cy_SysTick_EnableInterrupt
383 ****************************************************************************//**
384 *
385 * Enables the SysTick interrupt.
386 *
387 * \sideeffect Clears the SysTick count flag if it was set
388 *
389 *******************************************************************************/
390 void Cy_SysTick_EnableInterrupt(void);
391 
392 
393 /*******************************************************************************
394 * Function Name: Cy_SysTick_DisableInterrupt
395 ****************************************************************************//**
396 *
397 * Disables the SysTick interrupt.
398 *
399 * \sideeffect Clears the SysTick count flag if it was set
400 *
401 *******************************************************************************/
402 void Cy_SysTick_DisableInterrupt(void);
403 
404 
405 #if (defined(CY_PDL_TZ_ENABLED) && (defined(__SAUREGION_PRESENT) && (__SAUREGION_PRESENT == 1))) || defined (CY_DOXYGEN)
406 
407 /*******************************************************************************
408 * Function Name: Cy_NsSysTick_EnableInterrupt
409 ****************************************************************************//**
410 *
411 * Enables the Non-Secure SysTick interrupt.
412 *
413 * \sideeffect Clears the SysTick count flag if it was set
414 *
415 * \note
416 * It is available for CAT1B devices.
417 *
418 *******************************************************************************/
419 void Cy_NsSysTick_EnableInterrupt(void);
420 
421 
422 /*******************************************************************************
423 * Function Name: Cy_NsSysTick_DisableInterrupt
424 ****************************************************************************//**
425 *
426 * Disables the Non-Secure SysTick interrupt.
427 *
428 * \sideeffect Clears the SysTick count flag if it was set
429 *
430 * \note
431 * It is available for CAT1B devices.
432 *
433 *******************************************************************************/
434 void Cy_NsSysTick_DisableInterrupt(void);
435 
436 
437 #endif
438 
439 
440 /*******************************************************************************
441 * Function Name: Cy_SysTick_SetReload
442 ****************************************************************************//**
443 *
444 * Sets the value the counter is set to on a startup and after it reaches zero.
445 * This function does not change or reset the current sysTick counter value, so
446 * it should be cleared using the Cy_SysTick_Clear() API.
447 *
448 * \param value: The valid range is [0x0-0x00FFFFFF]. The counter reset value.
449 *
450 *******************************************************************************/
451 void Cy_SysTick_SetReload(uint32_t value);
452 
453 
454 /*******************************************************************************
455 * Function Name: Cy_SysTick_GetReload
456 ****************************************************************************//**
457 *
458 * Gets the value the counter is set to on a startup and after it reaches zero.
459 *
460 * \return The counter reset value.
461 *
462 *******************************************************************************/
463 uint32_t Cy_SysTick_GetReload(void);
464 
465 
466 /*******************************************************************************
467 * Function Name: Cy_SysTick_GetValue
468 ****************************************************************************//**
469 *
470 * Gets the current SysTick counter value.
471 *
472 * \return The current SysTick counter value.
473 *
474 *******************************************************************************/
475 uint32_t Cy_SysTick_GetValue(void);
476 
477 
478 /*******************************************************************************
479 * Function Name: Cy_SysTick_Clear
480 ****************************************************************************//**
481 *
482 * Clears the SysTick counter for a well-defined startup.
483 *
484 *******************************************************************************/
485 void Cy_SysTick_Clear(void);
486 
487 
488 /*******************************************************************************
489 * Function Name: Cy_SysTick_GetCountFlag
490 ****************************************************************************//**
491 *
492 * Gets the values of the count flag. The count flag is set once the SysTick
493 * counter reaches zero. The flag is cleared on read.
494 *
495 * \return Returns a non-zero value if a flag is set; otherwise a zero is
496 * returned.
497 *
498 * \sideeffect Clears the SysTick count flag if it was set.
499 *
500 * \note Applicable only in Polling mode. If the SysTick interrupt is enabled,
501 * the count flag will be cleared automatically on interrupt event.
502 *
503 *******************************************************************************/
504 uint32_t Cy_SysTick_GetCountFlag(void);
505 
506 
507 /** \} group_systick_functions */
508 
509 #ifdef __cplusplus
510 }
511 #endif
512 
513 #endif /* CY_IP_M33SYSCPUSS */
514 
515 #endif /* CY_SYSTICK_H */
516 
517 /** \} group_systick */
518 
519 
520 /* [] END OF FILE */
521