Change script for apply upstream code
[platform/upstream/connectedhomeip.git] / examples / platform / efr32 / efr32mg12 / BRD4170A / init_mcu.c
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    All rights reserved.
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18 /*******************************************************************************
19  * @file
20  * @brief init_mcu.c
21  *******************************************************************************
22  * # License
23  * <b>Copyright 2018 Silicon Laboratories Inc. www.silabs.com</b>
24  *******************************************************************************
25  *
26  * The licensor of this software is Silicon Laboratories Inc. Your use of this
27  * software is governed by the terms of Silicon Labs Master Software License
28  * Agreement (MSLA) available at
29  * www.silabs.com/about-us/legal/master-software-license-agreement. This
30  * software is distributed to you in Source Code format and is governed by the
31  * sections of the MSLA applicable to Source Code.
32  *
33  ******************************************************************************/
34
35 #if defined(HAL_CONFIG)
36 #include "bsphalconfig.h"
37 #include "hal-config.h"
38 #else
39 #include "bspconfig.h"
40 #endif
41
42 #include "board_features.h"
43
44 #include "em_chip.h"
45 #include "em_cmu.h"
46 #include "em_emu.h"
47 #include "em_rtcc.h"
48
49 #include "FreeRTOSConfig.h"
50 #include "bsp.h"
51 #include "init_mcu.h"
52
53 // Bit [19] in MODULEINFO is the HFXOCALVAL:
54 // 1=No factory cal, use default XO tunning value in FW
55 // 0=Factory Cal, use XO tunning value in DI
56 #define DEVINFO_MODULEINFO_HFXOCALVAL_MASK 0x00080000UL
57 // Calibration value for HFXO CTUNE is at DEVINFO Offset 0x08
58 #define DEVINFO_MODULEINFO_CRYSTALOSCCALVAL (*((uint16_t *) (uint32_t)(DEVINFO_BASE + 0x8UL)))
59 // [15:9] : (LFXOTUNING) Calibration for LFXO TUNING
60 // [8:0]  : (HFXOCTUNE) Calibration for HFXO CTUNE
61 #define DEVINFO_HFXOCTUNE_MASK 0x01FFUL
62
63 #define set_HFXO_CTUNE(val)                                                                                                        \
64     do                                                                                                                             \
65     {                                                                                                                              \
66         hfxoInit.ctuneSteadyState = (val);                                                                                         \
67     } while (0)
68
69 static void initMcu_clocks(void);
70 static void initHFXO(void);
71
72 void initMcu(void)
73 {
74     // ISR safe FreeRTOS API functions must *only* be called
75     // from interrupts that have been assigned a priority at or below
76     // configMAX_SYSCALL_INTERRUPT_PRIORITY.
77     // Here we init all IRQ prio to configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY
78     // to make sure no IRQ use default priority of zero as that is the highest possible priority
79     for (IRQn_Type i = 0; i < EXT_IRQ_COUNT; i++)
80     {
81         NVIC_SetPriority(i, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
82     }
83
84     // Device errata
85     CHIP_Init();
86
87     // Set up DC-DC converter
88     EMU_DCDCInit_TypeDef dcdcInit = BSP_DCDC_INIT;
89 #if HAL_DCDC_BYPASS
90     dcdcInit.dcdcMode = emuDcdcMode_Bypass;
91 #endif
92     EMU_DCDCInit(&dcdcInit);
93
94     // Set up clocks
95     initMcu_clocks();
96
97     RTCC_Init_TypeDef rtccInit = RTCC_INIT_DEFAULT;
98     rtccInit.enable            = true;
99     rtccInit.debugRun          = false;
100     rtccInit.precntWrapOnCCV0  = false;
101     rtccInit.cntWrapOnCCV1     = false;
102     rtccInit.prescMode         = rtccCntTickPresc;
103     rtccInit.presc             = rtccCntPresc_1;
104     rtccInit.enaOSCFailDetect  = false;
105     rtccInit.cntMode           = rtccCntModeNormal;
106     RTCC_Init(&rtccInit);
107
108 #if defined(EMU_VSCALE_PRESENT)
109     // Set up EM0, EM1 energy mode configuration
110     EMU_EM01Init_TypeDef em01Init = EMU_EM01INIT_DEFAULT;
111     EMU_EM01Init(&em01Init);
112 #endif // EMU_VSCALE_PRESENT
113     // Set up EM2, EM3 energy mode configuration
114     EMU_EM23Init_TypeDef em23init = EMU_EM23INIT_DEFAULT;
115 #if defined(EMU_VSCALE_PRESENT)
116     em23init.vScaleEM23Voltage = emuVScaleEM23_LowPower;
117 #endif // EMU_VSCALE_PRESENT
118     EMU_EM23Init(&em23init);
119
120 #if defined(RMU_PRESENT)
121     // Set reset mode for sysreset back to DEFAULT (extended), this might have
122     // been changed by the bootloader to FULL reset.
123     RMU->CTRL = (RMU->CTRL & ~_RMU_CTRL_SYSRMODE_MASK) | RMU_CTRL_SYSRMODE_DEFAULT;
124 #endif
125 }
126
127 static void initMcu_clocks(void)
128 {
129     // Initialize HFXO
130     initHFXO();
131
132     // Set system HFXO frequency
133     SystemHFXOClockSet(BSP_CLK_HFXO_FREQ);
134
135     // Enable HFXO oscillator, and wait for it to be stable
136     CMU_OscillatorEnable(cmuOsc_HFXO, true, true);
137
138     // Enable HFXO Autostart only if EM2 voltage scaling is disabled.
139     // In 1.0 V mode the chip does not support frequencies > 21 MHz,
140     // this is why HFXO autostart is not supported in this case.
141 #if !defined(_EMU_CTRL_EM23VSCALE_MASK)
142     // Automatically start and select HFXO
143     CMU_HFXOAutostartEnable(0, true, true);
144 #else
145     CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
146 #endif //_EMU_CTRL_EM23VSCALE_MASK
147
148     // HFRCO not needed when using HFXO
149     CMU_OscillatorEnable(cmuOsc_HFRCO, false, false);
150
151     // Enabling HFBUSCLKLE clock for LE peripherals
152     CMU_ClockEnable(cmuClock_HFLE, true);
153
154     // Initialize LFXO
155     CMU_LFXOInit_TypeDef lfxoInit = BSP_CLK_LFXO_INIT;
156     lfxoInit.ctune                = BSP_CLK_LFXO_CTUNE;
157     CMU_LFXOInit(&lfxoInit);
158     // Set system LFXO frequency
159     SystemLFXOClockSet(BSP_CLK_LFXO_FREQ);
160
161     // Set LFXO if selected as LFCLK
162     CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFXO);
163     CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_LFXO);
164     CMU_ClockSelectSet(cmuClock_LFE, cmuSelect_LFXO);
165 }
166
167 static void initHFXO(void)
168 {
169     // Initialize HFXO
170     // Use BSP_CLK_HFXO_INIT as last result (4th)
171     CMU_HFXOInit_TypeDef hfxoInit = BSP_CLK_HFXO_INIT;
172     // if Factory Cal exists in DEVINFO then use it above all (1st)
173     if (0 == (DEVINFO->MODULEINFO & DEVINFO_MODULEINFO_HFXOCALVAL_MASK))
174     {
175 #if defined(_SILICON_LABS_32B_SERIES_1)
176         set_HFXO_CTUNE(DEVINFO_MODULEINFO_CRYSTALOSCCALVAL);
177 #elif defined(_SILICON_LABS_32B_SERIES_2)
178         set_HFXO_CTUNE(DEVINFO->MODXOCAL & _DEVINFO_MODXOCAL_HFXOCTUNEXIANA_MASK);
179 #endif
180     }
181     // if User page has CTUNE from studio use that in 2nd place
182 #if (MFG_CTUNE_EN == 1)
183     else if (MFG_CTUNE_VAL != 0xFFFF)
184     {
185         set_HFXO_CTUNE(MFG_CTUNE_VAL);
186     }
187 #endif
188     // 3rd option, get data from header defined for product/board
189 #if defined(BSP_CLK_HFXO_CTUNE) && BSP_CLK_HFXO_CTUNE >= 0
190     else
191     {
192         set_HFXO_CTUNE(BSP_CLK_HFXO_CTUNE);
193     }
194 #endif
195     CMU_HFXOInit(&hfxoInit);
196 }