Skip to main content
gil_dobjanschi
Associate II
April 22, 2026
Solved

Starting the second channel of a timer causes assert failure

  • April 22, 2026
  • 5 replies
  • 296 views

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

Best answer by STackPointer64

Hello @gil_dobjanschi

After testing, I realized I was mistaken: all the debug options in the screenshot above need to be enabled to generate the necessary functions for the application to work. I tested it with a logic analyzer and confirmed that it works. Attached, you will find the project.

STackPointer64_0-1776940302365.png

Best regards,

5 replies

Technical Moderator
April 22, 2026

Hello @gil_dobjanschi,

Let me check this out and get back to you ASAP.

Best regards,

To improve visibility of answered topics, please click 'Accept as Solution' on the reply that resolved your issue or answered your question.
T_Hamdi
ST Employee
April 22, 2026

Hello @gil_dobjanschi 

Please, can you share the .ioc file or your example? I have tested it, and I noted that this line does not have any issue.

 

"To give better visibility on the answered topics, please click on ""Accept as Solution"" on the reply which solved your issue or answered your question.Hamdi Teyeb"
gil_dobjanschi
Associate II
April 22, 2026

Hi @T_Hamdi ,

 

I attached the ioc2 file. Since I wrote the message I also enabled channel 4.

Thank you,

Gil

Technical Moderator
April 22, 2026

After inspecting the IOC, I believe you forgot to enable output channel CH3, just as you did with CH1. I suggest that you inspect the pwm_output HAL2 example in the software package.

To improve visibility of answered topics, please click 'Accept as Solution' on the reply that resolved your issue or answered your question.
gil_dobjanschi
Associate II
April 22, 2026

Hi @STackPointer64 ,

In STM32CubeMx2 I enabled both channel 3 and channel 4. I attached the screenshots for the channel 3 and 4 configurations and I reattached the ioc2 just to make sure the mx2 configuration I'm looking at corresponds to the ioc2 file.

Thanks,

Gil

 

 

Technical Moderator
April 22, 2026

To resolve the issue, you need to enable Use default assert state function and 

To resolve the issue, you need to enable Use default assert state function and Generate default assert parameter handler in the HAL System Init configuration. These options are required for the program to build and run successfully when the parameter assert option is enabled. Doing so adds the template implementations of the assert_dbg_param_failed and assert_dbg_state_failed functions in the mx_system.c source file.

STackPointer64_0-1776867081998.png

 

To improve visibility of answered topics, please click 'Accept as Solution' on the reply that resolved your issue or answered your question.
gil_dobjanschi
Associate II
April 22, 2026

Hi @STackPointer64 ,

 

Yes, I provided my own implementation of the assert functions which print the file name and line number where the assert occurs. This is how I learned where the assert occurs. My code builds without problems. When I run it and try to start channel 3 the problem occurs.

 

Regards.

-Gil

gil_dobjanschi
Associate II
April 22, 2026

I commented out the asserts and I verified on the oscilloscope that channel 3 and 4 are working and outputting the expected waveform.

gil_dobjanschi
Associate II
April 23, 2026

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

Technical Moderator
April 23, 2026

You’re very welcome! If you come across any other issues, please open a thread, and we’ll be happy to help.

To improve visibility of answered topics, please click 'Accept as Solution' on the reply that resolved your issue or answered your question.