<?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>nrf_pwr_mgmt_run sometimes not sleeping?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/93376/nrf_pwr_mgmt_run-sometimes-not-sleeping</link><description>nRF52840, SDK v17.0.2, SD140 v7.0.1 
 
 My device is sometimes (only sometimes!) failing to enter a low-power sleep mode sometime around establishing a BLE connection; I am observing my main-loop calls to nrf_pwr_mgmt_run() returning immediately and never</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 03 Jun 2024 08:20:46 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/93376/nrf_pwr_mgmt_run-sometimes-not-sleeping" /><item><title>RE: nrf_pwr_mgmt_run sometimes not sleeping?</title><link>https://devzone.nordicsemi.com/thread/487051?ContentTypeID=1</link><pubDate>Mon, 03 Jun 2024 08:20:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:592244a3-da45-4a28-a1c1-a7798c881aba</guid><dc:creator>agecalculator</dc:creator><description>&lt;p&gt;Excellent, thanks a lot for your help.&lt;/p&gt;
&lt;p&gt;&lt;span style="color:#ffffff;"&gt;&lt;a style="color:#ffffff;" title="Age Calculator" href="https://www.agecalculator.page/"&gt;age calculator&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_pwr_mgmt_run sometimes not sleeping?</title><link>https://devzone.nordicsemi.com/thread/419720?ContentTypeID=1</link><pubDate>Tue, 11 Apr 2023 13:12:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:53b0bda1-533f-4148-9f40-332d019864fe</guid><dc:creator>charles_fi</dc:creator><description>&lt;p&gt;The issue turned out to be us doing blocking TWI transfers (and other assorted bus transmissions) from inside the EGU2 SoftDevice handler. For whatever reason, this causes the nRF52840 to never successfully enter the sleep state again; it always instantly wakes up.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If I were starting a project from scratch, I&amp;#39;d probably not use any of the SD Observers and simply poll SD for events at a known stable point in my application instead. That approach seems simpler and safer, even if it means not being able to use a good chunk of the SDK observer-based code.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_pwr_mgmt_run sometimes not sleeping?</title><link>https://devzone.nordicsemi.com/thread/419575?ContentTypeID=1</link><pubDate>Tue, 11 Apr 2023 08:42:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:69f666df-747a-40ee-a5ae-bfcfed7cbcb2</guid><dc:creator>xonoy18882</dc:creator><description>&lt;p&gt;If you are experiencing issues with the &lt;code&gt;nrf_pwr_mgmt_run&lt;/code&gt; function not sleeping your device as expected, there are a few potential causes to investigate.&lt;/p&gt;
&lt;p&gt;Firstly, ensure that you have configured the power management module correctly, using the &lt;code&gt;nrf_pwr_mgmt_init&lt;/code&gt; function. This should be called early in your application&amp;#39;s initialization process.&lt;/p&gt;
&lt;p&gt;Another potential cause is that some peripherals or modules may be preventing the device from entering sleep mode. Check that all peripherals are properly configured and that there are no interrupts or other events preventing the device from sleeping.&lt;/p&gt;
&lt;p&gt;Finally, ensure that your application is correctly handling any events or tasks that may be waking the device from sleep mode. If a task is waking the device but not allowing it to return to sleep, this could be causing the issue.&lt;/p&gt;
&lt;p&gt;By investigating these potential causes, you should be able to &lt;a title="fnf" href="https://fridaynightfunkin.lol/"&gt;fnf&lt;/a&gt;&amp;nbsp;diagnose and resolve any issues with the &lt;code&gt;nrf_pwr_mgmt_run&lt;/code&gt; function not sleeping your device as expected.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;div class="translationcontainer___extension" style="display:none;left:-5px;top:109px;"&gt;&lt;span class="sentences___extension" data-dstlang="ru"&gt;фн&lt;/span&gt;&lt;span class="origsentences___extension" data-srclang="no"&gt;fnf&lt;/span&gt;
&lt;div class="bottom___extension"&gt;
&lt;div class="speakerimg___extension"&gt;&lt;/div&gt;
&lt;div class="detected-language___extension"&gt;Detected language : Norwegian&lt;/div&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="translationcontainer___extension" style="display:none;left:-5px;top:135px;"&gt;&lt;span class="sentences___extension" data-dstlang="ru"&gt;фн&lt;/span&gt;&lt;span class="origsentences___extension" data-srclang="no"&gt;fnf&lt;/span&gt;
&lt;div class="bottom___extension"&gt;
&lt;div class="speakerimg___extension"&gt;&lt;/div&gt;
&lt;div class="detected-language___extension"&gt;Detected language : Norwegian&lt;/div&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_pwr_mgmt_run sometimes not sleeping?</title><link>https://devzone.nordicsemi.com/thread/393559?ContentTypeID=1</link><pubDate>Wed, 02 Nov 2022 03:13:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:247fa4d9-0c56-42a6-b33f-b43dd988c584</guid><dc:creator>yunaren</dc:creator><description>&lt;p&gt;Great. Thanks for your answers.&lt;/p&gt;
&lt;p style="text-align:center;"&gt;&lt;span style="color:#ffffff;font-size:75%;"&gt;&lt;a style="color:#ffffff;" href="https://lol-beans.com"&gt;lolbeans&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_pwr_mgmt_run sometimes not sleeping?</title><link>https://devzone.nordicsemi.com/thread/393529?ContentTypeID=1</link><pubDate>Tue, 01 Nov 2022 16:22:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:005a0d1d-010b-4b26-9ffe-e78d4a874a06</guid><dc:creator>charles_fi</dc:creator><description>&lt;p&gt;Einar- Thanks so much for the deep investigation. I&amp;#39;ve replaced my sleep call with the following:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void cpu_init(void) {
  SCB-&amp;gt;SCR &amp;amp;= ~SCB_SCR_SEVONPEND_Msk;
}

void cpu_sleep(void) {
  CRITICAL_REGION_ENTER();
  __set_FPSCR(__get_FPSCR() &amp;amp; ~0x9Fu);
  __DSB();
  NVIC_ClearPendingIRQ(FPU_IRQn);
  CRITICAL_REGION_EXIT();

  sd_app_evt_wait();
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;And it seems to be working well now. I think SEVONPEND was the culprit; I&amp;#39;ve shipped a few nRF528xx IoT products now and this was the first time I&amp;#39;ve ever tried nrf_pwr_mgmt_run().&lt;/p&gt;
&lt;p&gt;Going back to handling the FPU IRQ errata myself and just calling sd_app_evt_wait() directly seems to be working well (again).&lt;/p&gt;
&lt;p&gt;Thanks a bunch for your help!&lt;/p&gt;
&lt;p&gt;Best,&lt;/p&gt;
&lt;p&gt;Charles&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_pwr_mgmt_run sometimes not sleeping?</title><link>https://devzone.nordicsemi.com/thread/393467?ContentTypeID=1</link><pubDate>Tue, 01 Nov 2022 12:54:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:532e2193-21bb-4e5b-b29a-688a6fb09afc</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote user="charles_fi"]I don&amp;#39;t see any examples in the nRF SDK of calling sd_nvic_ClearPendingIRQ (or manipulating the ICPR registers in general) to implement this note. Am I doing it correctly?[/quote]
&lt;p&gt;That is a good question. This is only&amp;nbsp;relevant for disabled interrupts (as stated in the API documentation for&amp;nbsp;sd_app_evt_wait()). But that leads to something interesting. With disabling interrupt and setting SEVOPENED you would wake up on an event but the corresponding ISR is not run (as it is not enabled in the interrupt mask in the NVIC). If you have an interrupt source which is disabled in the interrupt mask it could explain what you are seeing (device keeps&amp;nbsp;waking&amp;nbsp;up, but no ISR is run).&lt;/p&gt;
[quote user="charles_fi"]I guess a deeper question I have is &amp;quot;why does nrf_pwr_mgmt_init set SEVONPEND?&amp;quot;[/quote]
&lt;p&gt;I have not been able to find&amp;nbsp;explicitly why this is doen in nrf_pwr_mgmt, but it makes sense in some cases. (For instance, in our FreeRTOS port we do the same for RTC the RTC to act as periodic wake-up source without&amp;nbsp;needing to spend CPU cycles in a ISR doing nothing.)&lt;/p&gt;
[quote user="charles_fi"]&lt;p&gt; If I&amp;#39;m not using nrf_pwr_mgmt_init anymore, and just doing the FPU errata and WFE myself, do I need SEVONPEND? My application is running at the lowest priority, which seems relevant.&lt;/p&gt;
&lt;p&gt;Can I simply leave SEVONPEND disabled and still just WFE? That seems simpler.&lt;/p&gt;[/quote]
&lt;p&gt;I don&amp;#39;t have a full overview of your design, but in principle, yes. It should be OK to leave SEVOPENED disabled in most cases.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_pwr_mgmt_run sometimes not sleeping?</title><link>https://devzone.nordicsemi.com/thread/393272?ContentTypeID=1</link><pubDate>Mon, 31 Oct 2022 14:16:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:49a31d6a-2afc-4732-89b4-2d008753cef8</guid><dc:creator>charles_fi</dc:creator><description>&lt;p&gt;Thanks for the quick response, Einar-&lt;/p&gt;
&lt;p&gt;I think my original table displays that no _new_ interrupts are occurring, or I&amp;#39;d expect to see the &amp;quot;main loop turns&amp;quot; counter match one of the &amp;quot;xyz_IRQn&amp;quot; counters, but they&amp;#39;re off by a few orders of magnitude. My original table also displays an almost exact 1:1 matching between &amp;quot;main loop turns&amp;quot; and &amp;quot;sleep attempts&amp;quot;; I do think that my &amp;quot;idle&amp;quot; flag works, or I would expect to see many loop turns and few sleep attempts.&lt;/p&gt;
&lt;p&gt;nrf_pwr_mgmt_run() does some tidying up and then calls sd_app_evt_wait(). The documentation for sd_app_evt_wait() says the following in a note:&lt;/p&gt;
&lt;p&gt;&amp;quot;&lt;span&gt;The application must ensure that the pended flag is cleared using&amp;nbsp;&lt;/span&gt;&lt;a class="el" title="Clear Pending Interrupt." href="https://www.taterli.com/nrf5/s140/group___n_r_f___n_v_i_c___f_u_n_c_t_i_o_n_s.html#gaaab82d624bd21852288a3a6ad178cec7"&gt;sd_nvic_ClearPendingIRQ&lt;/a&gt;&lt;span&gt;&amp;nbsp;in order to sleep using this function. This is only necessary for disabled interrupts, as the interrupt handler will clear the pending flag automatically for enabled interrupts.&amp;quot;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;One difference between my &amp;quot;this seems to work&amp;quot; update post and my original &amp;quot;this sometimes doesn&amp;#39;t work&amp;quot; post is that I&amp;#39;m doing&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt; // Un-pend any disabled pending interrupts&lt;br /&gt; NVIC-&amp;gt;ICPR[0] = ~NVIC-&amp;gt;ICER[0];&lt;br /&gt; NVIC-&amp;gt;ICPR[1] = ~NVIC-&amp;gt;ICER[1];&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I don&amp;#39;t see any examples in the nRF SDK of calling sd_nvic_ClearPendingIRQ (or manipulating the ICPR registers in general) to implement this note. Am I doing it correctly?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;In an earlier attempt, I un-pended all disabled interrupts while in a critical section- that made SoftDevice stop working. I&amp;#39;m assuming some number of SD interrupts fire between when I exit my critsec and when I hit ICPR.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;I guess a deeper question I have is &amp;quot;why does nrf_pwr_mgmt_init set SEVONPEND?&amp;quot; If I&amp;#39;m not using nrf_pwr_mgmt_init anymore, and just doing the FPU errata and WFE myself, do I need SEVONPEND? My application is running at the lowest priority, which seems relevant.&lt;/p&gt;
&lt;p&gt;Can I simply leave SEVONPEND disabled and still just WFE? That seems simpler.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_pwr_mgmt_run sometimes not sleeping?</title><link>https://devzone.nordicsemi.com/thread/393254?ContentTypeID=1</link><pubDate>Mon, 31 Oct 2022 13:29:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f0b5a60e-f4cb-489b-9ba4-8061793bd5fe</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;Charles,&lt;/p&gt;
[quote user=""]1. Are there any known errata in SD140 v7.0.1 that could cause this?[/quote]
&lt;p&gt;No, I do not think so.&lt;/p&gt;
[quote user=""]2. Are there any paths in the opaque SoftDevice binary&amp;nbsp;function sd_app_evt_wait that return to the user without actually doing the WFE/SEV sleep?[/quote]
&lt;p&gt;No,&amp;nbsp;&lt;code&gt;sd_app_evt_wait()&lt;/code&gt; will always result in a sequence of&amp;nbsp; &lt;code&gt;__WFE();&amp;nbsp;__SEV();&amp;nbsp;__WFE();&lt;/code&gt;. After wake-up, a mask is checked to mask out any SoftDevice IRQs. If any bits are set in the ISPR that are not set, sd_app_evt_wait() returns. If not it will (conceptually) call __WFE(); __SEV(); __WFE(); again.&lt;/p&gt;
[quote user=""]3. Does anyone have any ideas what I can try next, or where I can look, to root-cause and fix this?[/quote]
&lt;p&gt;The most obvious suspects are that either you do not clear some event(s) that cause interrupts, or simply the&amp;nbsp;idle variable in your pseudo code is not correctly updates (so that it is not true when it should be or it is not volatile).&lt;/p&gt;
[quote user="charles_fi"]In this specific experiment below, I have also set SEVONPEND to 1, though I&amp;#39;m not sure what the implications are or when / why I&amp;#39;d want that in production.[/quote]
&lt;p&gt;That should be OK I believe. However,&amp;nbsp;SEVONPEND should already be set to 1, as you use the nrf_pwr_mgmt module&amp;nbsp;(done when you call&amp;nbsp;&lt;code&gt;nrf_pwr_mgmt_init()&lt;/code&gt;).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf_pwr_mgmt_run sometimes not sleeping?</title><link>https://devzone.nordicsemi.com/thread/393111?ContentTypeID=1</link><pubDate>Sat, 29 Oct 2022 13:20:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a1f392bf-115a-46b1-9a36-3bcf076f07db</guid><dc:creator>charles_fi</dc:creator><description>&lt;p&gt;Update-&lt;/p&gt;
&lt;p&gt;This seems to help, but I don&amp;#39;t know why and I&amp;#39;m not sure if it&amp;#39;s safe to use when SoftDevice is active. I really don&amp;#39;t think this counts as a &amp;quot;fix&amp;quot; yet, but it does seem like evidence that sd_app_evt_wait() has a fallthrough path where it returns without running WFE/SEV? It sure would be nice if the source code for SoftDevice was available for reading... &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this specific experiment below, I have also set SEVONPEND to 1, though I&amp;#39;m not sure what the implications are or when / why I&amp;#39;d want that in production.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void cpu_sleep(void) {
  CRITICAL_REGION_ENTER();
  __set_FPSCR(__get_FPSCR() &amp;amp; ~0x9Fu);
  __DSB();
  NVIC_ClearPendingIRQ(FPU_IRQn);
  __WFE();
  CRITICAL_REGION_EXIT();

  // Un-pend any disabled pending interrupts
  NVIC-&amp;gt;ICPR[0] = ~NVIC-&amp;gt;ICER[0];
  NVIC-&amp;gt;ICPR[1] = ~NVIC-&amp;gt;ICER[1];
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Charles&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>