emlib/src/em_vdac.c
uint32_t VDAC_PrescaleCalc(uint32_t vdacFreq, bool syncMode, uint32_t hfperFreq)
{
  uint32_t ret, refFreq;

  /* Make sure selected VDAC clock is below max value */
  if (vdacFreq > VDAC_MAX_CLOCK) {
    vdacFreq = VDAC_MAX_CLOCK;
  }

  if (!syncMode) {
    refFreq = VDAC_INTERNAL_CLOCK_FREQ;
  } else {
    if (hfperFreq) {
      refFreq = hfperFreq;
    } else {
      refFreq = CMU_ClockFreqGet(cmuClock_HFPER);
    }
  }

  /* Iterate in order to determine best prescale value. Start with lowest */
  /* prescaler value in order to get the first equal or less VDAC         */
  /* frequency value. */
  for (ret = 0; ret <= _VDAC_CTRL_PRESC_MASK >> _VDAC_CTRL_PRESC_SHIFT; ret++) {
    if ((refFreq / (ret + 1)) <= vdacFreq) {
      break;
    }
  }

  /* If ret is higher than the max prescaler value, make sure to return
     the max value. */
  if (ret > (_VDAC_CTRL_PRESC_MASK >> _VDAC_CTRL_PRESC_SHIFT)) {
    ret = _VDAC_CTRL_PRESC_MASK >> _VDAC_CTRL_PRESC_SHIFT;
  }

  return ret;
}
/***************************************************************************/
/**
 * @brief
 *   Calculate prescaler value used to determine VDAC clock.
 *
 * @details
 *   The VDAC clock is given by input clock divided by prescaler+1.
 *
 *     VDAC_CLK = IN_CLK / (prescale + 1)
 *
 *   Maximum VDAC clock is 1 MHz. Input clock is HFPERCLK when VDAC synchronous
 *   mode is selected, or an internal oscillator of 10 MHz +/- 20% when
 *   asynchronous mode is selected.
 *
 * @note
 *   If the requested VDAC frequency is low and the max prescaler value can not
 *   adjust the actual VDAC frequency lower than requested, the max prescaler
 *   value is returned, resulting in a higher VDAC frequency than requested.
 *
 * @param[in] vdacFreq VDAC frequency target. The frequency will automatically
 *   be adjusted to be below max allowed VDAC clock.
 *
 * @param[in] syncMode Set to true if you intend to use VDAC in synchronous
 *   mode.
 *
 * @param[in] hfperFreq Frequency in Hz of HFPERCLK oscillator. Set to 0 to
 *   use currently defined HFPERCLK clock setting. This parameter is only used
 *   when syncMode is set to true.
 *
 * @return
 *   Prescaler value to use for VDAC in order to achieve a clock value less than
 *   or equal to @p vdacFreq.
 ******************************************************************************/
@brief
Calculate prescaler value used to determine VDAC clock.

@details
The VDAC clock is given by input clock divided by prescaler+1.

VDAC_CLK = IN_CLK / (prescale + 1)

Maximum VDAC clock is 1 MHz. Input clock is HFPERCLK when VDAC synchronous
mode is selected, or an internal oscillator of 10 MHz +/- 20% when
asynchronous mode is selected.

@note
If the requested VDAC frequency is low and the max prescaler value can not
adjust the actual VDAC frequency lower than requested, the max prescaler
value is returned, resulting in a higher VDAC frequency than requested.

@param[in] vdacFreq VDAC frequency target. The frequency will automatically
be adjusted to be below max allowed VDAC clock.

@param[in] syncMode Set to true if you intend to use VDAC in synchronous
mode.

@param[in] hfperFreq Frequency in Hz of HFPERCLK oscillator. Set to 0 to
use currently defined HFPERCLK clock setting. This parameter is only used
when syncMode is set to true.

@return
Prescaler value to use for VDAC in order to achieve a clock value less than
or equal to @p vdacFreq.
@brief Calculate prescaler value used to determine VDAC clock . @details The VDAC clock is given by input clock divided by prescaler + 1 . VDAC_CLK = IN_CLK / ( prescale + 1 ) Maximum VDAC clock is 1 MHz . Input clock is HFPERCLK when VDAC synchronous mode is selected or an internal oscillator of 10 MHz + / - 20% when asynchronous mode is selected . @note If the requested VDAC frequency is low and the max prescaler value can not adjust the actual VDAC frequency lower than requested the max prescaler value is returned resulting in a higher VDAC frequency than requested . @param [ in ] vdacFreq VDAC frequency target . The frequency will automatically be adjusted to be below max allowed VDAC clock . @param [ in ] syncMode Set to true if you intend to use VDAC in synchronous mode . @param [ in ] hfperFreq Frequency in Hz of HFPERCLK oscillator . Set to 0 to use currently defined HFPERCLK clock setting . This parameter is only used when syncMode is set to true . @return Prescaler value to use for VDAC in order to achieve a clock value less than or equal to @p vdacFreq .
uint32_t VDAC_PrescaleCalc(uint32_t vdacFreq, bool syncMode, uint32_t hfperFreq)
{
  uint32_t ret, refFreq;
  if (vdacFreq > VDAC_MAX_CLOCK) {
    vdacFreq = VDAC_MAX_CLOCK;
  }
  if (!syncMode) {
    refFreq = VDAC_INTERNAL_CLOCK_FREQ;
  } else {
    if (hfperFreq) {
      refFreq = hfperFreq;
    } else {
      refFreq = CMU_ClockFreqGet(cmuClock_HFPER);
    }
  }
  for (ret = 0; ret <= _VDAC_CTRL_PRESC_MASK >> _VDAC_CTRL_PRESC_SHIFT; ret++) {
    if ((refFreq / (ret + 1)) <= vdacFreq) {
      break;
    }
  }
  if (ret > (_VDAC_CTRL_PRESC_MASK >> _VDAC_CTRL_PRESC_SHIFT)) {
    ret = _VDAC_CTRL_PRESC_MASK >> _VDAC_CTRL_PRESC_SHIFT;
  }
  return ret;
}
uint32_t VDAC_PrescaleCalc ( uint32_t vdacFreq , bool syncMode , uint32_t hfperFreq ) { uint32_t ret , refFreq ; if ( vdacFreq > VDAC_MAX_CLOCK ) { vdacFreq = VDAC_MAX_CLOCK ; } if ( ! syncMode ) { refFreq = VDAC_INTERNAL_CLOCK_FREQ ; } else { if ( hfperFreq ) { refFreq = hfperFreq ; } else { refFreq = CMU_ClockFreqGet ( cmuClock_HFPER ) ; } } for ( ret = 0 ; ret <= _VDAC_CTRL_PRESC_MASK >> _VDAC_CTRL_PRESC_SHIFT ; ret ++ ) { if ( ( refFreq / ( ret + 1 ) ) <= vdacFreq ) { break ; } } if ( ret > ( _VDAC_CTRL_PRESC_MASK >> _VDAC_CTRL_PRESC_SHIFT ) ) { ret = _VDAC_CTRL_PRESC_MASK >> _VDAC_CTRL_PRESC_SHIFT ; } return ret ; }
@brief
   Calculate prescaler value used to determine VDAC clock.
@brief Calculate prescaler value used to determine VDAC clock .
/* Make sure selected VDAC clock is below max value */ /* Iterate in order to determine best prescale value. Start with lowest */ /* prescaler value in order to get the first equal or less VDAC         */ /* frequency value. */ /* If ret is higher than the max prescaler value, make sure to return
     the max value. */