EZR32 Leopard Gecko Software Documentation  ezr32lg-doc-5.1.2
system_ezr32lg.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 }
310 
311 
312 /**************************************************************************/
322 uint32_t SystemLFRCOClockGet(void)
323 {
324  /* Currently we assume that this frequency is properly tuned during */
325  /* manufacturing and is not changed after reset. If future requirements */
326  /* for re-tuning by user, we can add support for that. */
327  return EFM32_LFRCO_FREQ;
328 }
329 
330 
331 /**************************************************************************/
341 uint32_t SystemULFRCOClockGet(void)
342 {
343  /* The ULFRCO frequency is not tuned, and can be very inaccurate */
344  return EFM32_ULFRCO_FREQ;
345 }
346 
347 
348 /**************************************************************************/
358 uint32_t SystemLFXOClockGet(void)
359 {
360  /* External crystal oscillator present? */
361 #if (EFM32_LFXO_FREQ > 0)
362  return SystemLFXOClock;
363 #else
364  return 0;
365 #endif
366 }
367 
368 
369 /**************************************************************************/
384 void SystemLFXOClockSet(uint32_t freq)
385 {
386  /* External crystal oscillator present? */
387 #if (EFM32_LFXO_FREQ > 0)
388  SystemLFXOClock = freq;
389 
390  /* Update core clock frequency if LFXO is used to clock core */
391  if (CMU->STATUS & CMU_STATUS_LFXOSEL)
392  {
393  /* The function will update the global variable */
395  }
396 #else
397  (void)freq; /* Unused parameter */
398 #endif
399 }
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
#define _CMU_HFCORECLKDIV_HFCORECLKDIV_MASK
Definition: ezr32lg_cmu.h:234
#define CMU
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
uint32_t SystemHFClockGet(void)
Get the current HFCLK frequency.
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_HFRCOCTRL_BAND_MASK
Definition: ezr32lg_cmu.h:308
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_CTRL_HFCLKDIV_MASK
Definition: ezr32lg_cmu.h:155
#define DEVINFO
#define _DEVINFO_PART_PROD_REV_SHIFT
#define CMU_STATUS_HFRCOSEL
Definition: ezr32lg_cmu.h:596
uint32_t SystemLFXOClockGet(void)
Get low frequency crystal oscillator clock frequency for target system.
#define _DEVINFO_PART_PROD_REV_MASK
#define CMU_HFRCOCTRL_BAND_11MHZ
Definition: ezr32lg_cmu.h:318
uint32_t SystemMaxCoreClockGet(void)
Get the maximum core clock frequency.
#define CMU_HFRCOCTRL_BAND_14MHZ
Definition: ezr32lg_cmu.h:320
#define CMU_STATUS_HFXOSEL
Definition: ezr32lg_cmu.h:601
#define _CMU_CTRL_HFCLKDIV_SHIFT
Definition: ezr32lg_cmu.h:154
#define CMU_STATUS_LFXOSEL
Definition: ezr32lg_cmu.h:611
uint32_t SystemULFRCOClockGet(void)
Get ultra low frequency RC oscillator clock frequency for target system.
#define CMU_HFRCOCTRL_BAND_28MHZ
Definition: ezr32lg_cmu.h:322
#define CMU_HFRCOCTRL_BAND_1MHZ
Definition: ezr32lg_cmu.h:316
#define CMU_HFRCOCTRL_BAND_21MHZ
Definition: ezr32lg_cmu.h:321
uint32_t SystemCoreClock
System System Clock Frequency (Core Clock).
#define CMU_STATUS_LFRCOSEL
Definition: ezr32lg_cmu.h:606
#define _CMU_HFCORECLKDIV_HFCORECLKDIV_SHIFT
Definition: ezr32lg_cmu.h:233
#define CMU_HFRCOCTRL_BAND_7MHZ
Definition: ezr32lg_cmu.h:317
__STATIC_INLINE uint8_t GetProdRev(void)