1 /***************************************************************************//**
2 * @file
3 * @brief CMSIS Cortex-M3/M4 System Layer for EFR32 devices.
4 *******************************************************************************
5 * # License
6 * <b>Copyright 2020 Silicon Laboratories Inc. www.silabs.com</b>
7 *******************************************************************************
8 *
9 * SPDX-License-Identifier: Zlib
10 *
11 * The licensor of this software is Silicon Laboratories Inc.
12 *
13 * This software is provided 'as-is', without any express or implied
14 * warranty. In no event will the authors be held liable for any damages
15 * arising from the use of this software.
16 *
17 * Permission is granted to anyone to use this software for any purpose,
18 * including commercial applications, and to alter it and redistribute it
19 * freely, subject to the following restrictions:
20 *
21 * 1. The origin of this software must not be misrepresented; you must not
22 * claim that you wrote the original software. If you use this software
23 * in a product, an acknowledgment in the product documentation would be
24 * appreciated but is not required.
25 * 2. Altered source versions must be plainly marked as such, and must not be
26 * misrepresented as being the original software.
27 * 3. This notice may not be removed or altered from any source distribution.
28 *
29 ******************************************************************************/
30
31 #include <stdint.h>
32 #include "em_device.h"
33
34 /*******************************************************************************
35 ****************************** DEFINES ************************************
36 ******************************************************************************/
37
38 /** LFRCO frequency, tuned to below frequency during manufacturing. */
39 #define EFR32_LFRCO_FREQ (32768UL)
40 /** ULFRCO frequency */
41 #define EFR32_ULFRCO_FREQ (1000UL)
42
43 /*******************************************************************************
44 ************************** LOCAL VARIABLES ********************************
45 ******************************************************************************/
46
47 /* System oscillator frequencies. These frequencies are normally constant */
48 /* for a target, but they are made configurable in order to allow run-time */
49 /* handling of different boards. The crystal oscillator clocks can be set */
50 /* compile time to a non-default value by defining respective EFR32_nFXO_FREQ */
51 /* values according to board design. By defining the EFR32_nFXO_FREQ to 0, */
52 /* one indicates that the oscillator is not present, in order to save some */
53 /* SW footprint. */
54
55 #ifndef EFR32_HFRCO_MAX_FREQ
56 /** Maximum HFRCO frequency */
57 #define EFR32_HFRCO_MAX_FREQ (38000000UL)
58 #endif
59
60 #ifndef EFR32_HFXO_FREQ
61 /** HFXO frequency */
62 #define EFR32_HFXO_FREQ (38400000UL)
63 #endif
64
65 #ifndef EFR32_HFRCO_STARTUP_FREQ
66 /** HFRCO startup frequency */
67 #define EFR32_HFRCO_STARTUP_FREQ (19000000UL)
68 #endif
69
70 /* Do not define variable if HF crystal oscillator not present */
71 #if (EFR32_HFXO_FREQ > 0U)
72 /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
73 /** System HFXO clock. */
74 static uint32_t SystemHFXOClock = EFR32_HFXO_FREQ;
75 /** @endcond (DO_NOT_INCLUDE_WITH_DOXYGEN) */
76 #endif
77
78 #ifndef EFR32_LFXO_FREQ
79 /** LFXO frequency */
80 #define EFR32_LFXO_FREQ (EFR32_LFRCO_FREQ)
81 #endif
82 /* Do not define variable if LF crystal oscillator not present */
83 #if (EFR32_LFXO_FREQ > 0U)
84 /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
85 /** System LFXO clock. */
86 static uint32_t SystemLFXOClock = EFR32_LFXO_FREQ;
87 /** @endcond (DO_NOT_INCLUDE_WITH_DOXYGEN) */
88 #endif
89
90 /*******************************************************************************
91 ************************** GLOBAL VARIABLES *******************************
92 ******************************************************************************/
93
94 /**
95 * @brief
96 * System System Clock Frequency (Core Clock).
97 *
98 * @details
99 * Required CMSIS global variable that must be kept up-to-date.
100 */
101 uint32_t SystemCoreClock = EFR32_HFRCO_STARTUP_FREQ;
102
103 /**
104 * @brief
105 * System HFRCO frequency
106 *
107 * @note
108 * This is an EFR32 proprietary variable, not part of the CMSIS definition.
109 *
110 * @details
111 * Frequency of the system HFRCO oscillator
112 */
113 uint32_t SystemHfrcoFreq = EFR32_HFRCO_STARTUP_FREQ;
114
115 /*******************************************************************************
116 ************************** GLOBAL FUNCTIONS *******************************
117 ******************************************************************************/
118
119 /***************************************************************************//**
120 * @brief
121 * Get the current core clock frequency.
122 *
123 * @details
124 * Calculate and get the current core clock frequency based on the current
125 * configuration. Assuming that the SystemCoreClock global variable is
126 * maintained, the core clock frequency is stored in that variable as well.
127 * This function will however calculate the core clock based on actual HW
128 * configuration. It will also update the SystemCoreClock global variable.
129 *
130 * @note
131 * This is an EFR32 proprietary function, not part of the CMSIS definition.
132 *
133 * @return
134 * The current core clock frequency in Hz.
135 ******************************************************************************/
SystemCoreClockGet(void)136 uint32_t SystemCoreClockGet(void)
137 {
138 uint32_t ret;
139 uint32_t presc;
140
141 ret = SystemHFClockGet();
142 presc = (CMU->HFCOREPRESC & _CMU_HFCOREPRESC_PRESC_MASK)
143 >> _CMU_HFCOREPRESC_PRESC_SHIFT;
144 ret /= presc + 1U;
145
146 /* Keep CMSIS system clock variable up-to-date */
147 SystemCoreClock = ret;
148
149 return ret;
150 }
151
152 /***************************************************************************//**
153 * @brief
154 * Get the maximum core clock frequency.
155 *
156 * @note
157 * This is an EFR32 proprietary function, not part of the CMSIS definition.
158 *
159 * @return
160 * The maximum core clock frequency in Hz.
161 ******************************************************************************/
SystemMaxCoreClockGet(void)162 uint32_t SystemMaxCoreClockGet(void)
163 {
164 #if (EFR32_HFRCO_MAX_FREQ > EFR32_HFXO_FREQ)
165 return EFR32_HFRCO_MAX_FREQ;
166 #else
167 return EFR32_HFXO_FREQ;
168 #endif
169 }
170
171 /***************************************************************************//**
172 * @brief
173 * Get the current HFCLK frequency.
174 *
175 * @note
176 * This is an EFR32 proprietary function, not part of the CMSIS definition.
177 *
178 * @return
179 * The current HFCLK frequency in Hz.
180 ******************************************************************************/
SystemHFClockGet(void)181 uint32_t SystemHFClockGet(void)
182 {
183 uint32_t ret;
184
185 switch (CMU->HFCLKSTATUS & _CMU_HFCLKSTATUS_SELECTED_MASK) {
186 case CMU_HFCLKSTATUS_SELECTED_LFXO:
187 #if (EFR32_LFXO_FREQ > 0U)
188 ret = SystemLFXOClock;
189 #else
190 /* We should not get here, since core should not be clocked. May */
191 /* be caused by a misconfiguration though. */
192 ret = 0U;
193 #endif
194 break;
195
196 case CMU_HFCLKSTATUS_SELECTED_LFRCO:
197 ret = EFR32_LFRCO_FREQ;
198 break;
199
200 case CMU_HFCLKSTATUS_SELECTED_HFXO:
201 #if (EFR32_HFXO_FREQ > 0U)
202 ret = SystemHFXOClock;
203 #else
204 /* We should not get here, since core should not be clocked. May */
205 /* be caused by a misconfiguration though. */
206 ret = 0U;
207 #endif
208 break;
209
210 default: /* CMU_HFCLKSTATUS_SELECTED_HFRCO */
211 ret = SystemHfrcoFreq;
212 break;
213 }
214
215 return ret / (1U + ((CMU->HFPRESC & _CMU_HFPRESC_PRESC_MASK)
216 >> _CMU_HFPRESC_PRESC_SHIFT));
217 }
218
219 /***************************************************************************//**
220 * @brief
221 * Get high frequency crystal oscillator clock frequency for target system.
222 *
223 * @note
224 * This is an EFR32 proprietary function, not part of the CMSIS definition.
225 *
226 * @return
227 * HFXO frequency in Hz.
228 ******************************************************************************/
SystemHFXOClockGet(void)229 uint32_t SystemHFXOClockGet(void)
230 {
231 /* External crystal oscillator present? */
232 #if (EFR32_HFXO_FREQ > 0U)
233 return SystemHFXOClock;
234 #else
235 return 0U;
236 #endif
237 }
238
239 /***************************************************************************//**
240 * @brief
241 * Set high frequency crystal oscillator clock frequency for target system.
242 *
243 * @note
244 * This function is mainly provided for being able to handle target systems
245 * with different HF crystal oscillator frequencies run-time. If used, it
246 * should probably only be used once during system startup.
247 *
248 * @note
249 * This is an EFR32 proprietary function, not part of the CMSIS definition.
250 *
251 * @param[in] freq
252 * HFXO frequency in Hz used for target.
253 ******************************************************************************/
SystemHFXOClockSet(uint32_t freq)254 void SystemHFXOClockSet(uint32_t freq)
255 {
256 /* External crystal oscillator present? */
257 #if (EFR32_HFXO_FREQ > 0U)
258 SystemHFXOClock = freq;
259
260 /* Update core clock frequency if HFXO is used to clock core */
261 if ((CMU->HFCLKSTATUS & _CMU_HFCLKSTATUS_SELECTED_MASK)
262 == CMU_HFCLKSTATUS_SELECTED_HFXO) {
263 /* The function will update the global variable */
264 (void)SystemCoreClockGet();
265 }
266 #else
267 (void)freq; /* Unused parameter */
268 #endif
269 }
270
271 /***************************************************************************//**
272 * @brief
273 * Initialize the system.
274 *
275 * @details
276 * Do required generic HW system init.
277 *
278 * @note
279 * This function is invoked during system init, before the main() routine
280 * and any data has been initialized. For this reason, it cannot do any
281 * initialization of variables etc.
282 ******************************************************************************/
SystemInit(void)283 void SystemInit(void)
284 {
285 #if defined(__VTOR_PRESENT) && (__VTOR_PRESENT == 1U)
286 SCB->VTOR = (uint32_t)&__Vectors;
287 #endif
288
289 #if (__FPU_PRESENT == 1U) && (__FPU_USED == 1U)
290 /* Set floating point coprosessor access mode. */
291 SCB->CPACR |= ((3UL << 10 * 2) /* set CP10 Full Access */
292 | (3UL << 11 * 2)); /* set CP11 Full Access */
293 #endif
294
295 #if defined(UNALIGNED_SUPPORT_DISABLE)
296 SCB->CCR |= SCB_CCR_UNALIGN_TRP_Msk;
297 #endif
298
299 /****************************
300 * Fix for errata DCDC_E206
301 * Enable bypass switch as errata workaround. The bypass current limit will be
302 * disabled again in CHIP_Init() to avoid added current consumption. */
303
304 EMU->DCDCCLIMCTRL |= 1U << _EMU_DCDCCLIMCTRL_BYPLIMEN_SHIFT;
305 EMU->DCDCCTRL = (EMU->DCDCCTRL & ~_EMU_DCDCCTRL_DCDCMODE_MASK)
306 | EMU_DCDCCTRL_DCDCMODE_BYPASS;
307 *(volatile uint32_t *)(0x400E3074) &= ~(0x1UL << 0);
308 *(volatile uint32_t *)(0x400E3060) &= ~(0x1UL << 28);
309 }
310
311 /***************************************************************************//**
312 * @brief
313 * Get low frequency RC oscillator clock frequency for target system.
314 *
315 * @note
316 * This is an EFR32 proprietary function, not part of the CMSIS definition.
317 *
318 * @return
319 * LFRCO frequency in Hz.
320 ******************************************************************************/
SystemLFRCOClockGet(void)321 uint32_t SystemLFRCOClockGet(void)
322 {
323 /* Currently we assume that this frequency is properly tuned during */
324 /* manufacturing and is not changed after reset. If future requirements */
325 /* for re-tuning by user, we can add support for that. */
326 return EFR32_LFRCO_FREQ;
327 }
328
329 /***************************************************************************//**
330 * @brief
331 * Get ultra low frequency RC oscillator clock frequency for target system.
332 *
333 * @note
334 * This is an EFR32 proprietary function, not part of the CMSIS definition.
335 *
336 * @return
337 * ULFRCO frequency in Hz.
338 ******************************************************************************/
SystemULFRCOClockGet(void)339 uint32_t SystemULFRCOClockGet(void)
340 {
341 /* The ULFRCO frequency is not tuned, and can be very inaccurate */
342 return EFR32_ULFRCO_FREQ;
343 }
344
345 /***************************************************************************//**
346 * @brief
347 * Get low frequency crystal oscillator clock frequency for target system.
348 *
349 * @note
350 * This is an EFR32 proprietary function, not part of the CMSIS definition.
351 *
352 * @return
353 * LFXO frequency in Hz.
354 ******************************************************************************/
SystemLFXOClockGet(void)355 uint32_t SystemLFXOClockGet(void)
356 {
357 /* External crystal oscillator present? */
358 #if (EFR32_LFXO_FREQ > 0U)
359 return SystemLFXOClock;
360 #else
361 return 0U;
362 #endif
363 }
364
365 /***************************************************************************//**
366 * @brief
367 * Set low frequency crystal oscillator clock frequency for target system.
368 *
369 * @note
370 * This function is mainly provided for being able to handle target systems
371 * with different HF crystal oscillator frequencies run-time. If used, it
372 * should probably only be used once during system startup.
373 *
374 * @note
375 * This is an EFR32 proprietary function, not part of the CMSIS definition.
376 *
377 * @param[in] freq
378 * LFXO frequency in Hz used for target.
379 ******************************************************************************/
SystemLFXOClockSet(uint32_t freq)380 void SystemLFXOClockSet(uint32_t freq)
381 {
382 /* External crystal oscillator present? */
383 #if (EFR32_LFXO_FREQ > 0U)
384 SystemLFXOClock = freq;
385
386 /* Update core clock frequency if LFXO is used to clock core */
387 if ((CMU->HFCLKSTATUS & _CMU_HFCLKSTATUS_SELECTED_MASK)
388 == CMU_HFCLKSTATUS_SELECTED_LFXO) {
389 /* The function will update the global variable */
390 (void)SystemCoreClockGet();
391 }
392 #else
393 (void)freq; /* Unused parameter */
394 #endif
395 }
396