nrfx SAADC values missing when combined with BLE

Hello,

I have been developing an embedded system where, data would be captured from analog sensors and then processed and sent by Bluetooth to a android device. In my project, I am capturing ECG data from one of the analog pin of nRF52840 DevKit. Then, I save data in a buffer and send them by BLE. In the Android phone, we have a custom App where, we would receive those data and plot them.

Problem: I face an issue of missing ADC data on the mobile App side when I am trying to send 96 samples in a Bluetooth packet (96 samples are converted to 192 bytes, as each sample is 2 bytes). When I send 48 samples (96 bytes) in a Bluetooth packet, I still have some data missed on the Mobile App side but, the problem is not severe in that case. When ADC data checked without combining the BLE part, they look fine.

Description: I have taken the SAADC example from here and merged it with blue_app_uart example. I am sampling the ADC at a rate of 1kHz and storing 96 samples in the buffer. Few of the important BLE settings are as follows,

Min Conn Interval 95 ms

Max Conn Interval 105 ms

BLE GAP EVENT LENGTH 76 (95 MS)

GAP DATA LENGTH 251

Thank you for reading my post. I would be very grateful if anyone can point out a possible solution for this case. I can also share the code here if it seems required. Please let me know.

  • Hi,

    The first item to point out is that BLE is lossless so the data you queue up for transfer is either successfully sent over, or the connection is terminated. This in turns means that the missing data you're observing is happening on the application level somewhere. If you are 100% certain that the data you receive is correct, you have to look into if something is wrong in the application when you put together SAADC + BLE.

    One thing to consider is if you queue things faster than you send it (which will fill the SoftDevice buffers) will cause issues on the next attempt to queue data to the SoftDevice.

    Could you check if that is the case?

    Could you check the error code that nus_send returns?

    If you need any assistance with checking error code sent by ble_nus_send, feel free to upload the project here and we'll have a look.

    Kind regards,
    Andreas

  • Hello,

    Thank you for your prompt response. I have tried to go through the issues you mentioned.

    I have done the following modification/checks:

    1. For the BLE transmission (Tx), I send the packet through BLE only after storing 96 samples. I also added a partial code to increase the queue size of BLE Tx. The code is as follows (this code is added inside nrf_sdh_ble.c file): 

    code:

    ble_cfg.conn_cfg.params.gatts_conn_cfg.hvn_tx_queue_size = 10;

        ret_code = sd_ble_cfg_set(BLE_CONN_CFG_GATTS, &ble_cfg, *p_ram_start);
        if (ret_code != NRF_SUCCESS)
        {
            NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_CONN_CFG_GATTS.", nrf_strerror_get(ret_code));

        }

    2. I have added an APP_ERROR_CHECK FOR ble_nus_data_send function. After putting the error check, I get an error 5 code (no error found) in my debug window. If APP_ERROR_CHECK not added, then the code runs successfully and BLE Tx is active (debug window shows BLE event 0x57 in this cases) for around 30 minutes. After 30 minutes, the BLE gets disconnected by itself. I am adding below the debug info when error check is added after bee_nus_data_send:

    Debug:

    <debug> nrf_sdh: State request: 0x00000000

    <debug> nrf_sdh: State change: 0x00000000
    <debug> nrf_sdh: State change: 0x00000001
    <debug> nrf_sdh_ble: RAM starts at 0x200034C8
    <info> SAADC: Function: nrfx_saadc_init, error code: NRF_SUCCESS.
    <info> app: SAADC evt 5
    <error> app: ERROR 5 [NRF_ERROR_NOT_FOUND] at E:\nordic_final\nRF5_SDK_17.0.2_d674dde\examples\saadc_v0.3\main.c:1034
    PC at: 0x00032441

    <error> app: End of error report

    Moreover, I think I need to describe a bit about our custom Android App. In the Android App, we are converting and combining the hex (sent from the device) to form the same values we get from the SAADC. We have considered the concept that SAADC is filling the buffer in 2's complement form while building the App. I have added 2 snapshots of the App where, i) we can see the value we are getting through BLE and ii) we can see the plotting of a real time ECG (from this view, it seems like we are missing some ADC samples)

    i)                                                   ii)

                                 

    Also, I think it will be convenient if I share the project here. At this point, the code may look little bit messy. Please pardon me for that. I will clean them up and add few more things at later stages. I am using SDK V17.0.2 and nRF 52840 DevKit. The project is as follows:

    nus_saadc_v1.3.zip

    Thank you again for your help and sorry for being late. Please let me know if you need any other info.

  • Hi,

    I looked over this together with a colleague and we have the following thoughts that you should have a look at.

    Mahfuz said:
    1. For the BLE transmission (Tx), I send the packet through BLE only after storing 96 samples. I also added a partial code to increase the queue size of BLE Tx. The code is as follows (this code is added inside nrf_sdh_ble.c file): 

    Regarding item 1 we see that you make changes directly to nrf_sdh_ble.c. We don't recommend this practice as changing the source code in the drivers might lead to issues down the line as there might be other dependencies that expects the drivers to behave as they are implemented.

    Instead if you wish to get more space for your samples, you could implement a ring-buffer that collects all the data which then dumps that data in to ble_nus_data_send whenever available. Every sample should go through the ring buffer to ensure that the data is properly organized

    Mahfuz said:
    2. I have added an APP_ERROR_CHECK FOR ble_nus_data_send function. After putting the error check, I get an error 5 code (no error found) in my debug window. If APP_ERROR_CHECK not added, then the code runs successfully and BLE Tx is active (debug window shows BLE event 0x57 in this cases) for around 30 minutes. After 30 minutes, the BLE gets disconnected by itself. I am adding below the debug info when error check is added after bee_nus_data_send:

    This must be where the data is lost. 

    You get nRF_ERROR_NOT_FOUND when you attempt to call ble_nus_data_send without a valid connection handle/without being connected. Implement some more error handling in combination with APP_ERROR_CHECK for ble_nus_data_send should give some more hints about where stuff breaks.

    For now, try out the suggestions mentioned and see if that helps narrow down the issues and implementation. As for cleaning up the project, it will help us more easily narrow where the issue might be, but no worries about that for now. If we need to revisit the code at a later stage in this support case you can consider sharing a cleaner project

    (Edit: Removed one incomplete, duplicate sentence in my reply) 

    Kind regards,
    Andreas

  • Hi,

    Thank you so much for your response. I will make the changes and modifications you mentioned and get back to you soon. I will obviously clean up the project next time I share. Sorry for any inconvenience.

  • Hi,

    Happy to help, and no worries at all about the inconvenience :) Just keep me posted if you find the solution and/or feel free to open new tickets regarding new questions that are not related to this specific topic

    Kind regards,
    Andreas

Related