1 /***************************************************************************//**
2 * \file cy_systick.c
3 * \version 1.70.1
4 *
5 * Provides the API definitions of the SisTick 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 #include "cy_device.h"
27 
28 #if defined (CY_IP_M4CPUSS) || defined (CY_IP_M7CPUSS)
29 
30 #include <stddef.h>     /* for NULL */
31 #include "cy_systick.h"
32 #include "cy_sysint.h"
33 
34 static Cy_SysTick_Callback Cy_SysTick_Callbacks[CY_SYS_SYST_NUM_OF_CALLBACKS];
35 static void Cy_SysTick_ServiceCallbacks(void);
36 
Cy_SysTick_EnableInterrupt(void)37 void Cy_SysTick_EnableInterrupt(void)
38 {
39     SYSTICK_CTRL = SYSTICK_CTRL | SysTick_CTRL_TICKINT_Msk;
40 }
41 
Cy_SysTick_DisableInterrupt(void)42 void Cy_SysTick_DisableInterrupt(void)
43 {
44     SYSTICK_CTRL = SYSTICK_CTRL & ~SysTick_CTRL_TICKINT_Msk;
45 }
46 
Cy_SysTick_SetReload(uint32_t value)47 void Cy_SysTick_SetReload(uint32_t value)
48 {
49     CY_ASSERT_L1(CY_SYSTICK_IS_RELOAD_VALID(value));
50 
51     SYSTICK_LOAD = (value & SysTick_LOAD_RELOAD_Msk);
52 }
53 
Cy_SysTick_GetReload(void)54 uint32_t Cy_SysTick_GetReload(void)
55 {
56     return (SYSTICK_LOAD);
57 }
58 
Cy_SysTick_GetValue(void)59 uint32_t Cy_SysTick_GetValue(void)
60 {
61     return (SYSTICK_VAL);
62 }
63 
Cy_SysTick_Clear(void)64 void Cy_SysTick_Clear(void)
65 {
66     SYSTICK_VAL = 0u;
67 }
68 
Cy_SysTick_GetCountFlag(void)69 uint32_t Cy_SysTick_GetCountFlag(void)
70 {
71     return (SYSTICK_CTRL & SysTick_CTRL_COUNTFLAG_Msk);
72 }
73 
Cy_SysTick_Init(cy_en_systick_clock_source_t clockSource,uint32_t interval)74 void Cy_SysTick_Init(cy_en_systick_clock_source_t clockSource, uint32_t interval)
75 {
76     CY_ASSERT_L1(CY_SYSTICK_IS_RELOAD_VALID(interval));
77 
78     uint32_t i;
79 
80     for (i = 0u; i<CY_SYS_SYST_NUM_OF_CALLBACKS; i++)
81     {
82         Cy_SysTick_Callbacks[i] = NULL;
83     }
84 
85     (void)Cy_SysInt_SetVector((IRQn_Type)CY_SYSTICK_IRQ_NUM, Cy_SysTick_ServiceCallbacks); /* Suppress a compiler warning about unused return value */
86 
87     Cy_SysTick_SetClockSource(clockSource);
88 
89     Cy_SysTick_SetReload(interval);
90     Cy_SysTick_Clear();
91     Cy_SysTick_Enable();
92 }
93 
Cy_SysTick_Enable(void)94 void Cy_SysTick_Enable(void)
95 {
96     Cy_SysTick_EnableInterrupt();
97     SYSTICK_CTRL |= SysTick_CTRL_ENABLE_Msk;
98 }
99 
Cy_SysTick_Disable(void)100 void Cy_SysTick_Disable(void)
101 {
102     Cy_SysTick_DisableInterrupt();
103     SYSTICK_CTRL &= ~SysTick_CTRL_ENABLE_Msk;
104 }
105 
Cy_SysTick_SetClockSource(cy_en_systick_clock_source_t clockSource)106 void Cy_SysTick_SetClockSource(cy_en_systick_clock_source_t clockSource)
107 {
108     if (clockSource == CY_SYSTICK_CLOCK_SOURCE_CLK_CPU)
109     {
110         SYSTICK_CTRL |= SysTick_CTRL_CLKSOURCE_Msk;
111     }
112     else
113     {
114         #if ((CY_CPU_CORTEX_M0P) || (!defined(CY_DEVICE_SECURE)))
115             CPUSS_SYSTICK_CTL = _VAL2FLD(CPUSS_SYSTICK_CTL_CLOCK_SOURCE, (uint32_t) clockSource);
116         #endif /* ((CY_CPU_CORTEX_M0P) || (!defined(CY_DEVICE_SECURE))) */
117         SYSTICK_CTRL &= ~SysTick_CTRL_CLKSOURCE_Msk;
118     }
119 }
120 
Cy_SysTick_GetClockSource(void)121 cy_en_systick_clock_source_t Cy_SysTick_GetClockSource(void)
122 {
123     cy_en_systick_clock_source_t returnValue;
124 
125     if ((SYSTICK_CTRL & SysTick_CTRL_CLKSOURCE_Msk) != 0u)
126     {
127         returnValue = CY_SYSTICK_CLOCK_SOURCE_CLK_CPU;
128     }
129     else
130     {
131         returnValue =  (cy_en_systick_clock_source_t) ((uint32_t) _FLD2VAL(CPUSS_SYSTICK_CTL_CLOCK_SOURCE, CPUSS_SYSTICK_CTL));
132     }
133 
134     return(returnValue);
135 }
136 
Cy_SysTick_SetCallback(uint32_t number,Cy_SysTick_Callback function)137 Cy_SysTick_Callback Cy_SysTick_SetCallback(uint32_t number, Cy_SysTick_Callback function)
138 {
139     Cy_SysTick_Callback retVal;
140 
141     if (number < CY_SYS_SYST_NUM_OF_CALLBACKS)
142     {
143         retVal = Cy_SysTick_Callbacks[number];
144         Cy_SysTick_Callbacks[number] = function;
145     }
146     else
147     {
148         retVal = NULL;
149     }
150 
151     return (retVal);
152 }
153 
Cy_SysTick_GetCallback(uint32_t number)154 Cy_SysTick_Callback Cy_SysTick_GetCallback(uint32_t number)
155 {
156     Cy_SysTick_Callback retVal;
157 
158     if (number < CY_SYS_SYST_NUM_OF_CALLBACKS)
159     {
160         retVal = Cy_SysTick_Callbacks[number];
161     }
162     else
163     {
164         retVal = NULL;
165     }
166 
167     return (retVal);
168 }
169 
Cy_SysTick_ServiceCallbacks(void)170 static void Cy_SysTick_ServiceCallbacks(void)
171 {
172     uint32_t i;
173 
174     /* Verify that tick timer flag was set */
175     if (0u != Cy_SysTick_GetCountFlag())
176     {
177         for (i=0u; i < CY_SYS_SYST_NUM_OF_CALLBACKS; i++)
178         {
179             if (Cy_SysTick_Callbacks[i] != NULL)
180             {
181                 (void)(Cy_SysTick_Callbacks[i])();
182             }
183         }
184     }
185 }
186 
187 #endif
188 
189 /* [] END OF FILE */
190