2
\$\begingroup\$

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?

Bence Kaulics
6,47312 gold badges35 silver badges61 bronze badges
asked Oct 19, 2015 at 20:15
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

I've finally figured out my issue. It was nothing to do with my code, but rather the QEMU simulator I was running my code on.

Once I plugged in the board, and ran the code on it, I started getting timings as expected.

Not using QEMU again!

answered Oct 19, 2015 at 20:58
\$\endgroup\$
4
  • \$\begingroup\$ Exactly. When you have a board like the discovery that costs 15,ドル there's little reason to simulate. \$\endgroup\$ Commented Oct 19, 2015 at 21:44
  • \$\begingroup\$ I had no idea QEMU could simulate STM32F4 (well, badly as it turned out). There are some limitations outlined at gnuarmeclipse.github.io/qemu I didn't go through that list, but if it's not a known one, perhaps worth reporting as a bug. \$\endgroup\$ Commented Oct 19, 2015 at 22:20
  • \$\begingroup\$ Agreed, the only reason I simulated was because I was actually coding while in commuting to work/home with the bus. But now that boats' completely blown out the water. \$\endgroup\$ Commented Oct 20, 2015 at 6:09
  • \$\begingroup\$ QEMU is mostly for emulating the Arm core. The vendor peripherals are generally not implemented. The eclipse Qemu plugin supports GPIOs for some micros. Vendors could implement them but they don't seem to. \$\endgroup\$ Commented Jun 26, 2018 at 7:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.