<?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>Unknown interrupt wakes chip immediately after calling sd_app_evt_wait()</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/76538/unknown-interrupt-wakes-chip-immediately-after-calling-sd_app_evt_wait</link><description>Hi Nordic team, 
 I&amp;#39;m struggling with an issue where my chip wakes immediately after calling sd_app_evt_wait() - specifically if I call app_timer_start() before the wait for event command. 
 I&amp;#39;d had this issue for a while which I managed by calling sd_app_evt_wait</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 12 Jul 2021 21:52:36 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/76538/unknown-interrupt-wakes-chip-immediately-after-calling-sd_app_evt_wait" /><item><title>RE: Unknown interrupt wakes chip immediately after calling sd_app_evt_wait()</title><link>https://devzone.nordicsemi.com/thread/319685?ContentTypeID=1</link><pubDate>Mon, 12 Jul 2021 21:52:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:aa444a22-c3f8-430e-a310-2d3963c23460</guid><dc:creator>K.Eranda</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;Marjeris,&lt;/p&gt;
&lt;p&gt;It was semi answered: I&amp;nbsp;confirmed that the app_timer_start does indeed interrupt and wake the system when it&amp;#39;s called before going to sleep - and this repeats every loop with the system never going to sleep in the code snipped in my original post.&amp;nbsp;I wasn&amp;#39;t able to find a solution to stop this/wait for this interrupt before going to sleep.&lt;/p&gt;
&lt;p&gt;I did however manage to find a better solution without&amp;nbsp;restructuring my&amp;nbsp;code: this was by putting a while loop wrapper around the sleep command with&amp;nbsp;it only being broken with a flag change. This flag change I perform in any interrupts where I want the system to wake. For any others ints, it remains in the while loop and hence goes&amp;nbsp;immediately back to sleep. Hopefully this method helps others with the same issue.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unknown interrupt wakes chip immediately after calling sd_app_evt_wait()</title><link>https://devzone.nordicsemi.com/thread/319415?ContentTypeID=1</link><pubDate>Fri, 09 Jul 2021 14:30:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:347b5be2-d011-4ff7-b91e-4ac3e719f9ce</guid><dc:creator>Marjeris Romero</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Sigurd is currently out on vacation so I have taken over this case. I will need some time to get my feet into this case. Could you briefly summarize what you are struggling with at the moment? Was your original question answered? In that case could you please open a new ticket?&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Marjeris&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unknown interrupt wakes chip immediately after calling sd_app_evt_wait()</title><link>https://devzone.nordicsemi.com/thread/318702?ContentTypeID=1</link><pubDate>Mon, 05 Jul 2021 20:37:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e833a4cc-e95c-4b14-b7f4-10ddc225cc70</guid><dc:creator>K.Eranda</dc:creator><description>&lt;p&gt;@&lt;a class="internal-link view-user-profile" href="https://devzone.nordicsemi.com/members/sigurdon"&gt;Sigurd&lt;/a&gt;&amp;nbsp;just following up on the&amp;nbsp;above. Wondering if&amp;nbsp;you or the team have had a chance to take a look.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unknown interrupt wakes chip immediately after calling sd_app_evt_wait()</title><link>https://devzone.nordicsemi.com/thread/316922?ContentTypeID=1</link><pubDate>Thu, 24 Jun 2021 09:41:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f92f2b74-7704-4e05-8ccc-ccde2c88b2b4</guid><dc:creator>K.Eranda</dc:creator><description>[quote userid="15146" url="~/f/nordic-q-a/76538/unknown-interrupt-wakes-chip-immediately-after-calling-sd_app_evt_wait/316510#316510"]Looks like when you call app_timer_stop()/&lt;span&gt;app_timer_start(),&amp;nbsp;a&amp;nbsp;RTC interrupt is triggered&lt;/span&gt;[/quote]
&lt;p&gt;That&amp;#39;s what I expected but couldn&amp;#39;t figure&amp;nbsp;out what the interrupt was&amp;nbsp;&amp;nbsp;- I assumed it was SWI0 based on how the app_timer is &lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v15.0.0%2Fgroup__app__timer.html"&gt;implemented&lt;/a&gt;: &amp;quot;When calling app_timer_start() or app_timer_stop(), the timer operation is just queued, and the software interrupt is triggered. The actual timer start/stop operation is executed by the SWI0 interrupt handler&amp;quot;. However&amp;nbsp;this wasn&amp;#39;t showing&amp;nbsp;a pending interrupt when I checked.&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The 2 questions I have then are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Is there a way to prevent this behavior without restructuring&amp;nbsp;the rest of my code?&lt;/li&gt;
&lt;li&gt;Perhaps waiting for the RTC timer of the app_timer_start to fire before calling the sleep/idle command - is there a flag&amp;nbsp;that is accessible from main.c that shows the &amp;quot;timer_req_process&amp;quot; -&amp;gt; &amp;quot;TIMER_REQ_START&amp;quot; has been fired/successfully created? (Else I can try to make an extern var in app_timer2.c where this interrupt handler is)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Many thanks&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unknown interrupt wakes chip immediately after calling sd_app_evt_wait()</title><link>https://devzone.nordicsemi.com/thread/316510?ContentTypeID=1</link><pubDate>Tue, 22 Jun 2021 14:07:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b7602977-2a63-488f-8bf1-a287ee2f4e38</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote user="K.Eranda"]Are you able to reproduce the issue?[/quote]
&lt;p&gt;Yes. I took a closer look. Looks like when you call app_timer_stop()/&lt;span&gt;app_timer_start(),&amp;nbsp;a&amp;nbsp;RTC interrupt is triggered, the request(start/stop) is handled in the RTC interrupt handler.&amp;nbsp;&lt;/span&gt;&lt;span&gt;timer_request_proc_trigger(start/stop) --- &amp;gt; interrupt triggers -&amp;gt; rtc_irq -&amp;gt; timer_req_process -&amp;gt; start/stop timer.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
[quote user="K.Eranda"]&lt;p&gt;&lt;span style="font-size:inherit;"&gt;Unfortunately in my application the timer must be started within the loop as the sleep duration can vary and in the full code, is calculated at the start of the loop. This calculation can&amp;#39;t be done in a timeout handler as the variables it uses wont have been updated yet. E.g. consider this example:&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;&lt;span style="font-size:inherit;"&gt;&lt;em&gt;1.Chip wakes from timeout_handler -&amp;gt; 2.Loop: reads sensor val -&amp;gt; 3.calculates when to wake next based on sensor value -&amp;gt; 4.starts timer -&amp;gt; 5.sleeps&lt;/em&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="font-size:inherit;"&gt;&lt;em&gt;&amp;nbsp;&lt;/em&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="font-size:inherit;"&gt;The code won&amp;#39;t know when to wake next during the timeout handler i.e. step 1&lt;/span&gt;&lt;/div&gt;[/quote]
&lt;p&gt;I assume this sensor does not have any GPIO interrupt line then.&lt;/p&gt;
&lt;p&gt;How about something like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;main
{
.
.
init code
.
.


    // first time pooling rate:
    app_timer_start(rtc_wake, APP_TIMER_TICKS(10), NULL); //wake up in 10ms to check if first sensor reading and variables are ready.

    for (;;)
    {
        idle_state_handle();
    }
}

