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

nrf_drv_saadc_calibrate_offset() doesn't work

Running nrf_drv_saadc_calibrate_offset() doesn't seem to work. I'm monitoring a 3v battery. Shown below is the debug output where the calibration is run after the event number 4. As you can see, the voltage is reading what is should (scaled by 1000) before the calibration, but reads 3600 on each event after the calibration, never moving. 

ADC event number: 0
2960

ADC event number: 1
2960

ADC event number: 2
2949

ADC event number: 3
2953

ADC event number: 4
2953

ADC event number: 5
3600

ADC event number: 6
3600

ADC event number: 7
3600

	if (m_saadc_calibrate)
	{
	    nrf_drv_saadc_abort();
	    while(nrf_drv_saadc_calibrate_offset() != NRF_SUCCESS);
	    while(!nrf_saadc_event_check(NRF_SAADC_EVENT_CALIBRATEDONE));
	    m_saadc_calibrate = false;
	}

This is the code that runs the calibrate in the main forever loop. m_saadc_calibrate is set in saadc_callback().

Parents Reply Children
  • I've had a chance to get back to this issue and have made some changes. I've added the _buffer_convert as you have suggested and have moved the _sample trigger into the main loop. I also found that if the beacon is advertising, it hangs on the while(!nrf_saadc_event_check(NRF_SAADC_EVENT_CALIBRATEDONE)); statement, so I added a flag to make sure it's not advertising before running the calibration. Here is the modified code:

    	if (m_saadc_calibrate && advertising_completed)
    	{
    	    nrfx_saadc_evt_t const *p_event;
    	    nrfx_saadc_abort();
    	    while(nrfx_saadc_calibrate_offset() != NRF_SUCCESS);
    	    while(!nrf_saadc_event_check(NRF_SAADC_EVENT_CALIBRATEDONE));
    	    m_saadc_calibrate = false;
    	    SEGGER_RTT_WriteString(0, "SAADC Calibrated\n");
    	    ret_code_t err_code = nrfx_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
    	    APP_ERROR_CHECK(err_code);
    	}
    
    	if (intiate_advertising)
    	{
    	    for (uint8_t i = 0; i < SAMPLES_IN_BUFFER; i++)
    	    {
    		    ret_code_t err_code = nrfx_saadc_sample();
    		    APP_ERROR_CHECK(err_code);
    	    }
    
    	    intiate_advertising = false;
    	}

    The init code and the callback code posted previously is the same. SAMPLES_IN_BUFFER is set to 3.

    Now I'm getting bad values out of the SAADC after calibration:

    ADC event number: 0
    24563
    -32934
    21965

    ADC event number: 1
    24563
    -32934
    21965

    ADC event number: 2
    24563
    -32934
    21965

    And so on. The values never change. The values should be around 2990 which is what they are without running the calibration.

    If I remove _buffer_convert, it crashes when  _sample trigger is executed.

Related