EFM32 Leopard Gecko Software Documentation  efm32lg-doc-4.2.1
system_efm32lg.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  /* Leopard/Giant Gecko has an additional divider */
129  ret = ret / (1 + ((CMU->CTRL & _CMU_CTRL_HFCLKDIV_MASK)>>_CMU_CTRL_HFCLKDIV_SHIFT));
130  ret >>= (CMU->HFCORECLKDIV & _CMU_HFCORECLKDIV_HFCORECLKDIV_MASK) >>
132 
133  /* Keep CMSIS variable up-to-date just in case */
134  SystemCoreClock = ret;
135 
136  return ret;
137 }
138 
139 
140 /***************************************************************************/
150 uint32_t SystemMaxCoreClockGet(void)
151 {
152  return (EFM32_HFRCO_MAX_FREQ > EFM32_HFXO_FREQ ? \
153  EFM32_HFRCO_MAX_FREQ : EFM32_HFXO_FREQ);
154 }
155 
156 
157 /***************************************************************************/
167 uint32_t SystemHFClockGet(void)
168 {
169  uint32_t ret;
170 
171  switch (CMU->STATUS & (CMU_STATUS_HFRCOSEL | CMU_STATUS_HFXOSEL |
173  {
174  case CMU_STATUS_LFXOSEL:
175 #if (EFM32_LFXO_FREQ > 0)
176  ret = SystemLFXOClock;
177 #else
178  /* We should not get here, since core should not be clocked. May */
179  /* be caused by a misconfiguration though. */
180  ret = 0;
181 #endif
182  break;
183 
184  case CMU_STATUS_LFRCOSEL:
185  ret = EFM32_LFRCO_FREQ;
186  break;
187 
188  case CMU_STATUS_HFXOSEL:
189 #if (EFM32_HFXO_FREQ > 0)
190  ret = SystemHFXOClock;
191 #else
192  /* We should not get here, since core should not be clocked. May */
193  /* be caused by a misconfiguration though. */
194  ret = 0;
195 #endif
196  break;
197 
198  default: /* CMU_STATUS_HFRCOSEL */
199  switch (CMU->HFRCOCTRL & _CMU_HFRCOCTRL_BAND_MASK)
200  {
202  ret = 28000000;
203  break;
204 
206  ret = 21000000;
207  break;
208 
210  ret = 14000000;
211  break;
212 
214  ret = 11000000;
215  break;
216 
218  if ( GetProdRev() >= 19 )
219  ret = 6600000;
220  else
221  ret = 7000000;
222  break;
223 
225  if ( GetProdRev() >= 19 )
226  ret = 1200000;
227  else
228  ret = 1000000;
229  break;
230 
231  default:
232  ret = 0;
233  break;
234  }
235  break;
236  }
237 
238  return ret;
239 }
240 
241 
242 /**************************************************************************/
252 uint32_t SystemHFXOClockGet(void)
253 {
254  /* External crystal oscillator present? */
255 #if (EFM32_HFXO_FREQ > 0)
256  return SystemHFXOClock;
257 #else
258  return 0;
259 #endif
260 }
261 
262 
263 /**************************************************************************/
278 void SystemHFXOClockSet(uint32_t freq)
279 {
280  /* External crystal oscillator present? */
281 #if (EFM32_HFXO_FREQ > 0)
282  SystemHFXOClock = freq;
283 
284  /* Update core clock frequency if HFXO is used to clock core */
285  if (CMU->STATUS & CMU_STATUS_HFXOSEL)
286  {
287  /* The function will update the global variable */
289  }
290 #else
291  (void)freq; /* Unused parameter */
292 #endif
293 }
294 
295 
296 /**************************************************************************/
308 void SystemInit(void)
309 {
310 }
311 
312 
313 /**************************************************************************/
323 uint32_t SystemLFRCOClockGet(void)
324 {
325  /* Currently we assume that this frequency is properly tuned during */
326  /* manufacturing and is not changed after reset. If future requirements */
327  /* for re-tuning by user, we can add support for that. */
328  return EFM32_LFRCO_FREQ;
329 }
330 
331 
332 /**************************************************************************/
342 uint32_t SystemULFRCOClockGet(void)
343 {
344  /* The ULFRCO frequency is not tuned, and can be very inaccurate */
345  return EFM32_ULFRCO_FREQ;
346 }
347 
348 
349 /**************************************************************************/
359 uint32_t SystemLFXOClockGet(void)
360 {
361  /* External crystal oscillator present? */
362 #if (EFM32_LFXO_FREQ > 0)
363  return SystemLFXOClock;
364 #else
365  return 0;
366 #endif
367 }
368 
369 
370 /**************************************************************************/
385 void SystemLFXOClockSet(uint32_t freq)
386 {
387  /* External crystal oscillator present? */
388 #if (EFM32_LFXO_FREQ > 0)
389  SystemLFXOClock = freq;
390 
391  /* Update core clock frequency if LFXO is used to clock core */
392  if (CMU->STATUS & CMU_STATUS_LFXOSEL)
393  {
394  /* The function will update the global variable */
396  }
397 #else
398  (void)freq; /* Unused parameter */
399 #endif
400 }
uint32_t SystemCoreClockGet(void)
Get the current core clock frequency.
void SystemLFXOClockSet(uint32_t freq)
Set low frequency crystal oscillator clock frequency for target system.
#define _CMU_HFCORECLKDIV_HFCORECLKDIV_MASK
Definition: efm32lg_cmu.h:233
#define CMU
#define CMU_STATUS_HFRCOSEL
Definition: efm32lg_cmu.h:595
#define EFM32_LFXO_FREQ
#define CMU_HFRCOCTRL_BAND_14MHZ
Definition: efm32lg_cmu.h:319
__STATIC_INLINE uint8_t GetProdRev(void)
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
#define CMU_STATUS_LFXOSEL
Definition: efm32lg_cmu.h:610
#define CMU_HFRCOCTRL_BAND_28MHZ
Definition: efm32lg_cmu.h:321
#define _CMU_HFCORECLKDIV_HFCORECLKDIV_SHIFT
Definition: efm32lg_cmu.h:232
#define CMU_HFRCOCTRL_BAND_21MHZ
Definition: efm32lg_cmu.h:320
#define CMU_HFRCOCTRL_BAND_11MHZ
Definition: efm32lg_cmu.h:317
#define _DEVINFO_PART_PROD_REV_MASK
#define _CMU_HFRCOCTRL_BAND_MASK
Definition: efm32lg_cmu.h:307
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.
uint32_t SystemHFClockGet(void)
Get the current HFCLK frequency.
#define _DEVINFO_PART_PROD_REV_SHIFT
#define _CMU_CTRL_HFCLKDIV_SHIFT
Definition: efm32lg_cmu.h:153
void SystemHFXOClockSet(uint32_t freq)
Set high frequency crystal oscillator clock frequency for target system.
#define CMU_HFRCOCTRL_BAND_7MHZ
Definition: efm32lg_cmu.h:316
#define _CMU_CTRL_HFCLKDIV_MASK
Definition: efm32lg_cmu.h:154
#define EFM32_LFRCO_FREQ
uint32_t SystemMaxCoreClockGet(void)
Get the maximum core clock frequency.
uint32_t SystemULFRCOClockGet(void)
Get ultra low frequency RC oscillator clock frequency for target system.
#define CMU_HFRCOCTRL_BAND_1MHZ
Definition: efm32lg_cmu.h:315
uint32_t SystemLFRCOClockGet(void)
Get low frequency RC oscillator clock frequency for target system.
#define CMU_STATUS_HFXOSEL
Definition: efm32lg_cmu.h:600
uint32_t SystemCoreClock
System System Clock Frequency (Core Clock).
void SystemInit(void)
Initialize the system.
#define CMU_STATUS_LFRCOSEL
Definition: efm32lg_cmu.h:605
#define DEVINFO