1 /*
2  * Copyright (c) 2024 Centro de Inovacao EDGE
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/drivers/adc.h>
8 #include <zephyr/kernel.h>
9 
10 /* ADC node from the devicetree. */
11 #define ADC_NODE DT_ALIAS(adc0)
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 
16 /* Data of ADC device specified in devicetree. */
17 static const struct device *adc = DEVICE_DT_GET(ADC_NODE);
18 
19 /* Data array of ADC channels for the specified ADC. */
20 static const struct adc_channel_cfg channel_cfgs[] = {
21 	DT_FOREACH_CHILD_SEP(ADC_NODE, ADC_CHANNEL_CFG_DT, (,))};
22 
23 /* Data array of ADC channel voltage references. */
24 static uint32_t vrefs_mv[] = {DT_FOREACH_CHILD_SEP(ADC_NODE, CHANNEL_VREF, (,))};
25 
26 /* Get the number of channels defined on the DTS. */
27 #define CHANNEL_COUNT ARRAY_SIZE(channel_cfgs)
28 
main(void)29 int main(void)
30 {
31 	int err;
32 	uint32_t count = 0;
33 #ifdef CONFIG_SEQUENCE_32BITS_REGISTERS
34 	uint32_t channel_reading[CONFIG_SEQUENCE_SAMPLES][CHANNEL_COUNT];
35 #else
36 	uint16_t channel_reading[CONFIG_SEQUENCE_SAMPLES][CHANNEL_COUNT];
37 #endif
38 
39 	/* Options for the sequence sampling. */
40 	const struct adc_sequence_options options = {
41 		.extra_samplings = CONFIG_SEQUENCE_SAMPLES - 1,
42 		.interval_us = 0,
43 	};
44 
45 	/* Configure the sampling sequence to be made. */
46 	struct adc_sequence sequence = {
47 		.buffer = channel_reading,
48 		/* buffer size in bytes, not number of samples */
49 		.buffer_size = sizeof(channel_reading),
50 		.resolution = CONFIG_SEQUENCE_RESOLUTION,
51 		.options = &options,
52 	};
53 
54 	if (!device_is_ready(adc)) {
55 		printf("ADC controller device %s not ready\n", adc->name);
56 		return 0;
57 	}
58 
59 	/* Configure channels individually prior to sampling. */
60 	for (size_t i = 0U; i < CHANNEL_COUNT; i++) {
61 		sequence.channels |= BIT(channel_cfgs[i].channel_id);
62 		err = adc_channel_setup(adc, &channel_cfgs[i]);
63 		if (err < 0) {
64 			printf("Could not setup channel #%d (%d)\n", i, err);
65 			return 0;
66 		}
67 		if (channel_cfgs[i].reference == ADC_REF_INTERNAL) {
68 			vrefs_mv[i] = adc_ref_internal(adc);
69 		}
70 	}
71 
72 #ifndef CONFIG_COVERAGE
73 	while (1) {
74 #else
75 	for (int k = 0; k < 10; k++) {
76 #endif
77 		printf("ADC sequence reading [%u]:\n", count++);
78 		k_msleep(1000);
79 
80 		err = adc_read(adc, &sequence);
81 		if (err < 0) {
82 			printf("Could not read (%d)\n", err);
83 			continue;
84 		}
85 
86 		for (size_t channel_index = 0U; channel_index < CHANNEL_COUNT; channel_index++) {
87 			int32_t val_mv;
88 
89 			printf("- %s, channel %" PRId32 ", %" PRId32 " sequence samples:\n",
90 			       adc->name, channel_cfgs[channel_index].channel_id,
91 			       CONFIG_SEQUENCE_SAMPLES);
92 			for (size_t sample_index = 0U; sample_index < CONFIG_SEQUENCE_SAMPLES;
93 			     sample_index++) {
94 
95 				val_mv = channel_reading[sample_index][channel_index];
96 
97 				printf("- - %" PRId32, val_mv);
98 				err = adc_raw_to_millivolts(vrefs_mv[channel_index],
99 							    channel_cfgs[channel_index].gain,
100 							    CONFIG_SEQUENCE_RESOLUTION, &val_mv);
101 
102 				/* conversion to mV may not be supported, skip if not */
103 				if ((err < 0) || vrefs_mv[channel_index] == 0) {
104 					printf(" (value in mV not available)\n");
105 				} else {
106 					printf(" = %" PRId32 "mV\n", val_mv);
107 				}
108 			}
109 		}
110 	}
111 
112 	return 0;
113 }
114