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

Configuring SAADC on NRF9160 using the lower level driver instead of Zephyr

I've gotten the SAADC working in a blocking fashion and been able to evaluate sample times. Now, I'd like to set it up to automatically read three inputs in a row to see how fast I can get three reads in a row. Something isn't right. I don't ever see the callback function getting called. I know that I'm calling the nrfx_saadc_sample() function as I see my debug output. 

/*
 * Copyright (c) 2018 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
 */

#include <gpio.h>
#include <net/socket.h>
#include <nrf9160.h>
#include <stdio.h>
#include <string.h>
#include <uart.h>
#include <adc.h>
#include <zephyr.h>
#include <nrfx.h>
#include <nrfx_saadc.h>
#include <misc/printk.h>
//#include <hal/nrf_saadc.h>
//#include <nrfx_dppi.h> 


struct device *gpio_dev;

#define SAADC_DEFAULT_CONFIG                                               \
{                                                                               \
    .resolution         = (nrf_saadc_resolution_t)NRFX_SAADC_CONFIG_RESOLUTION, \
    .oversample         = (nrf_saadc_oversample_t)NRFX_SAADC_CONFIG_OVERSAMPLE, \
    .interrupt_priority = NRFX_SAADC_CONFIG_IRQ_PRIORITY,                       \
    .low_power_mode     = NRFX_SAADC_CONFIG_LP_MODE                             \
}

#define SAADC_DEFAULT_CHANNEL_CONFIG_SE(PIN_P) \
{                                                   \
    .resistor_p = NRF_SAADC_RESISTOR_DISABLED,      \
    .resistor_n = NRF_SAADC_RESISTOR_DISABLED,      \
    .gain       = NRF_SAADC_GAIN1_6,                \
    .reference  = NRF_SAADC_REFERENCE_INTERNAL,     \
    .acq_time   = NRF_SAADC_ACQTIME_3US,           \
    .mode       = NRF_SAADC_MODE_SINGLE_ENDED,      \
    .burst      = NRF_SAADC_BURST_DISABLED,         \
    .pin_p      = (nrf_saadc_input_t)(PIN_P),       \
    .pin_n      = NRF_SAADC_INPUT_DISABLED          \
}

nrf_saadc_channel_config_t saadc_channel_config_1 = SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
nrf_saadc_channel_config_t saadc_channel_config_2 = SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1);
nrf_saadc_channel_config_t saadc_channel_config_3 = SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN2);
static bool adcCalibrateDone;


#define BUFFER_SIZE 3
static nrf_saadc_value_t m_sample_buffer[BUFFER_SIZE];

static void saadc_callback(nrfx_saadc_evt_t const * p_event){

    if ( p_event->type == NRFX_SAADC_EVT_DONE )
    {
      gpio_pin_write(gpio_dev, 10, 0);
       printk("ADC raw value: %d\n", m_sample_buffer[0]);
    }
    else if( p_event->type == NRFX_SAADC_EVT_CALIBRATEDONE )
    {
      adcCalibrateDone = true;
    }
}

void adc_module_init(void)
{
    nrfx_err_t err_code;
    nrfx_saadc_config_t drv_saadc_cfg = SAADC_DEFAULT_CONFIG;
		
    err_code = nrfx_saadc_init( &drv_saadc_cfg, saadc_callback );
    if(err_code != NRFX_SUCCESS){
        //Blah error
    }

    err_code = nrfx_saadc_channel_init(0, &saadc_channel_config_1);
    err_code = nrfx_saadc_channel_init(1, &saadc_channel_config_2);
    err_code = nrfx_saadc_channel_init(2, &saadc_channel_config_3);
	
}

static int adc_sample_blocking(void)
{
	int ret;

        gpio_pin_write(gpio_dev, 10, 1);
	ret = nrfx_saadc_sample_convert(0, m_sample_buffer);
        ret = nrfx_saadc_sample_convert(1, m_sample_buffer);
        ret = nrfx_saadc_sample_convert(2, m_sample_buffer);
        gpio_pin_write(gpio_dev, 10, 0);

        if (ret != NRFX_SUCCESS) {
          printk("Error in adc sampling: %d\n", ret);
        }

	/* Print the AIN0 values */
	for (int i = 0; i < BUFFER_SIZE; i++) {
		float adc_voltage = 0;
		adc_voltage = (float)(((float)m_sample_buffer[i] / 1023.0f) *
				      3600.0f);
		printk("ADC raw value: %d\n", m_sample_buffer[i]);
		printf("Measured voltage: %f mV\n", adc_voltage);
	}

	return ret;
}


static int adc_sample_nonblocking(void)
{
	int ret;

        gpio_pin_write(gpio_dev, 10, 1);
        nrfx_saadc_buffer_convert(m_sample_buffer, 3);
        ret = nrfx_saadc_sample();

        if(ret == NRFX_SUCCESS){
          printk("Sampling Triggered!\n");
        }

        return ret;

}

int main(void)
{
	nrfx_err_t err;

	printk("nrf91 saadc sampling AIN0 (P0.13)\n");
	printk("Example requires secure_boot to have ");
	printk("SAADC set to non-secure!\n");
	printk("If not; BusFault/UsageFault will be triggered\n");

        gpio_dev = device_get_binding(DT_GPIO_P0_DEV_NAME);

        if (!gpio_dev) {
		printk("Cannot bind gpio device");
		return -ENODEV;
	}

        err = gpio_pin_configure(gpio_dev, 10, GPIO_DIR_OUT);//Set pin 0 to output

        adc_module_init();
    

	while (1) {

          //adc_sample_blocking();
          adc_sample_nonblocking();
          
          k_sleep(500);

	}
}

Related