<?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>nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/93531/nrf52840-app-not-sleeping-softdevice-seems-involved</link><description>Hello- 
 I&amp;#39;m seeing the nRF52840 stay awake when I don&amp;#39;t want it to. I&amp;#39;m calling sd_app_evt_wait() every turn through the main loop with SEVONPEND disabled, but it&amp;#39;s waking up and falling through. 
 
 I&amp;#39;m capturing a histogram of the entire IRQ set, now</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 24 Nov 2022 12:30:30 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/93531/nrf52840-app-not-sleeping-softdevice-seems-involved" /><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/397412?ContentTypeID=1</link><pubDate>Thu, 24 Nov 2022 12:30:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:94476fb3-97a4-4eb9-abb4-69addd26d8a1</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Charles,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks for the info. It&amp;#39;s a good practice to avoid&amp;nbsp;complex activities or blocking functions inside a interrupt handler. But I assume this doesn&amp;#39;t explain the interrupt histogram you got.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Let us know if you still see the issue occurs.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/397286?ContentTypeID=1</link><pubDate>Wed, 23 Nov 2022 22:25:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b0436337-f701-418f-9fdc-569e094a5493</guid><dc:creator>charles_fi</dc:creator><description>&lt;p&gt;Update-&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;We were doing a lot of work (blocking TWI / SPI transactions etc) inside of our SoftDevice event handler, which happens from inside the SWI2/EGU2 interrupt handler.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;We changed our code to simply enqueue inbound writes to a queue in our handler, and our main loop drains the queue and processes from the thread context, with the rest of our logic.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not sure exactly what caused the system from sleeping, but it might have been us doing something &amp;quot;bad&amp;quot; from inside the SWI2 interrupt observer callback.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/395844?ContentTypeID=1</link><pubDate>Tue, 15 Nov 2022 15:52:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e661a59a-7f08-4cad-af98-7deaff0762fd</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Charles,&amp;nbsp;&lt;br /&gt;I don&amp;#39;t see a problem calling&amp;nbsp;&lt;span&gt;sd_ble_gap_disconnect&amp;nbsp;() from a BLE event handler. Unless you changed the default priority.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;For example in most of our examples you can find that we&amp;nbsp;do this ( ble_app_hrs example):&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;img style="max-height:193px;max-width:479px;" height="193" src="https://devzone.nordicsemi.com/resized-image/__size/958x386/__key/communityserver-discussions-components-files/4/pastedimage1668527402297v1.png" width="479" alt=" " /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;br /&gt;You can have a look at this statement &lt;a href="https://infocenter.nordicsemi.com/topic/sds_s140/SDS/s1xx/processor_avail_interrupt_latency/exception_mgmt_sd.html?cp=4_7_4_0_15_1"&gt;here&lt;/a&gt;:&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1668527467777v2.png" alt=" " /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;So as long as you don&amp;#39;t call the softdevice API from an interrupt priority at higher level than 4 (at level 2 or 3) you should be fine.&amp;nbsp;&lt;br /&gt;By default SWI2 interrupt has interrupt priority level 6.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/395636?ContentTypeID=1</link><pubDate>Mon, 14 Nov 2022 19:06:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:84d22cad-36ac-49c2-8e6e-6fd738aca3e0</guid><dc:creator>charles_fi</dc:creator><description>&lt;p&gt;Hello! Maybe a clue-&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;It seems to start happening right around when I disconnect from BLE. I notice that I&amp;#39;m calling sd_ble_gap_disconnect from my event handler, which is called by the nRFSDK observer, which I believe happens from a SWI2 interrupt. I haven&amp;#39;t changed any of the interrupt priorities; is it safe to call arbitrary SoftDevice functions from inside an observer?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/395098?ContentTypeID=1</link><pubDate>Thu, 10 Nov 2022 12:30:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c4f9fb4b-a32c-47d0-9167-8e553adb93f1</guid><dc:creator>charles_fi</dc:creator><description>&lt;p&gt;I see, thanks for the response. I&amp;#39;ll keep collecting data and searching; I&amp;#39;ll update this thread with any evidence or questions as they arise.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks also for continuing to engage with me on this, I appreciate it!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/395096?ContentTypeID=1</link><pubDate>Thu, 10 Nov 2022 12:28:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f3e44dde-bf9e-4ce3-916e-2bd6af33505f</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Charles,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;The problem of not having a spinlock loop is that there is a chance that in your functions that is executed before the&amp;nbsp;&lt;span&gt;sd_app_evt_wait() or __WFE, you may trigger an interrupt.&amp;nbsp;&lt;br /&gt;This interrupt may wake the chip up immediately after&amp;nbsp;sd_app_evt_wait/WFE is called. If there is a spinlock, the loop continue and the next WFE at the beginning of the next loop will put the device to sleep as the event register is cleared by the WFE in the last loop.&amp;nbsp;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;If there is no spinlock loop (a loop checking for a&amp;nbsp;app_wakeup flag) the CPU will just jump to your poll functions which may trigger another interrupt.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;You may run to this issue if you process a UART logging right before the call. There can be an interrupt after you finish transmitting UART and that can wake the chip up. But if you already monitor all interrupts you should be able to spot that.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/394975?ContentTypeID=1</link><pubDate>Wed, 09 Nov 2022 16:40:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:327769b0-b7be-4e4a-ba1e-9d92a961f964</guid><dc:creator>charles_fi</dc:creator><description>&lt;p&gt;We have 2x UARTs in our system.&lt;/p&gt;
