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

DOUBT IN SAADC EXAMPLE FROM SDK V 14.2.0

HI,

       We are developing a 2d -3d  mouse with nRF5_SDK_14.2.0_17b948a  "ble_app_hids_mouse"  application.

  • For knowing the battery discharge rate of our product we are trying to implement adc into our code "modified ble_app_hids_mouse ".  
  • We are using p0.02(AIN0) for measuring  the battery status.
  • Actually our requirement is ,we want to check battery level(i.e adc value) at regular interval of time (may be 1m) and if the battery level is less than 20% we will turn on yellow led and if the battery level is less than 5% the red led will turn on.

For configuring the ADC , I look yours saadc code but i have some doubt in your code .

  • I think  in your saadc code they configured ADC ,TIMER AND PPI.

I will explain the flow of code execution what i understood from your saadc below if there is any mistake please correct me

  • TIMER will generate an event every 400ms .
  • By the help of PPI a task was occurring in SAADC peripheral   due to TIMER event . and its repeating at regular intervals (400 ms)
  1. "saadc_callback" function will execute when there is a TIMER event occur , is it right?
  2. During the initialization part(saadc_init) "nrf_drv_saadc_buffer_convert" function is calling and also inside the  "saadc_callback" function   is "nrf_drv_saadc_buffer_convert" function is calling . I known these function is for  issuing conversion of data to buffer. Then why its calling on both "saadc_init"  and  "saadc_callback" function.
  3. From which buffer we can read the converted analog to digital data ?

please answer to my 1,2 and 3

 if you need any clarification in my question please ask me .

  • Hi,

     

    The saadc example in the SDK is setup using PPI and TIMER to trigger the ADC conversion.

    Since your application relies on sampling the SAADC every 1 minute, you should instead trigger it from a app_timer handler or a similar RTC powered peripheral, as this will ensure low current consumption in sleep. Enable SAADC, wait for the conversion, read out the ADC value, then disable SAADC.

     

    "saadc_callback" function will execute when there is a TIMER event occur , is it right?

    For the "saadc" example; yes.

    During the initialization part(saadc_init) "nrf_drv_saadc_buffer_convert" function is calling and also inside the  "saadc_callback" function   is "nrf_drv_saadc_buffer_convert" function is calling . I known these function is for  issuing conversion of data to buffer. Then why its calling on both "saadc_init"  and  "saadc_callback" function.

    This will set the DMA pointer, as shown in the description of the function call in nrfx_saadc.h:

    /**
     * @brief Function for issuing conversion of data to the buffer.
     *
     * This function is non-blocking. The application is notified about filling the buffer by the event
     * handler. Conversion will be done on all enabled channels. If the ADC is in idle state, the
     * function will set up Easy DMA for the conversion. The ADC will be ready for sampling and wait for
     * the SAMPLE task. It can be triggered manually by the @ref nrfx_saadc_sample function or by PPI
     * using the @ref NRF_SAADC_TASK_SAMPLE task. If one buffer is already set and the conversion is
     * ongoing, calling this function will result in queuing the given buffer. The driver will start
     * filling the issued buffer when the first one is completed. If the function is called again before
     * the first buffer is filled or calibration is in progress, it will return with error.
     *
     * @param[in] buffer Result buffer.
     * @param[in] size   Buffer size in words.
     *
     * @retval NRFX_SUCCESS    If conversion was successful.
     * @retval NRFX_ERROR_BUSY If the driver already has two buffers set or calibration is in progress.
     */
    nrfx_err_t nrfx_saadc_buffer_convert(nrf_saadc_value_t * buffer, uint16_t size);
     

    From which buffer we can read the converted analog to digital data ?

     That would be the buffer you provide it via nrf_drv_saadc_buffer_convert(). 

     

    Kind regards,

    Håkon

  • Hi ,

         Thanks for your reply.

    I have few more doubts.

    Håkon Alseth said:
    Enable SAADC, wait for the conversion, read out the ADC value, then disable SAADC.
    1.  I known the functions for Enable SAADC and disable SAADC , then which function is used to wait for the conversion?

     

     

    Håkon Alseth said:
    That would be the buffer you provide it via nrf_drv_saadc_buffer_convert().

          2. I need some clarification here in Nordic saadc example  during the initialization part i e  "saadc_init " function  they assigning some buffer "m_buffer_pool[0] and m_buffer_pool[1]" and also in the "saadc_callback" function they are doing the same but different buffer "p_event->data.done.p_buffer" and they using this value as adc result too.  Suppose if we take ""m_buffer_pool[0] " or""m_buffer_pool[1] " as adc result what will happen, Is it the same value in the "p_event->data.done.p_buffer" ?

    PLEASE ANSWER TO 1 AND 2

  • NANDHU said:
     I known the functions for Enable SAADC and disable SAADC , then which function is used to wait for the conversion?

    That would be the callback handler, with p_evt->type == NRF_DRV_SAADC_EVT_DONE. At this point, the conversion is done.

    NANDHU said:
    I need some clarification here in Nordic saadc example  during the initialization part i e  "saadc_init " function  they assigning some buffer "m_buffer_pool[0] and m_buffer_pool[1]" and also in the "saadc_callback" function they are doing the same but different buffer "p_event->data.done.p_buffer" and they using this value as adc result too.  Suppose if we take ""m_buffer_pool[0] " or""m_buffer_pool[1] " as adc result what will happen, Is it the same value in the "p_event->data.done.p_buffer" ?

    Yes, p_event->data.done.p_buffer will point to the m_buffer_pool array. You can enter debug mode, and look at the pointers, and the global arrays, to see if they match in address to verify this.

     

    Kind regards,

    Håkon

  • HI ,

       Thanks 

    My next doubts.

    1. why Nordic using 2D array as buffer instead of normal array .                                                                   "static nrf_saadc_value_t     m_buffer_pool[2][SAMPLES_IN_BUFFER];"
    2. why Nordic assigning the buffer 2 times in  "saadc_init " function ? Is the first time assigning is enough?

     

  • The saadc driver has support for handling two buffers.

    The _END event will then alternate between m_buffer_pool[0] and m_buffer_pool[1]. You can look at this by entering debug mode and placing a breakpoint in that callback.

     

    Kind regards,

    Håkon

Related