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