EFM32 Tiny Gecko Software Documentation  efm32tg-doc-5.1.2
system_efm32tg.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 (32000000UL)
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;
237 }
238 
239 
240 /**************************************************************************/
250 uint32_t SystemHFXOClockGet(void)
251 {
252  /* External crystal oscillator present? */
253 #if (EFM32_HFXO_FREQ > 0)
254  return SystemHFXOClock;
255 #else
256  return 0;
257 #endif
258 }
259 
260 
261 /**************************************************************************/
276 void SystemHFXOClockSet(uint32_t freq)
277 {
278  /* External crystal oscillator present? */
279 #if (EFM32_HFXO_FREQ > 0)
280  SystemHFXOClock = freq;
281 
282  /* Update core clock frequency if HFXO is used to clock core */
283  if (CMU->STATUS & CMU_STATUS_HFXOSEL)
284  {
285  /* The function will update the global variable */
287  }
288 #else
289  (void)freq; /* Unused parameter */
290 #endif
291 }
292 
293 
294 /**************************************************************************/
306 void SystemInit(void)
307 {
308 }
309 
310 
311 /**************************************************************************/
321 uint32_t SystemLFRCOClockGet(void)
322 {
323  /* Currently we assume that this frequency is properly tuned during */
324  /* manufacturing and is not changed after reset. If future requirements */
325  /* for re-tuning by user, we can add support for that. */
326  return EFM32_LFRCO_FREQ;
327 }
328 
329 
330 /**************************************************************************/
340 uint32_t SystemULFRCOClockGet(void)
341 {
342  /* The ULFRCO frequency is not tuned, and can be very inaccurate */
343  return EFM32_ULFRCO_FREQ;
344 }
345 
346 
347 /**************************************************************************/
357 uint32_t SystemLFXOClockGet(void)
358 {
359  /* External crystal oscillator present? */
360 #if (EFM32_LFXO_FREQ > 0)
361  return SystemLFXOClock;
362 #else
363  return 0;
364 #endif
365 }
366 
367 
368 /**************************************************************************/
383 void SystemLFXOClockSet(uint32_t freq)
384 {
385  /* External crystal oscillator present? */
386 #if (EFM32_LFXO_FREQ > 0)
387  SystemLFXOClock = freq;
388 
389  /* Update core clock frequency if LFXO is used to clock core */
390  if (CMU->STATUS & CMU_STATUS_LFXOSEL)
391  {
392  /* The function will update the global variable */
394  }
395 #else
396  (void)freq; /* Unused parameter */
397 #endif
398 }
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_HFRCOCTRL_BAND_1MHZ
Definition: efm32tg_cmu.h:292
#define EFM32_LFXO_FREQ
__STATIC_INLINE uint8_t GetProdRev(void)
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
#define CMU_STATUS_LFRCOSEL
Definition: efm32tg_cmu.h:572
#define CMU_HFRCOCTRL_BAND_7MHZ
Definition: efm32tg_cmu.h:293
#define CMU_HFRCOCTRL_BAND_28MHZ
Definition: efm32tg_cmu.h:298
#define DEVINFO
#define CMU_STATUS_LFXOSEL
Definition: efm32tg_cmu.h:577
#define _CMU_HFCORECLKDIV_HFCORECLKDIV_MASK
Definition: efm32tg_cmu.h:219
uint32_t SystemLFXOClockGet(void)
Get low frequency crystal oscillator clock frequency for target system.
#define _DEVINFO_PART_PROD_REV_MASK
#define _DEVINFO_PART_PROD_REV_SHIFT
uint32_t SystemHFXOClockGet(void)
Get high frequency crystal oscillator clock frequency for target system.
uint32_t SystemHFClockGet(void)
Get the current HFCLK frequency.
#define CMU_STATUS_HFRCOSEL
Definition: efm32tg_cmu.h:562
#define CMU_HFRCOCTRL_BAND_14MHZ
Definition: efm32tg_cmu.h:296
void SystemHFXOClockSet(uint32_t freq)
Set high frequency crystal oscillator clock frequency for target system.
#define EFM32_LFRCO_FREQ
#define CMU
uint32_t SystemULFRCOClockGet(void)
Get ultra low frequency RC oscillator clock frequency for target system.
uint32_t SystemMaxCoreClockGet(void)
Get the maximum core clock frequency.
uint32_t SystemLFRCOClockGet(void)
Get low frequency RC oscillator clock frequency for target system.
#define CMU_HFRCOCTRL_BAND_11MHZ
Definition: efm32tg_cmu.h:294
uint32_t SystemCoreClock
System System Clock Frequency (Core Clock).
void SystemInit(void)
Initialize the system.
#define _CMU_HFCORECLKDIV_HFCORECLKDIV_SHIFT
Definition: efm32tg_cmu.h:218
#define CMU_HFRCOCTRL_BAND_21MHZ
Definition: efm32tg_cmu.h:297
#define _CMU_HFRCOCTRL_BAND_MASK
Definition: efm32tg_cmu.h:284
#define CMU_STATUS_HFXOSEL
Definition: efm32tg_cmu.h:567