voltage_divider: Add support for single-ended 16-bit ADC

Unless configured as differential, the raw ADC data is unsigned.

Includes workaround for #71119.

Signed-off-by: Björn Stenberg <bjorn@haxx.se>
This commit is contained in:
Björn Stenberg 2024-03-15 09:44:35 +01:00 committed by Johan Hedberg
commit 63c8b950e5

View file

@ -22,7 +22,7 @@ struct voltage_config {
struct voltage_data {
struct adc_sequence sequence;
int16_t raw;
uint16_t raw;
};
static int fetch(const struct device *dev, enum sensor_channel chan)
@ -47,7 +47,7 @@ static int get(const struct device *dev, enum sensor_channel chan, struct sensor
{
const struct voltage_config *config = dev->config;
struct voltage_data *data = dev->data;
int32_t raw_val = data->raw;
int32_t raw_val;
int32_t v_mv;
int ret;
@ -57,6 +57,15 @@ static int get(const struct device *dev, enum sensor_channel chan, struct sensor
return -ENOTSUP;
}
if (config->voltage.port.channel_cfg.differential) {
raw_val = (int16_t)data->raw;
} else if (config->voltage.port.resolution < 16) {
/* Can be removed when issue #71119 is resolved */
raw_val = (int16_t)data->raw;
} else {
raw_val = data->raw;
}
ret = adc_raw_to_millivolts_dt(&config->voltage.port, &raw_val);
if (ret != 0) {
LOG_ERR("raw_to_mv: %d", ret);