<?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>SPI &amp;amp; Bluetooth best practices</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/80640/spi-bluetooth-best-practices</link><description>Hi All, 
 
 I&amp;#39;m a little rusty on my embedded C and was wondering if I could get a few broad pointers on the best way to organize this research system I&amp;#39;m putting together. 
 
 The system is basic: I have an NRF52840 that talks to a custom IC via SPI</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 21 Oct 2021 21:29:51 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/80640/spi-bluetooth-best-practices" /><item><title>RE: SPI &amp; Bluetooth best practices</title><link>https://devzone.nordicsemi.com/thread/335373?ContentTypeID=1</link><pubDate>Thu, 21 Oct 2021 21:29:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2ea7096e-c5be-4fac-bbcd-9c17079d539a</guid><dc:creator>ryerye120</dc:creator><description>&lt;p&gt;Karl!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
[quote userid="87869" url="~/f/nordic-q-a/80640/spi-bluetooth-best-practices/335221#335221"]Great! Please do not hesitate to open another ticket about it if you encounter any issues. The sniffer will make it easy to spot any bottlenecks in your throughput, or other unintended slow-downs.[/quote]
&lt;p&gt;Yea sorry for having this ticket spiral out of control - luckily it looks like&amp;nbsp;we may have solved my issues.&lt;/p&gt;
[quote userid="87869" url="~/f/nordic-q-a/80640/spi-bluetooth-best-practices/335221#335221"]The SoftDevice will need to be allocated enough FLASH and RAM to be able to meet its configured operation. So, if you increase the buffer sizes, or number of concurrent connections, etc. the SoftDevice may need to be allocated more resources.[/quote]
&lt;p&gt;This makes a lot of sense. I&amp;#39;ll keep an eye out for any more RAM warnings. I actually updated the memory allocation to satisfy my larger MTU - that last increase may have given me enough space for my latest updates.&lt;/p&gt;
[quote userid="87869" url="~/f/nordic-q-a/80640/spi-bluetooth-best-practices/335221#335221"]I would rather recommend that you make an estimate for how many notifications that may be queued in a single connection interval, and increase this to account for possible retransmissions (alternatively you could implement specific error handling for the NRF_ERROR_RESOURCES that buffers the notification for later retransmitting).[/quote]
&lt;p&gt;What I ended up doing was calculating how many notifications I expect to have in 7.5ms ( ~8), rounded that up to 10, and then tripled it. Does that seem fair?&lt;br /&gt;&lt;br /&gt;Right now things seem to be working. I&amp;#39;d like to rubber ducky/walk through my fixes. Hopefully, you&amp;#39;ll be able to point out anything seriously wrong&amp;nbsp;in my thought process and&amp;nbsp;if not, others will be able to see my full debrief and learn from it.&lt;br /&gt;&lt;br /&gt;Ultimately my problem was that I was unable to&amp;nbsp;send data over bluetooth as fast as I wanted. My 1Mbps spec is well within&amp;nbsp;BLE5&amp;#39;s&amp;nbsp;capabilities but my code wasn&amp;#39;t up to snuff. To achieve a demo of what will eventually be a full 1Mbps&amp;nbsp;SPI &amp;lt;-&amp;gt; BLE &amp;lt;-&amp;gt; USB link, I had to make a few distinct changes. I started with the ble_app_uart peripheral example.&lt;/p&gt;
&lt;p&gt;First I had to make sure my peripheral and central devices were operating in the 2M PHY mode. To achieve this I&amp;nbsp;put in an option to run the following block of code after my link was established:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;  ble_gap_phys_t const phys =
  {
      .rx_phys = BLE_GAP_PHY_2MBPS,
      .tx_phys = BLE_GAP_PHY_2MBPS,
  };
  err_code = sd_ble_gap_phy_update(m_conn_handle, &amp;amp;phys);
  APP_ERROR_CHECK(err_code);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;To avoid weird specifics, let&amp;#39;s just say that every time I click one of my dev kit&amp;#39;s buttons, this block is run.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;Second, I had to increase my notification queue size. This was a little tricky because I&amp;#39;m not that good yet following SDKs, but thanks to Karl&amp;#39;s help I was able to figure out where to actually increase my queue size without breaking anything. In ble_stack_init(), there&amp;#39;s a function called nrf_sdh_ble_default_cfg_set(). This function helps set certain ble_stack parameters, the MTU, number of links,&amp;nbsp;etc. By default it doesn&amp;#39;t update your&amp;nbsp;notification queue size, but you can.&amp;nbsp;Inside nrf_sdh_ble.c, there exists nrf_sdh_ble_default_cfg_set(). I added the below block at the end of the function. After the fact I foung this &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/64837/change-queue-length-of-handle-value-notifications-sdk-15-3-0"&gt;thread&lt;/a&gt;&amp;nbsp;which discusses a similar fix - the OP in that thread was kind of an *** though.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;    ble_cfg.conn_cfg.params.gatts_conn_cfg.hvn_tx_queue_size = 30;
    ret_code = sd_ble_cfg_set(BLE_CONN_CFG_GATTS, &amp;amp;ble_cfg, *p_ram_start);
    if (ret_code != NRF_SUCCESS)
    {
        NRF_LOG_ERROR(&amp;quot;sd_ble_cfg_set() returned %s when attempting to set BLE_CONN_CFG_GATTS.&amp;quot;, nrf_strerror_get(ret_code));
    }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;These two fixes helped me push my BLE transfers&amp;nbsp;to run faster and without breaking but I still wasn&amp;#39;t able to run everything as fast as I wanted. The final puzzle piece dawned on me when re-reading something Karl said:&lt;/p&gt;