&lt;p&gt;1 is for logging data from our external WiFi chip, but we disable the UART when WiFi is inactive (and verify this by dumping the GPIO pin states + directions to the same server that creates the IRQ histograms). I&amp;#39;d love to hear your thoughts, though- are you thinking that the UART interrupts might be keeping the system awake or something?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Another UART is for factory-mode tests between nRF52840 and nRF9160 (LTE). In the field we use the TX/RX for SPI instead, and never initialize this UART peripheral.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;It&amp;#39;s absolutely possible that something in my poll functions triggers an interrupt, especially since starting and stopping timers triggers an interrupt in nRFSDK. But- If anything in my main loop triggered an interrupt , I would see it on my IRQn histogram, right? Is there a scenario where I wouldn&amp;#39;t?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I clear SEVONPEND once, in thread level, from my main function, early after boot.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;What happens if I don&amp;#39;t have WFE/SEV/WFE in a loop? Worst case, I run my main loop again, right?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/394951?ContentTypeID=1</link><pubDate>Wed, 09 Nov 2022 14:43:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6a290628-e026-4ba9-b5d3-80e0007f2303</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Charles,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;There is a mentioning about SEVONPEND in the softdevice specification at section 7.6:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:206px;max-width:616px;" height="206" src="https://devzone.nordicsemi.com/resized-image/__size/1232x412/__key/communityserver-discussions-components-files/4/5584.pastedimage1668003005620v1.png" width="616" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background-color:rgba(255, 0, 0, 1);"&gt;I&amp;nbsp;am not aware that softdevice explicitly set&amp;nbsp;&amp;nbsp;SEVONPEND &lt;/span&gt;&amp;nbsp;=&amp;gt; After check the source I saw that the softdevice may set/clear it , but it will return to the value has been set by the application after it&amp;#39;s done.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I assume you cleared the bit on low interrupt level or in thread level ?&amp;nbsp;&lt;br /&gt;&lt;br /&gt;If you think&amp;nbsp;&lt;span&gt;sd_app_evt_wait() could be the culprit you can try using:&lt;br /&gt;&lt;/span&gt;&lt;span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;__WFE&lt;span&gt;();&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;__SEV&lt;/span&gt;(); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt; &lt;span&gt;__WFE&lt;/span&gt;();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ADDED: Please make sure you use this in a spinlock loop as Vidar has suggested.&amp;nbsp;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It would have the same effect (except that the function returna when there is any softdevice activity)&lt;/p&gt;
&lt;p&gt;Unlike what you think of&amp;nbsp;&lt;span&gt;sd_app_evt_wait(), the spin for sleeping inside this function is not executed in the SVC context but in main context.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Have you made sure the&amp;nbsp;poll_a();poll_b(); doesn&amp;#39;t trigger any interrupt in a corner case or something ?&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Just to be extra sure, you don&amp;#39;t use any UART logging ?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/394760?ContentTypeID=1</link><pubDate>Tue, 08 Nov 2022 15:06:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:59006f3b-382f-4bfe-9087-997ce559b6ca</guid><dc:creator>charles_fi</dc:creator><description>&lt;p&gt;The difference between &amp;quot;mainLoopTurnCount&amp;quot; and &amp;quot;mainLoopSleepEnterCalls&amp;quot; is due to us realizing that because of the order of operations in our main loop, we can&amp;#39;t sleep yet,&amp;nbsp;roughly like this:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void main_loop(void) {
  increment_counter(mainLoopTurns);
  
  poll_a();
  poll_b(); // can provide immediate work for poll_a() to do!
  
  if (idle) { // no immediate work to do
    increment_counter(mainLoopSleepEnterCalls);
    sd_app_evt_wait();
  }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;We have RTT logging going, but of course there&amp;#39;s no debugger attached in the field &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;I collect this histogram from all of our employee modules, and I always see this pattern when the battery collapse happens. Sometimes it heals itself and stops, other times the battery is drained to exhaustion.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I agree that waking up at 2khz is really wild! I&amp;#39;m also extremely confused that I&amp;#39;m not seeing any of the histogram counts growing 1:1 with the main loop / sleep counts. I also agree that the SVCALL IRQn histogram growing almost 1:1 makes perfect sense- every call to sd_app_evt_wait() does an SVC, and then we have a few more for normal SD API calls that do the same.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I have never been able to reproduce this at my bench; it only happens in the field, and it always starts when the device receives a connection from a BLE central (phone or dedicated base).&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m running out of data to gather, and hypotheses to test, which is starting to make me nervous. If it&amp;#39;s not an IRQ, what else could be waking the system?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Perhaps another thread to explore: I&amp;#39;m explicitly disabling SEVONPEND. Does anything in SoftDevice turn it back on? Could it be related to that? If the ISR associated with an IRQ never actually runs, my counting shim won&amp;#39;t ever see it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/394728?ContentTypeID=1</link><pubDate>Tue, 08 Nov 2022 14:01:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5614881e-879d-4ab1-b80d-d5f72d8c6ea3</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Charles,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks for the explanation about the IRQ histogram&amp;nbsp;I wasn&amp;#39;t sure what it was earlier.&lt;/p&gt;
&lt;p&gt;So you have a counter on each of the IRQ handler and in the main loop.&lt;/p&gt;
&lt;p&gt;Could you explain&amp;nbsp;why there is a different of the counter between MainloopTurns and mainloopSleepEntercalls ?&amp;nbsp;&lt;/p&gt;
[quote user="charles_fi"]Note that the number of SVCALL interrupts is almost identical to the number of calls to sd_app_evt_wait(), which is also almost identical to the number of main loop turns.[/quote]
&lt;p&gt;I think the above behavior is true for any application, that the number of SVCall is almost identical to sd_app_evt_wait() and to the number of the main loop turns. It&amp;#39;s how it should be, isn&amp;#39;t it ?&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;The question here is only on why it waking up so often. As I can see from the table it&amp;#39;s about 2230 times per second.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It doesn&amp;#39;t ring any bell for us. It&amp;#39;s too fast to be any BLE activity. And without more detail or profiling it&amp;#39;s not possible to tell what could be the culprit here.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I assume you only managed to record the histogram once ? If you can reproduce the issue again you either using the power profiler or can think of toggling a GPIO in the main loop so that it&amp;#39;s easier to visualize the wake up behavior.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Do you have any other activity for example UART logging , RTT, ADC ? anything else that can involve ? Can you share the source code ? You can convert the case to a private case to share the code if needed.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/394525?ContentTypeID=1</link><pubDate>Mon, 07 Nov 2022 17:02:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:00996028-c623-4865-a2f1-553541f3e5f0</guid><dc:creator>charles_fi</dc:creator><description>&lt;p&gt;(btw the IRQn shim thing isn&amp;#39;t causing the wakeups; I wrote the IRQn shim thing in an attempt to gather data to diagnose and fix the issue :) )&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/394523?ContentTypeID=1</link><pubDate>Mon, 07 Nov 2022 17:00:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b124a3e0-f3b2-422b-a539-72035f7c78fa</guid><dc:creator>charles_fi</dc:creator><description>&lt;p&gt;I&amp;#39;m incrementing a counter every time I turn through my main loop, and I&amp;#39;m incrementing a separate counter every time I call sd_app_evt_wait(), and I&amp;#39;m incrementing a counter per-IRQn (all of them, via an assembly VTOR shim that intercepts all interrupts), so I can see all of the softdevice interrupts as well.&lt;/p&gt;
&lt;p&gt;Note that the number of SVCALL interrupts is almost identical to the number of calls to sd_app_evt_wait(), which is also almost identical to the number of main loop turns.&lt;/p&gt;
&lt;p&gt;This data tells me that I am regularly calling my main loop, and that I am also regularly calling sd_app_evt_wait(), and I&amp;#39;m not blasting SoftDevice with other calls.&lt;/p&gt;
&lt;p&gt;Please look to my first and second posts in this thread to observe the data that I&amp;#39;ve captured; perhaps something will jump out at you?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If I had some other high-frequency interrupt occurring, I&amp;#39;d expect to see it in my table above.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m using nRFSDK 17.0.2, SD140 7.0.1, nRF52840.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Also; I&amp;#39;m explicitly _clearing_ SEVONPEND, is it possible that disabled-but-pending interrupts are somehow contributing to wake?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/394514?ContentTypeID=1</link><pubDate>Mon, 07 Nov 2022 16:06:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3c187bb8-53ae-44bc-a973-62e61ad0714b</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Charles,&amp;nbsp;&lt;br /&gt;Is there any chance that it&amp;#39;s locked up somewhere and couldn&amp;#39;t enter&amp;nbsp;&lt;span&gt;sd_app_evt_wait() ?&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Short interval of 7.5ms shouldn&amp;#39;t be a problem. The BLE activity only takes from 2ms for an connection event. And if there is no BLE activity that requires the app involvement sd_app_evt_wait() will not return because of the BLE activity.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;br /&gt;&amp;nbsp;Could you give me some more info about the softdevice ? Which version are you using ?&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/394459?ContentTypeID=1</link><pubDate>Mon, 07 Nov 2022 14:03:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2f95b3d5-e888-49cd-8ee7-22f922e2d7b0</guid><dc:creator>charles_fi</dc:creator><description>&lt;p&gt;Thanks for the response. Because this is is very hard to reproduce, I&amp;#39;m looking for ways to gather more clues from units in the field.&lt;/p&gt;
&lt;p&gt;Another question- if I have a BLE central connect and hold a very fast connection interval, 7.5ms, could that cause behavior like this? sd_app_evt_wait() seems like it doesn&amp;#39;t return to my code from radio/timer0/rtc0 interrupts, but I&amp;#39;m curious if it&amp;#39;s possible.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m seeing ~100k+ main loop runs per minute, is the only option left that I actually have an interrupt firing that many times (kilohertz range)? I&amp;#39;d expect to see it on my IRQ histogram (see initial post) but I don&amp;#39;t.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/394400?ContentTypeID=1</link><pubDate>Mon, 07 Nov 2022 12:09:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a133d593-e116-422e-8cb8-2124e7228dd4</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Charles,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;What I meant by &amp;quot;clear the event&amp;quot; is to set an event register to 0 when you handle the event. For example like this:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1667821948693v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;But I was wrong when thinking that not clearing it may keep the CPU to not&amp;nbsp;entering sleep mode. The interrupt handler should be able to clear the interrupt source.&amp;nbsp;&lt;br /&gt;I don&amp;#39;t see any problem in the code you provided. I couldn&amp;#39;t think of anything else but an application interrupt that keep waking the CPU up.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you can reproduce the issue and can capture a power profile it should be possible to see if the CPU could enter sleep mode at all.&amp;nbsp;&lt;br /&gt;And if possible to reproduce, I would suggest to test on one of our example (with additional code to control PA LNA and coex) to see if you can see the same problem.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/394219?ContentTypeID=1</link><pubDate>Fri, 04 Nov 2022 15:09:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:84f81322-893a-4ef6-ab87-197e7ecc70f4</guid><dc:creator>charles_fi</dc:creator><description>&lt;p&gt;Sorry- to be more complete here, I&amp;#39;m doing this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void ble_pa_lna_enable(void) {
  if (s_util.pa_lna_enabled) {
    return;
  }

  APP_ERROR_CHECK_BOOL(s_util.radio_gpios.notification_gpio != NULL);
  APP_ERROR_CHECK_BOOL(s_util.radio_gpios.lna_gpio != NULL);
  APP_ERROR_CHECK_BOOL(s_util.radio_gpios.pa_gpio != NULL);

  s_util.pa_lna_enabled = true;
  nrf_ppi_channel_t ch_set, ch_clr;
  APP_ERROR_CHECK(nrfx_ppi_channel_alloc(&amp;amp;ch_set));
  APP_ERROR_CHECK(nrfx_ppi_channel_alloc(&amp;amp;ch_clr));
  APP_ERROR_CHECK(sd_ble_opt_set(BLE_COMMON_OPT_PA_LNA,
                                 &amp;amp;(ble_opt_t) {
                                   .common_opt = {
                                     .pa_lna = {
                                       .pa_cfg = {
                                         .enable = 1,
                                         .active_high = 1,
                                         .gpio_pin = s_util.radio_gpios.pa_gpio-&amp;gt;pin,
                                       },
                                       .lna_cfg = {
                                         .enable = 1,
                                         .active_high = 1,
                                         .gpio_pin = s_util.radio_gpios.lna_gpio-&amp;gt;pin,
                                       },
                                       .ppi_ch_id_set = ch_set,
                                       .ppi_ch_id_clr = ch_clr,
                                       .gpiote_ch_id = PA_LNA_GPIOTE_CHANNEL_ID,
                                     },
                                   },
                                 }));

  nrf_gpiote_task_configure(RADIO_NOTIFICATION_GPIOTE_CHANNEL_ID,
                            s_util.radio_gpios.notification_gpio-&amp;gt;pin,
                            (nrf_gpiote_polarity_t)GPIOTE_CONFIG_POLARITY_None,
                            NRF_GPIOTE_INITIAL_VALUE_LOW);

  APP_ERROR_CHECK(nrfx_ppi_channel_fork_assign(
    ch_set,
    nrf_gpiote_task_addr_get(
      nrf_gpiote_set_task_get(RADIO_NOTIFICATION_GPIOTE_CHANNEL_ID))));

  APP_ERROR_CHECK(nrfx_ppi_channel_fork_assign(
    ch_clr,
    nrf_gpiote_task_addr_get(
      nrf_gpiote_clr_task_get(RADIO_NOTIFICATION_GPIOTE_CHANNEL_ID))));
}

