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_M33SYSCPUSS) || defined(CY_IP_M55APPCPUSS)
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(CY_SYSTICK_IRQ_NUM, Cy_SysTick_ServiceCallbacks); /* Suppress a compiler warning about unused return value */
86     Cy_SysTick_SetClockSource(clockSource);
87 
88     Cy_SysTick_SetReload(interval);
89     Cy_SysTick_Clear();
90     Cy_SysTick_Enable();
91 }
92 
Cy_SysTick_Enable(void)93 void Cy_SysTick_Enable(void)
94 {
95     Cy_SysTick_EnableInterrupt();
96     SYSTICK_CTRL |= SysTick_CTRL_ENABLE_Msk;
97 }
98 
Cy_SysTick_Disable(void)99 void Cy_SysTick_Disable(void)
100 {
101     Cy_SysTick_DisableInterrupt();
102     SYSTICK_CTRL &= ~SysTick_CTRL_ENABLE_Msk;
103 }
104 
105 
106 #if defined(CY_PDL_TZ_ENABLED) && (defined(__SAUREGION_PRESENT) && (__SAUREGION_PRESENT == 1))
107 
Cy_NsSysTick_EnableInterrupt(void)108 void Cy_NsSysTick_EnableInterrupt(void)
109 {
110     SYSTICK_NS_CTRL = SYSTICK_NS_CTRL | SysTick_CTRL_TICKINT_Msk;
111 }
112 
113 
Cy_NsSysTick_DisableInterrupt(void)114 void Cy_NsSysTick_DisableInterrupt(void)
115 {
116     SYSTICK_NS_CTRL = SYSTICK_NS_CTRL & ~SysTick_CTRL_TICKINT_Msk;
117 }
118 
Cy_NsSysTick_Enable(void)119 void Cy_NsSysTick_Enable(void)
120 {
121     Cy_NsSysTick_EnableInterrupt();
122     SYSTICK_NS_CTRL |= SysTick_CTRL_ENABLE_Msk;
123 }
124 
Cy_NsSysTick_Disable(void)125 void Cy_NsSysTick_Disable(void)
126 {
127     Cy_NsSysTick_DisableInterrupt();
128     SYSTICK_NS_CTRL &= ~SysTick_CTRL_ENABLE_Msk;
129 }
130 #endif
131 
Cy_SysTick_SetClockSource(cy_en_systick_clock_source_t clockSource)132 void Cy_SysTick_SetClockSource(cy_en_systick_clock_source_t clockSource)
133 {
134     if (clockSource == CY_SYSTICK_CLOCK_SOURCE_CLK_CPU)
135     {
136         SYSTICK_CTRL |= SysTick_CTRL_CLKSOURCE_Msk;
137     }
138     else
139     {
140 #ifdef CY_PDL_TZ_ENABLED
141         CPUSS_SYSTICK_S_CTL = _VAL2FLD(CPUSS_SYSTICK_S_CTL_CLOCK_SOURCE, (uint32_t) clockSource);
142 #else
143         CPUSS_SYSTICK_NS_CTL = _VAL2FLD(CPUSS_SYSTICK_NS_CTL_CLOCK_SOURCE, (uint32_t) clockSource);
144 #endif
145 
146         SYSTICK_CTRL &= ~SysTick_CTRL_CLKSOURCE_Msk;
147     }
148 }
149 
Cy_SysTick_GetClockSource(void)150 cy_en_systick_clock_source_t Cy_SysTick_GetClockSource(void)
151 {
152     cy_en_systick_clock_source_t returnValue;
153 
154     if ((SYSTICK_CTRL & SysTick_CTRL_CLKSOURCE_Msk) != 0u)
155     {
156         returnValue = CY_SYSTICK_CLOCK_SOURCE_CLK_CPU;
157     }
158     else
159     {
160 #ifdef CY_PDL_TZ_ENABLED
161         returnValue =  (cy_en_systick_clock_source_t) ((uint32_t) _FLD2VAL(CPUSS_SYSTICK_S_CTL_CLOCK_SOURCE, CPUSS_SYSTICK_S_CTL));
162 #else
163         returnValue =  (cy_en_systick_clock_source_t) ((uint32_t) _FLD2VAL(CPUSS_SYSTICK_NS_CTL_CLOCK_SOURCE, CPUSS_SYSTICK_NS_CTL));
164 #endif
165 
166     }
167 
168     return(returnValue);
169 }
170 
Cy_SysTick_SetCallback(uint32_t number,Cy_SysTick_Callback function)171 Cy_SysTick_Callback Cy_SysTick_SetCallback(uint32_t number, Cy_SysTick_Callback function)
172 {
173     Cy_SysTick_Callback retVal;
174 
175     if (number < CY_SYS_SYST_NUM_OF_CALLBACKS)
176     {
177         retVal = Cy_SysTick_Callbacks[number];
178         Cy_SysTick_Callbacks[number] = function;
179     }
180     else
181     {
182         retVal = NULL;
183     }
184 
185     return (retVal);
186 }
187 
Cy_SysTick_GetCallback(uint32_t number)188 Cy_SysTick_Callback Cy_SysTick_GetCallback(uint32_t number)
189 {
190     Cy_SysTick_Callback retVal;
191 
192     if (number < CY_SYS_SYST_NUM_OF_CALLBACKS)
193     {
194         retVal = Cy_SysTick_Callbacks[number];
195     }
196     else
197     {
198         retVal = NULL;
199     }
200 
201     return (retVal);
202 }
203 
Cy_SysTick_ServiceCallbacks(void)204 static void Cy_SysTick_ServiceCallbacks(void)
205 {
206     uint32_t i;
207 
208     /* Verify that tick timer flag was set */
209     if (0u != Cy_SysTick_GetCountFlag())
210     {
211         for (i=0u; i < CY_SYS_SYST_NUM_OF_CALLBACKS; i++)
212         {
213             if (Cy_SysTick_Callbacks[i] != NULL)
214             {
215                 (void)(Cy_SysTick_Callbacks[i])();
216             }
217         }
218     }
219 }
220 
221 #endif
222 
223 /* [] END OF FILE */
224