Skip to main content
Graduate II
May 4, 2022
Question

What happens when the 32bit HAL uwTick timer variable overflows after 49 days?

  • May 4, 2022
  • 4 replies
  • 1990 views

I'm looking to use HAL_GetTick to track how many ms have lapsed. Unfortunately, it uses a 32bit uwTick variable which will overflow in 49 days. My device may be on more than 49 days, so I was planning on adding a uint64_t ms_passed and increment it at the same place where HAL_IncTick() for my TIM7's callback freeRTOS setup. Am I doing this right?

    This topic has been closed for replies.

    4 replies

    Super User
    May 4, 2022

    Sounds good.

    KnarfB

    Graduate II
    May 4, 2022

    There is nothing wrong with Hal tick counter overflow. You can reed more info from Nick Gammon:

    https://www.gammon.com.au/millis

    Super User
    May 4, 2022

    your reference says:

    "If you are using millis() you cannot time more than 49.71 days, ..., without extra code to detect the wrap-around."

    But that's what the OP wants.

    hth

    KnarfB

    Graduate II
    May 4, 2022

    May be. His question didn't seem so clear to me. Maybe he's just worried what happen when tick counter overflow like in "title question". But his suggested solution - adding another 64bit tick counter must be handled with care. He must read that 64bit value atomic way (reading must not be interrupted by IRQ routine where is that counter incremented).

    Graduate II
    May 4, 2022

    What actual resolution do you need?

    If you counted every 1000 ms in the SysTick_Handler, you could have a second counter good to 49710 days (136 years). In other words keep all the HAL_GetTick dependencies, and have a long-count

    The millisecond tick is design for relatively short timeout duration, and can handle the 49 day wrap with those using appropriate unsigned math.

    For Time/Date stuff I've historically used the Windows 64-bit FILETIME, and convert that to SYSTEMTIME when needed, that's good for 30,000 years, and I think I validated my leap year stuff out about 3,000

    LMorr.3Author
    Graduate II
    May 4, 2022

    I now see how the unsigned wrap will not be an issue. Thanks for that info.

    I don't need 1 beat to last 49 days, so from what I understand, I can just use a 32bit counter in SysTick_Handler, or re-use the 32bit sytem counter uwTick since I now understand how the wrap will not be an issue with my timestamp tracking.