1 /***************************************************************************//**
\r
2 * @file system_efm32pg1b.c
\r
3 * @brief CMSIS Cortex-M3/M4 System Layer for EFM32 devices.
\r
5 ******************************************************************************
\r
7 * <b>Copyright 2015 Silicon Laboratories, Inc. http://www.silabs.com</b>
\r
8 ******************************************************************************
\r
10 * Permission is granted to anyone to use this software for any purpose,
\r
11 * including commercial applications, and to alter it and redistribute it
\r
12 * freely, subject to the following restrictions:
\r
14 * 1. The origin of this software must not be misrepresented; you must not
\r
15 * claim that you wrote the original software.@n
\r
16 * 2. Altered source versions must be plainly marked as such, and must not be
\r
17 * misrepresented as being the original software.@n
\r
18 * 3. This notice may not be removed or altered from any source distribution.
\r
20 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Laboratories, Inc.
\r
21 * has no obligation to support this Software. Silicon Laboratories, Inc. is
\r
22 * providing the Software "AS IS", with no express or implied warranties of any
\r
23 * kind, including, but not limited to, any implied warranties of
\r
24 * merchantability or fitness for any particular purpose or warranties against
\r
25 * infringement of any proprietary rights of a third party.
\r
27 * Silicon Laboratories, Inc. will not be liable for any consequential,
\r
28 * incidental, or special damages, or any other relief, or for any claim by
\r
29 * any third party, arising from your use of this Software.
\r
31 *****************************************************************************/
\r
34 #include "em_device.h"
\r
36 /*******************************************************************************
\r
37 ****************************** DEFINES ************************************
\r
38 ******************************************************************************/
\r
40 /** LFRCO frequency, tuned to below frequency during manufacturing. */
\r
41 #define EFM32_LFRCO_FREQ (32768UL)
\r
42 #define EFM32_ULFRCO_FREQ (1000UL)
\r
44 /*******************************************************************************
\r
45 ************************** LOCAL VARIABLES ********************************
\r
46 ******************************************************************************/
\r
48 /* System oscillator frequencies. These frequencies are normally constant */
\r
49 /* for a target, but they are made configurable in order to allow run-time */
\r
50 /* handling of different boards. The crystal oscillator clocks can be set */
\r
51 /* compile time to a non-default value by defining respective EFM_nFXO_FREQ */
\r
52 /* values according to board design. By defining the EFM_nFXO_FREQ to 0, */
\r
53 /* one indicates that the oscillator is not present, in order to save some */
\r
56 #ifndef EFM32_HFRCO_MAX_FREQ
\r
57 #define EFM32_HFRCO_MAX_FREQ (38000000UL)
\r
60 #ifndef EFM32_HFXO_FREQ
\r
61 #define EFM32_HFXO_FREQ (40000000UL)
\r
64 #ifndef EFM32_HFRCO_STARTUP_FREQ
\r
65 #define EFM32_HFRCO_STARTUP_FREQ (19000000UL)
\r
69 /* Do not define variable if HF crystal oscillator not present */
\r
70 #if (EFM32_HFXO_FREQ > 0UL)
\r
71 /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
\r
72 /** System HFXO clock. */
\r
73 static uint32_t SystemHFXOClock = EFM32_HFXO_FREQ;
\r
74 /** @endcond (DO_NOT_INCLUDE_WITH_DOXYGEN) */
\r
77 #ifndef EFM32_LFXO_FREQ
\r
78 #define EFM32_LFXO_FREQ (EFM32_LFRCO_FREQ)
\r
80 /* Do not define variable if LF crystal oscillator not present */
\r
81 #if (EFM32_LFXO_FREQ > 0UL)
\r
82 /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
\r
83 /** System LFXO clock. */
\r
84 static uint32_t SystemLFXOClock = 32768UL;
\r
85 /** @endcond (DO_NOT_INCLUDE_WITH_DOXYGEN) */
\r
89 /*******************************************************************************
\r
90 ************************** GLOBAL VARIABLES *******************************
\r
91 ******************************************************************************/
\r
95 * System System Clock Frequency (Core Clock).
\r
98 * Required CMSIS global variable that must be kept up-to-date.
\r
100 uint32_t SystemCoreClock;
\r
105 * System HFRCO frequency
\r
108 * This is an EFM32 proprietary variable, not part of the CMSIS definition.
\r
111 * Frequency of the system HFRCO oscillator
\r
113 uint32_t SystemHfrcoFreq = EFM32_HFRCO_STARTUP_FREQ;
\r
116 /*******************************************************************************
\r
117 ************************** GLOBAL FUNCTIONS *******************************
\r
118 ******************************************************************************/
\r
120 /***************************************************************************//**
\r
122 * Get the current core clock frequency.
\r
125 * Calculate and get the current core clock frequency based on the current
\r
126 * configuration. Assuming that the SystemCoreClock global variable is
\r
127 * maintained, the core clock frequency is stored in that variable as well.
\r
128 * This function will however calculate the core clock based on actual HW
\r
129 * configuration. It will also update the SystemCoreClock global variable.
\r
132 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
135 * The current core clock frequency in Hz.
\r
136 ******************************************************************************/
\r
137 uint32_t SystemCoreClockGet(void)
\r
142 ret = SystemHFClockGet();
\r
143 presc = (CMU->HFCOREPRESC & _CMU_HFCOREPRESC_PRESC_MASK) >>
\r
144 _CMU_HFCOREPRESC_PRESC_SHIFT;
\r
145 ret /= (presc + 1);
\r
147 /* Keep CMSIS system clock variable up-to-date */
\r
148 SystemCoreClock = ret;
\r
154 /***************************************************************************//**
\r
156 * Get the maximum core clock frequency.
\r
159 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
162 * The maximum core clock frequency in Hz.
\r
163 ******************************************************************************/
\r
164 uint32_t SystemMaxCoreClockGet(void)
\r
166 return (EFM32_HFRCO_MAX_FREQ > EFM32_HFXO_FREQ ? \
\r
167 EFM32_HFRCO_MAX_FREQ : EFM32_HFXO_FREQ);
\r
171 /***************************************************************************//**
\r
173 * Get the current HFCLK frequency.
\r
176 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
179 * The current HFCLK frequency in Hz.
\r
180 ******************************************************************************/
\r
181 uint32_t SystemHFClockGet(void)
\r
185 switch (CMU->HFCLKSTATUS & _CMU_HFCLKSTATUS_SELECTED_MASK)
\r
187 case CMU_HFCLKSTATUS_SELECTED_LFXO:
\r
188 #if (EFM32_LFXO_FREQ > 0)
\r
189 ret = SystemLFXOClock;
\r
191 /* We should not get here, since core should not be clocked. May */
\r
192 /* be caused by a misconfiguration though. */
\r
197 case CMU_HFCLKSTATUS_SELECTED_LFRCO:
\r
198 ret = EFM32_LFRCO_FREQ;
\r
201 case CMU_HFCLKSTATUS_SELECTED_HFXO:
\r
202 #if (EFM32_HFXO_FREQ > 0)
\r
203 ret = SystemHFXOClock;
\r
205 /* We should not get here, since core should not be clocked. May */
\r
206 /* be caused by a misconfiguration though. */
\r
211 default: /* CMU_HFCLKSTATUS_SELECTED_HFRCO */
\r
212 ret = SystemHfrcoFreq;
\r
220 /**************************************************************************//**
\r
222 * Get high frequency crystal oscillator clock frequency for target system.
\r
225 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
228 * HFXO frequency in Hz.
\r
229 *****************************************************************************/
\r
230 uint32_t SystemHFXOClockGet(void)
\r
232 /* External crystal oscillator present? */
\r
233 #if (EFM32_HFXO_FREQ > 0)
\r
234 return SystemHFXOClock;
\r
241 /**************************************************************************//**
\r
243 * Set high frequency crystal oscillator clock frequency for target system.
\r
246 * This function is mainly provided for being able to handle target systems
\r
247 * with different HF crystal oscillator frequencies run-time. If used, it
\r
248 * should probably only be used once during system startup.
\r
251 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
254 * HFXO frequency in Hz used for target.
\r
255 *****************************************************************************/
\r
256 void SystemHFXOClockSet(uint32_t freq)
\r
258 /* External crystal oscillator present? */
\r
259 #if (EFM32_HFXO_FREQ > 0)
\r
260 SystemHFXOClock = freq;
\r
262 /* Update core clock frequency if HFXO is used to clock core */
\r
263 if ((CMU->HFCLKSTATUS & _CMU_HFCLKSTATUS_SELECTED_MASK) == CMU_HFCLKSTATUS_SELECTED_HFXO)
\r
265 /* The function will update the global variable */
\r
266 SystemCoreClockGet();
\r
269 (void)freq; /* Unused parameter */
\r
274 /**************************************************************************//**
\r
276 * Initialize the system.
\r
279 * Do required generic HW system init.
\r
282 * This function is invoked during system init, before the main() routine
\r
283 * and any data has been initialized. For this reason, it cannot do any
\r
284 * initialization of variables etc.
\r
285 *****************************************************************************/
\r
286 void SystemInit(void)
\r
288 #if (__FPU_PRESENT == 1)
\r
289 /* Set floating point coprosessor access mode. */
\r
290 SCB->CPACR |= ((3UL << 10 * 2) | /* set CP10 Full Access */
\r
291 (3UL << 11 * 2)); /* set CP11 Full Access */
\r
296 /**************************************************************************//**
\r
298 * Get low frequency RC oscillator clock frequency for target system.
\r
301 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
304 * LFRCO frequency in Hz.
\r
305 *****************************************************************************/
\r
306 uint32_t SystemLFRCOClockGet(void)
\r
308 /* Currently we assume that this frequency is properly tuned during */
\r
309 /* manufacturing and is not changed after reset. If future requirements */
\r
310 /* for re-tuning by user, we can add support for that. */
\r
311 return EFM32_LFRCO_FREQ;
\r
315 /**************************************************************************//**
\r
317 * Get ultra low frequency RC oscillator clock frequency for target system.
\r
320 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
323 * ULFRCO frequency in Hz.
\r
324 *****************************************************************************/
\r
325 uint32_t SystemULFRCOClockGet(void)
\r
327 /* The ULFRCO frequency is not tuned, and can be very inaccurate */
\r
328 return EFM32_ULFRCO_FREQ;
\r
332 /**************************************************************************//**
\r
334 * Get low frequency crystal oscillator clock frequency for target system.
\r
337 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
340 * LFXO frequency in Hz.
\r
341 *****************************************************************************/
\r
342 uint32_t SystemLFXOClockGet(void)
\r
344 /* External crystal oscillator present? */
\r
345 #if (EFM32_LFXO_FREQ > 0)
\r
346 return SystemLFXOClock;
\r
353 /**************************************************************************//**
\r
355 * Set low frequency crystal oscillator clock frequency for target system.
\r
358 * This function is mainly provided for being able to handle target systems
\r
359 * with different HF crystal oscillator frequencies run-time. If used, it
\r
360 * should probably only be used once during system startup.
\r
363 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
366 * LFXO frequency in Hz used for target.
\r
367 *****************************************************************************/
\r
368 void SystemLFXOClockSet(uint32_t freq)
\r
370 /* External crystal oscillator present? */
\r
371 #if (EFM32_LFXO_FREQ > 0)
\r
372 SystemLFXOClock = freq;
\r
374 /* Update core clock frequency if LFXO is used to clock core */
\r
375 if ((CMU->HFCLKSTATUS & _CMU_HFCLKSTATUS_SELECTED_MASK) == CMU_HFCLKSTATUS_SELECTED_LFXO)
\r
377 /* The function will update the global variable */
\r
378 SystemCoreClockGet();
\r
381 (void)freq; /* Unused parameter */
\r