cancel
Showing results for 
Search instead for 
Did you mean: 

How to identify which timer channel triggered the "HAL_TIM_PeriodElapsedCallback" interrupt?

NGalin
Associate II

I am using STM32F302R8, and have setup channels 1 to 5 of TIMER1 in various output modes. In the callback function, I would like to identify which channel generated the interrupt.

I am using HAL, and can figure out which timer generated the interrupt, like so:

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)

{

 if (htim->Instance == htim1.Instance) {

go_do_stuff_for_timer1();

}

else if (htim->Instance == htim2.Instance) {

go_do_stuff_for_timer2();

}

}

But, how to tell which channel of htim1 generated the interrupt?

1 ACCEPTED SOLUTION

Accepted Solutions
KnarfB
Principal III

HAL_TIM_PeriodElapsedCallback is never generated from a channel. It comes from the basic timer counter overflow/underflow which you start with HAL_TIM_Base_Start_IT.

Channels can be started individually in different modes, say HAL_TIM_PWM_Start_IT.

Then there is a HAL_TIM_PWM_PulseFinishedCallback. For those callbacks, channel can be determined using

if( htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4 ) ...

You can set a breakpoint in the real intertupt handler, say TIM17_IRQHandler, in stm32f0xx_it.c and then step into the HAL code to verify.

Note that HAL is not everybodies darling, because timers can do more, faster, and more flexible when programmed directly at register level. There is another learning curve though. So HAL is okay for me in many circumstances, but not all.

hth

KnarfB

View solution in original post

2 REPLIES 2
KnarfB
Principal III

HAL_TIM_PeriodElapsedCallback is never generated from a channel. It comes from the basic timer counter overflow/underflow which you start with HAL_TIM_Base_Start_IT.

Channels can be started individually in different modes, say HAL_TIM_PWM_Start_IT.

Then there is a HAL_TIM_PWM_PulseFinishedCallback. For those callbacks, channel can be determined using

if( htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4 ) ...

You can set a breakpoint in the real intertupt handler, say TIM17_IRQHandler, in stm32f0xx_it.c and then step into the HAL code to verify.

Note that HAL is not everybodies darling, because timers can do more, faster, and more flexible when programmed directly at register level. There is another learning curve though. So HAL is okay for me in many circumstances, but not all.

hth

KnarfB

NGalin
Associate II

Thank you!

Yes, I agree HAL is consistently frustrating. There is no doc (that I've found?) that tells you what HAL functions need to be called to initialize/start/use various timer functions etc.

The RM however tells you exactly how to set various registers to what values for what purpose. At some point I'll definitely stop using HAL.