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