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

sampling two analog

Hello

I want to sampling two analog.. with different configuration and handler.

so I set two channels like this.

    nrf_saadc_channel_config_t cds_channel_config =
    NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1);

    err_code = nrf_drv_saadc_init(&cds_saadc_config, cds_saadc_callback);
    if(err_code != NRF_SUCCESS) return 1;

    err_code = nrf_drv_saadc_channel_init(0, &cds_channel_config);
    if(err_code != NRF_SUCCESS) return 1;
    
    ///////////////////////////////// (different function)
    
    nrf_saadc_channel_config_t co_channel_config =
    NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN6);

    err_code = nrf_drv_saadc_init(&co_saadc_config, co_saadc_callback);
    if(err_code != NRF_SUCCESS) return 1;

    err_code = nrf_drv_saadc_channel_init(1, &co_channel_config);
    if(err_code != NRF_SUCCESS) return 1;

and then I call only one sample function like this.

void saadc_process()
{
    //..

    nrf_drv_saadc_sample();
}

Then error occurs. How can I change code?

and is there good example I can refer?

BR,

lyrics

Parents
  • Hi,

    Exactly what error occurs?

    The SAADC periperhal should only be initialized once with the same nrfx_saadc_config_t configuration.

    You can un-initialize it and then re-initialize with a new configuration. Is there any difference between cds_saadc_config and co_saadc_config? 

    The channels can be configured differently so this shouldn't be a problem.

    regards

    Jared 

  • Exactly what error occurs?

    it returns 0x08 error , second initializing.

     err_code = nrf_drv_saadc_init(&co_saadc_config, co_saadc_callback);

    Is there any difference between cds_saadc_config and co_saadc_config? 

    they are different sensor, so each sensor value needs to sampling.

  • But do they require a different configuration of the SAADC peripheral? If yes, exactly what needs to be different? 

     

    lyrics said:
    it returns 0x08 error , second initializing

     Yes, as I suspected, the issue is that you try to initialize the driver after it has already been initialized. You need to un initialize it in between if you want to use a different config for the SAADC driver.

    Error 0x08 = NRF_ERROR_INVALID_STATE

  • But do they require a different configuration of the SAADC peripheral? If yes, exactly what needs to be different? 

    Because I set different resolution value. cds is 8Bit , and co is 10Bit.  Also, I tried to handle each value

    with a handler. (cds_handler and co_handler)

    I don't know the ads functions well, so I just divided them into two.

    Is there a way to separate two channels in one handler? 

  • Hi,

     

    lyrics said:

    Because I set different resolution value. cds is 8Bit , and co is 10Bit.  Also, I tried to handle each value

     Then you need to:

    1. Initialize the SAADC with 8 bit resolution.
    2. Sample 
    3. Un initialize the SAADC
    4. Initialize the SAADC with 10 bit resolution
    5. Sample

    Maybe it would be better to just use 10 bits in both cases, that way you wouldn't need to reinitialize the SAADC peripheral between each sample. You can truncate the 10 bit result to 8 bit in your application instead. 

     

    lyrics said:
    Is there a way to separate two channels in one handler? 

    The samples are taken sequential starting from the lowest channel number. You can print the number of the sample to keep track of which channel it comes from. For example, if you're using two channels then you know that odd numbered samples are from the first channel, and even numbered samples are from the second channel.

    regards

    Jared 

Reply
  • Hi,

     

    lyrics said:

    Because I set different resolution value. cds is 8Bit , and co is 10Bit.  Also, I tried to handle each value

     Then you need to:

    1. Initialize the SAADC with 8 bit resolution.
    2. Sample 
    3. Un initialize the SAADC
    4. Initialize the SAADC with 10 bit resolution
    5. Sample

    Maybe it would be better to just use 10 bits in both cases, that way you wouldn't need to reinitialize the SAADC peripheral between each sample. You can truncate the 10 bit result to 8 bit in your application instead. 

     

    lyrics said:
    Is there a way to separate two channels in one handler? 

    The samples are taken sequential starting from the lowest channel number. You can print the number of the sample to keep track of which channel it comes from. For example, if you're using two channels then you know that odd numbered samples are from the first channel, and even numbered samples are from the second channel.

    regards

    Jared 

