cancel
Showing results for 
Search instead for 
Did you mean: 

Suggestion to measure execution time similar gettimeoftheday() in classic linux environment?

HNall.1
Associate III

Hello Everyone,

I'm new to application development with micro-controllers. Currently I'm using STM32F469IH6 for my project with the help of STM32CUBEIDE.

Can anyone suggest how to measure execution time of a certain piece of code. In traditional linux we will use gettimeoftheday(..) utility. How we can do this in the STM32F4 micro-controllers?

It would be very helpful if you provide example script to do this.

Thanks in advance.

3 REPLIES 3
KnarfB
Principal III

There are several possibilities:

HNall.1
Associate III

Hi @KnarfB​ ,

I did something similar to third method as follow. I would like to confirm with you whether the steps that I have followed are correct or not.

int main(){
    volatile uint32_t count = 0;
    volatile uint32_t actual_cycle_count = 180000000; // This represent cycles per second and this was based on clock speed of 180MHz for STM32F469IH6
 
  // addresses of registers
    volatile uint32_t *DWT_CONTROL = (uint32_t *)0xE0001000;
    volatile uint32_t *DWT_CYCCNT = (uint32_t *)0xE0001004;
    volatile uint32_t *DEMCR = (uint32_t *)0xE000EDFC;
 
    for(int iter=0; iter<numIter; iter++){
 
        // enable the use DWT
        *DEMCR = *DEMCR | 0x01000000;
 
        // Reset cycle counter
        *DWT_CYCCNT = 0;
 
        // enable cycle counter
        *DWT_CONTROL = *DWT_CONTROL | 1;
 
        /***************************************************/
        /****** Code that used to measure execution time *****/
        /*************************************************/
 
        count = *DWT_CYCCNT;
 
        milliseconds = ((float)count/actual_cycle_count)*1000;
     
        printf("ElapsedTime:%f in iteration:%d (in ms)\n", milliseconds, iter);
 
        totaltime = totaltime + milliseconds; // sum of milliseconds for numIter.
 
    }

I'm able to run it properly without any issues in debug mode. Please let me know whether i'm doing correctly or not.

Thanks,

Hari

Looks okay. The other day I used

CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->LAR = 0xC5ACCE55;
DWT->CYCCNT = 0;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;

which are define in the CMSIS headers (#include "stm32f4xx.h")