3 * Copyright (c) 2020 Project CHIP Authors
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 /*******************************************************************************
21 *******************************************************************************
23 * <b>Copyright 2018 Silicon Laboratories Inc. www.silabs.com</b>
24 *******************************************************************************
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.
33 ******************************************************************************/
35 #if defined(HAL_CONFIG)
36 #include "bsphalconfig.h"
37 #include "hal-config.h"
39 #include "bspconfig.h"
42 #include "board_features.h"
49 #include "FreeRTOSConfig.h"
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
63 #define set_HFXO_CTUNE(val) \
66 hfxoInit.ctuneSteadyState = (val); \
69 static void initMcu_clocks(void);
70 static void initHFXO(void);
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++)
81 NVIC_SetPriority(i, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
87 // Set up DC-DC converter
88 EMU_DCDCInit_TypeDef dcdcInit = BSP_DCDC_INIT;
90 dcdcInit.dcdcMode = emuDcdcMode_Bypass;
92 EMU_DCDCInit(&dcdcInit);
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);
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);
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;
127 static void initMcu_clocks(void)
132 // Set system HFXO frequency
133 SystemHFXOClockSet(BSP_CLK_HFXO_FREQ);
135 // Enable HFXO oscillator, and wait for it to be stable
136 CMU_OscillatorEnable(cmuOsc_HFXO, true, true);
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);
145 CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
146 #endif //_EMU_CTRL_EM23VSCALE_MASK
148 // HFRCO not needed when using HFXO
149 CMU_OscillatorEnable(cmuOsc_HFRCO, false, false);
151 // Enabling HFBUSCLKLE clock for LE peripherals
152 CMU_ClockEnable(cmuClock_HFLE, true);
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);
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);
167 static void initHFXO(void)
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))
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);
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)
185 set_HFXO_CTUNE(MFG_CTUNE_VAL);
188 // 3rd option, get data from header defined for product/board
189 #if defined(BSP_CLK_HFXO_CTUNE) && BSP_CLK_HFXO_CTUNE >= 0
192 set_HFXO_CTUNE(BSP_CLK_HFXO_CTUNE);
195 CMU_HFXOInit(&hfxoInit);