1 /**
2  * @file       system_max32660.c
3  * @brief      System-level initialization implementation file
4  */
5 
6 /*******************************************************************************
7  * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
23  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * Except as contained in this notice, the name of Maxim Integrated
28  * Products, Inc. shall not be used except as stated in the Maxim Integrated
29  * Products, Inc. Branding Policy.
30  *
31  * The mere transfer of this software does not imply any licenses
32  * of trade secrets, proprietary technology, copyrights, patents,
33  * trademarks, maskwork rights, or any other form of intellectual
34  * property whatsoever. Maxim Integrated Products, Inc. retains all
35  * ownership rights.
36  *
37  * $Date: 2018-12-18 15:37:22 -0600 (Tue, 18 Dec 2018) $
38  * $Revision: 40072 $
39  *
40  ******************************************************************************/
41 
42 #include <string.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include "max32660.h"
46 #include "gcr_regs.h"
47 #include "pwrseq_regs.h"
48 #include "tmr_regs.h"
49 #include "wdt_regs.h"
50 #include "mxc_sys.h"
51 
52 extern void (* const __isr_vector[])(void);
53 uint32_t SystemCoreClock = HIRC96_FREQ;
54 
SystemCoreClockUpdate(void)55 __weak void SystemCoreClockUpdate(void)
56 {
57     uint32_t base_freq, div, clk_src,ovr;
58 
59     // Get the clock source and frequency
60     clk_src = (MXC_GCR->clkcn & MXC_F_GCR_CLKCN_CLKSEL);
61 
62     if (clk_src == MXC_S_GCR_CLKCN_CLKSEL_HFXIN) {
63         base_freq = HFX_FREQ;
64     } else {
65 	if (clk_src == MXC_S_GCR_CLKCN_CLKSEL_NANORING) {
66 	    base_freq = NANORING_FREQ;
67 	} else {
68 	    ovr = (MXC_PWRSEQ->lp_ctrl & MXC_F_PWRSEQ_LP_CTRL_OVR);
69 	    if (ovr == MXC_S_PWRSEQ_LP_CTRL_OVR_0_9V) {
70 		base_freq = HIRC96_FREQ/4;
71 	    } else {
72 		if (ovr == MXC_S_PWRSEQ_LP_CTRL_OVR_1_0V) {
73 		    base_freq = HIRC96_FREQ/2;
74 		} else {
75 		    base_freq = HIRC96_FREQ;
76 		}
77 	    }
78 	}
79     }
80 
81     // Get the clock divider
82     div = (MXC_GCR->clkcn & MXC_F_GCR_CLKCN_PSC) >> MXC_F_GCR_CLKCN_PSC_POS;
83 
84     SystemCoreClock = base_freq >> div;
85 }
86 
87 /* This function is called before C runtime initialization and can be
88  * implemented by the application for early initializations. If a value other
89  * than '0' is returned, the C runtime initialization will be skipped.
90  *
91  * You may over-ride this function in your program by defining a custom
92  *  PreInit(), but care should be taken to reproduce the initilization steps
93  *  or a non-functional system may result.
94  */
PreInit(void)95 __weak int PreInit(void)
96 {
97     /* Do nothing */
98 #if defined ( __CC_ARM )
99     SystemInit();
100 #endif
101     return 0;
102 }
103 
104 /* This function can be implemented by the application to initialize the board */
Board_Init(void)105 __weak int Board_Init(void)
106 {
107     /* Do nothing */
108     return 0;
109 }
110 
111 /* This function is called just before control is transferred to main().
112  *
113  * You may over-ride this function in your program by defining a custom
114  *  SystemInit(), but care should be taken to reproduce the initialization
115  *  steps or a non-functional system may result.
116  */
SystemInit(void)117 __weak void SystemInit(void)
118 {
119     /* Configure the interrupt controller to use the application vector table in */
120     /* the application space */
121     /* IAR & Keil must set vector table after all memory initialization. */
122     SCB->VTOR = (unsigned long)__isr_vector;
123 
124     MXC_WDT0->ctrl &= ~MXC_F_WDT_CTRL_WDT_EN;  /* Turn off watchdog. Application can re-enable as needed. */
125 
126     /* Enable FPU on Cortex-M4, which occupies coprocessor slots 10 & 11 */
127     /* Grant full access, per "Table B3-24 CPACR bit assignments". */
128     /* DDI0403D "ARMv7-M Architecture Reference Manual" */
129     SCB->CPACR |= SCB_CPACR_CP10_Msk | SCB_CPACR_CP11_Msk;
130     __DSB();
131     __ISB();
132 
133     /* Switch system clock to HIRC */
134     SYS_Clock_Select(SYS_CLOCK_HIRC, MXC_TMR0);
135 
136     /* Disable clocks to peripherals by default to reduce power */
137     SYS_ClockDisable(SYS_PERIPH_CLOCK_DMA);
138     SYS_ClockDisable(SYS_PERIPH_CLOCK_SPI17Y);
139     SYS_ClockDisable(SYS_PERIPH_CLOCK_SPIMSS);
140     SYS_ClockDisable(SYS_PERIPH_CLOCK_UART0);
141     SYS_ClockDisable(SYS_PERIPH_CLOCK_UART1);
142     SYS_ClockDisable(SYS_PERIPH_CLOCK_I2C0);
143     SYS_ClockDisable(SYS_PERIPH_CLOCK_T0);
144     SYS_ClockDisable(SYS_PERIPH_CLOCK_T1);
145     SYS_ClockDisable(SYS_PERIPH_CLOCK_T2);
146     SYS_ClockDisable(SYS_PERIPH_CLOCK_I2C1);
147 
148     Board_Init();
149 }
150 
151 //#if defined ( __CC_ARM )
152 ///* Global variable initialization does not occur until post scatterload in Keil tools.*/
153 
154 ///* External function called after our post scatterload function implementation. */
155 //extern void $Super$$__main_after_scatterload(void);
156 
157 ///**
158 // * @brief   Initialization function for SystemCoreClock and Board_Init.
159 // * @details $Sub$$__main_after_scatterload is called during system startup in the Keil
160 // *          toolset. Global variable and static variable space must be set up by the compiler
161 // *          prior to using these memory spaces. Setting up the SystemCoreClock and Board_Init
162 // *          require global memory for variable storage and are called from this function in
163 // *          the Keil tool chain.
164 // */
165 //void $Sub$$__main_after_scatterload(void)
166 //{
167 //    SystemInit();
168 //    $Super$$__main_after_scatterload();
169 //}
170 //#endif /* __CC_ARM */
171