1 /*
2 * Copyright (c) 2020 - 2024 Renesas Electronics Corporation and/or its affiliates
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6 
7 /***********************************************************************************************************************
8  * Includes   <System Includes> , "Project Includes"
9  **********************************************************************************************************************/
10 #include "bsp_api.h"
11 
12 /***********************************************************************************************************************
13  * Macro definitions
14  **********************************************************************************************************************/
15 
16 /***********************************************************************************************************************
17  * Typedef definitions
18  **********************************************************************************************************************/
19 
20 /***********************************************************************************************************************
21  * Exported global variables (to be accessed by other files)
22  **********************************************************************************************************************/
23 
24 /***********************************************************************************************************************
25  * Exported global functions (to be accessed by other files)
26  **********************************************************************************************************************/
27 #if (1 == _RZN_ORDINAL)
28 extern void bsp_master_mpu_init(void);
29 extern void bsp_global_system_counter_init(void);
30 
31 #endif
32 
33 #if defined(BSP_CFG_CORE_CR52)
34  #if (BSP_FEATURE_TFU_SUPPORTED & BSP_CFG_USE_TFU_MATHLIB)
35 extern void bsp_tfu_init(void);
36 
37  #endif
38 #endif
39 
40 #if BSP_CFG_C_RUNTIME_INIT
41 extern void bsp_static_constructor_init(void);
42 extern void bsp_loader_data_init(void);
43 extern void bsp_loader_bss_init(void);
44 
45 #endif
46 
47 #if !BSP_CFG_PORT_PROTECT
48 extern void bsp_release_port_protect(void);
49 
50 #endif
51 
52 #if !(BSP_CFG_RAM_EXECUTION)
53 
54 extern void bsp_copy_to_ram(void);
55 
56 extern void bsp_application_bss_init(void);
57 
58 #endif
59 
60 extern void bsp_memory_protect_setting(void);
61 extern void bsp_irq_cfg_common(void);
62 
63 extern void R_BSP_WarmStart(bsp_warm_start_event_t event);
64 
65 /***********************************************************************************************************************
66  * Private global variables and functions
67  **********************************************************************************************************************/
68 int32_t main(void);
69 
70 #if BSP_CFG_EARLY_INIT
71 static void bsp_init_uninitialized_vars(void);
72 
73 #endif
74 
75 /*******************************************************************************************************************//**
76  * @addtogroup BSP_MCU
77  * @{
78  **********************************************************************************************************************/
79 
80 /*******************************************************************************************************************//**
81  * Initialize the MCU and the runtime environment.
82  **********************************************************************************************************************/
SystemInit(void)83 void SystemInit (void)
84 {
85 #if BSP_CFG_EARLY_INIT
86 
87     /* Initialize uninitialized BSP variables early for use in R_BSP_WarmStart. */
88     bsp_init_uninitialized_vars();
89 #endif
90 
91     /* Call before initializing clock and variables. */
92     R_BSP_WarmStart(BSP_WARM_START_RESET);
93 
94 #if (1 == _RZN_ORDINAL)
95 
96     /* Configure system clocks. */
97     bsp_clock_init();
98 #endif
99 
100     /* Call post clock initialization hook. */
101     R_BSP_WarmStart(BSP_WARM_START_POST_CLOCK);
102 
103 #if BSP_CFG_C_RUNTIME_INIT
104 
105     /* Copy the primary core loader data from external Flash to internal RAM. */
106     bsp_loader_data_init();
107 
108     /* Clear loader bss section in internal RAM. */
109     bsp_loader_bss_init();
110 #endif
111 
112 #if BSP_FEATURE_ADDRESS_EXPANDER_SUPPORTED
113  #if (1 == _RZN_ORDINAL)
114 
115     /* Initialize the Address Expander settings. */
116     bsp_address_expander_init();
117  #endif
118 #endif
119 
120 #if BSP_FEATURE_TZC400_SUPPORTED
121  #if (1 == _RZN_ORDINAL)
122 
123     /* Initialize the TZC-400 settings. */
124     bsp_tzc_400_cfg();
125  #endif
126 #endif
127 
128     /* Initialize SystemCoreClock variable. */
129     SystemCoreClockUpdate();
130 
131     /* Set memory attributes, etc. */
132     bsp_memory_protect_setting();
133 
134 #if !(BSP_CFG_RAM_EXECUTION)
135 
136     /* Copy the application program from external Flash to internal RAM.
137      * In the case of multi-core operation, copies each section (vector, loader(program/data), user(program/data)) of
138      * the secondary core (or later). */
139     bsp_copy_to_ram();
140 
141     /* Clear bss section in internal RAM. */
142     bsp_application_bss_init();
143 #endif
144 
145 #if BSP_CFG_C_RUNTIME_INIT
146 
147     /* Initialize static constructors */
148     bsp_static_constructor_init();
149 #endif
150 
151 #if !BSP_CFG_PORT_PROTECT
152 
153     /* When writing to the PRCR register the upper 8-bits must be the correct key. Set lower bits to 0 to
154      * disable writes. */
155     bsp_release_port_protect();
156 #endif
157 
158     /* Call Post C runtime initialization hook. */
159     R_BSP_WarmStart(BSP_WARM_START_POST_C);
160 
161 #if (1 == _RZN_ORDINAL)
162  #if BSP_CFG_SEMAPHORE_ENABLE
163 
164     /* Initialize semaphores required for synchronization and exclusive control between CPUs. */
165     bsp_semaphore_init();
166  #endif
167 
168     /* Initialize the Master-MPU settings. */
169     bsp_master_mpu_init();
170 
171     /* Initialize global system counter. The counter is enabled and is incrementing. */
172     bsp_global_system_counter_init();
173 #endif
174 
175     /* GIC initialization */
176     bsp_irq_cfg_common();
177 
178     /* Initialize GIC interrupts. */
179     bsp_irq_cfg();
180 
181 #if defined(BSP_CFG_CORE_CR52)
182  #if (BSP_FEATURE_TFU_SUPPORTED & BSP_CFG_USE_TFU_MATHLIB)
183 
184     /* Initialize the TFU settings. */
185     bsp_tfu_init();
186  #endif
187 #endif
188 
189     /* Jump to main. */
190     main();
191 }
192 
193 /** @} (end addtogroup BSP_MCU) */
194 
195 #if BSP_CFG_EARLY_INIT
196 
197 /*******************************************************************************************************************//**
198  * Initialize BSP variables not handled by C runtime startup.
199  **********************************************************************************************************************/
bsp_init_uninitialized_vars(void)200 static void bsp_init_uninitialized_vars (void)
201 {
202     g_protect_port_counter = 0;
203 
204     extern volatile uint16_t g_protect_counters[];
205     for (uint32_t i = 0; i < 4; i++)
206     {
207         g_protect_counters[i] = 0;
208     }
209 
210     SystemCoreClockUpdate();
211 }
212 
213 #endif
214