Fastest system timer/clock

Snipeye
Posts: 5
Joined: Fri Mar 08, 2019 4:30 am

Fastest system timer/clock

Postby Snipeye » Sun Oct 09, 2022 3:04 am

I've written some asynchronous code (using the Arduino core) that relies on the number of microseconds that have passed to perform certain actions.

I am currently using esp_timer_get_time() to get the number of microseconds, but there appears to be a fair bit of overhead to that function: when I call it repeatedly in the loop, depending on my clock frequency, it takes anywhere from 10-30us just to return.

I'm hoping for something a little more granular - optimally something that takes <1us to return. I was hoping I could find the registers that the system clock updates every tick and just read the register holding the lower 32 bits (since I know there are fancy things that are going on to freeze-and-read to get all 64 bits in sync), then divide by clock speed (since it doesn't vary for my application).

For 240mhz, I show that 32 bits should hold ~17.9 seconds before overflow, which should be plenty for me. Unfortunately, I can't figure out where to access this register. I've tried some of the FRC registers that I dug around, but they didn't seem to be changing at all. Also looked at other TIM0 registers, but they weren't changing either. I read *something* about needing to prompt an update by writing a register, but I figured I'd ask here if anybody had more information.

I'm not dead set on reading a system clock register, I just need a way of asynchronously and accurately tracking time, ideally with 1us resolution (though anything better than 10us is an improvement).

Thanks!

noweare
Posts: 67
Joined: Tue Jul 02, 2019 11:35 am

Re: Fastest system timer/clock

Postby noweare » Mon Oct 10, 2022 1:54 am

Maybe you could use a timer that interrupts (alarms) at 1 usec and set up an interrupt to just increase the a variable each time.
Then just read that variable. When using the idf 64 bit timers are available.

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: Fastest system timer/clock

Postby ESP_igrr » Mon Oct 10, 2022 6:20 am

Hi Snipeye,

Could you please attach your sdkconfig file and the code you used to benchmark esp_timer_get_time? Also please mention the version of IDF you are using.

Reason i am asking is, we have a test case that this function returns in less than 1us. So 10-30us sounds like way too much.

Snipeye
Posts: 5
Joined: Fri Mar 08, 2019 4:30 am

Re: Fastest system timer/clock

Postby Snipeye » Sun Nov 06, 2022 5:17 am

Hi, following up here after a long while.

I'm using the Arduino IDE right now, actually, and I found that the actual cause of the ~15us delay was the housekeeping in between loop() iterations. Wrapping my inner loop in a while(1) resulted in significant speedups, and the function call to esp_timer_get_time() is actually sufficiently fast.

Thanks for following up!

Who is online

Users browsing this forum: No registered users and 169 guests