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

What is the purpose of nrf_drv_saadc_buffer_convert()?

Hi everyone,

I am looking at the SAADC driver and I am struggling to understand the purpose of the nrf_drv_saadc_buffer_convert(). I have read that this is a non-blocking function and is related to conversion in channels..but.. what does it convert? How should I select the buffer size?

How does nrf_drv_saadc_buffer_convert is related with the nrf_drv_saadc_sample?

Thanks in advance

Nick

  • Hello Nick,

    nrf_drv_saadc_buffer_convert() sets up the SAADC for sampling, designating the buffers in which to fill the samples produces by nrf_drv_saadc_sample or NRF_SAADC_TASK_SAMPLE.
    So, in the default use case you would call buffer_convert during your SAADC init, and then call saadc_sample manually, or set up PPI with a timer and NRF_SAADC_TASK_SAMPLE - to actually perform the sampling.
    An example of the latter - using PPI and TASK_SAMPLE - is demonstrated in the SAADC example.

    How should I select the buffer size?

    That depends on your use case. The DONE event will be generated when the buffer is filled up, which could be useful to you for processing the sampled data. This size of the buffer is passed as the second argument to nrf_drv_saadc_buffer_convert.

    Does this make things more clear?
    Do not hesitate to let me know if you have any other questions or issues with this! :) 

    Best regards,
    Karl

  • Hi Karl and thank you for your reply... After reading the PPI, TImer and SAADC peripherals I have a clearer view. 

    Looking at the SAADC example I understood how to continuous sampling by connecting a timer event to SAADC's TASK_SAMPLE via PPI. You actually set a timer in comparing mode and when timer's value is equal to the CC register's value, a timer event triggers the TASK_SAMPLE via PPI isn't it?

    After each TASK_SAMPLE the ADC value is stored into the buffer that we have specified its size. When the buffer is filled up, a DONE event is generated (as you already mentioned).

    I do not understand two things from the SAADC example:

    1. I cannot see where the saadc_callback function is being called form. I can see that is being called when the buffer is filled but I cannot see how... It is been called only when then DONE event generated? If yes, how is been called?

    2. Why the nrf_drv_saadc_buffer_convert() function is been called again inside the saadc_callback(); You said that the nrf_drv_saadc_buffer_convert() sets up the SAADC for sampling and I should call it during the SAADC init

    Thanks in advance

    Nick

  • Hello Nick,

    Nikosant03 said:
    Hi Karl and thank you for your reply... After reading the PPI, TImer and SAADC peripherals I have a clearer view. 

     I am happy to hear that!

    Nikosant03 said:

    Looking at the SAADC example I understood how to continuous sampling by connecting a timer event to SAADC's TASK_SAMPLE via PPI. You actually set a timer in comparing mode and when timer's value is equal to the CC register's value, a timer event triggers the TASK_SAMPLE via PPI isn't it?

    After each TASK_SAMPLE the ADC value is stored into the buffer that we have specified its size. When the buffer is filled up, a DONE event is generated (as you already mentioned).

    Yes, this is correct.

    Nikosant03 said:
    1. I cannot see where the saadc_callback function is being called form. I can see that is being called when the buffer is filled but I cannot see how... It is been called only when then DONE event generated? If yes, how is been called?

    The saadc_callback is provided to the saadc_init function. You can not init the saadc without telling it how to handle its events.
    Peripheral events are HW interrupts, which trigger their respective saadc_callback functions.
    Does this make things more clear?

    Nikosant03 said:
    2. Why the nrf_drv_saadc_buffer_convert() function is been called again inside the saadc_callback(); You said that the nrf_drv_saadc_buffer_convert() sets up the SAADC for sampling and I should call it during the SAADC init

    Well yes, perhaps my answer was a bit ambiguous.

    The SAADC example uses double buffering in its SAADC demonstration. Double buffering is used to immediately switch over to a new buffer, upon reaching the end of the first specified buffer, and vice versa.
    By doing this, you avoid loosing samples in between readying your buffers for sampling(ie. when you are processing the buffers contents).
    When using double buffering, the buffer_convert() function will be called in the event handler, readying the next buffer for the SAADC to begin filling.

    An exempt from the SAADC peripheral documentation reads:

    The driver supports double buffering, which means that nrf_drv_saadc_buffer_convert can be called twice and the buffer that is provided in the second call will be used immediately after the first one is filled.

    Continuous conversion can be achieved by setting up two buffers and calling nrf_drv_saadc_buffer_convert again in the event handler to switch between them later.

    Nikosant03 said:
    Thanks in advance

    No problem at all Nick, I am happy to help!

    Best regards,
    Karl

Related