<?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>app_timer2 library new bug</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/97302/app_timer2-library-new-bug</link><description>Hi, 
 I recently discover a new bug in the app_timer2 lib of the lastest SDK (17.1.0) using the softdevice s132_7.2.0 
 Here are the steps to reproduce the bug: 
 1- create a repeated timer in main (). 
 
 2- start the timer with 2000ms of interval. </description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 08 Mar 2023 13:05:22 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/97302/app_timer2-library-new-bug" /><item><title>RE: app_timer2 library new bug</title><link>https://devzone.nordicsemi.com/thread/414135?ContentTypeID=1</link><pubDate>Wed, 08 Mar 2023 13:05:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f72e366b-2de1-4b0b-832b-b821cb161272</guid><dc:creator>Hieu</dc:creator><description>&lt;p&gt;Hi CHATAR,&lt;/p&gt;
&lt;p&gt;I think I found the &amp;quot;bug.&amp;quot; It lies in the timer_expire() function of app_timer2.c.&lt;/p&gt;
&lt;p&gt;The details of the issue are provided below, but first are two alternatives you can consider:&lt;/p&gt;
&lt;p&gt;- Use APP_TIMER_MODE_SINGLE_SHOT and always restart the timer in the handler&lt;br /&gt;- Defer the call to app_timer_start() out of the handler. This can be done, for example, with a boolean flag.&lt;/p&gt;
&lt;p&gt;Next, I would like to explain the technical details:&lt;/p&gt;
&lt;p&gt;Here is a snippet of code from the timer_expire() function. I marked the critical lines at the beginning of them:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;    #if APP_TIMER_CONFIG_USE_SCHEDULER
            app_timer_event_t timer_event;

            timer_event.timeout_handler = p_timer-&amp;gt;handler;
            timer_event.p_context       = p_timer-&amp;gt;p_context;
            uint32_t err_code = app_sched_event_put(&amp;amp;timer_event,
                                                    sizeof(timer_event),
                                                    scheduled_timeout_handler);
            APP_ERROR_CHECK(err_code);
    #else
            NRF_LOG_DEBUG(&amp;quot;Timer expired (context: %d)&amp;quot;, (uint32_t)p_timer-&amp;gt;p_context)
/**/        p_timer-&amp;gt;handler(p_timer-&amp;gt;p_context);
    #endif
            CRITICAL_REGION_ENTER();
            /* check active flag as it may have been stopped in the user handler */
            if (p_timer-&amp;gt;repeat_period &amp;amp;&amp;amp; !APP_TIMER_IS_IDLE(p_timer))
            {
/**/            p_timer-&amp;gt;end_val += p_timer-&amp;gt;repeat_period;
                cont = true;
            }
            else
            {
                cont = false;
            }
            CRITICAL_REGION_EXIT();&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s keep in mind that: In the library, a timer is set as inactive by having the end value field set to an invalid value.&lt;/p&gt;
&lt;p&gt;In your use case, each time the timer expires, the following things happen:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;app_timer_stop() is called, stopping the timer. end_val is set to&amp;nbsp;APP_TIMER_IDLE_VAL.&lt;/li&gt;
&lt;li&gt;app_timer_start() is called. end_val is set to &amp;lt;now&amp;gt; + repeat_period.&lt;/li&gt;
&lt;li&gt;In timer_expire(), after the application handler,&amp;nbsp;the driver check:
&lt;ul&gt;
&lt;li&gt;If the timer is in repeat mode (repeat_period != 0) and the timer is not stopped (end_val !=&amp;nbsp;&lt;span&gt;APP_TIMER_IDLE_VAL)&lt;/span&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span&gt;end_val is set to &amp;lt;now&amp;gt; + repeat_period&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If app_timer_start() is not called in the handler, then the timer_expire() logic above would&amp;nbsp;be correct.&lt;/p&gt;
&lt;p&gt;However, in your use case, app_timer_start() is used and thus the logic causes repeat_period to be added to end_val twice.&lt;/p&gt;
&lt;p&gt;Whether or not this is a &amp;quot;bug&amp;quot; depends on whether or not the design is expected to handle&amp;nbsp;app_timer_start() in the handler. I&amp;nbsp;can imagine your use case being logical, so I may agree this is a bug.&lt;/p&gt;
&lt;p&gt;Nonetheless, both the workaround I propose above should work, so unfortunately this issue will not be fixed.&lt;/p&gt;
&lt;p&gt;I am sorry for the inconvenience. If the workarounds&amp;nbsp;don&amp;#39;t work for you for any reasons, please let me know.&lt;/p&gt;
&lt;p&gt;Hieu&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: app_timer2 library new bug</title><link>https://devzone.nordicsemi.com/thread/413771?ContentTypeID=1</link><pubDate>Tue, 07 Mar 2023 10:29:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:04eebd56-45f3-4e5c-9198-547f92925a9c</guid><dc:creator>CHATAR</dc:creator><description>&lt;p&gt;Hi Hieu,&lt;/p&gt;
&lt;p&gt;this is just an example to show you the bug, in our program we have no choice but to use repeated mode and stop in the inerrupt in our complex behavior is mandatory, believe me. So in the previous version of sdk (17.0.2) the lib was working fine but now I have this bug.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: app_timer2 library new bug</title><link>https://devzone.nordicsemi.com/thread/413487?ContentTypeID=1</link><pubDate>Mon, 06 Mar 2023 11:23:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1759e69e-90bd-4b1d-9cd6-d403607f8a64</guid><dc:creator>Hieu</dc:creator><description>&lt;p&gt;Hi CHATAR,&lt;/p&gt;
&lt;p&gt;Let me try to reproduce and debug this and I will get back to you soon.&lt;/p&gt;
&lt;p&gt;Meanwhile, may I ask why you start the timer in repeat mode if you would stop and restart it in the handler?&lt;/p&gt;
&lt;p&gt;Also, you might already know this, but the app timers are not the actual TIMER peripheral, so you might want to avoid naming the instance and the handler in that way. It could be confusing.&lt;/p&gt;
&lt;p&gt;Hieu&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>