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 "max78000.h"
25 #include "gcr_regs.h"
26 #include "mxc_sys.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 
__enable_irq(void)46 void __enable_irq(void)
47 {
48     // Set the MIE bit if we're outside the interrupt context
49     __asm volatile("csrw mstatus, 0x8");
50 }
51 
SystemCoreClockUpdate(void)52 __weak void SystemCoreClockUpdate(void)
53 {
54     // uint32_t base_freq, div, clk_src;
55 
56     // ARM core does this stuff for us
57 }
58 
59 /* This function is called before C runtime initialization and can be
60  * implemented by the application for early initializations. If a value other
61  * than '0' is returned, the C runtime initialization will be skipped.
62  *
63  * You may over-ride this function in your program by defining a custom
64  *  PreInit(), but care should be taken to reproduce the initilization steps
65  *  or a non-functional system may result.
66  */
PreInit(void)67 __weak int PreInit(void)
68 {
69     /* Do nothing */
70     return 0;
71 }
72 
73 /* This function can be implemented by the application to initialize the board */
Board_Init(void)74 __weak int Board_Init(void)
75 {
76     /* Do nothing */
77     return 0;
78 }
79 
80 /* This function is called just before control is transferred to main().
81  *
82  * You may over-ride this function in your program by defining a custom
83  *  SystemInit(), but care should be taken to reproduce the initialization
84  *  steps or a non-functional system may result.
85  */
SystemInit(void)86 __weak void SystemInit(void)
87 {
88     // ARM core does this stuff for us
89     // but riscv needs variables initialized
90     SystemCoreClockUpdate();
91 
92     Board_Init();
93 }
94