2018-11-23 07:29 AM
Hi,
I try to use STM32 timer with one pulse mode and two output compare channels. I use cubeMX initialize the code, but now I have question abut how to enable two channels OC?
HAL library provide function HAL_TIM_OnePulse_Start(), which only have one parameter for channel selection. How can I select two channels?
And in this library function, it comments that if CH1 is used as output, CH2 will be input. what that mean? does it only support one channel output in this one pulse mode?
Here is the library function:
/**
* @brief Starts the TIM One Pulse signal generation.
* @param htim TIM One Pulse handle
* @param OutputChannel TIM Channels to be enabled
* This parameter can be one of the following values:
* @arg TIM_CHANNEL_1: TIM Channel 1 selected
* @arg TIM_CHANNEL_2: TIM Channel 2 selected
* @retval HAL status
*/
HAL_StatusTypeDef HAL_TIM_OnePulse_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(OutputChannel);
/* Enable the Capture compare and the Input Capture channels
(in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
No need to enable the counter, it's enabled automatically by hardware
(the counter starts in response to a stimulus and generate a pulse */
TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
{
/* Enable the main output */
__HAL_TIM_MOE_ENABLE(htim);
}
/* Return function status */
return HAL_OK;
}
2018-11-23 10:54 AM
The whole onepulse shebang in HAL is some bizarre single-purpose contraption, intended to support the example which works as a single-shot, i.e. upon an edge arrives on one channel, it generates a pulse on other. While this utilizes what one-pulse mode in the timer really means, it places nonsensical constraints on it too.
But why do you insist on using Cube/HAL? One-pulse mode is determined by TIMx_CR1.OPM being set. If it is set, once the timer is enabled (by setting TIMx_CR1.CEN), it counts up to the TIMx_ARR value, and together with generating update event at the moment of "rollover", it automatically disables itself (i.e. hardware clears TIMx_CR1.CEN). That's all.
So, the timer in the one-pulse mode counts only one full cycle. And, during that cycle, everything what's possible with normal running is possible too. So, among other things, you can generate a pulse by setting any or all of the four (or six, if you have some of the super-duper new chips and the best timers in them) channels to PWM.
I, of course, don't Cube.
JW
2018-11-26 07:52 AM
Thank you JW!
After I dig into the datasheet, I realize the STM32 timer is very powerful and complicated. but the HAL library doesn't support all functions. This is disappointed! I was using standard library for STM32 8 years ago. I was enjoy it! Now when I come back to STM32 with current project, I found ST encourage people using Cube and HAL library. but Cube is buggy and HAL is limited. Now I can understand why people like you doesn't like Cube!
Thanks for your answer, I will try LL library for it.
Jeff