-------

/**@brief Timeout handler for the rtc_wake timer
 */
static void rtc_wake_timer_handler(void * p_context)
{
     1. read sensor ?
     if sensor is not ready,  --&amp;gt; app_timer_start(m_single_shot_timer_id, APP_TIMER_TICKS(10), NULL); //wake up in 10ms again to check if sensor is ready
     if sensor is ready and variables it uses are updated --&amp;gt; app_timer_start(m_single_shot_timer_id, APP_TIMER_TICKS(sensor_reading), NULL) // wake up in x ms based on the variables / sensor readings?
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unknown interrupt wakes chip immediately after calling sd_app_evt_wait()</title><link>https://devzone.nordicsemi.com/thread/316355?ContentTypeID=1</link><pubDate>Mon, 21 Jun 2021 20:22:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:755b1286-7633-4947-a762-9816d575f384</guid><dc:creator>K.Eranda</dc:creator><description>&lt;p&gt;&lt;span style="font-size:inherit;"&gt;Hi Sigurd, Thanks for the quick response.&lt;/span&gt;&lt;/p&gt;
[quote userid="15146" url="~/f/nordic-q-a/76538/unknown-interrupt-wakes-chip-immediately-after-calling-sd_app_evt_wait/316348#316348"]The code you attached is now printing &amp;quot;Sleep&amp;quot; each time an interrupt occurs. You should instead print that in your &amp;quot;rtc_wake&amp;quot; app_timer callback/timeout function[/quote]
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;The print function I use is&amp;nbsp;exactly that - an indicator to see when any interrupt occurs &amp;amp; the system wakes.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;&lt;/span&gt;&lt;/p&gt;
[quote userid="15146" url="~/f/nordic-q-a/76538/unknown-interrupt-wakes-chip-immediately-after-calling-sd_app_evt_wait/316348#316348"]in the end of your timeout function, you start the timer again with&amp;nbsp;app_timer_start().[/quote]
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;Unfortunately in my application the timer must be started within the loop as the sleep duration can vary and in the full code, is calculated at the start of the loop. This calculation can&amp;#39;t be done in a timeout handler as the variables it uses wont have been updated yet. E.g. consider this example:&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;&lt;span style="font-size:inherit;"&gt;&lt;em&gt;1.Chip wakes from timeout_handler -&amp;gt; 2.Loop: reads sensor val -&amp;gt; 3.calculates when to wake next based on sensor value -&amp;gt; 4.starts timer -&amp;gt; 5.sleeps&lt;/em&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="font-size:inherit;"&gt;&lt;em&gt;&amp;nbsp;&lt;/em&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="font-size:inherit;"&gt;The code won&amp;#39;t know when to wake next during the timeout handler i.e. step 1.&amp;nbsp;&lt;/span&gt;Apologies, I may have simplified the code in my first post too much and this wasn&amp;#39;t clear.&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;[quote userid="15146" url="~/f/nordic-q-a/76538/unknown-interrupt-wakes-chip-immediately-after-calling-sd_app_evt_wait/316348#316348"]Printing&amp;nbsp;as shown in the main-loop snippet you attached, could&amp;nbsp;if you are using UART for the printing, trigger e.g. the UART &lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/uarte.html#concept_a54_2gm_wr"&gt;ENDTX event/interrupt&lt;/a&gt;, and cause an &amp;quot;infinite printing-loop&amp;quot;.[/quote]&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;The printing via UART was just an easy indicator I used - &amp;amp; doesn&amp;#39;t seem to be the issue. For example, using an LED toggle instead&amp;nbsp;still shows the issue persists. This is also seen when keeping the print command but removing the app_timer_stop command I aforementioned in my 1st post - the system sleeps in the 2nd loop despite the print command.&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;[quote userid="15146" url="~/f/nordic-q-a/76538/unknown-interrupt-wakes-chip-immediately-after-calling-sd_app_evt_wait/316348#316348"]I recommend using the power management module for sleeping.[/quote]&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;Thanks for the snippets -&amp;nbsp;I&amp;nbsp;tried sleeping via the power management module as suggested but the issue still persists.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;I have a feeling the problem may be with app_timer_start(). Are you able to reproduce the issue?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;Thanks in&amp;nbsp;advance.&lt;/span&gt;&lt;/p&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: Unknown interrupt wakes chip immediately after calling sd_app_evt_wait()</title><link>https://devzone.nordicsemi.com/thread/316348?ContentTypeID=1</link><pubDate>Mon, 21 Jun 2021 18:25:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c3f93d04-ca4c-4440-9f59-3dc32a875571</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote user=""]Ideally the system should wake via app_timer interrupt every 10s and print &amp;quot;Sleep&amp;quot;. [/quote]
&lt;p&gt;The code you attached is now printing &amp;quot;Sleep&amp;quot; each time an interrupt occurs. You should instead print that in your &amp;quot;rtc_wake&amp;quot; app_timer callback/timeout function, specified when you created the timer with&amp;nbsp;app_timer_create().&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;ret_code_t app_timer_create(app_timer_id_t const *      p_timer_id,
                            app_timer_mode_t            mode,
                            app_timer_timeout_handler_t timeout_handler);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;in the end of your timeout function, you start the timer again with&amp;nbsp;app_timer_start().&lt;/p&gt;
&lt;p&gt;Printing&amp;nbsp;as shown in the main-loop snippet you attached, could&amp;nbsp;if you are using UART for the printing, trigger e.g. the UART &lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/uarte.html#concept_a54_2gm_wr"&gt;ENDTX event/interrupt&lt;/a&gt;, and cause an &amp;quot;infinite printing-loop&amp;quot;.&lt;/p&gt;
&lt;p&gt;I recommend using the power management module for sleeping.&lt;/p&gt;
&lt;p&gt;Snippets:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;    // Enter main loop.
    for (;;)
    {
       idle_state_handle();
    }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/**@brief Function for handling the idle state (main loop).
 *
 * @details If there is no pending log operation, then sleep until next the next event occurs.
 */
static void idle_state_handle(void)
{
    if (NRF_LOG_PROCESS() == false)
    {
        nrf_pwr_mgmt_run();
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void nrf_pwr_mgmt_run(void)
{
    PWR_MGMT_FPU_SLEEP_PREPARE();
    PWR_MGMT_SLEEP_LOCK_ACQUIRE();
    PWR_MGMT_CPU_USAGE_MONITOR_SECTION_ENTER();
    PWR_MGMT_DEBUG_PIN_SET();

    // Wait for an event.
#ifdef SOFTDEVICE_PRESENT
    if (nrf_sdh_is_enabled())
    {
        ret_code_t ret_code = sd_app_evt_wait();
        ASSERT((ret_code == NRF_SUCCESS) || (ret_code == NRF_ERROR_SOFTDEVICE_NOT_ENABLED));
        UNUSED_VARIABLE(ret_code);
    }
    else
#endif // SOFTDEVICE_PRESENT
    {
        // Wait for an event.
        __WFE();
        // Clear the internal event register.
        __SEV();
        __WFE();
    }

    PWR_MGMT_DEBUG_PIN_CLEAR();
    PWR_MGMT_CPU_USAGE_MONITOR_SECTION_EXIT();
    PWR_MGMT_SLEEP_LOCK_RELEASE();
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;With the&amp;nbsp;&lt;span&gt;power management you can e.g. set&amp;nbsp;NRF_PWR_MGMT_CONFIG_DEBUG_PIN_ENABLED /&amp;nbsp;NRF_PWR_MGMT_SLEEP_DEBUG_PIN (in sdk_config.h), to monitor how long your application/CPU is sleeping.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>