Energy Micro IEC60355 Library Example Project 1.0 (internal use only!) GCC-Version
Example project demonstrating POST and BIST library functions

system_efm32.c

Go to the documentation of this file.
00001 /***************************************************************************/
00029 #include <stdint.h>
00030 #include "efm32.h"
00031 
00032 /*******************************************************************************
00033  ******************************   DEFINES   ************************************
00034  ******************************************************************************/
00035 
00037 #define EFM32_LFRCO_FREQ  (32768)
00038 
00039 /*******************************************************************************
00040  **************************   LOCAL VARIABLES   ********************************
00041  ******************************************************************************/
00042 
00043 /* System oscillator frequencies. These frequencies are normally constant */
00044 /* for a target, but they are made configurable in order to allow run-time */
00045 /* handling of different boards. The crystal oscillator clocks can be set */
00046 /* compile time to a non-default value by defining respective EFM32_nFXO_FREQ */
00047 /* values according to board design. By defining the EFM32_nFXO_FREQ to 0, */
00048 /* one indicates that the oscillator is not present, in order to save some */
00049 /* SW footprint. */
00050 
00051 #ifndef EFM32_HFXO_FREQ
00052 #define EFM32_HFXO_FREQ (32000000)
00053 #endif
00054 /* Do not define variable if HF crystal oscillator not present */
00055 #if (EFM32_HFXO_FREQ > 0)
00056 
00058 static uint32_t SystemHFXOClock = EFM32_HFXO_FREQ;
00060 #endif
00061 
00062 #ifndef EFM32_LFXO_FREQ 
00063 #define EFM32_LFXO_FREQ (EFM32_LFRCO_FREQ)
00064 #endif
00065 /* Do not define variable if LF crystal oscillator not present */
00066 #if (EFM32_LFXO_FREQ > 0)
00067 
00069 static uint32_t SystemLFXOClock = 32768;
00071 #endif
00072 
00073 
00074 /*******************************************************************************
00075  **************************   GLOBAL VARIABLES   *******************************
00076  ******************************************************************************/
00077 
00085 uint32_t SystemCoreClock;
00086 
00087 /*******************************************************************************
00088  **************************   GLOBAL FUNCTIONS   *******************************
00089  ******************************************************************************/
00090 
00091 /***************************************************************************/
00108 uint32_t SystemCoreClockGet(void)
00109 {
00110   uint32_t ret;
00111   
00112   ret = SystemHFClockGet();
00113   ret >>= (CMU->HFCORECLKDIV & _CMU_HFCORECLKDIV_HFCORECLKDIV_MASK) >> 
00114           _CMU_HFCORECLKDIV_HFCORECLKDIV_SHIFT;
00115 
00116   /* Keep CMSIS variable up-to-date just in case */
00117   SystemCoreClock = ret;
00118 
00119   return ret;
00120 }
00121 
00122 
00123 /***************************************************************************/
00133 uint32_t SystemHFClockGet(void)
00134 {
00135   uint32_t ret;
00136   
00137   switch (CMU->STATUS & (CMU_STATUS_HFRCOSEL | CMU_STATUS_HFXOSEL |
00138                          CMU_STATUS_LFRCOSEL | CMU_STATUS_LFXOSEL))
00139   {
00140     case CMU_STATUS_LFXOSEL:
00141 #if (EFM32_LFXO_FREQ > 0)
00142       ret = SystemLFXOClock;
00143 #else
00144       /* We should not get here, since core should not be clocked. May */
00145       /* be caused by a misconfiguration though. */
00146       ret = 0;
00147 #endif
00148       break;
00149       
00150     case CMU_STATUS_LFRCOSEL:
00151       ret = EFM32_LFRCO_FREQ;
00152       break;
00153       
00154     case CMU_STATUS_HFXOSEL:
00155 #if (EFM32_HFXO_FREQ > 0)
00156       ret = SystemHFXOClock;
00157 #else
00158       /* We should not get here, since core should not be clocked. May */
00159       /* be caused by a misconfiguration though. */
00160       ret = 0;
00161 #endif
00162       break;
00163       
00164     default: /* CMU_STATUS_HFRCOSEL */
00165       switch (CMU->HFRCOCTRL & _CMU_HFRCOCTRL_BAND_MASK)
00166       {
00167       case CMU_HFRCOCTRL_BAND_28MHZ:
00168         ret = 28000000;
00169         break;
00170 
00171       case CMU_HFRCOCTRL_BAND_21MHZ:
00172         ret = 21000000;
00173         break;
00174 
00175       case CMU_HFRCOCTRL_BAND_14MHZ:
00176         ret = 14000000;
00177         break;
00178 
00179       case CMU_HFRCOCTRL_BAND_11MHZ:
00180         ret = 11000000;
00181         break;
00182 
00183       case CMU_HFRCOCTRL_BAND_7MHZ:
00184         ret = 7000000;
00185         break;
00186 
00187       case CMU_HFRCOCTRL_BAND_1MHZ:
00188         ret = 1000000;
00189         break;
00190 
00191       default:
00192         ret = 0;
00193         break;
00194       }
00195       break;
00196   }
00197 
00198   return ret;
00199 }
00200 
00201 
00202 /**************************************************************************/
00212 uint32_t SystemHFXOClockGet(void)
00213 {
00214   /* External crystal oscillator present? */
00215 #if (EFM32_HFXO_FREQ > 0)
00216   return SystemHFXOClock;
00217 #else
00218   return 0;
00219 #endif
00220 }
00221 
00222 
00223 /**************************************************************************/
00238 void SystemHFXOClockSet(uint32_t freq)
00239 {
00240   /* External crystal oscillator present? */
00241 #if (EFM32_HFXO_FREQ > 0)
00242   SystemHFXOClock = freq;
00243 
00244   /* Update core clock frequency if HFXO is used to clock core */
00245   if (CMU->STATUS & CMU_STATUS_HFXOSEL)
00246   {
00247     /* The function will update the global variable */
00248     SystemCoreClockGet();
00249   }
00250 #else
00251   (void)freq; /* Unused parameter */
00252 #endif
00253 }
00254 
00255 
00256 /**************************************************************************/
00268 void SystemInit(void)
00269 {
00270 }
00271 
00272 
00273 /**************************************************************************/
00283 uint32_t SystemLFRCOClockGet(void)
00284 {
00285   /* Currently we assume that this frequency is properly tuned during */
00286   /* manufacturing and is not changed after reset. If future requirements */
00287   /* for re-tuning by user, we can add support for that. */
00288   return EFM32_LFRCO_FREQ;
00289 }
00290 
00291 
00292 /**************************************************************************/
00302 uint32_t SystemLFXOClockGet(void)
00303 {
00304   /* External crystal oscillator present? */
00305 #if (EFM32_LFXO_FREQ > 0)
00306   return SystemLFXOClock;
00307 #else
00308   return 0;
00309 #endif
00310 }
00311 
00312 
00313 /**************************************************************************/
00328 void SystemLFXOClockSet(uint32_t freq)
00329 {
00330   /* External crystal oscillator present? */
00331 #if (EFM32_LFXO_FREQ > 0)
00332   SystemLFXOClock = freq;
00333 
00334   /* Update core clock frequency if LFXO is used to clock core */
00335   if (CMU->STATUS & CMU_STATUS_LFXOSEL)
00336   {
00337     /* The function will update the global variable */
00338     SystemCoreClockGet();
00339   }
00340 #else
00341   (void)freq; /* Unused parameter */
00342 #endif
00343 }