emlib/src/em_emu.c
static uint32_t vmonCalibratedThreshold(EMU_VmonChannel_TypeDef channel,
                                        int threshold)
{
  uint32_t tLow;
  uint32_t tHigh;
  uint32_t calReg;

  /* Get calibration values for 1.86V and 2.98V */
  switch (channel) {
    case emuVmonChannel_AVDD:
      calReg = DEVINFO->VMONCAL0;
      tLow = (10 * ((calReg & _DEVINFO_VMONCAL0_AVDD1V86THRESCOARSE_MASK)
                    >> _DEVINFO_VMONCAL0_AVDD1V86THRESCOARSE_SHIFT))
             + ((calReg & _DEVINFO_VMONCAL0_AVDD1V86THRESFINE_MASK)
                >> _DEVINFO_VMONCAL0_AVDD1V86THRESFINE_SHIFT);
      tHigh = (10 * ((calReg & _DEVINFO_VMONCAL0_AVDD2V98THRESCOARSE_MASK)
                     >> _DEVINFO_VMONCAL0_AVDD2V98THRESCOARSE_SHIFT))
              + ((calReg & _DEVINFO_VMONCAL0_AVDD2V98THRESFINE_MASK)
                 >> _DEVINFO_VMONCAL0_AVDD2V98THRESFINE_SHIFT);
      break;
    case emuVmonChannel_ALTAVDD:
      calReg = DEVINFO->VMONCAL0;
      tLow = (10 * ((calReg & _DEVINFO_VMONCAL0_ALTAVDD1V86THRESCOARSE_MASK)
                    >> _DEVINFO_VMONCAL0_ALTAVDD1V86THRESCOARSE_SHIFT))
             + ((calReg & _DEVINFO_VMONCAL0_ALTAVDD1V86THRESFINE_MASK)
                >> _DEVINFO_VMONCAL0_ALTAVDD1V86THRESFINE_SHIFT);
      tHigh = (10 * ((calReg & _DEVINFO_VMONCAL0_ALTAVDD2V98THRESCOARSE_MASK)
                     >> _DEVINFO_VMONCAL0_ALTAVDD2V98THRESCOARSE_SHIFT))
              + ((calReg & _DEVINFO_VMONCAL0_ALTAVDD2V98THRESFINE_MASK)
                 >> _DEVINFO_VMONCAL0_ALTAVDD2V98THRESFINE_SHIFT);
      break;
    case emuVmonChannel_DVDD:
      calReg = DEVINFO->VMONCAL1;
      tLow = (10 * ((calReg & _DEVINFO_VMONCAL1_DVDD1V86THRESCOARSE_MASK)
                    >> _DEVINFO_VMONCAL1_DVDD1V86THRESCOARSE_SHIFT))
             + ((calReg & _DEVINFO_VMONCAL1_DVDD1V86THRESFINE_MASK)
                >> _DEVINFO_VMONCAL1_DVDD1V86THRESFINE_SHIFT);
      tHigh = (10 * ((calReg & _DEVINFO_VMONCAL1_DVDD2V98THRESCOARSE_MASK)
                     >> _DEVINFO_VMONCAL1_DVDD2V98THRESCOARSE_SHIFT))
              + ((calReg & _DEVINFO_VMONCAL1_DVDD2V98THRESFINE_MASK)
                 >> _DEVINFO_VMONCAL1_DVDD2V98THRESFINE_SHIFT);
      break;
    case emuVmonChannel_IOVDD0:
      calReg = DEVINFO->VMONCAL1;
      tLow = (10 * ((calReg & _DEVINFO_VMONCAL1_IO01V86THRESCOARSE_MASK)
                    >> _DEVINFO_VMONCAL1_IO01V86THRESCOARSE_SHIFT))
             + ((calReg & _DEVINFO_VMONCAL1_IO01V86THRESFINE_MASK)
                >> _DEVINFO_VMONCAL1_IO01V86THRESFINE_SHIFT);
      tHigh = (10 * ((calReg & _DEVINFO_VMONCAL1_IO02V98THRESCOARSE_MASK)
                     >> _DEVINFO_VMONCAL1_IO02V98THRESCOARSE_SHIFT))
              + ((calReg & _DEVINFO_VMONCAL1_IO02V98THRESFINE_MASK)
                 >> _DEVINFO_VMONCAL1_IO02V98THRESFINE_SHIFT);
      break;
    default:
      EFM_ASSERT(false);
      return threshold;
  }

  if (tHigh <= tLow) {
    /* Uncalibrated device guard */
    return threshold;
  }

  /* Calculate threshold.
   *
   * Note that volt is used in the reference manual, however we are interested
   * in millivolt results. We also increase the precision of Va and Vb in the
   * calculation instead of using floating points.
   */
  uint32_t va = (1120 * 100) / (tHigh - tLow);
  uint32_t vb = (1860 * 100) - (va * tLow);
  /* Round threshold to nearest integer value. */
  return ((threshold * 100) - vb + (va / 2)) / va;
}
/***************************************************************************/
/**
 * @brief
 *   Get calibrated threshold value.
 *
 * @details
 *   All VMON channels have two calibration fields in the DI page that
 *   describes the threshold at 1.86V and 2.98V. This function will convert
 *   the uncalibrated input voltage threshold in millivolts into a calibrated
 *   threshold.
 *
 * @param[in] channel
 *   VMON channel
 *
 * @param[in] threshold
 *   Desired threshold in millivolts.
 *
 * @return
 *   Calibrated threshold value to use. First digit of return value is placed
 *   in the "fine" register fields while the next digits are placed in the
 *   "coarse" register fields.
 ******************************************************************************/
