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

SAADC oversampling callback is not working as expected

Hello All,

I have referred the reference example code to suit my needs. Below is my code,

saadc_evnt_done_flag = false;

saadc_init();

Periodic_func()
{
    saadc_sampling_trigger();
    while(!saadc_evnt_done_flag)

    {
        __WFE();
    }

    /* Some code to use the oversampled value */
}

void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
    if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
    {
        ret_code_t err_code;

        saadc_evnt_done_flag = true;

        m_adc_evt_counter++;
        printf("saadc_callback: adc-m_adc_evt_counter = %d\r\n", m_adc_evt_counter);
        err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
    }
}

Code is stuck at waiting for event. Didn't get callback with oversamping of 4x.

But in the same code, if I add 1 sec delay, callback is getting triggered after some time delay. Can someone help here.

Parents
  • Hello,

    I am still facing the same issue, I have slightly modified the code to get the callback

    But now, once the samplE_trigger called, more time is taken to get the callback called ~10sec. Below is our setting and code. Please help to understand what we are missing here,

    #define SAADC_CONFIG_RESOLUTION      NRF_SAADC_RESOLUTION_14BIT
    #define SAADC_CONFIG_OVERSAMPLE      NRF_SAADC_OVERSAMPLE_4x
    #define SAADC_CONFIG_IRQ_PRIORITY    APP_IRQ_PRIORITY_LOW


    uint8_t adc_init_flag = false;
    static uint8_t count_trig = 0;
    static uint8_t count_cb = 0;

    Periodic_function()
    {
    if(adc_init_flag == false){
    saadc_init();
    adc_init_flag = true;
    }
    saadc_sampling_trigger();
    }

    #define SAMPLES_IN_BUFFER 1

    static nrf_saadc_value_t m_buffer_pool[2][SAMPLES_IN_BUFFER];

    void saadc_callback_1(nrf_drv_saadc_evt_t const * p_event)
    {
    count_trig ++;
    if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
    {
    int avg_sample = 0;
    int i;
    ret_code_t err_code;

    err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
    avg_sample = p_event->data.done.p_buffer[0];
    printf("avg_sample = %d\n",avg_sample);
    APP_ERROR_CHECK(err_code);
    }
    }
    void saadc_init(void)
    {
    ret_code_t err_code;
    nrf_saadc_channel_config_t channel_config =
    NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN5);
    err_code = nrf_drv_saadc_init(NULL, saadc_callback_1);
    APP_ERROR_CHECK(err_code);
    err_code = nrf_drv_saadc_channel_init(0, &channel_config);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAMPLES_IN_BUFFER);
    APP_ERROR_CHECK(err_code);
    }

    void saadc_sampling_trigger(void)
    {
    count_trig++;
    uint8_t err_code;
    //Event handler is called immediately after conversion is finished.
    err_code = nrf_drv_saadc_sample(); // Check error
    APP_ERROR_CHECK(err_code);
    }

    In above,
    1. Periodic function is getting called every 10sec to fetch ADC oversmapled value of 4x averaged.
    2. But we see for every 2 saadc_sampling_trigger() call, callback is getting called only once. which too at a delay of ~10sec.


    We couldn't see any issue in the code as we have referred many forum discussions and example code. Can someone help here to understand the behavior?

    Is there timers involved with oversmaping? Any issue might be with timing?
  • Hi,

    I do not have a full overview, but I notice a few things:

    • count_trig is used in several event handlers. This needs to be volatile in order to ensure that the compiler does not do some optimizations that causes problems here.
    • (You have two sample buffers so you could alternate between them, but you only use on all the time. This is just a detail though, as you have a sampling interval of 10 seconds using a single buffer is no problem, but it does not look clean.)

    None of these explain the issue, though, unless via some code that is not part of what you have posted. Can you upload a complete project (not just snippet, but full source code and project files) that reproduce this so that I can test on my side? There must be some simple detail that is obvious once found, but as of now I am not able to see anything in the code you posted here that would explain the behavior you are seeing.

Reply
  • Hi,

    I do not have a full overview, but I notice a few things:

    • count_trig is used in several event handlers. This needs to be volatile in order to ensure that the compiler does not do some optimizations that causes problems here.
    • (You have two sample buffers so you could alternate between them, but you only use on all the time. This is just a detail though, as you have a sampling interval of 10 seconds using a single buffer is no problem, but it does not look clean.)

    None of these explain the issue, though, unless via some code that is not part of what you have posted. Can you upload a complete project (not just snippet, but full source code and project files) that reproduce this so that I can test on my side? There must be some simple detail that is obvious once found, but as of now I am not able to see anything in the code you posted here that would explain the behavior you are seeing.

Children
No Data
Related