1 /***************************************************************************//**
2 * \file startup_cat1b_cm33.c
3 * \version 1.2
4 *
5 * The CAT1B CM33 startup source.
6 *
7 ********************************************************************************
8 * \copyright
9 * Copyright (c) (2020-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)
29 
30 #include <stdio.h>
31 #include <stdint.h>
32 #include <stddef.h>
33 
34 #include "startup_cat1b.h"
35 #include "cy_syslib.h"
36 #include "cmsis_compiler.h"
37 
38 /*----------------------------------------------------------------------------
39   External References
40  *----------------------------------------------------------------------------*/
41 extern unsigned int __INITIAL_SP;
42 extern unsigned int __STACK_LIMIT;
43 
44 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
45 extern uint32_t __STACK_SEAL;
46 #endif
47 
48 extern __NO_RETURN void __PROGRAM_START(void);
49 
50 #if defined(__ARMCC_VERSION)
51 #if defined(CY_PDL_TZ_ENABLED)
52 cy_israddress_cat1b __s_vector_table_rw[VECTORTABLE_SIZE] __attribute__( ( section(".bss.noinit.RESET_RAM"))) __attribute__((aligned(VECTORTABLE_ALIGN)));
53 #else
54 cy_israddress_cat1b __ns_vector_table_rw[VECTORTABLE_SIZE] __attribute__( ( section(".bss.noinit.RESET_RAM"))) __attribute__((aligned(VECTORTABLE_ALIGN)));
55 #endif
56 #elif defined (__GNUC__)
57 #if defined(CY_PDL_TZ_ENABLED)
58 cy_israddress_cat1b __s_vector_table_rw[VECTORTABLE_SIZE] __attribute__( ( section(".ram_vectors"))) __attribute__((aligned(VECTORTABLE_ALIGN)));
59 #else
60 cy_israddress_cat1b __ns_vector_table_rw[VECTORTABLE_SIZE] __attribute__( ( section(".ram_vectors"))) __attribute__((aligned(VECTORTABLE_ALIGN)));
61 #endif
62 #elif defined (__ICCARM__)
63 #if defined(CY_PDL_TZ_ENABLED)
64 cy_israddress_cat1b __s_vector_table_rw[VECTORTABLE_SIZE]  __attribute__( ( section(".intvec_ram"))) __attribute__((aligned(VECTORTABLE_ALIGN)));
65 #else
66 cy_israddress_cat1b __ns_vector_table_rw[VECTORTABLE_SIZE] __attribute__( ( section(".intvec_ram"))) __attribute__((aligned(VECTORTABLE_ALIGN)));
67 #endif
68 #else
69     #error "An unsupported toolchain"
70 #endif  /* (__ARMCC_VERSION) */
71 
72 
73 /*----------------------------------------------------------------------------
74   Internal References
75  *----------------------------------------------------------------------------*/
76 __NO_RETURN void Reset_Handler (void);
77 void SysLib_FaultHandler(uint32_t const *faultStackAddr);
78 void Default_Handler(void);
79 
SysLib_FaultHandler(uint32_t const * faultStackAddr)80 void SysLib_FaultHandler(uint32_t const *faultStackAddr)
81 {
82     Cy_SysLib_FaultHandler(faultStackAddr);
83 }
84 
85 /*----------------------------------------------------------------------------
86   Default Handler for Exceptions / Interrupts
87  *----------------------------------------------------------------------------*/
Default_Handler(void)88 void Default_Handler(void)
89 {
90     while(1);
91 }
92 
93 
94 /*----------------------------------------------------------------------------
95   Exception / Interrupt Handler
96  *----------------------------------------------------------------------------*/
97 void NMIException_Handler(void);
98 void HardFault_Handler(void);
99 void InterruptHandler(void);
100 __WEAK void cy_toolchain_init(void);
101 
102 
103 // Exception Vector Table & Handlers
104 //----------------------------------------------------------------
NMIException_Handler(void)105 void NMIException_Handler(void)
106 {
107     __asm volatile(
108         "bkpt #10\n"
109         "B .\n"
110     );
111 }
112 
HardFault_Handler(void)113 void HardFault_Handler(void)
114 {
115     __asm (
116         "MRS R0, CONTROL\n"
117         "TST R0, #2\n"
118         "ITE EQ\n"
119         "MRSEQ R0, MSP\n"
120         "MRSNE R0, PSP\n"
121         "B SysLib_FaultHandler\n"
122     );
123 }
124 
125 
InterruptHandler(void)126 void InterruptHandler(void)
127 {
128     __asm volatile(
129         "bkpt #1\n"
130         "B .\n"
131     );
132 }
133 
134 void MemManage_Handler      (void) __attribute__ ((weak, alias("Default_Handler")));
135 void BusFault_Handler       (void) __attribute__ ((weak, alias("HardFault_Handler")));
136 void UsageFault_Handler     (void) __attribute__ ((weak, alias("HardFault_Handler")));
137  void SVC_Handler            (void) __attribute__ ((weak, alias("HardFault_Handler")));
138 void DebugMon_Handler       (void) __attribute__ ((weak, alias("Default_Handler")));
139 void PendSV_Handler         (void) __attribute__ ((weak, alias("Default_Handler")));
140 void SysTick_Handler        (void) __attribute__ ((weak, alias("Default_Handler")));
141 
142 void Interrupt0_Handler     (void) __attribute__ ((weak, alias("InterruptHandler")));
143 void Interrupt1_Handler     (void) __attribute__ ((weak, alias("InterruptHandler")));
144 void Interrupt2_Handler     (void) __attribute__ ((weak, alias("InterruptHandler")));
145 void Interrupt3_Handler     (void) __attribute__ ((weak, alias("InterruptHandler")));
146 void Interrupt4_Handler     (void) __attribute__ ((weak, alias("InterruptHandler")));
147 void Interrupt5_Handler     (void) __attribute__ ((weak, alias("InterruptHandler")));
148 void Interrupt6_Handler     (void) __attribute__ ((weak, alias("InterruptHandler")));
149 void Interrupt7_Handler     (void) __attribute__ ((weak, alias("InterruptHandler")));
150 void Interrupt8_Handler     (void) __attribute__ ((weak, alias("InterruptHandler")));
151 void Interrupt9_Handler     (void) __attribute__ ((weak, alias("InterruptHandler")));
152 
153 /*----------------------------------------------------------------------------
154   Exception / Interrupt Vector table
155  *----------------------------------------------------------------------------*/
156 
157 const cy_israddress __Vectors[VECTORTABLE_SIZE];
158 
159 #if defined ( __GNUC__ )
160 #pragma GCC diagnostic push
161 #pragma GCC diagnostic ignored "-Wpedantic"
162 #endif
163 
164 const cy_israddress __Vectors[VECTORTABLE_SIZE] __VECTOR_TABLE_ATTRIBUTE  = {
165   (cy_israddress)(&__INITIAL_SP),                          /*     Initial Stack Pointer */
166   (cy_israddress)Reset_Handler,                            /*     Reset Handler */
167   (cy_israddress)NMIException_Handler,                     /* -14 NMI Handler */
168   (cy_israddress)HardFault_Handler,                        /* -13 Hard Fault Handler */
169   (cy_israddress)MemManage_Handler,                        /* -12 MPU Fault Handler */
170   (cy_israddress)BusFault_Handler,                         /* -11 Bus Fault Handler */
171   (cy_israddress)UsageFault_Handler,                       /* -10 Usage Fault Handler */
172   0,                                                       /*  -9 Secure Fault Handler */
173   0,                                                       /*     Reserved */
174   0,                                                       /*     Reserved */
175   0,                                                       /*     Reserved */
176   (cy_israddress)SVC_Handler,                              /*  -5 SVCall Handler */
177   0,                                                       /*  -4 Debug Monitor Handler */
178   0,                                        /*     Reserved */
179   (cy_israddress)PendSV_Handler,                           /*  -2 PendSV Handler */
180   (cy_israddress)SysTick_Handler,                          /*  -1 SysTick Handler */
181 
182   /* Interrupts */
183   (cy_israddress)Interrupt0_Handler,                       /*   0 Interrupt 0 */
184   (cy_israddress)Interrupt1_Handler,                       /*   1 Interrupt 1 */
185   (cy_israddress)Interrupt2_Handler,                       /*   2 Interrupt 2 */
186   (cy_israddress)Interrupt3_Handler,                       /*   3 Interrupt 3 */
187   (cy_israddress)Interrupt4_Handler,                       /*   4 Interrupt 4 */
188   (cy_israddress)Interrupt5_Handler,                       /*   5 Interrupt 5 */
189   (cy_israddress)Interrupt6_Handler,                       /*   6 Interrupt 6 */
190   (cy_israddress)Interrupt7_Handler,                       /*   7 Interrupt 7 */
191   (cy_israddress)Interrupt8_Handler,                       /*   8 Interrupt 8 */
192   (cy_israddress)Interrupt9_Handler,                       /*   9 Interrupt 9 */
193                                             /* Interrupts 10 .. 480 are left out */
194 
195 };
196 
197 #if defined ( __GNUC__ )
198 #pragma GCC diagnostic pop
199 #endif
200 
201 /* Provide empty __WEAK implementation for the low-level initialization
202    routine required by the RTOS-enabled applications.
203    clib-support library provides FreeRTOS-specific implementation:
204    https://github.com/Infineon/clib-support */
cy_toolchain_init(void)205 __WEAK void cy_toolchain_init(void)
206 {
207 }
208 
209 #if defined(__GNUC__) && !defined(__ARMCC_VERSION)
210 /* GCC: newlib crt0 _start executes software_init_hook.
211    The cy_toolchain_init hook provided by clib-support library must execute
212    after static data initialization and before static constructors. */
213 void software_init_hook();
software_init_hook()214 void software_init_hook()
215 {
216     cy_toolchain_init();
217 }
218 #elif defined(__ICCARM__)
219 /* Initialize data section */
220 void __iar_data_init3(void);
221 
222 /* Call the constructors of all global objects */
223 void __iar_dynamic_initialization(void);
224 
225 /* Define strong version to return zero for __iar_program_start
226    to skip data sections initialization (__iar_data_init3). */
227 int __low_level_init(void);
__low_level_init(void)228 int __low_level_init(void)
229 {
230     return 0;
231 }
232 #else
233 /**/
234 #endif /* defined(__GNUC__) && !defined(__ARMCC_VERSION) */
235 
236 /*----------------------------------------------------------------------------
237   Reset Handler called on controller reset
238  *----------------------------------------------------------------------------*/
Reset_Handler(void)239 __NO_RETURN void Reset_Handler(void)
240 {
241     /* Disable I cache */
242     ICACHE0->CTL = ICACHE0->CTL & (~ICACHE_CTL_CA_EN_Msk);
243 
244     /* Enable ECC */
245     //ICACHE0->CTL = ICACHE0->CTL | ICACHE_CTL_ECC_EN_Msk;
246 
247     /* Enable I cache */
248     ICACHE0->CTL = ICACHE0->CTL | ICACHE_CTL_CA_EN_Msk;
249 
250     __disable_irq();
251 
252     for (uint32_t count = 0; count < VECTORTABLE_SIZE; count++)
253     {
254         #if defined(CY_PDL_TZ_ENABLED)
255         __s_vector_table_rw[count] =__Vectors[count];
256         #else
257         __ns_vector_table_rw[count] =__Vectors[count];
258         #endif
259     }
260     #if defined(CY_PDL_TZ_ENABLED)
261     SCB->VTOR = (uint32_t)__s_vector_table_rw;
262     #else
263     SCB->VTOR = (uint32_t)__ns_vector_table_rw;
264     #endif
265 
266     __DMB();
267 
268     __set_MSPLIM((uint32_t)(&__STACK_LIMIT));
269 
270     SystemInit();
271 
272 #if defined(__ICCARM__)
273     /* Initialize data section */
274     __iar_data_init3();
275 
276     /* Initialization hook for RTOS environment  */
277     cy_toolchain_init();
278 
279     /* Call the constructors of all global objects */
280     __iar_dynamic_initialization();
281 #endif
282 
283    __PROGRAM_START();
284 }
285 
286 
287 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
288   #pragma clang diagnostic push
289   #pragma clang diagnostic ignored "-Wmissing-noreturn"
290 #endif
291 
292 
293 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
294   #pragma clang diagnostic pop
295 #endif
296 
297 #endif /* CY_IP_M33SYSCPUSS */
298