emlib/src/em_cmu.c
void CMU_HFXOInit(const CMU_HFXOInit_TypeDef *hfxoInit)
{
  /* Do not disable HFXO if it is currently selected as HF/Core clock */
  EFM_ASSERT(CMU_ClockSelectGet(cmuClock_HF) != cmuSelect_HFXO);

  /* HFXO must be disabled before reconfiguration */
  CMU_OscillatorEnable(cmuOsc_HFXO, false, true);

#if defined(_SILICON_LABS_32B_SERIES_1) && (_SILICON_LABS_GECKO_INTERNAL_SDID >= 100)
  uint32_t tmp = CMU_HFXOCTRL_MODE_XTAL;

  switch (hfxoInit->mode) {
    case cmuOscMode_Crystal:
      tmp = CMU_HFXOCTRL_MODE_XTAL;
      break;
    case cmuOscMode_External:
      tmp = CMU_HFXOCTRL_MODE_DIGEXTCLK;
      break;
    case cmuOscMode_AcCoupled:
      tmp = CMU_HFXOCTRL_MODE_ACBUFEXTCLK;
      break;
    default:
      EFM_ASSERT(false); /* Unsupported configuration */
  }

  /* HFXO Doubler can only be enabled on crystals up to max 25 MHz */
  if (SystemHFXOClockGet() <= 25000000) {
    tmp |= CMU_HFXOCTRL_HFXOX2EN;
  }

  CMU->HFXOCTRL = (CMU->HFXOCTRL & ~(_CMU_HFXOCTRL_MODE_MASK
                                     | _CMU_HFXOCTRL_HFXOX2EN_MASK))
                  | tmp;

  /* Set tuning for startup and steady state */
  CMU->HFXOSTARTUPCTRL = (hfxoInit->ctuneStartup << _CMU_HFXOSTARTUPCTRL_CTUNE_SHIFT)
                         | (hfxoInit->xoCoreBiasTrimStartup << _CMU_HFXOSTARTUPCTRL_IBTRIMXOCORE_SHIFT);

  CMU->HFXOSTEADYSTATECTRL = (CMU->HFXOSTEADYSTATECTRL & ~(_CMU_HFXOSTEADYSTATECTRL_CTUNE_MASK
                                                           | _CMU_HFXOSTEADYSTATECTRL_IBTRIMXOCORE_MASK))
                             | (hfxoInit->ctuneSteadyState << _CMU_HFXOSTEADYSTATECTRL_CTUNE_SHIFT)
                             | (hfxoInit->xoCoreBiasTrimSteadyState << _CMU_HFXOSTEADYSTATECTRL_IBTRIMXOCORE_SHIFT);

  /* Set timeouts */
  CMU->HFXOTIMEOUTCTRL = (hfxoInit->timeoutPeakDetect << _CMU_HFXOTIMEOUTCTRL_PEAKDETTIMEOUT_SHIFT)
                         | (hfxoInit->timeoutSteady << _CMU_HFXOTIMEOUTCTRL_STEADYTIMEOUT_SHIFT)
                         | (hfxoInit->timeoutStartup << _CMU_HFXOTIMEOUTCTRL_STARTUPTIMEOUT_SHIFT);

#elif defined(_CMU_HFXOCTRL_MASK)
  /* Verify that the deprecated autostart fields are not used,
   * @ref CMU_HFXOAutostartEnable must be used instead. */
  EFM_ASSERT(!(hfxoInit->autoStartEm01
               || hfxoInit->autoSelEm01
               || hfxoInit->autoStartSelOnRacWakeup));

  uint32_t tmp = CMU_HFXOCTRL_MODE_XTAL;

  /* AC coupled external clock not supported */
  EFM_ASSERT(hfxoInit->mode != cmuOscMode_AcCoupled);
  if (hfxoInit->mode == cmuOscMode_External) {
    tmp = CMU_HFXOCTRL_MODE_DIGEXTCLK;
  }

  /* Apply control settings */
  CMU->HFXOCTRL = (CMU->HFXOCTRL & ~_CMU_HFXOCTRL_MODE_MASK)
                  | tmp;
  BUS_RegBitWrite(&CMU->HFXOCTRL, _CMU_HFXOCTRL_LOWPOWER_SHIFT, hfxoInit->lowPowerMode);

  /* Set XTAL tuning parameters */

#if defined(_CMU_HFXOCTRL1_PEAKDETTHR_MASK)
  /* Set peak detection threshold */
  CMU->HFXOCTRL1 = (CMU->HFXOCTRL1 & ~_CMU_HFXOCTRL1_PEAKDETTHR_MASK)
                   | (hfxoInit->thresholdPeakDetect << _CMU_HFXOCTRL1_PEAKDETTHR_SHIFT);
#endif
  /* Set tuning for startup and steady state */
  CMU->HFXOSTARTUPCTRL = (hfxoInit->ctuneStartup << _CMU_HFXOSTARTUPCTRL_CTUNE_SHIFT)
                         | (hfxoInit->xoCoreBiasTrimStartup << _CMU_HFXOSTARTUPCTRL_IBTRIMXOCORE_SHIFT);

  CMU->HFXOSTEADYSTATECTRL = (CMU->HFXOSTEADYSTATECTRL & ~(_CMU_HFXOSTEADYSTATECTRL_CTUNE_MASK
                                                           | _CMU_HFXOSTEADYSTATECTRL_IBTRIMXOCORE_MASK
                                                           | _CMU_HFXOSTEADYSTATECTRL_REGISH_MASK
                                                           | _CMU_HFXOSTEADYSTATECTRL_REGISHUPPER_MASK))
                             | (hfxoInit->ctuneSteadyState << _CMU_HFXOSTEADYSTATECTRL_CTUNE_SHIFT)
                             | (hfxoInit->xoCoreBiasTrimSteadyState << _CMU_HFXOSTEADYSTATECTRL_IBTRIMXOCORE_SHIFT)
                             | (hfxoInit->regIshSteadyState << _CMU_HFXOSTEADYSTATECTRL_REGISH_SHIFT)
                             | getRegIshUpperVal(hfxoInit->regIshSteadyState);

  /* Set timeouts */
  CMU->HFXOTIMEOUTCTRL = (hfxoInit->timeoutPeakDetect << _CMU_HFXOTIMEOUTCTRL_PEAKDETTIMEOUT_SHIFT)
                         | (hfxoInit->timeoutSteady << _CMU_HFXOTIMEOUTCTRL_STEADYTIMEOUT_SHIFT)
                         | (hfxoInit->timeoutStartup << _CMU_HFXOTIMEOUTCTRL_STARTUPTIMEOUT_SHIFT)
                         | (hfxoInit->timeoutShuntOptimization << _CMU_HFXOTIMEOUTCTRL_SHUNTOPTTIMEOUT_SHIFT);

#else
  CMU->CTRL = (CMU->CTRL & ~(_CMU_CTRL_HFXOTIMEOUT_MASK
                             | _CMU_CTRL_HFXOBOOST_MASK
                             | _CMU_CTRL_HFXOMODE_MASK
                             | _CMU_CTRL_HFXOGLITCHDETEN_MASK))
              | (hfxoInit->timeout << _CMU_CTRL_HFXOTIMEOUT_SHIFT)
              | (hfxoInit->boost << _CMU_CTRL_HFXOBOOST_SHIFT)
              | (hfxoInit->mode << _CMU_CTRL_HFXOMODE_SHIFT)
              | (hfxoInit->glitchDetector ? CMU_CTRL_HFXOGLITCHDETEN : 0);
#endif
}
/**************************************************************************/
/**
 * @brief
 *   Set HFXO control registers
 *
 * @note
 *   HFXO configuration should be obtained from a configuration tool,
 *   app note or xtal datasheet. This function disables the HFXO to ensure
 *   a valid state before update.
 *
 * @param[in] hfxoInit
 *    HFXO setup parameters
 *****************************************************************************/
