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 "max32675.h"
25 #include "gcr_regs.h"
26 #include "mxc_sys.h"
27 #include "pwrseq_regs.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         uint32_t ovr = (MXC_PWRSEQ->lpcn & MXC_F_PWRSEQ_LPCN_OVR);
84         switch (ovr) {
85         case MXC_S_PWRSEQ_LPCN_OVR_0_9V:
86             base_freq = base_freq >> 3;
87             break;
88         case MXC_S_PWRSEQ_LPCN_OVR_1_0V:
89             base_freq = base_freq >> 1;
90             break;
91         case MXC_S_PWRSEQ_LPCN_OVR_1_1V:
92         default:
93             /* Nothing to do here.
94                 OVR = 1.1V means the clock runs full speed. */
95             break;
96         }
97 
98         base_freq = base_freq >> ((MXC_GCR->clkctrl & MXC_F_GCR_CLKCTRL_IPO_DIV) >>
99                                   MXC_F_GCR_CLKCTRL_IPO_DIV_POS);
100     }
101     div = (MXC_GCR->clkctrl & MXC_F_GCR_CLKCTRL_SYSCLK_DIV) >> MXC_F_GCR_CLKCTRL_SYSCLK_DIV_POS;
102 
103     SystemCoreClock = base_freq >> div;
104 }
105 
106 /* This function is called before C runtime initialization and can be
107  * implemented by the application for early initializations. If a value other
108  * than '0' is returned, the C runtime initialization will be skipped.
109  *
110  * You may over-ride this function in your program by defining a custom
111  *  PreInit(), but care should be taken to reproduce the initialization steps
112  *  or a non-functional system may result.
113  */
PreInit(void)114 __weak int PreInit(void)
115 {
116     /* Do nothing */
117     return 0;
118 }
119 
120 /* This function can be implemented by the application to initialize the board */
Board_Init(void)121 __weak int Board_Init(void)
122 {
123     /* Do nothing */
124     return 0;
125 }
126 
127 /* This function is called just before control is transferred to main().
128  *
129  * You may over-ride this function in your program by defining a custom
130  *  SystemInit(), but care should be taken to reproduce the initialization
131  *  steps or a non-functional system may result.
132  */
SystemInit(void)133 __weak void SystemInit(void)
134 {
135     /* Configure the interrupt controller to use the application vector table in */
136     /* the application space */
137 #if defined(__CC_ARM) || defined(__GNUC__)
138     /* IAR sets the VTOR pointer incorrectly and causes stack corruption */
139     SCB->VTOR = (uint32_t)__isr_vector;
140 #endif /* __CC_ARM || __GNUC__ */
141 
142 #if defined __ICCARM__
143     SCB->VTOR = (uint32_t)__vector_table;
144 #endif
145 
146     /* Make sure interrupts are enabled. */
147     __enable_irq();
148 
149     /* Enable FPU on Cortex-M4, which occupies coprocessor slots 10 & 11 */
150     /* Grant full access, per "Table B3-24 CPACR bit assignments". */
151     /* DDI0403D "ARMv7-M Architecture Reference Manual" */
152     SCB->CPACR |= SCB_CPACR_CP10_Msk | SCB_CPACR_CP11_Msk;
153     __DSB();
154     __ISB();
155 
156     /* Change system clock source to the main high-speed clock */
157     MXC_SYS_Clock_Select(MXC_SYS_CLOCK_IPO);
158     SystemCoreClockUpdate();
159 
160     MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_GPIO0);
161     MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_GPIO1);
162 
163     Board_Init();
164 }
165 
166 #if defined(__CC_ARM)
167 /* Global variable initialization does not occur until post scatterload in Keil tools.*/
168 
169 /* External function called after our post scatterload function implementation. */
170 extern void $Super$$__main_after_scatterload(void);
171 
172 /**
173  * @brief   Initialization function for SystemCoreClock and Board_Init.
174  * @details $Sub$$__main_after_scatterload is called during system startup in the Keil
175  *          toolset. Global variable and static variable space must be set up by the compiler
176  *          prior to using these memory spaces. Setting up the SystemCoreClock and Board_Init
177  *          require global memory for variable storage and are called from this function in
178  *          the Keil tool chain.
179  */
$Sub$$__main_after_scatterload(void)180 void $Sub$$__main_after_scatterload(void)
181 {
182     SystemInit();
183     $Super$$__main_after_scatterload();
184     while (1) {}
185 }
186 #endif /* __CC_ARM */
187