EFR32 Mighty Gecko 1 Software Documentation  efr32mg1-doc-5.1.2
system_efr32mg1p.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 EFR32_LFRCO_FREQ (32768UL)
42 #define EFR32_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 EFR_nFXO_FREQ */
52 /* values according to board design. By defining the EFR_nFXO_FREQ to 0, */
53 /* one indicates that the oscillator is not present, in order to save some */
54 /* SW footprint. */
55 
56 #ifndef EFR32_HFRCO_MAX_FREQ
57 #define EFR32_HFRCO_MAX_FREQ (38000000UL)
58 #endif
59 
60 #ifndef EFR32_HFXO_FREQ
61 #define EFR32_HFXO_FREQ (38400000UL)
62 #endif
63 
64 #ifndef EFR32_HFRCO_STARTUP_FREQ
65 #define EFR32_HFRCO_STARTUP_FREQ (19000000UL)
66 #endif
67 
68 
69 /* Do not define variable if HF crystal oscillator not present */
70 #if (EFR32_HFXO_FREQ > 0UL)
71 
73 static uint32_t SystemHFXOClock = EFR32_HFXO_FREQ;
75 #endif
76 
77 #ifndef EFR32_LFXO_FREQ
78 #define EFR32_LFXO_FREQ (EFR32_LFRCO_FREQ)
79 #endif
80 /* Do not define variable if LF crystal oscillator not present */
81 #if (EFR32_LFXO_FREQ > 0UL)
82 
84 static uint32_t SystemLFXOClock = 32768UL;
86 #endif
87 
88 
89 /*******************************************************************************
90  ************************** GLOBAL VARIABLES *******************************
91  ******************************************************************************/
92 
101 
102 
113 uint32_t SystemHfrcoFreq = EFR32_HFRCO_STARTUP_FREQ;
114 
115 
116 /*******************************************************************************
117  ************************** GLOBAL FUNCTIONS *******************************
118  ******************************************************************************/
119 
120 /***************************************************************************/
137 uint32_t SystemCoreClockGet(void)
138 {
139  uint32_t ret;
140  uint32_t presc;
141 
142  ret = SystemHFClockGet();
143  presc = (CMU->HFCOREPRESC & _CMU_HFCOREPRESC_PRESC_MASK) >>
145  ret /= (presc + 1);
146 
147  /* Keep CMSIS system clock variable up-to-date */
148  SystemCoreClock = ret;
149 
150  return ret;
151 }
152 
153 
154 /***************************************************************************/
164 uint32_t SystemMaxCoreClockGet(void)
165 {
166  return (EFR32_HFRCO_MAX_FREQ > EFR32_HFXO_FREQ ? \
167  EFR32_HFRCO_MAX_FREQ : EFR32_HFXO_FREQ);
168 }
169 
170 
171 /***************************************************************************/
181 uint32_t SystemHFClockGet(void)
182 {
183  uint32_t ret;
184 
185  switch (CMU->HFCLKSTATUS & _CMU_HFCLKSTATUS_SELECTED_MASK)
186  {
188 #if (EFR32_LFXO_FREQ > 0)
189  ret = SystemLFXOClock;
190 #else
191  /* We should not get here, since core should not be clocked. May */
192  /* be caused by a misconfiguration though. */
193  ret = 0;
194 #endif
195  break;
196 
198  ret = EFR32_LFRCO_FREQ;
199  break;
200 
202 #if (EFR32_HFXO_FREQ > 0)
203  ret = SystemHFXOClock;
204 #else
205  /* We should not get here, since core should not be clocked. May */
206  /* be caused by a misconfiguration though. */
207  ret = 0;
208 #endif
209  break;
210 
211  default: /* CMU_HFCLKSTATUS_SELECTED_HFRCO */
212  ret = SystemHfrcoFreq;
213  break;
214  }
215 
216  return ret / (1U + ((CMU->HFPRESC & _CMU_HFPRESC_PRESC_MASK)
218 }
219 
220 
221 /**************************************************************************/
231 uint32_t SystemHFXOClockGet(void)
232 {
233  /* External crystal oscillator present? */
234 #if (EFR32_HFXO_FREQ > 0)
235  return SystemHFXOClock;
236 #else
237  return 0;
238 #endif
239 }
240 
241 
242 /**************************************************************************/
257 void SystemHFXOClockSet(uint32_t freq)
258 {
259  /* External crystal oscillator present? */
260 #if (EFR32_HFXO_FREQ > 0)
261  SystemHFXOClock = freq;
262 
263  /* Update core clock frequency if HFXO is used to clock core */
265  {
266  /* The function will update the global variable */
268  }
269 #else
270  (void)freq; /* Unused parameter */
271 #endif
272 }
273 
274 
275 /**************************************************************************/
287 void SystemInit(void)
288 {
289 #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
290  /* Set floating point coprosessor access mode. */
291  SCB->CPACR |= ((3UL << 10*2) | /* set CP10 Full Access */
292  (3UL << 11*2) ); /* set CP11 Full Access */
293 #endif
294 }
295 
296 
297 /**************************************************************************/
307 uint32_t SystemLFRCOClockGet(void)
308 {
309  /* Currently we assume that this frequency is properly tuned during */
310  /* manufacturing and is not changed after reset. If future requirements */
311  /* for re-tuning by user, we can add support for that. */
312  return EFR32_LFRCO_FREQ;
313 }
314 
315 
316 /**************************************************************************/
326 uint32_t SystemULFRCOClockGet(void)
327 {
328  /* The ULFRCO frequency is not tuned, and can be very inaccurate */
329  return EFR32_ULFRCO_FREQ;
330 }
331 
332 
333 /**************************************************************************/
343 uint32_t SystemLFXOClockGet(void)
344 {
345  /* External crystal oscillator present? */
346 #if (EFR32_LFXO_FREQ > 0)
347  return SystemLFXOClock;
348 #else
349  return 0;
350 #endif
351 }
352 
353 
354 /**************************************************************************/
369 void SystemLFXOClockSet(uint32_t freq)
370 {
371  /* External crystal oscillator present? */
372 #if (EFR32_LFXO_FREQ > 0)
373  SystemLFXOClock = freq;
374 
375  /* Update core clock frequency if LFXO is used to clock core */
377  {
378  /* The function will update the global variable */
380  }
381 #else
382  (void)freq; /* Unused parameter */
383 #endif
384 }
void SystemLFXOClockSet(uint32_t freq)
Set low frequency crystal oscillator clock frequency for target system.
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
#define EFR32_LFRCO_FREQ
uint32_t SystemHFClockGet(void)
Get the current HFCLK frequency.
void SystemInit(void)
Initialize the system.
#define _CMU_HFCOREPRESC_PRESC_SHIFT
uint32_t SystemCoreClockGet(void)
Get the current core clock frequency.
#define _CMU_HFCLKSTATUS_SELECTED_MASK
uint32_t SystemHfrcoFreq
System HFRCO frequency.
#define _CMU_HFPRESC_PRESC_MASK
uint32_t SystemHFXOClockGet(void)
Get high frequency crystal oscillator clock frequency for target system.
uint32_t SystemLFXOClockGet(void)
Get low frequency crystal oscillator clock frequency for target system.
uint32_t SystemLFRCOClockGet(void)
Get low frequency RC oscillator clock frequency for target system.
#define _CMU_HFCOREPRESC_PRESC_MASK
#define CMU_HFCLKSTATUS_SELECTED_LFXO
#define _CMU_HFPRESC_PRESC_SHIFT
uint32_t SystemULFRCOClockGet(void)
Get ultra low frequency RC oscillator clock frequency for target system.
#define CMU_HFCLKSTATUS_SELECTED_HFXO
#define CMU
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.
#define CMU_HFCLKSTATUS_SELECTED_LFRCO
uint32_t SystemCoreClock
System System Clock Frequency (Core Clock).