1 /***************************************************************************//**
2 * \file cy_systick.h
3 * \version 1.80
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.80</td>
67 *     <td>Newly added macro CY_SYSTICK_IS_CLK_SRC_VALID and updated APIs \ref Cy_SysTick_Init, \ref Cy_SysTick_SetClockSource.<br>
68 *     <td>Added check for IMO clock source.</td>
69 *   </tr>
70 *    <tr>
71 *     <td>1.70.1</td>
72 *     <td>Updated driver guards.<br>
73 *     <td>Bug fixes.</td>
74 *   </tr>
75 *   <tr>
76 *     <td>1.70</td>
77 *     <td>Macro value change and enhancements.<br>
78 *     <td>Bug fix and Enhancements.</td>
79 *   </tr>
80 *   <tr>
81 *     <td>1.60</td>
82 *     <td>Bug fix and Enhancements.<br>
83 *     <td>Bug fix and Enhancements.</td>
84 *   </tr>
85 *   <tr>
86 *     <td>1.50</td>
87 *     <td>CAT1C, CAT1D devices support.<br>
88 *         Updated Cy_SysTick_Init() to use Cy_SysInt_SetVector() API to register the callback function and removed invalid define CY_SYSTICK_IRQ_NUM.</td>
89 *     <td>Support for new devices.</td>
90 *   </tr>
91 *   <tr>
92 *     <td>1.40</td>
93 *     <td>Support for CM33.</td>
94 *     <td>New devices support.</td>
95 *   </tr>
96 * <tr>
97 *   <td rowspan="2">1.30</td>
98 *     <td>Added function parameter checks.</td>
99 *     <td>Improved the debugging capability.</td>
100 * </tr>
101 * <tr>
102 *     <td>Minor documentation updates.</td>
103 *     <td>Documentation enhancement.</td>
104 * </tr>
105 * <tr>
106 *   <td rowspan="2">1.20</td>
107 *     <td>Updated Cy_SysTick_SetClockSource() for the PSoC 64 devices,
108 *         so that passing any other value than CY_SYSTICK_CLOCK_SOURCE_CLK_CPU
109 *         will not affect clock source and it will be as
110 *         \ref Cy_SysTick_GetClockSource() reports.</td>
111 *     <td>Added PSoC 64 devices support.</td>
112 * </tr>
113 * <tr>
114 *     <td>Minor documentation updates.</td>
115 *     <td>Documentation enhancement.</td>
116 * </tr>
117 * <tr>
118 *   <td>1.10.1</td>
119 *     <td>Updated include files.</td>
120 *     <td>Improve pdl usability.</td>
121 * </tr>
122 * <tr>
123 *   <td rowspan="2">1.10</td>
124 *     <td>Flattened the organization of the driver source code into the single
125 *         source directory and the single include directory.
126 *     </td>
127 *     <td>Driver library directory-structure simplification.</td>
128 * </tr>
129 *   <tr>
130 *     <td>Added register access layer. Use register access macros instead
131 *         of direct register access using dereferenced pointers.</td>
132 *     <td>Makes register access device-independent, so that the PDL does
133 *         not need to be recompiled for each supported part number.</td>
134 *   </tr>
135 * <tr>
136 * <td>1.0.1</td>
137 * <td>Fixed a warning issued when the compilation of C++ source code was
138 *     enabled.</td>
139 * <td></td>
140 * </tr>
141 * <tr>
142 * <td>1.0</td>
143 * <td>Initial version</td>
144 * <td></td>
145 * </tr>
146 * </table>
147 *
148 * \defgroup group_systick_macros Macros
149 * \defgroup group_systick_functions Functions
150 * \defgroup group_systick_data_structures Data Structures
151 */
152 
153 #include "cy_device.h"
154 
155 #if defined (CY_IP_M33SYSCPUSS) || defined (CY_IP_M4CPUSS) || defined (CY_IP_M7CPUSS) || defined(CY_IP_M55APPCPUSS)
156 
157 #include <stdint.h>
158 #include "cy_syslib.h"
159 
160 #ifdef __cplusplus
161 extern "C" {
162 #endif
163 
164 
165 /**
166 * \addtogroup group_systick_data_structures
167 * \{
168 */
169 /** SysTick clocks sources */
170 typedef enum
171 {
172     CY_SYSTICK_CLOCK_SOURCE_CLK_LF    = 0u,     /**< The low frequency clock clk_lf is selected. */
173     CY_SYSTICK_CLOCK_SOURCE_CLK_IMO   = 1u,     /**< The internal main oscillator (IMO) clock clk_imo is selected. */
174     CY_SYSTICK_CLOCK_SOURCE_CLK_ECO   = 2u,     /**< The external crystal oscillator (ECO) clock clk_eco is selected. */
175     CY_SYSTICK_CLOCK_SOURCE_CLK_TIMER = 3u,     /**< The SRSS clk_timer is selected. */
176     CY_SYSTICK_CLOCK_SOURCE_CLK_CPU   = 4u,     /**< The CPU clock is selected. */
177 } cy_en_systick_clock_source_t;
178 
179 
180 /** Pointer to the callback function */
181 typedef void (*Cy_SysTick_Callback)(void);
182 
183 /** \} group_systick_data_structures */
184 
185 
186 /**
187 * \addtogroup group_systick_macros
188 * \{
189 */
190 
191 /** Driver major version */
192 #define CY_SYSTICK_DRV_VERSION_MAJOR       1
193 
194 /** Driver minor version */
195 #define CY_SYSTICK_DRV_VERSION_MINOR       80
196 
197 /** SysTick driver ID */
198 #define CY_SYSTICK_ID           CY_PDL_DRV_ID(0x79U)
199 
200 /** Number of the callbacks assigned to the SysTick interrupt */
201 #define CY_SYS_SYST_NUM_OF_CALLBACKS         (5u)
202 
203 /** \} group_systick_macros */
204 
205 /** \cond */
206 /** Macros for the conditions used by CY_ASSERT calls */
207 #define CY_SYSTICK_IS_RELOAD_VALID(load)     ((load) <= 0xFFFFFFUL)
208 
209 #define CY_SYSTICK_IS_CLK_SRC_VALID(clkSrc)   (((clkSrc) == CY_SYSTICK_CLOCK_SOURCE_CLK_LF) || \
210                                                 ((clkSrc) == CY_SYSTICK_CLOCK_SOURCE_CLK_IMO) || \
211                                                 ((clkSrc) == CY_SYSTICK_CLOCK_SOURCE_CLK_ECO) || \
212                                                 ((clkSrc) == CY_SYSTICK_CLOCK_SOURCE_CLK_TIMER) || \
213                                                 ((clkSrc) == CY_SYSTICK_CLOCK_SOURCE_CLK_CPU))
214 /** \endcond */
215 
216 /** \cond */
217 /** Interrupt number in the vector table */
218 #define CY_SYSTICK_IRQ_NUM                   (SysTick_IRQn)
219 /** \endcond */
220 
221 
222 /**
223 * \addtogroup group_systick_functions
224 * \{
225 */
226 
227 
228 /*******************************************************************************
229 * Function Name: Cy_SysTick_Init
230 ****************************************************************************//**
231 *
232 * Initializes the SysTick driver:
233 * - Initializes the callback addresses with pointers to NULL
234 * - Associates the SysTick system vector with the callback functions
235 * - Sets the SysTick clock by calling \ref Cy_SysTick_SetClockSource()
236 * - Sets the SysTick reload interval by calling \ref Cy_SysTick_SetReload()
237 * - Clears the SysTick counter value by calling \ref Cy_SysTick_Clear()
238 * - Enables the SysTick by calling \ref Cy_SysTick_Enable(). Note the \ref
239 *   Cy_SysTick_Enable() function also enables the SysTick interrupt by calling
240 *   \ref Cy_SysTick_EnableInterrupt().
241 *
242 * \param clockSource The SysTick clock source \ref cy_en_systick_clock_source_t
243 * \param interval The SysTick reload value.
244 * \note The parameter interval is a tick value not the actual time interval.
245 * If user wants to configure systick interrupt based on time then the interval
246 * parameter value will be (((time in micro seconds) / 1000000.0) * source clock frequency in Hz)
247 *
248 * \sideeffect Clears the SysTick count flag if it was set.
249 *
250 *******************************************************************************/
251 void Cy_SysTick_Init(cy_en_systick_clock_source_t clockSource, uint32_t interval);
252 
253 
254 /*******************************************************************************
255 * Function Name: Cy_SysTick_Enable
256 ****************************************************************************//**
257 *
258 * Enables the SysTick timer and its interrupt.
259 *
260 * \sideeffect Clears the SysTick count flag if it was set
261 *
262 *******************************************************************************/
263 void Cy_SysTick_Enable(void);
264 
265 
266 /*******************************************************************************
267 * Function Name: Cy_SysTick_Disable
268 ****************************************************************************//**
269 *
270 * Disables the SysTick timer and its interrupt.
271 *
272 * \sideeffect Clears the SysTick count flag if it was set
273 *
274 *******************************************************************************/
275 void Cy_SysTick_Disable(void);
276 
277 
278 /*******************************************************************************
279 * Function Name: Cy_SysTick_SetCallback
280 ****************************************************************************//**
281 *
282 * Sets the callback function to the specified callback number.
283 *
284 * \param number The number of the callback function addresses to be set.
285 * The valid range is from 0 to \ref CY_SYS_SYST_NUM_OF_CALLBACKS - 1.
286 *
287 * \param function The pointer to the function that will be associated with the
288 * SysTick ISR for the specified number.
289 *
290 * \return \ref Cy_SysTick_Callback Returns the address of the previous callback function.
291 * The NULL is returned if the specified address in not set or incorrect
292 * parameter is specified.
293 
294 * \sideeffect
295 * The registered callback functions will be executed in the interrupt.
296 *
297 *******************************************************************************/
298 Cy_SysTick_Callback Cy_SysTick_SetCallback(uint32_t number, Cy_SysTick_Callback function);
299 
300 
301 /*******************************************************************************
302 * Function Name: Cy_SysTick_GetCallback
303 ****************************************************************************//**
304 *
305 * Gets the specified callback function address.
306 *
307 * \param number The number of the callback function address to get. The valid
308 * range is from 0 to \ref CY_SYS_SYST_NUM_OF_CALLBACKS - 1.
309 *
310 * \return \ref Cy_SysTick_Callback Returns the address of the specified callback function.
311 * The NULL is returned if the specified address in not initialized or incorrect
312 * parameter is specified.
313 *
314 *******************************************************************************/
315 Cy_SysTick_Callback Cy_SysTick_GetCallback(uint32_t number);
316 
317 
318 /*******************************************************************************
319 * Function Name: Cy_SysTick_SetClockSource
320 ****************************************************************************//**
321 *
322 * Sets the clock source for the SysTick counter.
323 *
324 * Clears the SysTick count flag if it was set. If the clock source is not ready
325 * this function call will have no effect. After changing the clock source to the
326 * low frequency clock, the counter and reload register values will remain
327 * unchanged so the time to the interrupt will be significantly longer and vice
328 * versa.
329 *
330 * Changing the SysTick clock source and/or its frequency will change
331 * the interrupt interval and Cy_SysTick_SetReload() should be
332 * called to compensate this change.
333 *
334 * \param clockSource \ref cy_en_systick_clock_source_t Clock source.
335 * For the PSoC 64 devices, passing any other value than
336 * CY_SYSTICK_CLOCK_SOURCE_CLK_CPU will not affect clock source
337 * and it will be as \ref Cy_SysTick_GetClockSource() reports.
338 *
339 *******************************************************************************/
340 void Cy_SysTick_SetClockSource(cy_en_systick_clock_source_t clockSource);
341 
342 
343 /*******************************************************************************
344 * Function Name: Cy_SysTick_GetClockSource
345 ****************************************************************************//**
346 *
347 * Gets the clock source for the SysTick counter.
348 *
349 * \returns \ref cy_en_systick_clock_source_t Clock source
350 *
351 *******************************************************************************/
352 cy_en_systick_clock_source_t Cy_SysTick_GetClockSource(void);
353 
354 
355 #if (defined(CY_PDL_TZ_ENABLED) && (defined(__SAUREGION_PRESENT) && (__SAUREGION_PRESENT == 1))) || defined (CY_DOXYGEN)
356 /*******************************************************************************
357 * Function Name: Cy_NssysTick_Enable
358 ****************************************************************************//**
359 *
360 * Enables 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_Enable(void);
369 
370 
371 /*******************************************************************************
372 * Function Name: Cy_NsSysTick_Disable
373 ****************************************************************************//**
374 *
375 * Disables the Non-Secure SysTick timer and its interrupt.
376 *
377 * \sideeffect Clears the SysTick count flag if it was set
378 *
379 * \note
380 * This macro is available for devices having M33SYSCPUSS IP.
381 *
382 *******************************************************************************/
383 void Cy_NsSysTick_Disable(void);
384 
385 
386 #endif
387 
388 /** \} group_systick_functions */
389 
390 
391 /**
392 * \addtogroup group_systick_functions
393 * \{
394 */
395 
396 /*******************************************************************************
397 * Function Name: Cy_SysTick_EnableInterrupt
398 ****************************************************************************//**
399 *
400 * Enables the SysTick interrupt.
401 *
402 * \sideeffect Clears the SysTick count flag if it was set
403 *
404 *******************************************************************************/
405 void Cy_SysTick_EnableInterrupt(void);
406 
407 
408 /*******************************************************************************
409 * Function Name: Cy_SysTick_DisableInterrupt
410 ****************************************************************************//**
411 *
412 * Disables the SysTick interrupt.
413 *
414 * \sideeffect Clears the SysTick count flag if it was set
415 *
416 *******************************************************************************/
417 void Cy_SysTick_DisableInterrupt(void);
418 
419 
420 #if (defined(CY_PDL_TZ_ENABLED) && (defined(__SAUREGION_PRESENT) && (__SAUREGION_PRESENT == 1))) || defined (CY_DOXYGEN)
421 
422 /*******************************************************************************
423 * Function Name: Cy_NsSysTick_EnableInterrupt
424 ****************************************************************************//**
425 *
426 * Enables 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_EnableInterrupt(void);
435 
436 
437 /*******************************************************************************
438 * Function Name: Cy_NsSysTick_DisableInterrupt
439 ****************************************************************************//**
440 *
441 * Disables the Non-Secure SysTick interrupt.
442 *
443 * \sideeffect Clears the SysTick count flag if it was set
444 *
445 * \note
446 * It is available for CAT1B devices.
447 *
448 *******************************************************************************/
449 void Cy_NsSysTick_DisableInterrupt(void);
450 
451 
452 #endif
453 
454 
455 /*******************************************************************************
456 * Function Name: Cy_SysTick_SetReload
457 ****************************************************************************//**
458 *
459 * Sets the value the counter is set to on a startup and after it reaches zero.
460 * This function does not change or reset the current sysTick counter value, so
461 * it should be cleared using the Cy_SysTick_Clear() API.
462 *
463 * \param value: The valid range is [0x0-0x00FFFFFF]. The counter reset value.
464 *
465 *******************************************************************************/
466 void Cy_SysTick_SetReload(uint32_t value);
467 
468 
469 /*******************************************************************************
470 * Function Name: Cy_SysTick_GetReload
471 ****************************************************************************//**
472 *
473 * Gets the value the counter is set to on a startup and after it reaches zero.
474 *
475 * \return The counter reset value.
476 *
477 *******************************************************************************/
478 uint32_t Cy_SysTick_GetReload(void);
479 
480 
481 /*******************************************************************************
482 * Function Name: Cy_SysTick_GetValue
483 ****************************************************************************//**
484 *
485 * Gets the current SysTick counter value.
486 *
487 * \return The current SysTick counter value.
488 *
489 *******************************************************************************/
490 uint32_t Cy_SysTick_GetValue(void);
491 
492 
493 /*******************************************************************************
494 * Function Name: Cy_SysTick_Clear
495 ****************************************************************************//**
496 *
497 * Clears the SysTick counter for a well-defined startup.
498 *
499 *******************************************************************************/
500 void Cy_SysTick_Clear(void);
501 
502 
503 /*******************************************************************************
504 * Function Name: Cy_SysTick_GetCountFlag
505 ****************************************************************************//**
506 *
507 * Gets the values of the count flag. The count flag is set once the SysTick
508 * counter reaches zero. The flag is cleared on read.
509 *
510 * \return Returns a non-zero value if a flag is set; otherwise a zero is
511 * returned.
512 *
513 * \sideeffect Clears the SysTick count flag if it was set.
514 *
515 * \note Applicable only in Polling mode. If the SysTick interrupt is enabled,
516 * the count flag will be cleared automatically on interrupt event.
517 *
518 *******************************************************************************/
519 uint32_t Cy_SysTick_GetCountFlag(void);
520 
521 
522 /** \} group_systick_functions */
523 
524 #ifdef __cplusplus
525 }
526 #endif
527 
528 #endif /* CY_IP_M33SYSCPUSS */
529 
530 #endif /* CY_SYSTICK_H */
531 
532 /** \} group_systick */
533 
534 
535 /* [] END OF FILE */
536