<?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>BLE interrupt priority question and BLE event lost issue</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/26832/ble-interrupt-priority-question-and-ble-event-lost-issue</link><description>Hi, 
 I use nrf51822 + S130 platform, in my application I used ADC,UART and APP TIMER, I mens only these can cause interruptions，all these interrupt priority is 3(APP_IRQ_PRIORITY_LOWEST), The related code is as follows： 
 ADC init function in main</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 14 Nov 2017 02:33:35 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/26832/ble-interrupt-priority-question-and-ble-event-lost-issue" /><item><title>RE: BLE interrupt priority question and BLE event lost issue</title><link>https://devzone.nordicsemi.com/thread/105468?ContentTypeID=1</link><pubDate>Tue, 14 Nov 2017 02:33:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bc4786be-5477-440f-9992-d1eb6f7296e1</guid><dc:creator>Ian</dc:creator><description>&lt;p&gt;I call SendDatas() in main loop every 10ms:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;int main(void)
{
     init code

     for(;;)
     {
          if (timer told me 10ms is passed)
          {
                 SendDatas();
          }

          other code
     }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Actually it seems the problem is resolved, I test from yesterday to now the program work well, I take RK&amp;#39;s advice and modify my code as this:&lt;/p&gt;
&lt;p&gt;The code when receive  BLE_EVT_TX_COMPLETE event:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;   case BLE_EVT_TX_COMPLETE:
            for (uint8_t i = 0; i &amp;lt; CENTRAL_LINK_COUNT; i++)
            {
                if (p_gap_evt-&amp;gt;conn_handle == m_conn_handle_nus_c[i])
                {
                    CRITICAL_REGION_ENTER();
                    m_conn_tx_packet_count[i] += p_ble_evt-&amp;gt;evt.common_evt.params.tx_complete.count;
                    CRITICAL_REGION_EXIT();
                    
                    break;
                }
            }
            break;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The SendDatas funciton:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; bool SendDatas(ptSendPar pars)
    {
          //some code
    
         while (there are datas to send)
         {
              while (1)     //while loop until time out or BLE disconnect or data send succuss
              {
                  while (m_conn_tx_packet_count[pars-&amp;gt;peripheral_id] == 0)  //wait free tx packet 
                  {
                      if (timeout)
                      {
                          return false;
                      }
         
                     if (disconnect)
                     {
                          return false;
                     }
                  }
    
                 if (ble_nus_c_string_send(p_ble_nus_c, &amp;amp;txbuf[send_index], len) == NRF_SUCCESS)
                 {
                      CRITICAL_REGION_ENTER();
                      m_conn_tx_packet_count[pars-&amp;gt;peripheral_id]--;
                      CRITICAL_REGION_EXIT();
                    
                      break;
                  }
    
                  the length of datas to send -= len
           }
    }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The difference between the new code and the old code is:&lt;/p&gt;
&lt;p&gt;1、new code use &lt;code&gt;m_conn_tx_packet_count[i] += p_ble_evt-&amp;gt;evt.common_evt.params.tx_complete.count;&lt;/code&gt; and the old code use just &lt;code&gt;m_conn_tx_packet_count[i] ++;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;2、new code use &lt;code&gt;CRITICAL_REGION_ENTER&lt;/code&gt; and &lt;code&gt;CRITICAL_REGION_EXIT&lt;/code&gt; then write &lt;code&gt;m_conn_tx_packet_count&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;I still do not understand why the old code will cause this problem(&lt;code&gt;m_conn_tx_packet_count&lt;/code&gt; always be 0), even &lt;code&gt;p_ble_evt-&amp;gt;evt.common_evt.params.tx_complete.count&lt;/code&gt; is greater than 1 and i only increase &lt;code&gt;m_conn_tx_packet_count&lt;/code&gt; by 1, it just will let the max value of &lt;code&gt;m_conn_tx_packet_count&lt;/code&gt; less than it should be but will not make &lt;code&gt;m_conn_tx_packet_count&lt;/code&gt; be 0.&lt;/p&gt;
&lt;p&gt;So is &lt;code&gt;CRITICAL_REGION_ENTER&lt;/code&gt; and &lt;code&gt;CRITICAL_REGION_EXIT&lt;/code&gt; solved my problem? I remember I used them before and there is still this problem, but I&amp;#39;m not so sure and I&amp;#39;m going to do more test about this.&lt;/p&gt;
&lt;p&gt;Anyone know what&amp;#39;s the exactly issue in my old code? Any suggest will be helpful~&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE interrupt priority question and BLE event lost issue</title><link>https://devzone.nordicsemi.com/thread/105467?ContentTypeID=1</link><pubDate>Mon, 13 Nov 2017 14:57:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:32e412e5-578a-4dce-8bce-be25c556a401</guid><dc:creator>Petter Myhre</dc:creator><description>&lt;p&gt;I don&amp;#39;t know exactly when you call SendDatas(), but as RK mentioned, you don&amp;#39;t necessarily receive one BLE_EVT_TX_COMPLETE for every time you send a write command, you have a count field that tells you have many was sent. If you get an unexpected m_conn_tx_packet_count of 0 you can try to do some printfs to figure out why it ends up at 0.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE interrupt priority question and BLE event lost issue</title><link>https://devzone.nordicsemi.com/thread/105466?ContentTypeID=1</link><pubDate>Mon, 13 Nov 2017 03:47:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e9f16068-3c3f-4a1c-a03a-bc55794cb175</guid><dc:creator>Ian</dc:creator><description>&lt;p&gt;I also believe that the Nordic will not make such a mistake. But if so I really can not explain what happened to my program, include the last question &amp;quot;&lt;code&gt;nrf_dfu_wait&lt;/code&gt; never return if I do not disable uart&amp;quot;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE interrupt priority question and BLE event lost issue</title><link>https://devzone.nordicsemi.com/thread/105465?ContentTypeID=1</link><pubDate>Mon, 13 Nov 2017 03:38:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6174eba5-7baa-49c1-a43e-53d8b650c500</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;BLE events never get lost. The stack has the highest priority of all, all events it generates it queues up until you find time to receive them. It never loses TX events or callbacks telling you a buffer is available. It just doesn&amp;#39;t.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE interrupt priority question and BLE event lost issue</title><link>https://devzone.nordicsemi.com/thread/105464?ContentTypeID=1</link><pubDate>Mon, 13 Nov 2017 03:33:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5899548e-66c3-4503-b3d1-3ac50f6aa79c</guid><dc:creator>Ian</dc:creator><description>&lt;p&gt;If other ISR  with priority equal to or higher than the priority of SD_EVT_IRQn cost too much time(e.g. APP TIMER event handler), the ble_event_handler cannot be executed in time，the BLE events also don&amp;#39;t get lost in this case?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE interrupt priority question and BLE event lost issue</title><link>https://devzone.nordicsemi.com/thread/105463?ContentTypeID=1</link><pubDate>Mon, 13 Nov 2017 03:27:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bd20dadd-c767-4a43-a01b-38399eb7dee0</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;BLE events don&amp;#39;t get lost, ever.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE interrupt priority question and BLE event lost issue</title><link>https://devzone.nordicsemi.com/thread/105462?ContentTypeID=1</link><pubDate>Mon, 13 Nov 2017 03:25:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:40c2b77d-cc7d-4265-aaac-ff67ce906a0f</guid><dc:creator>Ian</dc:creator><description>&lt;p&gt;I debug the program again, I watch &lt;code&gt;NVIC_IP&lt;/code&gt; register and call &lt;code&gt;NVIC_GetPriority(SD_EVT_IRQn)&lt;/code&gt; function,they all  showed that the priority of SD_EVT_IRQn is 3! In this case,maybe my other interrupt which the priority is also 3 cause the SD EVENT lost i guess. But 3 is the lowest priority i can use, I have no choice without it, unless I do not use interrupt.&lt;/p&gt;
&lt;p&gt;I tried change the priority of SD_EVT_IRQn use &lt;code&gt;NVIC_SetPriority(SD_EVT_IRQn, 2)&lt;/code&gt;, but no matter where i use it (e.g. before or after &lt;code&gt;SOFTDEVICE_HANDLER_INIT&lt;/code&gt;,before or after &lt;code&gt;softdevice_enable_get_default_config&lt;/code&gt;, before or after &lt;code&gt;softdevice_enable&lt;/code&gt;) the program will crash.&lt;/p&gt;
&lt;p&gt;In conclusion, there are two questions  I want to confirm:&lt;/p&gt;
&lt;p&gt;1、Will my interrupt with the same priority of SD_EVT_IRQn cause BLE EVENT lost?&lt;/p&gt;
&lt;p&gt;2、How can I set or change the priority of SD_EVT_IRQn?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE interrupt priority question and BLE event lost issue</title><link>https://devzone.nordicsemi.com/thread/105469?ContentTypeID=1</link><pubDate>Mon, 13 Nov 2017 03:07:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1baff666-0e74-4db5-9340-7c7259456020</guid><dc:creator>Ian</dc:creator><description>&lt;p&gt;Oh  that&amp;#39;s my carelessness, but this will not cause my problem, if every time &lt;code&gt;ble_nus_c_string_send&lt;/code&gt; return &lt;code&gt;NRF_SUCCESS&lt;/code&gt; I can receive &lt;code&gt;BLE_EVT_TX_COMPLETE&lt;/code&gt; event, &lt;code&gt;m_conn_tx_packet_count&lt;/code&gt; will never be 0 after the old &lt;code&gt;ble_nus_c_string_send&lt;/code&gt; success and the new &lt;code&gt;ble_nus_c_string_send&lt;/code&gt; be called.But my problem is sometimes &lt;code&gt;m_conn_tx_packet_count&lt;/code&gt; always be 0.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE interrupt priority question and BLE event lost issue</title><link>https://devzone.nordicsemi.com/thread/105461?ContentTypeID=1</link><pubDate>Sun, 12 Nov 2017 21:25:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c6d71f52-768f-470e-86f7-acf4b514885b</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;I haven&amp;#39;t read all the way through your entire application however&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Documentation for ble_evt_tx_complete_t&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Data Fields
uint8_t 	count
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;but you are always assuming the count is one and using &amp;#39;++&amp;#39;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>