adc: provide API to help with conversions

Gain values are specified with enumeration values that can't be used
to reverse the effects of scaling the input signal.  Provide a
function that reverses the effect of the gain by scaling a measured
value.

Also provide a function that converts a raw measurement captured with
a reference voltage and specific gain and resolution to the
corresponding voltage in millivolts.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
This commit is contained in:
Peter A. Bigot 2019-12-26 13:16:21 -06:00 committed by Carles Cufí
commit 4420c5ed40
3 changed files with 101 additions and 0 deletions

View file

@ -45,6 +45,25 @@ enum adc_gain {
ADC_GAIN_128, /**< x 128. */
};
/**
* @brief Invert the application of gain to a measurement value.
*
* For example, if the gain passed in is ADC_GAIN_1_6 and the
* referenced value is 10, the value after the function returns is 60.
*
* @param gain the gain used to amplify the input signal.
*
* @param value a pointer to a value that initially has the effect of
* the applied gain but has that effect removed when this function
* successfully returns. If the gain cannot be reversed the value
* remains unchanged.
*
* @retval 0 if the gain was successfully reversed
* @retval -EINVAL if the gain could not be interpreted
*/
int adc_gain_invert(enum adc_gain gain,
s32_t *value);
/** @brief ADC references. */
enum adc_reference {
ADC_REF_VDD_1, /**< VDD. */
@ -135,6 +154,43 @@ struct adc_channel_cfg {
#endif /* CONFIG_ADC_CONFIGURABLE_INPUTS */
};
/**
* @brief Convert a raw ADC value to millivolts.
*
* This function performs the necessary conversion to transform a raw
* ADC measurement to a voltage in millivolts.
*
* @param ref_mv the reference voltage used for the measurement, in
* millivolts. This may be from adc_ref_internal() or a known
* external reference.
*
* @param gain the ADC gain configuration used to sample the input
*
* @param resolution the number of bits in the absolute value of the
* sample. For differential sampling this may be one less than the
* resolution in struct adc_sequence.
*
* @param valp pointer to the raw measurement value on input, and the
* corresponding millivolt value on successful conversion. If
* conversion fails the stored value is left unchanged.
*
* @retval 0 on successful conversion
* @retval -EINVAL if the gain is not reversible
*/
static inline int adc_raw_to_millivolts(s32_t ref_mv,
enum adc_gain gain,
u8_t resolution,
s32_t *valp)
{
s32_t adc_mv = *valp * ref_mv;
int ret = adc_gain_invert(gain, &adc_mv);
if (ret == 0) {
*valp = (adc_mv >> resolution);
}
return ret;
}
/* Forward declaration of the adc_sequence structure. */
struct adc_sequence;