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

Send BLE data from saadc_callback

Hi,

There is something I don't understand and I request your help to answer my interrogation. What I want to do is a SAADC conversion then send the result of this conversion over BLE.

So basically my first working version looks something like (simplified for readability) :

#define SAMPLES_IN_BUFFER 1
static nrf_saadc_value_t m_buffer[SAMPLES_IN_BUFFER];
static int16_t value_to_send = 0;


void send_value()
{
    ble_custom_on_sensor_change(m_conn_handle, &m_ble, value_to_send);    // Same as ble_lbs_on_button_change
    if (err_code != NRF_SUCCESS &&
        err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
        err_code != NRF_ERROR_INVALID_STATE &&
        err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
    {
        APP_ERROR_CHECK(err_code);
    }
}


void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
    if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
    {
        nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);

        /* Send the value */
        value_to_send = p_event->data.done.p_buffer[0];
    }
}

void saadc_init(void)
{
...
}


int main(void)
{
    ...

    saadc_init();

    // Enter main loop.
    for (;;)
    {
        nrf_delay_ms(500);
        nrf_drv_saadc_sample();
        send_value();

        if (NRF_LOG_PROCESS() == false)
        {
            power_manage();
        }
    }
}

But with this version, I am afraid that send_value() could be called before nrf_drv_saadc_sample() has updated the value_to_send.

So now I want to call the function send_value() within the saadc_callback but it doesn't work and I can't find a way to Debug it. It looks like it failed during the sd_ble_gatts_hvx() call (from the ble_lbs_on_button_change function (or ble_custom_on_sensor_change in my case).

So basically, this doesn't work, doesn't even succeed to establish a connection in BLE :

#define SAMPLES_IN_BUFFER 1
static nrf_saadc_value_t m_buffer[SAMPLES_IN_BUFFER];


void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
    if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
    {
        nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);

        /* Send the value */
        ble_custom_on_sensor_change(m_conn_handle, &m_ble, p_event->data.done.p_buffer[0]);    // Same as ble_lbs_on_button_change
        if (err_code != NRF_SUCCESS &&
            err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
            err_code != NRF_ERROR_INVALID_STATE &&
            err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
        {
            APP_ERROR_CHECK(err_code);
        }
    }
}

void saadc_init(void)
{
...
}


int main(void)
{
    ...

    saadc_init();

    // Enter main loop.
    for (;;)
    {
        nrf_delay_ms(500);
        nrf_drv_saadc_sample();

        if (NRF_LOG_PROCESS() == false)
        {
            power_manage();
        }
    }
}

Any idea why ?

Thanks

Parents Reply Children
No Data
Related