2021-02-25 07:49 AM
Hi everyone,
I am working with some interrupt stuff and the interrupt needs to be fast. I need at least "-O2" optimization in gcc. However, setting the whole project to O2 is not a good solution as debugging becomes quite hard.
I found two possibilities to tell gcc a custom optimization setting for a particular function:
Add the optimize attribute:
void __attribute__ ((optimize("-O2"))) EXTI4_15_IRQHandler(void)
{
// ...
}
Or use the pragma command:
#pragma GCC push_options
#pragma GCC optimize ("-O2")
void EXTI4_15_IRQHandler(void)
{
// ...
}
#pragma GCC pop_options
Sadly both methods have no effect on the optimization.
I also tried this with a normal function, in case it is something special about the interrupt, but it also did not work.
Any suggestions?
2021-02-25 08:33 AM
> Sadly both methods have no effect on the optimization.
How do you know?
If you use Cube/HAL, it's likely that the bulk of the interrupt is *not* the EXTI4_15_IRQHandler() function, but whatever function is called from there. And then you may need to optimize that function (and all other functions called from there), too.
JW
2021-02-25 08:38 AM
I am using CubeMX, but not the HAL, everything is using LL.
The startup assembler file directly defined EXTI4_15_IRQHandler() as a weak function and puts it in the interrupt vector table, so I guess there is no other code anywhere.
Additonally, as I said, I also tried it with a seperate function which contains a simple while(1) toggling a pin. The toggle frequency changes significantly when the optimization is tuned on globally, but it does not with either of the mentioned methods.
2021-02-25 10:38 AM
> The startup assembler file directly defined EXTI4_15_IRQHandler() as a weak function and puts it in the interrupt vector table, so I guess there is no other code anywhere.
And you are trying to optimize that weak function? I guess not. I guess you've written your EXTI4_15_IRQHandler().
Show the source and disasm for both optimized and non-optimized code.
JW
2021-02-25 11:18 AM
I am not trying to optimize that weak function, as I wrote my own definition, which overrides the weak one.
Nevertheless, I think I figured it out myself. The "Low Level" driver is not as low as I thought it would be. The functions are not inlined and therefore not optimized. Thats why I don't see any (significant) difference. It worked in case of the while(1)-pin-toggle once I implemented the toggle myself directly in my own function without using calls to LL_GPIO_* but direct register access instead.
Thanks a lot anyway! But looking at the assembler was the right tip to bring me forward :)
2021-02-26 02:29 PM
@Fabian2 adding __attribute__ ((optimize("-O2"))) is working on my side based on STM32CubeIDE 1.6.0 at least
2021-03-06 03:25 AM
@Cartu38 OpenDev Including other non-optimized, non-inlined functions?
2021-03-08 05:14 AM
Yes.
I've played with sysmem.c file adding extra optimization moving from 'void *_sbrk(ptrdiff_t incr)' to '__attribute__ ((optimize("-O2"))) void *_sbrk(ptrdiff_t incr)'
Works for me moving from 108b to 68b size contribution.
2021-03-08 08:15 AM
I don't see why other functions than the one with the flag should be optimized. It did not do that in my case.
But my problem is solved anyway by putting everything in the optimized function.