cancel
Showing results for 
Search instead for 
Did you mean: 

How to enable a timer using cube hal?

Posted on November 26, 2015 at 14:06

Working with:

  • Stm32F401C-Disco
  • STM32Cube_FW_F4_V1.9.0
  • CubeMX 4.11.0
  • SW4STM32
In my project, I need to setup a timer (e.g. TIM3) directly in hand written code.

I struggled around on the question, what is the official way to do that using cube hal api.

I find things like:

__TIM3_CLK_ENABLE();

well, it works, but 3 things bother me here (yep, most might cosmetics, but because of my company guidelines the code must be as clean as possible):

  • The macro is located in stm32_hal_legacy.h. As I'm working on a fresh new project, I don't want to use legacy stuff.
  • The double underscore prefix of the macro sounds like private stuff rather than api for application code, isn't it?
  • Using SW4STM32 with -Wall -Wextra in C++ source give warnings: ''conversion to void will not access object of type 'volatile uint32_t {aka volatile long unsigned int}'''
I'd checked generated code when configuring the timer using CubeMX and find out that the code generator also uses this legacy call `__TIM3_CLK_ENABLE()`.

Regarding the gcc warning, I find out the problem is located to the UNUSED macro in stm32f4xx_hal_def.h:

#define UNUSED(x) ((void)(x))

The problem became solved when I just remove two brackets like:

#define UNUSED(x) ((void)x)

It seems that this is more portable than the current macro.

So could you please:

  • State out how to enable the timer using cube hal (without legacy macros and hopefully without underscore prefixing).
  • Change the `#define UNUSED` in stm32f4xx_hal_def.h

Thanks in advance,

    Joe

3 REPLIES 3
stm23
Associate
Posted on January 01, 2016 at 18:49

The Problem exists here too, even with the fresh Version 1.0.

If you change the definition


 #define UNUSED(x) ((void)(x))

to

#define UNUSED(x) ((void)x)

the problem vanishes. You should consider changing this, even if You say it ''is a rule of coding HAL library''
Posted on November 27, 2015 at 17:14

HiJoe,

1-To enable the timer, you have to use__HAL_RCC_TIM3_CLK_ENABLE() or __TIM3_CLK_ENABLE(). If you want to get rid of ''__'' you can access directly to the register

SET_BIT(RCC->APB1ENR, RCC_APB1ENR_TIM2EN);

2-Defining the macro unused as

#define UNUSED(x) ((void)(x))

is a rule of coding HAL library. Thank you for your feedback -Shahrzad-
Posted on December 12, 2015 at 23:38

regarding that

#define UNUSED(x) ((void)(x))

how should I get rid of the gcc warnings?