Let’s use CMSIS! Easy way to measure cycle count.

CMSIS Systick

I will show you a good tip when you want to measure the time or cycles taken for your code.

By means of CMSIS ( Cortex Micro-controller Software Interface and Standard) which is an application software interface for Cortex-M MCU, you can do it so easily.

Here is the easy way of using CMSIS interface to measure cycles. Let’s do it.

CMSIS Interface

There are seven software interface standards in total,  which are specified in a common and consistent software interface for the use of Cortex-M core devices or the peripherals for it.

  2. CMSIS-Driver
  5. CMSIS-Pack

With above interfaces, you can access the Cortex-M core, DSP instructions, embedded functions and even can access debug interface in common way.

CMSIS is a runtime system that allows access to the core.

With using this CMSIS, as you can access any Coretex-M device with common interfaces, it makes porting easier.



This time, I will use CMSIS-CORE out of these interfaces for measuring cycles taken for my code.

CMSIS-CORE is comprised of a HAL(Hardware Abstraction Layer), System exceptions, interrupt vectors and  system initialization and so on. Those are the interfaces that allows access to the core of the main system.

Here are the CMSIS-CORE.

  1. HAL (Hardware Abstraction Layer)
  2. System exceptions,  Interrupt vector
  3. Configuration of header files
  4. Initialization method of system
  5. Embedded function of instructions that doesn’t support by C/C++ complier
  6. SysTick timer initialization and start it

In large, it consists of six parts.

This time, I will use the sixth of them.

SysTick Timer

Let’s use it first without my long explanation.

First, you need to download MCUXpresso IDE and stall it. Also you need to download SDK for your board.

You can build the SDK according to the board you want. Once you download the SDK, you can import it by drag-and-drop the SDK in the “Installed SDK…”.

Now you can import any sample code projects.

CMSIS comes with any projects in default as you can see below picture, left hand side of red colored rectangle.


CMSIS files
CMSIS files

This time, I will use SysTick timer. There is SysTick_config() which is an in-line function in core_cm0plus.h.

SysTick timer is clocked by core clock. SysTick_config() sets the counter value and start the counter.

The usage of the timer is just simple! All you need to do is to set the counter value. The counter value is decremented by core clock and if it comes to zero, it generates an interrupt on SysTick_IRQn.

This time, I only want to measure the counter value for measuring cycle count. So, the counter value can  be any value, unless it is more than the time your code takes. Otherwise, the SysTick timer would be expired and generate an interrupt before your code finishes.

For the usage of the interrupt, it can be used for a context switch of operating system, or so.

 Be aware that Cortex-M0+/M3/M4 core has SysTick timer with 24Bit counter. Since the timer’s counter is upto 0xFFFFFF(16777215d), you get an error if you put more than that value.

Here is the usage of the SysTick timer in-line function.

uint32_t SysTick_Config (uint32_t ticks )
[in]	ticks	The number of ticks between interrupts.

0 - Success
1 - Error


Measuring cycle count

Now that I measure the cycle count.

You need to install SDK by drag-and-drop .zip file of SDK into MCUXpresso IDE.  And, Hello World sample program is imported from the SDK. How-to install the SDK, please refer to the other articles.

I prepare a simple code for the purpose of measuring cycle count. You can use it if you want.

You need to modify it in Hello World main() function as below.

int main(void)
	uint32_t cnt=0;
	volatile uint32_t i;
	uint16_t ret;

	/* Init board hardware. */

	/* Cycle count start */
	ret= SysTick_Config(16000000);/*<---- Here starts the cycle count */
		PRINTF("SysTick configuration is failed.\n\r");
	for (i=0; i<1000;i++ );
	cnt = 16000000 - SysTick->VAL;/*<--- Here stops the cycle counting */
	PRINTF("Result : %d Cycles\n\r",cnt);
	/* Cycle count ended */



Result of measuring the cycle count


In above picture, It measures the number of loops in the for-loop. Of course, the measured cycle would increase in proportion to the number of loops you set.

It also counts the IF sentence.

The result is 12048 cycles in total.

SysTick_config() sets the counter value and start it. And, in this code, I didn’t stop the SysTick timer, but you can stop it by clearing SysTick.CTRL Enable bit register.

Additional info: SysTick interrupt

SysTick_Config generates an interrupt after the counter expires according to the tick you set.

Let’s see the how-to.

The sample code is written in the CMSIS document.

#include "LPC17xx.h"
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 */ 
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 


You can count cycles easy with CMSIS-CORE. For me, I sometimes want to measure time or cycles taken to operate a specific code.

In the case of Cortex-M4F, you can use CYCLE COUNT in CPU register when you debug, but it is not the case to Cortex-M0/Mo+. If you want to count the cycle on Cortex-M0/M0+, you can use this tips of SysTick Timer.