ADC of zephyr

Hi,

I am working on the nrf5340 with zephyr (NCS 1.7.1), I need to monitor the voltage of the battery, in the beginning, I used the ADC API of zephyr, and it works well, but the zephyr API doesn't seem to provide API to close the ADC, so I have to switch to nrfx_saadc API, and I got a few examples of its usage here:

The issue is as below:

Only the "simple booking mode" works, the other two mode doesn't work, the system hangs after nrfx_saadc_mode_trigger() is executed, It appears the “nrfx saadc event handler” is not working properly.

the ADC config of prj.conf as below, am I missing anything?

CONFIG_ADC=y
CONFIG_ADC_ASYNC=y
CONFIG_ADC_NRFX_SAADC=y
CONFIG_NRFX_SAADC=y

Thanks!
Kind regards
Parents Reply Children
  • Hi Jason,

    What have you included in the folder where the example is stored, i.e., the folder with the prj.conf, src and any other files that you have created. It would be helpful if you could share your folder.

    Regards,

    Priyanka

  • Hi Priyanka,

    I just create an application from zephyr/samples/hello_world, and copy the ADC sample code to the main.c, 

    if the event handler is NULL, it works.

    nrfx_saadc_simple_mode_set((1 << 0),

                                              NRF_SAADC_RESOLUTION_12BIT,
                                              NRF_SAADC_OVERSAMPLE_DISABLED,
                                              NULL);
    if set the event handler, it does not work.
    nrfx_saadc_simple_mode_set((1 << 0),
                                              NRF_SAADC_RESOLUTION_12BIT,
                                              NRF_SAADC_OVERSAMPLE_DISABLED,
                                             event_handler
    );

    I attached the main.c and prj.conf as below.

    /*
     * Copyright (c) 2012-2014 Wind River Systems, Inc.
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <zephyr.h>
    #include <sys/printk.h>
    #include <stdbool.h>
    #include <stdint.h>
    #include <nrfx_saadc.h>
    
    static volatile bool is_ready;
    static nrf_saadc_value_t sample;
    
    nrfx_saadc_channel_t channel = NRFX_SAADC_DEFAULT_CHANNEL_SE(NRF_SAADC_INPUT_VDD, 0);
    #define BATTERY_VOLTAGE(sample) (sample * 6 * 600UL / (1 << 12))
    static void handle_error(nrfx_err_t error_code)
    {
    	if (error_code != NRFX_SUCCESS)
    	{
    		// error
    		while (1)
    		{
    		};
    	}
    }
    
    static void event_handler(nrfx_saadc_evt_t const *p_event)
    {
    	if (p_event->type == NRFX_SAADC_EVT_DONE)
    	{
    		// Buffer with data is available here: p_event->data.done.p_buffer
    		is_ready = true;
    	}
    }
    
    int main(void)
    {
    	nrfx_err_t err_code;
    
    	err_code = nrfx_saadc_init(NRFX_SAADC_DEFAULT_CONFIG_IRQ_PRIORITY);
    	handle_error(err_code);
    
    	err_code = nrfx_saadc_channels_config(&channel, 1);
    	handle_error(err_code);
    
    	err_code = nrfx_saadc_simple_mode_set((1 << 0),
    										  NRF_SAADC_RESOLUTION_12BIT,
    										  NRF_SAADC_OVERSAMPLE_DISABLED,
    										  NULL);
    	handle_error(err_code);
    
    	err_code = nrfx_saadc_buffer_set(&sample, 1);
    	handle_error(err_code);
    	printk("Hello World! %s\n", CONFIG_BOARD);
    	while (1)
    	{
    		err_code = nrfx_saadc_mode_trigger();
    		handle_error(err_code);
    		printk("sample %u", BATTERY_VOLTAGE(sample));
    		k_msleep(1000);
    	}
    }
    

    the pri.conf, only enable CONFIG_NRFX_SAADC

    CONFIG_NRFX_SAADC=y

  • Hi Jason,

    I am looking into your case and will get back to you soon. We are understaffed at the moment due to the holiday season and hence kindly expect some delays in the replies.

    Thank you for understanding,

    Regards,

    Priyanka

  • Hi Priyanka,

    I have found the answer, it needs to use Zephyr's method to set up ISR first.

    IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_SAADC), 1, nrfx_isr, nrfx_saadc_irq_handler, 0);
  • Can you detail your fix here? I think I'm running into the same issue. I cannot get the IRQ based mode to run. When including the IRQ_Connect line I get an error and cannot build.

Related