1 /***************************************************************************//**
2 * @file
3 * @brief CMSIS Cortex-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 EFM32_nFXO_FREQ */
51 /* values according to board design. By defining the EFM32_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 (72000000UL)
58 #endif
59
60 #ifndef EFM32_HFXO_FREQ
61 /** HFXO frequency */
62 #define EFM32_HFXO_FREQ (50000000UL)
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 case CMU_HFCLKSTATUS_SELECTED_USHFRCO:
211 switch ((CMU->USHFRCOCTRL & _CMU_USHFRCOCTRL_FREQRANGE_MASK)
212 >> _CMU_USHFRCOCTRL_FREQRANGE_SHIFT) {
213 case 7:
214 ret = 16000000U;
215 break;
216 case 11:
217 ret = 32000000U;
218 break;
219 case 13:
220 ret = 48000000U;
221 break;
222 case 14:
223 ret = 50000000U;
224 break;
225 default:
226 ret = 0U;
227 break;
228 }
229 break;
230
231 case CMU_HFCLKSTATUS_SELECTED_HFRCODIV2:
232 ret = SystemHfrcoFreq / 2;
233 break;
234
235 default: /* CMU_HFCLKSTATUS_SELECTED_HFRCO */
236 ret = SystemHfrcoFreq;
237 break;
238 }
239
240 return ret / (1U + ((CMU->HFPRESC & _CMU_HFPRESC_PRESC_MASK)
241 >> _CMU_HFPRESC_PRESC_SHIFT));
242 }
243
244 /***************************************************************************//**
245 * @brief
246 * Get high frequency crystal oscillator clock frequency for target system.
247 *
248 * @note
249 * This is an EFM32 proprietary function, not part of the CMSIS definition.
250 *
251 * @return
252 * HFXO frequency in Hz.
253 ******************************************************************************/
SystemHFXOClockGet(void)254 uint32_t SystemHFXOClockGet(void)
255 {
256 /* External crystal oscillator present? */
257 #if (EFM32_HFXO_FREQ > 0U)
258 return SystemHFXOClock;
259 #else
260 return 0U;
261 #endif
262 }
263
264 /***************************************************************************//**
265 * @brief
266 * Set high frequency crystal oscillator clock frequency for target system.
267 *
268 * @note
269 * This function is mainly provided for being able to handle target systems
270 * with different HF crystal oscillator frequencies run-time. If used, it
271 * should probably only be used once during system startup.
272 *
273 * @note
274 * This is an EFM32 proprietary function, not part of the CMSIS definition.
275 *
276 * @param[in] freq
277 * HFXO frequency in Hz used for target.
278 ******************************************************************************/
SystemHFXOClockSet(uint32_t freq)279 void SystemHFXOClockSet(uint32_t freq)
280 {
281 /* External crystal oscillator present? */
282 #if (EFM32_HFXO_FREQ > 0U)
283 SystemHFXOClock = freq;
284
285 /* Update core clock frequency if HFXO is used to clock core */
286 if ((CMU->HFCLKSTATUS & _CMU_HFCLKSTATUS_SELECTED_MASK)
287 == CMU_HFCLKSTATUS_SELECTED_HFXO) {
288 /* The function will update the global variable */
289 (void)SystemCoreClockGet();
290 }
291 #else
292 (void)freq; /* Unused parameter */
293 #endif
294 }
295
296 /***************************************************************************//**
297 * @brief
298 * Initialize the system.
299 *
300 * @details
301 * Do required generic HW system init.
302 *
303 * @note
304 * This function is invoked during system init, before the main() routine
305 * and any data has been initialized. For this reason, it cannot do any
306 * initialization of variables etc.
307 ******************************************************************************/
SystemInit(void)308 void SystemInit(void)
309 {
310 #if defined(__VTOR_PRESENT) && (__VTOR_PRESENT == 1U)
311 SCB->VTOR = (uint32_t)&__Vectors;
312 #endif
313
314 #if (__FPU_PRESENT == 1U) && (__FPU_USED == 1U)
315 /* Set floating point coprosessor access mode. */
316 SCB->CPACR |= ((3UL << 10 * 2) /* set CP10 Full Access */
317 | (3UL << 11 * 2)); /* set CP11 Full Access */
318 #endif
319
320 #if defined(UNALIGNED_SUPPORT_DISABLE)
321 SCB->CCR |= SCB_CCR_UNALIGN_TRP_Msk;
322 #endif
323 }
324
325 /***************************************************************************//**
326 * @brief
327 * Get low frequency RC oscillator clock frequency for target system.
328 *
329 * @note
330 * This is an EFM32 proprietary function, not part of the CMSIS definition.
331 *
332 * @return
333 * LFRCO frequency in Hz.
334 ******************************************************************************/
SystemLFRCOClockGet(void)335 uint32_t SystemLFRCOClockGet(void)
336 {
337 /* Currently we assume that this frequency is properly tuned during */
338 /* manufacturing and is not changed after reset. If future requirements */
339 /* for re-tuning by user, we can add support for that. */
340 return EFM32_LFRCO_FREQ;
341 }
342
343 /***************************************************************************//**
344 * @brief
345 * Get ultra low frequency RC oscillator clock frequency for target system.
346 *
347 * @note
348 * This is an EFM32 proprietary function, not part of the CMSIS definition.
349 *
350 * @return
351 * ULFRCO frequency in Hz.
352 ******************************************************************************/
SystemULFRCOClockGet(void)353 uint32_t SystemULFRCOClockGet(void)
354 {
355 /* The ULFRCO frequency is not tuned, and can be very inaccurate */
356 return EFM32_ULFRCO_FREQ;
357 }
358
359 /***************************************************************************//**
360 * @brief
361 * Get low frequency crystal oscillator clock frequency for target system.
362 *
363 * @note
364 * This is an EFM32 proprietary function, not part of the CMSIS definition.
365 *
366 * @return
367 * LFXO frequency in Hz.
368 ******************************************************************************/
SystemLFXOClockGet(void)369 uint32_t SystemLFXOClockGet(void)
370 {
371 /* External crystal oscillator present? */
372 #if (EFM32_LFXO_FREQ > 0U)
373 return SystemLFXOClock;
374 #else
375 return 0U;
376 #endif
377 }
378
379 /***************************************************************************//**
380 * @brief
381 * Set low frequency crystal oscillator clock frequency for target system.
382 *
383 * @note
384 * This function is mainly provided for being able to handle target systems
385 * with different HF crystal oscillator frequencies run-time. If used, it
386 * should probably only be used once during system startup.
387 *
388 * @note
389 * This is an EFM32 proprietary function, not part of the CMSIS definition.
390 *
391 * @param[in] freq
392 * LFXO frequency in Hz used for target.
393 ******************************************************************************/
SystemLFXOClockSet(uint32_t freq)394 void SystemLFXOClockSet(uint32_t freq)
395 {
396 /* External crystal oscillator present? */
397 #if (EFM32_LFXO_FREQ > 0U)
398 SystemLFXOClock = freq;
399
400 /* Update core clock frequency if LFXO is used to clock core */
401 if ((CMU->HFCLKSTATUS & _CMU_HFCLKSTATUS_SELECTED_MASK)
402 == CMU_HFCLKSTATUS_SELECTED_LFXO) {
403 /* The function will update the global variable */
404 (void)SystemCoreClockGet();
405 }
406 #else
407 (void)freq; /* Unused parameter */
408 #endif
409 }
410