<?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>nRF51 wrong sequence in received packets</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/71965/nrf51-wrong-sequence-in-received-packets</link><description>I&amp;#39;m using nRF51822_xxAA , with SDK12.3.0 and Softdevice S130 v2.0.1 on Keil V5.29.0.0. And as a central I use Redmi 7A phone with Android V10. I want to sample ADC @ 200Hz, and send the sample as 2 bytes inside a packet of 20 bytes (10 ADC values) at</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 01 Mar 2021 02:37:01 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/71965/nrf51-wrong-sequence-in-received-packets" /><item><title>RE: nRF51 wrong sequence in received packets</title><link>https://devzone.nordicsemi.com/thread/296724?ContentTypeID=1</link><pubDate>Mon, 01 Mar 2021 02:37:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7a368eec-51e0-4f15-aebf-4e2599672910</guid><dc:creator>Rami AM</dc:creator><description>[quote userid="6207" url="~/f/nordic-q-a/71965/nrf51-wrong-sequence-in-received-packets/296444#296444"]I think what you are seeing is a conflict or race condition in your code, which is a general programming issue that you need to debug. I am quite confident that this is not related to the Nordic solution[/quote]
&lt;p&gt;yes &lt;a href="https://devzone.nordicsemi.com/members/aryan"&gt;Susheel Nuguru&lt;/a&gt; it&amp;#39;s exactly as yo said, it&amp;#39;s a race condition between the ADC module, and BLE module, especially when I send the packet inside the main function&amp;#39;s forever loop, after putting send function inside the ADC event handler, the problem almost didn&amp;#39;t appear as much as before.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51 wrong sequence in received packets</title><link>https://devzone.nordicsemi.com/thread/296516?ContentTypeID=1</link><pubDate>Fri, 26 Feb 2021 11:57:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c7dc4750-043b-437a-9c62-6166db201e82</guid><dc:creator>Susheel Nuguru</dc:creator><description>[quote user="rami_am"]Is it possible that the problem exists in the central side?, because by using nRFconnect for pc the problem didn&amp;#39;t appear.&amp;nbsp;[/quote]
&lt;p&gt;It is possible, please take a look at how it is unpacking data, like the code you showed me here.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I am quite confident that the BLE side does not have a problem since i have not heard of this issue within BLE before&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51 wrong sequence in received packets</title><link>https://devzone.nordicsemi.com/thread/296461?ContentTypeID=1</link><pubDate>Fri, 26 Feb 2021 09:37:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2f52b3d1-83df-407e-8508-793dc35651d0</guid><dc:creator>Rami AM</dc:creator><description>&lt;p&gt;&lt;strong&gt;Is this p_data word aligned?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;yes it&amp;#39;s an array of 20 bytes&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is the value of&amp;nbsp;MY_DATA_LENGTH?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;20 bytes&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;is hr_buffer memory&amp;nbsp; being accessed both by ADC and BLE notification modules?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;when ADC fills out the array, it&amp;#39;ll set a flag &lt;strong&gt;&amp;quot;ble_packet_ready_to_send&amp;quot;&lt;/strong&gt;, so the array will be available for the BLE notification module, otherwise there&amp;#39;s no any other possibility for BLE module to get access to it. anyway, I&amp;#39;ll re-check if there&amp;#39;s any conflict or race-condition there.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Are you using&amp;nbsp;ble_samples_cntr anywhere else apart from the code you showed?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;no, only inside adc_event_handler, it&amp;#39;s a static variable inside the handler, and it might affect the bytes order into the array, but I don&amp;#39;t have such a case.&lt;/p&gt;
&lt;p&gt;Is it possible that the problem exists in the central side?, because by using nRFconnect for pc the problem didn&amp;#39;t appear.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51 wrong sequence in received packets</title><link>https://devzone.nordicsemi.com/thread/296444?ContentTypeID=1</link><pubDate>Fri, 26 Feb 2021 08:49:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8d525e47-8de1-43e5-8a7f-b7f8a0271d5b</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;There are few things that are of concern.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;hvx_params.p_data&amp;nbsp; &amp;nbsp;= (uint8_t*) p_data;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Is this p_data word aligned?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is the value of&amp;nbsp;MY_DATA_LENGTH?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The code below&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;pre class="ui-code" data-mode="text"&gt;if(sawtooth_cntr&amp;lt;100)
	sawtooth_cntr++;
else
	sawtooth_cntr=0;
	
