Skip to content

Commit 63c8b95

Browse files
zagorjhedberg
authored andcommitted
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>
1 parent 7c96743 commit 63c8b95

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

drivers/sensor/voltage_divider/voltage.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct voltage_config {
2222

2323
struct voltage_data {
2424
struct adc_sequence sequence;
25-
int16_t raw;
25+
uint16_t raw;
2626
};
2727

2828
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
4747
{
4848
const struct voltage_config *config = dev->config;
4949
struct voltage_data *data = dev->data;
50-
int32_t raw_val = data->raw;
50+
int32_t raw_val;
5151
int32_t v_mv;
5252
int ret;
5353

@@ -57,6 +57,15 @@ static int get(const struct device *dev, enum sensor_channel chan, struct sensor
5757
return -ENOTSUP;
5858
}
5959

60+
if (config->voltage.port.channel_cfg.differential) {
61+
raw_val = (int16_t)data->raw;
62+
} else if (config->voltage.port.resolution < 16) {
63+
/* Can be removed when issue #71119 is resolved */
64+
raw_val = (int16_t)data->raw;
65+
} else {
66+
raw_val = data->raw;
67+
}
68+
6069
ret = adc_raw_to_millivolts_dt(&config->voltage.port, &raw_val);
6170
if (ret != 0) {
6271
LOG_ERR("raw_to_mv: %d", ret);

0 commit comments

Comments
 (0)