Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

SAADC how to properly use it with scheduler?

Hi,

I have project based on nRF52840 (using SDK 15.2) and I have a problem with properly adding ADC measurement using scheduler - description as below.

Problem description:

I have an application where is used a scheduler - I need to add new functionality to my application. This new functionality is ADC (I need to measure incoming V). Regarding /examples/peripheral/saadc/main.c I implement some functions in my application - I mean void saadc_init(void) and void saadc_callback(nrf_drv_saadc_evt_t const * p_event). The current application is based on timer interrupt - when application/device is waking up I need to make an ADC measurement.

Question:

1) The problem is that I have no idea how to properly add to scheduler ADC measurement during timer interrupt? Should I call serial_scheduler_event_hanler() or should I app_sched_event_put()?

2) Regarding the code below -the interrupt never course does anybody have idea why? ( distanceMeas() is called but interrupt does not come)

Code:

//add to sched
app_sched_event_put(NULL, 0, distanceMeas);

void distanceMeas(void * data, uint16_t size)
{
    ret_code_t err_code;
	err_code = nrf_drv_saadc_sample();
	APP_ERROR_CHECK(err_code);

	if (err_code != NRF_SUCCESS)
	{
		bsp_board_led_invert(0);
	}

	(void)nrf_serial_write(&serial_uart, "\r\nTest here1\r\n", strlen("\r\nTest here1\r\n"), NULL, 0);
	(void)nrf_serial_flush(&serial_uart, 0);}

void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
	bsp_board_led_invert(1);
	(void)nrf_serial_write(&serial_uart, "\r\nTest here2\r\n", strlen("\r\nTest here1\r\n"), NULL, 0);
	(void)nrf_serial_flush(&serial_uart, 0);

    if (p_event->type == NRF_DRV_SAADC_EVT_DONE)	    //NRFX_SAADC_EVT_DONE < Event generated when the buffer is filled with samples.
    {
        ret_code_t err_code;
        float gainRef = 10240 / 36;
        float measMed = 0;
        float inVolt = 0;

        err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);

        int i;
        //NRF_LOG_INFO("ADC event number: %d", (int)m_adc_evt_counter);

        for (i = 0; i < SAMPLES_IN_BUFFER; i++)
        {
            NRF_LOG_INFO("meas: %d", p_event->data.done.p_buffer[i]);
            measMed += p_event->data.done.p_buffer[i];
        }
        //m_adc_evt_counter++;
		/*Convert the data into the voltage value*/
        measMed /=  SAMPLES_IN_BUFFER;
        inVolt = measMed / gainRef;

        NRF_LOG_INFO("VOLTAGE: " NRF_LOG_FLOAT_MARKER, NRF_LOG_FLOAT(inVolt));
    }
}

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_AIN0);

    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);
}

Related