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 "mxc_errors.h"
25 #include "max32572.h"
26 #include "gcr_regs.h"
27
28 volatile uint32_t mailbox __attribute__((section(".mailbox")));
29 uint32_t SystemCoreClock __attribute__((section(".shared")));
30
31 /*
32 The libc implementation from GCC 11+ depends on _getpid and _kill in some places.
33 There is no concept of processes/PIDs in the baremetal PeriphDrivers, therefore
34 we implement stub functions that return an error code to resolve linker warnings.
35 */
_getpid(void)36 __weak int _getpid(void)
37 {
38 return E_NOT_SUPPORTED;
39 }
40
_kill(void)41 __weak int _kill(void)
42 {
43 return E_NOT_SUPPORTED;
44 }
45
SystemCoreClockUpdate(void)46 __weak void SystemCoreClockUpdate(void)
47 {
48 uint32_t base_freq, div, clk_src;
49
50 // Get the clock source and frequency
51 clk_src = (MXC_GCR->clkctrl & MXC_F_GCR_CLKCTRL_SYSCLK_SEL);
52 switch (clk_src) {
53 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_INRO:
54 base_freq = INRO_FREQ;
55 break;
56 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_IPO:
57 base_freq = IPO_FREQ;
58 break;
59 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_IBRO:
60 base_freq = IBRO_FREQ;
61 break;
62 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_ISO:
63 base_freq = ISO_FREQ;
64 break;
65 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_ERTCO:
66 base_freq = ERTCO_FREQ;
67 break;
68 case MXC_S_GCR_CLKCTRL_SYSCLK_SEL_ERFO:
69 base_freq = ERFO_FREQ;
70 break;
71 default:
72 // Codes 001 and 111 are reserved.
73 // This code should never execute, however, initialize to safe value.
74 base_freq = ISO_FREQ;
75 break;
76 }
77
78 div = (MXC_GCR->clkctrl & MXC_F_GCR_CLKCTRL_SYSCLK_DIV) >> MXC_F_GCR_CLKCTRL_SYSCLK_DIV_POS;
79
80 SystemCoreClock = base_freq >> div;
81 }
82
83 /* This function is called before C runtime initialization and can be
84 * implemented by the application for early initializations. If a value other
85 * than '0' is returned, the C runtime initialization will be skipped.
86 *
87 * You may over-ride this function in your program by defining a custom
88 * PreInit(), but care should be taken to reproduce the initilization steps
89 * or a non-functional system may result.
90 */
PreInit(void)91 __weak int PreInit(void)
92 {
93 /* Do nothing */
94 return 0;
95 }
96
97 /* This function can be implemented by the application to initialize the board */
Board_Init(void)98 __weak int Board_Init(void)
99 {
100 /* Do nothing */
101 return 0;
102 }
103
__enable_irq(void)104 void __enable_irq(void)
105 {
106 // Set the MIE bit if we're outside the interrupt context
107 __asm volatile("csrw mstatus, 0x8");
108 }
109
110 /* This function is called just before control is transferred to main().
111 *
112 * You may over-ride this function in your program by defining a custom
113 * SystemInit(), but care should be taken to reproduce the initialization
114 * steps or a non-functional system may result.
115 */
SystemInit(void)116 __weak void SystemInit(void)
117 {
118 SystemCoreClockUpdate();
119
120 __enable_irq();
121
122 Board_Init();
123 }
124