[quote userid="87869" url="~/f/nordic-q-a/80640/spi-bluetooth-best-practices/335221#335221"]The highest throughput is not achieved with 7.5 ms a connection interval, but rather with a longer one where you send multiple maximum-length packets.&lt;br /&gt;This increases the throughput by reducing the number of bytes lost to overhead.[/quote]
&lt;p&gt;While I have kept a small connection interval, it occurred to me that I wasn&amp;#39;t maximizing how much data I was sending at once. In order to minimize overhead, it&amp;#39;s advantageous to pack as many of your samples&amp;nbsp;as possible into a single BLE packet. Due to the nature of my system, I can pack three packets into a maximally sized BLE packet (MTU = 251). Once I implemented this, my code was running without hitting any NRF lack_of_resource errors!&lt;br /&gt;&lt;br /&gt;I am running into&amp;nbsp;some errors on the&amp;nbsp;RX&amp;nbsp;side of my link but that&amp;#39;s for another thread!&lt;br /&gt;&lt;br /&gt;Hopefully this helps others - Karl, thank you so much for all of your help!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI &amp; Bluetooth best practices</title><link>https://devzone.nordicsemi.com/thread/335221?ContentTypeID=1</link><pubDate>Thu, 21 Oct 2021 08:44:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f49b0d83-a638-47f8-b3c4-8ad3828c565d</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello again Ryan,&lt;/p&gt;
[quote user="ryerye120"]As always, thank you for your patience and help! I realize a lot of these are basic questions - I can&amp;#39;t describe how appreciative I am of your time/effort.[/quote]
&lt;p&gt;I am glad to hear you say that, and happy to hear that you have found my comments helpful to your project!&lt;/p&gt;
[quote user="ryerye120"]EDIT: sorry for the double post, I accidentally &amp;#39;replied&amp;#39; to the wrong message so I copied my response to here so it&amp;#39;s easier for you/others to follow.[/quote]
&lt;p&gt;No need to apologize - thank you for editing it to make it easier to follow for both me and others!&lt;/p&gt;
[quote user="ryerye120"]I&amp;#39;m using an nrf52 dongle that&amp;#39;s acting like a BLE&amp;lt;-&amp;gt;USB pipe. I built this based off of the ble_uart_c example and took stuff from the usbd_ble_uart peripheral when needed.&amp;nbsp;[/quote]
&lt;p&gt;This is a good way to go about implementing this.&lt;/p&gt;
[quote user="ryerye120"]I&amp;#39;ve heard of it but haven&amp;#39;t used it - I&amp;#39;ll order another dev kit and set a sniffer up! I completely forgot this was a thing.[/quote]
&lt;p&gt;Great! Please do not hesitate to open another ticket about it if you encounter any issues. The sniffer will make it easy to spot any bottlenecks in your throughput, or other unintended slow-downs.&lt;/p&gt;
[quote user="ryerye120"]Oh you were spot on - I added it to the release build config. This time I added it to the projects common build config and I&amp;#39;m getting a proper error out now.[/quote]
&lt;p&gt;This is a common occurrence, no worries! The proper error logs are very helpful in debugging any driver related issues.&lt;/p&gt;
[quote user="ryerye120"]&amp;quot;&lt;span&gt;Too many notifications queued.&amp;quot; &amp;nbsp;For that user it seems to have boiled down to their notification queue being too small&lt;/span&gt;[/quote]
&lt;p&gt;This is the most common reason for the NRF_ERROR_RESOURCES error code being returned from sd_ble_gatts_hvx - queueing notifications faster than they are being sent. Increasing the queue size will only solve the root cause in the case that the queue is too small to hold all the notifications that will be queued each connection interval, and all queued notifications are being sent each connection event.&lt;br /&gt;Usually, one would additionally have to look at increasing the number of notifications sent per connection event. This could be done by increasing the MTU size, using a different PHY, and increasing the connection event length so that multiple notifications may be sent each connection event.&lt;/p&gt;
[quote user="ryerye120"]The&amp;nbsp;linked thread implies that this queue size is part of the BLE stack. If that&amp;#39;s the case the BLE &amp;#39;stack&amp;#39; includes&amp;nbsp;not just the BLE drivers but also the memory interface between the CPU &amp;amp; the drivers. Is that right?&amp;nbsp;[/quote]
&lt;p&gt;The SoftDevice will need to be allocated enough FLASH and RAM to be able to meet its configured operation. So, if you increase the buffer sizes, or number of concurrent connections, etc. the SoftDevice may need to be allocated more resources.&lt;/p&gt;
[quote user="ryerye120"]Firstly, is there a max queue size? Ideally i&amp;#39;d just set it to the max using&amp;nbsp;sd_ble_cfg_set() - BUT I can&amp;#39;t find a single reference to&amp;nbsp;sd_ble_cfg_set() in the existing solution. Do you have any pointers to where I may be able to find an example so I can safely call&amp;nbsp;sd_ble_cfg_set() with a new queue size?[/quote]
&lt;p&gt;I would rather recommend that you make an estimate for how many notifications that may be queued in a single connection interval, and increase this to account for possible retransmissions (alternatively you could implement specific error handling for the NRF_ERROR_RESOURCES that buffers the notification for later retransmitting).&lt;br /&gt;sd_ble_cfg_set may be called at any time when the SoftDevice is enabled, as long as the BLE part of the SoftDevice is not yet enabled. Please &lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.s140.api.v7.3.0%2Fgroup___b_l_e___c_o_m_m_o_n___f_u_n_c_t_i_o_n_s.html&amp;amp;anchor=ga4edae2bac8c68b672c0fa101ed2c687f"&gt;see the note in the sd_ble_cfg_set API reference for more information about this&lt;/a&gt;.&lt;/p&gt;
[quote user="ryerye120"]Now, assuming I get that set, in order to maximize my throughput, I can set my connection interval to as small as possible (7.5ms, right?), maximize my MTU (to 251, right?), set my PHY to 2M, and then set my total number of links to just 1 (so that I don&amp;#39;t have to share bandwidth).[/quote]
&lt;p&gt;This depends on whether latency is more important than throughput.&lt;br /&gt;The highest throughput is not achieved with 7.5 ms a connection interval, but rather with a longer one where you send multiple maximum-length packets.&lt;br /&gt;This increases the throughput by reducing the number of bytes lost to overhead.&lt;br /&gt;You could see the exact numbers for the difference here in the throughput documentation I referenced earlier.&amp;nbsp;&lt;br /&gt;The increased latency might not be worth it for certain applications, and you can of course still achieve a good throughput using the 7.5 ms connection interval (parameters also specified in the throughput documentation).&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI &amp; Bluetooth best practices</title><link>https://devzone.nordicsemi.com/thread/335174?ContentTypeID=1</link><pubDate>Thu, 21 Oct 2021 04:15:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3ba2931e-4144-4288-b843-99e2dd7b264a</guid><dc:creator>ryerye120</dc:creator><description>&lt;p&gt;EDIT: sorry for the double post, I accidentally &amp;#39;replied&amp;#39; to the wrong message so I copied my response to here so it&amp;#39;s easier for you/others to follow.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Hi Karl,&lt;/p&gt;
&lt;div class="quote-header"&gt;&lt;/div&gt;
&lt;blockquote class="quote"&gt;
&lt;div class="quote-user"&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/80640/spi-bluetooth-best-practices/335010#335010"&gt;Karl Ylvisaker said:&lt;/a&gt;&lt;/div&gt;
&lt;div class="quote-content"&gt;What are you currently using as your central?&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div class="quote-footer"&gt;&lt;/div&gt;
&lt;p&gt;I&amp;#39;m using an nrf52 dongle that&amp;#39;s acting like a BLE&amp;lt;-&amp;gt;USB pipe. I built this based off of the ble_uart_c example and took stuff from the usbd_ble_uart peripheral when needed.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;div class="quote-header"&gt;&lt;/div&gt;
&lt;blockquote class="quote"&gt;
&lt;div class="quote-user"&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/80640/spi-bluetooth-best-practices/335010#335010"&gt;Karl Ylvisaker said:&lt;/a&gt;&lt;/div&gt;
&lt;div class="quote-content"&gt;The best tool for debugging connectivity and throughput issues is&amp;nbsp;&lt;a href="https://www.nordicsemi.com/Products/Development-tools/nRF-Sniffer-for-Bluetooth-LE"&gt;the nRF Sniffer tool&lt;/a&gt;&amp;nbsp;which lets you monitor on-air BLE traffic. Are you familiar with this tool already?&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div class="quote-footer"&gt;&lt;/div&gt;
&lt;p&gt;I&amp;#39;ve heard of it but haven&amp;#39;t used it - I&amp;#39;ll order another dev kit and set a sniffer up! I completely forgot this was a thing.&lt;/p&gt;
&lt;div class="quote-header"&gt;&lt;/div&gt;
&lt;blockquote class="quote"&gt;
&lt;div class="quote-user"&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/80640/spi-bluetooth-best-practices/335010#335010"&gt;Karl Ylvisaker said:&lt;/a&gt;&lt;/div&gt;
&lt;div class="quote-content"&gt;Great - this is the first step in resolving the issue. Could you confirm for me that you&amp;#39;ve added the DEBUG define in the&amp;nbsp;&lt;em&gt;common&amp;nbsp;&lt;/em&gt;build configuration, as mentioned earlier? I notice that your logger is not outputting a complete error message.&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div class="quote-footer"&gt;&lt;/div&gt;
&lt;p&gt;Oh you were spot on - I added it to the release build config. This time I added it to the projects common build config and I&amp;#39;m getting a proper error out now.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;&amp;lt;error&amp;gt; app: ERROR 19 [NRF_ERROR_RESOURCES] at C:\Users\...\main.c:235
PC at: 0x0002B1F5
&amp;lt;error&amp;gt; app: End of error report&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Now&amp;nbsp;after some googling I found this&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/26166/ble_nus_string_send-error-codes"&gt;thread&lt;/a&gt;&amp;nbsp;where someone seems to have the same problem and the issue is - you guessed it &amp;quot;&lt;span&gt;Too many notifications queued.&amp;quot; &amp;nbsp;For that user it seems to have boiled down to their notification queue being too small but while trying to learn how they&amp;#39;ve changed it I&amp;#39;ve come across a few new questions:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span&gt;The&amp;nbsp;linked thread implies that this queue size is part of the BLE stack. If that&amp;#39;s the case the BLE &amp;#39;stack&amp;#39; includes&amp;nbsp;not just the BLE drivers but also the memory interface between the CPU &amp;amp; the drivers. Is that right?&amp;nbsp;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;In ble_stack_init() (in main.c), there is&amp;nbsp;nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &amp;amp;ram_start); The linked thread implies this sets a default queue size to&amp;nbsp;&amp;nbsp;&lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s132.api.v5.0.0/group___b_l_e___g_a_t_t_s___d_e_f_a_u_l_t_s.html#gadeb57ec178918eea438c8b4a4944ebd2"&gt;&lt;code&gt;BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT&lt;/code&gt;&lt;/a&gt;&amp;nbsp;(which is 1). Firstly, is there a max queue size? Ideally i&amp;#39;d just set it to the max using&amp;nbsp;sd_ble_cfg_set() - BUT I can&amp;#39;t find a single reference to&amp;nbsp;sd_ble_cfg_set() in the existing solution. Do you have any pointers to where I may be able to find an example so I can safely call&amp;nbsp;sd_ble_cfg_set() with a new queue size?&lt;br /&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;Now, assuming I get that set, in order to maximize my throughput, I can set my connection interval to as small as possible (7.5ms, right?), maximize my MTU (to 251, right?), set my PHY to 2M, and then set my total number of links to just 1 (so that I don&amp;#39;t have to share bandwidth). Once I get a sniffer up and running, I&amp;#39;ll hopefully be able to confirm that all of that is the case&amp;nbsp;after trying to test it out.&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;As always, thank you for your patience and help! I realize a lot of these are basic questions - I can&amp;#39;t describe how appreciative I am of your time/effort.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Kindest regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Ryan&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI &amp; Bluetooth best practices</title><link>https://devzone.nordicsemi.com/thread/335160?ContentTypeID=1</link><pubDate>Wed, 20 Oct 2021 20:48:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2ed8b77a-c3e9-48c4-b60c-4d25fcff6c8a</guid><dc:creator>ryerye120</dc:creator><description>&lt;p&gt;Hi Karl,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote userid="87869" url="~/f/nordic-q-a/80640/spi-bluetooth-best-practices/335010#335010"]What are you currently using as your central?[/quote]
&lt;p&gt;I&amp;#39;m using an nrf52 dongle that&amp;#39;s acting like a BLE&amp;lt;-&amp;gt;USB pipe. I built this based off of the ble_uart_c example and took stuff from the usbd_ble_uart peripheral when needed.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote userid="87869" url="~/f/nordic-q-a/80640/spi-bluetooth-best-practices/335010#335010"]The best tool for debugging connectivity and throughput issues is&amp;nbsp;&lt;a href="https://www.nordicsemi.com/Products/Development-tools/nRF-Sniffer-for-Bluetooth-LE"&gt;the nRF Sniffer tool&lt;/a&gt;&amp;nbsp;which lets you monitor on-air BLE traffic. Are you familiar with this tool already?[/quote]
&lt;p&gt;I&amp;#39;ve heard of it but haven&amp;#39;t used it - I&amp;#39;ll order another dev kit and set a sniffer up! I completely forgot this was a thing.&lt;/p&gt;
[quote userid="87869" url="~/f/nordic-q-a/80640/spi-bluetooth-best-practices/335010#335010"]Great - this is the first step in resolving the issue. Could you confirm for me that you&amp;#39;ve added the DEBUG define in the&amp;nbsp;&lt;em&gt;common&amp;nbsp;&lt;/em&gt;build configuration, as mentioned earlier? I notice that your logger is not outputting a complete error message.[/quote]
&lt;p&gt;Oh you were spot on - I added it to the release build config. This time I added it to the projects common build config and I&amp;#39;m getting a proper error out now.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;&amp;lt;error&amp;gt; app: ERROR 19 [NRF_ERROR_RESOURCES] at C:\Users\...\main.c:235
PC at: 0x0002B1F5
&amp;lt;error&amp;gt; app: End of error report&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Now&amp;nbsp;after some googling I found this &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/26166/ble_nus_string_send-error-codes"&gt;thread&lt;/a&gt;&amp;nbsp;where someone seems to have the same problem and the issue is - you guessed it &amp;quot;&lt;span&gt;Too many notifications queued.&amp;quot; &amp;nbsp;For that user it seems to have boiled down to their notification queue being too small but while trying to learn how they&amp;#39;ve changed it I&amp;#39;ve come across a few new questions:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span&gt;The&amp;nbsp;linked thread implies that this queue size is part of the BLE stack. If that&amp;#39;s the case the BLE &amp;#39;stack&amp;#39; includes&amp;nbsp;not just the BLE drivers but also the memory interface between the CPU &amp;amp; the drivers. Is that right?&amp;nbsp;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;In ble_stack_init() (in main.c), there is&amp;nbsp;nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &amp;amp;ram_start); The linked thread implies this sets a default queue size to&amp;nbsp;&amp;nbsp;&lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s132.api.v5.0.0/group___b_l_e___g_a_t_t_s___d_e_f_a_u_l_t_s.html#gadeb57ec178918eea438c8b4a4944ebd2"&gt;&lt;code&gt;BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT&lt;/code&gt;&lt;/a&gt;&amp;nbsp;(which is 1). Firstly, is there a max queue size? Ideally i&amp;#39;d just set it to the max using&amp;nbsp;sd_ble_cfg_set() - BUT I can&amp;#39;t find a single reference to&amp;nbsp;sd_ble_cfg_set() in the existing solution. Do you have any pointers to where I may be able to find an example so I can safely call&amp;nbsp;sd_ble_cfg_set() with a new queue size?&lt;br /&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;Now, assuming I get that set, in order to maximize my throughput, I can set my connection interval to as small as possible (7.5ms, right?), maximize my MTU (to 251, right?), set my PHY to 2M, and then set my total number of links to just 1 (so that I don&amp;#39;t have to share bandwidth). Once I get a sniffer up and running, I&amp;#39;ll hopefully be able to confirm that all of that is the case&amp;nbsp;after trying to test it out.&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;As always, thank you for your patience and help! I realize a lot of these are basic questions - I can&amp;#39;t describe how appreciative I am of your time/effort.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Kindest regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Ryan&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI &amp; Bluetooth best practices</title><link>https://devzone.nordicsemi.com/thread/335010?ContentTypeID=1</link><pubDate>Wed, 20 Oct 2021 08:37:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9f82642a-a534-47e9-bb79-4a9f4816c6aa</guid><dc:creator>Karl Ylvisaker</dc:creator><description>[quote user="ryerye120"]From my understanding/corroborated by the link, I need to make sure my central device sets the PHY to 2Mbps. It should be that way already - is there a way to ask the peripheral to print what the PHY setting is via a debug statement?[/quote]
&lt;p&gt;Yes, using the 2M PHY is important to achieve a high throughput.&lt;br /&gt;If you scan and advertise on the 2M PHY the connection will also use 2M PHY. If you wish you could print something in the PHY UPDATED event, in the case that this should change at any time. What are you currently using as your central?&lt;br /&gt;The best tool for debugging connectivity and throughput issues is&amp;nbsp;&lt;a href="https://www.nordicsemi.com/Products/Development-tools/nRF-Sniffer-for-Bluetooth-LE"&gt;the nRF Sniffer tool&lt;/a&gt;&amp;nbsp;which lets you monitor on-air BLE traffic. Are you familiar with this tool already?&lt;br /&gt;In case you are not &lt;a href="https://infocenter.nordicsemi.com/topic/ug_sniffer_ble/UG/sniffer_ble/intro.html"&gt;you can find its documentation here&lt;/a&gt;. It is a powerful tool to wield when developing with BLE.&lt;/p&gt;
[quote user="ryerye120"]As you expected,&amp;nbsp;the device starts to hang and I get thrown into an error. Unfortunately, I&amp;#39;m having a really hard time finding a stack trace&amp;nbsp;that gives me more info. This is what I see in segger:[/quote]
&lt;p&gt;Great - this is the first step in resolving the issue. Could you confirm for me that you&amp;#39;ve added the DEBUG define in the&amp;nbsp;&lt;em&gt;common&amp;nbsp;&lt;/em&gt;build configuration, as mentioned earlier? I notice that your logger is not outputting a complete error message.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
[quote user="ryerye120"]That&amp;#39;s encouraging to hear. When I add the SPI driver into this project, I&amp;#39;ll shift everything just to the spi trx&amp;#39; interrupt handler![/quote]
&lt;p&gt;Great, I&amp;#39;m happy to hear that you found the comment encouraging! &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI &amp; Bluetooth best practices</title><link>https://devzone.nordicsemi.com/thread/334952?ContentTypeID=1</link><pubDate>Tue, 19 Oct 2021 21:11:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:eb791cfa-1dc4-466c-be93-63eb673187a5</guid><dc:creator>ryerye120</dc:creator><description>&lt;p&gt;Hi Karl,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote userid="87869" url="~/f/nordic-q-a/80640/spi-bluetooth-best-practices/334791#334791"] The queue is FIFO, and the notification will be sent as soon as there is room for it in an upcoming connection event.[/quote]
&lt;p&gt;Ahhh this makes a lot of sense.&amp;nbsp;This seems like a likely issue because BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT isn&amp;#39;t defined in my sdk_config.h either.&amp;nbsp;I&amp;#39;m assuming this is a peripheral device thing since it has to do with notifications (which is what I&amp;#39;m working on right now).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
[quote userid="87869" url="~/f/nordic-q-a/80640/spi-bluetooth-best-practices/334791#334791"]You could take a look at&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/topic/sds_s140/SDS/s1xx/ble_data_throughput/ble_data_throughput.html"&gt;the SoftDevice&amp;#39;s throughput documentation&lt;/a&gt; for this - it details the parameters used to achieve the highest connection throughput. Have you calculated what throughput you will need for your application? Is there a constraint on the latency of the transfers?[/quote]
&lt;p&gt;Yes! Ultimately I&amp;#39;ll need to hit about 1Mbps so my goal is to transfer about 500 bits every 500 microsec (which means I should be transferring about 1000 bits total in every millisecond -&amp;gt; 1000kbps).&amp;nbsp;From my understanding/corroborated by the link, I need to make sure my central device sets the PHY to 2Mbps. It should be that way already - is there a way to ask the peripheral to print what the PHY setting is via a debug statement?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
[quote userid="87869" url="~/f/nordic-q-a/80640/spi-bluetooth-best-practices/334807#334807"]I just noticed that you do not immediately check the error code returned from ble_nus_data_send. I strongly recommend that you always pass the returned error code directly to an APP_ERROR_CHECK in order to be alerted to any possible failings by the function. [/quote]
&lt;p&gt;Ah great point! I&amp;#39;ve added an app error check and the pertinent code is now:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;  if ((ble_ready == NRF_SUCCESS)) { // &amp;amp; (app_fifo_get(&amp;amp;EE_rx_fifo, EE_transfer_buffer) == NRF_SUCCESS)){
    // uint16_t size2 = sprintf(m_tx_buffer, &amp;quot;%d \r\n&amp;quot;, *EE_transfer_buffer);
    uint16_t size2 = sprintf(m_tx_buffer, &amp;quot;%d \r\n&amp;quot;, test_counter);
    ble_ready = ble_nus_data_send(&amp;amp;m_nus, (uint8_t *) m_tx_buffer, &amp;amp;size2, m_conn_handle);
    APP_ERROR_CHECK(ble_ready);
  }&lt;/pre&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;As you expected,&amp;nbsp;the device starts to hang and I get thrown into an error. Unfortunately, I&amp;#39;m having a really hard time finding a stack trace&amp;nbsp;that gives me more info. This is what I see in segger:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1634677467903v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I figure the real error I need to fix is 0x00730075 but I can&amp;#39;t figure out what this error is. So far I&amp;#39;ve looked through sdk_errors.h, nrf_error.h, and google and haven&amp;#39;t been able to understand. Clearly, I don&amp;#39;t understand&amp;nbsp;the error hierarchy - is there a more detailed guide somewhere describing these? I&amp;#39;ve been trying to go through the appropriate nrf52 s140 documentation&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s140.api.v7.3.0/group__ble__err.html"&gt;infocenter&lt;/a&gt;&amp;nbsp;but if anything I&amp;#39;m more confused.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;As a side note:&lt;/p&gt;
