<?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>Replacing nrf_delay_ms with app_timer doesn&amp;#39;t work</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/51603/replacing-nrf_delay_ms-with-app_timer-doesn-t-work</link><description>Hi I managed to successfully implement a busy-wait using nrf_delay_ms(). However, I read that nrf_delay_ms() uses more power than app_timer&amp;#39;s RTC. So I am trying to use app_timer to do a busy_wait for 10 seconds before calling the rest of the code after</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 02 Sep 2019 08:56:47 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/51603/replacing-nrf_delay_ms-with-app_timer-doesn-t-work" /><item><title>RE: Replacing nrf_delay_ms with app_timer doesn't work</title><link>https://devzone.nordicsemi.com/thread/207312?ContentTypeID=1</link><pubDate>Mon, 02 Sep 2019 08:56:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:26fc033a-8b60-4656-ac1d-4f16f411d4b7</guid><dc:creator>AndreasF</dc:creator><description>&lt;p&gt;Hi.&lt;/p&gt;
&lt;p&gt;I think the issue is that you have to remember that your main-thread is not a loop.&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/618x734/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-de8a3e0570bb4d4fb749ae6614b1912e/pastedimage1567413505516v1.png" /&gt;&lt;/p&gt;
&lt;p&gt;So when you run this code, log and timers are initialized, the timer starts and so on. This procedure of the code is executed rapidly and then you run into your &lt;strong&gt;if-statement&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;By this point, since the code so far has been executed rapidly the &lt;strong&gt;notification_timeout_handler()&lt;/strong&gt; is yet to be called even &lt;strong&gt;&lt;/strong&gt;once, and &lt;strong&gt;m_custom_value&lt;/strong&gt; is 0. So the &lt;strong&gt;if-statement&lt;/strong&gt; is not entered and you continue passed the &lt;strong&gt;if-statement&lt;/strong&gt; into the &lt;strong&gt;for-loop&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The key point is, you will never go back and check the&lt;strong&gt; if-statement&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m guessing it will work if you put the &lt;strong&gt;if-statement&amp;nbsp;&lt;/strong&gt;in the&amp;nbsp;&lt;strong&gt;for-loop&lt;/strong&gt;, it will however look kind of messy.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Andreas&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Replacing nrf_delay_ms with app_timer doesn't work</title><link>https://devzone.nordicsemi.com/thread/207291?ContentTypeID=1</link><pubDate>Mon, 02 Sep 2019 07:35:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:858d5cc9-9ab0-46d9-a519-de1b8ff0f2fb</guid><dc:creator>thoric_fish</dc:creator><description>&lt;p&gt;Hi Andreas,&lt;/p&gt;
&lt;p&gt;Thank you for your help, the code you provided worked! But I still do not know why I cannot use the counter variable &amp;#39;m_custom_value&amp;#39; to create a 10 second delay? If &amp;#39;m_custom_value&amp;#39; is incremented every second by the notification_timeout_handler() called when the repeated &amp;#39;m_notification_timer_id&amp;#39; timer expires, then&amp;nbsp;&lt;span&gt;&amp;#39;m_custom_value&amp;#39; should increase to 10 after 10 seconds.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Shouldn&amp;#39;t I be able to use conditional while or if statement&amp;nbsp; to only call&amp;nbsp;services_init() and the other functions in main() after &amp;#39;m_custom_value&amp;#39; &amp;gt; 10?&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define NOTIFICATION_INTERVAL           APP_TIMER_TICKS(1000)     

APP_TIMER_DEF(m_notification_timer_id);

static uint8_t m_custom_value = 0;

static void notification_timeout_handler(void * p_context)
{
    UNUSED_PARAMETER(p_context);
    ret_code_t err_code;
    
    // Increment the value of m_custom_value before nortifing it.
    m_custom_value++;
}


static void timers_init(void)
{
    err_code = app_timer_create(&amp;amp;m_notification_timer_id, APP_TIMER_MODE_REPEATED, notification_timeout_handler);
    APP_ERROR_CHECK(err_code);
}



