<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>NRF52840 SAADC Calibration</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/38712/nrf52840-saadc-calibration</link><description>SDK: nRF5 SDK for Thread and Zigbee 1.0.0 
 I have code for the SAADC based on the Low Power SAADC example here . 
 I have modified it to sample 4 inputs, and changed the settings as follows: 
 
 I further modified the code to calibrate when Button 0</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 05 Oct 2018 07:35:23 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/38712/nrf52840-saadc-calibration" /><item><title>RE: NRF52840 SAADC Calibration</title><link>https://devzone.nordicsemi.com/thread/151766?ContentTypeID=1</link><pubDate>Fri, 05 Oct 2018 07:35:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6ba8249e-1230-4ef2-bab8-a61aa96021e9</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;No, not yet.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF52840 SAADC Calibration</title><link>https://devzone.nordicsemi.com/thread/151279?ContentTypeID=1</link><pubDate>Tue, 02 Oct 2018 15:07:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8b5ab025-66ed-4d77-8e80-f4d41ade77ef</guid><dc:creator>Mary</dc:creator><description>&lt;p&gt;I tried that, but it made no difference.&amp;nbsp; The data was still shifted after calibration.&lt;/p&gt;
&lt;p&gt;Were you able to reproduce the problem?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF52840 SAADC Calibration</title><link>https://devzone.nordicsemi.com/thread/150695?ContentTypeID=1</link><pubDate>Thu, 27 Sep 2018 12:12:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5b9a54de-6635-47ee-9b5a-81e54b788516</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;I think I found your problem, you need to wait for the &lt;span&gt;EVENTS_CALIBRATEDONE&amp;nbsp;&lt;/span&gt;because&amp;nbsp;&lt;a title="nrfx_saadc_calibrate_offset" href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.2.0/group__nrfx__saadc.html?cp=4_0_0_6_9_0_19_1_14#gabebf4b2245bff5bc767b0e679702708d"&gt;nrfx_saadc_calibrate_offset&lt;/a&gt;&amp;nbsp;is non-blocking.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;You need to add:&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;while(nrf_drv_saadc_calibrate_offset() != NRF_SUCCESS); //Trigger calibration task

while(!nrf_saadc_event_check(NRF_SAADC_EVENT_CALIBRATEDONE ))
{
    nrf_pwr_mgmt_run();
}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF52840 SAADC Calibration</title><link>https://devzone.nordicsemi.com/thread/149667?ContentTypeID=1</link><pubDate>Thu, 20 Sep 2018 14:05:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cd7e1584-16ec-4ea8-a2a1-7b248011529d</guid><dc:creator>Mary</dc:creator><description>&lt;p&gt;Below are the methods that I modified or added to the example.&lt;/p&gt;
&lt;p&gt;saadc_callback() (modified) plus additional auto variable, m_saadc_cal_prepare, and some definitions&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define NUM_ADC_CHANNELS    4

typedef enum {
    CAL_PREPARE = 0,
    CAL_START
} CAL_ACTION_TYPE; 


static bool                    m_saadc_cal_prepare = false; 

