cancel
Showing results for 
Search instead for 
Did you mean: 

timer interrupt callback stm32f769 cubemx

Stefan M�ller
Associate II
Posted on April 10, 2018 at 12:04

Hi,

I'm using stm32f769 with cube MX and Atollic for my latest project. So far I managed some stuff like IAP Bootloader via µSD /or USB, PWM Generation, Encoder readout... So far I would guess I understood how CubeMX and HAL Library work together and how to use HAL and LL Lib.

I want to use a timer Interrupt (TIM10) and on every update and PWM pulse. I see that the Interrupt handler gets called

void

TIM1_UP_TIM10_IRQHandler(

void

)

{

HAL_TIM_IRQHandler(&htim10);

and checks for the reason. Then a specific callback function is called like:

HAL_TIM_PWM_PulseFinishedCallback(htim);

or

HAL_TIM_PeriodElapsedCallback(htim)

it is marked __weak so I can use my own ...

BUT here is my issue:

As far as I read the code the Information which timer calls the callback function is lost as every Timer will call same callback function.

So in case I use more than one timer I have to check inside my callback function which timer caused it? I have to read the htim struct and get the info back, then have some switch case that leads to the code I want to have executed???

Or is there a better way to do it. Icouldn't find any example about this... 

And second question, but not so important: Why is it actually called 'callback function'? In my understanding a callback function is a function I call by a 'pointer to function' Parameter as Parameter of an other function ... I can not see anything like this here, so some explanation would be nice.

Thanks in advance

Stefan

6 REPLIES 6
Pevy.Andy
Associate III
Posted on April 10, 2018 at 12:49

You will have to check the htim->Instance value for the relevant timer channel.

Stefan M�ller
Associate II
Posted on April 10, 2018 at 13:12

Hi,

as far as I see not only the channel, also which timer, so code would be something like:

void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) {

   

   if(htim->Instance==TIM10)  {

      if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1){

         //do what needs to be done

      }else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2){

         //do what needs to be done

      }else ....

}else if (htim->Instance==TIM1)  { ...

Please proof me wrong, but if this is the way to go HAL Lib at this stage is 100% CRAP!

When getting the interrupt vector I know (at least with just very few different options) the reason why I'm there. Then I read the the detailed information from registers and clear the flags. Now I call a function ( I don't want to call it 'callback function' as I don't see this here) and have to go through all this finding the reason for my interrupt reason again just not on register level but on the TIM_HandleTypeDef struct level???

This does not look like efficient code!

Is this really the way it is designed to be?

Posted on April 10, 2018 at 14:25

Yup.

Posted on April 10, 2018 at 15:41

crazy...

and the name 'callback function' is also misleading here or did I get something wrong as I just see normal function calls?

Posted on April 10, 2018 at 15:45

Semantics.... It is a piece of code that gets called when something happens elsewhere. Who cares what it is called.

Stefan M�ller
Associate II
Posted on April 11, 2018 at 10:56

I see....

But now I got a real question ... I use the timer to generate fixed length pulses with adaptive frequency. To stop the pulse generation I put the ARR register value to smaller value than CCR register. I also need to count the number of pulses precisely. Therefor my idea was to trigger the counter on every interrupt with CCIF set... unfortunately the flag also gets set on the update event and I'm also counting pulses when in fact the output is stable....

Any idea is highly appreciated...