<?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>Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/9157/wakeup-immediately-from-sd_app_evt_wait-with-external-lfclk</link><description>Hi all 
 We use the nRF51822 with the S130 V1.0.0. Our application initializes the TIMER2 (f = 16MHz) with a EVENTS_COMPARE interrupt on the CC channel 1 to monitor some timings. 
 Afterwards the SoftDevice is enabled with the LFCLK source &amp;quot;NRF_CLOCK_LFCLKSRC_XTAL_20_PPM</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 16 Sep 2015 06:24:13 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/9157/wakeup-immediately-from-sd_app_evt_wait-with-external-lfclk" /><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33759?ContentTypeID=1</link><pubDate>Wed, 16 Sep 2015 06:24:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8d77d045-2d2f-475f-9856-af2a8989a343</guid><dc:creator>Remo</dc:creator><description>&lt;p&gt;Thanks for the clarification of application events! I know that I could keep it the way it works with disabling and enabling the timer :-) But I would like to know if there is something that I am doing wrong in my code or if there is hiccup in the softdevice initialization when an interrupt occurs at the wrong moment? Many thanks for your effort!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33758?ContentTypeID=1</link><pubDate>Tue, 15 Sep 2015 12:07:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:86b70766-bd7a-47c3-ac8f-3dfa6044663f</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Any event or interrupts that are caused by modules not used by softdevice are application events.
