Calculating program execution time in miliseconds or microseconds

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Calculating program execution time in miliseconds or microseconds

Jump to solution
2,504 Views
itr1718
Contributor III

Hello,

 

I am trying to calculate program running time in miliseconds or microseconds.

I used PIT and RTC, but it works not correctly.

"RTC_GetDatetime" calculate in seconds and I thought that this function "PIT_GetCurrentTimerCount" returns a counted execution time.

Moreover, functions from time.h don't work on KL27 Microcontroller.

 

Do I have some mistakes or misunderstanding or are there some methods for the execution time?

How can the time calculated? 

 

PIT Setting:

pit_config_t pitConfig;

PIT_GetDefaultConfig(&pitConfig);
PIT_Init(PIT, &pitConfig);
PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, USEC_TO_COUNT(1000000U, PIT_SOURCE_CLOCK));
PIT_EnableInterrupts(PIT, kPIT_Chnl_0, kPIT_TimerInterruptEnable);
EnableIRQ(PIT_IRQ_ID);
PIT_StartTimer(PIT, kPIT_Chnl_0);
PIT_IRQHandler();

 

PIT_IRQHandler:

void PIT_IRQHandler(void)
{
/* Clear interrupt flag.*/
PIT_ClearStatusFlags(PIT, kPIT_Chnl_0, kPIT_TimerFlag);
pitIsrFlag = true;
__DSB();
}

 

Start time:

int64_t start = PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_0);

 

Calculating running time:

int64_t get_timestamp_us(int64_t start)
{
int64_t now = PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_0);

return (now-start) * 1000000;
}

 

Best regards,

itr

0 Kudos
1 Solution
2,451 Views
itr1718
Contributor III

Hello @bobpaddock  and @mjbcswitzerland ,

 

thanks for your help. I solved the problem with timer interrupt which is called every second. 

I assigned a variable and increased, when the timer interrupt is called. 

 

Have a nice day

View solution in original post

0 Kudos
7 Replies
2,493 Views
bobpaddock
Senior Contributor III

"pitIsrFlag = true;
__DSB();"

Those concern me.
Flags like this frequently create race conditions.
Better to increment a var in the IRQ then outside compare with a previous value to see if it changed.

In any case such vars need to be declared 'volatile' .

The DSB instruction itself will cause an unknown number of cycles to be added to each IRQ.
I expect it was added to see 'will this make this work?'.  There are places such instructions are needed, this is not one of them.

What exactly does "time.h doesn't work" mean here?
What is the clock() function doing or not doing?
That may depend on the toolchain being used.



 

2,486 Views
itr1718
Contributor III

Hello @bobpaddock ,

That is an good idea which a var increases in the IRQ. 

I will try that. 

Furthermore, "time.h doesn't work" means that the functions from time.h which are clock(), time(), gettimeofday(), localtime() etc. They return always -1 after I added -lnosys.

This library, nosys, needs for USB driver.

 

Best regards

itr

0 Kudos
2,497 Views
mjbcswitzerland
Specialist V

Hi

The Cortex-M4 contains a cycle counter that is designed to do this very accurately and simply. Therefore it is best to use that instead:

Initialisation:

DEMCR |= DHCSR_TRCENA; // enable trace for DWT features
DWT_CYCCNT = 0; // reset the cycle count value
DWT_CTRL |= DWT_CTRL_CYCCNTENA; // enable the cycle counter

To measure the time a code section takes

unsigned long ulTimeStamp = DWT_CYCCNT; // time stamp before starting
.. code to be measured
ulTimeStamp = (DWT_CYCCNT - ulTimeStamp);   // counts during the code execution


To convert the value to us

ulTimeStamp /= (SYSTEM_CLOCK / 1000000);


Regards

Mark
[uTasker project developer for Kinetis and i.MX RT]
Contact me by personal message or on the uTasker web site to discuss professional training, solutions to problems or rapid product development requirements

For professionals searching for faster, problem-free Kinetis and i.MX RT 10xx developments the uTasker project holds the key

0 Kudos
2,484 Views
itr1718
Contributor III

Hello @mjbcswitzerland ,

as I know, KL27 has Cortex-m0+ and Cortex-m0+ and m4 are different. 

 

Do you know maybe about Cortex-m0?

 

Best regards

itr

0 Kudos
2,475 Views
mjbcswitzerland
Specialist V

Hi

My mistake, I oversaw the fact that you are using a KL27.

For the Cortex-M0+ parts one can use PIT0 as follows:

Initialisation:

POWER_UP_ATOMIC(6, PIT0);
PIT_MCR = 0;
LOAD_PIT(0, 0xffffffff);
PIT_TCTRL0 = PIT_TCTRL_TEN;

To measure the time a code section takes

unsigned long ulTimeStamp =(0xffffffff - PIT_CVAL0); // time stamp before starting
.. code to be measured
ulTimeStamp = ((0xffffffff - PIT_CVAL0) - ulTimeStamp);   // counts during the code execution


To convert the value to us

ulTimeStamp /= (BUS_CLOCK / 1000000);

There is no need to work with interrupts for such measurements.


Regards

Mark

P.S Methods taken from uTasker project code.



 

0 Kudos
2,494 Views
bobpaddock
Senior Contributor III

As to Op mentions the KL27, I presume that is what he is using.
Sadly the KL27 m0+ core doesn't have this.

0 Kudos
2,452 Views
itr1718
Contributor III

Hello @bobpaddock  and @mjbcswitzerland ,

 

thanks for your help. I solved the problem with timer interrupt which is called every second. 

I assigned a variable and increased, when the timer interrupt is called. 

 

Have a nice day

0 Kudos