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.ctuneXoAna = (val); \
67 hfxoInit.ctuneXiAna = (val); \
70 static void initMcu_clocks(void);
71 static void initHFXO(void);
75 // ISR safe FreeRTOS API functions must *only* be called
76 // from interrupts that have been assigned a priority at or below
77 // configMAX_SYSCALL_INTERRUPT_PRIORITY.
78 // Here we init all IRQ prio to configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY
79 // to make sure no IRQ use default priority of zero as that is the highest possible priority
80 for (IRQn_Type i = 0; i < EXT_IRQ_COUNT; i++)
82 NVIC_SetPriority(i, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
90 RTCC_Init_TypeDef rtccInit = RTCC_INIT_DEFAULT;
91 rtccInit.enable = true;
92 rtccInit.debugRun = false;
93 rtccInit.precntWrapOnCCV0 = false;
94 rtccInit.cntWrapOnCCV1 = false;
95 rtccInit.prescMode = rtccCntTickPresc;
96 rtccInit.presc = rtccCntPresc_1;
99 #if defined(EMU_VSCALE_PRESENT)
100 // Set up EM0, EM1 energy mode configuration
101 EMU_EM01Init_TypeDef em01Init = EMU_EM01INIT_DEFAULT;
102 EMU_EM01Init(&em01Init);
103 #endif // EMU_VSCALE_PRESENT
104 // Set up EM2, EM3 energy mode configuration
105 EMU_EM23Init_TypeDef em23init = EMU_EM23INIT_DEFAULT;
106 #if defined(EMU_VSCALE_PRESENT)
107 em23init.vScaleEM23Voltage = emuVScaleEM23_LowPower;
108 #endif // EMU_VSCALE_PRESENT
109 EMU_EM23Init(&em23init);
111 #if defined(RMU_PRESENT)
112 // Set reset mode for sysreset back to DEFAULT (extended), this might have
113 // been changed by the bootloader to FULL reset.
114 RMU->CTRL = (RMU->CTRL & ~_RMU_CTRL_SYSRMODE_MASK) | RMU_CTRL_SYSRMODE_DEFAULT;
118 static void initMcu_clocks(void)
123 // Set system HFXO frequency
124 SystemHFXOClockSet(BSP_CLK_HFXO_FREQ);
126 CMU_HFRCODPLLBandSet(cmuHFRCODPLLFreq_80M0Hz);
127 CMU_ClockSelectSet(cmuClock_SYSCLK, cmuSelect_HFRCODPLL);
130 CMU_LFXOInit_TypeDef lfxoInit = BSP_CLK_LFXO_INIT;
131 lfxoInit.capTune = BSP_CLK_LFXO_CTUNE;
132 CMU_LFXOInit(&lfxoInit);
133 // Set system LFXO frequency
134 SystemLFXOClockSet(BSP_CLK_LFXO_FREQ);
136 // Select LFXO as low frequency clock source
137 CMU_ClockSelectSet(cmuClock_RTCC, cmuSelect_LFXO);
138 CMU_ClockSelectSet(cmuClock_EM23GRPACLK, cmuSelect_LFXO);
139 CMU_ClockSelectSet(cmuClock_EM4GRPACLK, cmuSelect_LFXO);
140 CMU_ClockSelectSet(cmuClock_WDOG0, cmuSelect_LFXO);
141 CMU_ClockSelectSet(cmuClock_WDOG1, cmuSelect_LFXO);
144 static void initHFXO(void)
147 // Use BSP_CLK_HFXO_INIT as last result (4th)
148 CMU_HFXOInit_TypeDef hfxoInit = BSP_CLK_HFXO_INIT;
149 // if Factory Cal exists in DEVINFO then use it above all (1st)
150 if (0 == (DEVINFO->MODULEINFO & DEVINFO_MODULEINFO_HFXOCALVAL_MASK))
152 #if defined(_SILICON_LABS_32B_SERIES_1)
153 set_HFXO_CTUNE(DEVINFO_MODULEINFO_CRYSTALOSCCALVAL);
154 #elif defined(_SILICON_LABS_32B_SERIES_2)
155 set_HFXO_CTUNE(DEVINFO->MODXOCAL & _DEVINFO_MODXOCAL_HFXOCTUNEXIANA_MASK);
158 // if User page has CTUNE from studio use that in 2nd place
159 #if (MFG_CTUNE_EN == 1)
160 else if (MFG_CTUNE_VAL != 0xFFFF)
162 set_HFXO_CTUNE(MFG_CTUNE_VAL);
165 // 3rd option, get data from header defined for product/board
166 #if defined(BSP_CLK_HFXO_CTUNE) && BSP_CLK_HFXO_CTUNE >= 0
169 set_HFXO_CTUNE(BSP_CLK_HFXO_CTUNE);
172 CMU_HFXOInit(&hfxoInit);