Home » Embedded Processors » Measuring code execution time for ARM microcontrollers

Measuring code execution time for ARM microcontrollers

If you have written some code in C for the microcontrollers and want to measure time required for the execution of this piece of code, then we can use SysTick Timer by generating time timer interrupts at predefined interval like 1ms as we did here.

volatile uint32_t msTicks = 0;           /* Variable to store millisecond ticks */
  
void SysTick_Handler(void)  {            /* SysTick interrupt Handler. */
  msTicks++; 
}


void MyFunction(void) {
   
    .... some code for which we need to measure time ....
}
 
int main (void)  {
  uint32_t returnCode;
  
  returnCode = SysTick_Config(SystemCoreClock / 1000);      /* Configure SysTick to generate an interrupt every millisecond */
  
  if (returnCode != 0)  {                   /* Check return code for errors */
    // Error Handling 
  }
  
  TimeBeforeExecution = msTicks

  MyFunction();

  Totaltime = msTicks - TimeBeforeExecution;


  while(1);
}

The System Tick Time (SysTick) generates interrupt requests on a regular basis. This allows an OS to carry out context switching to support multiple tasking. For applications that do not require an OS, the SysTick can be used for time keeping, time measurement, or as an interrupt source for tasks that need to be executed regularly.

volatile uint32_t msTicks = 0;           /* Variable to store millisecond ticks */
  
void SysTick_Handler(void)  {            /* SysTick interrupt Handler. */
  msTicks++;            /* See startup file startup_LPC17xx.s for SysTick vector */ 
}

Initialises and starts the System Tick Timer and its interrupt. After this call, the SysTick timer creates interrupts with the specified time interval. 

SysTick_Config(SystemCoreClock / 1000);

Using Below code you can now measure the time required to call and execute the function code.

TimeBeforeExecution = msTicks

MyFunction();

Totaltime = msTicks - TimeBeforeExecution;

Reference : https://www.keil.com/pack/doc/CMSIS/Core/html/group__SysTick__gr.html

Leave a Comment