For example RTC0, RNG, RADIO etc are used directly and only by softdevice, so any interrupts caused by these modules are not application events.
Timer2 is application module and not used by SD, so its interrupt is application event.&lt;/p&gt;
&lt;p&gt;I have no clue why disabling of timer and enabling it after softdevice initialization works.
But if it works, then keep it that way :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33757?ContentTypeID=1</link><pubDate>Tue, 15 Sep 2015 12:01:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1559c8b4-282c-43b3-b6e5-cbdf07982d98</guid><dc:creator>Remo</dc:creator><description>&lt;p&gt;I also tried different waiting times (e.g. 500ms) before and after the initialization of the softdevice, but it didn&amp;#39;t help. Is an interrupt of the Timer2 also an application event? Maybe I understand the term application event wrong. Can you please explain by what application events are generated? Or do you see in my code the source of this event?&lt;/p&gt;
&lt;p&gt;Is it safer when the Timer2 is not running during the softdevice initialization? Because when I disabled the query timer before the SD init and re-enabled it afterwards, the problem didn&amp;#39;t occur anymore.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33756?ContentTypeID=1</link><pubDate>Tue, 15 Sep 2015 11:33:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1d5dd9fc-dc7c-45e3-bf2c-8487cd12a7cc</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;It is possible that when you choose different &amp;#39;lfclk with calibration option, it takes little more time (few cycles) to setup calibration timer and this changes the timing and the state of Program counter when the application event occurs.&lt;/p&gt;
&lt;p&gt;Important thing to note as RK already mentioned is that this wait for event in this scenario does not suit well if they are not called in a loop.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33755?ContentTypeID=1</link><pubDate>Tue, 15 Sep 2015 06:11:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cb13eea1-c46c-4251-b9e4-99aee4dd0a8c</guid><dc:creator>Remo</dc:creator><description>&lt;p&gt;I don&amp;#39;t see either what the choice of the LFCLK source has to do with this issue, but that&amp;#39;s exactly the problem I am seeing. When I choose the RC oscillator at the SD init, the chip does NOT awake after the function call sd_app_event_wait(). But when I choose the LF crystal, the chip awakes immediately.&lt;/p&gt;
&lt;p&gt;And another interesting thing is that when I initialize my query timer after the initialization of the SoftDevice, the chip stays sleeping even when I choose the LF crystal.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33748?ContentTypeID=1</link><pubDate>Mon, 14 Sep 2015 15:30:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:79356396-ae25-43d5-9ea4-1993a3befd36</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;Don&amp;#39;t think it&amp;#39;s what&amp;#39;s currently enabled or pending, it&amp;#39;s which &lt;em&gt;user space&lt;/em&gt; interrupts have fired since the softdevice was enabled and after any other calls to sd_app_event_wait(). If I&amp;#39;ve understood correctly what Aryan said, the user space interrupt sets the flag, the next call to sd_app_event_wait() clears it and exits instantly, else it waits.&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t see where the LFCLK vs XTAL comes into that equation at all. I got the impression in your code that by the time you turned it off, TIMER1 had already fired at least once and generated an interrupt.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33743?ContentTypeID=1</link><pubDate>Mon, 14 Sep 2015 14:49:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:986e4a08-4765-4e37-8ec2-9a4422a59a59</guid><dc:creator>Remo</dc:creator><description>&lt;p&gt;I didn&amp;#39;t enable the shortcut for clearing the timer because the compare event triggers when the counter value is 0xFFFF, which is followed by the overflow of the counter value. Do I need to enable this shortcut anyway?&lt;/p&gt;
&lt;p&gt;I checked the NVIC registers for enabled and pending interrupts. The enabled interrupts are (NVIC-&amp;gt;ISER = 0x03002801):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;POWER_CLOCK_IRQn&lt;/li&gt;
&lt;li&gt;RTC0_IRQn&lt;/li&gt;
&lt;li&gt;RNG_IRQn&lt;/li&gt;
&lt;li&gt;SWI4_IRQn&lt;/li&gt;
&lt;li&gt;SWI5_IRQn&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And the pending interrupts are (NVIC-&amp;gt;ISPR = 0x00002000):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;RNG_IRQn&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Are there any other registers to check whether an event/interrupt is pending? Assuming that a user interrupt like GPIOTE is enabled and pending, why would it only occur when the LFCLK crystal is used?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33754?ContentTypeID=1</link><pubDate>Mon, 14 Sep 2015 14:00:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:172e1490-86a5-4509-a9d4-b32846d85ec7</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;well you do have the benefit of having the source code :)&lt;/p&gt;
&lt;p&gt;If the flag is incremented any time there&amp;#39;s an interrupt forwarded to the app vector table after the softdevice is active, and if the only thing which resets it again is running sd_app_evt_wait(), then it seems if any user interrupt has occurred at any time after the SD was enabled then sd_app_evt_wait() will instantly exit the first time it&amp;#39;s run, and there&amp;#39;s not much Remo can do about that. A rather hacky fix would be to ensure a user interrupt has occurred, then run sd_app_evt_wait() twice, once to clear the flags, then to wait.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33753?ContentTypeID=1</link><pubDate>Mon, 14 Sep 2015 13:46:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9628d063-7ef2-4c57-84ef-ff956ce73b6f</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;its filter for interrupt forwarding mechanism, you seems to have decoded it almost.
one flag is incremented everytime there is an interrupt forwarded from SD vector table to App vector table.
the other flag is just checks that there are enough events happened for it to not wait. When they are not equal, the second will assign itself the value of the first one and exits, if they are equal then it waits. This is too simple to have a bug in it.&lt;/p&gt;
&lt;p&gt;@Remo: from your observation of RK&amp;#39;s suggestion it is 100% clear that there is an application interrupt happening (definetely not softdevice events). Check all your interrupt handlers. I also see that you did not enable clear timer short&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; NRF_TIMER2-&amp;gt;SHORTS  = (TIMER_SHORTS_COMPARE1_CLEAR_Enabled &amp;lt;&amp;lt; TIMER_SHORTS_COMPARE1_CLEAR_Pos);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Check if there are any other peripheral interrupts like GPIOTE etc.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33752?ContentTypeID=1</link><pubDate>Mon, 14 Sep 2015 13:40:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:95a43b62-a83b-48a8-9640-ed5372c23f92</guid><dc:creator>Remo</dc:creator><description>&lt;p&gt;Your replacement for the function sd_app_event_wait() didn&amp;#39;t work. It woke up again immediately. Do you have another idea?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33751?ContentTypeID=1</link><pubDate>Mon, 14 Sep 2015 13:23:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5d0f734c-7339-4ea2-b38f-bc2627890212</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;I hit break inside sd_app_event_wait(), it&amp;#39;s only about 10 lines of assembler and it&amp;#39;s not too hard to see what it does. If the two bytes are equal, it loops back to the __wfe(), if they aren&amp;#39;t, it exits the wait (having set them equal again).&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t think your suggestion will help. The softdevice is constantly generating interrupts and events so that code will pretty much instantly exit in all cases. Those internal flags seem to act as a filter to keep it looping on internal events and interrupts and only exit sd_app_event_wait() when the softdevice has generated an event the user might want to wake up for. What I don&amp;#39;t know is what increments the flag which says &amp;#39;this is a real user event&amp;#39; and causes the loop to exit.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33750?ContentTypeID=1</link><pubDate>Mon, 14 Sep 2015 12:30:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:df5112f3-2c6b-48ef-ae74-28b9bda4249c</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;@RK, where did you get those addresses? They are in RAM space used by softdevice. What kind of magic are you doing :)&lt;/p&gt;
&lt;p&gt;@Remo
Can you try replacing sd_app_event_wait() with&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;/* Wait for event */
__WFE();

