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
  • So if I ignore the invalid state then during the timer handler, I'm not yet able to see any sampling come through i.e. I don't see an NRFX_SAADC_EVT_DONE raised. Anything else obviously missing from my setup?

    Related: Should I avoid the V2 API given the lack of a good example to guide me? As I'm developing a new device, I'd like to adopt the most contemporary APIs, but perhaps I'm jumping the gun on this one...

    Thanks.

  • Hello Christopher,

    Christopher Hunt said:
    Thanks for the reply, Karl.

    No problem at all, I am happy to help!

    Christopher Hunt said:
    Gosh, I missed that description of the return code having even inspected the source! So, I should ignore the invalid state code perhaps during my timer event, as it is quite legitimate to be in this invalid state?

    I am not sure that I understand exactly what you mean by this, but if this error code is returned then that means the peripheral indeed is in an invalid state for beginning sampling. If you just ignore this error, then you will "lose" this sample ( i.e no sample will be taken at this time, causing a gap in your sampling ).

    Christopher Hunt said:
    So if I ignore the invalid state then during the timer handler, I'm not yet able to see any sampling come through i.e. I don't see an NRFX_SAADC_EVT_DONE raised.

    I would say this is as expected - if your buffer size is 3, and you attempt to do 3 calls to _sample, with 1 or more returning an error code != NRF_SUCCESS, then the buffer will not fill, and the DONE event thus will not be generated.

    Christopher Hunt said:
    The timer is firing once per 100ms i.e. 10 times per second.

    Thank you for clarifying.
    Could you show me the code in which you setup and start this timer? I just want to rule out that this in fact is happening at a different interval.

    Christopher Hunt said:
    Related: Should I avoid the V2 API given the lack of a good example to guide me? As I'm developing a new device, I'd like to adopt the most contemporary APIs, but perhaps I'm jumping the gun on this one...

    While not in the SDK, there exists some short examples to demonstrate the usage of the V2 driver, please see the SAADC section of the nrfx changelog for this. In this case, the Simple mode with IRQ example might be especially useful to take a look at.
    It is hard for me to advise on whether you could stick to the old SAADC driver, or should make the change to V2 since I do not know about the specifics of your project, but I definitely recommend getting familiar with the V2, as it is a improvement of the old driver. You might also want to consider having your sampling trigger by PPI, instead of by CPU intervention - especially if it is important that sampling is not interrupted or delayed due to the SoftDevice controlling the CPU.

    Please do not hesitate to ask if anything should be unclear, or if you should encounter any issues or questions! :) 

    Best regards,
    Karl

  • Just digesting the other bits, but here's my timer code. I can confirm that the timer is firing.

    Meanwhile, to declare the timer:

    APP_TIMER_DEF(m_hand_sanitizer_accel_sampler_timer_id);
    

    To create the timer:

        err_code = app_timer_create(&m_hand_sanitizer_accel_sampler_timer_id, APP_TIMER_MODE_REPEATED, hand_sanitizer_accel_sampler_timer);
        APP_ERROR_CHECK(err_code);

    To enable the timer:

        err_code = app_timer_start(m_hand_sanitizer_accel_sampler_timer_id, APP_TIMER_TICKS(ACCEL_SAMPLER_RESOLUTION_MS), NULL);
        APP_ERROR_CHECK(err_code);

    To handler the timer:

    static void hand_sanitizer_accel_sampler_timer(void *p_context)
    {
        nrfx_saadc_mode_trigger();
    }
    

  • You might also want to consider having your sampling trigger by PPI, instead of by CPU intervention - especially if it is important that sampling is not interrupted or delayed due to the SoftDevice controlling the CPU.

    I've been thinking more about this since you suggested it. Here's my use case: I've got a 3 axis analog accelerometer and want to detect if a "significant" force occurs on any one of its three axis points. Each axis value is supplied to an analog pin. The accelerometer reports up to 3G of force, so given 8-bit sampling, a sample value for an axis of 128 or more indicates 1.5G or greater.

    It'd be great to have PPI generate a sampling task for the SAADC if a value on any one of the three axis points exceeds 128. Reading (and re-reading!) the doc seems to indicate that there's an "EVENTS_CH[0].LIMITH" event that can be monitored. I've been looking for examples of how to use PPI specifically for the limit test, but I've not yet found any. It'd be great if you could point me in the right direction. My understanding so far is that I must use nrfx_ppi_channel_alloc, nrfx_ppi_channel_assign, and then nrf_drv_ppi_channel_enable. I'm thinking that I'll also have to set "start_on_end" for the SAADC advanced config and have a buffer sample size of 3; one for each channel. Again, thanks for any pointers here.

  • Here's my PPI/SAADC code so far in case it helps (it isn't fully working though...):

    /**@brief Handle analog events
     */
    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:
            // Do something with the data
            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;
        }
    }
    
    /**@brief Set up our analogue sensors
     */
    static void saadc_init()
    {
        ret_code_t err_code;
        nrf_saadc_event_t ppi_event;
    
        nrfx_saadc_adv_config_t config = {
            .oversampling = NRF_SAADC_OVERSAMPLE_DISABLED,
            .burst = NRF_SAADC_BURST_DISABLED,
            .internal_timer_cc = 0,
            .start_on_end = true,
        };
    
        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);
    
        for (int i = 0; i < 3; ++i)
        {
            err_code = nrfx_ppi_channel_alloc(&m_accel_ppi_channel[i]);
            APP_ERROR_CHECK(err_code);
            nrf_saadc_channel_limits_set(i, 0, 128);
            switch (i)
            {
            case 0:
                ppi_event = NRF_SAADC_EVENT_CH0_LIMITH;
            case 1:
                ppi_event = NRF_SAADC_EVENT_CH1_LIMITH;
            case 2:
                ppi_event = NRF_SAADC_EVENT_CH2_LIMITH;
            }
            err_code = nrfx_ppi_channel_assign(m_accel_ppi_channel[i],
                                               nrf_saadc_event_address_get(ppi_event),
                                               nrf_saadc_task_address_get(NRF_SAADC_TASK_SAMPLE));
            APP_ERROR_CHECK(err_code);
            err_code = nrfx_ppi_channel_enable(m_accel_ppi_channel[i]);
            APP_ERROR_CHECK(err_code);
        }
    
        err_code = nrfx_saadc_mode_trigger();
        APP_ERROR_CHECK(err_code);
    }

  • Hello Christopher,

    Christopher Hunt said:
    Just digesting the other bits, but here's my timer code. I can confirm that the timer is firing.

    No problem - thank you for providing the timer code.
    The general approach seems alright, but bear in mind that I do not know the values of the arguments passed to the different functions.

    Christopher Hunt said:
    I've been thinking more about this since you suggested it. Here's my use case: I've got a 3 axis analog accelerometer and want to detect if a "significant" force occurs on any one of its three axis points. Each axis value is supplied to an analog pin. The accelerometer reports up to 3G of force, so given 8-bit sampling, a sample value for an axis of 128 or more indicates 1.5G or greater.

    Thank you for clarifying - this helps a lot in understanding your issue.

    Christopher Hunt said:
    It'd be great to have PPI generate a sampling task for the SAADC if a value on any one of the three axis points exceeds 128. Reading (and re-reading!) the doc seems to indicate that there's an "EVENTS_CH[0].LIMITH" event that can be monitored. I've been looking for examples of how to use PPI specifically for the limit test, but I've not yet found any. It'd be great if you could point me in the right direction. My understanding so far is that I must use nrfx_ppi_channel_alloc, nrfx_ppi_channel_assign, and then nrf_drv_ppi_channel_enable. I'm thinking that I'll also have to set "start_on_end" for the SAADC advanced config and have a buffer sample size of 3; one for each channel. Again, thanks for any pointers here.

    You are correct that there exists a LIMIT event in the SAADC, which can be generated once a channel exceeds a certain value, please see the Event monitoring using limits section of the SAADC peripheral documentation. However, the limits are not monitored continuously - this would require the SAADC to sample 'continuously' - instead, they are checked against each finished conversion. So, to generate LIMIT events, you will need to be sampling -> You may not use the limit events to begin sampling, because the limits are checked against already acquired samples.
    Does what I have written here make sense? Please do not hesitate to let me know if it is unclear.

    However, what you perhaps could do instead - depending on the constraints and requirements of your application - is to use the LPCOMP peripheral for this.
    Are you familiar with the LPCOMP peripheral?

    You can see the PPI channel allocation - how to configure, setup and enable PPI channels - demonstrated in the SAADC peripheral example from the SDK. In the example the sampling task is connected to the CAPUTRE/COMPARE event of a counter, to have it trigger at a certain interval - without use of the CPU. You can follow the general approach of this example, and just change which event you are connecting the sampling task to.

    Best regards,
    Karl 

  • However, what you perhaps could do instead - depending on the constraints and requirements of your application - is to use the LPCOMP peripheral for this.
    Are you familiar with the LPCOMP peripheral?

    Thanks for the pointer toward LPCOMP - I may well come back to this one.

