adc: 64bit gain inversion function
Add a variant of `adc_gain_invert` that operates on an `int64_t` instead of `int32_t`. Signed-off-by: Jordan Yates <jordan@embeint.com>
This commit is contained in:
parent
4b52921df5
commit
8ab300d63e
2 changed files with 64 additions and 31 deletions
|
@ -6,14 +6,11 @@
|
||||||
|
|
||||||
#include <zephyr/drivers/adc.h>
|
#include <zephyr/drivers/adc.h>
|
||||||
|
|
||||||
int adc_gain_invert(enum adc_gain gain,
|
struct gain_desc {
|
||||||
int32_t *value)
|
|
||||||
{
|
|
||||||
struct gain_desc {
|
|
||||||
uint8_t mul;
|
uint8_t mul;
|
||||||
uint8_t div;
|
uint8_t div;
|
||||||
};
|
};
|
||||||
static const struct gain_desc gains[] = {
|
static const struct gain_desc gains[] = {
|
||||||
[ADC_GAIN_1_6] = {.mul = 6, .div = 1},
|
[ADC_GAIN_1_6] = {.mul = 6, .div = 1},
|
||||||
[ADC_GAIN_1_5] = {.mul = 5, .div = 1},
|
[ADC_GAIN_1_5] = {.mul = 5, .div = 1},
|
||||||
[ADC_GAIN_1_4] = {.mul = 4, .div = 1},
|
[ADC_GAIN_1_4] = {.mul = 4, .div = 1},
|
||||||
|
@ -35,7 +32,26 @@ int adc_gain_invert(enum adc_gain gain,
|
||||||
[ADC_GAIN_32] = {.mul = 1, .div = 32},
|
[ADC_GAIN_32] = {.mul = 1, .div = 32},
|
||||||
[ADC_GAIN_64] = {.mul = 1, .div = 64},
|
[ADC_GAIN_64] = {.mul = 1, .div = 64},
|
||||||
[ADC_GAIN_128] = {.mul = 1, .div = 128},
|
[ADC_GAIN_128] = {.mul = 1, .div = 128},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int adc_gain_invert(enum adc_gain gain, int32_t *value)
|
||||||
|
{
|
||||||
|
int rv = -EINVAL;
|
||||||
|
|
||||||
|
if ((uint8_t)gain < ARRAY_SIZE(gains)) {
|
||||||
|
const struct gain_desc *gdp = &gains[gain];
|
||||||
|
|
||||||
|
if ((gdp->mul != 0) && (gdp->div != 0)) {
|
||||||
|
*value = (gdp->mul * *value) / gdp->div;
|
||||||
|
rv = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
|
int adc_gain_invert_64(enum adc_gain gain, int64_t *value)
|
||||||
|
{
|
||||||
int rv = -EINVAL;
|
int rv = -EINVAL;
|
||||||
|
|
||||||
if ((uint8_t)gain < ARRAY_SIZE(gains)) {
|
if ((uint8_t)gain < ARRAY_SIZE(gains)) {
|
||||||
|
|
|
@ -71,8 +71,25 @@ enum adc_gain {
|
||||||
* @retval 0 if the gain was successfully reversed
|
* @retval 0 if the gain was successfully reversed
|
||||||
* @retval -EINVAL if the gain could not be interpreted
|
* @retval -EINVAL if the gain could not be interpreted
|
||||||
*/
|
*/
|
||||||
int adc_gain_invert(enum adc_gain gain,
|
int adc_gain_invert(enum adc_gain gain, int32_t *value);
|
||||||
int32_t *value);
|
|
||||||
|
/**
|
||||||
|
* @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_64(enum adc_gain gain, int64_t *value);
|
||||||
|
|
||||||
/** @brief ADC references. */
|
/** @brief ADC references. */
|
||||||
enum adc_reference {
|
enum adc_reference {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue