<?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>How do you put the nrf51822 chip to sleep?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/490/how-do-you-put-the-nrf51822-chip-to-sleep</link><description>I am wondering how you put the nrf51822 chip to sleep. I&amp;#39;ve determined that the way to do this with the soft device is sd_app_event_wait() in the while loop. What about non-softdevice? 
 I am wanting to have it sleep until woken by interrupts, then sleep</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 26 Jan 2017 17:02:50 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/490/how-do-you-put-the-nrf51822-chip-to-sleep" /><item><title>RE: How do you put the nrf51822 chip to sleep?</title><link>https://devzone.nordicsemi.com/thread/2570?ContentTypeID=1</link><pubDate>Thu, 26 Jan 2017 17:02:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:51ee463d-4a58-4398-8fff-acaaa79a3fd9</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;I agree what Radoslaw says. I think the correct order should be __WFE(); __SEV(); __WFE();
This way we make sure that the first __WFE does not sleep if the event just happened before calling it. The other way would just clear that event, which is not a problem if this is called in loop inside main. But it will be problem if it is called in different priority context.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you put the nrf51822 chip to sleep?</title><link>https://devzone.nordicsemi.com/thread/2569?ContentTypeID=1</link><pubDate>Wed, 09 Dec 2015 08:39:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:011f3f61-16f3-4ac1-b6d9-fa4b7af75196</guid><dc:creator>Radoslaw Koppel</dc:creator><description>&lt;p&gt;This is not the solution for every situation and please do not copy it without understanding. Clearing event flag just before the WFE that should actually wait for event basically doest the same what WFI instruction. WFI instruction is not safe because it would not wake up the system when interrupt happens just before its call. You may call WFE(); SEV(); WFE(); processing(); Now you are sure that you clear event flag that was set in any interrupts just before processing all the events in the main loop. Single WFE should also clear the flag but it may be set again if there would be many interrupts processing just after system wake-up.&lt;/p&gt;
&lt;p&gt;If you do not clear the event flag your main loop may be processed more times you expect.
Just thing twice before you clear event flag: in worst case you may lost information about event that should be processed.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you put the nrf51822 chip to sleep?</title><link>https://devzone.nordicsemi.com/thread/2568?ContentTypeID=1</link><pubDate>Mon, 20 Apr 2015 07:40:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0ab3a664-38a2-4558-9c53-e96fe4931d26</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Just curious but what happens if there is some event happening in between two __WFE() you mentioned. To guarantee this I am thinking that we need to set SEVONPEND = 0 in ARM system control register.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you put the nrf51822 chip to sleep?</title><link>https://devzone.nordicsemi.com/thread/2573?ContentTypeID=1</link><pubDate>Thu, 11 Sep 2014 10:08:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6ad072aa-6e8b-4934-bd42-1ebb1b56c95c</guid><dc:creator>Tim</dc:creator><description>&lt;p&gt;More info:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka15473.html"&gt;infocenter.arm.com/.../index.jsp&lt;/a&gt;
&lt;a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/Cjafcggi.html"&gt;infocenter.arm.com/.../index.jsp&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you put the nrf51822 chip to sleep?</title><link>https://devzone.nordicsemi.com/thread/2572?ContentTypeID=1</link><pubDate>Wed, 13 Aug 2014 14:18:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f4932362-8cf2-41b8-b692-da7bd65b5ec7</guid><dc:creator>Ck</dc:creator><description>&lt;p&gt;Anders wrote&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;quot;The event flag is set by a number of
different things in the CPU including
returning from an interrupt.&amp;quot;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Are you sure event flag is set after interrupt is done? In such case WFE is safe and race condition can&amp;#39;t happen. Just to be sure I&amp;#39;m using SEV after flag was set, like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;void RTC1_IRQHandler(void)
{
  /* Update compare counter */
	if (NRF_RTC1-&amp;gt;EVENTS_COMPARE[0] != 0)
  {
		NRF_RTC1-&amp;gt;EVENTS_COMPARE[0] = 0;
		NRF_RTC1-&amp;gt;TASKS_CLEAR = 1;  // Clear Counter		    
		if(U32_delay_ms) U32_delay_ms--; // used in V_hw_delay_ms()
		__SEV(); // to avoid race condition
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you put the nrf51822 chip to sleep?</title><link>https://devzone.nordicsemi.com/thread/2565?ContentTypeID=1</link><pubDate>Fri, 20 Sep 2013 06:05:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:64df67e6-1667-4906-b0e6-45f7e83d08ef</guid><dc:creator>Carles</dc:creator><description>&lt;p&gt;See Ander&amp;#39;s comments below. But calling it twice makes sure you actually do go to sleep (first call clears the Event Flag, second call will definitely sleep)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you put the nrf51822 chip to sleep?</title><link>https://devzone.nordicsemi.com/thread/2571?ContentTypeID=1</link><pubDate>Fri, 20 Sep 2013 05:12:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:40f5a0f4-b753-4329-9792-f2f807014b4d</guid><dc:creator>Anders Nore</dc:creator><description>&lt;p&gt;Calling __WFE twice is basically the same as doing a __WFI.&lt;/p&gt;
&lt;p&gt;When waiting for something in a loop it&amp;#39;s usually best to use __WFE(). This is to avoid hitting race conditions if you are waiting for a flag for instance.&lt;/p&gt;
&lt;p&gt;In the code below, we are waiting for a timer compare event to trigger. If you want to save power while waiting, then you should use a __WFE() instruction.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
main()
{
  ...

  while(flag == 0)
  {
    __WFE();  
  }

  ...
}

TIMER_ISR()
{
  flag = true;
}

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;__WFE has an internal event flag that will prevent it from going to sleep if it&amp;#39;s set. In addition, if you call it and the event flag is set, __WFE will clear it. The event flag is set by a number of different things in the CPU including returning from an interrupt.&lt;/p&gt;
&lt;p&gt;In the case above, the TIMER interrupt is enabled and will set a flag. Whenever there is a wake-up event in the system, the loop will check if the timer event has happened or else it will sleep.&lt;/p&gt;
&lt;p&gt;If you use __WFI() in the case above, there is a race condition. __WFI will sleep if you call it and wake up if there is a wake-up event. But it does not have an internal event register that prevents it from sleeping if something has occured. This means that if the timer interrupt occurs after you checking for (flag == 0) but before calling __WFI(), you could end up sleeping forever.&lt;/p&gt;
&lt;p&gt;There&amp;#39;s also more differences between WFI/WFE that are shown in the table below:
&lt;img src="https://devzone.nordicsemi.com/index.php/discussions?controller=attachment&amp;amp;task=displayFile&amp;amp;tmpl=component&amp;amp;id=195" alt="https://devzone.nordicsemi.com/index.php/discussions?controller=attachment&amp;amp;task=displayFile&amp;amp;tmpl=component&amp;amp;id=195" /&gt;&lt;/p&gt;
&lt;p&gt;SEVONPEND and PRIMASK are internal processor registers that can be configured.
In short, SEVONPEND enables you to wake up to interrupts occuring at a lower priority then the current context in which __WFE is called. __WFI does not care about the SEVONPEND status.
It&amp;#39;s only __WFI that can wake up when PRIMASK (aka. __disable_interrupts()) is set.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/WFE_5F00_WFI_5F00_wake_5F00_ups.jpg" alt="WFE_WFI_wake_ups.jpg" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you put the nrf51822 chip to sleep?</title><link>https://devzone.nordicsemi.com/thread/2564?ContentTypeID=1</link><pubDate>Thu, 19 Sep 2013 16:30:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:78929250-1439-495d-ad45-d12aa1b42053</guid><dc:creator>Bastiaan</dc:creator><description>&lt;p&gt;What about calling it in a loop?  Why do you use WFE twice?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you put the nrf51822 chip to sleep?</title><link>https://devzone.nordicsemi.com/thread/2567?ContentTypeID=1</link><pubDate>Thu, 19 Sep 2013 16:30:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d1c208ad-ca41-432e-8b32-0d0629a6ec03</guid><dc:creator>Guest</dc:creator><description>&lt;p&gt;What about calling it in a loop?  Why do you use WFE twice?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you put the nrf51822 chip to sleep?</title><link>https://devzone.nordicsemi.com/thread/2563?ContentTypeID=1</link><pubDate>Thu, 19 Sep 2013 11:50:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:12225ce3-f54a-4e40-859f-469575d5e03d</guid><dc:creator>Carles</dc:creator><description>&lt;p&gt;Actually yes, if you don&amp;#39;t call it in a loop then the order you suggest will make sure that you sleep.&lt;/p&gt;
&lt;p&gt;Fixed my response, thanks!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you put the nrf51822 chip to sleep?</title><link>https://devzone.nordicsemi.com/thread/2562?ContentTypeID=1</link><pubDate>Thu, 19 Sep 2013 11:42:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a38a5ded-b810-44b3-9319-efbf80702cb9</guid><dc:creator>Bastiaan</dc:creator><description>&lt;p&gt;Shouldn&amp;#39;t it be like this?&lt;/p&gt;
&lt;p&gt;__SEV();
__WFE();
__WFE();&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you put the nrf51822 chip to sleep?</title><link>https://devzone.nordicsemi.com/thread/2566?ContentTypeID=1</link><pubDate>Thu, 19 Sep 2013 11:42:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9536b5d2-69d8-4fe9-a5f6-f79ec66a0304</guid><dc:creator>Guest</dc:creator><description>&lt;p&gt;Shouldn&amp;#39;t it be like this?&lt;/p&gt;
&lt;p&gt;__SEV();
__WFE();
__WFE();&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you put the nrf51822 chip to sleep?</title><link>https://devzone.nordicsemi.com/thread/2561?ContentTypeID=1</link><pubDate>Thu, 19 Sep 2013 11:00:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:40334d2b-2966-4f4b-abef-7bb712fb5fc6</guid><dc:creator>Carles</dc:creator><description>&lt;p&gt;Something like this should work:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
void sleep()
{

    /* Clear Event Register */
    __SEV();
    /* Wait for event */
    __WFE();
    /* Wait for event */
    __WFE();
}

&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do you put the nrf51822 chip to sleep?</title><link>https://devzone.nordicsemi.com/thread/2560?ContentTypeID=1</link><pubDate>Thu, 19 Sep 2013 10:45:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ced7a667-7f81-4218-8d92-26b8ecfcb91c</guid><dc:creator>Matthieu Tardivon</dc:creator><description>&lt;p&gt;www.freqchina.com/cn/down.asp?ID=135&lt;/p&gt;
&lt;p&gt;The answer is at p42 (the answer is always 42 :p). I think you need the __WFE() command for your case. You will understand the difference by reading the first section 11.1.3&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>