Reply Children
  • Christopher Hunt said:
    Thanks for the pointer toward LPCOMP - I may well come back to this one.

    No problem at all, I am happy to help!
    I too think that the LPCOMP or COMP peripheral might sound right for your application here ( depending on what real-time and accuracy requirements you are working with ). 
    Please bear in mind that the LPCOMP, COMP and SAADC shares resources, which you will need to keep in mind when working with a combination of these ( LPCOMP and COMP is mutually exclusive, for example ). 

    Please do not hesitate to ask, if you should encounter any issues or questions in the future.

    Good luck with your development!

    Best regards,
    Karl

  • Please do not hesitate to ask, if you should encounter any issues or questions in the future.

    Thanks for everything Karl. I'm still having difficulty understanding why my SAADC sampling fires just once. Any pointers there would be great. I feel like I'm very close, but struggling with this last aspect. Here's my current code again:

    /**@brief Handle analog events
     */
    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:
            // Process the data
            
            // Set the next compare check that'll cause another sample
            next_sample_ticks += ACCEL_SAMPLER_RESOLUTION_TICKS;
            err_code = nrfx_rtc_cc_set(&m_rtc, 0, next_sample_ticks, false);
            APP_ERROR_CHECK(err_code);
            break;
        default:
            break;
        }
    }
    
    static void on_rtc_evt(nrfx_rtc_int_type_t int_type)
    {
    }
    
    /**@brief Set up our analogue sensors
     */
    static void saadc_init()
    {
        ret_code_t err_code;
    
        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_simple_mode_set((1 << 0) | (1 << 1) | (1 << 2),
                                              NRF_SAADC_RESOLUTION_8BIT,
                                              NRF_SAADC_OVERSAMPLE_DISABLED,
                                              on_saadc_in_evt);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrfx_saadc_buffer_set(m_accel_sample_buf, ACCEL_SAMPLE_CHANNELS);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrfx_saadc_mode_trigger();
        APP_ERROR_CHECK(err_code);
    
        nrfx_rtc_config_t rtc_config = NRFX_RTC_DEFAULT_CONFIG;
        err_code = nrfx_rtc_init(&m_rtc, &rtc_config, on_rtc_evt);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrfx_rtc_cc_set(&m_rtc, 0, next_sample_ticks, false);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrfx_ppi_channel_alloc(&m_accel_ppi_channel);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrfx_ppi_channel_assign(m_accel_ppi_channel,
                                           nrfx_rtc_event_address_get(&m_rtc, NRF_RTC_EVENT_COMPARE_0),
                                           nrf_saadc_task_address_get(NRF_SAADC_TASK_SAMPLE));
        APP_ERROR_CHECK(err_code);
        err_code = nrfx_ppi_channel_enable(m_accel_ppi_channel);
        APP_ERROR_CHECK(err_code);
    
        nrfx_rtc_enable(&m_rtc);
    }

  • Thanks for everything Karl. I'm still having difficulty understanding why my SAADC sampling fires just once. Any pointers there would be great. I feel like I'm very close, but struggling with this last aspect. Here's my current code again:

    I think I've now got to the bottom of the problem I had in understanding SAADC. My assumption was that I could drive the nrfx_saadc V2 state machine by firing SAADC tasks. I paired back everything to the Nordic Playground SAADC simple low power app timer example and replaced the app timer with RTC and PPI behaviour. My conclusion is that the NRF_SAADC_TASK_SAMPLE task has no effect when fired from PPI as the nrfx_saadc state machine is not in a "sampling" state. To attain a sampling state, nrfx_saadc_mode_trigger() must be called i.e. it isn't enough to issue a NRF_SAADC_TASK_SAMPLE task, even when nrfx_saadc_mode_trigger() has been called once to kick everything off.

    I've put my fork of the ADC playground low power app with RTC here for reference: https://github.com/huntc/nRF52-ADC-examples/tree/rtc-ppi/nrfx_saadc_simple_low_power_app_timer. To see my specific changes, please see this commit: https://github.com/huntc/nRF52-ADC-examples/commit/d4075a7ce59f840819b642f93c80af308a1e4f12.

    I think I shall now abandon the RTC/PPI driven approach here as it doesn't seem possible. The earlier suggestion of using LPCOMP seems more useful for my particular use-case anyhow. Thanks, and I hope this dialogue has been useful.

Related