FreeRTOS time anomalie?

enGaryN
Posts: 8
Joined: Mon Apr 29, 2024 6:05 pm

FreeRTOS time anomalie?

Postby enGaryN » Sat Aug 02, 2025 11:05 pm

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?

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: FreeRTOS time anomalie?

Postby MicroController » Sun Aug 03, 2025 7:45 am

At 240MHz, the 32 bit CPU cycle counter overflows every 17.89 seconds. Coincidence?

dmitrij999
Posts: 100
Joined: Sat Mar 02, 2019 8:06 pm

Re: FreeRTOS time anomalie?

Postby dmitrij999 » Sun Aug 03, 2025 9:37 am

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

enGaryN
Posts: 8
Joined: Mon Apr 29, 2024 6:05 pm

Re: FreeRTOS time anomalie?

Postby enGaryN » Sun Aug 03, 2025 1:10 pm

At 240MHz, the 32 bit CPU cycle counter overflows every 17.89 seconds. Coincidence?

This is obvious. Common place
Last edited by enGaryN on Sun Aug 03, 2025 1:15 pm, edited 1 time in total.

enGaryN
Posts: 8
Joined: Mon Apr 29, 2024 6:05 pm

Re: FreeRTOS time anomalie?

Postby enGaryN » Sun Aug 03, 2025 1:13 pm

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?

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: FreeRTOS time anomalie?

Postby MicroController » Sun Aug 03, 2025 4:05 pm

Do you need to match only the frequency or also the phase?

enGaryN
Posts: 8
Joined: Mon Apr 29, 2024 6:05 pm

Re: FreeRTOS time anomalie?

Postby enGaryN » Sun Aug 03, 2025 4:15 pm

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

dmitrij999
Posts: 100
Joined: Sat Mar 02, 2019 8:06 pm

Re: FreeRTOS time anomalie?

Postby dmitrij999 » Sun Aug 03, 2025 5:51 pm

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.

enGaryN
Posts: 8
Joined: Mon Apr 29, 2024 6:05 pm

Re: FreeRTOS time anomalie?

Postby enGaryN » Sun Aug 03, 2025 7:06 pm

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

Who is online

Users browsing this forum: Amazon [Bot], Applebot, ChatGPT-User and 7 guests