if(ble_samples_cntr &amp;lt;10)
{	
	hr_buffer[ble_samples_cntr *2] = (uint8_t)(sawtooth_cntr&amp;gt;&amp;gt;8);
	hr_buffer[ble_samples_cntr *2 + 1] = (uint8_t)(sawtooth_cntr);
	ble_samples_cntr++;
}
if(ble_samples_cntr&amp;gt;=10)
{
	ble_samples_cntr=0;
	ble_packet_ready_to_send = true;
}&lt;/pre&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;is hr_buffer memory&amp;nbsp; being accessed both by ADC and BLE notification modules? if yes, then you need to have it under critical section&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Are you using&amp;nbsp;&lt;/strong&gt;ble_samples_cntr anywhere else apart from the code you showed?&lt;/p&gt;
&lt;p&gt;I think what you are seeing is a conflict or race condition in your code, which is a general programming issue that you need to debug. I am quite confident that this is not related to the Nordic solution. But if you can provide me a stripped version of this code that is ready to compile and flash, i can attempt to debug this a bit and try to see where you might have a race condition.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51 wrong sequence in received packets</title><link>https://devzone.nordicsemi.com/thread/296408?ContentTypeID=1</link><pubDate>Fri, 26 Feb 2021 02:29:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:873de918-1785-4a3f-b72b-a21ef464f73b</guid><dc:creator>Rami AM</dc:creator><description>&lt;p&gt;for central side:&lt;/p&gt;
&lt;p&gt;1-When using Bluetooth Low Energy on nRfconnect for pc, there were no problems, I can send my data over BLE @200hz, and get them as correct as&amp;nbsp;I sent.&lt;/p&gt;
&lt;p&gt;2-&amp;nbsp;When&amp;nbsp;using nRFconnect for mobile phone (android), there was no wrong sequence order in data packets, but when my phone had no enough RAM (i.e. there are other APPs are working together at the same time), in this case there was missed BLE packets, when I checked the log file, I&amp;#39;ve found that packet is missed there too&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1614306115121v1.png" alt=" " /&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;where P1 and P2 are the received packets, but there must be another packet between them because the mcu is generating a sequence of numbers between 0 and 100.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51 wrong sequence in received packets</title><link>https://devzone.nordicsemi.com/thread/296404?ContentTypeID=1</link><pubDate>Fri, 26 Feb 2021 01:56:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f6bb69cd-6c02-4406-80a1-911215b4a3d6</guid><dc:creator>Rami AM</dc:creator><description>&lt;p&gt;this code is to generate the signal, it&amp;#39;s done inside the adc_event_handler:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;if(sawtooth_cntr&amp;lt;100)
	sawtooth_cntr++;
else
	sawtooth_cntr=0;
	
if(ble_samples_cntr &amp;lt;10)
{	
	hr_buffer[ble_samples_cntr *2] = (uint8_t)(sawtooth_cntr&amp;gt;&amp;gt;8);
	hr_buffer[ble_samples_cntr *2 + 1] = (uint8_t)(sawtooth_cntr);
	ble_samples_cntr++;
}
if(ble_samples_cntr&amp;gt;=10)
{
	ble_samples_cntr=0;
	ble_packet_ready_to_send = true;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;this is where to send the packet:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;for( ;;)
{
	.
	.
	.	
	
	if(hr_packet_ready_to_send &amp;amp;&amp;amp; m_nlrs.is_notification_enabled )
	{
		if(my_data_tx_complete)
		{
			my_ble_characteristic_update(&amp;amp;m_nlrs,hr_buffer);
			ble_packet_ready_to_send = false;
			my_data_tx_complete = false;
		}
		
	}
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;this is how to update the characteristic value:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint32_t my_ble_characteristic_update(ble_nlhrs_t * p_nlrhrs, uint8_t * p_data)
{
    uint32_t err_code;
    if((p_nlrhrs-&amp;gt;conn_handle != BLE_CONN_HANDLE_INVALID) || (!m_nlrhrs-&amp;gt;is_notification_enabled))            //ensure that ther&amp;#39;s a connection, otherwise softdevice will result in error
    {
        uint16_t                    len = MY_DATA_LENGTH;
        ble_gatts_hvx_params_t      hvx_params;		//20 bytes
        memset(&amp;amp;hvx_params,0,sizeof(hvx_params));
        
        hvx_params.handle   = p_nlrhrs-&amp;gt;char_handles.value_handle;
        hvx_params.type     = BLE_GATT_HVX_NOTIFICATION;
        hvx_params.offset   = 0;
        hvx_params.p_len    = &amp;amp;len;
        hvx_params.p_data   = (uint8_t*) p_data;
        err_code = sd_ble_gatts_hvx(p_nlrhrs-&amp;gt;conn_handle,&amp;amp;hvx_params);       
    }
    else
        err_code = NRF_ERROR_NOT_FOUND;
		
    return err_code;       
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;and this is how I implemented tx_complete:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;bool my_data_tx_complete=true;
static void on_tx_complete(ble_nlhrs_t * p_nlhrs, ble_evt_t * p_ble_evt)
{
    my_data_tx_complete=true;
    if(m_send_complete_para.callback) {
        m_send_complete_para.callback(m_send_complete_para.context,m_send_complete_para.task_type);
    }
}
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51 wrong sequence in received packets</title><link>https://devzone.nordicsemi.com/thread/296152?ContentTypeID=1</link><pubDate>Thu, 25 Feb 2021 07:36:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:554d3f67-4f0f-497d-8d80-e7a40876eb23</guid><dc:creator>Susheel Nuguru</dc:creator><description>[quote user=""]example, when I send 3 packets of data, I send them as follow:&lt;br /&gt;[Packet_1][Packet_2][Packet_3]...&lt;br /&gt;but when when I receive them, sometimes, I receive them as follow:&lt;br /&gt;[Packet_1][Packet_3][Packet_2]...[/quote]
&lt;p&gt;&amp;nbsp;I think this should not be possible to receive the swapped packets since BLE in its core have sequence numbers for packets so that the receiving controller layer will not accept a packet in wrong sequence order. It seems that there is something wrong in the way you are interpreting the data.&lt;/p&gt;
&lt;p&gt;It would help us if you show us some code snippets on how you are sending data&amp;nbsp;on the TX side and how you are pulling data on the receiver side.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>