SAADC measurement doesn`t occur after SAADC offset calibration.

Hello. I have a problem when using nrfx_saadc. I try to start the sampling right after the SAADC offset calibration, but it doesn't happen and the program hangs. Only if I add a delay(4ms) before the measurement,  then everything works as it should. I also noticed that if I do not add this delay before nrfx_saadc_sample() function calling, SAADC generate EVENT_END event.

SAADC Configuration:

- Resolution 14bit

- Oversampling 128x

- Interrupt priority 6

- Low power mode is turned ON.

Here is the code I use to run measurements after calibration:

if(p_event->type == NRFX_SAADC_EVT_CALIBRATEDONE)
{
    // Start new accurate measurement
    nrf_delay_ms(4); //Only if I add delay saadc measurement is occurring
    ret_code_t err_code = tenso_saadc_measure();
    NRF_LOG_INFO("Calibration done");
    APP_ERROR_CHECK(err_code);
    
    return;
}
Source code of tenso_saadc_measure:

static ret_code_t tenso_saadc_measure()
{
    ret_code_t err_code = nrfx_saadc_buffer_convert(&(m_cb.tenso_adc_buf),  1);
    if(err_code) return err_code;
 
    err_code = nrfx_saadc_sample();
    if(err_code) return err_code;

    return err_code;
}

Tell me why this happens, maybe I'm wrong in something?

Thanks in advance for your reply.

Parents
  • Hello,

    I try to start the sampling right after the SAADC offset calibration, but it doesn't happen and the program hangs.

    What happens when the program 'hangs'?
    Do you have logs from when this happens, or have you tried to use the debugging option to see where the program counter stops (if it does)?

    I also recommend that you make sure to have DEBUG defined in your preprocessor defines, like shown in the included image:

    This will make your logger output a detailed error message whenever a non-NRF_SUCCESS error code is passed to an APP_ERROR_CHECK.

    I also noticed that if I do not add this delay before nrfx_saadc_sample() function calling, SAADC generate EVENT_END event.

    I am not sure I fully understand this description, could you elaborate?
    If you do not add the delay then you will get an unexpected END event, is that so?
    This sounds like it might be due to the Errata-178.

    How often are you performing the offset calibration? Do you do so before every sample?
    In general we recommend that you perform a calibrateoffset at the boot, and then once more every time the ambient changes with 10 Celsius, for best results.

    SAADC Configuration:

    Just for context, could you elaborate on what you are sampling as well?
    For instance, what sampling frequency are you using?

    Best regards,
    Karl

  • 1. Sorry, I didn't express myself correctly. I meant that the SAADC measurement does not occur after calibration.

    2. Of course, I have selected the Debug configuration, and configured the nrfx_saadc driver to display 4th-level logs (Debug). But there was nothing interesting except that the event NRF_SAADC_EVENT_END is fired.

    Just in case, here are the SAADC logs:

    <info> SAADC: Channel initialized: 0.
    <info> SAADC: Function: nrfx_saadc_channel_init, error code: NRF_SUCCESS.
    <info> SAADC: Function: nrfx_saadc_calibrate_offset, error code: NRF_SUCCESS.
    <debug> SAADC: Event: NRF_SAADC_EVENT_CALIBRATEDONE.
    <info> SAADC: Function: nrfx_saadc_buffer_convert, buffer length: 1, active channels: 1.
    <info> SAADC: Function: nrfx_saadc_buffer_convert, error code: NRF_SUCCESS.
    <info> SAADC: Function: nrfx_saadc_sample, error code: NRF_SUCCESS.
    <debug> SAADC: Event: NRF_SAADC_EVENT_END.
    <debug> SAADC: Event: NRF_SAADC_EVENT_STARTED.

    3. Yes, you understand the problem correctly. It looks like the anomaly that was attached. Thanks.

    4. I do SAADC calibration every 20,000 measurements (1 measurement in 50ms). So I understand that this is not very optimal. Could you advise me on how to do it correctly without writing a ton of code? 

Reply
  • 1. Sorry, I didn't express myself correctly. I meant that the SAADC measurement does not occur after calibration.

    2. Of course, I have selected the Debug configuration, and configured the nrfx_saadc driver to display 4th-level logs (Debug). But there was nothing interesting except that the event NRF_SAADC_EVENT_END is fired.

    Just in case, here are the SAADC logs:

    <info> SAADC: Channel initialized: 0.
    <info> SAADC: Function: nrfx_saadc_channel_init, error code: NRF_SUCCESS.
    <info> SAADC: Function: nrfx_saadc_calibrate_offset, error code: NRF_SUCCESS.
    <debug> SAADC: Event: NRF_SAADC_EVENT_CALIBRATEDONE.
    <info> SAADC: Function: nrfx_saadc_buffer_convert, buffer length: 1, active channels: 1.
    <info> SAADC: Function: nrfx_saadc_buffer_convert, error code: NRF_SUCCESS.
    <info> SAADC: Function: nrfx_saadc_sample, error code: NRF_SUCCESS.
    <debug> SAADC: Event: NRF_SAADC_EVENT_END.
    <debug> SAADC: Event: NRF_SAADC_EVENT_STARTED.

    3. Yes, you understand the problem correctly. It looks like the anomaly that was attached. Thanks.

    4. I do SAADC calibration every 20,000 measurements (1 measurement in 50ms). So I understand that this is not very optimal. Could you advise me on how to do it correctly without writing a ton of code? 

Children
  • vitaliy2034 said:
    1. Sorry, I didn't express myself correctly. I meant that the SAADC measurement does not occur after calibration.

    No need to apologize - I will just ask if there's anything unclear. Thank you for clarifying.

    vitaliy2034 said:
    2. Of course, I have selected the Debug configuration, and configured the nrfx_saadc driver to display 4th-level logs (Debug). But there was nothing interesting except that the event NRF_SAADC_EVENT_END is fired.

    You must still add the DEBUG define to your preprocessor defines, like shown in the picture, if you would like to have the logger output the detailed error message. This is not defined in the debug configuration by default.

    vitaliy2034 said:
    3. Yes, you understand the problem correctly. It looks like the anomaly that was attached. Thanks.

    Great, I am glad to hear that we got this one figured out.
    Does it return to the expected behavior when you apply the workaround as described in the Errata?

    vitaliy2034 said:
    4. I do SAADC calibration every 20,000 measurements (1 measurement in 50ms). So I understand that this is not very optimal. Could you advise me on how to do it correctly without writing a ton of code? 

    As I mentioned we recommend performing a calibration on boot, and then once more every time the ambient temperature of the environment that the nRF is working in changes by 10 degrees celsius.
    You should not have to perform calibrations for every 20,000 measurement, but you can of course do this if you'd like - there's no harm done.
    If you know that the ambient temperature is prone to change, and you do not have any temperature sensor on your board, then you may perform the calibration periodically instead (like you are doing now) to keep the SAADC measurements accurate.

    Best regards,
    Karl

Related