1 /***************************************************************************//**
\r
2 * @file system_efm32gg.c
\r
3 * @brief CMSIS Cortex-M3 System Layer for EFM32GG devices.
\r
5 ******************************************************************************
\r
7 * <b>(C) 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 EFM32_nFXO_FREQ */
\r
52 /* values according to board design. By defining the EFM32_nFXO_FREQ to 0, */
\r
53 /* one indicates that the oscillator is not present, in order to save some */
\r
56 #ifndef EFM32_HFXO_FREQ
\r
57 #ifdef _EFM32_GIANT_FAMILY
\r
58 #define EFM32_HFXO_FREQ (48000000UL)
\r
60 #define EFM32_HFXO_FREQ (32000000UL)
\r
63 /* Do not define variable if HF crystal oscillator not present */
\r
64 #if (EFM32_HFXO_FREQ > 0)
\r
65 /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
\r
66 /** System HFXO clock. */
\r
67 static uint32_t SystemHFXOClock = EFM32_HFXO_FREQ;
\r
68 /** @endcond (DO_NOT_INCLUDE_WITH_DOXYGEN) */
\r
71 #ifndef EFM32_LFXO_FREQ
\r
72 #define EFM32_LFXO_FREQ (EFM32_LFRCO_FREQ)
\r
74 /* Do not define variable if LF crystal oscillator not present */
\r
75 #if (EFM32_LFXO_FREQ > 0)
\r
76 /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
\r
77 /** System LFXO clock. */
\r
78 static uint32_t SystemLFXOClock = EFM32_LFXO_FREQ;
\r
79 /** @endcond (DO_NOT_INCLUDE_WITH_DOXYGEN) */
\r
82 /* Inline function to get the chip's Production Revision. */
\r
83 __STATIC_INLINE uint8_t GetProdRev(void)
\r
85 return ((DEVINFO->PART & _DEVINFO_PART_PROD_REV_MASK)
\r
86 >> _DEVINFO_PART_PROD_REV_SHIFT);
\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
102 /*******************************************************************************
\r
103 ************************** GLOBAL FUNCTIONS *******************************
\r
104 ******************************************************************************/
\r
106 /***************************************************************************//**
\r
108 * Get the current core clock frequency.
\r
111 * Calculate and get the current core clock frequency based on the current
\r
112 * configuration. Assuming that the SystemCoreClock global variable is
\r
113 * maintained, the core clock frequency is stored in that variable as well.
\r
114 * This function will however calculate the core clock based on actual HW
\r
115 * configuration. It will also update the SystemCoreClock global variable.
\r
118 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
121 * The current core clock frequency in Hz.
\r
122 ******************************************************************************/
\r
123 uint32_t SystemCoreClockGet(void)
\r
127 ret = SystemHFClockGet();
\r
128 #if defined (_EFM32_GIANT_FAMILY)
\r
129 /* Leopard/Giant Gecko has an additional divider */
\r
130 ret = ret / (1 + ((CMU->CTRL & _CMU_CTRL_HFCLKDIV_MASK)>>_CMU_CTRL_HFCLKDIV_SHIFT));
\r
132 ret >>= (CMU->HFCORECLKDIV & _CMU_HFCORECLKDIV_HFCORECLKDIV_MASK) >>
\r
133 _CMU_HFCORECLKDIV_HFCORECLKDIV_SHIFT;
\r
135 /* Keep CMSIS variable up-to-date just in case */
\r
136 SystemCoreClock = ret;
\r
142 /***************************************************************************//**
\r
144 * Get the current HFCLK frequency.
\r
147 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
150 * The current HFCLK frequency in Hz.
\r
151 ******************************************************************************/
\r
152 uint32_t SystemHFClockGet(void)
\r
156 switch (CMU->STATUS & (CMU_STATUS_HFRCOSEL | CMU_STATUS_HFXOSEL |
\r
157 CMU_STATUS_LFRCOSEL | CMU_STATUS_LFXOSEL))
\r
159 case CMU_STATUS_LFXOSEL:
\r
160 #if (EFM32_LFXO_FREQ > 0)
\r
161 ret = SystemLFXOClock;
\r
163 /* We should not get here, since core should not be clocked. May */
\r
164 /* be caused by a misconfiguration though. */
\r
169 case CMU_STATUS_LFRCOSEL:
\r
170 ret = EFM32_LFRCO_FREQ;
\r
173 case CMU_STATUS_HFXOSEL:
\r
174 #if (EFM32_HFXO_FREQ > 0)
\r
175 ret = SystemHFXOClock;
\r
177 /* We should not get here, since core should not be clocked. May */
\r
178 /* be caused by a misconfiguration though. */
\r
183 default: /* CMU_STATUS_HFRCOSEL */
\r
184 switch (CMU->HFRCOCTRL & _CMU_HFRCOCTRL_BAND_MASK)
\r
186 case CMU_HFRCOCTRL_BAND_28MHZ:
\r
190 case CMU_HFRCOCTRL_BAND_21MHZ:
\r
194 case CMU_HFRCOCTRL_BAND_14MHZ:
\r
198 case CMU_HFRCOCTRL_BAND_11MHZ:
\r
202 case CMU_HFRCOCTRL_BAND_7MHZ:
\r
203 if ( GetProdRev() >= 19 )
\r
209 case CMU_HFRCOCTRL_BAND_1MHZ:
\r
210 if ( GetProdRev() >= 19 )
\r
227 /**************************************************************************//**
\r
229 * Get high frequency crystal oscillator clock frequency for target system.
\r
232 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
235 * HFXO frequency in Hz.
\r
236 *****************************************************************************/
\r
237 uint32_t SystemHFXOClockGet(void)
\r
239 /* External crystal oscillator present? */
\r
240 #if (EFM32_HFXO_FREQ > 0)
\r
241 return SystemHFXOClock;
\r
248 /**************************************************************************//**
\r
250 * Set high frequency crystal oscillator clock frequency for target system.
\r
253 * This function is mainly provided for being able to handle target systems
\r
254 * with different HF crystal oscillator frequencies run-time. If used, it
\r
255 * should probably only be used once during system startup.
\r
258 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
261 * HFXO frequency in Hz used for target.
\r
262 *****************************************************************************/
\r
263 void SystemHFXOClockSet(uint32_t freq)
\r
265 /* External crystal oscillator present? */
\r
266 #if (EFM32_HFXO_FREQ > 0)
\r
267 SystemHFXOClock = freq;
\r
269 /* Update core clock frequency if HFXO is used to clock core */
\r
270 if (CMU->STATUS & CMU_STATUS_HFXOSEL)
\r
272 /* The function will update the global variable */
\r
273 SystemCoreClockGet();
\r
276 (void)freq; /* Unused parameter */
\r
281 /**************************************************************************//**
\r
283 * Initialize the system.
\r
286 * Do required generic HW system init.
\r
289 * This function is invoked during system init, before the main() routine
\r
290 * and any data has been initialized. For this reason, it cannot do any
\r
291 * initialization of variables etc.
\r
292 *****************************************************************************/
\r
293 void SystemInit(void)
\r
298 /**************************************************************************//**
\r
300 * Get low frequency RC oscillator clock frequency for target system.
\r
303 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
306 * LFRCO frequency in Hz.
\r
307 *****************************************************************************/
\r
308 uint32_t SystemLFRCOClockGet(void)
\r
310 /* Currently we assume that this frequency is properly tuned during */
\r
311 /* manufacturing and is not changed after reset. If future requirements */
\r
312 /* for re-tuning by user, we can add support for that. */
\r
313 return EFM32_LFRCO_FREQ;
\r
317 /**************************************************************************//**
\r
319 * Get ultra low frequency RC oscillator clock frequency for target system.
\r
322 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
325 * ULFRCO frequency in Hz.
\r
326 *****************************************************************************/
\r
327 uint32_t SystemULFRCOClockGet(void)
\r
329 /* The ULFRCO frequency is not tuned, and can be very inaccurate */
\r
330 return EFM32_ULFRCO_FREQ;
\r
334 /**************************************************************************//**
\r
336 * Get low frequency crystal oscillator clock frequency for target system.
\r
339 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
342 * LFXO frequency in Hz.
\r
343 *****************************************************************************/
\r
344 uint32_t SystemLFXOClockGet(void)
\r
346 /* External crystal oscillator present? */
\r
347 #if (EFM32_LFXO_FREQ > 0)
\r
348 return SystemLFXOClock;
\r
355 /**************************************************************************//**
\r
357 * Set low frequency crystal oscillator clock frequency for target system.
\r
360 * This function is mainly provided for being able to handle target systems
\r
361 * with different HF crystal oscillator frequencies run-time. If used, it
\r
362 * should probably only be used once during system startup.
\r
365 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
368 * LFXO frequency in Hz used for target.
\r
369 *****************************************************************************/
\r
370 void SystemLFXOClockSet(uint32_t freq)
\r
372 /* External crystal oscillator present? */
\r
373 #if (EFM32_LFXO_FREQ > 0)
\r
374 SystemLFXOClock = freq;
\r
376 /* Update core clock frequency if LFXO is used to clock core */
\r
377 if (CMU->STATUS & CMU_STATUS_LFXOSEL)
\r
379 /* The function will update the global variable */
\r
380 SystemCoreClockGet();
\r
383 (void)freq; /* Unused parameter */
\r