1 /**
2  * @file       system_max32660.c
3  * @brief      System-level initialization implementation file
4  */
5 
6 /******************************************************************************
7  *
8  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9  * Analog Devices, Inc.),
10  * Copyright (C) 2023-2024 Analog Devices, Inc.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  ******************************************************************************/
25 
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include "max32660.h"
30 #include "gcr_regs.h"
31 #include "pwrseq_regs.h"
32 #include "tmr_regs.h"
33 #include "wdt_regs.h"
34 #include "mxc_sys.h"
35 
36 extern void (*const __isr_vector[])(void);
37 uint32_t SystemCoreClock = HIRC96_FREQ;
38 
39 /*
40 The libc implementation from GCC 11+ depends on _getpid and _kill in some places.
41 There is no concept of processes/PIDs in the baremetal PeriphDrivers, therefore
42 we implement stub functions that return an error code to resolve linker warnings.
43 */
_getpid(void)44 __weak int _getpid(void)
45 {
46     return E_NOT_SUPPORTED;
47 }
48 
_kill(void)49 __weak int _kill(void)
50 {
51     return E_NOT_SUPPORTED;
52 }
53 
SystemCoreClockUpdate(void)54 __weak void SystemCoreClockUpdate(void)
55 {
56     uint32_t base_freq, div, clk_src, ovr;
57 
58     // Get the clock source and frequency
59     clk_src = (MXC_GCR->clk_ctrl & MXC_F_GCR_CLK_CTRL_CLKSEL);
60 
61     if (clk_src == MXC_S_GCR_CLK_CTRL_CLKSEL_HFXIN) {
62         base_freq = HFX_FREQ;
63     } else {
64         if (clk_src == MXC_S_GCR_CLK_CTRL_CLKSEL_NANORING) {
65             base_freq = NANORING_FREQ;
66         } else {
67             ovr = (MXC_PWRSEQ->lp_ctrl & MXC_F_PWRSEQ_LP_CTRL_OVR);
68             if (ovr == MXC_S_PWRSEQ_LP_CTRL_OVR_0_9V) {
69                 base_freq = HIRC96_FREQ / 4;
70             } else {
71                 if (ovr == MXC_S_PWRSEQ_LP_CTRL_OVR_1_0V) {
72                     base_freq = HIRC96_FREQ / 2;
73                 } else {
74                     base_freq = HIRC96_FREQ;
75                 }
76             }
77         }
78     }
79 
80     // Get the clock divider
81     div = (MXC_GCR->clk_ctrl & MXC_F_GCR_CLK_CTRL_PSC) >> MXC_F_GCR_CLK_CTRL_PSC_POS;
82 
83     SystemCoreClock = base_freq >> div;
84 }
85 
86 /* This function is called before C runtime initialization and can be
87  * implemented by the application for early initializations. If a value other
88  * than '0' is returned, the C runtime initialization will be skipped.
89  *
90  * You may over-ride this function in your program by defining a custom
91  *  PreInit(), but care should be taken to reproduce the initilization steps
92  *  or a non-functional system may result.
93  */
PreInit(void)94 __weak int PreInit(void)
95 {
96     /* Do nothing */
97     return 0;
98 }
99 
100 /* This function can be implemented by the application to initialize the board */
Board_Init(void)101 __weak int Board_Init(void)
102 {
103     /* Do nothing */
104     return 0;
105 }
106 
107 /* This function is called just before control is transferred to main().
108  *
109  * You may over-ride this function in your program by defining a custom
110  *  SystemInit(), but care should be taken to reproduce the initialization
111  *  steps or a non-functional system may result.
112  */
SystemInit(void)113 __weak void SystemInit(void)
114 {
115     /* Configure the interrupt controller to use the application vector table in */
116     /* the application space */
117     /* IAR & Keil must set vector table after all memory initialization. */
118     SCB->VTOR = (uint32_t)__isr_vector;
119 
120     MXC_WDT0->ctrl &=
121         ~MXC_F_WDT_CTRL_WDT_EN; /* Turn off watchdog. Application can re-enable as needed. */
122 
123     /* Enable FPU on Cortex-M4, which occupies coprocessor slots 10 & 11 */
124     /* Grant full access, per "Table B3-24 CPACR bit assignments". */
125     /* DDI0403D "ARMv7-M Architecture Reference Manual" */
126     SCB->CPACR |= SCB_CPACR_CP10_Msk | SCB_CPACR_CP11_Msk;
127     __DSB();
128     __ISB();
129 
130     /* Switch system clock to HIRC */
131     MXC_SYS_Clock_Select(MXC_SYS_CLOCK_HIRC);
132 
133     /* Disable clocks to peripherals by default to reduce power */
134     MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_DMA);
135     MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_SPI0);
136     MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_SPI1);
137     MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_UART0);
138     MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_UART1);
139     MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_I2C0);
140     MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_TMR0);
141     MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_TMR1);
142     MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_TMR2);
143     MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_I2C1);
144 
145     Board_Init();
146 }
147 
148 #if defined(__CC_ARM)
149 /* Global variable initialization does not occur until post scatterload in Keil tools.*/
150 
151 /* External function called after our post scatterload function implementation. */
152 extern void $Super$$__main_after_scatterload(void);
153 
154 /**
155  * @brief   Initialization function for SystemCoreClock and Board_Init.
156  * @details $Sub$$__main_after_scatterload is called during system startup in the Keil
157  *          toolset. Global variable and static variable space must be set up by the compiler
158  *          prior to using these memory spaces. Setting up the SystemCoreClock and Board_Init
159  *          require global memory for variable storage and are called from this function in
160  *          the Keil tool chain.
161  */
$Sub$$__main_after_scatterload(void)162 void $Sub$$__main_after_scatterload(void)
163 {
164     SystemInit();
165     $Super$$__main_after_scatterload();
166 }
167 #endif /* __CC_ARM */
168