ADC configuration for nRF5340DK SDK Connect SES

I need twoADC channels. I tried to configure using :

CONFIG_ADC= y in prj file,

&adc{
#io-channel-cells = < 0x2 >;
};   in overlay file .. the compile error appears as shown in atteched iamge.

Gulzar Singh

Parents
  • Hi, I tested the program in the attached file. AN0 and AN1 channels configured to sample two anlaog Inputs. The AN0 channel is sampled very well and the ADC result is avaialbale at index 0 of the buffer. The result of AN1 should be available at index 1 of buffer, but remains zero. Please, have a look at the code. If, you test it with any variable voltage at AN0 and AN1, will be better to verify.

    #include <zephyr.h>
    #include <device.h>
    #include <drivers/adc.h>
    #include <hal/nrf_saadc.h>
    //*********************
    #define ADC_DEVICE_NAME DT_LABEL(DT_INST(0, nordic_nrf_saadc))
    #define ADC_RESOLUTION 10
    #define ADC_GAIN ADC_GAIN_1_6
    #define ADC_REFERENCE ADC_REF_INTERNAL
    #define ADC_ACQUISITION_TIME ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 10)
    #define ADC_1ST_CHANNEL_ID 0  
    #define ADC_1ST_CHANNEL_INPUT NRF_SAADC_INPUT_AIN0
    #define ADC_2ND_CHANNEL_ID 1
    #define ADC_2ND_CHANNEL_INPUT NRF_SAADC_INPUT_AIN1
    
    
    const struct device *adc_dev;
    
    static const struct adc_channel_cfg m_1st_channel_cfg = {
    	.gain = ADC_GAIN,
    	.reference = ADC_REFERENCE,
    	.acquisition_time = ADC_ACQUISITION_TIME,
    	.channel_id = ADC_1ST_CHANNEL_ID,
    #if defined(CONFIG_ADC_CONFIGURABLE_INPUTS)
    	.input_positive = ADC_1ST_CHANNEL_INPUT,
    #endif
    };
    
    static const struct adc_channel_cfg m_2nd_channel_cfg = {
    	.gain = ADC_GAIN,
    	.reference = ADC_REFERENCE,
    	.acquisition_time = ADC_ACQUISITION_TIME,
    	.channel_id = ADC_2ND_CHANNEL_ID,
    #if defined(CONFIG_ADC_CONFIGURABLE_INPUTS)
    	.input_positive = ADC_2ND_CHANNEL_INPUT,
    #endif
    };
    
    #define  BUFFER_SIZE 2
    int err;
    static int p_sample_buffer[BUFFER_SIZE];
    //*******************************************
    //****************
    static int adc_sample1(void)
    {
    	int ret;
    
    	const struct adc_sequence sequence = {
    		.channels = BIT(ADC_1ST_CHANNEL_ID),
    		.buffer = p_sample_buffer,
    		.buffer_size = sizeof(p_sample_buffer),
    		.resolution = ADC_RESOLUTION,
    	};
    
    	if (!adc_dev) {
    		return -1;
    	}
    
    	ret = adc_read(adc_dev, &sequence);
    	if (ret) {
    //        printk("adc_read() failed with code %d\n", ret);
    	}
    
    	for (int i = 0; i < BUFFER_SIZE; i++) {
    //                printk("ADC raw value: %d\n", p_sample_buffer[i]);                
            }
    	return ret;
    }
    
    static int adc_sample2(void)
    {
    	int ret;
    
    	const struct adc_sequence sequence = {
    		.channels = BIT(ADC_2ND_CHANNEL_ID),
    		.buffer = p_sample_buffer,
    		.buffer_size = sizeof(p_sample_buffer),
    		.resolution = ADC_RESOLUTION,
    	};
    
    	if (!adc_dev) {
    		return -1;
    	}
    
    	ret = adc_read(adc_dev, &sequence);
    	if (ret) {
    //        printk("adc_read() failed with code %d\n", ret);
    	}
    
    	for (int i = 0; i < BUFFER_SIZE; i++) {
    //                printk("ADC raw value: %d\n", p_sample_buffer[i]);                
            }
    	return ret;
    }
    //********************************************************
    void main()
    {
              //ADC setup
              adc_dev = device_get_binding(ADC_DEVICE_NAME);
                  if (!adc_dev) {
                  printk("device_get_binding ADC (=%s) failed\n", ADC_DEVICE_NAME);
              } 
        
              err = adc_channel_setup(adc_dev, &m_1st_channel_cfg);
              if (err) {
                      printk("Error in adc AIN0 setup: %d\n", err);
                  }
    
              err = adc_channel_setup(adc_dev, &m_2nd_channel_cfg);
              if (err) {
                      printk("Error in adc AIN1 setup: %d\n", err);
                  }
    
              #if defined (CONFIG_BOARD_BMS_NRF5340_CPUAPP)
              NRF_SAADC->TASKS_CALIBRATEOFFSET = 1;
              #else
              #error "Choose supported board or add new board for the application"
              #endif
    //***********************
    while(1)
    {int arr, var1,var2;
      err = adc_sample1();
      var1 = p_sample_buffer[0];
      err = adc_sample2();
      var2 = p_sample_buffer[1];
    }
    }
    
    

  • Hello Gulzar,

    Sorry for the late reply; I was in sick leave.

    How does your overlay file look like? 

    / {

    zephyr,user {

    /* adjust channel number according to pinmux in board.dts */

    io-channels = <&adc 0>, <&adc 1>;

    };

    };

    For these channels to be on AN0 and AN1, the above overlay needs to be added.
    Best Regards,
    Kazi Afroza Sultana
Reply Children
No Data
Related