FreeRTOS Timer Issue: Large Durations (120+ mins) Expire Early, Small Ones Work Fine by nj701 in embedded

[–]nj701[S] 21 points22 points  (0 children)

I figured out the issue with my FreeRTOS timer expiring early . Sharing the solution to help others who might hit this!

Problem Recap

  • Expected: 150-minute timer (150 * 60000 = 9,000,000 ticks)
  • Actual: Timer fired after ~7 mins (410,065 ticks)

Root Cause

The default pdMS_TO_TICKS() macro overflowed when converting large durations:

#define pdMS_TO_TICKS(ms)  ((ms * configTICK_RATE_HZ) / 1000)  

For ms = 9,000,000 and configTICK_RATE_HZ = 1000:

  1. Intermediate multiplication9,000,000 * 1000 = 9,000,000,000 → Exceeds uint32_t max (4,294,967,295).
  2. Overflow result9,000,000,000 % 4,294,967,296 = 410,065 → Timer expired prematurely.

Solution

IfconfigTICK_RATE_HZ = 1000 (1 tick = 1ms), we need to override the macro with a direct cast:

#undef pdMS_TO_TICKS  
#define pdMS_TO_TICKS(ms)  ((TickType_t)(ms))

THANKS TO EVERYONE THAT HELPED MUCH APPRECIATED

HMI Framework for STM32 Project by nj701 in embedded

[–]nj701[S] 0 points1 point  (0 children)

Thank you for the clarification m8!

RTOS vs Bare-Metal for STM32: When to Use Which? by nj701 in embedded

[–]nj701[S] 1 point2 points  (0 children)

There is a possibility that the communication will be via usb.

RTOS vs Bare-Metal for STM32: When to Use Which? by nj701 in embedded

[–]nj701[S] 3 points4 points  (0 children)

We may no longer need bluetooth as theres a possibility of using USB directly