|
10 | 10 | /* ADC node from the devicetree. */
|
11 | 11 | #define ADC_NODE DT_ALIAS(adc0)
|
12 | 12 |
|
| 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 | + |
13 | 16 | /* Data of ADC device specified in devicetree. */
|
14 | 17 | static const struct device *adc = DEVICE_DT_GET(ADC_NODE);
|
15 | 18 |
|
16 | 19 | /* Data array of ADC channels for the specified ADC. */
|
17 | 20 | static const struct adc_channel_cfg channel_cfgs[] = {
|
18 | 21 | DT_FOREACH_CHILD_SEP(ADC_NODE, ADC_CHANNEL_CFG_DT, (,))};
|
19 | 22 |
|
| 23 | +/* Data array of ADC channel voltage references. */ |
| 24 | +static uint32_t vrefs_mv[] = {DT_FOREACH_CHILD_SEP(ADC_NODE, CHANNEL_VREF, (,))}; |
| 25 | + |
20 | 26 | /* Get the number of channels defined on the DTS. */
|
21 | 27 | #define CHANNEL_COUNT ARRAY_SIZE(channel_cfgs)
|
22 | 28 |
|
@@ -54,6 +60,9 @@ int main(void)
|
54 | 60 | printf("Could not setup channel #%d (%d)\n", i, err);
|
55 | 61 | return 0;
|
56 | 62 | }
|
| 63 | + if (channel_cfgs[i].reference == ADC_REF_INTERNAL) { |
| 64 | + vrefs_mv[i] = adc_ref_internal(adc); |
| 65 | + } |
57 | 66 | }
|
58 | 67 |
|
59 | 68 | #ifndef CONFIG_COVERAGE
|
@@ -82,12 +91,12 @@ int main(void)
|
82 | 91 | val_mv = channel_reading[sample_index][channel_index];
|
83 | 92 |
|
84 | 93 | 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], |
86 | 95 | channel_cfgs[channel_index].gain,
|
87 | 96 | CONFIG_SEQUENCE_RESOLUTION, &val_mv);
|
88 | 97 |
|
89 | 98 | /* 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) { |
91 | 100 | printf(" (value in mV not available)\n");
|
92 | 101 | } else {
|
93 | 102 | printf(" = %" PRId32 "mV\n", val_mv);
|
|
0 commit comments