/* Clear Event Register */
__SEV();
__WFE();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If this works for you, there is something that looks to have a bug and it will make me dig more into softdevice code.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33749?ContentTypeID=1</link><pubDate>Mon, 14 Sep 2015 12:24:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bd686576-9bc7-4c12-b5dd-44f382780530</guid><dc:creator>Remo</dc:creator><description>&lt;p&gt;It looks like the function sd_evt_get() doesn&amp;#39;t reset these flags and it still wakes up immediately. I checked also if there is a pending BLE event, but it isn&amp;#39;t.
I guess you are right, but unfortunately, we need to put the CPU into sleep mode only at a specific point. Thank you for your help!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33747?ContentTypeID=1</link><pubDate>Mon, 14 Sep 2015 11:52:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b1036ab6-76d3-4ce1-b16a-30cc29630b9c</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;That&amp;#39;s what I expected. However I don&amp;#39;t know what events increment the counter at 0x2000006b. You would think it would only happen when there&amp;#39;s an event ready to be retrieved with sd_evt_get() but clearly there&amp;#39;s at least one other happening which sets the flag. I&amp;#39;m afraid I can&amp;#39;t even really hazard a guess as to what that might be. Does calling sd_evt_get(), even though it returns no pending event, reset the flags or does the event wait still wake up immediately? Did you also try getting a ble event, it could be one of those too.&lt;/p&gt;
&lt;p&gt;I suspect most people use sd_app_event_wait() to sleep the CPU in a processing loop and not to try and sleep the CPU exactly once and have it wake exactly once, so they don&amp;#39;t notice the issue.&lt;/p&gt;
&lt;p&gt;That exhausts my knowledge on this&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33746?ContentTypeID=1</link><pubDate>Mon, 14 Sep 2015 11:37:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:95173cd3-621e-4a5c-afeb-2097cc0a1aaf</guid><dc:creator>Remo</dc:creator><description>&lt;p&gt;I tried what you&amp;#39;ve suggested and you are right. The content of 0x2000006b is 0x01 and 0x2000006c is 0x00. After the function call sd_app_event_wait() they are both 0x01. So this means that there is any pending system event, right? But when I call the function sd_evt_get() right before sd_app_event_wait() it says that there is no pending event.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33745?ContentTypeID=1</link><pubDate>Mon, 14 Sep 2015 10:00:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9de2a90b-f4cf-4903-b5e2-d9c436ec7d3b</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;One possibility here is that a system event has already happened (why I don&amp;#39;t know) and not been dequeued so the sd_app_event_wait() returns immediately.&lt;/p&gt;
&lt;p&gt;If you try the following odd experiment it may show if that&amp;#39;s the case or not.&lt;/p&gt;
&lt;p&gt;before you call sd_app_event_wait(), get the contents of 0x2000006b and 0x2000006c into two byte-sized variables. If they are the same, sd_app_event_wait() should wait, if they are different, it should return immediately. If you get them again right after the sd_app_event_wait() they should be the same, and larger than before.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33744?ContentTypeID=1</link><pubDate>Mon, 14 Sep 2015 07:39:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:40f3b4eb-dd2d-4e5a-a7c6-411ab750a53b</guid><dc:creator>Remo</dc:creator><description>&lt;p&gt;Hi Aryan. I&amp;#39;ve updated the question with my code which causes this issue. Thank you for your help!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33742?ContentTypeID=1</link><pubDate>Sun, 13 Sep 2015 09:02:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b2fd0bd1-08ae-4eab-a736-8573bd01d627</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;can you post the code here, if not upload it somewhere and send me link in pvt msg&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33741?ContentTypeID=1</link><pubDate>Thu, 10 Sep 2015 14:14:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:75cbd231-7f31-483d-b666-0573804e67b5</guid><dc:creator>Remo</dc:creator><description>&lt;p&gt;Hi Aryan. I checked every NRF51 specific interrupt (from POWER_CLOCK_IRQn to SWI5_IRQn) directly after waking up but no interrupt was pending.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Wakeup immediately from sd_app_evt_wait() with external LFCLK</title><link>https://devzone.nordicsemi.com/thread/33740?ContentTypeID=1</link><pubDate>Thu, 10 Sep 2015 13:20:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:73503d14-0882-4c33-8496-d87bf6dfb2eb</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;can you check what is waking your system up.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;irq_num = NVIC_GetPendingIRQ(IRQn_Type IRQn)
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>