Error -11 when doing ADC read

I'm trying to do an ADC measurement on AIN6 on an nRF52832.  This is how I have things set up:

proj.conf:

#ADC/Comparator
CONFIG_ADC=y
CONFIG_NRFX_COMP=y

Overlay file:

/ {

zephyr,user {
    io-channels = <&adc 6>;
};

&adc {
    #address-cells = <1>;
    #size-cells = <0>;

    channel@6 {
        reg = <6>;
        zephyr,gain = "ADC_GAIN_1_6";
        zephyr,reference = "ADC_REF_INTERNAL";
        zephyr,acquisition-time = <ADC_ACQ_TIME_DEFAULT>;
        zephyr,input-positive = <NRF_SAADC_AIN6>; /* P0.30 */
        zephyr,resolution = <12>;
        zephyr,oversampling = <4>;
    };
};

ADC initialisation (this gets called on power up of the chip)

void adc_init (void)
{
    int8_t ret;

    if (!device_is_ready(adc_chan6.dev)) {
		LOG_ERR("ADC device not found");
		return;
	}

	// Setup ADC
	ret = adc_channel_setup_dt(&adc_chan6);
    if (ret < 0) {
		LOG_ERR("ADC channel set up failed");
		return;
	}

	ret = adc_sequence_init_dt(&adc_chan6, &sequence);
    if (ret < 0) {
		LOG_ERR("ADC channel sequence init failed");
		return;
	}
}

And firmware:

const struct adc_dt_spec adc_chan6 = ADC_DT_SPEC_GET_BY_IDX(DT_PATH(zephyr_user), 0);

struct adc_sequence sequence = {
	.buffer      = &sample_buffer,
	.buffer_size = sizeof(sample_buffer),
};


void counter_callback(const struct device *dev, void *user_data)
{
	if (sample_count < MAX_NUM_SAMPLES) {
		err = adc_read(adc_chan6.dev, &sequence);
		if (err < 0) {
			LOG_ERR("Could not read (%d)", err);
		}
		adc_samples[sample_count] = sample_buffer;
		sample_count++;
	}
	else {
		sampling_complete = true;
	}
}

I've got a counter set up to trigger an ADC read every ~ 300usec, and I take 32 samples.  Problem I am having is that the ADC read is returning error -11.  According to errno.h, this equates to EAGAIN, and there isn't any explanation on what that actually means, so I'm not exactly sure what I need to be doing to fix it.

Can anyone explain what this error is indicating?  And is there anything in my ADC setup that would indicate why I'm gettting this error?

Cheers,

Mike

  • Hi,

    Mike Austin (LPI) said:
    If I enable the LOG messages in the saadc_handler, to show whenever the  NRFX_SAADC_EVT_DONE or NRFX_SAADC_EVT_CALIBRATEDONE events are received, I notice that I am getting a NRFX_SAADC_EVT_CALIBRATEDONE event for every NRFX_SAADC_EVT_DONE.  I can't understand why that would be.  Does the nrfx_saadc_mode_trigger() API automatically do a calibration each time?

    no, the calibration shouldn't be triggered unless explicitly started. Have you checked that there isnt anything else in your application that is triggering the calibration? Try setting a breakpoint everywhere your start calibration and see if the program hits the breakpoint unexpectedly. 

    Mike Austin (LPI) said:
    Trying to set the conversion time to anything less than 10usec seems to stop things working.  Again, not sure why.  At this stage, a 10usec conversion is OK, but I may need to drop this down to 5usec for the next part of the firmware development.

    What happens when you set it to less than 10 µs? Any error code returned, or does it return erronus results from sampling?

    Mike Austin (LPI) said:
    The next thing I need to implement is once I've performed my "once every 3 seconds" ADC measurement, I need to set things up so that the SAADC is constantly sampling and will trigger an NRFX_SAADC_EVT_LIMIT event, so that I can capture any situtation where the ADC pin I'm monitoring goes outside my limits.  Not sure if I can do this with the simple_non_blocking setup I have - I believe I have to go to the advanced version and have dual buffers so I can constantly capture samples.  Does that sound right to you?

    Limits should be supported in simple using nrfx_saadc_limits_set():

    regards

    Jared

Related