EFM32 Wonder Gecko Software Documentation  efm32wg-doc-5.1.2
system_efm32wg.c
Go to the documentation of this file.
1 /***************************************************************************/
33 #include <stdint.h>
34 #include "em_device.h"
35 
36 /*******************************************************************************
37  ****************************** DEFINES ************************************
38  ******************************************************************************/
39 
41 #define EFM32_LFRCO_FREQ (32768UL)
42 #define EFM32_ULFRCO_FREQ (1000UL)
43 
44 /*******************************************************************************
45  ************************** LOCAL VARIABLES ********************************
46  ******************************************************************************/
47 
48 /* System oscillator frequencies. These frequencies are normally constant */
49 /* for a target, but they are made configurable in order to allow run-time */
50 /* handling of different boards. The crystal oscillator clocks can be set */
51 /* compile time to a non-default value by defining respective EFM32_nFXO_FREQ */
52 /* values according to board design. By defining the EFM32_nFXO_FREQ to 0, */
53 /* one indicates that the oscillator is not present, in order to save some */
54 /* SW footprint. */
55 
56 #ifndef EFM32_HFXO_FREQ
57 #define EFM32_HFXO_FREQ (48000000UL)
58 #endif
59 
60 #define EFM32_HFRCO_MAX_FREQ (28000000UL)
61 
62 /* Do not define variable if HF crystal oscillator not present */
63 #if (EFM32_HFXO_FREQ > 0)
64 
66 static uint32_t SystemHFXOClock = EFM32_HFXO_FREQ;
68 #endif
69 
70 #ifndef EFM32_LFXO_FREQ
71 #define EFM32_LFXO_FREQ (EFM32_LFRCO_FREQ)
72 #endif
73 
74 /* Do not define variable if LF crystal oscillator not present */
75 #if (EFM32_LFXO_FREQ > 0)
76 
78 static uint32_t SystemLFXOClock = EFM32_LFXO_FREQ;
80 #endif
81 
82 /* Inline function to get the chip's Production Revision. */
83 __STATIC_INLINE uint8_t GetProdRev(void)
84 {
85  return ((DEVINFO->PART & _DEVINFO_PART_PROD_REV_MASK)
87 }
88 
89 /*******************************************************************************
90  ************************** GLOBAL VARIABLES *******************************
91  ******************************************************************************/
92 
101 
102 /*******************************************************************************
103  ************************** GLOBAL FUNCTIONS *******************************
104  ******************************************************************************/
105 
106 /***************************************************************************/
123 uint32_t SystemCoreClockGet(void)
124 {
125  uint32_t ret;
126 
127  ret = SystemHFClockGet();
128  ret >>= (CMU->HFCORECLKDIV & _CMU_HFCORECLKDIV_HFCORECLKDIV_MASK) >>
130 
131  /* Keep CMSIS variable up-to-date just in case */
132  SystemCoreClock = ret;
133 
134  return ret;
135 }
136 
137 
138 /***************************************************************************/
148 uint32_t SystemMaxCoreClockGet(void)
149 {
150  return (EFM32_HFRCO_MAX_FREQ > EFM32_HFXO_FREQ ? \
151  EFM32_HFRCO_MAX_FREQ : EFM32_HFXO_FREQ);
152 }
153 
154 
155 /***************************************************************************/
165 uint32_t SystemHFClockGet(void)
166 {
167  uint32_t ret;
168 
169  switch (CMU->STATUS & (CMU_STATUS_HFRCOSEL | CMU_STATUS_HFXOSEL |
171  {
172  case CMU_STATUS_LFXOSEL:
173 #if (EFM32_LFXO_FREQ > 0)
174  ret = SystemLFXOClock;
175 #else
176  /* We should not get here, since core should not be clocked. May */
177  /* be caused by a misconfiguration though. */
178  ret = 0;
179 #endif
180  break;
181 
182  case CMU_STATUS_LFRCOSEL:
183  ret = EFM32_LFRCO_FREQ;
184  break;
185 
186  case CMU_STATUS_HFXOSEL:
187 #if (EFM32_HFXO_FREQ > 0)
188  ret = SystemHFXOClock;
189 #else
190  /* We should not get here, since core should not be clocked. May */
191  /* be caused by a misconfiguration though. */
192  ret = 0;
193 #endif
194  break;
195 
196  default: /* CMU_STATUS_HFRCOSEL */
197  switch (CMU->HFRCOCTRL & _CMU_HFRCOCTRL_BAND_MASK)
198  {
200  ret = 28000000;
201  break;
202 
204  ret = 21000000;
205  break;
206 
208  ret = 14000000;
209  break;
210 
212  ret = 11000000;
213  break;
214 
216  if ( GetProdRev() >= 19 )
217  ret = 6600000;
218  else
219  ret = 7000000;
220  break;
221 
223  if ( GetProdRev() >= 19 )
224  ret = 1200000;
225  else
226  ret = 1000000;
227  break;
228 
229  default:
230  ret = 0;
231  break;
232  }
233  break;
234  }
235 
236  return ret / (1U + ((CMU->CTRL & _CMU_CTRL_HFCLKDIV_MASK)
238 }
239 
240 
241 /**************************************************************************/
251 uint32_t SystemHFXOClockGet(void)
252 {
253  /* External crystal oscillator present? */
254 #if (EFM32_HFXO_FREQ > 0)
255  return SystemHFXOClock;
256 #else
257  return 0;
258 #endif
259 }
260 
261 
262 /**************************************************************************/
277 void SystemHFXOClockSet(uint32_t freq)
278 {
279  /* External crystal oscillator present? */
280 #if (EFM32_HFXO_FREQ > 0)
281  SystemHFXOClock = freq;
282 
283  /* Update core clock frequency if HFXO is used to clock core */
284  if (CMU->STATUS & CMU_STATUS_HFXOSEL)
285  {
286  /* The function will update the global variable */
288  }
289 #else
290  (void)freq; /* Unused parameter */
291 #endif
292 }
293 
294 
295 /**************************************************************************/
307 void SystemInit(void)
308 {
309 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
310  /* Set floating point coprosessor access mode. */
311  SCB->CPACR |= ((3UL << 10*2) | /* set CP10 Full Access */
312  (3UL << 11*2) ); /* set CP11 Full Access */
313 #endif
314 }
315 
316 
317 /**************************************************************************/
327 uint32_t SystemLFRCOClockGet(void)
328 {
329  /* Currently we assume that this frequency is properly tuned during */
330  /* manufacturing and is not changed after reset. If future requirements */
331  /* for re-tuning by user, we can add support for that. */
332  return EFM32_LFRCO_FREQ;
333 }
334 
335 
336 /**************************************************************************/
346 uint32_t SystemULFRCOClockGet(void)
347 {
348  /* The ULFRCO frequency is not tuned, and can be very inaccurate */
349  return EFM32_ULFRCO_FREQ;
350 }
351 
352 
353 /**************************************************************************/
363 uint32_t SystemLFXOClockGet(void)
364 {
365  /* External crystal oscillator present? */
366 #if (EFM32_LFXO_FREQ > 0)
367  return SystemLFXOClock;
368 #else
369  return 0;
370 #endif
371 }
372 
373 
374 /**************************************************************************/
389 void SystemLFXOClockSet(uint32_t freq)
390 {
391  /* External crystal oscillator present? */
392 #if (EFM32_LFXO_FREQ > 0)
393  SystemLFXOClock = freq;
394 
395  /* Update core clock frequency if LFXO is used to clock core */
396  if (CMU->STATUS & CMU_STATUS_LFXOSEL)
397  {
398  /* The function will update the global variable */
400  }
401 #else
402  (void)freq; /* Unused parameter */
403 #endif
404 }
#define DEVINFO
uint32_t SystemCoreClockGet(void)
Get the current core clock frequency.
#define _CMU_HFCORECLKDIV_HFCORECLKDIV_MASK
Definition: efm32wg_cmu.h:233
void SystemLFXOClockSet(uint32_t freq)
Set low frequency crystal oscillator clock frequency for target system.
#define CMU_STATUS_HFXOSEL
Definition: efm32wg_cmu.h:600
#define CMU
#define EFM32_LFXO_FREQ
__STATIC_INLINE uint8_t GetProdRev(void)
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
#define CMU_STATUS_HFRCOSEL
Definition: efm32wg_cmu.h:595
#define CMU_HFRCOCTRL_BAND_28MHZ
Definition: efm32wg_cmu.h:321
#define _CMU_HFCORECLKDIV_HFCORECLKDIV_SHIFT
Definition: efm32wg_cmu.h:232
#define CMU_STATUS_LFXOSEL
Definition: efm32wg_cmu.h:610
uint32_t SystemLFXOClockGet(void)
Get low frequency crystal oscillator clock frequency for target system.
#define _DEVINFO_PART_PROD_REV_SHIFT
#define CMU_HFRCOCTRL_BAND_21MHZ
Definition: efm32wg_cmu.h:320
#define CMU_HFRCOCTRL_BAND_11MHZ
Definition: efm32wg_cmu.h:317
#define CMU_HFRCOCTRL_BAND_14MHZ
Definition: efm32wg_cmu.h:319
#define _DEVINFO_PART_PROD_REV_MASK
uint32_t SystemHFXOClockGet(void)
Get high frequency crystal oscillator clock frequency for target system.
uint32_t SystemHFClockGet(void)
Get the current HFCLK frequency.
#define _CMU_HFRCOCTRL_BAND_MASK
Definition: efm32wg_cmu.h:307
void SystemHFXOClockSet(uint32_t freq)
Set high frequency crystal oscillator clock frequency for target system.
#define EFM32_LFRCO_FREQ
uint32_t SystemULFRCOClockGet(void)
Get ultra low frequency RC oscillator clock frequency for target system.
#define CMU_HFRCOCTRL_BAND_7MHZ
Definition: efm32wg_cmu.h:316
uint32_t SystemMaxCoreClockGet(void)
Get the maximum core clock frequency.
uint32_t SystemLFRCOClockGet(void)
Get low frequency RC oscillator clock frequency for target system.
#define CMU_HFRCOCTRL_BAND_1MHZ
Definition: efm32wg_cmu.h:315
#define _CMU_CTRL_HFCLKDIV_SHIFT
Definition: efm32wg_cmu.h:153
uint32_t SystemCoreClock
System System Clock Frequency (Core Clock).
#define _CMU_CTRL_HFCLKDIV_MASK
Definition: efm32wg_cmu.h:154
void SystemInit(void)
Initialize the system.
#define CMU_STATUS_LFRCOSEL
Definition: efm32wg_cmu.h:605