@brief
Get calibrated threshold value.

@details
All VMON channels have two calibration fields in the DI page that
describes the threshold at 1.86V and 2.98V. This function will convert
the uncalibrated input voltage threshold in millivolts into a calibrated
threshold.

@param[in] channel
VMON channel

@param[in] threshold
Desired threshold in millivolts.

@return
Calibrated threshold value to use. First digit of return value is placed
in the "fine" register fields while the next digits are placed in the
"coarse" register fields.
@brief Get calibrated threshold value . @details All VMON channels have two calibration fields in the DI page that describes the threshold at 1 . 86V and 2 . 98V . This function will convert the uncalibrated input voltage threshold in millivolts into a calibrated threshold . @param [ in ] channel VMON channel @param [ in ] threshold Desired threshold in millivolts . @return Calibrated threshold value to use . First digit of return value is placed in the " fine " register fields while the next digits are placed in the " coarse " register fields .
static uint32_t vmonCalibratedThreshold(EMU_VmonChannel_TypeDef channel,
                                        int threshold)
{
  uint32_t tLow;
  uint32_t tHigh;
  uint32_t calReg;
  switch (channel) {
    case emuVmonChannel_AVDD:
      calReg = DEVINFO->VMONCAL0;
      tLow = (10 * ((calReg & _DEVINFO_VMONCAL0_AVDD1V86THRESCOARSE_MASK)
                    >> _DEVINFO_VMONCAL0_AVDD1V86THRESCOARSE_SHIFT))
             + ((calReg & _DEVINFO_VMONCAL0_AVDD1V86THRESFINE_MASK)
                >> _DEVINFO_VMONCAL0_AVDD1V86THRESFINE_SHIFT);
      tHigh = (10 * ((calReg & _DEVINFO_VMONCAL0_AVDD2V98THRESCOARSE_MASK)
                     >> _DEVINFO_VMONCAL0_AVDD2V98THRESCOARSE_SHIFT))
              + ((calReg & _DEVINFO_VMONCAL0_AVDD2V98THRESFINE_MASK)
                 >> _DEVINFO_VMONCAL0_AVDD2V98THRESFINE_SHIFT);
      break;
    case emuVmonChannel_ALTAVDD:
      calReg = DEVINFO->VMONCAL0;
      tLow = (10 * ((calReg & _DEVINFO_VMONCAL0_ALTAVDD1V86THRESCOARSE_MASK)
                    >> _DEVINFO_VMONCAL0_ALTAVDD1V86THRESCOARSE_SHIFT))
             + ((calReg & _DEVINFO_VMONCAL0_ALTAVDD1V86THRESFINE_MASK)
                >> _DEVINFO_VMONCAL0_ALTAVDD1V86THRESFINE_SHIFT);
      tHigh = (10 * ((calReg & _DEVINFO_VMONCAL0_ALTAVDD2V98THRESCOARSE_MASK)
                     >> _DEVINFO_VMONCAL0_ALTAVDD2V98THRESCOARSE_SHIFT))
              + ((calReg & _DEVINFO_VMONCAL0_ALTAVDD2V98THRESFINE_MASK)
                 >> _DEVINFO_VMONCAL0_ALTAVDD2V98THRESFINE_SHIFT);
      break;
    case emuVmonChannel_DVDD:
      calReg = DEVINFO->VMONCAL1;
      tLow = (10 * ((calReg & _DEVINFO_VMONCAL1_DVDD1V86THRESCOARSE_MASK)
                    >> _DEVINFO_VMONCAL1_DVDD1V86THRESCOARSE_SHIFT))
             + ((calReg & _DEVINFO_VMONCAL1_DVDD1V86THRESFINE_MASK)
                >> _DEVINFO_VMONCAL1_DVDD1V86THRESFINE_SHIFT);
      tHigh = (10 * ((calReg & _DEVINFO_VMONCAL1_DVDD2V98THRESCOARSE_MASK)
                     >> _DEVINFO_VMONCAL1_DVDD2V98THRESCOARSE_SHIFT))
              + ((calReg & _DEVINFO_VMONCAL1_DVDD2V98THRESFINE_MASK)
                 >> _DEVINFO_VMONCAL1_DVDD2V98THRESFINE_SHIFT);
      break;
    case emuVmonChannel_IOVDD0:
      calReg = DEVINFO->VMONCAL1;
      tLow = (10 * ((calReg & _DEVINFO_VMONCAL1_IO01V86THRESCOARSE_MASK)
                    >> _DEVINFO_VMONCAL1_IO01V86THRESCOARSE_SHIFT))
             + ((calReg & _DEVINFO_VMONCAL1_IO01V86THRESFINE_MASK)
                >> _DEVINFO_VMONCAL1_IO01V86THRESFINE_SHIFT);
      tHigh = (10 * ((calReg & _DEVINFO_VMONCAL1_IO02V98THRESCOARSE_MASK)
                     >> _DEVINFO_VMONCAL1_IO02V98THRESCOARSE_SHIFT))
              + ((calReg & _DEVINFO_VMONCAL1_IO02V98THRESFINE_MASK)
                 >> _DEVINFO_VMONCAL1_IO02V98THRESFINE_SHIFT);
      break;
    default:
      EFM_ASSERT(false);
      return threshold;
  }
  if (tHigh <= tLow) {
    return threshold;
  }
  uint32_t va = (1120 * 100) / (tHigh - tLow);
  uint32_t vb = (1860 * 100) - (va * tLow);
  return ((threshold * 100) - vb + (va / 2)) / va;
}
static uint32_t vmonCalibratedThreshold ( EMU_VmonChannel_TypeDef channel , int threshold ) { uint32_t tLow ; uint32_t tHigh ; uint32_t calReg ; switch ( channel ) { case emuVmonChannel_AVDD : calReg = DEVINFO -> VMONCAL0 ; tLow = ( 10 * ( ( calReg & _DEVINFO_VMONCAL0_AVDD1V86THRESCOARSE_MASK ) >> _DEVINFO_VMONCAL0_AVDD1V86THRESCOARSE_SHIFT ) ) + ( ( calReg & _DEVINFO_VMONCAL0_AVDD1V86THRESFINE_MASK ) >> _DEVINFO_VMONCAL0_AVDD1V86THRESFINE_SHIFT ) ; tHigh = ( 10 * ( ( calReg & _DEVINFO_VMONCAL0_AVDD2V98THRESCOARSE_MASK ) >> _DEVINFO_VMONCAL0_AVDD2V98THRESCOARSE_SHIFT ) ) + ( ( calReg & _DEVINFO_VMONCAL0_AVDD2V98THRESFINE_MASK ) >> _DEVINFO_VMONCAL0_AVDD2V98THRESFINE_SHIFT ) ; break ; case emuVmonChannel_ALTAVDD : calReg = DEVINFO -> VMONCAL0 ; tLow = ( 10 * ( ( calReg & _DEVINFO_VMONCAL0_ALTAVDD1V86THRESCOARSE_MASK ) >> _DEVINFO_VMONCAL0_ALTAVDD1V86THRESCOARSE_SHIFT ) ) + ( ( calReg & _DEVINFO_VMONCAL0_ALTAVDD1V86THRESFINE_MASK ) >> _DEVINFO_VMONCAL0_ALTAVDD1V86THRESFINE_SHIFT ) ; tHigh = ( 10 * ( ( calReg & _DEVINFO_VMONCAL0_ALTAVDD2V98THRESCOARSE_MASK ) >> _DEVINFO_VMONCAL0_ALTAVDD2V98THRESCOARSE_SHIFT ) ) + ( ( calReg & _DEVINFO_VMONCAL0_ALTAVDD2V98THRESFINE_MASK ) >> _DEVINFO_VMONCAL0_ALTAVDD2V98THRESFINE_SHIFT ) ; break ; case emuVmonChannel_DVDD : calReg = DEVINFO -> VMONCAL1 ; tLow = ( 10 * ( ( calReg & _DEVINFO_VMONCAL1_DVDD1V86THRESCOARSE_MASK ) >> _DEVINFO_VMONCAL1_DVDD1V86THRESCOARSE_SHIFT ) ) + ( ( calReg & _DEVINFO_VMONCAL1_DVDD1V86THRESFINE_MASK ) >> _DEVINFO_VMONCAL1_DVDD1V86THRESFINE_SHIFT ) ; tHigh = ( 10 * ( ( calReg & _DEVINFO_VMONCAL1_DVDD2V98THRESCOARSE_MASK ) >> _DEVINFO_VMONCAL1_DVDD2V98THRESCOARSE_SHIFT ) ) + ( ( calReg & _DEVINFO_VMONCAL1_DVDD2V98THRESFINE_MASK ) >> _DEVINFO_VMONCAL1_DVDD2V98THRESFINE_SHIFT ) ; break ; case emuVmonChannel_IOVDD0 : calReg = DEVINFO -> VMONCAL1 ; tLow = ( 10 * ( ( calReg & _DEVINFO_VMONCAL1_IO01V86THRESCOARSE_MASK ) >> _DEVINFO_VMONCAL1_IO01V86THRESCOARSE_SHIFT ) ) + ( ( calReg & _DEVINFO_VMONCAL1_IO01V86THRESFINE_MASK ) >> _DEVINFO_VMONCAL1_IO01V86THRESFINE_SHIFT ) ; tHigh = ( 10 * ( ( calReg & _DEVINFO_VMONCAL1_IO02V98THRESCOARSE_MASK ) >> _DEVINFO_VMONCAL1_IO02V98THRESCOARSE_SHIFT ) ) + ( ( calReg & _DEVINFO_VMONCAL1_IO02V98THRESFINE_MASK ) >> _DEVINFO_VMONCAL1_IO02V98THRESFINE_SHIFT ) ; break ; default : EFM_ASSERT ( false ) ; return threshold ; } if ( tHigh <= tLow ) { return threshold ; } uint32_t va = ( 1120 * 100 ) / ( tHigh - tLow ) ; uint32_t vb = ( 1860 * 100 ) - ( va * tLow ) ; return ( ( threshold * 100 ) - vb + ( va / 2 ) ) / va ; }
@brief
   Get calibrated threshold value.
@brief Get calibrated threshold value .
/* Get calibration values for 1.86V and 2.98V */ /* Uncalibrated device guard */ /* Calculate threshold.
   *
   * Note that volt is used in the reference manual, however we are interested
   * in millivolt results. We also increase the precision of Va and Vb in the
   * calculation instead of using floating points.
   */ /* Round threshold to nearest integer value. */