This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

adc_read() returns -EINVAL

I am trying to read ADC values for 8 different channel, in order. It's just to test if the ADC and its API.

I successfully receive the ADC binding (it returns 0), and the channel setup also return 0 for all 8 channels.

However, the adc_read() returns -EINVAL (-22). I don't see why. Any help would be nice!

int16_t sampleBuffer[64];

const struct adc_sequence_options options = { 0, NULL, 0 };
const struct adc_sequence sequence = {
    .options = &options,
    .channels = BIT(0),
    .buffer = &sampleBuffer,
    .buffer_size = sizeof(sampleBuffer),
    .resolution = 10,
    .oversampling = 0,
    .calibrate = false,
};

int ret = adc_read(adc_dev, &sequence);
printk("Reading ADC %u : %i : %i\n", 0, ret, sampleBuffer[0]);

I have initialized the channels like this:

adc_dev = device_get_binding("ADC_0");
if (!adc_dev)
{
    printk("Failed to bind ADC\n");
}

static struct adc_channel_cfg adc_channel_cfg = {
    .gain = ADC_GAIN_1_6,
    .reference = ADC_REF_INTERNAL,
    .acquisition_time = ADC_ACQ_TIME_DEFAULT,
    .channel_id = 0,
};

printk("Setting up ADC channel %u: %i...\n", 0, adc_channel_setup(adc_dev, &adc_channel_cfg));

And my prj.conf contains

CONFIG_ADC=y

Related