bt_nus_send takes time to execute

Hi

We have a project where 1 central shall be connected to 1-7 peripherals. The central sends a short start/stop message to the peripheral then the peripheral starts to notify every 40 ms with approximately 34 bytes data. I started with the central_uart and peripheral_uart examples. Removed the uart code and only use the nus and nus client code. We are using zephyr (nrf connect sdk 2.0.0).

I have changed the connection interval to 24 (30 ms).

I have a basic implementation in the peripheral to send data. Code looks as follows:

    for (;;)
    {
        k_sleep(K_MSEC(40));

        if (sample)
        {
            time_stamp = k_uptime_get_32();
           
            err = bt_nus_send(current_conn, test_data, test_length);
            if (err)
            {
                LOG_WRN("Failed to send data over BLE connection (err %d)", err);
            }

            LOG_INF("Sent %d bytes - %d", test_length, (k_uptime_get_32() - time_stamp));
        }
    }
After a while the bt_nus_send() function takes time, see rtt log below:
[00:20:25.011,383] <inf> peripheral_uart: Received data from: E3:65:7B:28:98:A5 (random) (5 bytes)
[00:20:25.011,413] <inf> peripheral_uart: Started
[00:20:25.040,893] <inf> peripheral_uart: Sent 34 bytes - 0
[00:20:25.081,146] <inf> peripheral_uart: Sent 34 bytes - 1
[00:20:25.121,368] <inf> peripheral_uart: Sent 34 bytes - 0
[00:20:25.161,529] <inf> peripheral_uart: Sent 34 bytes - 0
[00:20:25.201,690] <inf> peripheral_uart: Sent 34 bytes - 0
[00:20:25.241,851] <inf> peripheral_uart: Sent 34 bytes - 0
[00:20:25.282,043] <inf> peripheral_uart: Sent 34 bytes - 1
[00:20:25.322,235] <inf> peripheral_uart: Sent 34 bytes - 0
[00:20:25.362,426] <inf> peripheral_uart: Sent 34 bytes - 0
[00:20:25.402,618] <inf> peripheral_uart: Sent 34 bytes - 0
[00:20:25.442,810] <inf> peripheral_uart: Sent 34 bytes - 0
[00:20:25.483,001] <inf> peripheral_uart: Sent 34 bytes - 0
[00:20:25.523,162] <inf> peripheral_uart: Sent 34 bytes - 0
[00:20:25.563,354] <inf> peripheral_uart: Sent 34 bytes - 0
[00:20:25.603,546] <inf> peripheral_uart: Sent 34 bytes - 0
[00:20:25.643,707] <inf> peripheral_uart: Sent 34 bytes - 0
[00:20:25.683,898] <inf> peripheral_uart: Sent 34 bytes - 0
[00:20:25.724,060] <inf> peripheral_uart: Sent 34 bytes - 1
[00:20:25.811,981] <inf> peripheral_uart: Sent 34 bytes - 47
[00:20:25.861,999] <inf> peripheral_uart: Sent 34 bytes - 9
[00:20:25.911,987] <inf> peripheral_uart: Sent 34 bytes - 9
[00:20:26.011,993] <inf> peripheral_uart: Sent 34 bytes - 59
[00:20:26.111,999] <inf> peripheral_uart: Sent 34 bytes - 59
Why? Is the function blocking? Is the nus service not a good example to start from for our needs?
Best regards
Niklas H
  • Hello,

    The bt_nus_send() is a wrapper around bt_gatt_notify_cb(). The gatt api can be blocking if there are no available buffers in the controller, in such case the call can take some time. It looks to me you may need to look into increasing the throughput, for instance make sure that you are using data length extension to ensure the packets are not fragmented on air. Also you can combine several 34bytes of data into one packet if needed.

    Kenneth

Related