Page 1 of 1

Function for reading cycle count of CPU clock

Posted: Wed Mar 28, 2018 6:35 am
by Vader_Mester
Hey Guys,

Is there an function in ESP-IDF to return the number of cycles?

I came across a function somewhere named "_getcyclescount()", but I'm not sure where, and I'm not really sure which header file should I look for.

Thanks in advance
Vader[BEN]

Re: Function for reading cycle count of CPU clock

Posted: Wed Mar 28, 2018 6:43 am
by WiFive

Re: Function for reading cycle count of CPU clock

Posted: Wed Mar 28, 2018 6:51 am
by Vader_Mester
You are my saviour!!!

Thanks! 8-)

Re: Function for reading cycle count of CPU clock

Posted: Wed Mar 28, 2018 10:32 am
by Vader_Mester
Upon checking the Xtensa documents, I found the CCOUNT register, and it says 32bit.

I assume when it reaches the value 0xFFFFFFFF, it will wrap to 0x00000000.

I also don't know if anything is writing/resetting this register during runtime. Perhaps someone can enlight me about this?

Thanks
Vader[BEN]

Re: Function for reading cycle count of CPU clock

Posted: Wed Mar 28, 2018 4:28 pm
by kolban
To the best of my knowledge, there is no way to reset / write to the register and it would be incredibly dangerous to do so. As I understand it this is a singleton and hardware ticker that ticks at the lowest level of the CPU operation. If logic needs to know how many cycles have passed between point A and point B then one would subtract A from B. If I could change the value of the register, that could break logic elsewhere that expected the register to monotonically increment without perturbation. Doing some mental math, I am guessing that it will look after about 20 elapsed seconds at 200MHz. Do you really need the finest possible granularity of time measurement over such a long duration? I think the standard gettimeofday returns a resolution at the microsecond level on an on-going basis.

Re: Function for reading cycle count of CPU clock

Posted: Thu Mar 29, 2018 1:29 am
by ESP_igrr

Re: Function for reading cycle count of CPU clock

Posted: Thu Mar 29, 2018 6:01 am
by Vader_Mester
Thanks Iggr! Does setting the CCOUNT register do any harm in the ESP32?
I guess not, but would be nice to make sure. It should be making my job easier, to sort-of zero out the register before calculating.

Re: Function for reading cycle count of CPU clock

Posted: Thu Mar 29, 2018 1:39 pm
by ESP_igrr
It doesn't harm the CPU, but keep in mind that FreeRTOS ticks are generated when CCOUNT matches CCOMPARE0. So if you modify CCOUNT, this will likely affect FreeRTOS tick interval. If tick interval exceeds the interrupt watchdog interval, interrupt watchdog will trigger. Ticks are also responsible for timeouts in all FreeRTOS blocking functions.

You may want to consider Timer Group timers instead, which are entirely at application's disposal. These have 64 bit counters, which might make your job easier in some cases.

Re: Function for reading cycle count of CPU clock

Posted: Sat Oct 20, 2018 7:11 pm
by JesusFreke
I was reading through this thread, and was trying to figure out what ESP_igrr was referring to, but his link to master is no longer relevant of course.

I tracked down the version I'm pretty sure he was linking to, in case anyone reading this thread now is curious:

https://github.com/espressif/esp-idf/bl ... clk.c#L726