STM32 Code Execution Time.

It all started when I needed to at least roughly estimate the execution time of a piece of code. It turned out that microcontrollers with a Cortex-M3 core have a special module for this called the Data Watchpoint and Trace Unit, DWT for short.

To configure DWT to measure code execution time, you need to set 2 bits.

The TRCENA bit in the DEMCR register; setting it to one enables the use of DWT.

The CYCCNTENA bit in the DWT_CTRL register; setting it to one starts the counter.

The current counter value can be read from the 32-bit CYCCNT register.

The code for estimating code execution time looks like this

#define    DWT_CYCCNT    *(volatile unsigned long *)0xE0001004
#define    DWT_CONTROL   *(volatile unsigned long *)0xE0001000
#define    SCB_DEMCR     *(volatile unsigned long *)0xE000EDFC

SCB_DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;// enable the use of DWT

DWT_CYCCNT = 0;// reset the value

DWT_CONTROL|= DWT_CTRL_CYCCNTENA_Msk; // start the counter

Some code

count_tic = DWT_CYCCNT;//see how many cycles have elapsed


count_tic is the number of clock cycles during which the code was executing. If we want the time, we divide this value by the clock frequency, in my case by 56,000,000.

In principle, we could stop here, but I want to check whether what we read from DWT_CYCCNT actually corresponds to real time.

To check this, let's set up a timer that generates an interrupt once per second; inside the interrupt we'll read the DWT_CYCCNT value, reset it, and so on in a loop.


#include "stm32f10x.h"

#define    DWT_CYCCNT    *(volatile unsigned long *)0xE0001004
#define    DWT_CONTROL   *(volatile unsigned long *)0xE0001000
#define    SCB_DEMCR     *(volatile unsigned long *)0xE000EDFC
	
uint32_t count_tic = 0;

void TIM3_IRQHandler(void)
{
	count_tic =  DWT_CYCCNT;
	//stop DWT
	DWT_CONTROL &= ~DWT_CTRL_CYCCNTENA_Msk; 
	DWT_CYCCNT  = 0;
	//disable the timer
	TIM3->CR1 &= ~TIM_CR1_CEN;
	//reset its value
	TIM3->CNT = 0;
	//clear the interrupt flag twice; otherwise it wouldn't clear for me
	TIM3->SR &= ~TIM_SR_UIF; 
	TIM3->SR &= ~TIM_SR_UIF; 	
}


int main(void)
{	
	//enable timer clocking
	RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
	//set the prescaler; the new timer frequency is 10KHz
        TIM3->PSC  = 5600 - 1; 
	//period of 1 second
        TIM3->ARR  = 10000 - 1; 
	//the counter counts down
        TIM3->CR1 &= ~TIM_CR1_DIR; 
	//in one-pulse mode
        TIM3->CR1 |= TIM_CR1_OPM; 
	//enable interrupts
        TIM3->DIER  |= TIM_DIER_UIE; 
	NVIC->ISER[0] = NVIC_ISER_SETENA_29;
	//start the timer
	TIM3->CR1 |= TIM_CR1_CEN;
	
         //enable DWT
	 SCB_DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
	 DWT_CYCCNT  = 0;
	 DWT_CONTROL|= DWT_CTRL_CYCCNTENA_Msk; 
	
	while(1)
	{
               //enable the timer
		TIM3->CR1 |= TIM_CR1_CEN;
		//start DWT
		DWT_CONTROL|= DWT_CTRL_CYCCNTENA_Msk; 
	}
}

STM32 Code Execution Time.

Setting the TIM_CR1_OPM bit enables one-pulse mode; in this mode counting stops after the update event occurs.
The first value differs from the others because the timer and the counter should really be started at the same time; the following ones differ from the calculated value by a few cycles needed to store the DWT_CYCCNT value in count_tic, so this method works. That's all; thank you for your attention.