static void application_timers_start(void)
{
       ret_code_t err_code;
       err_code = app_timer_start(m_notification_timer_id, NOTIFICATION_INTERVAL, NULL);
       APP_ERROR_CHECK(err_code);
}

int main(void)
{
    bool erase_bonds;

    // Initialize.
    log_init();
    timers_init();
    application_timers_start();
    buttons_leds_init(&amp;amp;erase_bonds);
    power_management_init();
    ble_stack_init();
    gap_params_init();
    gatt_init();
    peer_manager_init();
    
    //nrf_delay_ms(10000);
    if(m_custom_value &amp;gt;= 5){
        services_init();
        advertising_init();
        conn_params_init();
  
        NRF_LOG_INFO(&amp;quot;Template example started.&amp;quot;);
        advertising_start(erase_bonds);
      }

    
    // Enter main loop.
    for (;;)
    {
        idle_state_handle();
    }
    
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Replacing nrf_delay_ms with app_timer doesn't work</title><link>https://devzone.nordicsemi.com/thread/207070?ContentTypeID=1</link><pubDate>Fri, 30 Aug 2019 08:56:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:67222200-89f7-4998-8122-d08ac53f0ffe</guid><dc:creator>AndreasF</dc:creator><description>&lt;p&gt;Hi.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not quite sure what you&amp;#39;re trying to do here:&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/614x234/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-de8a3e0570bb4d4fb749ae6614b1912e/pastedimage1567154609713v1.png" /&gt;&lt;/p&gt;
&lt;p&gt;If you want to create a 10 second delay you should use the single shot mode for the app timer.&lt;/p&gt;
&lt;p&gt;You can see how it&amp;#39;s implemented in this &lt;a href="https://devzone.nordicsemi.com/nordic/short-range-guides/b/software-development-kit/posts/application-timer-tutorial"&gt;tutorial&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Just scroll down to &amp;quot;&lt;strong&gt;Create a single shot timer&lt;/strong&gt;&amp;quot;.&lt;/p&gt;
&lt;p&gt;If you create a while loop that sleeps while waiting for 10 seconds to pass, and then exit the while loop it will be very power friendly. Perhaps something like this could be an idea:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define NOTIFICATION_INTERVAL           APP_TIMER_TICKS(10000)  // 10 seconds interval
static uint8_t wait_flag = 0;

static void timers_init(void)
{
    // Initialize timer module.
    ret_code_t err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);

    // Create timers.
    err_code = app_timer_create(&amp;amp;m_notification_timer_id, APP_TIMER_MODE_SINGLE_SHOT, single_shot_timer_handler);
    APP_ERROR_CHECK(err_code);
}

/**@brief Function for starting timers.
 */
static void application_timers_start(void)
{
       ret_code_t err_code;
       err_code = app_timer_start(m_notification_timer_id, NOTIFICATION_INTERVAL, NULL);
       APP_ERROR_CHECK(err_code);
}


/**@brief Timeout handler for the single shot timer.
 */
static void single_shot_timer_handler(void * p_context)
{
    wait_flag = 1;
}

/**@brief Function for application main entry.
 */
int main(void)
{
    bool erase_bonds;

    // Initialize.
    log_init();
    timers_init();
    application_timers_start();
    buttons_leds_init(&amp;amp;erase_bonds);
    power_management_init();
    ble_stack_init();
    gap_params_init();
    gatt_init();
    peer_manager_init();
    
    // Sleep for 10 seconds
    while(wait_flag == 0)
    {
        idle_state_handle();
    }
    
    services_init();
    advertising_init();
    conn_params_init();
  
    NRF_LOG_INFO(&amp;quot;Template example started.&amp;quot;);
    advertising_start(erase_bonds);
    
    // Enter main loop.
    for (;;)
    {
        idle_state_handle();
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I hope this is helpful.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Andreas&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>