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

nRF52 SAADC re-initialize

Hi guys! Got a problem with re-initialising my SAADC to read my current battery voltage. I'm using SDK11 and don't want to migrate to v12, so I wanted to do a workaround for the power consumption bug when using SAADC and going to sleep.

I'm initialising the SAADC

void saadc_init(void)
{
ret_code_t err_code;
nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_VDD);	
err_code = nrf_drv_saadc_init(NULL, saadc_callback);
APP_ERROR_CHECK(err_code);

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

err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0],SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);

err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1],SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);
}

my Callback looks like this

void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{	
if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
{		
    ret_code_t err_code;
 
    err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
    APP_ERROR_CHECK(err_code);

batt_val[0]=	p_event->data.done.p_buffer[0];
nrf_drv_saadc_uninit();
NRF_SAADC->INTENCLR = (SAADC_INTENCLR_END_Clear << SAADC_INTENCLR_END_Pos);
NVIC_ClearPendingIRQ(SAADC_IRQn);			
}

}

But when I want to re-initialise the SAADC before taking a new measurement via nrf_drv_saadc_sample(); by calling the saadc_init(); again, my controller seems to freeze and connecting via BLE isn't possible anymore. Is the SAADC not properly uninitialized so a Re-Initializing breaks my execution? Any suggestions how to fix this? Implementation seems alright to me. Using Segger Studio with GCC btw.

  • The way you do it is the way I do it and it works fine for me. What are you getting back in your error codes?

  • Compiled with DEBUG, but the Error-Handler puts nothing into my RTT Viewer. I've commented every non related functioncall, just the BLE and ADC Stuff is called in my project right now with the same behaviour.

    I've included some Error-Messages by myself, it shows that it crashes when I execute the Channel-Init nrf_drv_saadc_channel_init(0, &channel_config); 2nd time. I've put Segger-Output into the callback itself, but I don't get any messages from the callback whatsoever. Seems like the callback is never called at all...

    EDIT: Without deactivating I'm getting a Callback but just every 8th or 9th measurement. My SAADC settings are as follows:

    #define SAADC_ENABLED 1
    
    #if (SAADC_ENABLED == 1)
    #define SAADC_CONFIG_RESOLUTION      NRF_SAADC_RESOLUTION_8BIT		// NORMALLY 10BIT
    #define SAADC_CONFIG_OVERSAMPLE      NRF_SAADC_OVERSAMPLE_8X //NRF_SAADC_OVERSAMPLE_DISABLED
    #define SAADC_CONFIG_IRQ_PRIORITY    APP_IRQ_PRIORITY_LOW
    #endif
    
  • These are mine

    /* SAADC */
    #define SAADC_ENABLED 1
    
    #if (SAADC_ENABLED == 1)
    #define SAADC_CONFIG_RESOLUTION      NRF_SAADC_RESOLUTION_10BIT
    #define SAADC_CONFIG_OVERSAMPLE      NRF_SAADC_OVERSAMPLE_DISABLED
    #define SAADC_CONFIG_IRQ_PRIORITY    APP_IRQ_PRIORITY_LOW
    #endif
    
  • Silly me, I'm using 8x oversampling and don't start the measurement 8 times, so the Interrupt won't occur unless I start the measurement 8 times too. Thanks for the hint :)

Related