nrf52840 SAADC: I use 2 adc channels, how do I sample multiple points(e.g.32) per channel at once, the buffer contains only multiple items of data in one channel, data in the other channel is missing.

nrf_saadc_channel_config_t channel_config_0 =
    NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
nrf_saadc_channel_config_t channel_config_1 =
    NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1);
    
err_code = nrf_drv_saadc_init(NULL, saadc_callback);
APP_ERROR_CHECK(err_code);

err_code = nrf_drv_saadc_channel_init(1, &channel_config_0);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_channel_init(2, &channel_config_1);
APP_ERROR_CHECK(err_code);

err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool, 64);
APP_ERROR_CHECK(err_code);

  • Hello,

    In the SAADC there is something called oversampling, which will sample 2^OVERSAMPLE times where OVERSAMPLE is a configuration you can set when you initialize the saadc.

    In fact, when you call nrf_drv_saadc_init() with the first parameter set to NULL, you can see from nrf_drv_saadc.h that it will use NRFX_SAADC_DEFAULT_CONFIG:

    __STATIC_INLINE ret_code_t nrf_drv_saadc_init(nrf_drv_saadc_config_t const * p_config,
                                                  nrf_drv_saadc_event_handler_t  event_handler)
    {
        if (p_config == NULL)
        {
            static const nrfx_saadc_config_t default_config = NRFX_SAADC_DEFAULT_CONFIG;
            p_config = &default_config;
        }
        return nrfx_saadc_init(p_config, event_handler);
    }

    Which uses:

    oversample         = (nrf_saadc_oversample_t)NRFX_SAADC_CONFIG_OVERSAMPLE, \

    which is set in sdk_config.h. By default it is 0, but you can set it to the value you want to use. Please note that if you have defined both SAADC_CONFIG_OVERSAMPLE and NRFX_SAADC_CONFIG_OVERSAMPLE, then SAADC_CONFIG_OVERSAMPLE will be the one being used.

    So if you set SAADC_CONFIG_OVERSAMPLE to e.g. 4, it will sample 2^4 = 16 times, and average this for you before you are presented with the result.

    Also note that you need to choose whether it should sample all the times needed to fill the oversampling buffers, or if you have to trigger it manually this many times. This is done in NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE (you can look at this definition). If you want it to take all the samples at once, please do something like:

    nrf_saadc_channel_config_t channel_config_0 =
        NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
    nrf_saadc_channel_config_t channel_config_1 =
        NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1);
    
    channel_config_0.burst = NRF_SAADC_BURST_ENABLED;
    channel_config_1.burst = NRF_SAADC_BURST_ENABLED;
        
    err_code = nrf_drv_saadc_init(NULL, saadc_callback);
    APP_ERROR_CHECK(err_code);
    ...

    Best regards,

    Edvin

Related