1 /***************************************************************************//**
2  * @file
3  * @brief CMSIS Cortex-M4 System Layer for EFM32 devices.
4  *******************************************************************************
5  * # License
6  * <b>Copyright 2022 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    Exception / Interrupt Vector table
117  *---------------------------------------------------------------------------*/
118 extern const tVectorEntry __VECTOR_TABLE[16 + EXT_IRQ_COUNT];
119 
120 /*******************************************************************************
121  **************************   GLOBAL FUNCTIONS   *******************************
122  ******************************************************************************/
123 
124 /***************************************************************************//**
125  * @brief
126  *   Get the current core clock frequency.
127  *
128  * @details
129  *   Calculate and get the current core clock frequency based on the current
130  *   configuration. Assuming that the SystemCoreClock global variable is
131  *   maintained, the core clock frequency is stored in that variable as well.
132  *   This function will however calculate the core clock based on actual HW
133  *   configuration. It will also update the SystemCoreClock global variable.
134  *
135  * @note
136  *   This is an EFM32 proprietary function, not part of the CMSIS definition.
137  *
138  * @return
139  *   The current core clock frequency in Hz.
140  ******************************************************************************/
SystemCoreClockGet(void)141 uint32_t SystemCoreClockGet(void)
142 {
143   uint32_t ret;
144   uint32_t presc;
145 
146   ret   = SystemHFClockGet();
147   presc = (CMU->HFCOREPRESC & _CMU_HFCOREPRESC_PRESC_MASK)
148           >> _CMU_HFCOREPRESC_PRESC_SHIFT;
149   ret  /= presc + 1U;
150 
151   /* Keep CMSIS system clock variable up-to-date */
152   SystemCoreClock = ret;
153 
154   return ret;
155 }
156 
157 /***************************************************************************//**
158  * @brief
159  *   Get the maximum core clock frequency.
160  *
161  * @note
162  *   This is an EFM32 proprietary function, not part of the CMSIS definition.
163  *
164  * @return
165  *   The maximum core clock frequency in Hz.
166  ******************************************************************************/
SystemMaxCoreClockGet(void)167 uint32_t SystemMaxCoreClockGet(void)
168 {
169 #if (EFM32_HFRCO_MAX_FREQ > EFM32_HFXO_FREQ)
170   return EFM32_HFRCO_MAX_FREQ;
171 #else
172   return EFM32_HFXO_FREQ;
173 #endif
174 }
175 
176 /***************************************************************************//**
177  * @brief
178  *   Get the current HFCLK frequency.
179  *
180  * @note
181  *   This is an EFM32 proprietary function, not part of the CMSIS definition.
182  *
183  * @return
184  *   The current HFCLK frequency in Hz.
185  ******************************************************************************/
SystemHFClockGet(void)186 uint32_t SystemHFClockGet(void)
187 {
188   uint32_t ret;
189 
190   switch (CMU->HFCLKSTATUS & _CMU_HFCLKSTATUS_SELECTED_MASK) {
191     case CMU_HFCLKSTATUS_SELECTED_LFXO:
192 #if (EFM32_LFXO_FREQ > 0U)
193       ret = SystemLFXOClock;
194 #else
195       /* We should not get here, since core should not be clocked. May */
196       /* be caused by a misconfiguration though. */
197       ret = 0U;
198 #endif
199       break;
200 
201     case CMU_HFCLKSTATUS_SELECTED_LFRCO:
202       ret = EFM32_LFRCO_FREQ;
203       break;
204 
205     case CMU_HFCLKSTATUS_SELECTED_HFXO:
206 #if (EFM32_HFXO_FREQ > 0U)
207       ret = SystemHFXOClock;
208 #else
209       /* We should not get here, since core should not be clocked. May */
210       /* be caused by a misconfiguration though. */
211       ret = 0U;
212 #endif
213       break;
214 
215     case CMU_HFCLKSTATUS_SELECTED_USHFRCO:
216       switch ((CMU->USHFRCOCTRL & _CMU_USHFRCOCTRL_FREQRANGE_MASK)
217               >> _CMU_USHFRCOCTRL_FREQRANGE_SHIFT) {
218         case 7:
219           ret = 16000000U;
220           break;
221         case 11:
222           ret = 32000000U;
223           break;
224         case 13:
225           ret = 48000000U;
226           break;
227         case 14:
228           ret = 50000000U;
229           break;
230         default:
231           ret = 0U;
232           break;
233       }
234       break;
235 
236     case CMU_HFCLKSTATUS_SELECTED_HFRCODIV2:
237       ret = SystemHfrcoFreq / 2;
238       break;
239 
240     default: /* CMU_HFCLKSTATUS_SELECTED_HFRCO */
241       ret = SystemHfrcoFreq;
242       break;
243   }
244 
245   return ret / (1U + ((CMU->HFPRESC & _CMU_HFPRESC_PRESC_MASK)
246                       >> _CMU_HFPRESC_PRESC_SHIFT));
247 }
248 
249 /***************************************************************************//**
250  * @brief
251  *   Get high frequency crystal oscillator clock frequency for target system.
252  *
253  * @note
254  *   This is an EFM32 proprietary function, not part of the CMSIS definition.
255  *
256  * @return
257  *   HFXO frequency in Hz.
258  ******************************************************************************/
SystemHFXOClockGet(void)259 uint32_t SystemHFXOClockGet(void)
260 {
261   /* External crystal oscillator present? */
262 #if (EFM32_HFXO_FREQ > 0U)
263   return SystemHFXOClock;
264 #else
265   return 0U;
266 #endif
267 }
268 
269 /***************************************************************************//**
270  * @brief
271  *   Set high frequency crystal oscillator clock frequency for target system.
272  *
273  * @note
274  *   This function is mainly provided for being able to handle target systems
275  *   with different HF crystal oscillator frequencies run-time. If used, it
276  *   should probably only be used once during system startup.
277  *
278  * @note
279  *   This is an EFM32 proprietary function, not part of the CMSIS definition.
280  *
281  * @param[in] freq
282  *   HFXO frequency in Hz used for target.
283  ******************************************************************************/
SystemHFXOClockSet(uint32_t freq)284 void SystemHFXOClockSet(uint32_t freq)
285 {
286   /* External crystal oscillator present? */
287 #if (EFM32_HFXO_FREQ > 0U)
288   SystemHFXOClock = freq;
289 
290   /* Update core clock frequency if HFXO is used to clock core */
291   if ((CMU->HFCLKSTATUS & _CMU_HFCLKSTATUS_SELECTED_MASK)
292       == CMU_HFCLKSTATUS_SELECTED_HFXO) {
293     /* The function will update the global variable */
294     (void)SystemCoreClockGet();
295   }
296 #else
297   (void)freq; /* Unused parameter */
298 #endif
299 }
300 
301 /***************************************************************************//**
302  * @brief
303  *   Initialize the system.
304  *
305  * @details
306  *   Do required generic HW system init.
307  *
308  * @note
309  *   This function is invoked during system init, before the main() routine
310  *   and any data has been initialized. For this reason, it cannot do any
311  *   initialization of variables etc.
312  ******************************************************************************/
SystemInit(void)313 void SystemInit(void)
314 {
315 #if defined (__VTOR_PRESENT) && (__VTOR_PRESENT == 1U)
316   SCB->VTOR = (uint32_t)(&__VECTOR_TABLE[0]);
317 #endif
318 
319 #if (__FPU_PRESENT == 1U) && (__FPU_USED == 1U)
320   /* Set floating point coprosessor access mode. */
321   SCB->CPACR |= ((3UL << 10 * 2)                    /* set CP10 Full Access */
322                  | (3UL << 11 * 2));                /* set CP11 Full Access */
323 #endif
324 
325 #if defined(UNALIGNED_SUPPORT_DISABLE)
326   SCB->CCR |= SCB_CCR_UNALIGN_TRP_Msk;
327 #endif
328 }
329 
330 /***************************************************************************//**
331  * @brief
332  *   Get low frequency RC oscillator clock frequency for target system.
333  *
334  * @note
335  *   This is an EFM32 proprietary function, not part of the CMSIS definition.
336  *
337  * @return
338  *   LFRCO frequency in Hz.
339  ******************************************************************************/
SystemLFRCOClockGet(void)340 uint32_t SystemLFRCOClockGet(void)
341 {
342   /* Currently we assume that this frequency is properly tuned during */
343   /* manufacturing and is not changed after reset. If future requirements */
344   /* for re-tuning by user, we can add support for that. */
345   return EFM32_LFRCO_FREQ;
346 }
347 
348 /***************************************************************************//**
349  * @brief
350  *   Get ultra low frequency RC oscillator clock frequency for target system.
351  *
352  * @note
353  *   This is an EFM32 proprietary function, not part of the CMSIS definition.
354  *
355  * @return
356  *   ULFRCO frequency in Hz.
357  ******************************************************************************/
SystemULFRCOClockGet(void)358 uint32_t SystemULFRCOClockGet(void)
359 {
360   /* The ULFRCO frequency is not tuned, and can be very inaccurate */
361   return EFM32_ULFRCO_FREQ;
362 }
363 
364 /***************************************************************************//**
365  * @brief
366  *   Get low frequency crystal oscillator clock frequency for target system.
367  *
368  * @note
369  *   This is an EFM32 proprietary function, not part of the CMSIS definition.
370  *
371  * @return
372  *   LFXO frequency in Hz.
373  ******************************************************************************/
SystemLFXOClockGet(void)374 uint32_t SystemLFXOClockGet(void)
375 {
376   /* External crystal oscillator present? */
377 #if (EFM32_LFXO_FREQ > 0U)
378   return SystemLFXOClock;
379 #else
380   return 0U;
381 #endif
382 }
383 
384 /***************************************************************************//**
385  * @brief
386  *   Set low frequency crystal oscillator clock frequency for target system.
387  *
388  * @note
389  *   This function is mainly provided for being able to handle target systems
390  *   with different HF crystal oscillator frequencies run-time. If used, it
391  *   should probably only be used once during system startup.
392  *
393  * @note
394  *   This is an EFM32 proprietary function, not part of the CMSIS definition.
395  *
396  * @param[in] freq
397  *   LFXO frequency in Hz used for target.
398  ******************************************************************************/
SystemLFXOClockSet(uint32_t freq)399 void SystemLFXOClockSet(uint32_t freq)
400 {
401   /* External crystal oscillator present? */
402 #if (EFM32_LFXO_FREQ > 0U)
403   SystemLFXOClock = freq;
404 
405   /* Update core clock frequency if LFXO is used to clock core */
406   if ((CMU->HFCLKSTATUS & _CMU_HFCLKSTATUS_SELECTED_MASK)
407       == CMU_HFCLKSTATUS_SELECTED_LFXO) {
408     /* The function will update the global variable */
409     (void)SystemCoreClockGet();
410   }
411 #else
412   (void)freq; /* Unused parameter */
413 #endif
414 }
415