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

saadc_event_handler not being called

Hello ...

New to Nordic SDK and trying to implement SAADC to read battery level. Have looked at a bunch of posts. I started with the ble_app_uart example app and tried to add SAADC code from the proximity example. Here's relevant code:

#include "nrfx_saadc.h"

#define ADC_REF_VOLTAGE_IN_MILLIVOLTS  600  //!< Reference voltage (in milli volts) used by ADC while doing conversion.
#define ADC_RES_10BIT                  1024 //!< Maximum digital value for 10-bit ADC conversion.
#define ADC_PRE_SCALING_COMPENSATION   6    //!< The ADC is configured to use VDD with 1/3 prescaling as input. And hence the result of conversion is to be multiplied by 3 to get the actual value of the battery voltage.
#define DIODE_FWD_VOLT_DROP_MILLIVOLTS 270  //!< Typical forward voltage drop of the diode (Part no: SD103ATW-7-F) that is connected in series with the voltage supply. This is the voltage drop when the forward current is 1mA. Source: Data sheet of 'SURFACE MOUNT SCHOTTKY BARRIER DIODE ARRAY' available at www.diodes.com.
#define ADC_RESULT_IN_MILLI_VOLTS(ADC_VALUE)\
        ((((ADC_VALUE) * ADC_REF_VOLTAGE_IN_MILLIVOLTS) / ADC_RES_10BIT) * ADC_PRE_SCALING_COMPENSATION)

static nrf_saadc_value_t adc_buf[2];


void saadc_event_handler(nrfx_saadc_evt_t const * p_event)
{
    if (p_event->type == NRFX_SAADC_EVT_DONE)
    {
        nrf_saadc_value_t adc_result;
        uint16_t          batt_lvl_in_milli_volts;
        uint32_t          err_code;

        adc_result = p_event->data.done.p_buffer[0];

        err_code = nrfx_saadc_buffer_convert(p_event->data.done.p_buffer, 1);
        APP_ERROR_CHECK(err_code);

        batt_lvl_in_milli_volts = ADC_RESULT_IN_MILLI_VOLTS(adc_result) +
                                  DIODE_FWD_VOLT_DROP_MILLIVOLTS;

    }
}


static void adc_configure(void)
{
    ret_code_t err_code = nrfx_saadc_init(NULL, saadc_event_handler);
    APP_ERROR_CHECK(err_code);

    nrf_saadc_channel_config_t config =
        NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_VDD);
    err_code = nrfx_saadc_channel_init(0, &config);
    APP_ERROR_CHECK(err_code);

    err_code = nrfx_saadc_buffer_convert(&adc_buf[0], 1);
    APP_ERROR_CHECK(err_code);

    err_code = nrfx_saadc_buffer_convert(&adc_buf[1], 1);
    APP_ERROR_CHECK(err_code);
}



void read_battery_voltage(void)
{
    ret_code_t err_code;
    err_code = nrfx_saadc_sample();
    APP_ERROR_CHECK(err_code);
}


.
.
.

int main(void)
{
    bool erase_bonds;

    // Initialize.
    uart_init();
    log_init();
    timers_init();
    buttons_leds_init(&erase_bonds);
    power_management_init();
    ble_stack_init();
    adc_configure();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();

    // Start execution.
    NRF_LOG_INFO("Debug logging for UART over RTT started.");
    advertising_start();

    // Enter main loop.
    for (;;)
    {
        idle_state_handle();
    }
}

I've added nrfx_saadc.c to the nRF_Drivers project folder.

I looked at sdk_config.h for the proximity example and set relevant defines accordingly:

// <e> NRFX_SAADC_ENABLED - nrfx_saadc - SAADC peripheral driver
//==========================================================
#ifndef NRFX_SAADC_ENABLED
#define NRFX_SAADC_ENABLED 1
#endif

// <e> SAADC_ENABLED - nrf_drv_saadc - SAADC peripheral driver - legacy layer
//==========================================================
#ifndef SAADC_ENABLED
#define SAADC_ENABLED 1
#endif

The app calls read_battery_voltage() in response to a message sent from the connected BLE central (iOS app). saadc_event_handler(...) is never called.

Thank you for any help. -Tim

Parents
  • Solution: nrf_drv_saadc allows NULL for p_config in call to nrf_drv_saadc_init and uses default configuration. I'm using nrfx_saadc and nrfx_saadc_init requires non-NULL p_config. Issue resolved once I added:

        nrfx_saadc_config_t saadc_config = NRFX_SAADC_DEFAULT_CONFIG;
    

    before calling nrfx_saadc_init and changed call to nrfx_saadc_init to:

        ret_code_t err_code = nrfx_saadc_init(&saadc_config, saadc_event_handler);
    

    Thanks.

Reply
  • Solution: nrf_drv_saadc allows NULL for p_config in call to nrf_drv_saadc_init and uses default configuration. I'm using nrfx_saadc and nrfx_saadc_init requires non-NULL p_config. Issue resolved once I added:

        nrfx_saadc_config_t saadc_config = NRFX_SAADC_DEFAULT_CONFIG;
    

    before calling nrfx_saadc_init and changed call to nrfx_saadc_init to:

        ret_code_t err_code = nrfx_saadc_init(&saadc_config, saadc_event_handler);
    

    Thanks.

Children
No Data
Related