1 /*
2 * Copyright (c) 2020 Libre Solar Technologies GmbH
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <inttypes.h>
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include <zephyr/device.h>
12 #include <zephyr/devicetree.h>
13 #include <zephyr/drivers/adc.h>
14 #include <zephyr/kernel.h>
15 #include <zephyr/sys/printk.h>
16 #include <zephyr/sys/util.h>
17
18 #if !DT_NODE_EXISTS(DT_PATH(zephyr_user)) || \
19 !DT_NODE_HAS_PROP(DT_PATH(zephyr_user), io_channels)
20 #error "No suitable devicetree overlay specified"
21 #endif
22
23 #define DT_SPEC_AND_COMMA(node_id, prop, idx) \
24 ADC_DT_SPEC_GET_BY_IDX(node_id, idx),
25
26 /* Data of ADC io-channels specified in devicetree. */
27 static const struct adc_dt_spec adc_channels[] = {
28 DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels,
29 DT_SPEC_AND_COMMA)
30 };
31
main(void)32 int main(void)
33 {
34 int err;
35 uint32_t count = 0;
36 uint16_t buf;
37 struct adc_sequence sequence = {
38 .buffer = &buf,
39 /* buffer size in bytes, not number of samples */
40 .buffer_size = sizeof(buf),
41 };
42
43 /* Configure channels individually prior to sampling. */
44 for (size_t i = 0U; i < ARRAY_SIZE(adc_channels); i++) {
45 if (!adc_is_ready_dt(&adc_channels[i])) {
46 printk("ADC controller device %s not ready\n", adc_channels[i].dev->name);
47 return 0;
48 }
49
50 err = adc_channel_setup_dt(&adc_channels[i]);
51 if (err < 0) {
52 printk("Could not setup channel #%d (%d)\n", i, err);
53 return 0;
54 }
55 }
56
57 #ifndef CONFIG_COVERAGE
58 while (1) {
59 #else
60 for (int k = 0; k < 10; k++) {
61 #endif
62 printk("ADC reading[%u]:\n", count++);
63 for (size_t i = 0U; i < ARRAY_SIZE(adc_channels); i++) {
64 int32_t val_mv;
65
66 printk("- %s, channel %d: ",
67 adc_channels[i].dev->name,
68 adc_channels[i].channel_id);
69
70 (void)adc_sequence_init_dt(&adc_channels[i], &sequence);
71
72 err = adc_read_dt(&adc_channels[i], &sequence);
73 if (err < 0) {
74 printk("Could not read (%d)\n", err);
75 continue;
76 }
77
78 /*
79 * If using differential mode, the 16 bit value
80 * in the ADC sample buffer should be a signed 2's
81 * complement value.
82 */
83 if (adc_channels[i].channel_cfg.differential) {
84 val_mv = (int32_t)((int16_t)buf);
85 } else {
86 val_mv = (int32_t)buf;
87 }
88 printk("%"PRId32, val_mv);
89 err = adc_raw_to_millivolts_dt(&adc_channels[i],
90 &val_mv);
91 /* conversion to mV may not be supported, skip if not */
92 if (err < 0) {
93 printk(" (value in mV not available)\n");
94 } else {
95 printk(" = %"PRId32" mV\n", val_mv);
96 }
97 }
98
99 k_sleep(K_MSEC(1000));
100 }
101 return 0;
102 }
103