1 /******************************************************************************
2 *
3 * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
4 * Analog Devices, Inc.),
5 * Copyright (C) 2023-2024 Analog Devices, Inc.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ******************************************************************************/
20
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include "max32680.h"
25 #include "gcr_regs.h"
26 #include "mxc_sys.h"
27 #include "flc_regs.h"
28 #include "icc.h"
29
30 extern void (*const __isr_vector[])(void);
31
32 uint32_t SystemCoreClock = HIRC_FREQ;
33
34 /*
35 The libc implementation from GCC 11+ depends on _getpid and _kill in some places.
36 There is no concept of processes/PIDs in the baremetal PeriphDrivers, therefore
37 we implement stub functions that return an error code to resolve linker warnings.
38 */
_getpid(void)39 __weak int _getpid(void)
40 {
41 return E_NOT_SUPPORTED;
42 }
43
_kill(void)44 __weak int _kill(void)
45 {
46 return E_NOT_SUPPORTED;
47 }
48
SystemCoreClockUpdate(void)49 __weak void SystemCoreClockUpdate(void)
50 {
51 uint32_t base_freq, div, clk_src;
52
53 // Get the clock source and frequency
54 clk_src = (MXC_GCR->clkctrl & MXC_F_GCR_CLKCTRL_SYSCLK_SEL);
55 switch (clk_src) {
56 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_EXTCLK:
57 base_freq = EXTCLK_FREQ;
58 break;
59 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_ERFO:
60 base_freq = ERFO_FREQ;
61 break;
62 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_INRO:
63 base_freq = INRO_FREQ;
64 break;
65 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_IPO:
66 base_freq = IPO_FREQ;
67 break;
68 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_ISO:
69 base_freq = ISO_FREQ;
70 break;
71 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_IBRO:
72 base_freq = IBRO_FREQ;
73 break;
74 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_ERTCO:
75 base_freq = ERTCO_FREQ;
76 break;
77 default:
78 // Codes 001 and 111 are reserved.
79 // This code should never execute, however, initialize to safe value.
80 base_freq = HIRC_FREQ;
81 break;
82 }
83
84 // Get the clock divider
85 div = (MXC_GCR->clkctrl & MXC_F_GCR_CLKCTRL_SYSCLK_DIV) >> MXC_F_GCR_CLKCTRL_SYSCLK_DIV_POS;
86
87 SystemCoreClock = base_freq >> div;
88 }
89
90 /* This function is called before C runtime initialization and can be
91 * implemented by the application for early initializations. If a value other
92 * than '0' is returned, the C runtime initialization will be skipped.
93 *
94 * You may over-ride this function in your program by defining a custom
95 * PreInit(), but care should be taken to reproduce the initialization steps
96 * or a non-functional system may result.
97 */
PreInit(void)98 __weak int PreInit(void)
99 {
100 /* Do nothing */
101 return 0;
102 }
103
104 /* This function can be implemented by the application to initialize the board */
Board_Init(void)105 __weak int Board_Init(void)
106 {
107 /* Do nothing */
108 return 0;
109 }
110
PalSysInit(void)111 __weak void PalSysInit(void) {}
112
113 /* This function is called just before control is transferred to main().
114 *
115 * You may over-ride this function in your program by defining a custom
116 * SystemInit(), but care should be taken to reproduce the initialization
117 * steps or a non-functional system may result.
118 */
SystemInit(void)119 __weak void SystemInit(void)
120 {
121 /* Configure the interrupt controller to use the application vector table in */
122 /* the application space */
123 #if defined(__CC_ARM) || defined(__GNUC__)
124 /* IAR sets the VTOR pointer incorrectly and causes stack corruption */
125 SCB->VTOR = (uint32_t)__isr_vector;
126 #endif /* __CC_ARM || __GNUC__ */
127
128 /* Make sure interrupts are enabled. */
129 __enable_irq();
130
131 /* Enable instruction cache */
132 MXC_ICC_Enable(MXC_ICC0);
133
134 /* Enable FPU on Cortex-M4, which occupies coprocessor slots 10 & 11 */
135 /* Grant full access, per "Table B3-24 CPACR bit assignments". */
136 /* DDI0403D "ARMv7-M Architecture Reference Manual" */
137 SCB->CPACR |= SCB_CPACR_CP10_Msk | SCB_CPACR_CP11_Msk;
138 __DSB();
139 __ISB();
140
141 /* Change system clock source to the main high-speed clock */
142 MXC_SYS_Clock_Select(MXC_SYS_CLOCK_IPO);
143 MXC_SYS_SetClockDiv(MXC_SYS_CLOCK_DIV_1);
144 SystemCoreClockUpdate();
145
146 Board_Init();
147
148 PalSysInit();
149 }
150
151 #if defined(__CC_ARM)
152 /* Global variable initialization does not occur until post scatterload in Keil tools.*/
153
154 /* External function called after our post scatterload function implementation. */
155 extern void $Super$$__main_after_scatterload(void);
156
157 /**
158 * @brief Initialization function for SystemCoreClock and Board_Init.
159 * @details $Sub$$__main_after_scatterload is called during system startup in the Keil
160 * toolset. Global variable and static variable space must be set up by the compiler
161 * prior to using these memory spaces. Setting up the SystemCoreClock and Board_Init
162 * require global memory for variable storage and are called from this function in
163 * the Keil tool chain.
164 */
$Sub$$__main_after_scatterload(void)165 void $Sub$$__main_after_scatterload(void)
166 {
167 SystemInit();
168 $Super$$__main_after_scatterload();
169 while (1) {}
170 }
171 #endif /* __CC_ARM */
172