Skip to content

Commit eeb933b

Browse files
wkhadgarnashif
authored andcommitted
samples: drivers: adc: adc_sequence: fix adc_sequence channel vref info.
Fix wrong usage of channel adc_raw_to_millivolts on the sequence sample. Signed-off-by: Paulo Santos <[email protected]>
1 parent 782967a commit eeb933b

File tree

1 file changed

+11
-2
lines changed
  • samples/drivers/adc/adc_sequence/src

1 file changed

+11
-2
lines changed

samples/drivers/adc/adc_sequence/src/main.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@
1010
/* ADC node from the devicetree. */
1111
#define ADC_NODE DT_ALIAS(adc0)
1212

13+
/* Auxiliary macro to obtain channel vref, if available. */
14+
#define CHANNEL_VREF(node_id) DT_PROP_OR(node_id, zephyr_vref_mv, 0)
15+
1316
/* Data of ADC device specified in devicetree. */
1417
static const struct device *adc = DEVICE_DT_GET(ADC_NODE);
1518

1619
/* Data array of ADC channels for the specified ADC. */
1720
static const struct adc_channel_cfg channel_cfgs[] = {
1821
DT_FOREACH_CHILD_SEP(ADC_NODE, ADC_CHANNEL_CFG_DT, (,))};
1922

23+
/* Data array of ADC channel voltage references. */
24+
static uint32_t vrefs_mv[] = {DT_FOREACH_CHILD_SEP(ADC_NODE, CHANNEL_VREF, (,))};
25+
2026
/* Get the number of channels defined on the DTS. */
2127
#define CHANNEL_COUNT ARRAY_SIZE(channel_cfgs)
2228

@@ -54,6 +60,9 @@ int main(void)
5460
printf("Could not setup channel #%d (%d)\n", i, err);
5561
return 0;
5662
}
63+
if (channel_cfgs[i].reference == ADC_REF_INTERNAL) {
64+
vrefs_mv[i] = adc_ref_internal(adc);
65+
}
5766
}
5867

5968
#ifndef CONFIG_COVERAGE
@@ -82,12 +91,12 @@ int main(void)
8291
val_mv = channel_reading[sample_index][channel_index];
8392

8493
printf("- - %" PRId32, val_mv);
85-
err = adc_raw_to_millivolts(channel_cfgs[channel_index].reference,
94+
err = adc_raw_to_millivolts(vrefs_mv[channel_index],
8695
channel_cfgs[channel_index].gain,
8796
CONFIG_SEQUENCE_RESOLUTION, &val_mv);
8897

8998
/* conversion to mV may not be supported, skip if not */
90-
if ((err < 0) || channel_cfgs[channel_index].reference == 0) {
99+
if ((err < 0) || vrefs_mv[channel_index] == 0) {
91100
printf(" (value in mV not available)\n");
92101
} else {
93102
printf(" = %" PRId32 "mV\n", val_mv);

0 commit comments

Comments
 (0)