[quote userid="87869" url="~/f/nordic-q-a/80640/spi-bluetooth-best-practices/334791#334791"]The ble_nus_data_send call could also happen as part of the SPI event handler, so that you do not have to wait in the timer interrupt for the SPI transaction to complete before you can queue the data for transfer, for instance.[/quote]
&lt;p&gt;That&amp;#39;s encouraging to hear. When I add the SPI driver into this project, I&amp;#39;ll shift everything just to the spi trx&amp;#39; interrupt handler!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI &amp; Bluetooth best practices</title><link>https://devzone.nordicsemi.com/thread/334807?ContentTypeID=1</link><pubDate>Tue, 19 Oct 2021 09:35:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d0760e40-6a1c-4287-ad81-9f79ceb83366</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello again, Ryan&lt;br /&gt;&lt;br /&gt;I just noticed that you do not immediately check the error code returned from ble_nus_data_send. I strongly recommend that you always pass the returned error code directly to an APP_ERROR_CHECK in order to be alerted to any possible failings by the function. There might be an unexpected condition or situation that makes a function call fail, and checking the returned error code is the only way you may know that this has happened.&lt;br /&gt;&lt;br /&gt;This is likely also the reason why you might be seeing &amp;#39;lost data&amp;#39; - if the call to queue data for sending fails, and your application proceeds as if it succeeded, it will discard the data and move on to the next.&lt;br /&gt;The returned error codes will also let you know exactly how to remedy the issue (by looking up the returned error code in the function&amp;#39;s API reference) if you have DEBUG defined in your preprocessor defines, like shown in the included image:&lt;br /&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/4061.enabling_5F00_debug_5F00_SES.PNG" /&gt;&lt;br /&gt;This will make a detailed error message be outputted by your logger whenever a non-NRF_SUCCESS error code is passed to an APP_ERROR_CHECK.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI &amp; Bluetooth best practices</title><link>https://devzone.nordicsemi.com/thread/334791?ContentTypeID=1</link><pubDate>Tue, 19 Oct 2021 09:04:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7ad14da3-070d-418f-999e-458387c174ec</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello again,&lt;/p&gt;
[quote user="ryerye120"]As always, Karl, thank you so much for your help.[/quote]
&lt;p&gt;It is no problem at all, Ryan, I am happy to help!&lt;/p&gt;
[quote user="ryerye120"]I&amp;#39;m under the assumption the timer will always leave enough time for the ble_nus_data_send to do its thing.[/quote]
&lt;p&gt;The ble_nus_data_send function does not actually send the data directly, but it queues the notification for sending (placing it in the hvn queue). The queue is FIFO, and the notification will be sent as soon as there is room for it in an upcoming connection event.&lt;br /&gt;- This is why it is important that your queue size is at&amp;nbsp;&lt;em&gt;minimum&amp;nbsp;&lt;/em&gt;large enough to hold all notifications you intend to generate over the span of your connection interval. I say at&amp;nbsp;&lt;em&gt;minimum&lt;/em&gt; because you also need to account for possible retransmission due to corrupted packets or otherwise lost packets (data is never lost in the link since the un-acknowledged packet will be retransmitted).&lt;/p&gt;
[quote user="ryerye120"]I&amp;#39;ve set up&amp;nbsp;the Bluetooth transfer inside the&amp;nbsp;timer&amp;#39;s interrupt handler.[/quote]
&lt;p&gt;The ble_nus_data_send call could also happen as part of the SPI event handler, so that you do not have to wait in the timer interrupt for the SPI transaction to complete before you can queue the data for transfer, for instance.&lt;/p&gt;
[quote user="ryerye120"]The only ones I changed were the min and max_conn_interval:[/quote]
&lt;p&gt;This is indeed how you change the connection interval&amp;nbsp;&lt;em&gt;preferences&amp;nbsp;&lt;/em&gt;of the peripheral - but keep in mind that it is always the central that actually determines what connection parameters will be used. The peripheral may only send connection parameter update requests in order to ask the central to change to parameters within its preferences, but the central is free to reject these requests.&lt;br /&gt;If the central rejects the peripheral can either accept the parameters used by the central, or terminate the link. Many applications such as nRF Connect for smartphone etc. will accommodate any preferences, but some native applications might not in order to save power on the central device. Certain phone OS&amp;#39;s especially limit this, in order to avoid excess power consumption.&lt;/p&gt;
[quote user="ryerye120"]I can&amp;#39;t find any&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s140.api.v7.2.0/group___b_l_e___g_a_t_t_s___d_e_f_a_u_l_t_s.html#gadeb57ec178918eea438c8b4a4944ebd2"&gt;BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT&lt;/a&gt;&amp;nbsp;in the project or ble_gatt.h.[/quote]
&lt;p&gt;Does it not exist in your sdk_config.h file? The sdk_config is used to set all these types of defines and configurations for your project in the nRF5 SDK.&lt;/p&gt;
[quote user="ryerye120"]Are there any other connection parameters you think I should update to speed up the bluetooth speed?[/quote]
&lt;p&gt;You could take a look at&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/topic/sds_s140/SDS/s1xx/ble_data_throughput/ble_data_throughput.html"&gt;the SoftDevice&amp;#39;s throughput documentation&lt;/a&gt; for this - it details the parameters used to achieve the highest connection throughput. Have you calculated what throughput you will need for your application? Is there a constraint on the latency of the transfers?&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI &amp; Bluetooth best practices</title><link>https://devzone.nordicsemi.com/thread/334722?ContentTypeID=1</link><pubDate>Tue, 19 Oct 2021 06:16:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a24df587-c489-4db3-beee-66c2db91b2a1</guid><dc:creator>ryerye120</dc:creator><description>&lt;p&gt;Karl!&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Hi Karl,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve been able to get something to work but it still can&amp;#39;t run at max speed. Per your suggestion, I&amp;#39;ve set up&amp;nbsp;the Bluetooth transfer inside the&amp;nbsp;timer&amp;#39;s interrupt handler. I fixed one problem - I had a &amp;#39;while (ble_ready == success)&amp;#39; gating the ble_nus_data_send - removing that helped things a little. I figure this is okay because I&amp;#39;m under the assumption the timer will always leave enough time for the ble_nus_data_send to do its thing. My timer&amp;#39;s interrupt handler is below:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;pre class="ui-code" data-mode="text"&gt;static void trx_timer_handler(void * p_context)
{
  // fire off a SPI transaction

  // For now, just send an incrementing counter over BLE
  if (test_counter == 15) {
    test_counter = 0;
  } else {
    test_counter = test_counter + 1;
  }
  

  //app_fifo_put(&amp;amp;EE_rx_fifo, test_counter);

  if ((ble_ready == NRF_SUCCESS)) { // &amp;amp; (app_fifo_get(&amp;amp;EE_rx_fifo, EE_transfer_buffer) == NRF_SUCCESS)){
    // uint16_t size2 = sprintf(m_tx_buffer, &amp;quot;%d \r\n&amp;quot;, *EE_transfer_buffer);
    uint16_t size2 = sprintf(m_tx_buffer, &amp;quot;%d \r\n&amp;quot;, test_counter);
    ble_ready = ble_nus_data_send(&amp;amp;m_nus, (uint8_t *) m_tx_buffer, &amp;amp;size2, m_conn_handle);
  }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;This works if I slow the timer down - if it triggers once every 3ms I don&amp;#39;t drop any packets. Unfortunately,&amp;nbsp;when I&amp;nbsp;speed the timer up to trigger every&amp;nbsp;500us (which is my target), I start &amp;quot;dropping&amp;quot; packets. I suspect this is because I&amp;#39;m effectively trying to queue notifications faster than I can send them.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m using most of the default connection parameters from the ble_app_uart example! The only ones I changed were the min and max_conn_interval:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#define MIN_CONN_INTERVAL               MSEC_TO_UNITS(7.5, UNIT_1_25_MS)             /**&amp;lt; Minimum acceptable connection interval (20 ms), Connection interval uses 1.25 ms units. */
#define MAX_CONN_INTERVAL               MSEC_TO_UNITS(7.5, UNIT_1_25_MS)             /**&amp;lt; Maximum acceptable connection interval (75 ms), Connection interval uses 1.25 ms units. */&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Are there any other connection parameters you think I should update to speed up the bluetooth speed? I can&amp;#39;t find any&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s140.api.v7.2.0/group___b_l_e___g_a_t_t_s___d_e_f_a_u_l_t_s.html#gadeb57ec178918eea438c8b4a4944ebd2"&gt;BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT&lt;/a&gt;&amp;nbsp;in the project or ble_gatt.h. Is this something I need to implement?&amp;nbsp;Is there an example that implements this? I&amp;#39;m currently using NRF5 sdk 17 with the nrf52840 DK.&lt;/p&gt;
&lt;p&gt;As always, Karl, thank you so much for your help.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI &amp; Bluetooth best practices</title><link>https://devzone.nordicsemi.com/thread/334503?ContentTypeID=1</link><pubDate>Mon, 18 Oct 2021 08:12:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7cc682de-3d73-458c-baae-5e6fd2494040</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
[quote user="ryerye120"]This was incredibly helpful![/quote][quote user="ryerye120"]Thanks again for all of your help![/quote]
&lt;p&gt;No problem at all, Ryan - I am happy to hear that you found my comment helpful!&lt;/p&gt;
[quote user="ryerye120"]To make things simpler for now, I am leaving the SPI out and just&amp;nbsp;using my 500us timer to increment a counter. I then have an if statement in my main while loop that sends the updated counter to another device over bluetooth. Is this the best way&amp;nbsp;to organize fast transactions through bluetooth?[/quote]
&lt;p&gt;In general, the best way to do this is to handle it as part of an event handler, rather than having it happen in the main loop. This way you can easily control its priority, and compartmentalize the code better (not have everything running in the main loop).&lt;br /&gt;Additionally, for power saving, you should only have the SYSTEM_ON sleep (&lt;em&gt;idle_state_handler&amp;nbsp;&lt;/em&gt;function in the BLE Peripheral examples) happen in the main loop, since this will make your system go to SYSTEM_ON sleep whenever there are no other work to be done.&amp;nbsp;&lt;br /&gt;I would therefore recommend that you either have the transfers happens as the handler to a TIMER CC event, or similar.&lt;/p&gt;
[quote user="ryerye120"]I must be doing something wrong because I&amp;#39;m losing a lot of data (I suspect the bluetooth transactions are taking too long - or the cpu is getting halted for too long so the counter increments more than once between bluetooth transfers. I tried putting the ble_nus_data_send() command in my timer&amp;#39;s interrupt handler but that just caused&amp;nbsp;nrf52840 to hang and become unresponsive.[/quote]
&lt;p&gt;Could you possibly show some code of how you are doing this? Are you getting any error codes returned from your call to queue the data for sending? You could see the likely returned error codes in &lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.s140.api.v7.2.0%2Fgroup___b_l_e___g_a_t_t_s___f_u_n_c_t_i_o_n_s.html&amp;amp;anchor=ga313fe43c2e93267da668572e885945db"&gt;the sd_ble_gatts_hvx API reference documentation&lt;/a&gt;.&lt;br /&gt;Since data is never lost in the link it is likely that you are dropping the data in the case that it fails to queue - for example if the HVN queue is already full. This could happen if you are queueing notifications faster than you are sending them, or if your hvn queue is not big enough to accommodate all the notifications generated between each connection event.&lt;br /&gt;What connection parameters are you using for your application, and how big is your &lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s140.api.v7.2.0/group___b_l_e___g_a_t_t_s___d_e_f_a_u_l_t_s.html#gadeb57ec178918eea438c8b4a4944ebd2"&gt;BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT&lt;/a&gt;?&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI &amp; Bluetooth best practices</title><link>https://devzone.nordicsemi.com/thread/334419?ContentTypeID=1</link><pubDate>Fri, 15 Oct 2021 19:23:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2d9f78e3-4f40-4488-a717-9849d490bcb3</guid><dc:creator>ryerye120</dc:creator><description>&lt;p&gt;Hi Karl,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;This was incredibly helpful! I&amp;#39;ll play with the SPIM peripheral/drivers and take a look at the SAADC example!&amp;nbsp;&lt;br /&gt;&lt;br /&gt;I have one more question regarding best practices - this time with my &amp;#39;main mode of operation&amp;#39;. Eventually, I will have a timer triggering a SPI transaction every 500us. The resulting data from this SPI transaction will then be beamed via bluetooth.&lt;/p&gt;
&lt;p&gt;To make things simpler for now, I am leaving the SPI out and just&amp;nbsp;using my 500us timer to increment a counter. I then have an if statement in my main while loop that sends the updated counter to another device over bluetooth. Is this the best way&amp;nbsp;to organize fast transactions through bluetooth? I must be doing something wrong because I&amp;#39;m losing a lot of data (I suspect the bluetooth transactions are taking too long - or the cpu is getting halted for too long so the counter increments more than once between bluetooth transfers. I tried putting the ble_nus_data_send() command in my timer&amp;#39;s interrupt handler but that just caused&amp;nbsp;nrf52840 to hang and become unresponsive.&lt;br /&gt;&lt;br /&gt;I&amp;#39;m currently building off of the ble_app_uart examples.&lt;/p&gt;
&lt;p&gt;Thanks again for all of your help!&lt;/p&gt;
&lt;p&gt;Ryan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SPI &amp; Bluetooth best practices</title><link>https://devzone.nordicsemi.com/thread/334313?ContentTypeID=1</link><pubDate>Fri, 15 Oct 2021 10:56:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c0f23cfd-cdac-42da-bfc6-318faaf1a792</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello Ryan,&lt;br /&gt;&lt;br /&gt;Thank you for your patience with this.&lt;/p&gt;
[quote user=""]If this was one way then it would be especially straight forward but unfortunately, the NRF52840 can also receive commands from the dongle (over bluetooth) that may or may not trigger specific SPI transactions. It seems to me that the best way to organize the NRF52840 (that&amp;#39;s talking to the custom IC) as the peripheral and the dongle as the central device. This way the peripheral can use an interrupt to query the IC and then notify the central device with a new packet.[/quote]
&lt;p&gt;Fortunately, it does not have to be complicated just because it is two way! :)&amp;nbsp;&lt;/p&gt;
[quote user=""]I&amp;#39;m having a little more trouble with the other main function though (dongle sending commands to the peripheral) though since it&amp;#39;s really important that this bluetooth connection runs as fast as possible without dropping any packets.[/quote]
&lt;p&gt;The BLE protocol is lossless, so you can be certain that no packets will be lost in the link. The only way packets may be lost is if they are not handled correctly when queued to be transferred by the SoftDevice on one side (such as if the queue command returns an error, and the program proceeds as though nothing had happened - dropping the packet that failed to be queued), or if the packet is discarded after reception on the central side.&lt;br /&gt;You will also not have to worry about bogging down the speed of your BLE transfers with your application, &lt;a href="https://infocenter.nordicsemi.com/topic/sds_s140/SDS/s1xx/processor_avail_interrupt_latency/exception_mgmt_sd.html"&gt;since the SoftDevice takes priority over any application-layer task&lt;/a&gt;. This way, the SoftDevice will always be guaranteed to meet its BLE timing-critical deadlines, since it knows that it can control the CPU whenever it needs to.&lt;br /&gt;So you may configure your connection parameters to whatever you would like, and the SoftDevice will make sure it is met - it is rather the application that must adjust to the possibility that the SoftDevice will take control of the CPU at any time in the program.&lt;/p&gt;
[quote user=""]Is it fair to have a main while loop on all my devices that spins (just checking if there&amp;#39;s been an incoming command) when they&amp;#39;re in their main interrupt-driven operating mode.[/quote]
&lt;p&gt;Are you here referring to an incoming BLE event, or SPI transaction containing some command? I assume the former, and in this case you will not need to have a spinning while loop that checks for received commands, since the SoftDevice will take control of the CPU (even wake it if in SYSTEM_ON sleep) to meet the connection event and receive the command.&lt;/p&gt;
[quote user=""]Then if there has been&amp;nbsp;a command, they break out of their interrupt-driven mode and work on whatever the command is?[/quote]
&lt;p&gt;This also happens as part of the ble_evt_handler or specific service event handler you&amp;#39;ve implemented. When the SoftDevice has completed a connection event it will forward the data received (like a write to a command characteristic) to the the appropriate event handler in the application layer. Provided that the particular event handler has a high enough priority it can then be executed right away.&lt;br /&gt;You can see how this in done in most of the BLE peripheral examples. To be specific, you could take a look at &lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.1.0/ble_sdk_app_nus_eval.html"&gt;the BLE UART peripheral example&lt;/a&gt;&amp;nbsp;where the NUS events are forwarded to the nus event handler and processed immediately upon the application resuming control over the CPU.&lt;/p&gt;
[quote user=""]I worry that this constant spinning may burn unnecessary power and also may slow down the main BLE operation (which needs to run as fast as possible!).[/quote]
&lt;p&gt;As mentioned the BLE operation will happen at the configured rate regardless of what the application is doing. The CPU might very well be in the low power SYSTEM_ON sleep state for most of the time, ready to receive BLE commands in the next connection event. All of our BLE peripheral examples demonstrate this - the idle_state_handler which is running in the main while loop places the CPU in SYSTEM_ON sleep whenever there is nothing else that needs doing. The CPU will then be ready to quickly wake up as soon as there is an event generated.&lt;/p&gt;
[quote user=""]Are there other alternatives that may allow the bluetooth and spi to operate as fast as possible&amp;nbsp;in parallel?[/quote]
&lt;p&gt;Yes, this is a good question - how to proceed in order to operate the SPI when the CPU might be claimed by the SoftDevice at any time.&lt;br /&gt;The answer to this is to make use of the nRF52840&amp;#39;s easyDMA feature through using &lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fps_nrf52840%2Fspim.html"&gt;the SPIM peripheral&lt;/a&gt; (rather than the SPI), since this utilizes easyDMA.&lt;br /&gt;easyDMA lets a peripheral access RAM without needing CPU intervention. You could therefore both receive or send (if queued in advanced) data while the CPU is busy with the SoftDevice. To achieve this totally without CPU intervention you would need to also make use of the PPI peripheral, which lets you connect an event to a task trigger - i.e that when a specific event occurs, you would have it trigger a task (such as &lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fps_nrf52840%2Fspim.html&amp;amp;anchor=register.TASKS_START"&gt;TASKS_START&lt;/a&gt;) without needing to involve the CPU.&lt;br /&gt;You could see the PPI peripheral demonstrated in &lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.1.0/nrf_dev_saadc_example.html"&gt;the SAADC peripheral example&lt;/a&gt;&amp;nbsp;where it is used to connect a TIMER CC event to the SAADC&amp;#39;s TASKS_SAMPLE - enabling the SAADC to sample periodically without concern that the CPU might not be available at the particular time the sampling should happen.&lt;br /&gt;&lt;br /&gt;Please do not hesitate to ask if any part of this should be unclear, or if you encounter any other issues or questions!&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>