Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

SAADC V2 sampling using the RTC

Hi there,

I'm having some trouble piecing together an app to use V2 of the NRFX SAADC API, compounded with this being my first time using the SAADC functionality on an nRF board.

In summary, my program receives an "ERROR 8 [NRF_ERROR_INVALID_STATE]" when calling the nrfx_saadc_mode_trigger function.

My understanding is that we first initialise SAADC:

static void saadc_init()
{
    ret_code_t err_code;

    nrfx_saadc_adv_config_t config = {
        .oversampling = NRF_SAADC_OVERSAMPLE_DISABLED,
        .burst = false,
        .internal_timer_cc = 0,
        .start_on_end = false,
    };

    err_code = nrfx_saadc_init(NRFX_SAADC_CONFIG_IRQ_PRIORITY);
    APP_ERROR_CHECK(err_code);
    err_code = nrfx_saadc_channels_config(m_accel, 3);
    APP_ERROR_CHECK(err_code);

    err_code = nrfx_saadc_advanced_mode_set((1 << 0) | (1 << 1) | (1 << 2),
                                            NRF_SAADC_RESOLUTION_8BIT,
                                            &config,
                                            on_saadc_in_evt);
    APP_ERROR_CHECK(err_code);

    err_code = nrfx_saadc_buffer_set(m_accel_sample_bufs[m_accel_current_buffer], ACCEL_SAMPLES_PER_BUFFER);
    APP_ERROR_CHECK(err_code);
}

My intention is to use the RTC to sample three analogue pins previously defined as:

nrfx_saadc_channel_t m_accel[] = {
    NRFX_SAADC_DEFAULT_CHANNEL_SE(NRF_SAADC_INPUT_AIN5, 0),
    NRFX_SAADC_DEFAULT_CHANNEL_SE(NRF_SAADC_INPUT_AIN6, 1),
    NRFX_SAADC_DEFAULT_CHANNEL_SE(NRF_SAADC_INPUT_AIN7, 2)};

A timer is created and, when it fires, it then triggers the sampling:

static void accel_sampler_timer(void *p_context)
{
    ret_code_t err_code = nrfx_saadc_mode_trigger();
    APP_ERROR_CHECK(err_code);
}

...and that's when the invalid state is reported.

The SAADC event handler is as follows:

static void on_saadc_in_evt(nrfx_saadc_evt_t const *p_event)
{
    ret_code_t err_code;

    switch (p_event->type)
    {
    case NRFX_SAADC_EVT_DONE:
        // TODO: Use p_event->data.done.p_buffer[0];
        break;
    case NRFX_SAADC_EVT_BUF_REQ:
        m_accel_current_buffer = 1 - m_accel_current_buffer;
        err_code = nrfx_saadc_buffer_set(m_accel_sample_bufs[m_accel_current_buffer], ACCEL_SAMPLES_PER_BUFFER);
        APP_ERROR_CHECK(err_code);
        break;
    default:
        break;
    }
}

Am I under the false impression that the timer should trigger the sampling?

Thanks for your guidance.

Parents Reply Children
No Data
Related