How can I get more time from the millis function?

Hi,

I created a function like Arduino's millis which returns the number of milliseconds elapsed since the micro started executing the current program. Look down:

uint32_t millis(void)
{
    return (app_timer_cnt_get() / APP_TIMER_TICKS(1));
}

The problem is that the timer overflows every 17 minutes or so. The Arduino millis function instead overflows every 50 days.
Is there a way to increase the overflow time of my millis function?

Thank you

Parents
  • Hi,

    If you are using a recent SDK and the app_timer implementation is app_timer2.c, then this is quite straight-forward, but you need to do a small change in the app_timer. The app_timer2 implementation use a 64 bit counter internally, so what you could do is to make get_now() non-static, and add that to  app_timer.h as shown in this diff:

    diff --git a/components/libraries/timer/app_timer.h b/components/libraries/timer/app_timer.h
    index cd5b6d2..7d7b854 100644
    --- a/components/libraries/timer/app_timer.h
    +++ b/components/libraries/timer/app_timer.h
    @@ -311,6 +311,11 @@ void app_timer_pause(void);
      */
     void app_timer_resume(void);
     
    +/**
    + * @brief Return current 64 bit timestamp
    + */
    +uint64_t get_now(void);
    +
     #ifdef __cplusplus
     }
     #endif
    diff --git a/components/libraries/timer/app_timer2.c b/components/libraries/timer/app_timer2.c
    index dfafbcb..cfa28f7 100644
    --- a/components/libraries/timer/app_timer2.c
    +++ b/components/libraries/timer/app_timer2.c
    @@ -113,7 +113,7 @@ NRF_SORTLIST_DEF(m_app_timer_sortlist, compare_func); /**< Sortlist used for sto
     /**
      * @brief Return current 64 bit timestamp
      */
    -static uint64_t get_now(void)
    +uint64_t get_now(void)
     {
         uint64_t now = m_base_counter + drv_rtc_counter_get(&m_rtc_inst);
     
    

  • Thanks for the answer but I don't understand what get_now returns. Milliseconds like millis?
    How do I get milliseconds?

  • I'm using a NINA-B3 module where the nRF52840 microcontroller is present.

Reply Children
No Data
Related