<?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>Not Exiting &amp;quot;while loop&amp;quot; after a TWI event</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/11943/not-exiting-while-loop-after-a-twi-event</link><description>Hi, 
 I&amp;#39;m facing a weird problem resulting from the following code: 
 while((!twi_tx_done) &amp;amp;&amp;amp; timeout--); 
 
 The main file and the MPU9150.c which includes this command are attached main-2016-02-16.c mpu9150.c . Normally the program should exit this</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 22 Feb 2016 11:54:47 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/11943/not-exiting-while-loop-after-a-twi-event" /><item><title>RE: Not Exiting "while loop" after a TWI event</title><link>https://devzone.nordicsemi.com/thread/45237?ContentTypeID=1</link><pubDate>Mon, 22 Feb 2016 11:54:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8e4fb8cf-ff21-4076-9d29-df512c26d5ef</guid><dc:creator>MartinBL</dc:creator><description>&lt;p&gt;Hi. Thank you for the tips on the timeout. You are right and I have updated the code on github. I&amp;#39;ll look into an interrupt driven example if I can find some time for it. Regarding your issue with the interrupt handler let us discuss it in the thread you created &lt;a href="https://devzone.nordicsemi.com/question/68758/twi-function-not-working-when-called-from-a-pin-interrupt-handler/"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Not Exiting "while loop" after a TWI event</title><link>https://devzone.nordicsemi.com/thread/45236?ContentTypeID=1</link><pubDate>Wed, 17 Feb 2016 21:55:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c8c25fe9-04ec-40a3-8c59-da8983c9812e</guid><dc:creator>FA</dc:creator><description>&lt;p&gt;Hi Martin,&lt;/p&gt;
&lt;p&gt;I was using optimization level 3 and as you said when changed the variables to volatile the delay issue is solved.&lt;/p&gt;
&lt;p&gt;Regarding the MPU9150 example you provided, I could get it work with the nRF interrupting the MPU. But the reverse, although I can see the program going into the interrupt handler and requesting data (i.e., tx is done), but no data appears on the line after that (no rx). If it works it will be a good example for everyone I think. Thanks for writing the mpu9150.c&lt;/p&gt;
&lt;p&gt;BTW, I think after using (timeout--) in your loop, you should check for (!(timeout+1)) to get the timeout error OR use (--timeout).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Not Exiting "while loop" after a TWI event</title><link>https://devzone.nordicsemi.com/thread/45235?ContentTypeID=1</link><pubDate>Wed, 17 Feb 2016 11:48:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f8478fa1-4e77-4d82-ae38-d8e5e555017a</guid><dc:creator>MartinBL</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;When I developed the example I used Optimization level 0 (-O0). Based on a hunch I tried to set it to level 3 (-O3) and now I see something similar to you. I only see four TWI transfers and when using the debugger the timeouts seems to be the issue.&lt;/p&gt;
&lt;p&gt;Setting &lt;code&gt;twi_tx_done&lt;/code&gt; and &lt;code&gt;twi_rx_done&lt;/code&gt; to volatile like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;volatile static bool twi_tx_done = false;
volatile static bool twi_rx_done = false;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;...seemed to fix the problem for me. A good explanation of why the volatile keyword might help can be found &lt;a href="http://www.barrgroup.com/Embedded-Systems/How-To/C-Volatile-Keyword"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So what Optimization level do you use?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>