Counter/Timer

cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 - Reading general purpose timer count

There seems to be a little bit of trouble when I run this code.

The situation is following,I'm using the STM32F4 HAL library on an emulated [in QEMU] STM32F4 Discovery board, and trying to configure TIM2 (general purpose timer) and read its' count register (without an interrupt). Currently I'm always getting 0 when attempting to read the timer counter with

uint32_t count = __HAL_TIM_GetCounter(&hTim2);

I don't want to move on to using an interrupt just yet, until I get this step working. Taking it step by step.

Here's how I configured the timer so far:

__initialize_hardware.c

__TIM2_CLK_ENABLE(); // Enable the TIM2 clock
// ...
Timer_Init();

timer.h

TIM_HandleTypeDef hTim2;

timer.c

#include "timer.h"

void Timer_Init(void) {
    hTim2.Instance = TIM2;
    hTim2.Init.Prescaler = 40000;
    hTim2.Init.CounterMode = TIM_COUNTERMODE_UP;
    hTim2.Init.Period = 500;
    hTim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    HAL_TIM_Base_Init(&hTim2);

    HAL_TIM_Base_Start(&hTim2); // Trying to start the base counter
}

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) {
    if (htim_base->Instance == TIM2) {
        __TIM2_CLK_ENABLE();
    }
}

void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base) {
    if (htim_base->Instance == TIM2) {
        __TIM2_CLK_DISABLE();
    }
}

then in main.c

int main(int argc, char* argv[]) {
    while (1) {
        uint32_t count = __HAL_TIM_GetCounter(&hTim2);
        trace_printf("%lu\n", count);
    }
}

I'm always getting 0 in count above, not sure why? Can anyone please offer some advice for my program?

Thanks

0 Kudos
Message 1 of 1
(4,331 Views)