<?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>APP Timer and While loop in BLE</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/16527/app-timer-and-while-loop-in-ble</link><description>Hi, 
 I am using App Timer for measuring time and at the same time while loop to check if the timer reached particular time. 
 I have noticed during while loop App Timer event doesn&amp;#39;t seems to work. Is this correct? 
 Regards
Siva</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 21 Sep 2016 11:16:32 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/16527/app-timer-and-while-loop-in-ble" /><item><title>RE: APP Timer and While loop in BLE</title><link>https://devzone.nordicsemi.com/thread/63232?ContentTypeID=1</link><pubDate>Wed, 21 Sep 2016 11:16:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8577f95a-40d9-43ed-acb4-330f61e2e6a7</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;The app_timer uses RTC1 for all your timers, and always uses APP_LOW priority level. You should not configure RTC1 separately when using the app_timer. Also note that the SoftDevice uses RTC0, so you cannot use that from your application. My gut feeling is that you should clean-up your application, make sure not do unnecessary blocking stuff etc, rather than trying to fix too much with priorities. Do as little as possible in interrupt routines, only what must be done fast, and do the rest in main context. Use timers for waiting, not loops checking a timer value etc...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: APP Timer and While loop in BLE</title><link>https://devzone.nordicsemi.com/thread/63231?ContentTypeID=1</link><pubDate>Wed, 21 Sep 2016 11:04:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:740cf3cf-9da4-4d9a-b902-a6557de0bacd</guid><dc:creator>Siva</dc:creator><description>&lt;p&gt;Hi Einar,
Thanks for your advise.
I am also thinking to use another &amp;quot;App timer&amp;quot; with low priority such that I will use it to call &amp;quot;Peace_Mask-Active()&amp;quot; event once every few seconds(It doesn&amp;#39;t has to be very accurate).&lt;/p&gt;
&lt;p&gt;I didn&amp;#39;t find a correct syntax to change priorities for App timer can you advice on this please. Also want to know which RTCs has been used while using multiple app timer functions.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: APP Timer and While loop in BLE</title><link>https://devzone.nordicsemi.com/thread/63230?ContentTypeID=1</link><pubDate>Wed, 21 Sep 2016 06:22:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4db03530-184c-4e38-800f-c50b0a5730fa</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Do you really need to do so much in an interrupt context? And do you really need those loops waiting for a long time? It tends to give you a complex application where it is difficult to handle what is waiting for what (as you are seeing). Moreover, the CPU will be running for much of the time, causing an unnecessarily high current consumption.&lt;/p&gt;
&lt;p&gt;There are other issues with your code as well. In your &lt;code&gt;Peace_Mask_Active()&lt;/code&gt;, where you have for example this loop:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;		while(true)
		{
			if(Current_Time_Sec &amp;gt; (Measured_Treatment_Time+20))
			{
				break;
			}
		}		
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;you are waiting for &lt;code&gt;Current_Time_Sec&lt;/code&gt; to change, which is updated in another event handler (in interrupt context). Even if you make sure that the &lt;code&gt;Current_Time_Sec&lt;/code&gt; is updated in an event handler with higher priority (which is a must), it still won&amp;#39;t help you unless you disable optimization, as the variable is not &lt;code&gt;volatile&lt;/code&gt; and the compiler cannot easily know that it can change. Therefore, with normal optimization, the above loop would be optimized to something like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;		while(true)
		{
		}	
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;...which will continue to do nothing forever. (As long as you are just incrementing and waiting for it to become larger than a limit, making the variable volatile should be sufficient in this case. In other cases you would have to do more in order to make the operation atomic, using for example the &lt;code&gt;CRITICAL_REGION_ENTER()&lt;/code&gt; and &lt;code&gt;CRITICAL_REGION_EXIT()&lt;/code&gt; macros supplied with the SDK.)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: APP Timer and While loop in BLE</title><link>https://devzone.nordicsemi.com/thread/63229?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2016 12:06:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:98eccc75-1b26-4906-af26-5e8b508406b6</guid><dc:creator>Siva</dc:creator><description>&lt;p&gt;I have tried changing &amp;quot;APP_IRQ_PRIORITY_LOW&amp;quot; to &amp;quot;APP_IRQ_PRIORITY_HIGH&amp;quot; on RTC1 in nrf_drv_config.h file but it didn&amp;#39;t work as it still got stuck in while loop.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: APP Timer and While loop in BLE</title><link>https://devzone.nordicsemi.com/thread/63228?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2016 10:07:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9c553daf-61eb-48d4-a265-d1b7041772a8</guid><dc:creator>Siva</dc:creator><description>&lt;p&gt;I found how to set IRQ priorities for Protocols now.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: APP Timer and While loop in BLE</title><link>https://devzone.nordicsemi.com/thread/63227?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2016 09:35:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fcd55445-18c5-4e07-a4f1-5cdaa150eb6c</guid><dc:creator>Siva</dc:creator><description>&lt;p&gt;Can you advice on how to change interrupt priorities for timer,ble, gpio, protocols(I2C,SPI,Uart,etc).&lt;/p&gt;
&lt;p&gt;I have attached my code. At line 1067 where our problem actually starts.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;We use &amp;quot;rtc_config()&amp;quot; to read touch value, I have noticed that it doesn&amp;#39;t read data when it enters into the loop for the first time I have to call this function again by button press in order to read the data. It is recursive(I have to call the event twice to read the touch data). Any Advice?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In lines 1078,1091 where I started using while loop to check time.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You are correct if I can set the priorities right this should work. In our application ble,timer needs to be highest priority followed by I2C and then while/main loops.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: APP Timer and While loop in BLE</title><link>https://devzone.nordicsemi.com/thread/63226?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2016 05:50:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d4f20d8c-412f-4852-a08e-e4f7fd06d05c</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;In what interrupt priority do you check the timer value in a loop? If it is running in the same or higher priority than the application timer, your timeout event handler will never be called as it must wait for the other (same or higher priority) interrupt handler / event handler to finish first.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>