2026-04-22 1:01 AM - last edited on 2026-04-22 1:19 AM by Gyessine
Hello,
I configured a timer in STM32CubeMX2 to use 2 channels (HAL_TIM_CHANNEL_1 and HAL_TIM_CHANNEL_3). After the mx_system_init() I use the following code:
hal_tim_handle_t *htim5 = mx_tim5_gethandle();
if (HAL_TIM_OC_StartChannel(htim5, HAL_TIM_CHANNEL_1) != HAL_OK) {
return (-1);
}
if (HAL_TIM_OC_StartChannel(htim5, HAL_TIM_CHANNEL_3) != HAL_OK) {
return (-1);
}
if (HAL_TIM_Start(htim5) != HAL_OK) {
return (-1);
}
The first channel starts as expected, but when I attempt to start the second channel the code causes an assert in HAL_TIM_OC_StartChannel:
/* Check and update the channel state */
ASSERT_DBG_STATE(htim->channel_states[channel],
HAL_TIM_OC_CHANNEL_STATE_IDLE);
Do I need to do anything different?
Regards,
-Gil
Solved! Go to Solution.
2026-04-23 6:04 AM
Hi @STackPointer64 ,
Thank you for looking into this issue! I used your ioc2 file, I generated the project, added the application code you used and I could not reproduce the problem. Then I went back to my project regenerated the code, compiled, ran the code and it ran fine (I made sure to uncomment the assert lines in HAL_TIM_OC_StartChannel).
I think that this issue occurs because my channels were not initialized properly meaning that there was a discrepancy between what I saw in MX2 and the actual code generated in mx_tim5.c. I am not blaming MX2 for not generating the code; it could have been a user error. If this issue happens again I will look at the generated code to see if all required channels were initialized.
Sorry for the trouble,
-GIl
2026-04-23 6:16 AM
You’re very welcome! If you come across any other issues, please open a thread, and we’ll be happy to help.