Dev Academy Lesson 6 Exercise 2 – nrfx ADC is non-blocking but does not wait for conversion to complete

Working through the dev academy exercise, it appears that the sample code does not wait for the conversion to complete before reading out the sample:

/* STEP 7.1 - Implement timer callback handler function */
void battery_sample_timer_handler(struct k_timer *timer)
{

        /* STEP 7.2 - Trigger the sampling */
        nrfx_err_t err = nrfx_saadc_mode_trigger();
        if (err != NRFX_SUCCESS) {
                printk("nrfx_saadc_mode_trigger error: %08x", err);
                return;
        }

        /* STEP 7.3 - Calculate and print voltage */
        int battery_voltage = ((600*6) * sample) / ((1<<12));

        printk("SAADC sample: %d\n", sample);
        printk("Battery Voltage: %d mV\n", battery_voltage);

}
Would it not be more appropriate to get a call back from the nrfx_saadc_irq_handler when the conversion is complete?
When using the Zephyr ADC driver on the same exact board, it appears the ADC measurements are ~30 mV different.
Can you please confirm if we should be waiting for the conversion to complete here?
  • I figured this out.  I found NULL was passed as the event handler to this:

    /**
     * @brief Function for setting the SAADC driver in the simple mode.
     *
     * The simple mode allows obtaining a single sample from each requested channel.
     * The conversion can be done in a blocking or non-blocking manner.
     * Sampling is initiated by calling @ref nrfx_saadc_mode_trigger() once.
     *
     * @param[in] channel_mask  Bitmask of channels to be used in the simple mode.
     * @param[in] resolution    Resolution configuration.
     * @param[in] oversampling  Oversampling configuration.
     * @param[in] event_handler Event handler provided by the user. In case of providing NULL,
     *                          the conversion will be performed in the blocking manner.
     *
     * @retval NRFX_SUCCESS             Initialization was successful.
     * @retval NRFX_ERROR_BUSY          There is a conversion or calibration ongoing.
     * @retval NRFX_ERROR_INVALID_PARAM Attempt to activate channel that is not configured.
     */
    nrfx_err_t nrfx_saadc_simple_mode_set(uint32_t                   channel_mask,
                                          nrf_saadc_resolution_t     resolution,
                                          nrf_saadc_oversample_t     oversampling,
                                          nrfx_saadc_event_handler_t event_handler);
    

    I wish this was more clearly written out.

    Also the offset discrepancy was caused by not calling 

    nrfx_saadc_offset_calibrate() prior to starting the conversion in the sample.
  • Hello,

    Thank you for the feedback. I will relay this to the team responsible for this course. I also think it is worth mentioning about the calibration. The blocking part appears to be covered already in the updated course:

    Best regards,

    Vidar

  • The team has added a note about SAADC calibration in the course now:

      

    Thanks again for your feedback.

Related