Function for reading cycle count of CPU clock

User avatar
Vader_Mester
Posts: 300
Joined: Tue Dec 05, 2017 8:28 pm
Location: Hungary
Contact:

Function for reading cycle count of CPU clock

Postby Vader_Mester » Wed Mar 28, 2018 6:35 am

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]

Code: Select all

task_t coffeeTask()
{
	while(atWork){
		if(!xStreamBufferIsEmpty(mug)){
			coffeeDrink(mug);
		} else {
			xTaskCreate(sBrew, "brew", 9000, &mug, 1, NULL);
			xSemaphoreTake(sCoffeeRdy, portMAX_DELAY);
		}
	}
	vTaskDelete(NULL);
}


User avatar
Vader_Mester
Posts: 300
Joined: Tue Dec 05, 2017 8:28 pm
Location: Hungary
Contact:

Re: Function for reading cycle count of CPU clock

Postby Vader_Mester » Wed Mar 28, 2018 6:51 am

You are my saviour!!!

Thanks! 8-)

Code: Select all

task_t coffeeTask()
{
	while(atWork){
		if(!xStreamBufferIsEmpty(mug)){
			coffeeDrink(mug);
		} else {
			xTaskCreate(sBrew, "brew", 9000, &mug, 1, NULL);
			xSemaphoreTake(sCoffeeRdy, portMAX_DELAY);
		}
	}
	vTaskDelete(NULL);
}

User avatar
Vader_Mester
Posts: 300
Joined: Tue Dec 05, 2017 8:28 pm
Location: Hungary
Contact:

Re: Function for reading cycle count of CPU clock

Postby Vader_Mester » Wed Mar 28, 2018 10:32 am

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]

Code: Select all

task_t coffeeTask()
{
	while(atWork){
		if(!xStreamBufferIsEmpty(mug)){
			coffeeDrink(mug);
		} else {
			xTaskCreate(sBrew, "brew", 9000, &mug, 1, NULL);
			xSemaphoreTake(sCoffeeRdy, portMAX_DELAY);
		}
	}
	vTaskDelete(NULL);
}

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Function for reading cycle count of CPU clock

Postby kolban » Wed Mar 28, 2018 4:28 pm

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.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

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

Re: Function for reading cycle count of CPU clock

Postby ESP_igrr » Thu Mar 29, 2018 1:29 am


User avatar
Vader_Mester
Posts: 300
Joined: Tue Dec 05, 2017 8:28 pm
Location: Hungary
Contact:

Re: Function for reading cycle count of CPU clock

Postby Vader_Mester » Thu Mar 29, 2018 6:01 am

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.

Code: Select all

task_t coffeeTask()
{
	while(atWork){
		if(!xStreamBufferIsEmpty(mug)){
			coffeeDrink(mug);
		} else {
			xTaskCreate(sBrew, "brew", 9000, &mug, 1, NULL);
			xSemaphoreTake(sCoffeeRdy, portMAX_DELAY);
		}
	}
	vTaskDelete(NULL);
}

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

Re: Function for reading cycle count of CPU clock

Postby ESP_igrr » Thu Mar 29, 2018 1:39 pm

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.

JesusFreke
Posts: 1
Joined: Sat Oct 20, 2018 7:07 pm

Re: Function for reading cycle count of CPU clock

Postby JesusFreke » Sat Oct 20, 2018 7:11 pm

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

Who is online

Users browsing this forum: rr0x1958 and 85 guests