EFM32 Zero Gecko Software Documentation  efm32zg-doc-5.1.2
system_efm32zg.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 (24000000UL)
58 #endif
59 
60 #define EFM32_HFRCO_MAX_FREQ (21000000UL)
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 /*******************************************************************************
83  ************************** GLOBAL VARIABLES *******************************
84  ******************************************************************************/
85 
93 uint32_t SystemCoreClock;
94 
95 /*******************************************************************************
96  ************************** GLOBAL FUNCTIONS *******************************
97  ******************************************************************************/
98 
99 /***************************************************************************/
116 uint32_t SystemCoreClockGet(void)
117 {
118  uint32_t ret;
119 
120  ret = SystemHFClockGet();
121  ret >>= (CMU->HFCORECLKDIV & _CMU_HFCORECLKDIV_HFCORECLKDIV_MASK) >>
123 
124  /* Keep CMSIS variable up-to-date just in case */
125  SystemCoreClock = ret;
126 
127  return ret;
128 }
129 
130 
131 /***************************************************************************/
141 uint32_t SystemMaxCoreClockGet(void)
142 {
143  return (EFM32_HFRCO_MAX_FREQ > EFM32_HFXO_FREQ ? \
144  EFM32_HFRCO_MAX_FREQ : EFM32_HFXO_FREQ);
145 }
146 
147 
148 /***************************************************************************/
158 uint32_t SystemHFClockGet(void)
159 {
160  uint32_t ret;
161 
162  switch (CMU->STATUS & (CMU_STATUS_HFRCOSEL | CMU_STATUS_HFXOSEL |
164  {
165  case CMU_STATUS_LFXOSEL:
166 #if (EFM32_LFXO_FREQ > 0)
167  ret = SystemLFXOClock;
168 #else
169  /* We should not get here, since core should not be clocked. May */
170  /* be caused by a misconfiguration though. */
171  ret = 0;
172 #endif
173  break;
174 
175  case CMU_STATUS_LFRCOSEL:
176  ret = EFM32_LFRCO_FREQ;
177  break;
178 
179  case CMU_STATUS_HFXOSEL:
180 #if (EFM32_HFXO_FREQ > 0)
181  ret = SystemHFXOClock;
182 #else
183  /* We should not get here, since core should not be clocked. May */
184  /* be caused by a misconfiguration though. */
185  ret = 0;
186 #endif
187  break;
188 
189  default: /* CMU_STATUS_HFRCOSEL */
190  switch (CMU->HFRCOCTRL & _CMU_HFRCOCTRL_BAND_MASK)
191  {
193  ret = 21000000;
194  break;
195 
197  ret = 14000000;
198  break;
199 
201  ret = 11000000;
202  break;
203 
205  ret = 6600000;
206  break;
207 
209  ret = 1200000;
210  break;
211 
212  default:
213  ret = 0;
214  break;
215  }
216  break;
217  }
218 
219  return ret;
220 }
221 
222 
223 /**************************************************************************/
233 uint32_t SystemHFXOClockGet(void)
234 {
235  /* External crystal oscillator present? */
236 #if (EFM32_HFXO_FREQ > 0)
237  return SystemHFXOClock;
238 #else
239  return 0;
240 #endif
241 }
242 
243 
244 /**************************************************************************/
259 void SystemHFXOClockSet(uint32_t freq)
260 {
261  /* External crystal oscillator present? */
262 #if (EFM32_HFXO_FREQ > 0)
263  SystemHFXOClock = freq;
264 
265  /* Update core clock frequency if HFXO is used to clock core */
266  if (CMU->STATUS & CMU_STATUS_HFXOSEL)
267  {
268  /* The function will update the global variable */
270  }
271 #else
272  (void)freq; /* Unused parameter */
273 #endif
274 }
275 
276 
277 /**************************************************************************/
289 void SystemInit(void)
290 {
291 }
292 
293 
294 /**************************************************************************/
304 uint32_t SystemLFRCOClockGet(void)
305 {
306  /* Currently we assume that this frequency is properly tuned during */
307  /* manufacturing and is not changed after reset. If future requirements */
308  /* for re-tuning by user, we can add support for that. */
309  return EFM32_LFRCO_FREQ;
310 }
311 
312 
313 /**************************************************************************/
323 uint32_t SystemULFRCOClockGet(void)
324 {
325  /* The ULFRCO frequency is not tuned, and can be very inaccurate */
326  return EFM32_ULFRCO_FREQ;
327 }
328 
329 
330 /**************************************************************************/
340 uint32_t SystemLFXOClockGet(void)
341 {
342  /* External crystal oscillator present? */
343 #if (EFM32_LFXO_FREQ > 0)
344  return SystemLFXOClock;
345 #else
346  return 0;
347 #endif
348 }
349 
350 
351 /**************************************************************************/
366 void SystemLFXOClockSet(uint32_t freq)
367 {
368  /* External crystal oscillator present? */
369 #if (EFM32_LFXO_FREQ > 0)
370  SystemLFXOClock = freq;
371 
372  /* Update core clock frequency if LFXO is used to clock core */
373  if (CMU->STATUS & CMU_STATUS_LFXOSEL)
374  {
375  /* The function will update the global variable */
377  }
378 #else
379  (void)freq; /* Unused parameter */
380 #endif
381 }
#define CMU_HFRCOCTRL_BAND_11MHZ
Definition: efm32zg_cmu.h:295
#define EFM32_LFXO_FREQ
void SystemLFXOClockSet(uint32_t freq)
Set low frequency crystal oscillator clock frequency for target system.
#define CMU_STATUS_LFRCOSEL
Definition: efm32zg_cmu.h:570
#define _CMU_HFCORECLKDIV_HFCORECLKDIV_MASK
Definition: efm32zg_cmu.h:212
#define CMU_STATUS_LFXOSEL
Definition: efm32zg_cmu.h:575
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
#define CMU_STATUS_HFRCOSEL
Definition: efm32zg_cmu.h:560
#define _CMU_HFRCOCTRL_BAND_MASK
Definition: efm32zg_cmu.h:286
#define CMU
#define CMU_HFRCOCTRL_BAND_21MHZ
Definition: efm32zg_cmu.h:298
uint32_t SystemHFClockGet(void)
Get the current HFCLK frequency.
#define CMU_HFRCOCTRL_BAND_14MHZ
Definition: efm32zg_cmu.h:297
uint32_t SystemLFXOClockGet(void)
Get low frequency crystal oscillator clock frequency for target system.
uint32_t SystemHFXOClockGet(void)
Get high frequency crystal oscillator clock frequency for target system.
#define CMU_HFRCOCTRL_BAND_7MHZ
Definition: efm32zg_cmu.h:294
#define _CMU_HFCORECLKDIV_HFCORECLKDIV_SHIFT
Definition: efm32zg_cmu.h:211
#define EFM32_LFRCO_FREQ
uint32_t SystemULFRCOClockGet(void)
Get ultra low frequency RC oscillator clock frequency for target system.
#define CMU_STATUS_HFXOSEL
Definition: efm32zg_cmu.h:565
uint32_t SystemLFRCOClockGet(void)
Get low frequency RC oscillator clock frequency for target system.
uint32_t SystemMaxCoreClockGet(void)
Get the maximum core clock frequency.
void SystemHFXOClockSet(uint32_t freq)
Set high frequency crystal oscillator clock frequency for target system.
void SystemInit(void)
Initialize the system.
uint32_t SystemCoreClock
System System Clock Frequency (Core Clock).
uint32_t SystemCoreClockGet(void)
Get the current core clock frequency.
#define CMU_HFRCOCTRL_BAND_1MHZ
Definition: efm32zg_cmu.h:293