Commit 63c8b950 authored by Björn Stenberg's avatar Björn Stenberg Committed by Johan Hedberg
Browse files

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: default avatarBjörn Stenberg <bjorn@haxx.se>
parent 7c96743f
Loading
Loading
Loading
Loading
+11 −2
Changes for drivers/sensor/voltage_divider/voltage.c: 11 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -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);