EZR32 Leopard Gecko Software Documentation  ezr32lg-doc-4.2.1
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 #if defined (_EFM32_GIANT_FAMILY)
129  /* Leopard/Giant 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 }
313 
314 
315 /**************************************************************************/
325 uint32_t SystemLFRCOClockGet(void)
326 {
327  /* Currently we assume that this frequency is properly tuned during */
328  /* manufacturing and is not changed after reset. If future requirements */
329  /* for re-tuning by user, we can add support for that. */
330  return EFM32_LFRCO_FREQ;
331 }
332 
333 
334 /**************************************************************************/
344 uint32_t SystemULFRCOClockGet(void)
345 {
346  /* The ULFRCO frequency is not tuned, and can be very inaccurate */
347  return EFM32_ULFRCO_FREQ;
348 }
349 
350 
351 /**************************************************************************/
361 uint32_t SystemLFXOClockGet(void)
362 {
363  /* External crystal oscillator present? */
364 #if (EFM32_LFXO_FREQ > 0)
365  return SystemLFXOClock;
366 #else
367  return 0;
368 #endif
369 }
370 
371 
372 /**************************************************************************/
387 void SystemLFXOClockSet(uint32_t freq)
388 {
389  /* External crystal oscillator present? */
390 #if (EFM32_LFXO_FREQ > 0)
391  SystemLFXOClock = freq;
392 
393  /* Update core clock frequency if LFXO is used to clock core */
394  if (CMU->STATUS & CMU_STATUS_LFXOSEL)
395  {
396  /* The function will update the global variable */
398  }
399 #else
400  (void)freq; /* Unused parameter */
401 #endif
402 }
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)