<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>How to check if the NUS TX Buffer is empty?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/69687/how-to-check-if-the-nus-tx-buffer-is-empty</link><description>Hello, 
 in our project we have a nRF52840DK as Central and a custom nRF52832 board as Peripheral. The software on the Central is based on the ble_app_uart_c example and the software on the Peripheral is based on the ble_app_uart example. So we are using</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 21 Dec 2020 13:40:44 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/69687/how-to-check-if-the-nus-tx-buffer-is-empty" /><item><title>RE: How to check if the NUS TX Buffer is empty?</title><link>https://devzone.nordicsemi.com/thread/286061?ContentTypeID=1</link><pubDate>Mon, 21 Dec 2020 13:40:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a9c9bd15-df3c-46c3-a31a-07e307491576</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;t know enough to say for sure, but if you for instance notify every 40 ms and have a connection interval of 40 ms (?) then a possible explanation is that the code snippet that checks buffer filing runs after you have queued a packet but before it has been sent. And this would just continue... Is that what is happening here?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to check if the NUS TX Buffer is empty?</title><link>https://devzone.nordicsemi.com/thread/286059?ContentTypeID=1</link><pubDate>Mon, 21 Dec 2020 13:34:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f8b4a2fc-5c74-48ea-8ba0-7b194af9175c</guid><dc:creator>Michael01101</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;we implemented the counter for the amount of NUS packets in the TX buffer (&lt;span&gt;BLE_NUS_TX_BUFFER)&amp;nbsp;&lt;/span&gt;as recommended:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static int16_t BLE_NUS_TX_BUFFER = 0;


void saadc_callback(nrf_drv_saadc_evt_t const * p_event)    // Every 40ms
{
    ...
    if(BLE_NUS_TX_BUFFER == 0)
    {
        err_code = ble_nus_data_send(&amp;amp;m_nus, stringA, &amp;amp;bytes_to_send, m_conn_handle);
    }
    else
    {
        err_code = ble_nus_data_send(&amp;amp;m_nus, stringB, &amp;amp;bytes_to_send, m_conn_handle);   
    }
    BLE_NUS_TX_BUFFER++;
    ...
}


static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
    switch (p_ble_evt-&amp;gt;header.evt_id)
    {
        case BLE_GATTS_EVT_HVN_TX_COMPLETE:
             BLE_NUS_TX_BUFFER -= p_ble_evt-&amp;gt;evt.gatts_evt.params.hvn_tx_complete.count;
             break;
        ...
    ...
    }
...
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;We use a Connection Interval of 40ms (min. and max.). Equal to the SAADC callback.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Our problem is, that our counter BLE_NUS_TX_BUFFER&amp;nbsp; contains always &amp;quot;1&amp;quot; or higher. It seems, that the TX Buffer is never empty.&amp;nbsp;So the &amp;quot;stringA&amp;quot; gets never sent.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Do you know, why the counter BLE_NUS_TX_BUFFER never reach &amp;quot;zero&amp;quot;?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thank you very much in advance.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to check if the NUS TX Buffer is empty?</title><link>https://devzone.nordicsemi.com/thread/285852?ContentTypeID=1</link><pubDate>Fri, 18 Dec 2020 13:59:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6c64a3c1-8587-4085-a487-74b97e8e09ab</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Yes, that looks correct.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to check if the NUS TX Buffer is empty?</title><link>https://devzone.nordicsemi.com/thread/285847?ContentTypeID=1</link><pubDate>Fri, 18 Dec 2020 13:45:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c0e21fe0-985a-4325-961e-880453a3b572</guid><dc:creator>Michael01101</dc:creator><description>&lt;p&gt;Thank you again for your fast reply.&lt;/p&gt;
&lt;p&gt;So when the BLE_NUS_TX_BUFFER variable is the counter for the amount of NUS packets in the TX buffer, then this would be the right implementation?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
    uint32_t err_code;

    switch (p_ble_evt-&amp;gt;header.evt_id)
    {
        case BLE_GATTS_EVT_HVN_TX_COMPLETE:
             BLE_NUS_TX_BUFFER -= p_ble_evt-&amp;gt;evt.gatts_evt.params.hvn_tx_complete.count;
             break;
             
        ...
    ...
...&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Thank you very much in advance.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to check if the NUS TX Buffer is empty?</title><link>https://devzone.nordicsemi.com/thread/285841?ContentTypeID=1</link><pubDate>Fri, 18 Dec 2020 13:33:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1cf81e37-a103-4e0a-90c3-917beb7fc374</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Yes, that is correct. (Just make sure to check the count when you get a TX complete event, so that if e.g. it is 2, then you decrement the counter by 2 instead of 1.)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to check if the NUS TX Buffer is empty?</title><link>https://devzone.nordicsemi.com/thread/285839?ContentTypeID=1</link><pubDate>Fri, 18 Dec 2020 13:30:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7e8933b3-23f5-4c31-8d04-010a73bb4f5c</guid><dc:creator>Michael01101</dc:creator><description>&lt;p&gt;Thank you very much for your quick response.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;So we have to add the BLE_GATTS_EVT_HVN_TX_COMPLETE case in the ble_evt_handler(...).&lt;/p&gt;
&lt;p&gt;Then we can create a counter which increases every time we call ble_nus_data_send() and decreases every time a&amp;nbsp;&lt;span&gt;BLE_GATTS_EVT_HVN_TX_COMPLETE&amp;nbsp;event occured, is that right?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Finally we can use the counter as an indicator for the amount of NUS packets in the TX buffer?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thank you very much in advance.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to check if the NUS TX Buffer is empty?</title><link>https://devzone.nordicsemi.com/thread/285830?ContentTypeID=1</link><pubDate>Fri, 18 Dec 2020 13:18:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:09c9bee8-95aa-44e1-a853-e4de794a5370</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;There is unfortunately no SoftDevice API to check the number of packets in the Tx buffer. The only way to know this is to count the number of packets you queue and the BLE_GATTS_EVT_HVN_TX_COMPLETE events (note that the &lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s132.api.v6.1.1/structble__gatts__evt__hvn__tx__complete__t.html"&gt;count&lt;/a&gt; can be higher than 1).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>