Page 1 of 1

FreeRTOS time anomalie?

Posted: Sat Aug 02, 2025 11:05 pm
by enGaryN
My program measures the exact time between two events (interrupts) with microsecond accuracy. Every 17.8 seconds the measurements are distorted by several hundred microseconds. Has anyone encountered such behavior of FreeRTOS?

Re: FreeRTOS time anomalie?

Posted: Sun Aug 03, 2025 7:45 am
by MicroController
At 240MHz, the 32 bit CPU cycle counter overflows every 17.89 seconds. Coincidence?

Re: FreeRTOS time anomalie?

Posted: Sun Aug 03, 2025 9:37 am
by dmitrij999
What kind of timer for microseconds checking you use?
If it's the hardware timer, then it could be interrupts priorities: systick timer may have the highest interrupt priority.
What's the original purpose to account microseconds?
For flow counter I used PCNT

Re: FreeRTOS time anomalie?

Posted: Sun Aug 03, 2025 1:10 pm
by enGaryN
At 240MHz, the 32 bit CPU cycle counter overflows every 17.89 seconds. Coincidence?

This is obvious. Common place

Re: FreeRTOS time anomalie?

Posted: Sun Aug 03, 2025 1:13 pm
by enGaryN
What kind of timer for microseconds checking you use?
If it's the hardware timer, then it could be interrupts priorities: systick timer may have the highest interrupt priority.
What's the original purpose to account microseconds?
For flow counter I used PCNT

I use esp_timer_get_time() function.
Original purpose is soft PLL for incoming periodic sequence.
Can PCNT provide microsecond (~10us) accuracy?

Re: FreeRTOS time anomalie?

Posted: Sun Aug 03, 2025 4:05 pm
by MicroController
Do you need to match only the frequency or also the phase?

Re: FreeRTOS time anomalie?

Posted: Sun Aug 03, 2025 4:15 pm
by enGaryN
Do you need to match only the frequency or also the phase?

Phase (the exact time beetwen local and remote periodic events) is a fundamentally important and is the main goal of measurements

Re: FreeRTOS time anomalie?

Posted: Sun Aug 03, 2025 5:51 pm
by dmitrij999
I use esp_timer_get_time() function.
That's the reason
esp_timer is driven the software way within FreeRTOS, so due to some hardware spinlocks FreeRTOS scheduler may be locked.
I used PCNT for pulse counting.
For your purpose i'd advise you to check with hardware timer, I think it's the high-accuracy timer so it allows to account microseconds.

Re: FreeRTOS time anomalie?

Posted: Sun Aug 03, 2025 7:06 pm
by enGaryN
I use esp_timer_get_time() function.
That's the reason.

You're right.
I found a simple intermediate solution:
using cpu_hal_get_cycle_count() function (paired with _set_) instead of esp_timer_get_time()

Thanks everyone!

UPD: hardware timer also works well