Children
  • Hi,

    Maybe it would be better to just use 10 bits in both cases,

    First of all, I just added a channel and ran the program..

    But error occurs second channel init function.. (nrf_drv_saadc_channel_init)

     I hope there is reference code (example)!

        ret_code_t err_code;
        nrf_drv_saadc_config_t cds_saadc_config;
    
        cds_saadc_config.low_power_mode = false;                                  //Enable low power mode.
        cds_saadc_config.resolution = NRF_SAADC_RESOLUTION_8BIT;                  //Set SAADC resolution to 12-bit. This will make the SAADC output values from 0 (when input voltage is 0V) to 2^12=4096 (when input voltage is 3.6V for channel gain setting of 1/6).
        cds_saadc_config.oversample = NRF_SAADC_OVERSAMPLE_DISABLED;              //Set oversample to 4x. This will make the SAADC output a single averaged value when the SAMPLE task is triggered 4 times.
        cds_saadc_config.interrupt_priority = APP_IRQ_PRIORITY_LOW;               //Set SAADC interrupt to low priority.
    
        nrf_saadc_channel_config_t cds_channel_config =
        NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1);
    
        err_code = nrf_drv_saadc_init(&cds_saadc_config, cds_saadc_callback);
        if(err_code != NRF_SUCCESS) return 1;
    
        err_code = nrf_drv_saadc_channel_init(0, &cds_channel_config);
        if(err_code != NRF_SUCCESS) return 1;
    
        err_code = nrf_drv_saadc_buffer_convert(m_cds_buffer_pool[0], 1);
        APP_ERROR_CHECK(err_code);
        
        err_code = nrf_drv_saadc_buffer_convert(m_cds_buffer_pool[1], 1);
        APP_ERROR_CHECK(err_code);
        
        nrf_saadc_channel_config_t co_channel_config =
        NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN6);
    
        err_code = nrf_drv_saadc_channel_init(1, &co_channel_config );
        APP_ERROR_CHECK(err_code);
        
        err_code = nrf_drv_saadc_buffer_convert(m_co_buffer_pool[0], 1);
        APP_ERROR_CHECK(err_code);
        
        err_code = nrf_drv_saadc_buffer_convert(m_co_buffer_pool[1], 1);
        APP_ERROR_CHECK(err_code);
        
        //CDS_F=1;
        return 0;
    

  • lyrics said:

    But error occurs second channel init function.. (nrf_drv_saadc_channel_init)

     OK, but this is a different function than earlier. What error code is returned?

    regards

    Jared 

  • Hi,

    OK, but this is a different function than earlier. What error code is returned?

    In that case, the err_code is 0x11 (17).

    However, if I move on to the next function with the break point, the error below occurs.

  • Hi, I fixed the issue.. But another question.

    I reference this github code, and followed the code like this.

        nrf_saadc_channel_config_t cds_channel_config =
        NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1);
    
        cds_channel_config.gain = NRF_SAADC_GAIN1;
    
        nrf_saadc_channel_config_t co_channel_config =
        NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN6);
    
        co_channel_config.gain = NRF_SAADC_GAIN1;
    
        err_code = nrf_drv_saadc_init(&cds_saadc_config, cds_saadc_callback);
        if(err_code != NRF_SUCCESS) return 1;
    
        err_code = nrf_drv_saadc_channel_init(0, &cds_channel_config);
        APP_ERROR_CHECK(err_code);
        if(err_code != NRF_SUCCESS) return 1;
    
        err_code = nrf_drv_saadc_channel_init(1, &co_channel_config );
        APP_ERROR_CHECK(err_code);
        //if(err_code != NRF_SUCCESS) return 1;
    
        err_code = nrf_drv_saadc_buffer_convert(m_cds_buffer_pool[0], 1);
        APP_ERROR_CHECK(err_code);
        //if(err_code != NRF_SUCCESS) return 1;
    
        err_code = nrf_drv_saadc_buffer_convert(m_cds_buffer_pool[1], 1);
        APP_ERROR_CHECK(err_code);

    and then it works well, but the result of data are not what I expected.

    So, what is role of SAADC_CH_CONFIG_GAIN_Gain1_4 and  SAADC_CH_CONFIG_REFSEL_VDD1_4 ?

  • lyrics said:
    So, what is role of SAADC_CH_CONFIG_GAIN_Gain1_4 and  SAADC_CH_CONFIG_REFSEL_VDD1_4 ?

    They are the configured Gain and Reference that is used during the sampling. Please read the product specification regarding this topic, and consider making a new ticket for future problems as the original problem has been solved.

    regards

    Jared 

Related