nrfx_saadc_sample_convert

Hi,

I was using the nrfx_saadc_buffer_convert to make everytime a single sample.

void saadc_init(void)
{
  ret_code_t  err_code;
   
  nrf_saadc_channel_config_t channel_config = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN2);

  err_code = nrf_drv_saadc_init(NULL, saadc_callback_handler);
  APP_ERROR_CHECK(err_code);

  err_code = nrfx_saadc_channel_init(0, &channel_config);
  APP_ERROR_CHECK(err_code);

  err_code = nrfx_saadc_buffer_convert(m_buffer_pool[0], SAMPLE_BUFFER_LEN);
  APP_ERROR_CHECK(err_code);

  err_code = nrfx_saadc_buffer_convert(m_buffer_pool[1], SAMPLE_BUFFER_LEN);
  APP_ERROR_CHECK(err_code);
}

Now I did find out there is also nrfx_saadc_sample_convert. How do I initialize this?

And do I get my val then by val = p_event->data.done.p_value instead of val = p_event->data.done.p_buffer[i]

  • void saadc_callback_handler(nrf_drv_saadc_evt_t const * p_event)
    {
      float val;
    
      if(p_event -> type == NRFX_SAADC_EVT_DONE)
      {
        ret_code_t err_code;
        uint8_t data[20];
    
        err_code = nrfx_saadc_sample_convert(0, &m_sample);
        APP_ERROR_CHECK(err_code);
      
        val = m_sample * 3.3 / 4096;
        
        sprintf((char*)data, "ADC: %.2f", val);
        uint16_t d_len   = strlen(data);
        err_code = ble_nus_data_send(&m_nus, data, &d_len, m_conn_handle); 
        APP_ERROR_CHECK(err_code);
    
      }
    }
    
    void saadc_init(void)
    {
      ret_code_t  err_code;
       
      nrf_saadc_channel_config_t channel_config = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN2);
    
      err_code = nrf_drv_saadc_init(NULL, saadc_callback_handler);
      APP_ERROR_CHECK(err_code);
    
      err_code = nrfx_saadc_channel_init(0, &channel_config);
      APP_ERROR_CHECK(err_code);
    
      err_code = nrfx_saadc_sample_convert(0, &m_sample);
      APP_ERROR_CHECK(err_code);
    
      //err_code = nrfx_saadc_buffer_convert(m_buffer_pool[0], SAMPLE_BUFFER_LEN);
      //APP_ERROR_CHECK(err_code);
    
      //err_code = nrfx_saadc_buffer_convert(m_buffer_pool[1], SAMPLE_BUFFER_LEN);
      //APP_ERROR_CHECK(err_code);
    }

    I tried this but don't get any value.

  • Have you verified that the calback is invoked with the NRFX_SAADC_EVT_DONE? Also, you should read the sample result from the event pointer as shown in the ble_app_proximity example.

  • Also, you should read the sample result from the event pointer as shown in the ble_app_proximity example

    In the ble_app_proximity they are also using nrf_drv_saadc_buffer_convert().

    That also worked for me, but I don't need a buffer. I tried to figure out how nrfx_saadc_sample_convert() works, but it works different.

    void saadc_callback_handler(nrf_drv_saadc_evt_t const * p_event)
    {
      float val;
    
      if(p_event -> type == NRFX_SAADC_EVT_DONE)
      {
        ret_code_t err_code;
        uint8_t data[20];
    
        err_code = nrfx_saadc_sample_convert(0, &m_sample);
        APP_ERROR_CHECK(err_code);
      
        val = m_sample * 3.3 / 4096;
        
        sprintf((char*)data, "ADC: %.2f", val);
        uint16_t d_len   = strlen(data);
        err_code = ble_nus_data_send(&m_nus, data, &d_len, m_conn_handle); 
        APP_ERROR_CHECK(err_code);
    
      }
    }
    
    void saadc_init(void)
    {
      ret_code_t  err_code;
       
      nrf_saadc_channel_config_t channel_config = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN2);
    
      err_code = nrf_drv_saadc_init(NULL, saadc_callback_handler);
      APP_ERROR_CHECK(err_code);
    
      err_code = nrfx_saadc_channel_init(0, &channel_config);
      APP_ERROR_CHECK(err_code);
    
      err_code = nrfx_saadc_sample_convert(0, &m_sample);
      APP_ERROR_CHECK(err_code);
    
      //err_code = nrfx_saadc_buffer_convert(m_buffer_pool[0], SAMPLE_BUFFER_LEN);
      //APP_ERROR_CHECK(err_code);
    
      //err_code = nrfx_saadc_buffer_convert(m_buffer_pool[1], SAMPLE_BUFFER_LEN);
      //APP_ERROR_CHECK(err_code);
    }

    I think I'm doing something wrong here:

    val = m_sample * 3.3 / 4096;

    Is there any example of using nrfx_saadc_sample_convert()?

  • Sorry, I read nrfx_saadc_sample_convert() as nrf_drv_saadc_buffer_convert(). But nrfx_saadc_sample_convert() is blocking and it does not make sense to call from the saadc handler. You can try to call nrfx_saadc_sample_convert() from main() after configuring the SAADC and see if that works.

  • Yes, I think that was the matter. I did make it an empty handler and call the function in my application timer. It works now. 

    What is the difference between blocking and non-blocking mode? Does it have effect on the bluetooth functionality? 

Related