I am using CubeMX to generate code for my NucleoF303. I am a bit confused how to set the pulse width of a timer in PWM mode.

Previously (using an F4) there has been a __HAL_TIM_SetCompare macro in the stm32fxxx_hal_tim.h file.

But that does not exist. Am I supposed to set the duty cycle in some other manner?

Of course I can just directly set the CCRx registers. That works. But if I am going to use a HAL with lots of abstraction like cube seems to be, I would at least like to know how they intend me to use it. Even if I don't.

Not really any code to go with this question.. But here is my current commit in case it is useful: https://github.com/c-herring/Sumo2017_NucleoF303/tree/221dc57a305ccd64a4b98726df3cce02720b280b


  • \$\begingroup\$ Well you can check the STM32CubeF3 HAL description, but there are nothing for you as far as I know, apart from re-configuring the entire channel. __HAL_TIM_SetCompare this just sets the CCRx register directly anyway. So IMO it is OK at it is now. \$\endgroup\$ – Bence Kaulics Sep 18 '17 at 14:32

Previously (using an F4) there has been a __HAL_TIM_SetCompare macro in the stm32fxxx_hal_tim.h file.

That was indeed some time ago. I've found a copy of STM32CubeF4 V1.6.0 (released 28-May-2015) lying around in an old project directory, and it already has the following alias definition

Inc/Legacy/stm32_hal_legacy.h:#define __HAL_TIM_SetCompare            __HAL_TIM_SET_COMPARE

Digging even deeper, the description of 1.4.0 says

STM32CubeF4 Firmware Package V1.4.0 / December 26, 2014

Main Changes

Maintenance release to fix known defects and several enhancements implementation

HAL Macros and literals renaming to ensure compatibles across STM32 series, backward compatibility maintained thanks to new added file stm32_hal_legacy.h under /Inc/Legacy

So, it must be the time when __HAL_TIM_SetCompare got deprecated in favor of __HAL_TIM_SET_COMPARE.

You should still get the alias definition though, because all HAL headers include stm32_hal_legacy.h.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.