<?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>Application firmware hangs up in twi_wait() function</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/13358/application-firmware-hangs-up-in-twi_wait-function</link><description>Hello 
 I`m using the NORDIC nrf_drv_twi driver implementation from the PACK environment (version equivalent to SDK 8.1.0). Together with the S120 softdevice.
If there is an active ble connection, somtimes the firmware hangs up in the twi_wait() function</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 29 Apr 2016 10:25:51 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/13358/application-firmware-hangs-up-in-twi_wait-function" /><item><title>RE: Application firmware hangs up in twi_wait() function</title><link>https://devzone.nordicsemi.com/thread/50932?ContentTypeID=1</link><pubDate>Fri, 29 Apr 2016 10:25:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1a947551-2905-4780-9c3c-c2d6d6c2602a</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;Do you have some traces from a logic analyzer?&lt;/p&gt;
&lt;p&gt;I believe you have multiple error conditions here which you are not handling gracefully in your application code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;A twi slave might NACK the transaction if it&amp;#39;s not ready, this will cause a twi error. Add a delay and try again.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;A twi sensor can delay an twi transaction by &amp;quot;clock stretching&amp;quot; if it&amp;#39;s not ready. This might cause the transaction to be busy or not complete if you call a new transaction before the previous transaction is complete. Make sure not not call a new twi transaction before the previous is finished.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The execution of a twi transacation might be delayed by other processes. This should not be a problem, just add a delay and try again.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The driver does not make a copy of the supplied parameters internally, so if you update any of the buffers before the previous twi transaction is complete, this might not work well. Make sure not to update any of the buffers before the previous transacation is complete.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All the above can cause a problem if the application does not differentiate the different error conditions that might occur. Also check the datasheet of the twi slave on timing details that might apply.&lt;/p&gt;
&lt;p&gt;During debugging it might also make sense to update the driver to the latest version for possible bug fixes that might apply.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Application firmware hangs up in twi_wait() function</title><link>https://devzone.nordicsemi.com/thread/50933?ContentTypeID=1</link><pubDate>Thu, 28 Apr 2016 14:00:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2d9396fa-36e6-4ac1-b6ef-b1a14742c07b</guid><dc:creator>Andreas</dc:creator><description>&lt;p&gt;Image (cross referenced from a comment)
&lt;img src="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/TWI_5F00_problem.jpg" alt="image description" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Application firmware hangs up in twi_wait() function</title><link>https://devzone.nordicsemi.com/thread/50931?ContentTypeID=1</link><pubDate>Thu, 28 Apr 2016 13:59:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:65227a2c-7395-4eb5-8bfb-e974fb47a45d</guid><dc:creator>Andreas</dc:creator><description>&lt;p&gt;Hello Kenneth, I tried it that way:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;do
{
	res = nrf_drv_twi_tx(&amp;amp;m_twi_eeprom, EEPROM_HW_ADDR, m_address, 2, true);
	if(res == NRF_SUCCESS) break;
}while(res != NRF_SUCCESS);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But than, the operation will hang up again in that while loop.
This while loop will never be done, because res will not become NRF_SUCCESS anymore.
I have an idea why that my happens. I will attach a sketch in another answer. Unfortunately it is not possible to add images to a comment.
Is it possible that there is a problem, if two processes (application and softdevice-process are using the TWI BUS). I will try to implement some kind of a MUTEX for that behavior. But this will be very complicated, because the &amp;quot;normal&amp;quot; application process can be interrupted by an BLE event anytime. And I have no idea how it is possible, to wait for the completion of an TWI operation that is running in the normal application process. I hope the sketch and my explanation is clear enough, to understand the problem.
Regards&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Application firmware hangs up in twi_wait() function</title><link>https://devzone.nordicsemi.com/thread/50930?ContentTypeID=1</link><pubDate>Wed, 27 Apr 2016 12:52:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2319f115-8c37-49b3-8a15-d9a82da2f25d</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;But wouldn&amp;#39;t that loop execute very fast? I think you should add a delay for each iteration so you have some control on how fast that loop execute.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Application firmware hangs up in twi_wait() function</title><link>https://devzone.nordicsemi.com/thread/50926?ContentTypeID=1</link><pubDate>Wed, 27 Apr 2016 11:17:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:887bd7e2-eff4-4561-9ecf-9277d2ee1c8b</guid><dc:creator>Andreas</dc:creator><description>&lt;p&gt;Hello Kenneth, sorry, but there must be something different. For testing I implemented it like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;for(retries = 0; retries &amp;lt; 250; retries++)
{
	res = nrf_drv_twi_tx(&amp;amp;m_twi_eeprom, EEPROM_HW_ADDR, m_address, 2, true);
	if(res == NRF_SUCCESS) break;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But I still reach the 250 retries. And for me it does not look liker there is any improvement.
Thanks for spending the time and helping solving these open issues. I believe, if I would implement it with a while loop, the software would hang up again. Then it is the same behaviout than with the event handler solution...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Application firmware hangs up in twi_wait() function</title><link>https://devzone.nordicsemi.com/thread/50928?ContentTypeID=1</link><pubDate>Wed, 27 Apr 2016 10:14:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8c2b5355-ad80-4a90-a97f-d74957b03755</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;Retry until NRF_SUCCESS is fine.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Application firmware hangs up in twi_wait() function</title><link>https://devzone.nordicsemi.com/thread/50929?ContentTypeID=1</link><pubDate>Wed, 27 Apr 2016 09:37:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7511f4d0-fd96-4957-a234-5e39122ccd5c</guid><dc:creator>Andreas</dc:creator><description>&lt;p&gt;Hello Kenneth, sorry for that mistake. Of course, res will be one every time if it is bool... I changed it to ret_code_t. Now I get NRF_ERROR_BUSY, as return value from nrf_drv_twi_tx().
Is it a possible solution to only wait and try the transfer again?
BR&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Application firmware hangs up in twi_wait() function</title><link>https://devzone.nordicsemi.com/thread/50927?ContentTypeID=1</link><pubDate>Wed, 27 Apr 2016 09:07:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:88630ae7-2c42-4055-8968-cc54ba48ff80</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;You should modify:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  //bool res;
  ret_code_t res; 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then most likely you will see it return the correct error code, but I am not sure why the compiler doesn&amp;#39;t return a warning when it cast an uint32_t to bool.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Application firmware hangs up in twi_wait() function</title><link>https://devzone.nordicsemi.com/thread/50925?ContentTypeID=1</link><pubDate>Tue, 26 Apr 2016 12:53:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b2a3cfaa-5bb2-4ea8-837e-47a02fbbebae</guid><dc:creator>Andreas</dc:creator><description>&lt;p&gt;Hello Kenneth, interesting, I can confirm, that I do not get the error if I slow down the function which causes very much I2C transfers per second in background.
My EEPROM_write() function where I get the error looks like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;res = nrf_drv_twi_tx(&amp;amp;m_twi_eeprom, EEPROM_HW_ADDR, m_address, 2, true);
if(res==NRF_SUCCESS){
...
}
else
{
dbg_printf(&amp;quot;Error transmitting I2C read address %d!\r&amp;quot;, res);
return NRF_DRV_TWI_ERROR;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and the output I get on the terminal looks like this:
&lt;code&gt;Error transmitting I2C read address 1!&lt;/code&gt;
So for me it is clear that I get the error value 1 from nrf_drv_twi_tx()&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Application firmware hangs up in twi_wait() function</title><link>https://devzone.nordicsemi.com/thread/50924?ContentTypeID=1</link><pubDate>Tue, 26 Apr 2016 12:34:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f4d9bf40-e534-48f4-91f3-d4515e312944</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;I am not sure what is the problem here, but I can&amp;#39;t find that nrf_drv_twi_tx() can return NRF_ERROR_SVC_HANDLER_MISSING error code. I searched for NRF_ERROR_SVC_HANDLER_MISSING in the twi source code in nRF5 SDK v11 - \examples\peripheral\twi_master_using_app_twi.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Application firmware hangs up in twi_wait() function</title><link>https://devzone.nordicsemi.com/thread/50923?ContentTypeID=1</link><pubDate>Tue, 26 Apr 2016 07:32:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c51e8998-91de-47f0-94e1-22979a4e73ff</guid><dc:creator>Andreas</dc:creator><description>&lt;p&gt;Hello Kenneth, I changed my TWI driver initialisation and set the event handler to NULL. Now the application does not hang up any more. But I still get error 1 (NRF_ERROR_SVC_HANDLER_MISSING) sometimes from the nrf_drv_twi_tx() function call. How to go on with that? I think this breaks down my BLE data transmission...
BR&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Application firmware hangs up in twi_wait() function</title><link>https://devzone.nordicsemi.com/thread/50922?ContentTypeID=1</link><pubDate>Mon, 25 Apr 2016 10:57:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4b06dd7d-15f1-49bd-8294-0b6442cd3e51</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;Likely there is a race condition of some sort, where you try to start the next transmission at the same time as the previous one is done. Have you considered using the driver in blocking mode without a callback registered? E.g. init nrf_drv_twi_init() with NULL as the event_handler(). Then each call to nrf_drv_twi_tx() is blocking, and you don&amp;#39;t need to use twi_wait() between calls. The nrf_drv_twi_tx() will return when the twi operation is complete, with either NRF_SUCCESS or NRF_ERROR_INTERNAL return value for the executed twi operation.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Application firmware hangs up in twi_wait() function</title><link>https://devzone.nordicsemi.com/thread/50921?ContentTypeID=1</link><pubDate>Thu, 21 Apr 2016 14:14:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:76f51da1-b240-40c3-a2ac-22a96ce85acf</guid><dc:creator>Andreas</dc:creator><description>&lt;p&gt;Another point. I can see, this error happens only, if there is a high payload in the TWI bus --&amp;gt; 300-400 calls per second. If I switch OFF the function which causes these calls, the error seems not to happen anymore. How to handle this?
Regards&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Application firmware hangs up in twi_wait() function</title><link>https://devzone.nordicsemi.com/thread/50920?ContentTypeID=1</link><pubDate>Thu, 21 Apr 2016 13:10:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1f2bafca-d6cb-4fda-9fe1-14da199326a2</guid><dc:creator>Andreas</dc:creator><description>&lt;p&gt;Hello Kenneth
I changed my twi_wait function a little bit to:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;void twi_wait(void){
uint32_t count1 = 0, count2 = 0;
uint32_t retval = NRF_ERROR_TIMEOUT;
for(count1 = 0; count1&amp;lt;50; count1++)
{
	for(count2 = 0; count2 &amp;lt; 10000; count2++)
	{
		__nop();
		if(m_xfer_done)
		{
			retval = NRF_SUCCESS;
			break;
		}
	}
	if(m_xfer_done) break;
}
if(retval != NRF_SUCCESS) dbg_printf(&amp;quot;twi_wait error\r&amp;quot;);}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now I can see where it is going wrong. It breaks when trying to transmitt a read address to the EEPROM. nrf_drv_twi_tx() returns 1. Is that helpful anyway? With the new twi_wait() function, of course my firmware does not hang up. But the problem is still alive...
Unfortunately I have no logic analyzer. Only a oscilloscope. And it is pretty hard to trigger to that mistake.&lt;/p&gt;
&lt;p&gt;BTW: I know that support for PACK is deprecated. This is the next point on my list, to switch from PACK to SDK. But before doing such a step, I want to have a stable firmware.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Application firmware hangs up in twi_wait() function</title><link>https://devzone.nordicsemi.com/thread/50919?ContentTypeID=1</link><pubDate>Thu, 21 Apr 2016 10:28:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5e4b73a3-c9f0-4b8f-ae6c-f94fed1b3bd1</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;Hi, I can&amp;#39;t find any examples out of the box with both BLE and TWI at the moment. I think what would help here is if you have access to a logic analyzer so you can see what occurs on the serial interface. Also it might help to debug the events in twi_handler() for NRF_DRV_TWI_RX_DONE, NRF_DRV_TWI_TX_DONE, and NRF_DRV_TWI_ERROR. You should ensure that application interrupt priorities is set according to the softdevice specification document.&lt;/p&gt;
&lt;p&gt;FYI: We are no longer maintaining pack, so you should download the latest &lt;a href="https://developer.nordicsemi.com/nRF5_SDK/"&gt;nRF5 SDK&lt;/a&gt; in zip format.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Application firmware hangs up in twi_wait() function</title><link>https://devzone.nordicsemi.com/thread/50918?ContentTypeID=1</link><pubDate>Thu, 21 Apr 2016 09:55:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3d9c8f08-995f-489d-98a4-acde7820f11a</guid><dc:creator>Andreas</dc:creator><description>&lt;p&gt;I am just on ivestigating that problem. My idea why that happens is: If the priority of TWI is High, it has the same prio than the Sofdevice. Now, if the transfer rate on BLE is high, but the softdevice is &amp;quot;waiting&amp;quot; for the result from another characteristic call, it comes to some kind of a dead loop. Is that right!? But how to avoid this? I tried to add a twi_wait in the BLE on_write() function. But that doesn`t help. With that change, the firmware hangs up in this while loop (in the on_write function).
Regards!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>