void ble_radio_notification_enable(void) {
  if (s_util.enabled) {
    return;
  }
  s_util.enabled = true;
  APP_ERROR_CHECK_BOOL(s_util.pa_lna_enabled);
  nrf_gpiote_task_enable(RADIO_NOTIFICATION_GPIOTE_CHANNEL_ID);
}

void ble_radio_notification_disable(void) {
  if (!s_util.enabled) {
    return;
  }
  s_util.enabled = false;
  APP_ERROR_CHECK_BOOL(s_util.pa_lna_enabled);
  nrf_gpiote_task_disable(RADIO_NOTIFICATION_GPIOTE_CHANNEL_ID);
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;This code is doing two things-&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;1. It&amp;#39;s loading the desired PA/LNA pin behavior into SoftDevice using channel 0 (PA_LNA_GPIOTE_CHANNEL_ID)&amp;nbsp;&lt;/p&gt;
&lt;p&gt;2. It&amp;#39;s forking the set and clear events into a new task using channel 1 (RADIO_NOTIFICATION_GPIOTE_CHANNEL_ID)&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;This way the PA set event sets my WiFi coex pin, and the LNA clear event clears my WiFi coex pin.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t have the WiFi coex pin set up to trigger an interrupt, because my nRF52840 firmware doesn&amp;#39;t need to react to it; I just need the PA + LNA tasks to also set and clear this WiFi coex pin.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Note that my &amp;quot;disable&amp;quot; call just calls nrf_gpiote_task_disable here- I&amp;#39;m not ever explicitly clearing the PA/LNA events. Is it possible that a poorly-timed call to my ble_radio_notification_disable function is leaving the event in a state where it&amp;#39;s preventing the nRF52840 from sleeping in sd_app_evt_wait()?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;In general, is this approach reasonable and sound?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks again for reading!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/394196?ContentTypeID=1</link><pubDate>Fri, 04 Nov 2022 14:21:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4e4b351b-5699-4b44-a864-b86b6b75dfbf</guid><dc:creator>charles_fi</dc:creator><description>&lt;p&gt;Thanks for the response, I appreciate it. Unfortunately this only happens sometimes, and I can&amp;#39;t get it to happen at my bench, so I can&amp;#39;t use local setups like the Power Profiler Kit.&lt;/p&gt;
&lt;p&gt;I do have real-time battery statistics I can watch that we collect for this purpose, though, and this issue is happening very rarely.&lt;/p&gt;
&lt;p&gt;Since writing this post, I reverse-engineered the SoftDevice binary, and disassembled the functions on the path from the SVCall ISR into the implementation of sd_app_evt_wait(), I see that it loops back into the wfe call after every softdevice interrupt and only returns when a non-softdevice interrupt happens. So, I don&amp;#39;t think it&amp;#39;s SoftDevice&amp;#39;s fault here!&lt;/p&gt;
&lt;p&gt;I am curious what you mean by&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;quot;&lt;span&gt;I think what important here is that if you have an interrupt that trigger an event you clear the event after you handle the interrupt. If not the CPU won&amp;#39;t be able to enter sleep mode if the event has not been cleared.&amp;quot;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Do you meant the PPI event here? I am using the PPI task/event system to tie the PA + LNA pins to a WiFi coex pin on a separate chip on our board. Could I ask you for more information about &amp;quot;clear the event&amp;quot; please?&lt;/p&gt;
&lt;p&gt;Thank you again,&lt;/p&gt;
&lt;p&gt;Charles&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/394180?ContentTypeID=1</link><pubDate>Fri, 04 Nov 2022 13:57:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:890fe034-dfe6-4f35-86dd-620c98a37dbc</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Charles,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The main purpose of PPI is to not get the CPU involved in the automation of event and task. So PPI wouldn&amp;#39;t wake the CPU up. But the event/interrupt can.&lt;br /&gt;I think what important here is that if you have an interrupt that trigger and even tyou clear the event after you handle the interrupt. If not the CPU won&amp;#39;t be able to enter sleep mode if the event has not been cleared.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Have you tried to disable the GPIO interrupt to check if it has something to do with waking up the CPU ?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;The softdevice only trigger an event when there is BLE activity that require the application to handle. So It&amp;#39;s not normal that the application get waking up on every&amp;nbsp;&lt;span&gt;sd_app_evt_wait(). Note that&amp;nbsp;sd_app_evt_wait() will not exist when the softdevice is active to handle BLE event. It only exist when there is application interrupt or when there is BLE event.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I would also suggest to use the Power Profiler Kit to monitor the activity of the CPU so that we have better view of when the CPU waking up or if the CPU enters sleep mode at all.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52840 app not sleeping, SoftDevice seems involved</title><link>https://devzone.nordicsemi.com/thread/393898?ContentTypeID=1</link><pubDate>Thu, 03 Nov 2022 13:33:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:12506ffc-d294-498b-8034-abb2d764f038</guid><dc:creator>charles_fi</dc:creator><description>&lt;p&gt;Ah, sorry, I&amp;#39;m going to give readers some whiplash here-&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I think my hypothesis was incorrect! If I&amp;#39;m seeing an almost exactly 1:1 relationship between SVCALL_IRQn calls and main loop turns and sleep calls, that means that things are working ok because sd_app_evt_wait() is run via a SVCALL.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;So I want to explore a different track, perhaps it&amp;#39;s a GPIO issue. I see in section 6.9 of the nRF52840 Product Specification:&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/Screen-Shot-2022_2D00_11_2D00_03-at-9.26.20-AM.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I see that &amp;quot;Wake-up from high or low level triggers on all pins&amp;quot; is a separate line item from &amp;quot;Trigger interrupt on state changes on any pin&amp;quot;.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Is it possible to configure the GPIO and/or GPIOTE subsystems so that the level of a pin will cause a wake-up, and/or keep the system from sleeping? We are using PPI to drive a wifi/ble coexistence pin and it&amp;#39;s tied to PA/LNA, does the PPI system have level-based wakes anywhere?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Note that my IRQ histogram shows that I am not getting GPIOTE_IRQn 100,000+ times per minute, so it&amp;#39;s not a strobing pin:&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/Screen-Shot-2022_2D00_11_2D00_03-at-9.35.14-AM.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;ll keep studying the data sheet, but if anyone has any information or quick answers about pin levels being able to keep the nRF52840 from entering WFE sleep, that would help me greatly&amp;nbsp;&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;&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Charles&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>