1 /***************************************************************************//**
2 * @file
3 * @brief CMSIS Cortex-M3/M4 System Layer for EFM32 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 EFM32_LFRCO_FREQ (32768UL)
40 /** ULFRCO frequency */
41 #define EFM32_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 EFM_nFXO_FREQ */
51 /* values according to board design. By defining the EFM_nFXO_FREQ to 0, */
52 /* one indicates that the oscillator is not present, in order to save some */
53 /* SW footprint. */
54
55 #ifndef EFM32_HFRCO_MAX_FREQ
56 /** Maximum HFRCO frequency */
57 #define EFM32_HFRCO_MAX_FREQ (38000000UL)
58 #endif
59
60 #ifndef EFM32_HFXO_FREQ
61 /** HFXO frequency */
62 #define EFM32_HFXO_FREQ (40000000UL)
63 #endif
64
65 #ifndef EFM32_HFRCO_STARTUP_FREQ
66 /** HFRCO startup frequency */
67 #define EFM32_HFRCO_STARTUP_FREQ (19000000UL)
68 #endif
69
70 /* Do not define variable if HF crystal oscillator not present */
71 #if (EFM32_HFXO_FREQ > 0U)
72 /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
73 /** System HFXO clock. */
74 static uint32_t SystemHFXOClock = EFM32_HFXO_FREQ;
75 /** @endcond (DO_NOT_INCLUDE_WITH_DOXYGEN) */
76 #endif
77
78 #ifndef EFM32_LFXO_FREQ
79 /** LFXO frequency */
80 #define EFM32_LFXO_FREQ (EFM32_LFRCO_FREQ)
81 #endif
82 /* Do not define variable if LF crystal oscillator not present */
83 #if (EFM32_LFXO_FREQ > 0U)
84 /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
85 /** System LFXO clock. */
86 static uint32_t SystemLFXOClock = EFM32_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 = EFM32_HFRCO_STARTUP_FREQ;
102
103 /**
104 * @brief
105 * System HFRCO frequency
106 *
107 * @note
108 * This is an EFM32 proprietary variable, not part of the CMSIS definition.
109 *
110 * @details
111 * Frequency of the system HFRCO oscillator
112 */
113 uint32_t SystemHfrcoFreq = EFM32_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 EFM32 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 EFM32 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 (EFM32_HFRCO_MAX_FREQ > EFM32_HFXO_FREQ)
165 return EFM32_HFRCO_MAX_FREQ;
166 #else
167 return EFM32_HFXO_FREQ;
168 #endif
169 }
170
171 /***************************************************************************//**
172 * @brief
173 * Get the current HFCLK frequency.
174 *
175 * @note
176 * This is an EFM32 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 (EFM32_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 = EFM32_LFRCO_FREQ;
198 break;
199
200 case CMU_HFCLKSTATUS_SELECTED_HFXO:
201 #if (EFM32_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 #if defined(CMU_HFCLKSTATUS_SELECTED_HFRCODIV2)
211 case CMU_HFCLKSTATUS_SELECTED_HFRCODIV2:
212 ret = SystemHfrcoFreq / 2;
213 break;
214 #endif
215
216 default: /* CMU_HFCLKSTATUS_SELECTED_HFRCO */
217 ret = SystemHfrcoFreq;
218 break;
219 }
220
221 return ret / (1U + ((CMU->HFPRESC & _CMU_HFPRESC_PRESC_MASK)
222 >> _CMU_HFPRESC_PRESC_SHIFT));
223 }
224
225 /***************************************************************************//**
226 * @brief
227 * Get high frequency crystal oscillator clock frequency for target system.
228 *
229 * @note
230 * This is an EFM32 proprietary function, not part of the CMSIS definition.
231 *
232 * @return
233 * HFXO frequency in Hz.
234 ******************************************************************************/
SystemHFXOClockGet(void)235 uint32_t SystemHFXOClockGet(void)
236 {
237 /* External crystal oscillator present? */
238 #if (EFM32_HFXO_FREQ > 0U)
239 return SystemHFXOClock;
240 #else
241 return 0U;
242 #endif
243 }
244
245 /***************************************************************************//**
246 * @brief
247 * Set high frequency crystal oscillator clock frequency for target system.
248 *
249 * @note
250 * This function is mainly provided for being able to handle target systems
251 * with different HF crystal oscillator frequencies run-time. If used, it
252 * should probably only be used once during system startup.
253 *
254 * @note
255 * This is an EFM32 proprietary function, not part of the CMSIS definition.
256 *
257 * @param[in] freq
258 * HFXO frequency in Hz used for target.
259 ******************************************************************************/
SystemHFXOClockSet(uint32_t freq)260 void SystemHFXOClockSet(uint32_t freq)
261 {
262 /* External crystal oscillator present? */
263 #if (EFM32_HFXO_FREQ > 0U)
264 SystemHFXOClock = freq;
265
266 /* Update core clock frequency if HFXO is used to clock core */
267 if ((CMU->HFCLKSTATUS & _CMU_HFCLKSTATUS_SELECTED_MASK)
268 == CMU_HFCLKSTATUS_SELECTED_HFXO) {
269 /* The function will update the global variable */
270 (void)SystemCoreClockGet();
271 }
272 #else
273 (void)freq; /* Unused parameter */
274 #endif
275 }
276
277 /***************************************************************************//**
278 * @brief
279 * Initialize the system.
280 *
281 * @details
282 * Do required generic HW system init.
283 *
284 * @note
285 * This function is invoked during system init, before the main() routine
286 * and any data has been initialized. For this reason, it cannot do any
287 * initialization of variables etc.
288 ******************************************************************************/
SystemInit(void)289 void SystemInit(void)
290 {
291 #if defined(__VTOR_PRESENT) && (__VTOR_PRESENT == 1U)
292 SCB->VTOR = (uint32_t)&__Vectors;
293 #endif
294
295 #if (__FPU_PRESENT == 1U) && (__FPU_USED == 1U)
296 /* Set floating point coprosessor access mode. */
297 SCB->CPACR |= ((3UL << 10 * 2) /* set CP10 Full Access */
298 | (3UL << 11 * 2)); /* set CP11 Full Access */
299 #endif
300
301 #if defined(UNALIGNED_SUPPORT_DISABLE)
302 SCB->CCR |= SCB_CCR_UNALIGN_TRP_Msk;
303 #endif
304
305 #if defined(_SILICON_LABS_32B_SERIES_1_CONFIG_1)
306 /****************************
307 * Fix for errata DCDC_E206
308 * Enable bypass switch as errata workaround. The bypass current limit will be
309 * disabled again in CHIP_Init() to avoid added current consumption. */
310
311 EMU->DCDCCLIMCTRL |= 1U << _EMU_DCDCCLIMCTRL_BYPLIMEN_SHIFT;
312 EMU->DCDCCTRL = (EMU->DCDCCTRL & ~_EMU_DCDCCTRL_DCDCMODE_MASK)
313 | EMU_DCDCCTRL_DCDCMODE_BYPASS;
314 *(volatile uint32_t *)(0x400E3074) &= ~(0x1UL << 0);
315 *(volatile uint32_t *)(0x400E3060) &= ~(0x1UL << 28);
316 #endif
317 }
318
319 /***************************************************************************//**
320 * @brief
321 * Get low frequency RC oscillator clock frequency for target system.
322 *
323 * @note
324 * This is an EFM32 proprietary function, not part of the CMSIS definition.
325 *
326 * @return
327 * LFRCO frequency in Hz.
328 ******************************************************************************/
SystemLFRCOClockGet(void)329 uint32_t SystemLFRCOClockGet(void)
330 {
331 /* Currently we assume that this frequency is properly tuned during */
332 /* manufacturing and is not changed after reset. If future requirements */
333 /* for re-tuning by user, we can add support for that. */
334 return EFM32_LFRCO_FREQ;
335 }
336
337 /***************************************************************************//**
338 * @brief
339 * Get ultra low frequency RC oscillator clock frequency for target system.
340 *
341 * @note
342 * This is an EFM32 proprietary function, not part of the CMSIS definition.
343 *
344 * @return
345 * ULFRCO frequency in Hz.
346 ******************************************************************************/
SystemULFRCOClockGet(void)347 uint32_t SystemULFRCOClockGet(void)
348 {
349 /* The ULFRCO frequency is not tuned, and can be very inaccurate */
350 return EFM32_ULFRCO_FREQ;
351 }
352
353 /***************************************************************************//**
354 * @brief
355 * Get low frequency crystal oscillator clock frequency for target system.
356 *
357 * @note
358 * This is an EFM32 proprietary function, not part of the CMSIS definition.
359 *
360 * @return
361 * LFXO frequency in Hz.
362 ******************************************************************************/
SystemLFXOClockGet(void)363 uint32_t SystemLFXOClockGet(void)
364 {
365 /* External crystal oscillator present? */
366 #if (EFM32_LFXO_FREQ > 0U)
367 return SystemLFXOClock;
368 #else
369 return 0U;
370 #endif
371 }
372
373 /***************************************************************************//**
374 * @brief
375 * Set low frequency crystal oscillator clock frequency for target system.
376 *
377 * @note
378 * This function is mainly provided for being able to handle target systems
379 * with different HF crystal oscillator frequencies run-time. If used, it
380 * should probably only be used once during system startup.
381 *
382 * @note
383 * This is an EFM32 proprietary function, not part of the CMSIS definition.
384 *
385 * @param[in] freq
386 * LFXO frequency in Hz used for target.
387 ******************************************************************************/
SystemLFXOClockSet(uint32_t freq)388 void SystemLFXOClockSet(uint32_t freq)
389 {
390 /* External crystal oscillator present? */
391 #if (EFM32_LFXO_FREQ > 0U)
392 SystemLFXOClock = freq;
393
394 /* Update core clock frequency if LFXO is used to clock core */
395 if ((CMU->HFCLKSTATUS & _CMU_HFCLKSTATUS_SELECTED_MASK)
396 == CMU_HFCLKSTATUS_SELECTED_LFXO) {
397 /* The function will update the global variable */
398 (void)SystemCoreClockGet();
399 }
400 #else
401 (void)freq; /* Unused parameter */
402 #endif
403 }
404