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 "max78002.h"
25 #include "gcr_regs.h"
26 #include "mxc_sys.h"
27
28 extern void (*const __isr_vector[])(void);
29
30 uint32_t SystemCoreClock __attribute__((section(".shared")));
31 volatile uint32_t mailbox __attribute__((section(".mailbox")));
32
33 /*
34 The libc implementation from GCC 11+ depends on _getpid and _kill in some places.
35 There is no concept of processes/PIDs in the baremetal PeriphDrivers, therefore
36 we implement stub functions that return an error code to resolve linker warnings.
37 */
_getpid(void)38 __weak int _getpid(void)
39 {
40 return E_NOT_SUPPORTED;
41 }
42
_kill(void)43 __weak int _kill(void)
44 {
45 return E_NOT_SUPPORTED;
46 }
47
SystemCoreClockUpdate(void)48 __weak void SystemCoreClockUpdate(void)
49 {
50 uint32_t base_freq, div, clk_src;
51
52 // Get the clock source and frequency
53 clk_src = (MXC_GCR->clkctrl & MXC_F_GCR_CLKCTRL_SYSCLK_SEL);
54 switch (clk_src) {
55 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_EXTCLK:
56 base_freq = EXTCLK_FREQ;
57 break;
58 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_INRO:
59 base_freq = INRO_FREQ;
60 break;
61 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_IPO:
62 base_freq = IPO_FREQ;
63 break;
64 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_IPLL:
65 base_freq = IPLL_FREQ;
66 break;
67 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_EBO:
68 base_freq = EBO_FREQ;
69 break;
70 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_IBRO:
71 base_freq = IBRO_FREQ;
72 break;
73 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_ISO:
74 base_freq = ISO_FREQ;
75 break;
76 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_ERTCO:
77 base_freq = ERTCO_FREQ;
78 break;
79 default:
80 // Codes 001 and 111 are reserved.
81 // This code should never execute, however, initialize to safe value.
82 base_freq = HIRC_FREQ;
83 break;
84 }
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(__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 /* Enable FPU on Cortex-M4, which occupies coprocessor slots 10 & 11 */
128 /* Grant full access, per "Table B3-24 CPACR bit assignments". */
129 /* DDI0403D "ARMv7-M Architecture Reference Manual" */
130 SCB->CPACR |= SCB_CPACR_CP10_Msk | SCB_CPACR_CP11_Msk;
131 __DSB();
132 __ISB();
133
134 SystemCoreClockUpdate();
135
136 Board_Init();
137 }
138