@brief
Set HFXO control registers

@note
HFXO configuration should be obtained from a configuration tool,
app note or xtal datasheet. This function disables the HFXO to ensure
a valid state before update.

@param[in] hfxoInit
HFXO setup parameters
@brief Set HFXO control registers @note HFXO configuration should be obtained from a configuration tool app note or xtal datasheet . This function disables the HFXO to ensure a valid state before update . @param [ in ] hfxoInit HFXO setup parameters
void CMU_HFXOInit(const CMU_HFXOInit_TypeDef *hfxoInit)
{
  EFM_ASSERT(CMU_ClockSelectGet(cmuClock_HF) != cmuSelect_HFXO);
  CMU_OscillatorEnable(cmuOsc_HFXO, false, true);
#if defined(_SILICON_LABS_32B_SERIES_1) && (_SILICON_LABS_GECKO_INTERNAL_SDID >= 100)
  uint32_t tmp = CMU_HFXOCTRL_MODE_XTAL;
  switch (hfxoInit->mode) {
    case cmuOscMode_Crystal:
      tmp = CMU_HFXOCTRL_MODE_XTAL;
      break;
    case cmuOscMode_External:
      tmp = CMU_HFXOCTRL_MODE_DIGEXTCLK;
      break;
    case cmuOscMode_AcCoupled:
      tmp = CMU_HFXOCTRL_MODE_ACBUFEXTCLK;
      break;
    default:
      EFM_ASSERT(false); 
  }
  if (SystemHFXOClockGet() <= 25000000) {
    tmp |= CMU_HFXOCTRL_HFXOX2EN;
  }
  CMU->HFXOCTRL = (CMU->HFXOCTRL & ~(_CMU_HFXOCTRL_MODE_MASK
                                     | _CMU_HFXOCTRL_HFXOX2EN_MASK))
                  | tmp;
  CMU->HFXOSTARTUPCTRL = (hfxoInit->ctuneStartup << _CMU_HFXOSTARTUPCTRL_CTUNE_SHIFT)
                         | (hfxoInit->xoCoreBiasTrimStartup << _CMU_HFXOSTARTUPCTRL_IBTRIMXOCORE_SHIFT);
  CMU->HFXOSTEADYSTATECTRL = (CMU->HFXOSTEADYSTATECTRL & ~(_CMU_HFXOSTEADYSTATECTRL_CTUNE_MASK
                                                           | _CMU_HFXOSTEADYSTATECTRL_IBTRIMXOCORE_MASK))
                             | (hfxoInit->ctuneSteadyState << _CMU_HFXOSTEADYSTATECTRL_CTUNE_SHIFT)
                             | (hfxoInit->xoCoreBiasTrimSteadyState << _CMU_HFXOSTEADYSTATECTRL_IBTRIMXOCORE_SHIFT);
  CMU->HFXOTIMEOUTCTRL = (hfxoInit->timeoutPeakDetect << _CMU_HFXOTIMEOUTCTRL_PEAKDETTIMEOUT_SHIFT)
                         | (hfxoInit->timeoutSteady << _CMU_HFXOTIMEOUTCTRL_STEADYTIMEOUT_SHIFT)
                         | (hfxoInit->timeoutStartup << _CMU_HFXOTIMEOUTCTRL_STARTUPTIMEOUT_SHIFT);
#elif defined(_CMU_HFXOCTRL_MASK)
  EFM_ASSERT(!(hfxoInit->autoStartEm01
               || hfxoInit->autoSelEm01
               || hfxoInit->autoStartSelOnRacWakeup));
  uint32_t tmp = CMU_HFXOCTRL_MODE_XTAL;
  EFM_ASSERT(hfxoInit->mode != cmuOscMode_AcCoupled);
  if (hfxoInit->mode == cmuOscMode_External) {
    tmp = CMU_HFXOCTRL_MODE_DIGEXTCLK;
  }
  CMU->HFXOCTRL = (CMU->HFXOCTRL & ~_CMU_HFXOCTRL_MODE_MASK)
                  | tmp;
  BUS_RegBitWrite(&CMU->HFXOCTRL, _CMU_HFXOCTRL_LOWPOWER_SHIFT, hfxoInit->lowPowerMode);
#if defined(_CMU_HFXOCTRL1_PEAKDETTHR_MASK)
  CMU->HFXOCTRL1 = (CMU->HFXOCTRL1 & ~_CMU_HFXOCTRL1_PEAKDETTHR_MASK)
                   | (hfxoInit->thresholdPeakDetect << _CMU_HFXOCTRL1_PEAKDETTHR_SHIFT);
#endif
  CMU->HFXOSTARTUPCTRL = (hfxoInit->ctuneStartup << _CMU_HFXOSTARTUPCTRL_CTUNE_SHIFT)
                         | (hfxoInit->xoCoreBiasTrimStartup << _CMU_HFXOSTARTUPCTRL_IBTRIMXOCORE_SHIFT);
  CMU->HFXOSTEADYSTATECTRL = (CMU->HFXOSTEADYSTATECTRL & ~(_CMU_HFXOSTEADYSTATECTRL_CTUNE_MASK
                                                           | _CMU_HFXOSTEADYSTATECTRL_IBTRIMXOCORE_MASK
                                                           | _CMU_HFXOSTEADYSTATECTRL_REGISH_MASK
                                                           | _CMU_HFXOSTEADYSTATECTRL_REGISHUPPER_MASK))
                             | (hfxoInit->ctuneSteadyState << _CMU_HFXOSTEADYSTATECTRL_CTUNE_SHIFT)
                             | (hfxoInit->xoCoreBiasTrimSteadyState << _CMU_HFXOSTEADYSTATECTRL_IBTRIMXOCORE_SHIFT)
                             | (hfxoInit->regIshSteadyState << _CMU_HFXOSTEADYSTATECTRL_REGISH_SHIFT)
                             | getRegIshUpperVal(hfxoInit->regIshSteadyState);
  CMU->HFXOTIMEOUTCTRL = (hfxoInit->timeoutPeakDetect << _CMU_HFXOTIMEOUTCTRL_PEAKDETTIMEOUT_SHIFT)
                         | (hfxoInit->timeoutSteady << _CMU_HFXOTIMEOUTCTRL_STEADYTIMEOUT_SHIFT)
                         | (hfxoInit->timeoutStartup << _CMU_HFXOTIMEOUTCTRL_STARTUPTIMEOUT_SHIFT)
                         | (hfxoInit->timeoutShuntOptimization << _CMU_HFXOTIMEOUTCTRL_SHUNTOPTTIMEOUT_SHIFT);
#else
  CMU->CTRL = (CMU->CTRL & ~(_CMU_CTRL_HFXOTIMEOUT_MASK
                             | _CMU_CTRL_HFXOBOOST_MASK
                             | _CMU_CTRL_HFXOMODE_MASK
                             | _CMU_CTRL_HFXOGLITCHDETEN_MASK))
              | (hfxoInit->timeout << _CMU_CTRL_HFXOTIMEOUT_SHIFT)
              | (hfxoInit->boost << _CMU_CTRL_HFXOBOOST_SHIFT)
              | (hfxoInit->mode << _CMU_CTRL_HFXOMODE_SHIFT)
              | (hfxoInit->glitchDetector ? CMU_CTRL_HFXOGLITCHDETEN : 0);
#endif
}
void CMU_HFXOInit ( const CMU_HFXOInit_TypeDef * hfxoInit ) { EFM_ASSERT ( CMU_ClockSelectGet ( cmuClock_HF ) != cmuSelect_HFXO ) ; CMU_OscillatorEnable ( cmuOsc_HFXO , false , true ) ; #if defined ( _SILICON_LABS_32B_SERIES_1 ) && ( _SILICON_LABS_GECKO_INTERNAL_SDID >= 100 ) uint32_t tmp = CMU_HFXOCTRL_MODE_XTAL ; switch ( hfxoInit -> mode ) { case cmuOscMode_Crystal : tmp = CMU_HFXOCTRL_MODE_XTAL ; break ; case cmuOscMode_External : tmp = CMU_HFXOCTRL_MODE_DIGEXTCLK ; break ; case cmuOscMode_AcCoupled : tmp = CMU_HFXOCTRL_MODE_ACBUFEXTCLK ; break ; default : EFM_ASSERT ( false ) ; } if ( SystemHFXOClockGet ( ) <= 25000000 ) { tmp |= CMU_HFXOCTRL_HFXOX2EN ; } CMU -> HFXOCTRL = ( CMU -> HFXOCTRL & ~ ( _CMU_HFXOCTRL_MODE_MASK | _CMU_HFXOCTRL_HFXOX2EN_MASK ) ) | tmp ; CMU -> HFXOSTARTUPCTRL = ( hfxoInit -> ctuneStartup << _CMU_HFXOSTARTUPCTRL_CTUNE_SHIFT ) | ( hfxoInit -> xoCoreBiasTrimStartup << _CMU_HFXOSTARTUPCTRL_IBTRIMXOCORE_SHIFT ) ; CMU -> HFXOSTEADYSTATECTRL = ( CMU -> HFXOSTEADYSTATECTRL & ~ ( _CMU_HFXOSTEADYSTATECTRL_CTUNE_MASK | _CMU_HFXOSTEADYSTATECTRL_IBTRIMXOCORE_MASK ) ) | ( hfxoInit -> ctuneSteadyState << _CMU_HFXOSTEADYSTATECTRL_CTUNE_SHIFT ) | ( hfxoInit -> xoCoreBiasTrimSteadyState << _CMU_HFXOSTEADYSTATECTRL_IBTRIMXOCORE_SHIFT ) ; CMU -> HFXOTIMEOUTCTRL = ( hfxoInit -> timeoutPeakDetect << _CMU_HFXOTIMEOUTCTRL_PEAKDETTIMEOUT_SHIFT ) | ( hfxoInit -> timeoutSteady << _CMU_HFXOTIMEOUTCTRL_STEADYTIMEOUT_SHIFT ) | ( hfxoInit -> timeoutStartup << _CMU_HFXOTIMEOUTCTRL_STARTUPTIMEOUT_SHIFT ) ; #elif defined ( _CMU_HFXOCTRL_MASK ) EFM_ASSERT ( ! ( hfxoInit -> autoStartEm01 || hfxoInit -> autoSelEm01 || hfxoInit -> autoStartSelOnRacWakeup ) ) ; uint32_t tmp = CMU_HFXOCTRL_MODE_XTAL ; EFM_ASSERT ( hfxoInit -> mode != cmuOscMode_AcCoupled ) ; if ( hfxoInit -> mode == cmuOscMode_External ) { tmp = CMU_HFXOCTRL_MODE_DIGEXTCLK ; } CMU -> HFXOCTRL = ( CMU -> HFXOCTRL & ~ _CMU_HFXOCTRL_MODE_MASK ) | tmp ; BUS_RegBitWrite ( & CMU -> HFXOCTRL , _CMU_HFXOCTRL_LOWPOWER_SHIFT , hfxoInit -> lowPowerMode ) ; #if defined ( _CMU_HFXOCTRL1_PEAKDETTHR_MASK ) CMU -> HFXOCTRL1 = ( CMU -> HFXOCTRL1 & ~ _CMU_HFXOCTRL1_PEAKDETTHR_MASK ) | ( hfxoInit -> thresholdPeakDetect << _CMU_HFXOCTRL1_PEAKDETTHR_SHIFT ) ; #endif CMU -> HFXOSTARTUPCTRL = ( hfxoInit -> ctuneStartup << _CMU_HFXOSTARTUPCTRL_CTUNE_SHIFT ) | ( hfxoInit -> xoCoreBiasTrimStartup << _CMU_HFXOSTARTUPCTRL_IBTRIMXOCORE_SHIFT ) ; CMU -> HFXOSTEADYSTATECTRL = ( CMU -> HFXOSTEADYSTATECTRL & ~ ( _CMU_HFXOSTEADYSTATECTRL_CTUNE_MASK | _CMU_HFXOSTEADYSTATECTRL_IBTRIMXOCORE_MASK | _CMU_HFXOSTEADYSTATECTRL_REGISH_MASK | _CMU_HFXOSTEADYSTATECTRL_REGISHUPPER_MASK ) ) | ( hfxoInit -> ctuneSteadyState << _CMU_HFXOSTEADYSTATECTRL_CTUNE_SHIFT ) | ( hfxoInit -> xoCoreBiasTrimSteadyState << _CMU_HFXOSTEADYSTATECTRL_IBTRIMXOCORE_SHIFT ) | ( hfxoInit -> regIshSteadyState << _CMU_HFXOSTEADYSTATECTRL_REGISH_SHIFT ) | getRegIshUpperVal ( hfxoInit -> regIshSteadyState ) ; CMU -> HFXOTIMEOUTCTRL = ( hfxoInit -> timeoutPeakDetect << _CMU_HFXOTIMEOUTCTRL_PEAKDETTIMEOUT_SHIFT ) | ( hfxoInit -> timeoutSteady << _CMU_HFXOTIMEOUTCTRL_STEADYTIMEOUT_SHIFT ) | ( hfxoInit -> timeoutStartup << _CMU_HFXOTIMEOUTCTRL_STARTUPTIMEOUT_SHIFT ) | ( hfxoInit -> timeoutShuntOptimization << _CMU_HFXOTIMEOUTCTRL_SHUNTOPTTIMEOUT_SHIFT ) ; #else CMU -> CTRL = ( CMU -> CTRL & ~ ( _CMU_CTRL_HFXOTIMEOUT_MASK | _CMU_CTRL_HFXOBOOST_MASK | _CMU_CTRL_HFXOMODE_MASK | _CMU_CTRL_HFXOGLITCHDETEN_MASK ) ) | ( hfxoInit -> timeout << _CMU_CTRL_HFXOTIMEOUT_SHIFT ) | ( hfxoInit -> boost << _CMU_CTRL_HFXOBOOST_SHIFT ) | ( hfxoInit -> mode << _CMU_CTRL_HFXOMODE_SHIFT ) | ( hfxoInit -> glitchDetector ? CMU_CTRL_HFXOGLITCHDETEN : 0 ) ; #endif }
@brief
   Set HFXO control registers
@brief Set HFXO control registers
/* Do not disable HFXO if it is currently selected as HF/Core clock */ /* HFXO must be disabled before reconfiguration */ /* Unsupported configuration */ /* HFXO Doubler can only be enabled on crystals up to max 25 MHz */ /* Set tuning for startup and steady state */ /* Set timeouts */ /* Verify that the deprecated autostart fields are not used,
   * @ref CMU_HFXOAutostartEnable must be used instead. */ /* AC coupled external clock not supported */ /* Apply control settings */ /* Set XTAL tuning parameters */ /* Set peak detection threshold */ /* Set tuning for startup and steady state */ /* Set timeouts */