Just going through the sample for reading an adc and I’m a little confused. What is the purpose of the all the ‘sequence’ stuff? The documentation for the adc_sequence struct and adc_sequence_init_dt has me a little confused. I’ve included a sample program that reads an adc pin on a nrf52840 board below. The example calls adc_sequence_init_dt(&channel_dt, &sequence);
and passes a pointer to the adc channel and this ‘sequence’ thing that also specifies channels I want to read. I’m missing something fundamental here.
#include <zephyr/device.h>
#include <zephyr/logging/log.h>
#include <zephyr/drivers/adc.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/kernel.h>
LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG);
void main(void)
{
int err;
const struct device *adc_dev;
// Get the ADC device
adc_dev = DEVICE_DT_GET(DT_NODELABEL(adc));
if (!adc_dev)
{
printk("Failed to get ADC device\n");
return;
}
struct adc_dt_spec channel_dt = ADC_DT_SPEC_GET_BY_IDX(DT_NODELABEL(adc), 0);
err = adc_channel_setup_dt(&channel_dt);
if (err)
{
printk("Failed to setup ADC channel: %d\n", err);
return;
}
uint16_t buf;
struct adc_sequence sequence = {
.buffer = &buf,
/* buffer size in bytes, not number of samples */
.buffer_size = sizeof(buf),
};
// Read the ADC value
while (1)
{
(void)adc_sequence_init_dt(&channel_dt, &sequence); // <-- Why??
err = adc_read(adc_dev, &sequence); // Why seq. and not a channel/pin/label
if (err)
{
printk("Failed to read ADC value: %d\n", err);
return;
}
int32_t mvValue = buf;
err = adc_raw_to_millivolts_dt(&channel_dt, &mvValue);
if (err)
{
printk("Failed to read ADC value: %d\n", err);
return;
}
printk("ADC value: %d, mV: %d\n", buf, mvValue);
k_msleep(1000);
}
}
```