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 "max32670.h"
25 #include "gcr_regs.h"
26 #include "pwrseq_regs.h"
27 #include "mxc_sys.h"
28
29 extern void (*const __vector_table[])(void);
30
31 extern void (*const __isr_vector[])(void);
32
33 uint32_t SystemCoreClock = HIRC_FREQ;
34
35 /*
36 The libc implementation from GCC 11+ depends on _getpid and _kill in some places.
37 There is no concept of processes/PIDs in the baremetal PeriphDrivers, therefore
38 we implement stub functions that return an error code to resolve linker warnings.
39 */
_getpid(void)40 __weak int _getpid(void)
41 {
42 return E_NOT_SUPPORTED;
43 }
44
_kill(void)45 __weak int _kill(void)
46 {
47 return E_NOT_SUPPORTED;
48 }
49
SystemCoreClockUpdate(void)50 __weak void SystemCoreClockUpdate(void)
51 {
52 uint32_t base_freq, div, clk_src;
53
54 // Get the clock source and frequency
55 clk_src = (MXC_GCR->clkctrl & MXC_F_GCR_CLKCTRL_SYSCLK_SEL);
56 switch (clk_src) {
57 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_EXTCLK:
58 base_freq = EXTCLK_FREQ;
59 break;
60 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_ERFO:
61 base_freq = ERFO_FREQ;
62 break;
63 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_INRO:
64 base_freq = INRO_FREQ;
65 break;
66 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_IPO:
67 base_freq = IPO_FREQ;
68 break;
69 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_IBRO:
70 base_freq = IBRO_FREQ;
71 break;
72 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_ERTCO:
73 base_freq = ERTCO_FREQ;
74 break;
75 default:
76 // Codes 001 and 111 are reserved.
77 // This code should never execute, however, initialize to safe value.
78 base_freq = HIRC_FREQ;
79 break;
80 }
81 // Get the clock divider
82 if (clk_src == MXC_S_GCR_CLKCTRL_SYSCLK_SEL_IPO) {
83 base_freq = base_freq >> ((MXC_GCR->clkctrl & MXC_F_GCR_CLKCTRL_IPO_DIV) >>
84 MXC_F_GCR_CLKCTRL_IPO_DIV_POS);
85 }
86 div = (MXC_GCR->clkctrl & MXC_F_GCR_CLKCTRL_SYSCLK_DIV) >> MXC_F_GCR_CLKCTRL_SYSCLK_DIV_POS;
87
88 SystemCoreClock = base_freq >> div;
89 }
90
91 /* This function is called before C runtime initialization and can be
92 * implemented by the application for early initializations. If a value other
93 * than '0' is returned, the C runtime initialization will be skipped.
94 *
95 * You may over-ride this function in your program by defining a custom
96 * PreInit(), but care should be taken to reproduce the initialization steps
97 * or a non-functional system may result.
98 */
PreInit(void)99 __weak int PreInit(void)
100 {
101 /* Do nothing */
102 return 0;
103 }
104
105 /* This function can be implemented by the application to initialize the board */
Board_Init(void)106 __weak int Board_Init(void)
107 {
108 /* Do nothing */
109 return 0;
110 }
111
112 /* This function is called just before control is transferred to main().
113 *
114 * You may over-ride this function in your program by defining a custom
115 * SystemInit(), but care should be taken to reproduce the initialization
116 * steps or a non-functional system may result.
117 */
SystemInit(void)118 __weak void SystemInit(void)
119 {
120 /* Configure the interrupt controller to use the application vector table in */
121 /* the application space */
122 #if defined(__CC_ARM) || defined(__ARMCC_VERSION) || defined(__GNUC__)
123 /* IAR sets the VTOR pointer incorrectly and causes stack corruption */
124 SCB->VTOR = (uint32_t)__isr_vector;
125 #endif /* __CC_ARM || __GNUC__ */
126
127 #if defined __ICCARM__
128 SCB->VTOR = (uint32_t)__vector_table;
129 #endif
130
131 /* Make sure interrupts are enabled. */
132 __enable_irq();
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 SystemCoreClockUpdate();
144
145 /* Make sure INRO is enabled. INRO should already be enabled during power up. */
146 MXC_PWRSEQ->lpcn |= MXC_F_PWRSEQ_LPCN_INRO_EN;
147
148 MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_GPIO0);
149 MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_GPIO1);
150
151 Board_Init();
152 }
153
154 #if defined(__CC_ARM) || defined(__ARMCC_VERSION)
155 /* Global variable initialization does not occur until post scatterload in Keil tools.*/
156
157 /* External function called after our post scatterload function implementation. */
158 extern void $Super$$__main_after_scatterload(void);
159
160 /**
161 * @brief Initialization function for SystemCoreClock and Board_Init.
162 * @details $Sub$$__main_after_scatterload is called during system startup in the Keil
163 * toolset. Global variable and static variable space must be set up by the compiler
164 * prior to using these memory spaces. Setting up the SystemCoreClock and Board_Init
165 * require global memory for variable storage and are called from this function in
166 * the Keil tool chain.
167 */
$Sub$$__main_after_scatterload(void)168 void $Sub$$__main_after_scatterload(void)
169 {
170 SystemInit();
171 $Super$$__main_after_scatterload();
172 while (1) {}
173 }
174 #endif /* __CC_ARM */
175