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.
Contents
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.
- CMSIS-CORE
- CMSIS-Driver
- CMSIS-DSP
- CMSIS-RTOS
- CMSIS-Pack
- CMSIS-SVD
- CMSIS-DAP
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.
CMSIS-CORE
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.
- HAL (Hardware Abstraction Layer)
- System exceptions, Interrupt vector
- Configuration of header files
- Initialization method of system
- Embedded function of instructions that doesn’t support by C/C++ complier
- 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.

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.
Here is the usage of the SysTick timer in-line function.
uint32_t SysTick_Config (uint32_t ticks )
Parameter
[in] ticks The number of ticks between interrupts.
Return
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. */
BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();
/* Cycle count start */
ret= SysTick_Config(16000000);/*<---- Here starts the cycle count */
if(ret){
PRINTF("SysTick configuration is failed.\n\r");
while(1);
}
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 */
while(1);
}
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
}
while(1);
}
Summary
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.