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

NRF52840 BLE transfer using NUS service

image descriptionI am writing an application for sending data received over SPI from a non-nordic chip to nrf52840 to nrf uart app using NUS service. I am using 20 bytes of a packet size the max in NUS. The SPI sampling happens at 4 MHZ. I am receiving a very low throughput of around 2.88kbps. Can anyone guide me how to change BLE setting to maximize the throughput

  • Hi Pranav,

    You need to queue as much as possible using the sd_ble_gatts_hvx() command (what inside ble_nus_string_send) until you receive NRF_ERROR_RESOURCES meaning all the buffer is queued. And then after that you wait for the BLE_GATTS_EVT_HVN_TX_COMPLETE event telling that there are some packets sent, and the buffer is free, then you can queue again using hvx command.

  • Hi, I still have very basic doubts. I have added a snippet of ble_nus code in main question. So basically you are suggesting to use sd_ble_gatts_hvx() command and pass parameters to it that are set in ble_nus_send_string(), and wait for NRF_ERROR_RESOURCES.

    Then I do ble_nus_send_string() to send data over ble link, and every time transfer is completed I get BLE_GATTS_EVT_HVN_TX_COMPLETE event in

    static void on_ble_evt(ble_evt_t * p_ble_evt)

    after which I again start queing? But if I queue using sd_ble_gatts_hvx(), and then call ble_nus_send_string, won't it again queue same data, as the above function is already being called in ble_nus_send_string?

  • Well as you see in the code snippet from NUS in SDK ble_nus_send_string calls in the end sd_ble_gatts_hvx and you just get the status code as return value from ble_nus_send_string. So you should examine this code and once it's NRF_ERROR_RESOURCES you should store the state variables where in your global transfer you are (before this last ble_nus_send_string call!) and restart sending process (state machine) once you get any BLE_GATTS_EVT_HVN_TX_COMPLETE event. And repeat until all data are sent out, simple as that.

  • Thanks for the help. Just one more question there are two event handlers one in main file and one in ble_nus services. Where should I check for event BLE_GATTS_EVT_HVN_TX_COMPLETE?

  • The idea is that all SDK modules and libraries should work as they are an in case they handle some events you should call their handler from you main. All the rest which you do as part of your application logic should live in your main file (or other if you build your modules).

Related