1 /***************************************************************************//**
2 * \file system_cm7.c
3 * \version 1.0
4 *
5 * The device system-source file.
6 *
7 ********************************************************************************
8 * \copyright
9 * Copyright 2021 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 <stdbool.h>
26 #include "system_cat1c.h"
27 #include "cy_device.h"
28 #include "cy_device_headers.h"
29 #include "cy_syslib.h"
30 #include "cy_sysclk.h"
31 #include "cy_wdt.h"
32
33
34 void Cy_DefaultUserHandler(void);
35
36 #define DEFAULT_HANDLER_NAME Cy_DefaultUserHandler
37
38 CY_NOINIT cy_israddress Cy_SystemIrqUserTable[CPUSS_SYSTEM_INT_NR] ;
39
40 CY_NOINIT cy_israddress * Cy_SysInt_SystemIrqUserTableRamPointer ;
41
42
43 /*******************************************************************************
44 * SystemCoreClockUpdate()
45 *******************************************************************************/
46
47 /** Default HFClk frequency in Hz */
48 #define CY_CLK_HFCLK0_FREQ_HZ_DEFAULT (8000000UL)
49
50 /** Default PeriClk frequency in Hz */
51 #define CY_CLK_PERICLK_FREQ_HZ_DEFAULT (8000000UL)
52
53 /** Default system core frequency in Hz */
54 #define CY_CLK_SYSTEM_FREQ_HZ_DEFAULT (8000000UL)
55
56 /** Holds the CLK_SLOW(Cortex-M0+) or CLK_FAST0(Cortex-M7_0) or CLK_FAST(Cortex-M7_1) system core clock */
57 CY_NOINIT uint32_t SystemCoreClock ;
58
59 /** Holds the HFClk0 clock frequency. Updated by \ref SystemCoreClockUpdate(). */
60 CY_NOINIT uint32_t cy_Hfclk0FreqHz ;
61
62 /** Holds the PeriClk clock frequency. Updated by \ref SystemCoreClockUpdate(). */
63 CY_NOINIT uint32_t cy_PeriClkFreqHz ;
64
65 /** Holds the AHB frequency. Updated by \ref SystemCoreClockUpdate(). */
66 CY_NOINIT uint32_t cy_AhbFreqHz ;
67
68 /*******************************************************************************
69 * SystemCoreClockUpdate (void)
70 *******************************************************************************/
71
72 /* Do not use these definitions directly in your application */
73 #define CY_DELAY_MS_OVERFLOW_THRESHOLD (0x8000u)
74 #define CY_DELAY_1K_THRESHOLD (1000u)
75 #define CY_DELAY_1K_MINUS_1_THRESHOLD (CY_DELAY_1K_THRESHOLD - 1u)
76 #define CY_DELAY_1M_THRESHOLD (1000000u)
77 #define CY_DELAY_1M_MINUS_1_THRESHOLD (CY_DELAY_1M_THRESHOLD - 1u)
78
79 CY_NOINIT uint32_t cy_delayFreqHz ;
80
81 CY_NOINIT uint32_t cy_delayFreqKhz ;
82
83 CY_NOINIT uint32_t cy_delayFreqMhz ;
84
85
86 /*******************************************************************************
87 * Function Name: SystemInit
88 ****************************************************************************//**
89 * \cond
90 * Initializes the system:
91 * - Unlocks and disables WDT.
92 * - Calls the Cy_SystemInit() function.
93 * - Calls \ref SystemCoreClockUpdate().
94 * \endcond
95 *******************************************************************************/
SystemInit(void)96 void SystemInit(void)
97 {
98
99 /* if CM0+ is not enabled then unlock and disable WDT */
100 #ifdef __CM0P_PRESENT
101 #if (__CM0P_PRESENT == 0)
102
103 /* Release reset for all groups IP except group 0 */
104 (void)Cy_SysClk_PeriGroupSetSlaveCtl(1, CY_SYSCLK_PERI_GROUP_SL_CTL, 0xFFFFU); /* Suppress a compiler warning about unused return value */
105 (void)Cy_SysClk_PeriGroupSetSlaveCtl(2, CY_SYSCLK_PERI_GROUP_SL_CTL, 0xFFFFU); /* Suppress a compiler warning about unused return value */
106 (void)Cy_SysClk_PeriGroupSetSlaveCtl(3, CY_SYSCLK_PERI_GROUP_SL_CTL, 0xFFFFU); /* Suppress a compiler warning about unused return value */
107 (void)Cy_SysClk_PeriGroupSetSlaveCtl(4, CY_SYSCLK_PERI_GROUP_SL_CTL, 0xFFFFU); /* Suppress a compiler warning about unused return value */
108 (void)Cy_SysClk_PeriGroupSetSlaveCtl(5, CY_SYSCLK_PERI_GROUP_SL_CTL, 0xFFFFU); /* Suppress a compiler warning about unused return value */
109 (void)Cy_SysClk_PeriGroupSetSlaveCtl(6, CY_SYSCLK_PERI_GROUP_SL_CTL, 0xFFFFU); /* Suppress a compiler warning about unused return value */
110 (void)Cy_SysClk_PeriGroupSetSlaveCtl(8, CY_SYSCLK_PERI_GROUP_SL_CTL, 0xFFFFU); /* Suppress a compiler warning about unused return value */
111 (void)Cy_SysClk_PeriGroupSetSlaveCtl(9, CY_SYSCLK_PERI_GROUP_SL_CTL, 0xFFFFU); /* Suppress a compiler warning about unused return value */
112
113 /* Unlock and disable WDT */
114 Cy_WDT_Unlock();
115 Cy_WDT_Disable();
116
117 #endif /* (__CM0P_PRESENT == 0) */
118 #endif /* __CM0P_PRESENT */
119
120 Cy_SystemInit();
121 SystemCoreClockUpdate();
122
123 SystemIrqInit();
124 }
125
126 /*******************************************************************************
127 * Function Name: SystemIrqInit
128 ****************************************************************************//**
129 *
130 * The function is called during device startup.
131 *
132 *******************************************************************************/
SystemIrqInit(void)133 void SystemIrqInit(void)
134 {
135 for (int i=0; i<(int)CPUSS_SYSTEM_INT_NR; i++)
136 {
137 Cy_SystemIrqUserTable[i] = DEFAULT_HANDLER_NAME;
138 }
139
140 Cy_SysInt_SystemIrqUserTableRamPointer = Cy_SystemIrqUserTable;
141 }
142
143 /*******************************************************************************
144 * Function Name: Cy_SystemInit
145 ****************************************************************************//**
146 *
147 * The function is called during device startup.
148 *
149 *******************************************************************************/
Cy_SystemInit(void)150 __WEAK void Cy_SystemInit(void)
151 {
152 /* Empty weak function. The actual implementation to be in the user application
153 * as strong function.
154 */
155 }
156
157 /*******************************************************************************
158 * Function Name: SystemCoreClockUpdate
159 ****************************************************************************//**
160 *
161 * Gets core clock frequency and updates \ref SystemCoreClock, \ref
162 * cy_Hfclk0FreqHz, and \ref cy_PeriClkFreqHz.
163 *
164 * Updates global variables used by the \ref Cy_SysLib_Delay(), \ref
165 * Cy_SysLib_DelayUs(), and \ref Cy_SysLib_DelayCycles().
166 *
167 *******************************************************************************/
SystemCoreClockUpdate(void)168 void SystemCoreClockUpdate (void)
169 {
170 uint32_t pathFreqHz;
171 uint32_t clkHfPath;
172
173 /* Get frequency for the high-frequency clock # 0 */
174 clkHfPath = CY_SYSCLK_CLK_CORE_HF_PATH_NUM;
175
176 pathFreqHz = Cy_SysClk_ClkHfGetFrequency(clkHfPath);
177
178 SystemCoreClock = pathFreqHz;
179
180 cy_Hfclk0FreqHz = SystemCoreClock;
181
182 /* Get frequency for the high-frequency clock # 2 , whcih is used for PERI PCLK*/
183 clkHfPath = CY_SYSCLK_CLK_PERI_HF_PATH_NUM;
184
185 pathFreqHz = Cy_SysClk_ClkHfGetFrequency(clkHfPath);
186
187 cy_PeriClkFreqHz = pathFreqHz;
188
189 /* Sets clock frequency for Delay API */
190 cy_delayFreqHz = SystemCoreClock;
191 cy_delayFreqMhz = (uint32_t)((cy_delayFreqHz + CY_DELAY_1M_MINUS_1_THRESHOLD) / CY_DELAY_1M_THRESHOLD);
192 cy_delayFreqKhz = (cy_delayFreqHz + CY_DELAY_1K_MINUS_1_THRESHOLD) / CY_DELAY_1K_THRESHOLD;
193
194 /* Get the frequency of AHB source, CLK HF0 is the source for AHB*/
195 cy_AhbFreqHz = Cy_SysClk_ClkHfGetFrequency(0UL);
196 }
197
198
199 /*******************************************************************************
200 * Function Name: Cy_DefaultUserHandler
201 ****************************************************************************//**
202 *
203 *The Handler is called when the CPU attempts to call IRQ that has not been mapped to user functions.
204 *
205 *
206 *******************************************************************************/
Cy_DefaultUserHandler(void)207 void Cy_DefaultUserHandler(void)
208 {
209 // This IRQ occurred because CPU attempted to call IRQ that has not been mapped to user function
210 while(1);
211 }
212
213
214 /*******************************************************************************
215 * Function Name: CM7_CpuIntr_Handler
216 ****************************************************************************//**
217 *
218 * The Inline handler for CPU interrupt.
219 * The system interrupt mapped to CPU interrupt will be fetched and executed
220 *
221 *******************************************************************************/
222 CY_SECTION_ITCM_BEGIN
CM7_CpuIntr_Handler(uint8_t intrNum)223 void CM7_CpuIntr_Handler(uint8_t intrNum)
224 {
225 uint32_t system_int_idx;
226 cy_israddress handler;
227
228 #ifdef CORE_NAME_CM7_0
229 if((_FLD2VAL(CPUSS_CM7_0_INT_STATUS_SYSTEM_INT_VALID, CPUSS_CM7_0_INT_STATUS[intrNum])))
230 {
231 system_int_idx = _FLD2VAL(CPUSS_CM7_0_INT_STATUS_SYSTEM_INT_IDX, CPUSS_CM7_0_INT_STATUS[intrNum]);
232 handler = Cy_SystemIrqUserTable[system_int_idx];
233 handler(); // jump to system interrupt handler
234 }
235 #endif
236 #ifdef CORE_NAME_CM7_1
237 if((_FLD2VAL(CPUSS_CM7_1_INT_STATUS_SYSTEM_INT_VALID, CPUSS_CM7_1_INT_STATUS[intrNum])))
238 {
239 system_int_idx = _FLD2VAL(CPUSS_CM7_1_INT_STATUS_SYSTEM_INT_IDX, CPUSS_CM7_1_INT_STATUS[intrNum]);
240 handler = Cy_SystemIrqUserTable[system_int_idx];
241 handler(); // jump to system interrupt handler
242 }
243 #endif
244 NVIC_ClearPendingIRQ((IRQn_Type)intrNum);
245 }
246 CY_SECTION_ITCM_END
247
248