|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Centro de Inovacao EDGE |
| 3 | + * Copyright (c) 2025 Analog Devices, Inc. |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdio.h> |
| 8 | +#include <stdlib.h> |
| 9 | + |
| 10 | +#include <zephyr/device.h> |
| 11 | +#include <zephyr/drivers/adc.h> |
| 12 | +#include <zephyr/sys/util_macro.h> |
| 13 | +#include <zephyr/rtio/rtio.h> |
| 14 | +#include <zephyr/kernel.h> |
| 15 | +#include <zephyr/dsp/print_format.h> |
| 16 | + |
| 17 | +/* ADC node from the devicetree. */ |
| 18 | +#define SAMPLE_ADC_NODE DT_ALIAS(adc0) |
| 19 | + |
| 20 | +#define SAMPLE_DT_SPEC_AND_COMMA(node_id, prop, idx) ADC_DT_SPEC_GET_BY_IDX(node_id, idx), |
| 21 | + |
| 22 | +#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), io_channels) |
| 23 | +/* Data of ADC io-channels specified in devicetree. */ |
| 24 | +static const struct adc_dt_spec adc_channels[] = { |
| 25 | + DT_FOREACH_PROP_ELEM(DT_PATH(zephyr_user), io_channels, SAMPLE_DT_SPEC_AND_COMMA) |
| 26 | +}; |
| 27 | +static const int adc_channels_count = ARRAY_SIZE(adc_channels); |
| 28 | +#endif |
| 29 | + |
| 30 | +static void init_adc(void) |
| 31 | +{ |
| 32 | + int i, ret; |
| 33 | + |
| 34 | + ret = adc_is_ready_dt(&adc_channels[0]); |
| 35 | + |
| 36 | + for (i = 0; i < adc_channels_count; i++) { |
| 37 | + ret = adc_channel_setup_dt(&adc_channels[i]); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/* Define triggers for ADC stream. Trigger stands for an event that |
| 42 | + * will cause the ADC to read data and perform an operation on it. |
| 43 | + * |
| 44 | + * Format for each trigger is: |
| 45 | + * {trigger, operation to be done on the data associated to the trigger} |
| 46 | + * |
| 47 | + * For more information about supported triggers and data operations, |
| 48 | + * refer to the enums adc_trigger_type and adc_stream_data_opt. |
| 49 | + */ |
| 50 | +#define SAMPLE_ADC_TRIGGERS \ |
| 51 | + {ADC_TRIG_FIFO_FULL, ADC_STREAM_DATA_INCLUDE}, \ |
| 52 | + {ADC_TRIG_FIFO_WATERMARK, ADC_STREAM_DATA_INCLUDE} |
| 53 | + |
| 54 | +ADC_DT_STREAM_IODEV(iodev, SAMPLE_ADC_NODE, adc_channels, SAMPLE_ADC_TRIGGERS); |
| 55 | + |
| 56 | +/* Mempool is used for sharing ADC data that has been read between ADC driver and the application. |
| 57 | + * Data read in the ADC driver is stored in the mempool and can be processed in the application. |
| 58 | + * Current values are set to support range of ADC drivers and can be optimized for smaller memory |
| 59 | + * footprint. sizeof(void *) is used so memory blocks are properly aligned for different |
| 60 | + * platforms. |
| 61 | + */ |
| 62 | +RTIO_DEFINE_WITH_MEMPOOL(adc_ctx, 16, 16, 20, 256, sizeof(void *)); |
| 63 | + |
| 64 | +static int print_adc_stream(const struct device *adc, struct rtio_iodev *local_iodev) |
| 65 | +{ |
| 66 | + int rc = 0; |
| 67 | + const struct adc_decoder_api *decoder; |
| 68 | + struct rtio_cqe *cqe; |
| 69 | + uint8_t *buf; |
| 70 | + uint32_t buf_len; |
| 71 | + struct rtio_sqe *handles; |
| 72 | + |
| 73 | + /* Start the streams */ |
| 74 | + adc_stream(local_iodev, &adc_ctx, NULL, &handles); |
| 75 | + |
| 76 | + while (1) { |
| 77 | + /* CQE is a RTIO completion event. It is created in a ADC driver that is |
| 78 | + * doing the streaming when there is a batch of data to be processed |
| 79 | + * or some error occurred during ADC streaming. CQE contains result of |
| 80 | + * data reading and userdata (if one is passed in adc_stream). |
| 81 | + * It is used to retrieve mempool buffer where data is stored. |
| 82 | + */ |
| 83 | + cqe = rtio_cqe_consume_block(&adc_ctx); |
| 84 | + |
| 85 | + if (cqe->result != 0) { |
| 86 | + printk("async read failed %d\n", cqe->result); |
| 87 | + return cqe->result; |
| 88 | + } |
| 89 | + |
| 90 | + rc = rtio_cqe_get_mempool_buffer(&adc_ctx, cqe, &buf, &buf_len); |
| 91 | + |
| 92 | + if (rc != 0) { |
| 93 | + printk("get mempool buffer failed %d\n", rc); |
| 94 | + return rc; |
| 95 | + } |
| 96 | + |
| 97 | + rtio_cqe_release(&adc_ctx, cqe); |
| 98 | + |
| 99 | + rc = adc_get_decoder(adc, &decoder); |
| 100 | + |
| 101 | + if (rc != 0) { |
| 102 | + printk("sensor_get_decoder failed %d\n", rc); |
| 103 | + return rc; |
| 104 | + } |
| 105 | + |
| 106 | + /* Frame iterator values when data comes from a FIFO */ |
| 107 | + uint32_t adc_fit = 0; |
| 108 | + struct adc_data adc_data = {0}; |
| 109 | + |
| 110 | + /* Number of accelerometer data frames */ |
| 111 | + uint16_t frame_count; |
| 112 | + |
| 113 | + rc = decoder->get_frame_count(buf, 0, &frame_count); |
| 114 | + |
| 115 | + if (rc != 0) { |
| 116 | + printk("get_frame_count failed %d\n", rc); |
| 117 | + return rc; |
| 118 | + } |
| 119 | + |
| 120 | + /* Decode all available accelerometer sample frames */ |
| 121 | + for (int i = 0; i < frame_count; i++) { |
| 122 | + decoder->decode(buf, 0, &adc_fit, 1, &adc_data); |
| 123 | + |
| 124 | + printk("ADC data for %s (%" PRIq(6) ") %lluns\n", adc->name, |
| 125 | + PRIq_arg(adc_data.readings[0].value, 6, adc_data.shift), |
| 126 | + (adc_data.header.base_timestamp_ns |
| 127 | + + adc_data.readings[0].timestamp_delta)); |
| 128 | + } |
| 129 | + |
| 130 | + rtio_release_buffer(&adc_ctx, buf, buf_len); |
| 131 | + } |
| 132 | + |
| 133 | + return rc; |
| 134 | +} |
| 135 | + |
| 136 | +int main(void) |
| 137 | +{ |
| 138 | + int ret; |
| 139 | + struct adc_sequence sequence; |
| 140 | + struct adc_read_config *read_cfg = iodev.data; |
| 141 | + |
| 142 | + read_cfg->sequence = &sequence; |
| 143 | + |
| 144 | + init_adc(); |
| 145 | + ret = adc_sequence_init_dt(&adc_channels[0], &sequence); |
| 146 | + if (ret < 0) { |
| 147 | + printk("Failed to initialize ADC sequence: %d\n", ret); |
| 148 | + return 0; |
| 149 | + } |
| 150 | + |
| 151 | + print_adc_stream(adc_channels[0].dev, &iodev); |
| 152 | + |
| 153 | + return 0; |
| 154 | +} |
0 commit comments