cancel
Showing results for 
Search instead for 
Did you mean: 

How can I reduce the time for interrupt callback function execution?

ki01
Associate II

I have this timer that is configured to generate an interrupt after every 15.625 µs, i.e., when it’s period elapses. The timer elapsed callback function contains the code below. The requirement is that new data be transmitted via SPI every 15.625 µs but a single iteration of this callback function is taking 17.95 µs currently. The clock speeds are at the maximum frequency I can achieve with the internal clock. I’ve also tried to reduce the length of the code as much as I can (There’s no code in while (1)). I believe if I use an external oscillator to achieve a higher SYSCLK frequency, the callback execution time will reduce. I wanted to know if there’s anything else I can try before redesigning the board.

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

       if (htim == &htim2)  {

              DAC_EN;

              HAL_SPI_Transmit(&hspi1, &wave_data[index], 3, 1);

              DAC_DIS;

              index += 3;

              if (index >= 1535)

              {

              index = 0;

              }

}

}
9 REPLIES 9
SofLit
ST Employee

Hello and welcome to the community.

Please in next time use <\> button to insert your code for better visibility.

Thank you for your understanding.

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.
ONadr.1
Senior III

You can speed it up by not using HAL, but by writing your own code directly to handle the iterrupt from the timer. HAL is too complex and time inefficient. The same applies to SPI calls via HAL in an interrupt.

Do less? Do it directly in the Handler?

Use TIM+DMA to pace output to SPI->DR ?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Using HAL is unfortunately the requirement in this case.

If HAL is a requirement, there is no other way than to use a much more powerful MCU.

I modified the code in the HAL_TIM_IRQHandler function to only contain code relevant to the timer channel in use and this did the trick. Not sure if this is going to lead to any issues but I'm guessing it won't since this the only timer interrupt I am using.

Yes, but then you didn't meet the condition of using HAL drivers, but in essence you made your own by modifying. In that case, wouldn't it be better to do it properly and without the rest of the ballast that is in HAL?

BarryWhit
Senior

Why are you using the blocking variant of HAL_SPI_Transmit call in an interrupt? Of course you have a problem.

You should use either the interrupt or the DMA versions of the API.

 

Yes, the HAL Timer IRQHandler is a bloated mess, but the real problem is that you're not using the facilities the HAL offers to operate more efficiently.

 

I'm concerned by the fact that your stated timer period is 15.625us, when the SPI transfer seems to take about that or even more. It suggests you haven't designed your system properly. You should either increase the speed of your SPI channel, or make your timer fire more slowly.  Or both. Otherwise, by design your system can't meet the stated deadline. If you cannot do either, you really do need to redesign your system. But then, your bottleneck is not the HCLK frequency, it's the SPI frequency.

 

As long as you ensure (by design) that the SPI transfer completes well before the next timer event, you should have no overruns. So just verify that the transfer has completed at the start of the callback and you should be good.

 

You should try to get as much timing margin as possible. For various reasons the transfer duration is not completely deterministic, so you need to design conservatively. Try for 2x (say, to get SPI completed in 8us) or less if you can. At the very worst case, I would not feel comfortable with less than  10-15% of slack.

 

It would be wise to measure the transfer duration accurately with a logic analyzer (you could do it in software, but that would introduce more overhead to your already-borderline design).

- If you feel a post has answered your question, please click "Accept as Solution".
- Once you've solved your issue, please consider posting a summary with any additional details you've learned. Your new knowledge may help others in the future.
BarryWhit
Senior

I've been on this forum a few months now, and it's getting to be very annoying that people post their question, get a response and then just disappear without a word or, even worse just disappear. So that anyone tempted to respond with advice is forced to wonder whether it's be a total waste of time and effort.

 

I now feel very guilty about the handful of times I've committed this very crime over the years, elsewhere.

Thank you. Thank you, avatar-clad strangers,  for this moral education you are providing me with.

- If you feel a post has answered your question, please click "Accept as Solution".
- Once you've solved your issue, please consider posting a summary with any additional details you've learned. Your new knowledge may help others in the future.