Nrf52810 crashes when connected to central and using SAADC

Hello,

I am finishing the development of a device based on the nrf52810. When the device is connected to a central, I would like to periodically measure the voltage on VDD to get a rough idea of how low the devices battery is. I added a characteristic to the lbs service I am testing which contains this data. The characteristic works fine and its values can be read when connected. The issue I am facing is that I cannot properly use SAADC when the device is connected to the central. When disconnected, SAADC operates normally, however once connected, it will only sample once, then the next time the sampling timer fires the program crashes. I am using an app timer instead of the regular nrf_drv timer in the saadc example if that helps. I will post my code for the SAADC function as well. I tried preforming the calculation in the callback with no success.

void saadc_callback(nrf_drv_saadc_evt_t const * p_event) 
{
    if (p_event->type == NRF_DRV_SAADC_EVT_DONE) {
        saadc_conversion_complete = true;
        //NRF_LOG_INFO("SAADC conversion done.");
        //NRF_LOG_FLUSH();
        int16_t adc_result = saadc_buffer[0];
        float battery_voltage = (((adc_result)*(REFERENCE_VOLTAGE))/1024)*6;

        if (battery_voltage < 3.4)
        {
            led_event_handler(1);
        }
        else 
        {
            led_event_handler(0);
        }

        NRF_LOG_INFO("Made to end of sampling event");
        
        
        
    }
}

void saadc_init(void) // lastest code
{
    ret_code_t err_code;

    // Initialize SAADC
    err_code = nrf_drv_saadc_init(NULL, saadc_callback);
    APP_ERROR_CHECK(err_code);

    // Configure SAADC channel
    nrf_saadc_channel_config_t channel_config = 
        NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_VDD);
    
    err_code = nrf_drv_saadc_channel_init(SAADC_CHANNEL, &channel_config);
    APP_ERROR_CHECK(err_code);

    // Allocate buffer
    err_code = nrf_drv_saadc_buffer_convert(saadc_buffer, SAADC_BUFFER_SIZE);
    APP_ERROR_CHECK(err_code);
}

float get_battery_voltage(void) 
{
    ret_code_t err_code;
    saadc_conversion_complete = false;

    // Start SAADC conversion
    err_code = nrf_drv_saadc_sample();
    APP_ERROR_CHECK(err_code);

    // Wait for conversion to complete
    while (!saadc_conversion_complete); // appears that the while loop is blocking when connected. Likely was working fine during testing because of advertising.

    //if (!saadc_conversion_complete)
    //{
    //    nrf_delay_ms(100); //testing to see if the while loop is interupting the connection to central device
    //}
    // Calculate voltage from SAADC result
    // Assuming 12-bit resolution and internal reference voltage of 0.6V
    //int16_t adc_result = saadc_buffer[0];
    float battery_voltage = 0;//(((adc_result)*(REFERENCE_VOLTAGE))/1024)*6;

    //saadc_conversion_complete = false;

    NRF_LOG_INFO("SAADC RUNNING");

    return battery_voltage;
}

uint8_t detect_low_battery(void)
{
    float voltage = get_battery_voltage();
    //uint8_t low_Battery = 0; 

    if (voltage < 3.4)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

Thank you for the assistance,

M

Parents Reply Children
No Data
Related