EZR32 Wonder Gecko Software Documentation  ezr32wg-doc-4.2.1
system_ezr32wg.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 #if defined(_EFM32_GIANT_FAMILY) || defined(_EFM32_WONDER_FAMILY)
129  /* Leopard/Giant/Wonder Gecko has an additional divider */
130  ret = ret / (1 + ((CMU->CTRL & _CMU_CTRL_HFCLKDIV_MASK)>>_CMU_CTRL_HFCLKDIV_SHIFT));
131 #endif
132  ret >>= (CMU->HFCORECLKDIV & _CMU_HFCORECLKDIV_HFCORECLKDIV_MASK) >>
134 
135  /* Keep CMSIS variable up-to-date just in case */
136  SystemCoreClock = ret;
137 
138  return ret;
139 }
140 
141 
142 /***************************************************************************/
152 uint32_t SystemMaxCoreClockGet(void)
153 {
154  return (EFM32_HFRCO_MAX_FREQ > EFM32_HFXO_FREQ ? \
155  EFM32_HFRCO_MAX_FREQ : EFM32_HFXO_FREQ);
156 }
157 
158 
159 /***************************************************************************/
169 uint32_t SystemHFClockGet(void)
170 {
171  uint32_t ret;
172 
173  switch (CMU->STATUS & (CMU_STATUS_HFRCOSEL | CMU_STATUS_HFXOSEL |
175  {
176  case CMU_STATUS_LFXOSEL:
177 #if (EFM32_LFXO_FREQ > 0)
178  ret = SystemLFXOClock;
179 #else
180  /* We should not get here, since core should not be clocked. May */
181  /* be caused by a misconfiguration though. */
182  ret = 0;
183 #endif
184  break;
185 
186  case CMU_STATUS_LFRCOSEL:
187  ret = EFM32_LFRCO_FREQ;
188  break;
189 
190  case CMU_STATUS_HFXOSEL:
191 #if (EFM32_HFXO_FREQ > 0)
192  ret = SystemHFXOClock;
193 #else
194  /* We should not get here, since core should not be clocked. May */
195  /* be caused by a misconfiguration though. */
196  ret = 0;
197 #endif
198  break;
199 
200  default: /* CMU_STATUS_HFRCOSEL */
201  switch (CMU->HFRCOCTRL & _CMU_HFRCOCTRL_BAND_MASK)
202  {
204  ret = 28000000;
205  break;
206 
208  ret = 21000000;
209  break;
210 
212  ret = 14000000;
213  break;
214 
216  ret = 11000000;
217  break;
218 
220  if ( GetProdRev() >= 19 )
221  ret = 6600000;
222  else
223  ret = 7000000;
224  break;
225 
227  if ( GetProdRev() >= 19 )
228  ret = 1200000;
229  else
230  ret = 1000000;
231  break;
232 
233  default:
234  ret = 0;
235  break;
236  }
237  break;
238  }
239 
240  return ret;
241 }
242 
243 
244 /**************************************************************************/
254 uint32_t SystemHFXOClockGet(void)
255 {
256  /* External crystal oscillator present? */
257 #if (EFM32_HFXO_FREQ > 0)
258  return SystemHFXOClock;
259 #else
260  return 0;
261 #endif
262 }
263 
264 
265 /**************************************************************************/
280 void SystemHFXOClockSet(uint32_t freq)
281 {
282  /* External crystal oscillator present? */
283 #if (EFM32_HFXO_FREQ > 0)
284  SystemHFXOClock = freq;
285 
286  /* Update core clock frequency if HFXO is used to clock core */
287  if (CMU->STATUS & CMU_STATUS_HFXOSEL)
288  {
289  /* The function will update the global variable */
291  }
292 #else
293  (void)freq; /* Unused parameter */
294 #endif
295 }
296 
297 
298 /**************************************************************************/
310 void SystemInit(void)
311 {
312  /* Set floating point coprosessor access mode. */
313  SCB->CPACR |= ((3UL << 10*2) | /* set CP10 Full Access */
314  (3UL << 11*2) ); /* set CP11 Full Access */
315 }
316 
317 
318 /**************************************************************************/
328 uint32_t SystemLFRCOClockGet(void)
329 {
330  /* Currently we assume that this frequency is properly tuned during */
331  /* manufacturing and is not changed after reset. If future requirements */
332  /* for re-tuning by user, we can add support for that. */
333  return EFM32_LFRCO_FREQ;
334 }
335 
336 
337 /**************************************************************************/
347 uint32_t SystemULFRCOClockGet(void)
348 {
349  /* The ULFRCO frequency is not tuned, and can be very inaccurate */
350  return EFM32_ULFRCO_FREQ;
351 }
352 
353 
354 /**************************************************************************/
364 uint32_t SystemLFXOClockGet(void)
365 {
366  /* External crystal oscillator present? */
367 #if (EFM32_LFXO_FREQ > 0)
368  return SystemLFXOClock;
369 #else
370  return 0;
371 #endif
372 }
373 
374 
375 /**************************************************************************/
390 void SystemLFXOClockSet(uint32_t freq)
391 {
392  /* External crystal oscillator present? */
393 #if (EFM32_LFXO_FREQ > 0)
394  SystemLFXOClock = freq;
395 
396  /* Update core clock frequency if LFXO is used to clock core */
397  if (CMU->STATUS & CMU_STATUS_LFXOSEL)
398  {
399  /* The function will update the global variable */
401  }
402 #else
403  (void)freq; /* Unused parameter */
404 #endif
405 }
#define CMU_HFRCOCTRL_BAND_7MHZ
Definition: ezr32wg_cmu.h:317
#define CMU_HFRCOCTRL_BAND_14MHZ
Definition: ezr32wg_cmu.h:320
#define _CMU_CTRL_HFCLKDIV_SHIFT
Definition: ezr32wg_cmu.h:154
void SystemLFXOClockSet(uint32_t freq)
Set low frequency crystal oscillator clock frequency for target system.
uint32_t SystemCoreClockGet(void)
Get the current core clock frequency.
#define EFM32_LFRCO_FREQ
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
#define CMU_HFRCOCTRL_BAND_1MHZ
Definition: ezr32wg_cmu.h:316
uint32_t SystemHFClockGet(void)
Get the current HFCLK frequency.
#define DEVINFO
#define _CMU_CTRL_HFCLKDIV_MASK
Definition: ezr32wg_cmu.h:155
#define _CMU_HFCORECLKDIV_HFCORECLKDIV_SHIFT
Definition: ezr32wg_cmu.h:233
void SystemHFXOClockSet(uint32_t freq)
Set high frequency crystal oscillator clock frequency for target system.
uint32_t SystemLFRCOClockGet(void)
Get low frequency RC oscillator clock frequency for target system.
#define CMU_STATUS_LFXOSEL
Definition: ezr32wg_cmu.h:611
#define CMU_HFRCOCTRL_BAND_11MHZ
Definition: ezr32wg_cmu.h:318
#define _DEVINFO_PART_PROD_REV_SHIFT
uint32_t SystemHFXOClockGet(void)
Get high frequency crystal oscillator clock frequency for target system.
#define EFM32_LFXO_FREQ
void SystemInit(void)
Initialize the system.
#define CMU_STATUS_LFRCOSEL
Definition: ezr32wg_cmu.h:606
uint32_t SystemLFXOClockGet(void)
Get low frequency crystal oscillator clock frequency for target system.
#define _CMU_HFCORECLKDIV_HFCORECLKDIV_MASK
Definition: ezr32wg_cmu.h:234
#define CMU_HFRCOCTRL_BAND_21MHZ
Definition: ezr32wg_cmu.h:321
#define _DEVINFO_PART_PROD_REV_MASK
uint32_t SystemMaxCoreClockGet(void)
Get the maximum core clock frequency.
#define _CMU_HFRCOCTRL_BAND_MASK
Definition: ezr32wg_cmu.h:308
#define CMU_STATUS_HFXOSEL
Definition: ezr32wg_cmu.h:601
uint32_t SystemULFRCOClockGet(void)
Get ultra low frequency RC oscillator clock frequency for target system.
#define CMU_STATUS_HFRCOSEL
Definition: ezr32wg_cmu.h:596
#define CMU_HFRCOCTRL_BAND_28MHZ
Definition: ezr32wg_cmu.h:322
#define CMU
uint32_t SystemCoreClock
System System Clock Frequency (Core Clock).
__STATIC_INLINE uint8_t GetProdRev(void)