ballack
ballack

Reputation: 21

How to find the execution time in stm32f3xx?

#include "main.h"
#include "stm32f3xx_hal.h"
#include<time.h>

TIM_HandleTypeDef htim2
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);
long int count1,count2,count,i=5;


int main(void)
{
    HAL_Init();

    SystemClock_Config();

    MX_GPIO_Init();
    MX_TIM2_Init();
    HAL_TIM_Base_Init(&htim2);
    HAL_TIM_Base_Start(&htim2);

    count1= __HAL_TIM_GET_COUNTER(&htim2);

    while (i)
    {
        HAL_GPIO_WritePin(GPIOB,GPIO_PIN_3,GPIO_PIN_SET);
        HAL_Delay(50000);
        HAL_GPIO_WritePin(GPIOB,GPIO_PIN_3,GPIO_PIN_RESET);
        HAL_Delay(10000);       
        i--;
    } 
    count=count2-count1;
    count2= __HAL_TIM_GET_COUNTER(&htim2);
}

The code output is always 0. I am unable to obtain the count values. can anyone tell me why it is not executing? I am using STM32F303k8 microcontroller. The count values are always zero, even though it takes few minutes to execute completely!!

Thanks in advance!

Upvotes: 0

Views: 3198

Answers (2)

Miro Samek
Miro Samek

Reputation: 1975

Newer ARM Cortex-M3/4/7 devices provide a register called CYCLECOUNTER, which can be often inspected in a debugger even without using any additional timer/counter and without adding any instrumentation to the code. The technique is described for example in the IAR AppNote "How to measure execution time with CYCLECOUNTER" at:

https://www.iar.com/support/resources/articles/how-to-measure-execution-time-with-cyclecounter/

Upvotes: 1

unwind
unwind

Reputation: 400129

This:

count=count2-count1;
count2= __HAL_TIM_GET_COUNTER(&htim2);

makes no sense at all, you're subtracting from count2 before refreshing it from the timer?

It should perhaps be:

const uint32_t now = __HAL_TIM_GET_COUNTER(&htim2);
count += now - last;
last = now;

With uint32_t last = __HAL_TIM_GET_COUNTER(&htim2); before the loop.

Upvotes: 1

Related Questions