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

Manually triggering SAADC - which to use?

I want to trigger SAADC samples manually, when a certain BLE message arrives.  I'd ideally like to have a trigger scan 4 individual channels.

I see two function calls, though.  Here are their descriptions:

  • nrfx_saadc_mode_trigger() - Function for triggering the conversion in the configured mode.
  • nrfx_saadc_sample() - Function for starting the SAADC sampling.

Then, to make things even more confusing, there's this:

  • nrf_drv_saadc_buffer_convert() - The function nrf_drv_saadc_buffer_convert() can be used to start the conversion in non-blocking mode

I'm using S140, and now I'm not sure which one of those is correct.  This API is just confusing the hell out of me.My best guess here is as follows, maybe somebody will be kind enough to tell me if this is right.  The idea here comes from the nordic playground on github.  I initialize things like this (I edited the code to make it more readable, which is why you don't see any error checks or anything)

/* This is 4 because we're sampling 4 channels */
#define SAADC_SAMPLES_IN_BUFFER 4

/* I don't think this is used because I'm manually triggering */
#define SAADC_SAMPLE_RATE               250                                         /**< SAADC sample rate in ms. */               

/*
 * The [2] here is because we're setting up two buffers, each being
 * numChannels long - i.e. it's double buffered
 */
static nrf_saadc_value_t     m_buffer_pool[2][SAADC_SAMPLES_IN_BUFFER];


...
    /* Initialize the SAADC driver */
    err_code = nrf_drv_saadc_init(&saadc_config, saadc_callback);

    /*
     * Initialize each individual channel using the channel_x_config
     * structs have already been set up
     */
    err_code = nrf_drv_saadc_channel_init(0, &channel_0_config);
    err_code = nrf_drv_saadc_channel_init(1, &channel_1_config);
    err_code = nrf_drv_saadc_channel_init(2, &channel_2_config);
    err_code = nrf_drv_saadc_channel_init(3, &channel_3_config);

    /*
     * What is this doing exactly?  I envision that it's taking some
     * memory buffers that belong to me - let's call it "user space" -
     * and handing them over to the saadc driver, maybe by putting
     * them into some kind of queue
     */
    err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0],SAADC_SAMPLES_IN_BUFFER);
    err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1],SAADC_SAMPLES_IN_BUFFER);

where SAMPLES_IN_BUFFER is 4 beacuse I'm scanning 4 channels, and [2] is because it's somehow double buffering.  With manual triggering, I'm not really sure that's necessary, though, is it?

I think, then, when I want it to fire, I call nrfx_saadc_mode_trigger() which has the effect of triggering it ONCE.  It will then call the saadc_callback that I specified.  That callback doesn't have to do anything except read the data out of the buffer.  Or does it?  Does the callback have to call nrf_drv_saadc_buffer_convert() again?  The init code called nrf_drv_saadc_buffer_convert() and I'm not sure why.  I'm not sure if you have to do that before starting it, or only before you start processing the data.  If it "starts the conversion" as the description says then it wouldn't make sense to call it twice.

Parents
  • I see two function calls, though.  Here are their descriptions:

    • nrfx_saadc_mode_trigger() - Function for triggering the conversion in the configured mode.
    • nrfx_saadc_sample() - Function for starting the SAADC sampling.

     use nrfx_saadc_sample.  

    Then, to make things even more confusing, there's this:

    • nrf_drv_saadc_buffer_convert() - The function nrf_drv_saadc_buffer_convert() can be used to start the conversion in non-blocking mode

     It prepares the next buffer by writing to the RESULT.PTR and RESULT.MAXCNT registers and then triggering a START task. It does not start sampling however. 


     

    where SAMPLES_IN_BUFFER is 4 beacuse I'm scanning 4 channels, and [2] is because it's somehow double buffering.  With manual triggering, I'm not really sure that's necessary, though, is it?

     If you trigger another SAMPLE task before you have process your buffer you will override it, double-buffering prevents that. Whether you need it or not is up to you to decide.

Reply
  • I see two function calls, though.  Here are their descriptions:

    • nrfx_saadc_mode_trigger() - Function for triggering the conversion in the configured mode.
    • nrfx_saadc_sample() - Function for starting the SAADC sampling.

     use nrfx_saadc_sample.  

    Then, to make things even more confusing, there's this:

    • nrf_drv_saadc_buffer_convert() - The function nrf_drv_saadc_buffer_convert() can be used to start the conversion in non-blocking mode

     It prepares the next buffer by writing to the RESULT.PTR and RESULT.MAXCNT registers and then triggering a START task. It does not start sampling however. 


     

    where SAMPLES_IN_BUFFER is 4 beacuse I'm scanning 4 channels, and [2] is because it's somehow double buffering.  With manual triggering, I'm not really sure that's necessary, though, is it?

     If you trigger another SAMPLE task before you have process your buffer you will override it, double-buffering prevents that. Whether you need it or not is up to you to decide.

Children
No Data
Related