This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

APP_TIMER_TICKS inverse calculation

Hello,

I would like to know the number on ms that a timer has been running before stopping in a breakpoint while debugging.

I can know the number of ticks with the following function: app_timer_cnt_get().

And the APP_TIMER_TICKS returns the number of ticks if you know the ms, but I want the opposite operation: know the time (in ms) with the number of ticks.

Thank you

Parents
  • Hi 

    The APP_TIMER_TICKS macro is defined in app_timer.h like this:

    #define APP_TIMER_TICKS(MS)                                \
                ((uint32_t)ROUNDED_DIV(                        \
                (MS) * (uint64_t)APP_TIMER_CLOCK_FREQ,         \
                1000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1)))

    The opposite macro doesn't exist, but it should be possible to make one like this:

    #define APP_TIMER_MS(TICKS)                                \
                ((uint32_t)ROUNDED_DIV(                        \
                (TICKS) * 1000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1),\
                (uint64_t)APP_TIMER_CLOCK_FREQ))

    Essentially you just have to switch around the divisor and dividend components, so that you multiply with 1000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1), and divide by (uint64_t)APP_TIMER_CLOCK_FREQ. 

    I haven't tested it myself, so use at your own peril ;)

    Best regards
    Torbjørn

Reply
  • Hi 

    The APP_TIMER_TICKS macro is defined in app_timer.h like this:

    #define APP_TIMER_TICKS(MS)                                \
                ((uint32_t)ROUNDED_DIV(                        \
                (MS) * (uint64_t)APP_TIMER_CLOCK_FREQ,         \
                1000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1)))

    The opposite macro doesn't exist, but it should be possible to make one like this:

    #define APP_TIMER_MS(TICKS)                                \
                ((uint32_t)ROUNDED_DIV(                        \
                (TICKS) * 1000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1),\
                (uint64_t)APP_TIMER_CLOCK_FREQ))

    Essentially you just have to switch around the divisor and dividend components, so that you multiply with 1000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1), and divide by (uint64_t)APP_TIMER_CLOCK_FREQ. 

    I haven't tested it myself, so use at your own peril ;)

    Best regards
    Torbjørn

Children
No Data
Related