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

How to stop SAADC

#define SAMPLES_IN_BUFFER 5
static nrf_saadc_value_t     m_buffer_pool[2][SAMPLES_IN_BUFFER];


void saadc_sampling_event_enable(void)
{
    ret_code_t err_code = nrf_drv_ppi_channel_enable(m_ppi_channel);

    APP_ERROR_CHECK(err_code);
}


void saadc_sampling_event_init(void)
{
    ret_code_t err_code;

    err_code = nrf_drv_ppi_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;
    err_code = nrf_drv_timer_init(&m_timer, &timer_cfg, timer_handler);
    APP_ERROR_CHECK(err_code);

    /* setup m_timer for compare event every 400ms */
    uint32_t ticks = nrf_drv_timer_ms_to_ticks(&m_timer, 11);
    nrf_drv_timer_extended_compare(&m_timer,
                                   NRF_TIMER_CC_CHANNEL0,
                                   ticks,
                                   NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,
                                   false);
    nrf_drv_timer_enable(&m_timer);

    uint32_t timer_compare_event_addr = nrf_drv_timer_compare_event_address_get(&m_timer,
                                                                                NRF_TIMER_CC_CHANNEL0);
    uint32_t saadc_sample_task_addr   = nrf_drv_saadc_sample_task_get();

    /* setup ppi channel so that timer compare event is triggering sample task in SAADC */
    err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_ppi_channel_assign(m_ppi_channel,
                                          timer_compare_event_addr,
                                          saadc_sample_task_addr);
    APP_ERROR_CHECK(err_code);
}



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;
		// i tried stop_adc() here but its the same resulut
		err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);
		
		stop_adc();
        
    }
}


void saadc_init(void)
{
    ret_code_t err_code;
    nrf_saadc_channel_config_t channel_config =
    {                                                   \
    .resistor_p = NRF_SAADC_RESISTOR_DISABLED,      \
    .resistor_n = NRF_SAADC_RESISTOR_DISABLED,      \
    .gain       = NRF_SAADC_GAIN1_4,                \
    .reference  = NRF_SAADC_REFERENCE_INTERNAL  ,     \
    .acq_time   = NRF_SAADC_ACQTIME_10US,           \
    .mode       = NRF_SAADC_MODE_SINGLE_ENDED,      \
    .burst      = NRF_SAADC_BURST_DISABLED,         \
    .pin_p      = NRF_SAADC_INPUT_AIN1,       \
    .pin_n      = NRF_SAADC_INPUT_DISABLED          \
};

    nrf_drv_saadc_config_t saadc_config;
    //Configure SAADC
    saadc_config.low_power_mode = true;                                                   //Enable low power mode.
    saadc_config.resolution = NRF_SAADC_RESOLUTION_12BIT;                                 //Set SAADC resolution to 12-bit. This will make the SAADC output values from 0 (when input voltage is 0V) to 2^12=2048 (when input voltage is 3.6V for channel gain setting of 1/6).
    saadc_config.interrupt_priority = _PRIO_APP_LOW;                               //Set SAADC interrupt to low priority.
    saadc_config.oversample = NRF_SAADC_OVERSAMPLE_DISABLED;
    
    err_code = nrf_drv_saadc_init(&saadc_config, saadc_callback);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_saadc_channel_init(1, &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);

}


void start_adc
{
    saadc_init();
    saadc_sampling_event_init();
    saadc_sampling_event_enable();
}

void stop_adc()
{
    nrf_drv_timer_disable(&m_timer);
    nrfx_timer_uninit(&m_timer);
    ret_code_t err_code = nrf_drv_ppi_channel_disable(m_ppi_channel);
    APP_ERROR_CHECK(err_code);  
   
    err_code = nrf_drv_ppi_uninit();
    APP_ERROR_CHECK(err_code);
      
    nrfx_saadc_uninit(); 
      
    err_code = nrf_drv_saadc_channel_uninit(1);
    APP_ERROR_CHECK(err_code);    
}

Hi, 

I am using nrf52840 with custom board, sdk 15 and and softdevice 6.0.0. I  modified saadc example to sample SAADC value once and stop it in saadc_callback. Everything works great, but when I start SAADC and measure the current, it consumes 2 extra mA which means SAADC is not stopped because this happens only with SAADC start and only software reset can stop it. I used nrfx_saadc_sample() and nrf_drv_saadc_abort instead of timer but it does not stop as well. 

I tried all the answers for related problems, but it did not help.

Related