1 /***************************************************************************//**
\r
2 * @file system_efm32gg.c
\r
3 * @brief CMSIS Cortex-M3 System Layer for EFM32GG 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 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 #define EFM32_HFXO_FREQ (48000000UL)
\r
60 #define EFM32_HFRCO_MAX_FREQ (28000000UL)
\r
62 /* Do not define variable if HF crystal oscillator not present */
\r
63 #if (EFM32_HFXO_FREQ > 0)
\r
64 /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
\r
65 /** System HFXO clock. */
\r
66 static uint32_t SystemHFXOClock = EFM32_HFXO_FREQ;
\r
67 /** @endcond (DO_NOT_INCLUDE_WITH_DOXYGEN) */
\r
70 #ifndef EFM32_LFXO_FREQ
\r
71 #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 /* Leopard/Giant Gecko has an additional divider */
\r
129 ret = ret / (1 + ((CMU->CTRL & _CMU_CTRL_HFCLKDIV_MASK)>>_CMU_CTRL_HFCLKDIV_SHIFT));
\r
130 ret >>= (CMU->HFCORECLKDIV & _CMU_HFCORECLKDIV_HFCORECLKDIV_MASK) >>
\r
131 _CMU_HFCORECLKDIV_HFCORECLKDIV_SHIFT;
\r
133 /* Keep CMSIS variable up-to-date just in case */
\r
134 SystemCoreClock = ret;
\r
140 /***************************************************************************//**
\r
142 * Get the maximum core clock frequency.
\r
145 * This is an EFR32 proprietary function, not part of the CMSIS definition.
\r
148 * The maximum core clock frequency in Hz.
\r
149 ******************************************************************************/
\r
150 uint32_t SystemMaxCoreClockGet(void)
\r
152 return (EFM32_HFRCO_MAX_FREQ > EFM32_HFXO_FREQ ? \
\r
153 EFM32_HFRCO_MAX_FREQ : EFM32_HFXO_FREQ);
\r
157 /***************************************************************************//**
\r
159 * Get the current HFCLK frequency.
\r
162 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
165 * The current HFCLK frequency in Hz.
\r
166 ******************************************************************************/
\r
167 uint32_t SystemHFClockGet(void)
\r
171 switch (CMU->STATUS & (CMU_STATUS_HFRCOSEL | CMU_STATUS_HFXOSEL |
\r
172 CMU_STATUS_LFRCOSEL | CMU_STATUS_LFXOSEL))
\r
174 case CMU_STATUS_LFXOSEL:
\r
175 #if (EFM32_LFXO_FREQ > 0)
\r
176 ret = SystemLFXOClock;
\r
178 /* We should not get here, since core should not be clocked. May */
\r
179 /* be caused by a misconfiguration though. */
\r
184 case CMU_STATUS_LFRCOSEL:
\r
185 ret = EFM32_LFRCO_FREQ;
\r
188 case CMU_STATUS_HFXOSEL:
\r
189 #if (EFM32_HFXO_FREQ > 0)
\r
190 ret = SystemHFXOClock;
\r
192 /* We should not get here, since core should not be clocked. May */
\r
193 /* be caused by a misconfiguration though. */
\r
198 default: /* CMU_STATUS_HFRCOSEL */
\r
199 switch (CMU->HFRCOCTRL & _CMU_HFRCOCTRL_BAND_MASK)
\r
201 case CMU_HFRCOCTRL_BAND_28MHZ:
\r
205 case CMU_HFRCOCTRL_BAND_21MHZ:
\r
209 case CMU_HFRCOCTRL_BAND_14MHZ:
\r
213 case CMU_HFRCOCTRL_BAND_11MHZ:
\r
217 case CMU_HFRCOCTRL_BAND_7MHZ:
\r
218 if ( GetProdRev() >= 19 )
\r
224 case CMU_HFRCOCTRL_BAND_1MHZ:
\r
225 if ( GetProdRev() >= 19 )
\r
242 /**************************************************************************//**
\r
244 * Get high frequency crystal oscillator clock frequency for target system.
\r
247 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
250 * HFXO frequency in Hz.
\r
251 *****************************************************************************/
\r
252 uint32_t SystemHFXOClockGet(void)
\r
254 /* External crystal oscillator present? */
\r
255 #if (EFM32_HFXO_FREQ > 0)
\r
256 return SystemHFXOClock;
\r
263 /**************************************************************************//**
\r
265 * Set high frequency crystal oscillator clock frequency for target system.
\r
268 * This function is mainly provided for being able to handle target systems
\r
269 * with different HF crystal oscillator frequencies run-time. If used, it
\r
270 * should probably only be used once during system startup.
\r
273 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
276 * HFXO frequency in Hz used for target.
\r
277 *****************************************************************************/
\r
278 void SystemHFXOClockSet(uint32_t freq)
\r
280 /* External crystal oscillator present? */
\r
281 #if (EFM32_HFXO_FREQ > 0)
\r
282 SystemHFXOClock = freq;
\r
284 /* Update core clock frequency if HFXO is used to clock core */
\r
285 if (CMU->STATUS & CMU_STATUS_HFXOSEL)
\r
287 /* The function will update the global variable */
\r
288 SystemCoreClockGet();
\r
291 (void)freq; /* Unused parameter */
\r
296 /**************************************************************************//**
\r
298 * Initialize the system.
\r
301 * Do required generic HW system init.
\r
304 * This function is invoked during system init, before the main() routine
\r
305 * and any data has been initialized. For this reason, it cannot do any
\r
306 * initialization of variables etc.
\r
307 *****************************************************************************/
\r
308 void SystemInit(void)
\r
313 /**************************************************************************//**
\r
315 * Get low frequency RC oscillator clock frequency for target system.
\r
318 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
321 * LFRCO frequency in Hz.
\r
322 *****************************************************************************/
\r
323 uint32_t SystemLFRCOClockGet(void)
\r
325 /* Currently we assume that this frequency is properly tuned during */
\r
326 /* manufacturing and is not changed after reset. If future requirements */
\r
327 /* for re-tuning by user, we can add support for that. */
\r
328 return EFM32_LFRCO_FREQ;
\r
332 /**************************************************************************//**
\r
334 * Get ultra low frequency RC oscillator clock frequency for target system.
\r
337 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
340 * ULFRCO frequency in Hz.
\r
341 *****************************************************************************/
\r
342 uint32_t SystemULFRCOClockGet(void)
\r
344 /* The ULFRCO frequency is not tuned, and can be very inaccurate */
\r
345 return EFM32_ULFRCO_FREQ;
\r
349 /**************************************************************************//**
\r
351 * Get low frequency crystal oscillator clock frequency for target system.
\r
354 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
357 * LFXO frequency in Hz.
\r
358 *****************************************************************************/
\r
359 uint32_t SystemLFXOClockGet(void)
\r
361 /* External crystal oscillator present? */
\r
362 #if (EFM32_LFXO_FREQ > 0)
\r
363 return SystemLFXOClock;
\r
370 /**************************************************************************//**
\r
372 * Set low frequency crystal oscillator clock frequency for target system.
\r
375 * This function is mainly provided for being able to handle target systems
\r
376 * with different HF crystal oscillator frequencies run-time. If used, it
\r
377 * should probably only be used once during system startup.
\r
380 * This is an EFM32 proprietary function, not part of the CMSIS definition.
\r
383 * LFXO frequency in Hz used for target.
\r
384 *****************************************************************************/
\r
385 void SystemLFXOClockSet(uint32_t freq)
\r
387 /* External crystal oscillator present? */
\r
388 #if (EFM32_LFXO_FREQ > 0)
\r
389 SystemLFXOClock = freq;
\r
391 /* Update core clock frequency if LFXO is used to clock core */
\r
392 if (CMU->STATUS & CMU_STATUS_LFXOSEL)
\r
394 /* The function will update the global variable */
\r
395 SystemCoreClockGet();
\r
398 (void)freq; /* Unused parameter */
\r