void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
    nrf_saadc_value_t *p_samples;

    ret_code_t err_code;

    if (p_event-&amp;gt;type == NRF_DRV_SAADC_EVT_DONE)                                                        //Capture offset calibration complete event
    {
			
        LEDS_INVERT(BSP_LED_2_MASK);                                                                    //Toggle LED3 to indicate SAADC buffer full		

        //if((m_adc_evt_counter % SAADC_CALIBRATION_INTERVAL) == 0)                                  //Evaluate if offset calibration should be performed. Configure the SAADC_CALIBRATION_INTERVAL constant to change the calibration frequency
        if (m_saadc_cal_prepare)
        {
            nrf_drv_saadc_abort();                                                                      // Abort all ongoing conversions. Calibration cannot be run if SAADC is busy
            m_saadc_calibrate = true;                                                                   // Set flag to trigger calibration in main context when SAADC is stopped
        }
        

#ifdef UART_PRINTING_ENABLED
        NRF_LOG_INFO(&amp;quot;ADC event number: %d \r\n&amp;quot;,(int)m_adc_evt_counter);                                //Print the event number on UART

        for (int i = 0; i &amp;lt; p_event-&amp;gt;data.done.size; i++)
        {
            NRF_LOG_INFO(&amp;quot;%d\r\n&amp;quot;, p_event-&amp;gt;data.done.p_buffer[i]);                                     //Print the SAADC result on UART
            
        }
#endif //UART_PRINTING_ENABLED   

        if(m_saadc_calibrate == false)
        {
            err_code = nrf_drv_saadc_buffer_convert(p_event-&amp;gt;data.done.p_buffer, SAADC_SAMPLES_IN_BUFFER * NUM_ADC_CHANNELS);             //Set buffer so the SAADC can write to it again. 
            APP_ERROR_CHECK(err_code);
        }
        
        m_adc_evt_counter++;

    }
    else if (p_event-&amp;gt;type == NRF_DRV_SAADC_EVT_CALIBRATEDONE)
    {
        LEDS_INVERT(BSP_LED_2_MASK);                                                                    //Toggle LED3 to indicate SAADC calibration complete

        err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAADC_SAMPLES_IN_BUFFER * NUM_ADC_CHANNELS);             //Set buffer so the SAADC can write to it again. 
        APP_ERROR_CHECK(err_code);
        err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAADC_SAMPLES_IN_BUFFER * NUM_ADC_CHANNELS);             //Need to setup both buffers, as they were both removed with the call to nrf_drv_saadc_abort before calibration.
        APP_ERROR_CHECK(err_code);
        
#ifdef UART_PRINTING_ENABLED
        NRF_LOG_INFO(&amp;quot;SAADC calibration complete ! \r\n&amp;quot;);                                              //Print on UART
#endif //UART_PRINTING_ENABLED	
        
        m_adc_evt_counter = 0;

    }

}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Additional methods&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;bool adc_is_cal_prepared()
{
    return m_saadc_calibrate;
}

void adc_calibrate(int action)
{
    switch(action)
    {
        case CAL_PREPARE:
            m_saadc_cal_prepare = true;                             // Stop conversions when this one is done
            break;

        case CAL_START:
            NRF_LOG_INFO(&amp;quot;SAADC calibration starting...  \r\n&amp;quot;);    //Print on UART
            while(nrf_drv_saadc_calibrate_offset() != NRF_SUCCESS); //Trigger calibration task
            m_saadc_calibrate = false;
            m_saadc_cal_prepare = false;
            break;
    }
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Buttons and main loop:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/***************************************************************************************************
 * @section Buttons
 **************************************************************************************************/

static void bsp_event_handler(bsp_event_t event)
{

    switch (event)
    {
        case BSP_EVENT_KEY_0:
        {
            adc_calibrate(CAL_PREPARE);
            break;
        }


        default:
            break;
    }
}



/***************************************************************************************************
 * @section Main
 **************************************************************************************************/

int main(int argc, char *argv[])
{

    LEDS_CONFIGURE(LEDS_MASK);                       //Configure all leds
    LEDS_OFF(LEDS_MASK);                             //Turn off all leds
	
    NRF_POWER-&amp;gt;DCDCEN = 1;                           //Enabling the DCDC converter for lower current consumption
	
#ifdef UART_PRINTING_ENABLED
    uint32_t err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);                       //Configure Logging. LOGGING is used to show the SAADC sampled result. Default is UART, but RTT can be configured in sdk_config.h
    NRF_LOG_INFO(&amp;quot;\n\rSAADC Low Power Example.\r\n&amp;quot;);	
#endif //UART_PRINTING_ENABLED	
	

    lfclk_config();                                  //Configure low frequency 32kHz clock
    rtc_config();                                    //Configure RTC. The RTC will generate periodic interrupts. Requires 32kHz clock to operate.

    saadc_init();                                    //Initialize and start SAADC
 
    while(1)
    {
        // Calilbrate if ready
        if (adc_is_cal_prepared())
        {
            adc_calibrate(CAL_START);
        }

        nrf_pwr_mgmt_run();
#ifdef UART_PRINTING_ENABLED
        NRF_LOG_FLUSH();
#endif //UART_PRINTING_ENABLED

    
    }


}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;This should be enough for you to repeat it.&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Mary&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NRF52840 SAADC Calibration</title><link>https://devzone.nordicsemi.com/thread/149643?ContentTypeID=1</link><pubDate>Thu, 20 Sep 2018 12:55:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5b2beb34-01b5-432e-aec2-690ebb35ba2b</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;Do you mind sharing the code relevant to the calibration?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>