-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_mcu_efr32xg1.c
154 lines (129 loc) · 4.97 KB
/
init_mcu_efr32xg1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/***************************************************************************//**
* @file
* @brief init_mcu_efr32xg1.c
*******************************************************************************
* # License
* <b>Copyright 2018 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/
#if defined(HAL_CONFIG)
#include "bsphalconfig.h"
#include "hal-config.h"
#else
#include "bspconfig.h"
#endif
#include "board_features.h"
#include "em_chip.h"
#include "em_cmu.h"
#include "em_emu.h"
#include "em_rtcc.h"
#include "bsp.h"
#include "init_mcu.h"
// Bit [19] in MODULEINFO is the HFXOCALVAL:
// 1=No factory cal, use default XO tunning value in FW
// 0=Factory Cal, use XO tunning value in DI
#define DEVINFO_MODULEINFO_HFXOCALVAL_MASK 0x00080000UL
// Calibration value for HFXO CTUNE is at DEVINFO Offset 0x08
#define DEVINFO_MODULEINFO_CRYSTALOSCCALVAL (*((uint16_t *) (uint32_t)(DEVINFO_BASE+0x8UL)))
// [15:9] : (LFXOTUNING) Calibration for LFXO TUNING
// [8:0] : (HFXOCTUNE) Calibration for HFXO CTUNE
#define DEVINFO_HFXOCTUNE_MASK 0x01FFUL
static void initMcu_clocks(void);
void initMcu(void)
{
// Device errata
CHIP_Init();
// Set up DC-DC converter
EMU_DCDCInit_TypeDef dcdcInit = BSP_DCDC_INIT;
#if HAL_DCDC_BYPASS
dcdcInit.dcdcMode = emuDcdcMode_Bypass;
#endif
EMU_DCDCInit(&dcdcInit);
// Set up clocks
initMcu_clocks();
RTCC_Init_TypeDef rtccInit = RTCC_INIT_DEFAULT;
rtccInit.enable = true;
rtccInit.debugRun = false;
rtccInit.precntWrapOnCCV0 = false;
rtccInit.cntWrapOnCCV1 = false;
rtccInit.prescMode = rtccCntTickPresc;
rtccInit.presc = rtccCntPresc_1;
rtccInit.enaOSCFailDetect = false;
rtccInit.cntMode = rtccCntModeNormal;
RTCC_Init(&rtccInit);
#if defined(_EMU_CMD_EM01VSCALE0_MASK)
// Set up EM0, EM1 energy mode configuration
EMU_EM01Init_TypeDef em01Init = EMU_EM01INIT_DEFAULT;
EMU_EM01Init(&em01Init);
#endif // _EMU_CMD_EM01VSCALE0_MASK
#if defined(_EMU_CTRL_EM23VSCALE_MASK)
// Set up EM2, EM3 energy mode configuration
EMU_EM23Init_TypeDef em23init = EMU_EM23INIT_DEFAULT;
em23init.vScaleEM23Voltage = emuVScaleEM23_LowPower;
EMU_EM23Init(&em23init);
#endif //_EMU_CTRL_EM23VSCALE_MASK
#if defined(RMU_PRESENT)
// Set reset mode for sysreset back to DEFAULT (extended), this might have
// been changed by the bootloader to FULL reset.
RMU->CTRL = (RMU->CTRL & ~_RMU_CTRL_SYSRMODE_MASK) | RMU_CTRL_SYSRMODE_DEFAULT;
#endif
}
static void initMcu_clocks(void)
{
// Initialize HFXO
// Use BSP_CLK_HFXO_INIT as last result (4th)
CMU_HFXOInit_TypeDef hfxoInit = BSP_CLK_HFXO_INIT;
// if Factory Cal exists in DEVINFO then use it above all (1st)
if (0 == (DEVINFO->MODULEINFO & DEVINFO_MODULEINFO_HFXOCALVAL_MASK)) {
hfxoInit.ctuneSteadyState = DEVINFO_MODULEINFO_CRYSTALOSCCALVAL
& DEVINFO_HFXOCTUNE_MASK;
}
// if User page has CTUNE from studio use that in 2nd place
#if defined(MFG_CTUNE_ADDR) && (MFG_CTUNE_EN == 1)
else if (MFG_CTUNE_VAL != 0xFFFF) {
hfxoInit.ctuneSteadyState = MFG_CTUNE_VAL;
}
#endif
// 3rd option, get data from header defined for product/board
#if defined(BSP_CLK_HFXO_CTUNE) && BSP_CLK_HFXO_CTUNE >= 0
else {
hfxoInit.ctuneSteadyState = BSP_CLK_HFXO_CTUNE;
}
#endif
CMU_HFXOInit(&hfxoInit);
// Set system HFXO frequency
SystemHFXOClockSet(BSP_CLK_HFXO_FREQ);
// Enable HFXO oscillator, and wait for it to be stable
CMU_OscillatorEnable(cmuOsc_HFXO, true, true);
// Enable HFXO Autostart only if EM2 voltage scaling is disabled.
// In 1.0 V mode the chip does not support frequencies > 21 MHz,
// this is why HFXO autostart is not supported in this case.
#if!defined(_EMU_CTRL_EM23VSCALE_MASK)
// Automatically start and select HFXO
CMU_HFXOAutostartEnable(0, true, true);
#else
CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
#endif//_EMU_CTRL_EM23VSCALE_MASK
// HFRCO not needed when using HFXO
CMU_OscillatorEnable(cmuOsc_HFRCO, false, false);
// Enabling HFBUSCLKLE clock for LE peripherals
CMU_ClockEnable(cmuClock_HFLE, true);
// Initialize LFXO
CMU_LFXOInit_TypeDef lfxoInit = BSP_CLK_LFXO_INIT;
lfxoInit.ctune = BSP_CLK_LFXO_CTUNE;
CMU_LFXOInit(&lfxoInit);
// Set system LFXO frequency
SystemLFXOClockSet(BSP_CLK_LFXO_FREQ);
// Set LFXO if selected as LFCLK
CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFXO);
CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_LFXO);
CMU_ClockSelectSet(cmuClock_LFE, cmuSelect_LFXO);
}